Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13
fix(profiler): eliminate SIGSEGV race on _class_map.clear() in signal handler and dump path#516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jbachorik
merged 8 commits into
muse/sigsegv-in-recording-writeclasses
from
muse/sigsegv-in-profiler-dumpMay 12, 2026
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4da7f45
refactor(profiler): RAII factory methods for classMap lock; fix use-a…
jbachorik aa81372
address(spinlock): restore tryLockShared() spinning semantics; add bo…
jbachorik 9876873
fix(spinlock): OptionalSharedLockGuard must call tryLockSharedBounded…
Copilot c7496a4
address(spinlock): separate bounded and unbounded optional shared guards
jbachorik 9c31897
fix(spinlock): correct tryLockShared() comment
jbachorik 7df7230
test(spinlock): add BoundedOptionalSharedLockGuard and tryLockSharedB…
jbachorik 45454fd
test(dictionary): cover both OptionalSharedLockGuard and BoundedOptio…
jbachorik f91271c
refactor(spinlock): drop unused move ctors; make all guards uniformly…
jbachorik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -47,11 +47,30 @@ class alignas(DEFAULT_CACHE_LINE_SIZE) SpinLock { | ||
| void unlock() { __sync_fetch_and_sub(&_lock, 1); } | ||
| bool tryLockShared() { | ||
| // Spins while no exclusive lock is held and the CAS to acquire a shared | ||
| // lock fails (due to concurrent reader contention). Returns false ONLY when | ||
| // an exclusive lock is observed (_lock > 0); never returns false spuriously. | ||
| int value; | ||
| while ((value = __atomic_load_n(&_lock, __ATOMIC_ACQUIRE)) <= 0) { | ||
| if (__sync_bool_compare_and_swap(&_lock, value, value - 1)) { | ||
| return true; | ||
| } | ||
| spinPause(); | ||
| } | ||
| return false; | ||
| } | ||
| // Signal-safe variant: returns false after at most 5 CAS attempts. | ||
| // Use only in signal-handler paths where spinning indefinitely is unsafe. | ||
| bool tryLockSharedBounded() { | ||
| for (int attempts = 0; attempts < 5; ++attempts) { | ||
| int value = __atomic_load_n(&_lock, __ATOMIC_ACQUIRE); | ||
| if (value > 0) { | ||
| return false; | ||
| } | ||
jbachorik marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (__sync_bool_compare_and_swap(&_lock, value, value - 1)) { | ||
| return true; | ||
| } | ||
jbachorik marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| return false; | ||
| } | ||
| @@ -88,9 +107,9 @@ class SharedLockGuard { | ||
| class OptionalSharedLockGuard { | ||
| SpinLock* _lock; | ||
| public: | ||
| OptionalSharedLockGuard(SpinLock* lock) : _lock(lock) { | ||
| explicit OptionalSharedLockGuard(SpinLock* lock) : _lock(lock) { | ||
| if (!_lock->tryLockShared()) { | ||
| // Locking failed, no need to unlock. | ||
| // Exclusive lock is held; no unlock needed. Only fails when an exclusive lock is observed. | ||
| _lock = nullptr; | ||
| } | ||
| } | ||
| @@ -108,6 +127,33 @@ class OptionalSharedLockGuard { | ||
| OptionalSharedLockGuard& operator=(OptionalSharedLockGuard&&) = delete; | ||
| }; | ||
| // Signal-safe variant of OptionalSharedLockGuard: uses bounded CAS retries | ||
| // and may fail spuriously when racing other shared lockers. Use ONLY in | ||
| // signal-handler paths where spinning indefinitely is unsafe; do NOT use | ||
| // in hot recording paths where silent acquisition failures would drop events. | ||
| class BoundedOptionalSharedLockGuard { | ||
| SpinLock* _lock; | ||
| public: | ||
| explicit BoundedOptionalSharedLockGuard(SpinLock* lock) : _lock(lock) { | ||
| if (!_lock->tryLockSharedBounded()) { | ||
| // Locking failed (bounded retries exhausted or exclusive lock held); no unlock needed. | ||
| _lock = nullptr; | ||
| } | ||
| } | ||
| ~BoundedOptionalSharedLockGuard() { | ||
| if (_lock != nullptr) { | ||
| _lock->unlockShared(); | ||
| } | ||
| } | ||
| bool ownsLock() { return _lock != nullptr; } | ||
| // Non-copyable and non-movable | ||
| BoundedOptionalSharedLockGuard(const BoundedOptionalSharedLockGuard&) = delete; | ||
| BoundedOptionalSharedLockGuard& operator=(const BoundedOptionalSharedLockGuard&) = delete; | ||
| BoundedOptionalSharedLockGuard(BoundedOptionalSharedLockGuard&&) = delete; | ||
| BoundedOptionalSharedLockGuard& operator=(BoundedOptionalSharedLockGuard&&) = delete; | ||
| }; | ||
| class ExclusiveLockGuard { | ||
| private: | ||
| SpinLock* _lock; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| * Copyright 2026, Datadog, Inc. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| #include <gtest/gtest.h> | ||
| #include <atomic> | ||
| #include <chrono> | ||
| #include <thread> | ||
| #include <vector> | ||
| #include "spinLock.h" | ||
| #include "../../main/cpp/gtest_crash_handler.h" | ||
| // Unit tests for tryLockSharedBounded() and BoundedOptionalSharedLockGuard — | ||
| // the signal-safe locking API introduced to replace unbounded spinning in | ||
| // hotspotSupport.cpp (classMapTrySharedGuard()). | ||
| static constexpr char SPINLOCK_BOUNDED_TEST_NAME[] = "SpinLockBounded"; | ||
| namespace { | ||
| class SpinLockBoundedSetup { | ||
| public: | ||
| SpinLockBoundedSetup() { | ||
| installGtestCrashHandler<SPINLOCK_BOUNDED_TEST_NAME>(); | ||
| } | ||
| ~SpinLockBoundedSetup() { | ||
| restoreDefaultSignalHandlers(); | ||
| } | ||
| }; | ||
| static SpinLockBoundedSetup spinlock_bounded_global_setup; | ||
| } // namespace | ||
| TEST(SpinLockBounded, BoundedTryLockSucceedsOnFreeLock) { | ||
| SpinLock lock; | ||
| EXPECT_TRUE(lock.tryLockSharedBounded()); | ||
| lock.unlockShared(); | ||
| } | ||
| TEST(SpinLockBounded, BoundedTryLockFailsWhenExclusiveLockHeld) { | ||
| SpinLock lock; | ||
| lock.lock(); | ||
| EXPECT_FALSE(lock.tryLockSharedBounded()); | ||
| lock.unlock(); | ||
| } | ||
| // Multiple shared holders must coexist — bounded acquire must not treat a | ||
| // concurrent reader's negative _lock value as an exclusive lock. | ||
| TEST(SpinLockBounded, BoundedTryLockAllowsMultipleSharedHolders) { | ||
| SpinLock lock; | ||
| ASSERT_TRUE(lock.tryLockSharedBounded()); | ||
| EXPECT_TRUE(lock.tryLockSharedBounded()); | ||
| lock.unlockShared(); | ||
| lock.unlockShared(); | ||
| } | ||
| TEST(SpinLockBounded, BoundedGuardOwnsLockWhenFree) { | ||
| SpinLock lock; | ||
| BoundedOptionalSharedLockGuard guard(&lock); | ||
| EXPECT_TRUE(guard.ownsLock()); | ||
| } | ||
| // When the exclusive lock is held the guard must not own the lock, and its | ||
| // destructor must not accidentally release the exclusive lock. | ||
| TEST(SpinLockBounded, BoundedGuardFailsWhenExclusiveLockHeld) { | ||
| SpinLock lock; | ||
| lock.lock(); | ||
| { | ||
| BoundedOptionalSharedLockGuard guard(&lock); | ||
| EXPECT_FALSE(guard.ownsLock()); | ||
| } | ||
| // Exclusive lock must still be held — unlock() must succeed exactly once. | ||
| lock.unlock(); | ||
| } | ||
| // After the guard goes out of scope the lock must be fully released so that an | ||
| // exclusive acquire succeeds. | ||
| TEST(SpinLockBounded, BoundedGuardReleasesLockOnDestruction) { | ||
| SpinLock lock; | ||
| { | ||
| BoundedOptionalSharedLockGuard guard(&lock); | ||
| ASSERT_TRUE(guard.ownsLock()); | ||
| } | ||
| EXPECT_TRUE(lock.tryLock()); | ||
| lock.unlock(); | ||
| } | ||
| // Stress: concurrent BoundedOptionalSharedLockGuard readers vs an exclusive | ||
| // locker. Mirrors the classMapTrySharedGuard() signal-handler path vs the | ||
| // Profiler::dump path, using the RAII type that hotspotSupport.cpp now uses. | ||
| TEST(SpinLockBounded, BoundedGuardConcurrentWithExclusiveLock) { | ||
| SpinLock lock; | ||
| constexpr int kReaders = 4; | ||
| const auto kDuration = std::chrono::milliseconds(300); | ||
| std::atomic<bool> stop{false}; | ||
| std::atomic<long> total_acquired{0}; | ||
| std::atomic<long> total_skipped{0}; | ||
| std::atomic<long> total_exclusive{0}; | ||
| std::vector<std::thread> readers; | ||
| readers.reserve(kReaders); | ||
| for (int i = 0; i < kReaders; ++i) { | ||
| readers.emplace_back([&]() { | ||
| while (!stop.load(std::memory_order_relaxed)) { | ||
| BoundedOptionalSharedLockGuard guard(&lock); | ||
| if (guard.ownsLock()) { | ||
| total_acquired.fetch_add(1, std::memory_order_relaxed); | ||
| } else { | ||
| total_skipped.fetch_add(1, std::memory_order_relaxed); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| std::thread exclusive_thread([&]() { | ||
| while (!stop.load(std::memory_order_relaxed)) { | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(5)); | ||
| lock.lock(); | ||
| lock.unlock(); | ||
| total_exclusive.fetch_add(1, std::memory_order_relaxed); | ||
| } | ||
| }); | ||
| std::this_thread::sleep_for(kDuration); | ||
| stop.store(true, std::memory_order_relaxed); | ||
| for (auto& t : readers) t.join(); | ||
| exclusive_thread.join(); | ||
| EXPECT_GT(total_acquired.load(), 0L); | ||
| EXPECT_GT(total_exclusive.load(), 0L); | ||
| } |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.