Home | History | Annotate | Line # | Download | only in lsan
lsan_common.h revision 1.4
      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.3  mrg #include "sanitizer_common/sanitizer_stoptheworld.h"
     21  1.1  mrg #include "sanitizer_common/sanitizer_symbolizer.h"
     22  1.1  mrg 
     23  1.4  mrg // LeakSanitizer relies on some Glibc's internals (e.g. TLS machinery) thus
     24  1.4  mrg // supported for Linux only. Also, LSan doesn't like 32 bit architectures
     25  1.4  mrg // because of "small" (4 bytes) pointer size that leads to high false negative
     26  1.4  mrg // ratio on large leaks. But we still want to have it for some 32 bit arches
     27  1.4  mrg // (e.g. x86), see https://github.com/google/sanitizers/issues/403.
     28  1.4  mrg // To enable LeakSanitizer on new architecture, one need to implement
     29  1.4  mrg // internal_clone function as well as (probably) adjust TLS machinery for
     30  1.4  mrg // new architecture inside sanitizer library.
     31  1.4  mrg #if (SANITIZER_LINUX && !SANITIZER_ANDROID || SANITIZER_MAC || SANITIZER_NETBSD) && \
     32  1.4  mrg     (SANITIZER_WORDSIZE == 64) &&                               \
     33  1.4  mrg     (defined(__x86_64__) || defined(__mips64) || defined(__aarch64__) || \
     34  1.4  mrg      defined(__powerpc64__))
     35  1.4  mrg #define CAN_SANITIZE_LEAKS 1
     36  1.4  mrg #elif defined(__i386__) && \
     37  1.4  mrg     ((SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_MAC || SANITIZER_NETBSD)
     38  1.4  mrg #define CAN_SANITIZE_LEAKS 1
     39  1.4  mrg #elif defined(__arm__) && \
     40  1.4  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.3  mrg namespace __sanitizer {
     47  1.3  mrg class FlagParser;
     48  1.3  mrg struct DTLS;
     49  1.3  mrg }
     50  1.3  mrg 
     51  1.1  mrg namespace __lsan {
     52  1.1  mrg 
     53  1.1  mrg // Chunk tags.
     54  1.1  mrg enum ChunkTag {
     55  1.1  mrg   kDirectlyLeaked = 0,  // default
     56  1.1  mrg   kIndirectlyLeaked = 1,
     57  1.1  mrg   kReachable = 2,
     58  1.1  mrg   kIgnored = 3
     59  1.1  mrg };
     60  1.1  mrg 
     61  1.4  mrg const u32 kInvalidTid = (u32) -1;
     62  1.4  mrg 
     63  1.1  mrg struct Flags {
     64  1.3  mrg #define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name;
     65  1.3  mrg #include "lsan_flags.inc"
     66  1.3  mrg #undef LSAN_FLAG
     67  1.3  mrg 
     68  1.3  mrg   void SetDefaults();
     69  1.1  mrg   uptr pointer_alignment() const {
     70  1.1  mrg     return use_unaligned ? 1 : sizeof(uptr);
     71  1.1  mrg   }
     72  1.1  mrg };
     73  1.1  mrg 
     74  1.1  mrg extern Flags lsan_flags;
     75  1.1  mrg inline Flags *flags() { return &lsan_flags; }
     76  1.3  mrg void RegisterLsanFlags(FlagParser *parser, Flags *f);
     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.4  mrg 
    121  1.4  mrg struct RootRegion {
    122  1.4  mrg   uptr begin;
    123  1.4  mrg   uptr size;
    124  1.4  mrg };
    125  1.4  mrg 
    126  1.4  mrg InternalMmapVector<RootRegion> const *GetRootRegions();
    127  1.4  mrg void ScanRootRegion(Frontier *frontier, RootRegion const &region,
    128  1.4  mrg                     uptr region_begin, uptr region_end, bool is_readable);
    129  1.3  mrg // Run stoptheworld while holding any platform-specific locks.
    130  1.3  mrg 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.4  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.4  mrg const char *MaybeCallLsanDefaultOptions();
    145  1.3  mrg void InitCommonLsan();
    146  1.1  mrg void DoLeakCheck();
    147  1.4  mrg void DoRecoverableLeakCheckVoid();
    148  1.4  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.4  mrg // According to Itanium C++ ABI array cookie is a one word containing
    162  1.4  mrg // size of allocated array.
    163  1.4  mrg static inline bool IsItaniumABIArrayCookie(uptr chunk_beg, uptr chunk_size,
    164  1.4  mrg                                            uptr addr) {
    165  1.4  mrg   return chunk_size == sizeof(uptr) && chunk_beg + chunk_size == addr &&
    166  1.4  mrg          *reinterpret_cast<uptr *>(chunk_beg) == 0;
    167  1.4  mrg }
    168  1.4  mrg 
    169  1.4  mrg // According to ARM C++ ABI array cookie consists of two words:
    170  1.4  mrg // struct array_cookie {
    171  1.4  mrg //   std::size_t element_size; // element_size != 0
    172  1.4  mrg //   std::size_t element_count;
    173  1.4  mrg // };
    174  1.4  mrg static inline bool IsARMABIArrayCookie(uptr chunk_beg, uptr chunk_size,
    175  1.4  mrg                                        uptr addr) {
    176  1.4  mrg   return chunk_size == 2 * sizeof(uptr) && chunk_beg + chunk_size == addr &&
    177  1.4  mrg          *reinterpret_cast<uptr *>(chunk_beg + sizeof(uptr)) == 0;
    178  1.4  mrg }
    179  1.4  mrg 
    180  1.1  mrg // Special case for "new T[0]" where T is a type with DTOR.
    181  1.4  mrg // new T[0] will allocate a cookie (one or two words) for the array size (0)
    182  1.4  mrg // and store a pointer to the end of allocated chunk. The actual cookie layout
    183  1.4  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.4  mrg #if defined(__arm__)
    187  1.4  mrg   return IsARMABIArrayCookie(chunk_beg, chunk_size, addr);
    188  1.4  mrg #else
    189  1.4  mrg   return IsItaniumABIArrayCookie(chunk_beg, chunk_size, addr);
    190  1.4  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.4  mrg bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
    207  1.3  mrg                            uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
    208  1.3  mrg                            uptr *cache_end, DTLS **dtls);
    209  1.4  mrg void ForEachExtraStackRange(tid_t os_id, RangeIteratorCallback callback,
    210  1.1  mrg                             void *arg);
    211  1.1  mrg // If called from the main thread, updates the main thread's TID in the thread
    212  1.1  mrg // registry. We need this to handle processes that fork() without a subsequent
    213  1.1  mrg // exec(), which invalidates the recorded TID. To update it, we must call
    214  1.1  mrg // gettid() from the main thread. Our solution is to call this function before
    215  1.1  mrg // leak checking and also before every call to pthread_create() (to handle cases
    216  1.1  mrg // where leak checking is initiated from a non-main thread).
    217  1.1  mrg void EnsureMainThreadIDIsCorrect();
    218  1.1  mrg // If p points into a chunk that has been allocated to the user, returns its
    219  1.1  mrg // user-visible address. Otherwise, returns 0.
    220  1.1  mrg uptr PointsIntoChunk(void *p);
    221  1.1  mrg // Returns address of user-visible chunk contained in this allocator chunk.
    222  1.1  mrg uptr GetUserBegin(uptr chunk);
    223  1.1  mrg // Helper for __lsan_ignore_object().
    224  1.1  mrg IgnoreObjectResult IgnoreObjectLocked(const void *p);
    225  1.4  mrg 
    226  1.4  mrg // Return the linker module, if valid for the platform.
    227  1.4  mrg LoadedModule *GetLinker();
    228  1.4  mrg 
    229  1.4  mrg // Return true if LSan has finished leak checking and reported leaks.
    230  1.4  mrg bool HasReportedLeaks();
    231  1.4  mrg 
    232  1.4  mrg // Run platform-specific leak handlers.
    233  1.4  mrg void HandleLeaks();
    234  1.4  mrg 
    235  1.1  mrg // Wrapper for chunk metadata operations.
    236  1.1  mrg class LsanMetadata {
    237  1.1  mrg  public:
    238  1.1  mrg   // Constructor accepts address of user-visible chunk.
    239  1.1  mrg   explicit LsanMetadata(uptr chunk);
    240  1.1  mrg   bool allocated() const;
    241  1.1  mrg   ChunkTag tag() const;
    242  1.1  mrg   void set_tag(ChunkTag value);
    243  1.1  mrg   uptr requested_size() const;
    244  1.1  mrg   u32 stack_trace_id() const;
    245  1.1  mrg  private:
    246  1.1  mrg   void *metadata_;
    247  1.1  mrg };
    248  1.1  mrg 
    249  1.1  mrg }  // namespace __lsan
    250  1.1  mrg 
    251  1.1  mrg extern "C" {
    252  1.1  mrg SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
    253  1.4  mrg const char *__lsan_default_options();
    254  1.4  mrg 
    255  1.4  mrg SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
    256  1.1  mrg int __lsan_is_turned_off();
    257  1.1  mrg 
    258  1.1  mrg SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
    259  1.1  mrg const char *__lsan_default_suppressions();
    260  1.1  mrg }  // extern "C"
    261  1.1  mrg 
    262  1.1  mrg #endif  // LSAN_COMMON_H
    263