aboutsummaryrefslogtreecommitdiff
path: root/lib/asan/asan_thread_registry.h
blob: adb1a6d4f32df0fdbba50bd2ebd70aad96502a48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//===-- asan_thread_registry.h ----------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of AddressSanitizer, an address sanity checker.
//
// ASan-private header for asan_thread_registry.cc
//===----------------------------------------------------------------------===//

#ifndef ASAN_THREAD_REGISTRY_H
#define ASAN_THREAD_REGISTRY_H

#include "asan_stack.h"
#include "asan_stats.h"
#include "asan_thread.h"
#include "sanitizer_common/sanitizer_mutex.h"

namespace __asan {

// Stores summaries of all created threads, returns current thread,
// thread by tid, thread by stack address. There is a single instance
// of AsanThreadRegistry for the whole program.
// AsanThreadRegistry is thread-safe.
class AsanThreadRegistry {
 public:
  explicit AsanThreadRegistry(LinkerInitialized);
  void Init();
  void RegisterThread(AsanThread *thread);
  void UnregisterThread(AsanThread *thread);

  AsanThread *GetMain();
  // Get the current thread. May return 0.
  AsanThread *GetCurrent();
  void SetCurrent(AsanThread *t);

  u32 GetCurrentTidOrInvalid() {
    if (!inited_) return 0;
    AsanThread *t = GetCurrent();
    return t ? t->tid() : kInvalidTid;
  }

  // Returns stats for GetCurrent(), or stats for
  // T0 if GetCurrent() returns 0.
  AsanStats &GetCurrentThreadStats();
  // Flushes all thread-local stats to accumulated stats, and makes
  // a copy of accumulated stats.
  void GetAccumulatedStats(AsanStats *stats);
  uptr GetCurrentAllocatedBytes();
  uptr GetHeapSize();
  uptr GetFreeBytes();
  void FillMallocStatistics(AsanMallocStats *malloc_stats);

  AsanThreadSummary *FindByTid(u32 tid);
  AsanThread *FindThreadByStackAddress(uptr addr);

 private:
  void UpdateAccumulatedStatsUnlocked();
  // Adds values of all counters in "stats" to accumulated stats,
  // and fills "stats" with zeroes.
  void FlushToAccumulatedStatsUnlocked(AsanStats *stats);

  static const u32 kMaxNumberOfThreads = (1 << 22);  // 4M
  AsanThreadSummary *thread_summaries_[kMaxNumberOfThreads];
  AsanThread main_thread_;
  AsanThreadSummary main_thread_summary_;
  AsanStats accumulated_stats_;
  // Required for malloc_zone_statistics() on OS X. This can't be stored in
  // per-thread AsanStats.
  uptr max_malloced_memory_;
  u32 n_threads_;
  BlockingMutex mu_;
  bool inited_;
};

// Returns a single instance of registry.
AsanThreadRegistry &asanThreadRegistry();

}  // namespace __asan

#endif  // ASAN_THREAD_REGISTRY_H