Skip to content

Commit 7d5e6b4

Browse files
committed
[HWASAN] Fix TLS + signal handling related crash
When a signal is raised before HWASAN has a chance to initialize it's TLS entry the program crashes. This only happens when hwasan-with-tls is true, which is default value. This patch fixes the problem by disabling signals during thread initialization time. Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D149085
1 parent c450515 commit 7d5e6b4

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

compiler-rt/lib/hwasan/hwasan_interceptors.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
// sanitizer_common/sanitizer_common_interceptors.h
1515
//===----------------------------------------------------------------------===//
1616

17-
#include "interception/interception.h"
1817
#include "hwasan.h"
1918
#include "hwasan_thread.h"
19+
#include "interception/interception.h"
20+
#include "sanitizer_common/sanitizer_linux.h"
2021
#include "sanitizer_common/sanitizer_stackdepot.h"
2122

2223
#if !SANITIZER_FUCHSIA
@@ -28,11 +29,13 @@ using namespace __hwasan;
2829
struct ThreadStartArg {
2930
thread_callback_t callback;
3031
void *param;
32+
__sanitizer_sigset_t starting_sigset_;
3133
};
3234

3335
static void *HwasanThreadStartFunc(void *arg) {
3436
__hwasan_thread_enter();
3537
ThreadStartArg A = *reinterpret_cast<ThreadStartArg*>(arg);
38+
SetSigProcMask(&A.starting_sigset_, nullptr);
3639
UnmapOrDie(arg, GetPageSizeCached());
3740
return A.callback(A.param);
3841
}
@@ -43,16 +46,14 @@ INTERCEPTOR(int, pthread_create, void *th, void *attr, void *(*callback)(void*),
4346
ScopedTaggingDisabler tagging_disabler;
4447
ThreadStartArg *A = reinterpret_cast<ThreadStartArg *> (MmapOrDie(
4548
GetPageSizeCached(), "pthread_create"));
46-
*A = {callback, param};
47-
int res;
48-
{
49-
// ASAN uses the same approach to disable leaks from pthread_create.
49+
A->callback = callback;
50+
A->param = param;
51+
ScopedBlockSignals block(&A->starting_sigset_);
52+
// ASAN uses the same approach to disable leaks from pthread_create.
5053
# if CAN_SANITIZE_LEAKS
51-
__lsan::ScopedInterceptorDisabler lsan_disabler;
54+
__lsan::ScopedInterceptorDisabler lsan_disabler;
5255
# endif
53-
res = REAL(pthread_create)(th, attr, &HwasanThreadStartFunc, A);
54-
}
55-
return res;
56+
return REAL(pthread_create)(th, attr, &HwasanThreadStartFunc, A);
5657
}
5758

5859
INTERCEPTOR(int, pthread_join, void *t, void **arg) {

0 commit comments

Comments
 (0)