dfsan_thread.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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(void *start_routine_trampoline,
  23. thread_callback_t start_routine, void *arg,
  24. bool track_origins = false);
  25. static void TSDDtor(void *tsd);
  26. void Destroy();
  27. void Init(); // Should be called from the thread itself.
  28. thread_return_t ThreadStart();
  29. uptr stack_top();
  30. uptr stack_bottom();
  31. uptr tls_begin() { return tls_begin_; }
  32. uptr tls_end() { return tls_end_; }
  33. bool IsMainThread() { return start_routine_ == nullptr; }
  34. bool InSignalHandler() { return in_signal_handler_; }
  35. void EnterSignalHandler() { in_signal_handler_++; }
  36. void LeaveSignalHandler() { in_signal_handler_--; }
  37. DFsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
  38. int destructor_iterations_;
  39. __sanitizer_sigset_t starting_sigset_;
  40. private:
  41. void SetThreadStackAndTls();
  42. void ClearShadowForThreadStackAndTLS();
  43. struct StackBounds {
  44. uptr bottom;
  45. uptr top;
  46. };
  47. StackBounds GetStackBounds() const;
  48. bool AddrIsInStack(uptr addr);
  49. void *start_routine_trampoline_;
  50. thread_callback_t start_routine_;
  51. void *arg_;
  52. bool track_origins_;
  53. StackBounds stack_;
  54. uptr tls_begin_;
  55. uptr tls_end_;
  56. unsigned in_signal_handler_;
  57. DFsanThreadLocalMallocStorage malloc_storage_;
  58. };
  59. DFsanThread *GetCurrentThread();
  60. void SetCurrentThread(DFsanThread *t);
  61. void DFsanTSDInit(void (*destructor)(void *tsd));
  62. void DFsanTSDDtor(void *tsd);
  63. } // namespace __dfsan
  64. #endif // DFSAN_THREAD_H