dfsan_thread.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //===-- dfsan_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. // This file is a part of DataFlowSanitizer.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef DFSAN_THREAD_H
  13. #define DFSAN_THREAD_H
  14. #include "dfsan_allocator.h"
  15. #include "sanitizer_common/sanitizer_common.h"
  16. #include "sanitizer_common/sanitizer_posix.h"
  17. namespace __dfsan {
  18. class DFsanThread {
  19. public:
  20. // NOTE: There is no DFsanThread constructor. It is allocated
  21. // via mmap() and *must* be valid in zero-initialized state.
  22. static DFsanThread *Create(thread_callback_t start_routine, void *arg,
  23. bool track_origins = false);
  24. static void TSDDtor(void *tsd);
  25. void Destroy();
  26. void Init(); // Should be called from the thread itself.
  27. thread_return_t ThreadStart();
  28. uptr stack_top();
  29. uptr stack_bottom();
  30. uptr tls_begin() { return tls_begin_; }
  31. uptr tls_end() { return tls_end_; }
  32. bool IsMainThread() { return start_routine_ == nullptr; }
  33. bool InSignalHandler() { return in_signal_handler_; }
  34. void EnterSignalHandler() { in_signal_handler_++; }
  35. void LeaveSignalHandler() { in_signal_handler_--; }
  36. DFsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
  37. int destructor_iterations_;
  38. __sanitizer_sigset_t starting_sigset_;
  39. private:
  40. void SetThreadStackAndTls();
  41. void ClearShadowForThreadStackAndTLS();
  42. struct StackBounds {
  43. uptr bottom;
  44. uptr top;
  45. };
  46. StackBounds GetStackBounds() const;
  47. bool AddrIsInStack(uptr addr);
  48. thread_callback_t start_routine_;
  49. void *arg_;
  50. bool track_origins_;
  51. StackBounds stack_;
  52. uptr tls_begin_;
  53. uptr tls_end_;
  54. unsigned in_signal_handler_;
  55. DFsanThreadLocalMallocStorage malloc_storage_;
  56. };
  57. DFsanThread *GetCurrentThread();
  58. void SetCurrentThread(DFsanThread *t);
  59. void DFsanTSDInit(void (*destructor)(void *tsd));
  60. void DFsanTSDDtor(void *tsd);
  61. } // namespace __dfsan
  62. #endif // DFSAN_THREAD_H