1 //===- nsan_thread.h --------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef NSAN_THREAD_H 10 #define NSAN_THREAD_H 11 12 #include "nsan_allocator.h" 13 #include "sanitizer_common/sanitizer_common.h" 14 #include "sanitizer_common/sanitizer_posix.h" 15 16 namespace __nsan { 17 18 class NsanThread { 19 public: 20 static NsanThread *Create(thread_callback_t start_routine, void *arg); 21 static void TSDDtor(void *tsd); 22 void Destroy(); 23 24 void Init(); // Should be called from the thread itself. 25 thread_return_t ThreadStart(); 26 27 uptr stack_top(); 28 uptr stack_bottom(); tls_begin()29 uptr tls_begin() { return tls_begin_; } tls_end()30 uptr tls_end() { return tls_end_; } IsMainThread()31 bool IsMainThread() { return start_routine_ == nullptr; } 32 33 bool AddrIsInStack(uptr addr); 34 35 void StartSwitchFiber(uptr bottom, uptr size); 36 void FinishSwitchFiber(uptr *bottom_old, uptr *size_old); 37 malloc_storage()38 NsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; } 39 40 int destructor_iterations_; 41 __sanitizer_sigset_t starting_sigset_; 42 43 private: 44 void SetThreadStackAndTls(); 45 void ClearShadowForThreadStackAndTLS(); 46 struct StackBounds { 47 uptr bottom; 48 uptr top; 49 }; 50 StackBounds GetStackBounds() const; 51 52 thread_callback_t start_routine_; 53 void *arg_; 54 55 bool stack_switching_; 56 57 StackBounds stack_; 58 StackBounds next_stack_; 59 60 uptr tls_begin_; 61 uptr tls_end_; 62 63 NsanThreadLocalMallocStorage malloc_storage_; 64 }; 65 66 NsanThread *GetCurrentThread(); 67 void SetCurrentThread(NsanThread *t); 68 void NsanTSDInit(void (*destructor)(void *tsd)); 69 void NsanTSDDtor(void *tsd); 70 71 } // namespace __nsan 72 73 #endif // NSAN_THREAD_H 74