1 //===-- asan_thread_registry.h ----------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file is a part of AddressSanitizer, an address sanity checker. 11 // 12 // ASan-private header for asan_thread_registry.cc 13 //===----------------------------------------------------------------------===// 14 15 #ifndef ASAN_THREAD_REGISTRY_H 16 #define ASAN_THREAD_REGISTRY_H 17 18 #include "asan_stack.h" 19 #include "asan_stats.h" 20 #include "asan_thread.h" 21 #include "sanitizer_common/sanitizer_mutex.h" 22 23 namespace __asan { 24 25 // Stores summaries of all created threads, returns current thread, 26 // thread by tid, thread by stack address. There is a single instance 27 // of AsanThreadRegistry for the whole program. 28 // AsanThreadRegistry is thread-safe. 29 class AsanThreadRegistry { 30 public: 31 explicit AsanThreadRegistry(LinkerInitialized); 32 void Init(); 33 void RegisterThread(AsanThread *thread); 34 void UnregisterThread(AsanThread *thread); 35 36 AsanThread *GetMain(); 37 // Get the current thread. May return 0. 38 AsanThread *GetCurrent(); 39 void SetCurrent(AsanThread *t); 40 GetCurrentTidOrInvalid()41 u32 GetCurrentTidOrInvalid() { 42 if (!inited_) return 0; 43 AsanThread *t = GetCurrent(); 44 return t ? t->tid() : kInvalidTid; 45 } 46 47 // Returns stats for GetCurrent(), or stats for 48 // T0 if GetCurrent() returns 0. 49 AsanStats &GetCurrentThreadStats(); 50 // Flushes all thread-local stats to accumulated stats, and makes 51 // a copy of accumulated stats. 52 void GetAccumulatedStats(AsanStats *stats); 53 uptr GetCurrentAllocatedBytes(); 54 uptr GetHeapSize(); 55 uptr GetFreeBytes(); 56 void FillMallocStatistics(AsanMallocStats *malloc_stats); 57 58 AsanThreadSummary *FindByTid(u32 tid); 59 AsanThread *FindThreadByStackAddress(uptr addr); 60 61 private: 62 void UpdateAccumulatedStatsUnlocked(); 63 // Adds values of all counters in "stats" to accumulated stats, 64 // and fills "stats" with zeroes. 65 void FlushToAccumulatedStatsUnlocked(AsanStats *stats); 66 67 static const u32 kMaxNumberOfThreads = (1 << 22); // 4M 68 AsanThreadSummary *thread_summaries_[kMaxNumberOfThreads]; 69 AsanThread main_thread_; 70 AsanThreadSummary main_thread_summary_; 71 AsanStats accumulated_stats_; 72 // Required for malloc_zone_statistics() on OS X. This can't be stored in 73 // per-thread AsanStats. 74 uptr max_malloced_memory_; 75 u32 n_threads_; 76 BlockingMutex mu_; 77 bool inited_; 78 }; 79 80 // Returns a single instance of registry. 81 AsanThreadRegistry &asanThreadRegistry(); 82 83 } // namespace __asan 84 85 #endif // ASAN_THREAD_REGISTRY_H 86