Home | History | Annotate | Line # | Download | only in lsan
lsan_common.h revision 1.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  mrg #include "sanitizer_common/sanitizer_symbolizer.h"
     21  1.1  mrg 
     22  1.1  mrg #if SANITIZER_LINUX && defined(__x86_64__) && (SANITIZER_WORDSIZE == 64)
     23  1.1  mrg #define CAN_SANITIZE_LEAKS 1
     24  1.1  mrg #else
     25  1.1  mrg #define CAN_SANITIZE_LEAKS 0
     26  1.1  mrg #endif
     27  1.1  mrg 
     28  1.1  mrg namespace __lsan {
     29  1.1  mrg 
     30  1.1  mrg // Chunk tags.
     31  1.1  mrg enum ChunkTag {
     32  1.1  mrg   kDirectlyLeaked = 0,  // default
     33  1.1  mrg   kIndirectlyLeaked = 1,
     34  1.1  mrg   kReachable = 2,
     35  1.1  mrg   kIgnored = 3
     36  1.1  mrg };
     37  1.1  mrg 
     38  1.1  mrg struct Flags {
     39  1.1  mrg   uptr pointer_alignment() const {
     40  1.1  mrg     return use_unaligned ? 1 : sizeof(uptr);
     41  1.1  mrg   }
     42  1.1  mrg 
     43  1.1  mrg   // Print addresses of leaked objects after main leak report.
     44  1.1  mrg   bool report_objects;
     45  1.1  mrg   // Aggregate two objects into one leak if this many stack frames match. If
     46  1.1  mrg   // zero, the entire stack trace must match.
     47  1.1  mrg   int resolution;
     48  1.1  mrg   // The number of leaks reported.
     49  1.1  mrg   int max_leaks;
     50  1.1  mrg   // If nonzero kill the process with this exit code upon finding leaks.
     51  1.1  mrg   int exitcode;
     52  1.1  mrg 
     53  1.1  mrg   // Flags controlling the root set of reachable memory.
     54  1.1  mrg   // Global variables (.data and .bss).
     55  1.1  mrg   bool use_globals;
     56  1.1  mrg   // Thread stacks.
     57  1.1  mrg   bool use_stacks;
     58  1.1  mrg   // Thread registers.
     59  1.1  mrg   bool use_registers;
     60  1.1  mrg   // TLS and thread-specific storage.
     61  1.1  mrg   bool use_tls;
     62  1.1  mrg   // Regions added via __lsan_register_root_region().
     63  1.1  mrg   bool use_root_regions;
     64  1.1  mrg 
     65  1.1  mrg   // Consider unaligned pointers valid.
     66  1.1  mrg   bool use_unaligned;
     67  1.1  mrg   // Consider pointers found in poisoned memory to be valid.
     68  1.1  mrg   bool use_poisoned;
     69  1.1  mrg 
     70  1.1  mrg   // Debug logging.
     71  1.1  mrg   bool log_pointers;
     72  1.1  mrg   bool log_threads;
     73  1.1  mrg };
     74  1.1  mrg 
     75  1.1  mrg extern Flags lsan_flags;
     76  1.1  mrg inline Flags *flags() { return &lsan_flags; }
     77  1.1  mrg 
     78  1.1  mrg struct Leak {
     79  1.1  mrg   u32 id;
     80  1.1  mrg   uptr hit_count;
     81  1.1  mrg   uptr total_size;
     82  1.1  mrg   u32 stack_trace_id;
     83  1.1  mrg   bool is_directly_leaked;
     84  1.1  mrg   bool is_suppressed;
     85  1.1  mrg };
     86  1.1  mrg 
     87  1.1  mrg struct LeakedObject {
     88  1.1  mrg   u32 leak_id;
     89  1.1  mrg   uptr addr;
     90  1.1  mrg   uptr size;
     91  1.1  mrg };
     92  1.1  mrg 
     93  1.1  mrg // Aggregates leaks by stack trace prefix.
     94  1.1  mrg class LeakReport {
     95  1.1  mrg  public:
     96  1.1  mrg   LeakReport() : next_id_(0), leaks_(1), leaked_objects_(1) {}
     97  1.1  mrg   void AddLeakedChunk(uptr chunk, u32 stack_trace_id, uptr leaked_size,
     98  1.1  mrg                       ChunkTag tag);
     99  1.1  mrg   void ReportTopLeaks(uptr max_leaks);
    100  1.1  mrg   void PrintSummary();
    101  1.1  mrg   void ApplySuppressions();
    102  1.1  mrg   uptr UnsuppressedLeakCount();
    103  1.1  mrg 
    104  1.1  mrg 
    105  1.1  mrg  private:
    106  1.1  mrg   void PrintReportForLeak(uptr index);
    107  1.1  mrg   void PrintLeakedObjectsForLeak(uptr index);
    108  1.1  mrg 
    109  1.1  mrg   u32 next_id_;
    110  1.1  mrg   InternalMmapVector<Leak> leaks_;
    111  1.1  mrg   InternalMmapVector<LeakedObject> leaked_objects_;
    112  1.1  mrg };
    113  1.1  mrg 
    114  1.1  mrg typedef InternalMmapVector<uptr> Frontier;
    115  1.1  mrg 
    116  1.1  mrg // Platform-specific functions.
    117  1.1  mrg void InitializePlatformSpecificModules();
    118  1.1  mrg void ProcessGlobalRegions(Frontier *frontier);
    119  1.1  mrg void ProcessPlatformSpecificAllocations(Frontier *frontier);
    120  1.1  mrg 
    121  1.1  mrg void ScanRangeForPointers(uptr begin, uptr end,
    122  1.1  mrg                           Frontier *frontier,
    123  1.1  mrg                           const char *region_type, ChunkTag tag);
    124  1.1  mrg 
    125  1.1  mrg enum IgnoreObjectResult {
    126  1.1  mrg   kIgnoreObjectSuccess,
    127  1.1  mrg   kIgnoreObjectAlreadyIgnored,
    128  1.1  mrg   kIgnoreObjectInvalid
    129  1.1  mrg };
    130  1.1  mrg 
    131  1.1  mrg // Functions called from the parent tool.
    132  1.1  mrg void InitCommonLsan(bool standalone);
    133  1.1  mrg void DoLeakCheck();
    134  1.1  mrg bool DisabledInThisThread();
    135  1.1  mrg 
    136  1.1  mrg // Special case for "new T[0]" where T is a type with DTOR.
    137  1.1  mrg // new T[0] will allocate one word for the array size (0) and store a pointer
    138  1.1  mrg // to the end of allocated chunk.
    139  1.1  mrg inline bool IsSpecialCaseOfOperatorNew0(uptr chunk_beg, uptr chunk_size,
    140  1.1  mrg                                         uptr addr) {
    141  1.1  mrg   return chunk_size == sizeof(uptr) && chunk_beg + chunk_size == addr &&
    142  1.1  mrg          *reinterpret_cast<uptr *>(chunk_beg) == 0;
    143  1.1  mrg }
    144  1.1  mrg 
    145  1.1  mrg // The following must be implemented in the parent tool.
    146  1.1  mrg 
    147  1.1  mrg void ForEachChunk(ForEachChunkCallback callback, void *arg);
    148  1.1  mrg // Returns the address range occupied by the global allocator object.
    149  1.1  mrg void GetAllocatorGlobalRange(uptr *begin, uptr *end);
    150  1.1  mrg // Wrappers for allocator's ForceLock()/ForceUnlock().
    151  1.1  mrg void LockAllocator();
    152  1.1  mrg void UnlockAllocator();
    153  1.1  mrg // Returns true if [addr, addr + sizeof(void *)) is poisoned.
    154  1.1  mrg bool WordIsPoisoned(uptr addr);
    155  1.1  mrg // Wrappers for ThreadRegistry access.
    156  1.1  mrg void LockThreadRegistry();
    157  1.1  mrg void UnlockThreadRegistry();
    158  1.1  mrg bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
    159  1.1  mrg                            uptr *tls_begin, uptr *tls_end,
    160  1.1  mrg                            uptr *cache_begin, uptr *cache_end);
    161  1.1  mrg void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback,
    162  1.1  mrg                             void *arg);
    163  1.1  mrg // If called from the main thread, updates the main thread's TID in the thread
    164  1.1  mrg // registry. We need this to handle processes that fork() without a subsequent
    165  1.1  mrg // exec(), which invalidates the recorded TID. To update it, we must call
    166  1.1  mrg // gettid() from the main thread. Our solution is to call this function before
    167  1.1  mrg // leak checking and also before every call to pthread_create() (to handle cases
    168  1.1  mrg // where leak checking is initiated from a non-main thread).
    169  1.1  mrg void EnsureMainThreadIDIsCorrect();
    170  1.1  mrg // If p points into a chunk that has been allocated to the user, returns its
    171  1.1  mrg // user-visible address. Otherwise, returns 0.
    172  1.1  mrg uptr PointsIntoChunk(void *p);
    173  1.1  mrg // Returns address of user-visible chunk contained in this allocator chunk.
    174  1.1  mrg uptr GetUserBegin(uptr chunk);
    175  1.1  mrg // Helper for __lsan_ignore_object().
    176  1.1  mrg IgnoreObjectResult IgnoreObjectLocked(const void *p);
    177  1.1  mrg // Wrapper for chunk metadata operations.
    178  1.1  mrg class LsanMetadata {
    179  1.1  mrg  public:
    180  1.1  mrg   // Constructor accepts address of user-visible chunk.
    181  1.1  mrg   explicit LsanMetadata(uptr chunk);
    182  1.1  mrg   bool allocated() const;
    183  1.1  mrg   ChunkTag tag() const;
    184  1.1  mrg   void set_tag(ChunkTag value);
    185  1.1  mrg   uptr requested_size() const;
    186  1.1  mrg   u32 stack_trace_id() const;
    187  1.1  mrg  private:
    188  1.1  mrg   void *metadata_;
    189  1.1  mrg };
    190  1.1  mrg 
    191  1.1  mrg }  // namespace __lsan
    192  1.1  mrg 
    193  1.1  mrg extern "C" {
    194  1.1  mrg SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
    195  1.1  mrg int __lsan_is_turned_off();
    196  1.1  mrg 
    197  1.1  mrg SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
    198  1.1  mrg const char *__lsan_default_suppressions();
    199  1.1  mrg }  // extern "C"
    200  1.1  mrg 
    201  1.1  mrg #endif  // LSAN_COMMON_H
    202