]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/lsan/lsan_common.h
Update compiler-rt to trunk r228651. This enables using Address
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / lsan / lsan_common.h
1 //=-- lsan_common.h -------------------------------------------------------===//
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 LeakSanitizer.
11 // Private LSan header.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LSAN_COMMON_H
16 #define LSAN_COMMON_H
17
18 #include "sanitizer_common/sanitizer_allocator.h"
19 #include "sanitizer_common/sanitizer_common.h"
20 #include "sanitizer_common/sanitizer_internal_defs.h"
21 #include "sanitizer_common/sanitizer_platform.h"
22 #include "sanitizer_common/sanitizer_symbolizer.h"
23
24 #if SANITIZER_LINUX && defined(__x86_64__) && (SANITIZER_WORDSIZE == 64)
25 #define CAN_SANITIZE_LEAKS 1
26 #else
27 #define CAN_SANITIZE_LEAKS 0
28 #endif
29
30 namespace __lsan {
31
32 // Chunk tags.
33 enum ChunkTag {
34   kDirectlyLeaked = 0,  // default
35   kIndirectlyLeaked = 1,
36   kReachable = 2,
37   kIgnored = 3
38 };
39
40 struct Flags {
41 #define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name;
42 #include "lsan_flags.inc"
43 #undef LSAN_FLAG
44
45   void SetDefaults();
46   uptr pointer_alignment() const {
47     return use_unaligned ? 1 : sizeof(uptr);
48   }
49 };
50
51 extern Flags lsan_flags;
52 inline Flags *flags() { return &lsan_flags; }
53
54 struct Leak {
55   u32 id;
56   uptr hit_count;
57   uptr total_size;
58   u32 stack_trace_id;
59   bool is_directly_leaked;
60   bool is_suppressed;
61 };
62
63 struct LeakedObject {
64   u32 leak_id;
65   uptr addr;
66   uptr size;
67 };
68
69 // Aggregates leaks by stack trace prefix.
70 class LeakReport {
71  public:
72   LeakReport() : next_id_(0), leaks_(1), leaked_objects_(1) {}
73   void AddLeakedChunk(uptr chunk, u32 stack_trace_id, uptr leaked_size,
74                       ChunkTag tag);
75   void ReportTopLeaks(uptr max_leaks);
76   void PrintSummary();
77   void ApplySuppressions();
78   uptr UnsuppressedLeakCount();
79
80
81  private:
82   void PrintReportForLeak(uptr index);
83   void PrintLeakedObjectsForLeak(uptr index);
84
85   u32 next_id_;
86   InternalMmapVector<Leak> leaks_;
87   InternalMmapVector<LeakedObject> leaked_objects_;
88 };
89
90 typedef InternalMmapVector<uptr> Frontier;
91
92 // Platform-specific functions.
93 void InitializePlatformSpecificModules();
94 void ProcessGlobalRegions(Frontier *frontier);
95 void ProcessPlatformSpecificAllocations(Frontier *frontier);
96
97 void ScanRangeForPointers(uptr begin, uptr end,
98                           Frontier *frontier,
99                           const char *region_type, ChunkTag tag);
100
101 enum IgnoreObjectResult {
102   kIgnoreObjectSuccess,
103   kIgnoreObjectAlreadyIgnored,
104   kIgnoreObjectInvalid
105 };
106
107 // Functions called from the parent tool.
108 void InitCommonLsan(bool standalone);
109 void DoLeakCheck();
110 bool DisabledInThisThread();
111
112 // Special case for "new T[0]" where T is a type with DTOR.
113 // new T[0] will allocate one word for the array size (0) and store a pointer
114 // to the end of allocated chunk.
115 inline bool IsSpecialCaseOfOperatorNew0(uptr chunk_beg, uptr chunk_size,
116                                         uptr addr) {
117   return chunk_size == sizeof(uptr) && chunk_beg + chunk_size == addr &&
118          *reinterpret_cast<uptr *>(chunk_beg) == 0;
119 }
120
121 // The following must be implemented in the parent tool.
122
123 void ForEachChunk(ForEachChunkCallback callback, void *arg);
124 // Returns the address range occupied by the global allocator object.
125 void GetAllocatorGlobalRange(uptr *begin, uptr *end);
126 // Wrappers for allocator's ForceLock()/ForceUnlock().
127 void LockAllocator();
128 void UnlockAllocator();
129 // Returns true if [addr, addr + sizeof(void *)) is poisoned.
130 bool WordIsPoisoned(uptr addr);
131 // Wrappers for ThreadRegistry access.
132 void LockThreadRegistry();
133 void UnlockThreadRegistry();
134 bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
135                            uptr *tls_begin, uptr *tls_end,
136                            uptr *cache_begin, uptr *cache_end);
137 void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback,
138                             void *arg);
139 // If called from the main thread, updates the main thread's TID in the thread
140 // registry. We need this to handle processes that fork() without a subsequent
141 // exec(), which invalidates the recorded TID. To update it, we must call
142 // gettid() from the main thread. Our solution is to call this function before
143 // leak checking and also before every call to pthread_create() (to handle cases
144 // where leak checking is initiated from a non-main thread).
145 void EnsureMainThreadIDIsCorrect();
146 // If p points into a chunk that has been allocated to the user, returns its
147 // user-visible address. Otherwise, returns 0.
148 uptr PointsIntoChunk(void *p);
149 // Returns address of user-visible chunk contained in this allocator chunk.
150 uptr GetUserBegin(uptr chunk);
151 // Helper for __lsan_ignore_object().
152 IgnoreObjectResult IgnoreObjectLocked(const void *p);
153 // Wrapper for chunk metadata operations.
154 class LsanMetadata {
155  public:
156   // Constructor accepts address of user-visible chunk.
157   explicit LsanMetadata(uptr chunk);
158   bool allocated() const;
159   ChunkTag tag() const;
160   void set_tag(ChunkTag value);
161   uptr requested_size() const;
162   u32 stack_trace_id() const;
163  private:
164   void *metadata_;
165 };
166
167 }  // namespace __lsan
168
169 extern "C" {
170 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
171 int __lsan_is_turned_off();
172
173 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
174 const char *__lsan_default_suppressions();
175 }  // extern "C"
176
177 #endif  // LSAN_COMMON_H