Fix tsan warning about ExecutionCoverage::SetTracing() See: https://github.com/google/fuzztest/issues/1965 PiperOrigin-RevId: 862829024
diff --git a/fuzztest/internal/coverage.h b/fuzztest/internal/coverage.h index 8948d52..e908da8 100644 --- a/fuzztest/internal/coverage.h +++ b/fuzztest/internal/coverage.h
@@ -28,6 +28,7 @@ #define FUZZTEST_FUZZTEST_INTERNAL_COVERAGE_H_ #include <algorithm> +#include <atomic> #include <cstddef> #include <cstdint> #include <cstring> @@ -135,11 +136,13 @@ // Flag marking if the control flow is currently in target codes. // We don't want to collect unrelated updates to cmp score and dictionary. - bool IsTracing() { return is_tracing_; } + bool IsTracing() { return is_tracing_.load(std::memory_order_relaxed); } // Right before target run, call this method with true; right after // target run, call this method with false. - void SetIsTracing(bool is_tracing) { is_tracing_ = is_tracing; } + void SetIsTracing(bool is_tracing) { + is_tracing_.store(is_tracing, std::memory_order_relaxed); + } // Update the PC->max stack usage map for `PC`. // It compares the current stack frame against the stack frame specified in @@ -190,7 +193,7 @@ std::atomic<bool> new_coverage_{false}; TablesOfRecentCompares tables_of_recent_compares_ = {}; - bool is_tracing_ = false; + std::atomic<bool> is_tracing_{false}; }; // Set the singleton ExecutionCoverage object.