lsan_common.h 6.1 KB

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