Skip to content

Commit

Permalink
[sanitizer] Remove GetCurrentThread nullness checks from Allocate
Browse files Browse the repository at this point in the history
The nullness check is unreachable.

* For the main thead and pthread_create created threads, the `*Allocate` functions must be called after `*_current_thread` is set.
set.
* For threads created by Linux's `clone`, static TLS is either reused or
  set to a new value (CLONE_SETTLS).

Make this change for asan/msan and possibly extend the change to other
sanitizers. (asan supports many platforms and I am not 100% certain that
all platforms have the property.)

Pull Request: llvm#102828
  • Loading branch information
MaskRay authored Aug 15, 2024
1 parent 5873aa8 commit 4411d1e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 18 deletions.
11 changes: 2 additions & 9 deletions compiler-rt/lib/asan/asan_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,15 +576,8 @@ struct Allocator {
}

AsanThread *t = GetCurrentThread();
void *allocated;
if (t) {
AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
allocated = allocator.Allocate(cache, needed_size, 8);
} else {
SpinMutexLock l(&fallback_mutex);
AllocatorCache *cache = &fallback_allocator_cache;
allocated = allocator.Allocate(cache, needed_size, 8);
}
void *allocated = allocator.Allocate(
GetAllocatorCache(&t->malloc_storage()), needed_size, 8);
if (UNLIKELY(!allocated)) {
SetAllocatorOutOfMemory();
if (AllocatorMayReturnNull())
Expand Down
11 changes: 2 additions & 9 deletions compiler-rt/lib/msan/msan_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,8 @@ static void *MsanAllocate(BufferedStackTrace *stack, uptr size, uptr alignment,
ReportRssLimitExceeded(stack);
}
MsanThread *t = GetCurrentThread();
void *allocated;
if (t) {
AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
allocated = allocator.Allocate(cache, size, alignment);
} else {
SpinMutexLock l(&fallback_mutex);
AllocatorCache *cache = &fallback_allocator_cache;
allocated = allocator.Allocate(cache, size, alignment);
}
void *allocated = allocator.Allocate(GetAllocatorCache(&t->malloc_storage()),
size, alignment);
if (UNLIKELY(!allocated)) {
SetAllocatorOutOfMemory();
if (AllocatorMayReturnNull())
Expand Down

0 comments on commit 4411d1e

Please sign in to comment.