blob: acc27e52e224798d4fd8d535f11a93c8e48e84d9 [file] [log] [blame]
Kostya Serebryany019b76f2011-11-30 01:07:02 +00001//===-- 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"
Kostya Serebryany78713bc2012-07-17 07:20:13 +000021#include "sanitizer_common/sanitizer_libc.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000022
23namespace __asan {
24
Kostya Serebryany79437fe2012-06-06 15:06:58 +000025const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits.
26
Kostya Serebryany019b76f2011-11-30 01:07:02 +000027class AsanThread;
28
29// These objects are created for every thread and are never deleted,
30// so we can find them by tid even if the thread is long dead.
31class AsanThreadSummary {
32 public:
33 explicit AsanThreadSummary(LinkerInitialized) { } // for T0.
Kostya Serebryany6b0d7752012-08-28 11:54:30 +000034 void Init(u32 parent_tid, StackTrace *stack) {
Kostya Serebryany78713bc2012-07-17 07:20:13 +000035 parent_tid_ = parent_tid;
36 announced_ = false;
Kostya Serebryany79437fe2012-06-06 15:06:58 +000037 tid_ = kInvalidTid;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000038 if (stack) {
Kostya Serebryany78713bc2012-07-17 07:20:13 +000039 internal_memcpy(&stack_, stack, sizeof(*stack));
Kostya Serebryany019b76f2011-11-30 01:07:02 +000040 }
41 thread_ = 0;
Kostya Serebryanye7108222012-12-07 15:15:01 +000042 name_[0] = 0;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000043 }
Kostya Serebryany79437fe2012-06-06 15:06:58 +000044 u32 tid() { return tid_; }
45 void set_tid(u32 tid) { tid_ = tid; }
Alexey Samsonovc402cb62012-09-05 07:37:15 +000046 u32 parent_tid() { return parent_tid_; }
47 bool announced() { return announced_; }
48 void set_announced(bool announced) { announced_ = announced; }
49 StackTrace *stack() { return &stack_; }
Kostya Serebryany019b76f2011-11-30 01:07:02 +000050 AsanThread *thread() { return thread_; }
51 void set_thread(AsanThread *thread) { thread_ = thread; }
Kostya Serebryanyb5eb5a72012-02-07 00:27:15 +000052 static void TSDDtor(void *tsd);
Kostya Serebryanye7108222012-12-07 15:15:01 +000053 void set_name(const char *name) {
54 internal_strncpy(name_, name, sizeof(name_) - 1);
55 }
56 const char *name() { return name_; }
Kostya Serebryanyb5eb5a72012-02-07 00:27:15 +000057
Kostya Serebryany019b76f2011-11-30 01:07:02 +000058 private:
Kostya Serebryany79437fe2012-06-06 15:06:58 +000059 u32 tid_;
60 u32 parent_tid_;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000061 bool announced_;
Kostya Serebryany6b0d7752012-08-28 11:54:30 +000062 StackTrace stack_;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000063 AsanThread *thread_;
Kostya Serebryanye7108222012-12-07 15:15:01 +000064 char name_[128];
Kostya Serebryany019b76f2011-11-30 01:07:02 +000065};
66
Kostya Serebryanye7108222012-12-07 15:15:01 +000067// AsanThreadSummary objects are never freed, so we need many of them.
68COMPILER_CHECK(sizeof(AsanThreadSummary) <= 4094);
69
Kostya Serebryany019b76f2011-11-30 01:07:02 +000070// AsanThread are stored in TSD and destroyed when the thread dies.
71class AsanThread {
72 public:
73 explicit AsanThread(LinkerInitialized); // for T0.
Kostya Serebryany79437fe2012-06-06 15:06:58 +000074 static AsanThread *Create(u32 parent_tid, thread_callback_t start_routine,
Kostya Serebryany6b0d7752012-08-28 11:54:30 +000075 void *arg, StackTrace *stack);
Kostya Serebryany3f4b9bb2012-01-06 19:44:11 +000076 void Destroy();
Kostya Serebryany019b76f2011-11-30 01:07:02 +000077
Kostya Serebryany6bb2f1d2011-12-16 19:13:35 +000078 void Init(); // Should be called from the thread itself.
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +000079 thread_return_t ThreadStart();
Kostya Serebryany019b76f2011-11-30 01:07:02 +000080
Kostya Serebryany8d032042012-05-31 14:35:53 +000081 uptr stack_top() { return stack_top_; }
82 uptr stack_bottom() { return stack_bottom_; }
83 uptr stack_size() { return stack_top_ - stack_bottom_; }
Kostya Serebryany79437fe2012-06-06 15:06:58 +000084 u32 tid() { return summary_->tid(); }
Kostya Serebryany019b76f2011-11-30 01:07:02 +000085 AsanThreadSummary *summary() { return summary_; }
86 void set_summary(AsanThreadSummary *summary) { summary_ = summary; }
87
Kostya Serebryany8d032042012-05-31 14:35:53 +000088 const char *GetFrameNameByAddr(uptr addr, uptr *offset);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000089
Kostya Serebryany8d032042012-05-31 14:35:53 +000090 bool AddrIsInStack(uptr addr) {
Kostya Serebryany019b76f2011-11-30 01:07:02 +000091 return addr >= stack_bottom_ && addr < stack_top_;
92 }
93
94 FakeStack &fake_stack() { return fake_stack_; }
95 AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
96 AsanStats &stats() { return stats_; }
97
Kostya Serebryany019b76f2011-11-30 01:07:02 +000098 private:
Kostya Serebryany019b76f2011-11-30 01:07:02 +000099 void SetThreadStackTopAndBottom();
100 void ClearShadowForThreadStack();
101 AsanThreadSummary *summary_;
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000102 thread_callback_t start_routine_;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000103 void *arg_;
Kostya Serebryany8d032042012-05-31 14:35:53 +0000104 uptr stack_top_;
105 uptr stack_bottom_;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000106
107 FakeStack fake_stack_;
108 AsanThreadLocalMallocStorage malloc_storage_;
109 AsanStats stats_;
110};
111
112} // namespace __asan
113
114#endif // ASAN_THREAD_H