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