sanitizer_thread_registry.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //===-- sanitizer_thread_registry.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 shared between sanitizer tools.
  10. //
  11. // General thread bookkeeping functionality.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef SANITIZER_THREAD_REGISTRY_H
  14. #define SANITIZER_THREAD_REGISTRY_H
  15. #include "sanitizer_common.h"
  16. #include "sanitizer_dense_map.h"
  17. #include "sanitizer_list.h"
  18. #include "sanitizer_mutex.h"
  19. namespace __sanitizer {
  20. enum ThreadStatus {
  21. ThreadStatusInvalid, // Non-existent thread, data is invalid.
  22. ThreadStatusCreated, // Created but not yet running.
  23. ThreadStatusRunning, // The thread is currently running.
  24. ThreadStatusFinished, // Joinable thread is finished but not yet joined.
  25. ThreadStatusDead // Joined, but some info is still available.
  26. };
  27. enum class ThreadType {
  28. Regular, // Normal thread
  29. Worker, // macOS Grand Central Dispatch (GCD) worker thread
  30. Fiber, // Fiber
  31. };
  32. // Generic thread context. Specific sanitizer tools may inherit from it.
  33. // If thread is dead, context may optionally be reused for a new thread.
  34. class ThreadContextBase {
  35. public:
  36. explicit ThreadContextBase(u32 tid);
  37. const u32 tid; // Thread ID. Main thread should have tid = 0.
  38. u64 unique_id; // Unique thread ID.
  39. u32 reuse_count; // Number of times this tid was reused.
  40. tid_t os_id; // PID (used for reporting).
  41. uptr user_id; // Some opaque user thread id (e.g. pthread_t).
  42. char name[64]; // As annotated by user.
  43. ThreadStatus status;
  44. bool detached;
  45. ThreadType thread_type;
  46. u32 parent_tid;
  47. ThreadContextBase *next; // For storing thread contexts in a list.
  48. atomic_uint32_t thread_destroyed; // To address race of Joined vs Finished
  49. void SetName(const char *new_name);
  50. void SetDead();
  51. void SetJoined(void *arg);
  52. void SetFinished();
  53. void SetStarted(tid_t _os_id, ThreadType _thread_type, void *arg);
  54. void SetCreated(uptr _user_id, u64 _unique_id, bool _detached,
  55. u32 _parent_tid, void *arg);
  56. void Reset();
  57. void SetDestroyed();
  58. bool GetDestroyed();
  59. // The following methods may be overriden by subclasses.
  60. // Some of them take opaque arg that may be optionally be used
  61. // by subclasses.
  62. virtual void OnDead() {}
  63. virtual void OnJoined(void *arg) {}
  64. virtual void OnFinished() {}
  65. virtual void OnStarted(void *arg) {}
  66. virtual void OnCreated(void *arg) {}
  67. virtual void OnReset() {}
  68. virtual void OnDetached(void *arg) {}
  69. protected:
  70. ~ThreadContextBase();
  71. };
  72. typedef ThreadContextBase* (*ThreadContextFactory)(u32 tid);
  73. class SANITIZER_MUTEX ThreadRegistry {
  74. public:
  75. ThreadRegistry(ThreadContextFactory factory);
  76. ThreadRegistry(ThreadContextFactory factory, u32 max_threads,
  77. u32 thread_quarantine_size, u32 max_reuse);
  78. void GetNumberOfThreads(uptr *total = nullptr, uptr *running = nullptr,
  79. uptr *alive = nullptr);
  80. uptr GetMaxAliveThreads();
  81. void Lock() SANITIZER_ACQUIRE() { mtx_.Lock(); }
  82. void CheckLocked() const SANITIZER_CHECK_LOCKED() { mtx_.CheckLocked(); }
  83. void Unlock() SANITIZER_RELEASE() { mtx_.Unlock(); }
  84. // Should be guarded by ThreadRegistryLock.
  85. ThreadContextBase *GetThreadLocked(u32 tid) {
  86. return threads_.empty() ? nullptr : threads_[tid];
  87. }
  88. u32 NumThreadsLocked() const { return threads_.size(); }
  89. u32 CreateThread(uptr user_id, bool detached, u32 parent_tid, void *arg);
  90. typedef void (*ThreadCallback)(ThreadContextBase *tctx, void *arg);
  91. // Invokes callback with a specified arg for each thread context.
  92. // Should be guarded by ThreadRegistryLock.
  93. void RunCallbackForEachThreadLocked(ThreadCallback cb, void *arg);
  94. typedef bool (*FindThreadCallback)(ThreadContextBase *tctx, void *arg);
  95. // Finds a thread using the provided callback. Returns kInvalidTid if no
  96. // thread is found.
  97. u32 FindThread(FindThreadCallback cb, void *arg);
  98. // Should be guarded by ThreadRegistryLock. Return 0 if no thread
  99. // is found.
  100. ThreadContextBase *FindThreadContextLocked(FindThreadCallback cb,
  101. void *arg);
  102. ThreadContextBase *FindThreadContextByOsIDLocked(tid_t os_id);
  103. void SetThreadName(u32 tid, const char *name);
  104. void SetThreadNameByUserId(uptr user_id, const char *name);
  105. void DetachThread(u32 tid, void *arg);
  106. void JoinThread(u32 tid, void *arg);
  107. // Finishes thread and returns previous status.
  108. ThreadStatus FinishThread(u32 tid);
  109. void StartThread(u32 tid, tid_t os_id, ThreadType thread_type, void *arg);
  110. u32 ConsumeThreadUserId(uptr user_id);
  111. void SetThreadUserId(u32 tid, uptr user_id);
  112. // OnFork must be called in the child process after fork to purge old
  113. // threads that don't exist anymore (except for the current thread tid).
  114. // Returns number of alive threads before fork.
  115. u32 OnFork(u32 tid);
  116. private:
  117. const ThreadContextFactory context_factory_;
  118. const u32 max_threads_;
  119. const u32 thread_quarantine_size_;
  120. const u32 max_reuse_;
  121. Mutex mtx_;
  122. u64 total_threads_; // Total number of created threads. May be greater than
  123. // max_threads_ if contexts were reused.
  124. uptr alive_threads_; // Created or running.
  125. uptr max_alive_threads_;
  126. uptr running_threads_;
  127. InternalMmapVector<ThreadContextBase *> threads_;
  128. IntrusiveList<ThreadContextBase> dead_threads_;
  129. IntrusiveList<ThreadContextBase> invalid_threads_;
  130. DenseMap<uptr, Tid> live_;
  131. void QuarantinePush(ThreadContextBase *tctx);
  132. ThreadContextBase *QuarantinePop();
  133. };
  134. typedef GenericScopedLock<ThreadRegistry> ThreadRegistryLock;
  135. } // namespace __sanitizer
  136. #endif // SANITIZER_THREAD_REGISTRY_H