Commit 6e586b48 authored by jameslahm's avatar jameslahm Committed by V8 LUCI CQ

[sampler] Fix data race in Sampler::DoSample

In Sampler::DoSample, we only guard SignalHandler::Installed before
and Sampler::Stop may happen at the same time, which may cause SIGPROF
signal handler was already restored before SIGPROF was emit and trigger
profiling timer expired. This CL changes Sampler::DoSample to use
SignalHandler::mutex() to guard the entire function and also change
the mutex to recursive mutex.

Bug: v8:12838
Change-Id: I5195742ecdbade342986755233840d7be5d83c62
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3616429Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Commit-Queue: 王澳 <wangao.james@bytedance.com>
Cr-Commit-Position: refs/heads/main@{#80308}
parent a42a2f41
......@@ -310,20 +310,23 @@ class Sampler::PlatformData {
class SignalHandler {
public:
static void IncreaseSamplerCount() {
base::MutexGuard lock_guard(mutex_.Pointer());
base::RecursiveMutexGuard lock_guard(mutex_.Pointer());
if (++client_count_ == 1) Install();
}
static void DecreaseSamplerCount() {
base::MutexGuard lock_guard(mutex_.Pointer());
base::RecursiveMutexGuard lock_guard(mutex_.Pointer());
if (--client_count_ == 0) Restore();
}
static bool Installed() {
base::MutexGuard lock_guard(mutex_.Pointer());
// mutex_ will also be used in Sampler::DoSample to guard the state below.
base::RecursiveMutexGuard lock_guard(mutex_.Pointer());
return signal_handler_installed_;
}
static v8::base::RecursiveMutex* mutex() { return mutex_.Pointer(); }
private:
static void Install() {
struct sigaction sa;
......@@ -349,13 +352,15 @@ class SignalHandler {
static void HandleProfilerSignal(int signal, siginfo_t* info, void* context);
// Protects the process wide state below.
static base::LazyMutex mutex_;
static base::LazyRecursiveMutex mutex_;
static int client_count_;
static bool signal_handler_installed_;
static struct sigaction old_signal_handler_;
};
base::LazyMutex SignalHandler::mutex_ = LAZY_MUTEX_INITIALIZER;
base::LazyRecursiveMutex SignalHandler::mutex_ =
LAZY_RECURSIVE_MUTEX_INITIALIZER;
int SignalHandler::client_count_ = 0;
struct sigaction SignalHandler::old_signal_handler_;
bool SignalHandler::signal_handler_installed_ = false;
......@@ -568,6 +573,7 @@ void Sampler::Stop() {
#if defined(USE_SIGNALS)
void Sampler::DoSample() {
base::RecursiveMutexGuard lock_guard(SignalHandler::mutex());
if (!SignalHandler::Installed()) return;
DCHECK(IsActive());
SetShouldRecordSample();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment