Skip to content

[compiler-rt][asan] Fix for flaky asan check #88177

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
merged 1 commit into from
Apr 9, 2024

Conversation

PiJoules
Copy link
Contributor

@PiJoules PiJoules commented Apr 9, 2024

This fixes #87324.

We haven't been able to come up with a minimal reproducer but we can reliabely avoid this failure with the following fix. Prior to the GetGlobalLowLevelAllocator change, the old LowLevelAllocator aquired a lock associated with it preventing that specific allocator from being accessed at the same time by many threads. With the GetGlobalLowLevelAllocator change, I had accidentally replaced it but not taken into account the lock, so we can have a data race if the allocator is used at any point while a thread is being created. The global allocator can be used for flag parsing or registering asan globals.

@PiJoules PiJoules marked this pull request as ready for review April 9, 2024 18:56
@PiJoules PiJoules requested a review from vitalybuka April 9, 2024 18:57
@llvmbot
Copy link
Member

llvmbot commented Apr 9, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: None (PiJoules)

Changes

This fixes #87324.

We haven't been able to come up with a minimal reproducer but we can reliabely avoid this failure with the following fix. Prior to the GetGlobalLowLevelAllocator change, the old LowLevelAllocator aquired a lock associated with it preventing that specific allocator from being accessed at the same time by many threads. With the GetGlobalLowLevelAllocator change, I had accidentally replaced it but not taken into account the lock, so we can have a data race if the allocator is used at any point while a thread is being created. The global allocator can be used for flag parsing or registering asan globals.


Full diff: https://github.com/llvm/llvm-project/pull/88177.diff

1 Files Affected:

  • (modified) compiler-rt/lib/asan/asan_thread.cpp (+2-1)
diff --git a/compiler-rt/lib/asan/asan_thread.cpp b/compiler-rt/lib/asan/asan_thread.cpp
index 8798968947e82e..95aeccd070b551 100644
--- a/compiler-rt/lib/asan/asan_thread.cpp
+++ b/compiler-rt/lib/asan/asan_thread.cpp
@@ -44,10 +44,11 @@ static ThreadRegistry *asan_thread_registry;
 static ThreadArgRetval *thread_data;
 
 static Mutex mu_for_thread_context;
+static LowLevelAllocator allocator_for_thread_context;
 
 static ThreadContextBase *GetAsanThreadContext(u32 tid) {
   Lock lock(&mu_for_thread_context);
-  return new (GetGlobalLowLevelAllocator()) AsanThreadContext(tid);
+  return new (allocator_for_thread_context) AsanThreadContext(tid);
 }
 
 static void InitThreads() {

@PiJoules
Copy link
Contributor Author

PiJoules commented Apr 9, 2024

cc @landell-xperi

@vitalybuka
Copy link
Collaborator

vitalybuka commented Apr 9, 2024

On https://reviews.llvm.org/D158786 I didn't realized that it's not thread safe, it should be easy to make LowLevelAllocator thread safe.

Feel free just to add TODO, or we can do thread safe in this patch?

This is approx idea how this could be done
https://github.com/llvm/llvm-project/blob/6bf71be9f920417a772b123fd42ab855496ad149/compiler-rt/lib/sanitizer_common/sanitizer_stack_store.cpp

This fixes llvm#87324.

We haven't been able to come up with a minimal reproducer but we can
reliabely avoid this failure with the following fix. Prior to the
GetGlobalLowLevelAllocator change, the old LowLevelAllocator aquired a
lock associated with it preventing that specific allocator from being
accessed at the same time by many threads. With the
GetGlobalLowLevelAllocator change, I had accidentally replaced it but
not taken into account the lock, so we can have a data race if the
allocator is used at any point while a thread is being created. The
global allocator can be used for flag parsing or registering asan
globals.
@PiJoules PiJoules force-pushed the fix-asan-assertion-flake branch from 249c313 to 3123d15 Compare April 9, 2024 22:37
@PiJoules
Copy link
Contributor Author

PiJoules commented Apr 9, 2024

On https://reviews.llvm.org/D158786 I didn't realized that it's not thread safe, it should be easy to make LowLevelAllocator thread safe.

Feel free just to add TODO, or we can do thread safe in this patch?

This is approx idea how this could be done https://github.com/llvm/llvm-project/blob/6bf71be9f920417a772b123fd42ab855496ad149/compiler-rt/lib/sanitizer_common/sanitizer_stack_store.cpp

Added just the TODO for now since we know this quick fix works but I'll followup with the threadsafe changes afterwards. Just want to make sure the libc tests this might affect for us downstream absorb this change well.

@PiJoules PiJoules merged commit 4a04fca into llvm:main Apr 9, 2024
@PiJoules PiJoules deleted the fix-asan-assertion-flake branch April 9, 2024 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CHECK failed: "((tctx->status)) == ((ThreadStatusInvalid))"
3 participants