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