10b57cec5SDimitry Andric //===-- msan_thread.h -------------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file is a part of MemorySanitizer. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #ifndef MSAN_THREAD_H 140b57cec5SDimitry Andric #define MSAN_THREAD_H 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "msan_allocator.h" 170b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_common.h" 18*349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_posix.h" 190b57cec5SDimitry Andric namespace __msan { 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric class MsanThread { 220b57cec5SDimitry Andric public: 230b57cec5SDimitry Andric static MsanThread *Create(thread_callback_t start_routine, void *arg); 240b57cec5SDimitry Andric static void TSDDtor(void *tsd); 250b57cec5SDimitry Andric void Destroy(); 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric void Init(); // Should be called from the thread itself. 280b57cec5SDimitry Andric thread_return_t ThreadStart(); 290b57cec5SDimitry Andric 30e8d8bef9SDimitry Andric uptr stack_top(); 31e8d8bef9SDimitry Andric uptr stack_bottom(); tls_begin()320b57cec5SDimitry Andric uptr tls_begin() { return tls_begin_; } tls_end()330b57cec5SDimitry Andric uptr tls_end() { return tls_end_; } IsMainThread()340b57cec5SDimitry Andric bool IsMainThread() { return start_routine_ == nullptr; } 350b57cec5SDimitry Andric 36e8d8bef9SDimitry Andric bool AddrIsInStack(uptr addr); 370b57cec5SDimitry Andric InSignalHandler()380b57cec5SDimitry Andric bool InSignalHandler() { return in_signal_handler_; } EnterSignalHandler()390b57cec5SDimitry Andric void EnterSignalHandler() { in_signal_handler_++; } LeaveSignalHandler()400b57cec5SDimitry Andric void LeaveSignalHandler() { in_signal_handler_--; } 410b57cec5SDimitry Andric 42e8d8bef9SDimitry Andric void StartSwitchFiber(uptr bottom, uptr size); 43e8d8bef9SDimitry Andric void FinishSwitchFiber(uptr *bottom_old, uptr *size_old); 44e8d8bef9SDimitry Andric malloc_storage()450b57cec5SDimitry Andric MsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; } 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric int destructor_iterations_; 48*349cc55cSDimitry Andric __sanitizer_sigset_t starting_sigset_; 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric private: 510b57cec5SDimitry Andric // NOTE: There is no MsanThread constructor. It is allocated 520b57cec5SDimitry Andric // via mmap() and *must* be valid in zero-initialized state. 530b57cec5SDimitry Andric void SetThreadStackAndTls(); 540b57cec5SDimitry Andric void ClearShadowForThreadStackAndTLS(); 55e8d8bef9SDimitry Andric struct StackBounds { 56e8d8bef9SDimitry Andric uptr bottom; 57e8d8bef9SDimitry Andric uptr top; 58e8d8bef9SDimitry Andric }; 59e8d8bef9SDimitry Andric StackBounds GetStackBounds() const; 600b57cec5SDimitry Andric thread_callback_t start_routine_; 610b57cec5SDimitry Andric void *arg_; 62e8d8bef9SDimitry Andric 63e8d8bef9SDimitry Andric bool stack_switching_; 64e8d8bef9SDimitry Andric 65e8d8bef9SDimitry Andric StackBounds stack_; 66e8d8bef9SDimitry Andric StackBounds next_stack_; 67e8d8bef9SDimitry Andric 680b57cec5SDimitry Andric uptr tls_begin_; 690b57cec5SDimitry Andric uptr tls_end_; 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric unsigned in_signal_handler_; 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric MsanThreadLocalMallocStorage malloc_storage_; 740b57cec5SDimitry Andric }; 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric MsanThread *GetCurrentThread(); 770b57cec5SDimitry Andric void SetCurrentThread(MsanThread *t); 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric } // namespace __msan 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric #endif // MSAN_THREAD_H 82