From 029b410df31afd6ea1aee9da6949c84b6db19112 Mon Sep 17 00:00:00 2001 From: Kirill Stoimenov Date: Thu, 2 Feb 2023 01:32:16 +0000 Subject: [PATCH] [HWASAN] Set os_id in Thread::Init to make sure that the thread can be found by GetThreadByOsIDLocked. Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D143125 --- compiler-rt/lib/hwasan/hwasan_interceptors.cpp | 1 + compiler-rt/lib/hwasan/hwasan_thread.cpp | 14 +++++++++----- compiler-rt/lib/hwasan/hwasan_thread.h | 3 +++ .../test/lsan/TestCases/Linux/cleanup_in_tsd_destructor.c | 3 --- compiler-rt/test/lsan/TestCases/Linux/fork_threaded.cpp | 3 --- compiler-rt/test/lsan/TestCases/Linux/guard-page.c | 3 --- compiler-rt/test/lsan/TestCases/Linux/libdl_deadlock.cpp | 3 --- compiler-rt/test/lsan/TestCases/many_tls_keys_pthread.cpp | 5 +---- compiler-rt/test/lsan/TestCases/many_tls_keys_thread.cpp | 5 +---- compiler-rt/test/lsan/TestCases/use_registers.cpp | 5 +---- compiler-rt/test/lsan/TestCases/use_registers_extra.cpp | 5 +---- compiler-rt/test/lsan/TestCases/use_stacks_threaded.cpp | 5 +---- 12 files changed, 18 insertions(+), 37 deletions(-) diff --git a/compiler-rt/lib/hwasan/hwasan_interceptors.cpp b/compiler-rt/lib/hwasan/hwasan_interceptors.cpp index 05bf3f2..c1ed2cd 100644 --- a/compiler-rt/lib/hwasan/hwasan_interceptors.cpp +++ b/compiler-rt/lib/hwasan/hwasan_interceptors.cpp @@ -39,6 +39,7 @@ static void *HwasanThreadStartFunc(void *arg) { INTERCEPTOR(int, pthread_create, void *th, void *attr, void *(*callback)(void*), void * param) { + EnsureMainThreadIDIsCorrect(); ScopedTaggingDisabler disabler; ThreadStartArg *A = reinterpret_cast (MmapOrDie( GetPageSizeCached(), "pthread_create")); diff --git a/compiler-rt/lib/hwasan/hwasan_thread.cpp b/compiler-rt/lib/hwasan/hwasan_thread.cpp index 4a78f60..3375782 100644 --- a/compiler-rt/lib/hwasan/hwasan_thread.cpp +++ b/compiler-rt/lib/hwasan/hwasan_thread.cpp @@ -44,6 +44,8 @@ void Thread::Init(uptr stack_buffer_start, uptr stack_buffer_size, static atomic_uint64_t unique_id; unique_id_ = atomic_fetch_add(&unique_id, 1, memory_order_relaxed); + if (!IsMainThread()) + os_id_ = GetTid(); if (auto sz = flags()->heap_history_size) heap_allocations_ = HeapAllocationsRingBuffer::New(sz); @@ -149,6 +151,12 @@ tag_t Thread::GenerateRandomTag(uptr num_bits) { return tag; } +void EnsureMainThreadIDIsCorrect() { + auto *t = __hwasan::GetCurrentThread(); + if (t && (t->IsMainThread())) + t->set_os_id(GetTid()); +} + } // namespace __hwasan // --- Implementation of LSan-specific functions --- {{{1 @@ -169,11 +177,7 @@ void LockThreadRegistry() { __hwasan::hwasanThreadList().Lock(); } void UnlockThreadRegistry() { __hwasan::hwasanThreadList().Unlock(); } -void EnsureMainThreadIDIsCorrect() { - auto *t = __hwasan::GetCurrentThread(); - if (t && (t->IsMainThread())) - t->set_os_id(GetTid()); -} +void EnsureMainThreadIDIsCorrect() { __hwasan::EnsureMainThreadIDIsCorrect(); } bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end, uptr *tls_begin, uptr *tls_end, uptr *cache_begin, diff --git a/compiler-rt/lib/hwasan/hwasan_thread.h b/compiler-rt/lib/hwasan/hwasan_thread.h index 9727585..9e1b438 100644 --- a/compiler-rt/lib/hwasan/hwasan_thread.h +++ b/compiler-rt/lib/hwasan/hwasan_thread.h @@ -110,6 +110,9 @@ class Thread { Thread *GetCurrentThread(); uptr *GetCurrentThreadLongPtr(); +// Used to handle fork(). +void EnsureMainThreadIDIsCorrect(); + struct ScopedTaggingDisabler { ScopedTaggingDisabler() { GetCurrentThread()->DisableTagging(); } ~ScopedTaggingDisabler() { GetCurrentThread()->EnableTagging(); } diff --git a/compiler-rt/test/lsan/TestCases/Linux/cleanup_in_tsd_destructor.c b/compiler-rt/test/lsan/TestCases/Linux/cleanup_in_tsd_destructor.c index 29f6a48..00a0ec3 100644 --- a/compiler-rt/test/lsan/TestCases/Linux/cleanup_in_tsd_destructor.c +++ b/compiler-rt/test/lsan/TestCases/Linux/cleanup_in_tsd_destructor.c @@ -7,9 +7,6 @@ // RUN: %env_lsan_opts="report_objects=1:use_registers=0:use_stacks=0:use_tls=1" %run %t // RUN: %env_lsan_opts="report_objects=1:use_registers=0:use_stacks=0:use_tls=0" not %run %t 2>&1 | FileCheck %s -// Fixme: remove once test passes with hwasan -// UNSUPPORTED: hwasan - // Investigate why it does not fail with use_stack=0 // UNSUPPORTED: arm-linux || armhf-linux diff --git a/compiler-rt/test/lsan/TestCases/Linux/fork_threaded.cpp b/compiler-rt/test/lsan/TestCases/Linux/fork_threaded.cpp index b80cfee..62702b4 100644 --- a/compiler-rt/test/lsan/TestCases/Linux/fork_threaded.cpp +++ b/compiler-rt/test/lsan/TestCases/Linux/fork_threaded.cpp @@ -3,9 +3,6 @@ // RUN: %clangxx_lsan %s -o %t // RUN: %run %t 2>&1 -// Fixme: remove once test passes with hwasan -// UNSUPPORTED: hwasan - #include #include #include diff --git a/compiler-rt/test/lsan/TestCases/Linux/guard-page.c b/compiler-rt/test/lsan/TestCases/Linux/guard-page.c index dd2a581..ba03c1f 100644 --- a/compiler-rt/test/lsan/TestCases/Linux/guard-page.c +++ b/compiler-rt/test/lsan/TestCases/Linux/guard-page.c @@ -1,9 +1,6 @@ // Check that if LSan finds that SP doesn't point into thread stack (e.g. // if swapcontext is used), LSan will not hit the guard page. // RUN: %clang_lsan %s -o %t && %run %t - -// Fixme: remove once test passes with hwasan -// UNSUPPORTED: hwasan // Missing 'getcontext' and 'makecontext' on Android. // UNSUPPORTED: android diff --git a/compiler-rt/test/lsan/TestCases/Linux/libdl_deadlock.cpp b/compiler-rt/test/lsan/TestCases/Linux/libdl_deadlock.cpp index ddc9d88..ad7abe2 100644 --- a/compiler-rt/test/lsan/TestCases/Linux/libdl_deadlock.cpp +++ b/compiler-rt/test/lsan/TestCases/Linux/libdl_deadlock.cpp @@ -2,9 +2,6 @@ // where lsan would call dl_iterate_phdr while holding the allocator lock. // RUN: %clangxx_lsan %s -o %t && %run %t -// Fixme: remove once test passes with hwasan -// UNSUPPORTED: hwasan - #include #include #include diff --git a/compiler-rt/test/lsan/TestCases/many_tls_keys_pthread.cpp b/compiler-rt/test/lsan/TestCases/many_tls_keys_pthread.cpp index 86a2839..b67550f 100644 --- a/compiler-rt/test/lsan/TestCases/many_tls_keys_pthread.cpp +++ b/compiler-rt/test/lsan/TestCases/many_tls_keys_pthread.cpp @@ -4,9 +4,6 @@ // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_tls=1" %run %t 2>&1 // RUN: %env_lsan_opts="" %run %t 2>&1 -// Fixme: remove once test passes with hwasan -// UNSUPPORTED: hwasan - // On glibc, this requires the range returned by GetTLS to include // specific_1stblock and specific in `struct pthread`. // UNSUPPORTED: arm-linux, armhf-linux @@ -77,4 +74,4 @@ int main() { } // CHECK: LeakSanitizer: detected memory leaks -// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer: +// CHECK: SUMMARY: {{.*}}Sanitizer: diff --git a/compiler-rt/test/lsan/TestCases/many_tls_keys_thread.cpp b/compiler-rt/test/lsan/TestCases/many_tls_keys_thread.cpp index dac1363..f0deb44 100644 --- a/compiler-rt/test/lsan/TestCases/many_tls_keys_thread.cpp +++ b/compiler-rt/test/lsan/TestCases/many_tls_keys_thread.cpp @@ -4,9 +4,6 @@ // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_tls=1" %run %t 2>&1 // RUN: %env_lsan_opts="" %run %t 2>&1 -// Fixme: remove once test passes with hwasan -// UNSUPPORTED: hwasan - // Patch r303906 did not fix all the problems. // UNSUPPORTED: arm-linux,armhf-linux @@ -60,4 +57,4 @@ int main() { } // CHECK: LeakSanitizer: detected memory leaks -// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer: +// CHECK: SUMMARY: {{.*}}Sanitizer: diff --git a/compiler-rt/test/lsan/TestCases/use_registers.cpp b/compiler-rt/test/lsan/TestCases/use_registers.cpp index 69ac4b1..a4fbf27 100644 --- a/compiler-rt/test/lsan/TestCases/use_registers.cpp +++ b/compiler-rt/test/lsan/TestCases/use_registers.cpp @@ -4,9 +4,6 @@ // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=1" %run %t 2>&1 // RUN: %env_lsan_opts="" %run %t 2>&1 -// Fixme: remove once test passes with hwasan -// UNSUPPORTED: hwasan - #include "sanitizer_common/print_address.h" #include #include @@ -80,4 +77,4 @@ int main() { // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]] // CHECK: LeakSanitizer: detected memory leaks // CHECK: [[ADDR]] (1337 bytes) -// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer: +// CHECK: SUMMARY: {{.*}}Sanitizer: diff --git a/compiler-rt/test/lsan/TestCases/use_registers_extra.cpp b/compiler-rt/test/lsan/TestCases/use_registers_extra.cpp index ffeffc4..0a423df 100644 --- a/compiler-rt/test/lsan/TestCases/use_registers_extra.cpp +++ b/compiler-rt/test/lsan/TestCases/use_registers_extra.cpp @@ -4,9 +4,6 @@ // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=1" %run %t 2>&1 // RUN: %env_lsan_opts="" %run %t 2>&1 -// Fixme: remove once test passes with hwasan -// UNSUPPORTED: hwasan - // FIXME: Support more platforms. // REQUIRES: x86-target-arch && linux @@ -60,4 +57,4 @@ int main() { // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]] // CHECK: LeakSanitizer: detected memory leaks // CHECK: [[ADDR]] (1337 bytes) -// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer: +// CHECK: SUMMARY: {{.*}}Sanitizer: diff --git a/compiler-rt/test/lsan/TestCases/use_stacks_threaded.cpp b/compiler-rt/test/lsan/TestCases/use_stacks_threaded.cpp index 015070f..f1ad7a1 100644 --- a/compiler-rt/test/lsan/TestCases/use_stacks_threaded.cpp +++ b/compiler-rt/test/lsan/TestCases/use_stacks_threaded.cpp @@ -4,9 +4,6 @@ // RUN: %env_lsan_opts="report_objects=1:use_registers=0:use_stacks=1" %run %t 2>&1 // RUN: %env_lsan_opts="" %run %t 2>&1 -// Fixme: remove once test passes with hwasan -// UNSUPPORTED: hwasan - #include #include #include @@ -37,4 +34,4 @@ int main() { // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]] // CHECK: LeakSanitizer: detected memory leaks // CHECK: [[ADDR]] (1337 bytes) -// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer: +// CHECK: SUMMARY: {{.*}}Sanitizer: -- 2.7.4