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