blob: 45351a7a7af2b11bdb4e5b80055b69c2c52aef0b [file] [log] [blame]
Kostya Serebryany019b76f2011-11-30 01:07:021//===-- asan_thread.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.cc.
13//===----------------------------------------------------------------------===//
14#ifndef ASAN_THREAD_H
15#define ASAN_THREAD_H
16
17#include "asan_allocator.h"
18#include "asan_internal.h"
19#include "asan_stack.h"
20#include "asan_stats.h"
21
22namespace __asan {
23
24const size_t kMaxThreadStackSize = 16 * (1 << 20); // 16M
25
26class AsanThread;
27
28// These objects are created for every thread and are never deleted,
29// so we can find them by tid even if the thread is long dead.
30class AsanThreadSummary {
31 public:
32 explicit AsanThreadSummary(LinkerInitialized) { } // for T0.
Alexey Samsonov2d3a67b2012-01-17 06:35:3133 AsanThreadSummary(int parent_tid, AsanStackTrace *stack)
34 : parent_tid_(parent_tid),
Kostya Serebryany019b76f2011-11-30 01:07:0235 announced_(false) {
Alexey Samsonov2d3a67b2012-01-17 06:35:3136 tid_ = -1;
Kostya Serebryany019b76f2011-11-30 01:07:0237 if (stack) {
38 stack_ = *stack;
39 }
40 thread_ = 0;
41 }
42 void Announce() {
43 if (tid_ == 0) return; // no need to announce the main thread.
44 if (!announced_) {
45 announced_ = true;
46 Printf("Thread T%d created by T%d here:\n", tid_, parent_tid_);
47 stack_.PrintStack();
48 }
49 }
50 int tid() { return tid_; }
Alexey Samsonov2d3a67b2012-01-17 06:35:3151 void set_tid(int tid) { tid_ = tid; }
Kostya Serebryany019b76f2011-11-30 01:07:0252 AsanThread *thread() { return thread_; }
53 void set_thread(AsanThread *thread) { thread_ = thread; }
54 private:
55 int tid_;
56 int parent_tid_;
57 bool announced_;
58 AsanStackTrace stack_;
59 AsanThread *thread_;
60};
61
62// AsanThread are stored in TSD and destroyed when the thread dies.
63class AsanThread {
64 public:
65 explicit AsanThread(LinkerInitialized); // for T0.
Kostya Serebryany3f4b9bb2012-01-06 19:44:1166 static AsanThread *Create(int parent_tid, void *(*start_routine) (void *),
Alexey Samsonov2d3a67b2012-01-17 06:35:3167 void *arg, AsanStackTrace *stack);
Kostya Serebryany3f4b9bb2012-01-06 19:44:1168 void Destroy();
Kostya Serebryany019b76f2011-11-30 01:07:0269
Kostya Serebryany6bb2f1d2011-12-16 19:13:3570 void Init(); // Should be called from the thread itself.
Kostya Serebryany019b76f2011-11-30 01:07:0271 void *ThreadStart();
72
73 uintptr_t stack_top() { return stack_top_; }
74 uintptr_t stack_bottom() { return stack_bottom_; }
75 size_t stack_size() { return stack_top_ - stack_bottom_; }
76 int tid() { return summary_->tid(); }
77 AsanThreadSummary *summary() { return summary_; }
78 void set_summary(AsanThreadSummary *summary) { summary_ = summary; }
79
80 const char *GetFrameNameByAddr(uintptr_t addr, uintptr_t *offset);
81
82 bool AddrIsInStack(uintptr_t addr) {
83 return addr >= stack_bottom_ && addr < stack_top_;
84 }
85
86 FakeStack &fake_stack() { return fake_stack_; }
87 AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
88 AsanStats &stats() { return stats_; }
89
90 static const int kInvalidTid = -1;
91
92 private:
93
94 void SetThreadStackTopAndBottom();
95 void ClearShadowForThreadStack();
96 AsanThreadSummary *summary_;
97 void *(*start_routine_) (void *param);
98 void *arg_;
99 uintptr_t stack_top_;
100 uintptr_t stack_bottom_;
101
102 FakeStack fake_stack_;
103 AsanThreadLocalMallocStorage malloc_storage_;
104 AsanStats stats_;
105};
106
107} // namespace __asan
108
109#endif // ASAN_THREAD_H