Skip to content

Commit 10972a7

Browse files
committed
Add API to temporalily disable usage of ASAN's fake stack
Intended use-case is for threads that use (or switch to) stack with special properties e.g. backed by MADV_DONTDUMP memory.
1 parent 92f5d8d commit 10972a7

File tree

10 files changed

+93
-12
lines changed

10 files changed

+93
-12
lines changed

compiler-rt/include/sanitizer/asan_interface.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@ void SANITIZER_CDECL __asan_handle_no_return(void);
333333
/// trace. Returns 1 if successful, 0 if not.
334334
int SANITIZER_CDECL __asan_update_allocation_context(void *addr);
335335

336+
/// Disables fake stack for the current thread.
337+
/// Temporarily disables use-after-return detection for current thread.
338+
void SANITIZER_CDECL __asan_disable_fake_stack(void);
339+
340+
/// (Re)enables fake stack for the current thread.
341+
void SANITIZER_CDECL __asan_enable_fake_stack(void);
342+
336343
#ifdef __cplusplus
337344
} // extern "C"
338345
#endif

compiler-rt/lib/asan/asan_fake_stack.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,26 +217,44 @@ static THREADLOCAL FakeStack *fake_stack_tls;
217217
FakeStack *GetTLSFakeStack() {
218218
return fake_stack_tls;
219219
}
220-
void SetTLSFakeStack(FakeStack *fs) {
220+
void SetTLSFakeStack(AsanThread* t, FakeStack* fs) {
221+
if (fs && !t->IsFakeStackEnabled()) {
222+
return;
223+
}
221224
fake_stack_tls = fs;
222225
}
223226
#else
224227
FakeStack *GetTLSFakeStack() { return 0; }
225-
void SetTLSFakeStack(FakeStack *fs) { }
228+
void SetTLSFakeStack(AsanThread* t, FakeStack* fs) {}
226229
#endif // (SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_FUCHSIA
227230

228-
static FakeStack *GetFakeStack() {
231+
static void DisableFakeStack() {
229232
AsanThread *t = GetCurrentThread();
230-
if (!t) return nullptr;
233+
if (t) {
234+
t->SetFakeStackEnabled(false);
235+
}
236+
}
237+
238+
static void EnableFakeStack() {
239+
AsanThread* t = GetCurrentThread();
240+
if (t) {
241+
t->SetFakeStackEnabled(true);
242+
}
243+
}
244+
245+
static FakeStack* GetFakeStack(bool for_allocation = true) {
246+
AsanThread* t = GetCurrentThread();
247+
if (!t || (for_allocation && !t->IsFakeStackEnabled()))
248+
return nullptr;
231249
return t->get_or_create_fake_stack();
232250
}
233251

234-
static FakeStack *GetFakeStackFast() {
252+
static FakeStack* GetFakeStackFast(bool for_allocation = true) {
235253
if (FakeStack *fs = GetTLSFakeStack())
236254
return fs;
237255
if (!__asan_option_detect_stack_use_after_return)
238256
return nullptr;
239-
return GetFakeStack();
257+
return GetFakeStack(for_allocation);
240258
}
241259

242260
static FakeStack *GetFakeStackFastAlways() {
@@ -311,7 +329,9 @@ extern "C" {
311329
// -asan-use-after-return=never, after modal UAR flag lands
312330
// (https://github.com/google/sanitizers/issues/1394)
313331
SANITIZER_INTERFACE_ATTRIBUTE
314-
void *__asan_get_current_fake_stack() { return GetFakeStackFast(); }
332+
void* __asan_get_current_fake_stack() {
333+
return GetFakeStackFast(/*for_allocation=*/false);
334+
}
315335

316336
SANITIZER_INTERFACE_ATTRIBUTE
317337
void *__asan_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
@@ -349,4 +369,9 @@ void __asan_allocas_unpoison(uptr top, uptr bottom) {
349369
(reinterpret_cast<void *>(MemToShadow(top)), 0,
350370
(bottom - top) / ASAN_SHADOW_GRANULARITY);
351371
}
372+
373+
SANITIZER_INTERFACE_ATTRIBUTE
374+
void __asan_disable_fake_stack() { return DisableFakeStack(); }
375+
SANITIZER_INTERFACE_ATTRIBUTE
376+
void __asan_enable_fake_stack() { return EnableFakeStack(); }
352377
} // extern "C"

compiler-rt/lib/asan/asan_fake_stack.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
namespace __asan {
2020

21+
class AsanThread;
22+
2123
// Fake stack frame contains local variables of one function.
2224
struct FakeFrame {
2325
uptr magic; // Modified by the instrumented code.
@@ -196,7 +198,7 @@ class FakeStack {
196198
};
197199

198200
FakeStack *GetTLSFakeStack();
199-
void SetTLSFakeStack(FakeStack *fs);
201+
void SetTLSFakeStack(AsanThread* t, FakeStack* fs);
200202

201203
} // namespace __asan
202204

compiler-rt/lib/asan/asan_interface.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ INTERFACE_FUNCTION(__asan_alloca_poison)
1515
INTERFACE_FUNCTION(__asan_allocas_unpoison)
1616
INTERFACE_FUNCTION(__asan_before_dynamic_init)
1717
INTERFACE_FUNCTION(__asan_describe_address)
18+
INTERFACE_FUNCTION(__asan_disable_fake_stack)
19+
INTERFACE_FUNCTION(__asan_enable_fake_stack)
1820
INTERFACE_FUNCTION(__asan_exp_load1)
1921
INTERFACE_FUNCTION(__asan_exp_load2)
2022
INTERFACE_FUNCTION(__asan_exp_load4)

compiler-rt/lib/asan/asan_thread.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void AsanThread::StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom,
163163
if (fake_stack_save)
164164
*fake_stack_save = fake_stack_;
165165
fake_stack_ = nullptr;
166-
SetTLSFakeStack(nullptr);
166+
SetTLSFakeStack(this, nullptr);
167167
// if fake_stack_save is null, the fiber will die, delete the fakestack
168168
if (!fake_stack_save && current_fake_stack)
169169
current_fake_stack->Destroy(this->tid());
@@ -177,7 +177,7 @@ void AsanThread::FinishSwitchFiber(FakeStack *fake_stack_save, uptr *bottom_old,
177177
}
178178

179179
if (fake_stack_save) {
180-
SetTLSFakeStack(fake_stack_save);
180+
SetTLSFakeStack(this, fake_stack_save);
181181
fake_stack_ = fake_stack_save;
182182
}
183183

@@ -242,7 +242,7 @@ FakeStack *AsanThread::AsyncSignalSafeLazyInitFakeStack() {
242242
Max(stack_size_log, static_cast<uptr>(flags()->min_uar_stack_size_log));
243243
fake_stack_ = FakeStack::Create(stack_size_log);
244244
DCHECK_EQ(GetCurrentThread(), this);
245-
SetTLSFakeStack(fake_stack_);
245+
SetTLSFakeStack(this, fake_stack_);
246246
return fake_stack_;
247247
}
248248
return nullptr;
@@ -251,6 +251,7 @@ FakeStack *AsanThread::AsyncSignalSafeLazyInitFakeStack() {
251251
void AsanThread::Init(const InitOptions *options) {
252252
DCHECK_NE(tid(), kInvalidTid);
253253
next_stack_top_ = next_stack_bottom_ = 0;
254+
fake_stack_enabled_ = true;
254255
atomic_store(&stack_switching_, false, memory_order_release);
255256
CHECK_EQ(this->stack_size(), 0U);
256257
SetThreadStackAndTls(options);

compiler-rt/lib/asan/asan_thread.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class AsanThread {
104104
if (!fake_stack_) return;
105105
FakeStack *t = fake_stack_;
106106
fake_stack_ = nullptr;
107-
SetTLSFakeStack(nullptr);
107+
SetTLSFakeStack(this, nullptr);
108108
t->Destroy(tid);
109109
}
110110

@@ -144,6 +144,14 @@ class AsanThread {
144144
GetStartData(&data, sizeof(data));
145145
}
146146

147+
bool IsFakeStackEnabled() const { return fake_stack_enabled_; }
148+
void SetFakeStackEnabled(bool enabled) {
149+
fake_stack_enabled_ = enabled;
150+
if (!enabled) {
151+
SetTLSFakeStack(this, nullptr);
152+
}
153+
}
154+
147155
private:
148156
// NOTE: There is no AsanThread constructor. It is allocated
149157
// via mmap() and *must* be valid in zero-initialized state.
@@ -179,6 +187,7 @@ class AsanThread {
179187
DTLS *dtls_;
180188

181189
FakeStack *fake_stack_;
190+
bool fake_stack_enabled_;
182191
AsanThreadLocalMallocStorage malloc_storage_;
183192
AsanStats stats_;
184193
bool unwinding_;

compiler-rt/lib/asan_abi/asan_abi.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ void *__asan_abi_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
7373
void **end) {
7474
return NULL;
7575
}
76+
void __asan_abi_disable_fake_stack(void) {}
77+
void __asan_abi_enable_fake_stack(void) {}
7678

7779
// Functions concerning poisoning and unpoisoning fake stack alloca
7880
void __asan_abi_alloca_poison(void *addr, size_t size) {}

compiler-rt/lib/asan_abi/asan_abi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ void *__asan_abi_load_cxx_array_cookie(void **p);
7676
void *__asan_abi_get_current_fake_stack();
7777
void *__asan_abi_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
7878
void **end);
79+
void *__asan_abi_disable_fake_stack();
80+
void *__asan_abi_enable_fake_stack();
81+
7982
// Functions concerning poisoning and unpoisoning fake stack alloca
8083
void __asan_abi_alloca_poison(void *addr, size_t size);
8184
void __asan_abi_allocas_unpoison(void *top, void *bottom);

compiler-rt/lib/asan_abi/asan_abi_shim.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ void *__asan_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
365365
void **end) {
366366
return __asan_abi_addr_is_in_fake_stack(fake_stack, addr, beg, end);
367367
}
368+
void __asan_disable_fake_stack(void) { return __asan_abi_disable_fake_stack(); }
369+
void __asan_enable_fake_stack(void) { return __asan_abi_enable_fake_stack(); }
368370

369371
// Functions concerning poisoning and unpoisoning fake stack alloca
370372
void __asan_alloca_poison(uptr addr, uptr size) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clangxx_asan %s -o %t && %run %t
2+
3+
#include "defines.h"
4+
5+
#include <sanitizer/asan_interface.h>
6+
7+
volatile char *saved;
8+
9+
ATTRIBUTE_NOINLINE bool IsOnStack() {
10+
volatile char temp = ' ';
11+
void *fake_stack = __asan_get_current_fake_stack();
12+
void *real = __asan_addr_is_in_fake_stack(
13+
fake_stack, const_cast<char *>(&temp), nullptr, nullptr);
14+
saved = &temp;
15+
return real == nullptr;
16+
}
17+
18+
int main(int argc, char *argv[]) {
19+
__asan_disable_fake_stack();
20+
if (!IsOnStack()) {
21+
return 1;
22+
}
23+
__asan_enable_fake_stack();
24+
if (IsOnStack()) {
25+
return 2;
26+
}
27+
return 0;
28+
}

0 commit comments

Comments
 (0)