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->Destroy(true); } 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,7 @@ static AsanThread *Create(thread_callback_t start_routine, void *arg, u32 parent_tid, StackTrace *stack, bool detached); static void TSDDtor(void *tsd); - void Destroy(); + void Destroy(bool create_failed = false); 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 @@ -96,22 +96,27 @@ context->thread->Destroy(); } -void AsanThread::Destroy() { +void AsanThread::Destroy(bool create_failed) { int tid = this->tid(); VReport(1, "T%d exited\n", tid); - malloc_storage().CommitBack(); - if (common_flags()->use_sigaltstack) UnsetAlternateSignalStack(); + if (!create_failed) { + malloc_storage().CommitBack(); + if (common_flags()->use_sigaltstack) + UnsetAlternateSignalStack(); + } asanThreadRegistry().FinishThread(tid); - FlushToDeadThreadStats(&stats_); - // We also clear the shadow on thread destruction because - // some code may still be executing in later TSD destructors - // and we don't want it to have any poisoned stack. - ClearShadowForThreadStackAndTLS(); - DeleteFakeStack(tid); + if (!create_failed) { + FlushToDeadThreadStats(&stats_); + // We also clear the shadow on thread destruction because + // some code may still be executing in later TSD destructors + // and we don't want it to have any poisoned stack. + ClearShadowForThreadStackAndTLS(); + DeleteFakeStack(tid); + DTLS_Destroy(); + } uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached()); UnmapOrDie(this, size); - DTLS_Destroy(); } void AsanThread::StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom, 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,29 @@ +// 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); + size_t sz = ~0; + 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; +}