Add libstdc++-raw-cxx.m4 and use it in libsanitizer
[gcc.git] / libsanitizer / asan / asan_thread_registry.cc
1 //===-- asan_thread_registry.cc -------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // AsanThreadRegistry-related code. AsanThreadRegistry is a container
11 // for summaries of all created threads.
12 //===----------------------------------------------------------------------===//
13
14 #include "asan_stack.h"
15 #include "asan_thread.h"
16 #include "asan_thread_registry.h"
17 #include "sanitizer_common/sanitizer_common.h"
18
19 namespace __asan {
20
21 static AsanThreadRegistry asan_thread_registry(LINKER_INITIALIZED);
22
23 AsanThreadRegistry &asanThreadRegistry() {
24 return asan_thread_registry;
25 }
26
27 AsanThreadRegistry::AsanThreadRegistry(LinkerInitialized x)
28 : main_thread_(x),
29 main_thread_summary_(x),
30 accumulated_stats_(x),
31 max_malloced_memory_(x),
32 mu_(x) { }
33
34 void AsanThreadRegistry::Init() {
35 AsanTSDInit(AsanThreadSummary::TSDDtor);
36 main_thread_.set_summary(&main_thread_summary_);
37 main_thread_summary_.set_thread(&main_thread_);
38 RegisterThread(&main_thread_);
39 SetCurrent(&main_thread_);
40 // At this point only one thread exists.
41 inited_ = true;
42 }
43
44 void AsanThreadRegistry::RegisterThread(AsanThread *thread) {
45 ScopedLock lock(&mu_);
46 u32 tid = n_threads_;
47 n_threads_++;
48 CHECK(n_threads_ < kMaxNumberOfThreads);
49
50 AsanThreadSummary *summary = thread->summary();
51 CHECK(summary != 0);
52 summary->set_tid(tid);
53 thread_summaries_[tid] = summary;
54 }
55
56 void AsanThreadRegistry::UnregisterThread(AsanThread *thread) {
57 ScopedLock lock(&mu_);
58 FlushToAccumulatedStatsUnlocked(&thread->stats());
59 AsanThreadSummary *summary = thread->summary();
60 CHECK(summary);
61 summary->set_thread(0);
62 }
63
64 AsanThread *AsanThreadRegistry::GetMain() {
65 return &main_thread_;
66 }
67
68 AsanThread *AsanThreadRegistry::GetCurrent() {
69 AsanThreadSummary *summary = (AsanThreadSummary *)AsanTSDGet();
70 if (!summary) {
71 #if ASAN_ANDROID
72 // On Android, libc constructor is called _after_ asan_init, and cleans up
73 // TSD. Try to figure out if this is still the main thread by the stack
74 // address. We are not entirely sure that we have correct main thread
75 // limits, so only do this magic on Android, and only if the found thread is
76 // the main thread.
77 AsanThread* thread = FindThreadByStackAddress((uptr)&summary);
78 if (thread && thread->tid() == 0) {
79 SetCurrent(thread);
80 return thread;
81 }
82 #endif
83 return 0;
84 }
85 return summary->thread();
86 }
87
88 void AsanThreadRegistry::SetCurrent(AsanThread *t) {
89 CHECK(t->summary());
90 if (flags()->verbosity >= 2) {
91 Report("SetCurrent: %p for thread %p\n",
92 t->summary(), (void*)GetThreadSelf());
93 }
94 // Make sure we do not reset the current AsanThread.
95 CHECK(AsanTSDGet() == 0);
96 AsanTSDSet(t->summary());
97 CHECK(AsanTSDGet() == t->summary());
98 }
99
100 AsanStats &AsanThreadRegistry::GetCurrentThreadStats() {
101 AsanThread *t = GetCurrent();
102 return (t) ? t->stats() : main_thread_.stats();
103 }
104
105 void AsanThreadRegistry::GetAccumulatedStats(AsanStats *stats) {
106 ScopedLock lock(&mu_);
107 UpdateAccumulatedStatsUnlocked();
108 internal_memcpy(stats, &accumulated_stats_, sizeof(accumulated_stats_));
109 }
110
111 uptr AsanThreadRegistry::GetCurrentAllocatedBytes() {
112 ScopedLock lock(&mu_);
113 UpdateAccumulatedStatsUnlocked();
114 uptr malloced = accumulated_stats_.malloced;
115 uptr freed = accumulated_stats_.freed;
116 // Return sane value if malloced < freed due to racy
117 // way we update accumulated stats.
118 return (malloced > freed) ? malloced - freed : 1;
119 }
120
121 uptr AsanThreadRegistry::GetHeapSize() {
122 ScopedLock lock(&mu_);
123 UpdateAccumulatedStatsUnlocked();
124 return accumulated_stats_.mmaped;
125 }
126
127 uptr AsanThreadRegistry::GetFreeBytes() {
128 ScopedLock lock(&mu_);
129 UpdateAccumulatedStatsUnlocked();
130 uptr total_free = accumulated_stats_.mmaped
131 + accumulated_stats_.really_freed
132 + accumulated_stats_.really_freed_redzones;
133 uptr total_used = accumulated_stats_.malloced
134 + accumulated_stats_.malloced_redzones;
135 // Return sane value if total_free < total_used due to racy
136 // way we update accumulated stats.
137 return (total_free > total_used) ? total_free - total_used : 1;
138 }
139
140 // Return several stats counters with a single call to
141 // UpdateAccumulatedStatsUnlocked().
142 void AsanThreadRegistry::FillMallocStatistics(AsanMallocStats *malloc_stats) {
143 ScopedLock lock(&mu_);
144 UpdateAccumulatedStatsUnlocked();
145 malloc_stats->blocks_in_use = accumulated_stats_.mallocs;
146 malloc_stats->size_in_use = accumulated_stats_.malloced;
147 malloc_stats->max_size_in_use = max_malloced_memory_;
148 malloc_stats->size_allocated = accumulated_stats_.mmaped;
149 }
150
151 AsanThreadSummary *AsanThreadRegistry::FindByTid(u32 tid) {
152 CHECK(tid < n_threads_);
153 CHECK(thread_summaries_[tid]);
154 return thread_summaries_[tid];
155 }
156
157 AsanThread *AsanThreadRegistry::FindThreadByStackAddress(uptr addr) {
158 ScopedLock lock(&mu_);
159 for (u32 tid = 0; tid < n_threads_; tid++) {
160 AsanThread *t = thread_summaries_[tid]->thread();
161 if (!t || !(t->fake_stack().StackSize())) continue;
162 if (t->fake_stack().AddrIsInFakeStack(addr) || t->AddrIsInStack(addr)) {
163 return t;
164 }
165 }
166 return 0;
167 }
168
169 void AsanThreadRegistry::UpdateAccumulatedStatsUnlocked() {
170 for (u32 tid = 0; tid < n_threads_; tid++) {
171 AsanThread *t = thread_summaries_[tid]->thread();
172 if (t != 0) {
173 FlushToAccumulatedStatsUnlocked(&t->stats());
174 }
175 }
176 // This is not very accurate: we may miss allocation peaks that happen
177 // between two updates of accumulated_stats_. For more accurate bookkeeping
178 // the maximum should be updated on every malloc(), which is unacceptable.
179 if (max_malloced_memory_ < accumulated_stats_.malloced) {
180 max_malloced_memory_ = accumulated_stats_.malloced;
181 }
182 }
183
184 void AsanThreadRegistry::FlushToAccumulatedStatsUnlocked(AsanStats *stats) {
185 // AsanStats consists of variables of type uptr only.
186 uptr *dst = (uptr*)&accumulated_stats_;
187 uptr *src = (uptr*)stats;
188 uptr num_fields = sizeof(AsanStats) / sizeof(uptr);
189 for (uptr i = 0; i < num_fields; i++) {
190 dst[i] += src[i];
191 src[i] = 0;
192 }
193 }
194
195 } // namespace __asan