diff --git a/compiler-rt/lib/asan/asan_interceptors.cpp b/compiler-rt/lib/asan/asan_interceptors.cpp --- a/compiler-rt/lib/asan/asan_interceptors.cpp +++ b/compiler-rt/lib/asan/asan_interceptors.cpp @@ -226,7 +226,7 @@ // If the thread didn't start delete the AsanThread to avoid leaking it. // Note AsanThreadContexts never get destroyed so the AsanThreadContext // that was just created for the AsanThread is wasted. - t->Destroy(); + t->DestroyFailed(); } return result; } diff --git a/compiler-rt/lib/asan/asan_thread.h b/compiler-rt/lib/asan/asan_thread.h --- a/compiler-rt/lib/asan/asan_thread.h +++ b/compiler-rt/lib/asan/asan_thread.h @@ -64,7 +64,10 @@ static AsanThread *Create(thread_callback_t start_routine, void *arg, u32 parent_tid, StackTrace *stack, bool detached); static void TSDDtor(void *tsd); + // Destroys the terminating thread. void Destroy(); + // Destroys the thread failed to start. + void DestroyFailed(); struct InitOptions; void Init(const InitOptions *options = nullptr); diff --git a/compiler-rt/lib/asan/asan_thread.cpp b/compiler-rt/lib/asan/asan_thread.cpp --- a/compiler-rt/lib/asan/asan_thread.cpp +++ b/compiler-rt/lib/asan/asan_thread.cpp @@ -114,6 +114,16 @@ DTLS_Destroy(); } +void AsanThread::DestroyFailed() { + int tid = this->tid(); + VReport(1, "T%d not started\n", tid); + CHECK_NE(this, GetCurrentThread()); + + asanThreadRegistry().FinishThread(tid); + uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached()); + UnmapOrDie(this, size); +} + void AsanThread::StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom, uptr size) { if (atomic_load(&stack_switching_, memory_order_relaxed)) { diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/create_thread_fail.cpp b/compiler-rt/test/sanitizer_common/TestCases/Posix/create_thread_fail.cpp new file mode 100644 --- /dev/null +++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/create_thread_fail.cpp @@ -0,0 +1,31 @@ +// Sanitizer should not crash if pthread_create fails. +// RUN: %clangxx -pthread %s -o %t && %run %t + +// pthread_create with lsan i386 does not fail here. +// UNSUPPORTED: i386-linux && lsan + +#include +#include +#include + +void *null_func(void *args) { + return NULL; +} + +int main(void) { + pthread_t thread; + pthread_attr_t attrs; + pthread_attr_init(&attrs); + // Set size huge enough to fail pthread_create. + size_t sz = ~0; + // Align the size just in case. + sz >>= 16; + sz <<= 16; + int res = pthread_attr_setstacksize(&attrs, sz); + assert(res == 0); + for (size_t i = 0; i < 10; ++i) { + res = pthread_create(&thread, &attrs, null_func, NULL); + assert(res != 0); + } + return 0; +}