From 21a54bce88b3edf76a9e7c74eaab76854228f63a Mon Sep 17 00:00:00 2001 From: Xinhao Yuan Date: Mon, 21 Sep 2026 07:28:15 -0700 Subject: [PATCH] Make OnThread{Start,Stop} idempotent. This allows multiple callers to call OnThread{Start,Stop} safely without coordination, which may be needed (or making things easier) on some platforms. PiperOrigin-RevId: 985263765 --- centipede/sancov_state.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/centipede/sancov_state.cc b/centipede/sancov_state.cc index 8adedeeaf..abcaffede 100644 --- a/centipede/sancov_state.cc +++ b/centipede/sancov_state.cc @@ -121,6 +121,7 @@ void ThreadLocalSancovState::TraceMemCmp(uintptr_t caller_pc, const uint8_t *s1, } void ThreadLocalSancovState::OnThreadStart() { + if (tls.started) return; termination_detector.EnsureAlive(); tls.started = true; // Always trace threads by default. Internal threads that do not want tracing @@ -145,6 +146,11 @@ void ThreadLocalSancovState::OnThreadStart() { } void ThreadLocalSancovState::OnThreadStop() { + if (tls.sancov_lowest_sp == nullptr) { + // This can happen only before OnTheadStart() or after OnThreadStop() - + // nothing to do if that's the case. + return; + } tls.traced = false; LockGuard lock(sancov_state->tls_list_mu); const size_t sancov_lowest_sp = *tls.sancov_lowest_sp;