lsan_common.h revision 9fbfd96608070eb71e11fbfe42ec9e84016429ae
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__)
25#define CAN_SANITIZE_LEAKS 1
26#else
27#define CAN_SANITIZE_LEAKS 0
28#endif
29
30namespace __lsan {
31
32// Chunk tags.
33enum ChunkTag {
34  kDirectlyLeaked = 0,  // default
35  kIndirectlyLeaked = 1,
36  kReachable = 2,
37  kIgnored = 3
38};
39
40struct Flags {
41  uptr pointer_alignment() const {
42    return use_unaligned ? 1 : sizeof(uptr);
43  }
44
45  // Print addresses of leaked objects after main leak report.
46  bool report_objects;
47  // Aggregate two objects into one leak if this many stack frames match. If
48  // zero, the entire stack trace must match.
49  int resolution;
50  // The number of leaks reported.
51  int max_leaks;
52  // If nonzero kill the process with this exit code upon finding leaks.
53  int exitcode;
54
55  // Flags controlling the root set of reachable memory.
56  // Global variables (.data and .bss).
57  bool use_globals;
58  // Thread stacks.
59  bool use_stacks;
60  // Thread registers.
61  bool use_registers;
62  // TLS and thread-specific storage.
63  bool use_tls;
64
65  // Consider unaligned pointers valid.
66  bool use_unaligned;
67
68  // User-visible verbosity.
69  int verbosity;
70
71  // Debug logging.
72  bool log_pointers;
73  bool log_threads;
74};
75
76extern Flags lsan_flags;
77inline Flags *flags() { return &lsan_flags; }
78
79struct Leak {
80  uptr hit_count;
81  uptr total_size;
82  u32 stack_trace_id;
83  bool is_directly_leaked;
84};
85
86// Aggregates leaks by stack trace prefix.
87class LeakReport {
88 public:
89  LeakReport() : leaks_(1) {}
90  void Add(u32 stack_trace_id, uptr leaked_size, ChunkTag tag);
91  void PrintLargest(uptr max_leaks);
92  void PrintSummary();
93  bool IsEmpty() { return leaks_.size() == 0; }
94 private:
95  InternalMmapVector<Leak> leaks_;
96};
97
98typedef InternalMmapVector<uptr> Frontier;
99
100// Platform-specific functions.
101void InitializePlatformSpecificModules();
102void ProcessGlobalRegions(Frontier *frontier);
103void ProcessPlatformSpecificAllocations(Frontier *frontier);
104
105void ScanRangeForPointers(uptr begin, uptr end,
106                          Frontier *frontier,
107                          const char *region_type, ChunkTag tag);
108
109enum IgnoreObjectResult {
110  kIgnoreObjectSuccess,
111  kIgnoreObjectAlreadyIgnored,
112  kIgnoreObjectInvalid
113};
114
115// Functions called from the parent tool.
116void InitCommonLsan();
117void DoLeakCheck();
118bool DisabledInThisThread();
119
120// The following must be implemented in the parent tool.
121
122void ForEachChunk(ForEachChunkCallback callback, void *arg);
123// Returns the address range occupied by the global allocator object.
124void GetAllocatorGlobalRange(uptr *begin, uptr *end);
125// Wrappers for allocator's ForceLock()/ForceUnlock().
126void LockAllocator();
127void UnlockAllocator();
128// Wrappers for ThreadRegistry access.
129void LockThreadRegistry();
130void UnlockThreadRegistry();
131bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
132                           uptr *tls_begin, uptr *tls_end,
133                           uptr *cache_begin, uptr *cache_end);
134// If p points into a chunk that has been allocated to the user, returns its
135// user-visible address. Otherwise, returns 0.
136uptr PointsIntoChunk(void *p);
137// Returns address of user-visible chunk contained in this allocator chunk.
138uptr GetUserBegin(uptr chunk);
139// Helper for __lsan_ignore_object().
140IgnoreObjectResult IgnoreObjectLocked(const void *p);
141// Wrapper for chunk metadata operations.
142class LsanMetadata {
143 public:
144  // Constructor accepts address of user-visible chunk.
145  explicit LsanMetadata(uptr chunk);
146  bool allocated() const;
147  ChunkTag tag() const;
148  void set_tag(ChunkTag value);
149  uptr requested_size() const;
150  u32 stack_trace_id() const;
151 private:
152  void *metadata_;
153};
154
155}  // namespace __lsan
156
157extern "C" {
158int __lsan_is_turned_off() SANITIZER_WEAK_ATTRIBUTE
159    SANITIZER_INTERFACE_ATTRIBUTE;
160}  // extern "C"
161
162#endif  // LSAN_COMMON_H
163