Index: include/sanitizer/common_interface_defs.h =================================================================== --- include/sanitizer/common_interface_defs.h +++ include/sanitizer/common_interface_defs.h @@ -139,6 +139,11 @@ // `top_percent` should be between 1 and 100. // Experimental feature currently available only with asan on Linux/x86_64. void __sanitizer_print_memory_profile(size_t top_percent); + + void __sanitizer_start_enter_fiber(const void *bottom, size_t size); + void __sanitizer_finish_enter_fiber(); + void __sanitizer_start_exit_fiber(); + void __sanitizer_finish_exit_fiber(); #ifdef __cplusplus } // extern "C" #endif Index: lib/asan/asan_thread.h =================================================================== --- lib/asan/asan_thread.h +++ lib/asan/asan_thread.h @@ -66,9 +66,9 @@ thread_return_t ThreadStart(uptr os_id, atomic_uintptr_t *signal_thread_is_registered); - uptr stack_top() { return stack_top_; } - uptr stack_bottom() { return stack_bottom_; } - uptr stack_size() { return stack_size_; } + uptr stack_top(); + uptr stack_bottom(); + uptr stack_size(); uptr tls_begin() { return tls_begin_; } uptr tls_end() { return tls_end_; } DTLS *dtls() { return dtls_; } @@ -83,9 +83,7 @@ }; bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access); - bool AddrIsInStack(uptr addr) { - return addr >= stack_bottom_ && addr < stack_top_; - } + bool AddrIsInStack(uptr addr); void DeleteFakeStack(int tid) { if (!fake_stack_) return; @@ -95,6 +93,19 @@ t->Destroy(tid); } + void StartEnterFiber(uptr bottom, uptr size) { + fiber_stack_bottom_ = bottom; + fiber_stack_top_ = bottom + size; + fiber_switching_ = true; + } + void FinishEnterFiber() { fiber_switching_ = false; } + + void StartExitFiber() { fiber_switching_ = true; } + void FinishExitFiber() { + fiber_stack_top_ = fiber_stack_bottom_ = 0; + fiber_switching_ = false; + } + bool has_fake_stack() { return (reinterpret_cast(fake_stack_) > 1); } @@ -127,14 +138,23 @@ void ClearShadowForThreadStackAndTLS(); FakeStack *AsyncSignalSafeLazyInitFakeStack(); + struct StackBounds { + uptr bottom; + uptr top; + }; + StackBounds GetStackBounds() const; + AsanThreadContext *context_; thread_callback_t start_routine_; void *arg_; uptr stack_top_; uptr stack_bottom_; - // stack_size_ == stack_top_ - stack_bottom_; - // It needs to be set in a async-signal-safe manner. - uptr stack_size_; + + uptr fiber_stack_top_; + uptr fiber_stack_bottom_; + // true if switching is in progress + bool fiber_switching_; + uptr tls_begin_; uptr tls_end_; DTLS *dtls_; Index: lib/asan/asan_thread.cc =================================================================== --- lib/asan/asan_thread.cc +++ lib/asan/asan_thread.cc @@ -11,6 +11,8 @@ // // Thread-related code. //===----------------------------------------------------------------------===// +#include + #include "asan_allocator.h" #include "asan_interceptors.h" #include "asan_poisoning.h" @@ -120,6 +122,36 @@ DTLS_Destroy(); } +inline AsanThread::StackBounds AsanThread::GetStackBounds() const { + if (!fiber_switching_) { + if (fiber_stack_bottom_) + return StackBounds{fiber_stack_bottom_, fiber_stack_top_}; // NOLINT + else + return StackBounds{stack_bottom_, stack_top_}; // NOLINT + } else { + char local; + const uptr cur_stack = (uptr)&local; + if (cur_stack >= fiber_stack_bottom_ && cur_stack < fiber_stack_top_) + return StackBounds{fiber_stack_bottom_, fiber_stack_top_}; // NOLINT + if (cur_stack >= stack_bottom_ && cur_stack < stack_top_) + return StackBounds{stack_bottom_, stack_top_}; // NOLINT + return StackBounds{stack_bottom_, stack_top_}; // NOLINT + } +} + +uptr AsanThread::stack_top() { + return GetStackBounds().top; +} + +uptr AsanThread::stack_bottom() { + return GetStackBounds().bottom; +} + +uptr AsanThread::stack_size() { + const auto bounds = GetStackBounds(); + return bounds.top - bounds.bottom; +} + // We want to create the FakeStack lazyly on the first use, but not eralier // than the stack size is known and the procedure has to be async-signal safe. FakeStack *AsanThread::AsyncSignalSafeLazyInitFakeStack() { @@ -195,9 +227,12 @@ void AsanThread::SetThreadStackAndTls() { uptr tls_size = 0; - GetThreadStackAndTls(tid() == 0, &stack_bottom_, &stack_size_, &tls_begin_, + uptr stack_size = 0; + GetThreadStackAndTls(tid() == 0, &stack_bottom_, &stack_size, &tls_begin_, &tls_size); - stack_top_ = stack_bottom_ + stack_size_; + stack_top_ = stack_bottom_ + stack_size; + fiber_stack_top_ = fiber_stack_bottom_ = 0; + fiber_switching_ = false; tls_end_ = tls_begin_ + tls_size; dtls_ = DTLS_Get(); @@ -250,6 +285,11 @@ return true; } +bool AsanThread::AddrIsInStack(uptr addr) { + const auto bounds = GetStackBounds(); + return addr >= bounds.bottom && addr < bounds.top; +} + static bool ThreadStackContainsAddress(ThreadContextBase *tctx_base, void *addr) { AsanThreadContext *tctx = static_cast(tctx_base); @@ -357,3 +397,48 @@ __asan::EnsureMainThreadIDIsCorrect(); } } // namespace __lsan + +// ---------------------- Interface ---------------- {{{1 +using namespace __asan; // NOLINT + +extern "C" { +SANITIZER_INTERFACE_ATTRIBUTE +void __sanitizer_start_enter_fiber(const void *bottom, size_t size) { + AsanThread *t = GetCurrentThread(); + if (!t) { + Report("WARNING: __asan_enter_fiber called from unknown thread\n"); + return; + } + t->StartEnterFiber((uptr)bottom, size); +} + +SANITIZER_INTERFACE_ATTRIBUTE +void __sanitizer_finish_enter_fiber() { + AsanThread *t = GetCurrentThread(); + if (!t) { + Report("WARNING: __asan_exit_fiber called from unknown thread\n"); + return; + } + t->FinishEnterFiber(); +} + +SANITIZER_INTERFACE_ATTRIBUTE +void __sanitizer_start_exit_fiber() { + AsanThread *t = GetCurrentThread(); + if (!t) { + Report("WARNING: __asan_enter_fiber called from unknown thread\n"); + return; + } + t->StartExitFiber(); +} + +SANITIZER_INTERFACE_ATTRIBUTE +void __sanitizer_finish_exit_fiber() { + AsanThread *t = GetCurrentThread(); + if (!t) { + Report("WARNING: __asan_exit_fiber called from unknown thread\n"); + return; + } + t->FinishExitFiber(); +} +} Index: test/asan/TestCases/Linux/swapcontext_test.cc =================================================================== --- test/asan/TestCases/Linux/swapcontext_test.cc +++ test/asan/TestCases/Linux/swapcontext_test.cc @@ -12,6 +12,8 @@ #include #include +#include + ucontext_t orig_context; ucontext_t child_context; @@ -32,12 +34,14 @@ } void Child(int mode) { + __sanitizer_finish_enter_fiber(); char x[32] = {0}; // Stack gets poisoned. printf("Child: %p\n", x); ThrowAndCatch(); // Simulate __asan_handle_no_return(). // (a) Do nothing, just return to parent function. // (b) Jump into the original function. Stack remains poisoned unless we do // something. + __sanitizer_start_exit_fiber(); if (mode == 1) { if (swapcontext(&child_context, &orig_context) < 0) { perror("swapcontext"); @@ -56,10 +60,14 @@ child_context.uc_link = &orig_context; } makecontext(&child_context, (void (*)())Child, 1, mode); + __sanitizer_start_enter_fiber(child_context.uc_stack.ss_sp, + child_context.uc_stack.ss_size); if (swapcontext(&orig_context, &child_context) < 0) { perror("swapcontext"); return 0; } + __sanitizer_finish_exit_fiber(); + // Touch childs's stack to make sure it's unpoisoned. for (int i = 0; i < kStackSize; i++) { child_stack[i] = i; @@ -73,16 +81,20 @@ int ret = 0; ret += Run(argc - 1, 0, stack); printf("Test1 passed\n"); + // CHECK-NOT: ASan is ignoring requested __asan_handle_no_return // CHECK: Test1 passed ret += Run(argc - 1, 1, stack); printf("Test2 passed\n"); + // CHECK-NOT: ASan is ignoring requested __asan_handle_no_return // CHECK: Test2 passed char *heap = new char[kStackSize + 1]; ret += Run(argc - 1, 0, heap); printf("Test3 passed\n"); + // CHECK-NOT: ASan is ignoring requested __asan_handle_no_return // CHECK: Test3 passed ret += Run(argc - 1, 1, heap); printf("Test4 passed\n"); + // CHECK-NOT: ASan is ignoring requested __asan_handle_no_return // CHECK: Test4 passed delete [] heap;