Home | History | Annotate | Line # | Download | only in lsan
lsan_common.h revision 1.7
      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.2  christos #include "sanitizer_common/sanitizer_stoptheworld.h"
     21  1.1       mrg #include "sanitizer_common/sanitizer_symbolizer.h"
     22  1.1       mrg 
     23  1.6       mrg // LeakSanitizer relies on some Glibc's internals (e.g. TLS machinery) thus
     24  1.6       mrg // supported for Linux only. Also, LSan doesn't like 32 bit architectures
     25  1.6       mrg // because of "small" (4 bytes) pointer size that leads to high false negative
     26  1.6       mrg // ratio on large leaks. But we still want to have it for some 32 bit arches
     27  1.6       mrg // (e.g. x86), see https://github.com/google/sanitizers/issues/403.
     28  1.7       mrg // To enable LeakSanitizer on a new architecture, one needs to implement the
     29  1.7       mrg // internal_clone function as well as (probably) adjust the TLS machinery for
     30  1.7       mrg // the new architecture inside the sanitizer library.
     31  1.6       mrg #if (SANITIZER_LINUX && !SANITIZER_ANDROID || SANITIZER_MAC || SANITIZER_NETBSD) && \
     32  1.6       mrg     (SANITIZER_WORDSIZE == 64) &&                               \
     33  1.6       mrg     (defined(__x86_64__) || defined(__mips64) || defined(__aarch64__) || \
     34  1.6       mrg      defined(__powerpc64__))
     35  1.6       mrg #define CAN_SANITIZE_LEAKS 1
     36  1.6       mrg #elif defined(__i386__) && \
     37  1.6       mrg     ((SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_MAC || SANITIZER_NETBSD)
     38  1.6       mrg #define CAN_SANITIZE_LEAKS 1
     39  1.6       mrg #elif defined(__arm__) && \
     40  1.6       mrg     ((SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_NETBSD)
     41  1.1       mrg #define CAN_SANITIZE_LEAKS 1
     42  1.1       mrg #else
     43  1.1       mrg #define CAN_SANITIZE_LEAKS 0
     44  1.1       mrg #endif
     45  1.1       mrg 
     46  1.2  christos namespace __sanitizer {
     47  1.2  christos class FlagParser;
     48  1.7       mrg class ThreadRegistry;
     49  1.3       mrg struct DTLS;
     50  1.2  christos }
     51  1.2  christos 
     52  1.1       mrg namespace __lsan {
     53  1.1       mrg 
     54  1.1       mrg // Chunk tags.
     55  1.1       mrg enum ChunkTag {
     56  1.1       mrg   kDirectlyLeaked = 0,  // default
     57  1.1       mrg   kIndirectlyLeaked = 1,
     58  1.1       mrg   kReachable = 2,
     59  1.1       mrg   kIgnored = 3
     60  1.1       mrg };
     61  1.1       mrg 
     62  1.6       mrg const u32 kInvalidTid = (u32) -1;
     63  1.6       mrg 
     64  1.1       mrg struct Flags {
     65  1.2  christos #define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name;
     66  1.2  christos #include "lsan_flags.inc"
     67  1.2  christos #undef LSAN_FLAG
     68  1.2  christos 
     69  1.2  christos   void SetDefaults();
     70  1.1       mrg   uptr pointer_alignment() const {
     71  1.1       mrg     return use_unaligned ? 1 : sizeof(uptr);
     72  1.1       mrg   }
     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.2  christos void RegisterLsanFlags(FlagParser *parser, Flags *f);
     78  1.1       mrg 
     79  1.1       mrg struct Leak {
     80  1.1       mrg   u32 id;
     81  1.1       mrg   uptr hit_count;
     82  1.1       mrg   uptr total_size;
     83  1.1       mrg   u32 stack_trace_id;
     84  1.1       mrg   bool is_directly_leaked;
     85  1.1       mrg   bool is_suppressed;
     86  1.1       mrg };
     87  1.1       mrg 
     88  1.1       mrg struct LeakedObject {
     89  1.1       mrg   u32 leak_id;
     90  1.1       mrg   uptr addr;
     91  1.1       mrg   uptr size;
     92  1.1       mrg };
     93  1.1       mrg 
     94  1.1       mrg // Aggregates leaks by stack trace prefix.
     95  1.1       mrg class LeakReport {
     96  1.1       mrg  public:
     97  1.7       mrg   LeakReport() {}
     98  1.1       mrg   void AddLeakedChunk(uptr chunk, u32 stack_trace_id, uptr leaked_size,
     99  1.1       mrg                       ChunkTag tag);
    100  1.1       mrg   void ReportTopLeaks(uptr max_leaks);
    101  1.1       mrg   void PrintSummary();
    102  1.1       mrg   void ApplySuppressions();
    103  1.1       mrg   uptr UnsuppressedLeakCount();
    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.7       mrg   u32 next_id_ = 0;
    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.6       mrg 
    121  1.6       mrg struct RootRegion {
    122  1.6       mrg   uptr begin;
    123  1.6       mrg   uptr size;
    124  1.6       mrg };
    125  1.6       mrg 
    126  1.6       mrg InternalMmapVector<RootRegion> const *GetRootRegions();
    127  1.6       mrg void ScanRootRegion(Frontier *frontier, RootRegion const &region,
    128  1.6       mrg                     uptr region_begin, uptr region_end, bool is_readable);
    129  1.2  christos // Run stoptheworld while holding any platform-specific locks.
    130  1.2  christos void DoStopTheWorld(StopTheWorldCallback callback, void* argument);
    131  1.1       mrg 
    132  1.1       mrg void ScanRangeForPointers(uptr begin, uptr end,
    133  1.1       mrg                           Frontier *frontier,
    134  1.1       mrg                           const char *region_type, ChunkTag tag);
    135  1.6       mrg void ScanGlobalRange(uptr begin, uptr end, Frontier *frontier);
    136  1.1       mrg 
    137  1.1       mrg enum IgnoreObjectResult {
    138  1.1       mrg   kIgnoreObjectSuccess,
    139  1.1       mrg   kIgnoreObjectAlreadyIgnored,
    140  1.1       mrg   kIgnoreObjectInvalid
    141  1.1       mrg };
    142  1.1       mrg 
    143  1.1       mrg // Functions called from the parent tool.
    144  1.6       mrg const char *MaybeCallLsanDefaultOptions();
    145  1.2  christos void InitCommonLsan();
    146  1.1       mrg void DoLeakCheck();
    147  1.6       mrg void DoRecoverableLeakCheckVoid();
    148  1.6       mrg void DisableCounterUnderflow();
    149  1.1       mrg bool DisabledInThisThread();
    150  1.1       mrg 
    151  1.3       mrg // Used to implement __lsan::ScopedDisabler.
    152  1.3       mrg void DisableInThisThread();
    153  1.3       mrg void EnableInThisThread();
    154  1.3       mrg // Can be used to ignore memory allocated by an intercepted
    155  1.3       mrg // function.
    156  1.3       mrg struct ScopedInterceptorDisabler {
    157  1.3       mrg   ScopedInterceptorDisabler() { DisableInThisThread(); }
    158  1.3       mrg   ~ScopedInterceptorDisabler() { EnableInThisThread(); }
    159  1.3       mrg };
    160  1.3       mrg 
    161  1.6       mrg // According to Itanium C++ ABI array cookie is a one word containing
    162  1.6       mrg // size of allocated array.
    163  1.6       mrg static inline bool IsItaniumABIArrayCookie(uptr chunk_beg, uptr chunk_size,
    164  1.6       mrg                                            uptr addr) {
    165  1.6       mrg   return chunk_size == sizeof(uptr) && chunk_beg + chunk_size == addr &&
    166  1.6       mrg          *reinterpret_cast<uptr *>(chunk_beg) == 0;
    167  1.6       mrg }
    168  1.6       mrg 
    169  1.6       mrg // According to ARM C++ ABI array cookie consists of two words:
    170  1.6       mrg // struct array_cookie {
    171  1.6       mrg //   std::size_t element_size; // element_size != 0
    172  1.6       mrg //   std::size_t element_count;
    173  1.6       mrg // };
    174  1.6       mrg static inline bool IsARMABIArrayCookie(uptr chunk_beg, uptr chunk_size,
    175  1.6       mrg                                        uptr addr) {
    176  1.6       mrg   return chunk_size == 2 * sizeof(uptr) && chunk_beg + chunk_size == addr &&
    177  1.6       mrg          *reinterpret_cast<uptr *>(chunk_beg + sizeof(uptr)) == 0;
    178  1.6       mrg }
    179  1.6       mrg 
    180  1.1       mrg // Special case for "new T[0]" where T is a type with DTOR.
    181  1.6       mrg // new T[0] will allocate a cookie (one or two words) for the array size (0)
    182  1.6       mrg // and store a pointer to the end of allocated chunk. The actual cookie layout
    183  1.6       mrg // varies between platforms according to their C++ ABI implementation.
    184  1.1       mrg inline bool IsSpecialCaseOfOperatorNew0(uptr chunk_beg, uptr chunk_size,
    185  1.1       mrg                                         uptr addr) {
    186  1.6       mrg #if defined(__arm__)
    187  1.6       mrg   return IsARMABIArrayCookie(chunk_beg, chunk_size, addr);
    188  1.6       mrg #else
    189  1.6       mrg   return IsItaniumABIArrayCookie(chunk_beg, chunk_size, addr);
    190  1.6       mrg #endif
    191  1.1       mrg }
    192  1.1       mrg 
    193  1.1       mrg // The following must be implemented in the parent tool.
    194  1.1       mrg 
    195  1.1       mrg void ForEachChunk(ForEachChunkCallback callback, void *arg);
    196  1.1       mrg // Returns the address range occupied by the global allocator object.
    197  1.1       mrg void GetAllocatorGlobalRange(uptr *begin, uptr *end);
    198  1.1       mrg // Wrappers for allocator's ForceLock()/ForceUnlock().
    199  1.1       mrg void LockAllocator();
    200  1.1       mrg void UnlockAllocator();
    201  1.1       mrg // Returns true if [addr, addr + sizeof(void *)) is poisoned.
    202  1.1       mrg bool WordIsPoisoned(uptr addr);
    203  1.1       mrg // Wrappers for ThreadRegistry access.
    204  1.1       mrg void LockThreadRegistry();
    205  1.1       mrg void UnlockThreadRegistry();
    206  1.7       mrg ThreadRegistry *GetThreadRegistryLocked();
    207  1.6       mrg bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
    208  1.3       mrg                            uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
    209  1.3       mrg                            uptr *cache_end, DTLS **dtls);
    210  1.6       mrg void ForEachExtraStackRange(tid_t os_id, RangeIteratorCallback callback,
    211  1.1       mrg                             void *arg);
    212  1.1       mrg // If called from the main thread, updates the main thread's TID in the thread
    213  1.1       mrg // registry. We need this to handle processes that fork() without a subsequent
    214  1.1       mrg // exec(), which invalidates the recorded TID. To update it, we must call
    215  1.1       mrg // gettid() from the main thread. Our solution is to call this function before
    216  1.1       mrg // leak checking and also before every call to pthread_create() (to handle cases
    217  1.1       mrg // where leak checking is initiated from a non-main thread).
    218  1.1       mrg void EnsureMainThreadIDIsCorrect();
    219  1.1       mrg // If p points into a chunk that has been allocated to the user, returns its
    220  1.1       mrg // user-visible address. Otherwise, returns 0.
    221  1.1       mrg uptr PointsIntoChunk(void *p);
    222  1.1       mrg // Returns address of user-visible chunk contained in this allocator chunk.
    223  1.1       mrg uptr GetUserBegin(uptr chunk);
    224  1.1       mrg // Helper for __lsan_ignore_object().
    225  1.1       mrg IgnoreObjectResult IgnoreObjectLocked(const void *p);
    226  1.6       mrg 
    227  1.6       mrg // Return the linker module, if valid for the platform.
    228  1.6       mrg LoadedModule *GetLinker();
    229  1.6       mrg 
    230  1.6       mrg // Return true if LSan has finished leak checking and reported leaks.
    231  1.6       mrg bool HasReportedLeaks();
    232  1.6       mrg 
    233  1.6       mrg // Run platform-specific leak handlers.
    234  1.6       mrg void HandleLeaks();
    235  1.6       mrg 
    236  1.1       mrg // Wrapper for chunk metadata operations.
    237  1.1       mrg class LsanMetadata {
    238  1.1       mrg  public:
    239  1.1       mrg   // Constructor accepts address of user-visible chunk.
    240  1.1       mrg   explicit LsanMetadata(uptr chunk);
    241  1.1       mrg   bool allocated() const;
    242  1.1       mrg   ChunkTag tag() const;
    243  1.1       mrg   void set_tag(ChunkTag value);
    244  1.1       mrg   uptr requested_size() const;
    245  1.1       mrg   u32 stack_trace_id() const;
    246  1.1       mrg  private:
    247  1.1       mrg   void *metadata_;
    248  1.1       mrg };
    249  1.1       mrg 
    250  1.1       mrg }  // namespace __lsan
    251  1.1       mrg 
    252  1.1       mrg extern "C" {
    253  1.1       mrg SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
    254  1.6       mrg const char *__lsan_default_options();
    255  1.6       mrg 
    256  1.6       mrg SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
    257  1.1       mrg int __lsan_is_turned_off();
    258  1.1       mrg 
    259  1.1       mrg SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
    260  1.1       mrg const char *__lsan_default_suppressions();
    261  1.1       mrg }  // extern "C"
    262  1.1       mrg 
    263  1.1       mrg #endif  // LSAN_COMMON_H
    264