Skip to content

Commit b7c9ebe

Browse files
authored
[sanitizer] Fix partially initialized static TLS range (#108685)
Fixes asan, msan crash on check added in #108684. The #108684 includes reproducer of the issue. Change interface of `GetThreadStackAndTls` to set `tls_begin` and `tls_end` at the same time.
1 parent 8f023ec commit b7c9ebe

File tree

16 files changed

+86
-120
lines changed

16 files changed

+86
-120
lines changed

compiler-rt/lib/asan/asan_posix.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ bool PlatformUnpoisonStacks() {
5959

6060
// Since we're on the signal alternate stack, we cannot find the DEFAULT
6161
// stack bottom using a local variable.
62-
uptr default_bottom, tls_addr, tls_size, stack_size;
63-
GetThreadStackAndTls(/*main=*/false, &default_bottom, &stack_size, &tls_addr,
64-
&tls_size);
65-
UnpoisonStack(default_bottom, default_bottom + stack_size, "default");
62+
uptr stack_begin, stack_end, tls_begin, tls_end;
63+
GetThreadStackAndTls(/*main=*/false, &stack_begin, &stack_end, &tls_begin,
64+
&tls_end);
65+
UnpoisonStack(stack_begin, stack_end, "default");
6666
return true;
6767
}
6868

compiler-rt/lib/asan/asan_rtl.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,10 +580,8 @@ static void UnpoisonDefaultStack() {
580580
} else {
581581
CHECK(!SANITIZER_FUCHSIA);
582582
// If we haven't seen this thread, try asking the OS for stack bounds.
583-
uptr tls_addr, tls_size, stack_size;
584-
GetThreadStackAndTls(/*main=*/false, &bottom, &stack_size, &tls_addr,
585-
&tls_size);
586-
top = bottom + stack_size;
583+
uptr tls_begin, tls_end;
584+
GetThreadStackAndTls(/*main=*/false, &bottom, &top, &tls_begin, &tls_end);
587585
}
588586

589587
UnpoisonStack(bottom, top, "default");

compiler-rt/lib/asan/asan_thread.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,10 @@ AsanThread *CreateMainThread() {
306306
// OS-specific implementations that need more information passed through.
307307
void AsanThread::SetThreadStackAndTls(const InitOptions *options) {
308308
DCHECK_EQ(options, nullptr);
309-
uptr tls_size = 0;
310-
uptr stack_size = 0;
311-
GetThreadStackAndTls(tid() == kMainTid, &stack_bottom_, &stack_size,
312-
&tls_begin_, &tls_size);
313-
stack_top_ = RoundDownTo(stack_bottom_ + stack_size, ASAN_SHADOW_GRANULARITY);
309+
GetThreadStackAndTls(tid() == kMainTid, &stack_bottom_, &stack_top_,
310+
&tls_begin_, &tls_end_);
311+
stack_top_ = RoundDownTo(stack_top_, ASAN_SHADOW_GRANULARITY);
314312
stack_bottom_ = RoundDownTo(stack_bottom_, ASAN_SHADOW_GRANULARITY);
315-
tls_end_ = tls_begin_ + tls_size;
316313
dtls_ = DTLS_Get();
317314

318315
if (stack_top_ != stack_bottom_) {

compiler-rt/lib/dfsan/dfsan_thread.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@ DFsanThread *DFsanThread::Create(thread_callback_t start_routine, void *arg,
2121
}
2222

2323
void DFsanThread::SetThreadStackAndTls() {
24-
uptr tls_size = 0;
25-
uptr stack_size = 0;
26-
GetThreadStackAndTls(IsMainThread(), &stack_.bottom, &stack_size, &tls_begin_,
27-
&tls_size);
28-
stack_.top = stack_.bottom + stack_size;
29-
tls_end_ = tls_begin_ + tls_size;
30-
24+
GetThreadStackAndTls(IsMainThread(), &stack_.bottom, &stack_.top, &tls_begin_,
25+
&tls_end_);
3126
int local;
3227
CHECK(AddrIsInStack((uptr)&local));
3328
}

compiler-rt/lib/hwasan/hwasan_linux.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -499,12 +499,8 @@ void HwasanOnDeadlySignal(int signo, void *info, void *context) {
499499
}
500500

501501
void Thread::InitStackAndTls(const InitState *) {
502-
uptr tls_size;
503-
uptr stack_size;
504-
GetThreadStackAndTls(IsMainThread(), &stack_bottom_, &stack_size, &tls_begin_,
505-
&tls_size);
506-
stack_top_ = stack_bottom_ + stack_size;
507-
tls_end_ = tls_begin_ + tls_size;
502+
GetThreadStackAndTls(IsMainThread(), &stack_bottom_, &stack_top_, &tls_begin_,
503+
&tls_end_);
508504
}
509505

510506
uptr TagMemoryAligned(uptr p, uptr size, tag_t tag) {

compiler-rt/lib/lsan/lsan_posix.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,8 @@ void ThreadContext::OnStarted(void *arg) {
5050

5151
void ThreadStart(u32 tid, tid_t os_id, ThreadType thread_type) {
5252
OnStartedArgs args;
53-
uptr stack_size = 0;
54-
uptr tls_size = 0;
55-
GetThreadStackAndTls(tid == kMainTid, &args.stack_begin, &stack_size,
56-
&args.tls_begin, &tls_size);
57-
args.stack_end = args.stack_begin + stack_size;
58-
args.tls_end = args.tls_begin + tls_size;
53+
GetThreadStackAndTls(tid == kMainTid, &args.stack_begin, &args.stack_end,
54+
&args.tls_begin, &args.tls_end);
5955
GetAllocatorCacheRange(&args.cache_begin, &args.cache_end);
6056
args.dtls = DTLS_Get();
6157
ThreadContextLsanBase::ThreadStart(tid, os_id, thread_type, &args);

compiler-rt/lib/memprof/memprof_thread.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,8 @@ MemprofThread *CreateMainThread() {
168168
// OS-specific implementations that need more information passed through.
169169
void MemprofThread::SetThreadStackAndTls(const InitOptions *options) {
170170
DCHECK_EQ(options, nullptr);
171-
uptr tls_size = 0;
172-
uptr stack_size = 0;
173-
GetThreadStackAndTls(tid() == kMainTid, &stack_bottom_, &stack_size,
174-
&tls_begin_, &tls_size);
175-
stack_top_ = stack_bottom_ + stack_size;
176-
tls_end_ = tls_begin_ + tls_size;
171+
GetThreadStackAndTls(tid() == kMainTid, &stack_bottom_, &stack_top_,
172+
&tls_begin_, &tls_end_);
177173
dtls_ = DTLS_Get();
178174

179175
if (stack_top_ != stack_bottom_) {

compiler-rt/lib/msan/msan_thread.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,8 @@ MsanThread *MsanThread::Create(thread_callback_t start_routine,
2020
}
2121

2222
void MsanThread::SetThreadStackAndTls() {
23-
uptr tls_size = 0;
24-
uptr stack_size = 0;
25-
GetThreadStackAndTls(IsMainThread(), &stack_.bottom, &stack_size, &tls_begin_,
26-
&tls_size);
27-
stack_.top = stack_.bottom + stack_size;
28-
tls_end_ = tls_begin_ + tls_size;
29-
23+
GetThreadStackAndTls(IsMainThread(), &stack_.bottom, &stack_.top, &tls_begin_,
24+
&tls_end_);
3025
int local;
3126
CHECK(AddrIsInStack((uptr)&local));
3227
}

compiler-rt/lib/nsan/nsan_thread.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,8 @@ NsanThread *NsanThread::Create(thread_callback_t start_routine, void *arg) {
2929
}
3030

3131
void NsanThread::SetThreadStackAndTls() {
32-
uptr tls_size = 0;
33-
uptr stack_size = 0;
34-
GetThreadStackAndTls(IsMainThread(), &stack_.bottom, &stack_size, &tls_begin_,
35-
&tls_size);
36-
stack_.top = stack_.bottom + stack_size;
37-
tls_end_ = tls_begin_ + tls_size;
38-
32+
GetThreadStackAndTls(IsMainThread(), &stack_.bottom, &stack_.top, &tls_begin_,
33+
&tls_end_);
3934
int local;
4035
CHECK(AddrIsInStack((uptr)&local));
4136
}

compiler-rt/lib/sanitizer_common/sanitizer_common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ int TgKill(pid_t pid, tid_t tid, int sig);
8383
uptr GetThreadSelf();
8484
void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
8585
uptr *stack_bottom);
86-
void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
87-
uptr *tls_addr, uptr *tls_size);
86+
void GetThreadStackAndTls(bool main, uptr *stk_begin, uptr *stk_end,
87+
uptr *tls_begin, uptr *tls_end);
8888

8989
// Memory management
9090
void *MmapOrDie(uptr size, const char *mem_type, bool raw_report = false);

0 commit comments

Comments
 (0)