2013-05-20 19:06:50 +08:00
|
|
|
//=-- lsan_common.h -------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of LeakSanitizer.
|
|
|
|
// Private LSan header.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LSAN_COMMON_H
|
|
|
|
#define LSAN_COMMON_H
|
|
|
|
|
2013-06-24 16:34:50 +08:00
|
|
|
#include "sanitizer_common/sanitizer_allocator.h"
|
2013-05-20 19:06:50 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_internal_defs.h"
|
2013-05-21 22:12:11 +08:00
|
|
|
#include "sanitizer_common/sanitizer_platform.h"
|
2013-05-20 19:06:50 +08:00
|
|
|
#include "sanitizer_common/sanitizer_symbolizer.h"
|
|
|
|
|
2013-05-21 22:12:11 +08:00
|
|
|
#if SANITIZER_LINUX && defined(__x86_64__)
|
|
|
|
#define CAN_SANITIZE_LEAKS 1
|
|
|
|
#else
|
|
|
|
#define CAN_SANITIZE_LEAKS 0
|
|
|
|
#endif
|
|
|
|
|
2013-05-20 19:06:50 +08:00
|
|
|
namespace __lsan {
|
|
|
|
|
|
|
|
// Chunk tags.
|
|
|
|
enum ChunkTag {
|
|
|
|
kDirectlyLeaked = 0, // default
|
|
|
|
kIndirectlyLeaked = 1,
|
2013-06-03 19:21:34 +08:00
|
|
|
kReachable = 2,
|
2013-06-11 23:26:20 +08:00
|
|
|
kIgnored = 3
|
2013-05-20 19:06:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Flags {
|
|
|
|
uptr pointer_alignment() const {
|
2013-05-27 19:41:46 +08:00
|
|
|
return use_unaligned ? 1 : sizeof(uptr);
|
2013-05-20 19:06:50 +08:00
|
|
|
}
|
|
|
|
|
2013-06-11 23:26:20 +08:00
|
|
|
// Print addresses of leaked objects after main leak report.
|
|
|
|
bool report_objects;
|
|
|
|
// Aggregate two objects into one leak if this many stack frames match. If
|
2013-05-20 19:06:50 +08:00
|
|
|
// zero, the entire stack trace must match.
|
|
|
|
int resolution;
|
|
|
|
// The number of leaks reported.
|
|
|
|
int max_leaks;
|
2013-05-24 21:16:02 +08:00
|
|
|
// If nonzero kill the process with this exit code upon finding leaks.
|
|
|
|
int exitcode;
|
2013-12-18 02:01:45 +08:00
|
|
|
// Print matched suppressions after leak checking.
|
|
|
|
bool print_suppressions;
|
2013-06-28 22:38:31 +08:00
|
|
|
// Suppressions file name.
|
|
|
|
const char* suppressions;
|
2013-05-20 19:06:50 +08:00
|
|
|
|
2013-05-27 19:41:46 +08:00
|
|
|
// Flags controlling the root set of reachable memory.
|
|
|
|
// Global variables (.data and .bss).
|
|
|
|
bool use_globals;
|
|
|
|
// Thread stacks.
|
|
|
|
bool use_stacks;
|
|
|
|
// Thread registers.
|
|
|
|
bool use_registers;
|
|
|
|
// TLS and thread-specific storage.
|
|
|
|
bool use_tls;
|
2013-12-17 19:11:23 +08:00
|
|
|
// Regions added via __lsan_register_root_region().
|
|
|
|
bool use_root_regions;
|
2013-05-27 19:41:46 +08:00
|
|
|
|
|
|
|
// Consider unaligned pointers valid.
|
|
|
|
bool use_unaligned;
|
2013-12-09 21:12:10 +08:00
|
|
|
// Consider pointers found in poisoned memory to be valid.
|
|
|
|
bool use_poisoned;
|
2013-05-27 19:41:46 +08:00
|
|
|
|
2013-05-20 19:06:50 +08:00
|
|
|
// Debug logging.
|
|
|
|
bool log_pointers;
|
|
|
|
bool log_threads;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern Flags lsan_flags;
|
|
|
|
inline Flags *flags() { return &lsan_flags; }
|
|
|
|
|
|
|
|
struct Leak {
|
2013-12-24 20:03:02 +08:00
|
|
|
u32 id;
|
2013-05-20 19:06:50 +08:00
|
|
|
uptr hit_count;
|
|
|
|
uptr total_size;
|
|
|
|
u32 stack_trace_id;
|
|
|
|
bool is_directly_leaked;
|
2013-06-28 22:38:31 +08:00
|
|
|
bool is_suppressed;
|
2013-05-20 19:06:50 +08:00
|
|
|
};
|
|
|
|
|
2013-12-24 20:03:02 +08:00
|
|
|
struct LeakedObject {
|
2013-12-24 20:42:15 +08:00
|
|
|
u32 leak_id;
|
2013-12-24 20:03:02 +08:00
|
|
|
uptr addr;
|
|
|
|
uptr size;
|
|
|
|
};
|
|
|
|
|
2013-05-20 19:06:50 +08:00
|
|
|
// Aggregates leaks by stack trace prefix.
|
|
|
|
class LeakReport {
|
|
|
|
public:
|
2013-12-24 20:03:02 +08:00
|
|
|
LeakReport() : next_id_(0), leaks_(1), leaked_objects_(1) {}
|
2013-12-24 20:42:15 +08:00
|
|
|
void AddLeakedChunk(uptr chunk, u32 stack_trace_id, uptr leaked_size,
|
|
|
|
ChunkTag tag);
|
|
|
|
void ReportTopLeaks(uptr max_leaks);
|
2013-05-24 22:49:13 +08:00
|
|
|
void PrintSummary();
|
2013-12-24 20:42:15 +08:00
|
|
|
void ApplySuppressions();
|
|
|
|
uptr UnsuppressedLeakCount();
|
|
|
|
|
|
|
|
|
2013-05-20 19:06:50 +08:00
|
|
|
private:
|
2013-12-24 20:42:15 +08:00
|
|
|
void PrintReportForLeak(uptr index);
|
|
|
|
void PrintLeakedObjectsForLeak(uptr index);
|
|
|
|
|
2013-12-24 20:03:02 +08:00
|
|
|
u32 next_id_;
|
2013-06-14 17:59:40 +08:00
|
|
|
InternalMmapVector<Leak> leaks_;
|
2013-12-24 20:03:02 +08:00
|
|
|
InternalMmapVector<LeakedObject> leaked_objects_;
|
2013-05-20 19:06:50 +08:00
|
|
|
};
|
|
|
|
|
2013-06-14 18:07:56 +08:00
|
|
|
typedef InternalMmapVector<uptr> Frontier;
|
|
|
|
|
2013-05-20 19:06:50 +08:00
|
|
|
// Platform-specific functions.
|
|
|
|
void InitializePlatformSpecificModules();
|
2013-06-14 18:07:56 +08:00
|
|
|
void ProcessGlobalRegions(Frontier *frontier);
|
|
|
|
void ProcessPlatformSpecificAllocations(Frontier *frontier);
|
2013-05-20 19:06:50 +08:00
|
|
|
|
2013-06-14 17:59:40 +08:00
|
|
|
void ScanRangeForPointers(uptr begin, uptr end,
|
2013-06-14 18:07:56 +08:00
|
|
|
Frontier *frontier,
|
2013-05-20 19:06:50 +08:00
|
|
|
const char *region_type, ChunkTag tag);
|
|
|
|
|
2013-06-06 22:17:56 +08:00
|
|
|
enum IgnoreObjectResult {
|
|
|
|
kIgnoreObjectSuccess,
|
|
|
|
kIgnoreObjectAlreadyIgnored,
|
|
|
|
kIgnoreObjectInvalid
|
|
|
|
};
|
|
|
|
|
2013-06-21 22:51:52 +08:00
|
|
|
// Functions called from the parent tool.
|
|
|
|
void InitCommonLsan();
|
|
|
|
void DoLeakCheck();
|
|
|
|
bool DisabledInThisThread();
|
|
|
|
|
2014-01-10 18:48:01 +08:00
|
|
|
// Special case for "new T[0]" where T is a type with DTOR.
|
|
|
|
// new T[0] will allocate one word for the array size (0) and store a pointer
|
|
|
|
// to the end of allocated chunk.
|
|
|
|
inline bool IsSpecialCaseOfOperatorNew0(uptr chunk_beg, uptr chunk_size,
|
|
|
|
uptr addr) {
|
|
|
|
return chunk_size == sizeof(uptr) && chunk_beg + chunk_size == addr &&
|
|
|
|
*reinterpret_cast<uptr *>(chunk_beg) == 0;
|
|
|
|
}
|
|
|
|
|
2013-05-20 19:06:50 +08:00
|
|
|
// The following must be implemented in the parent tool.
|
|
|
|
|
2013-06-24 16:34:50 +08:00
|
|
|
void ForEachChunk(ForEachChunkCallback callback, void *arg);
|
|
|
|
// Returns the address range occupied by the global allocator object.
|
2013-05-20 19:06:50 +08:00
|
|
|
void GetAllocatorGlobalRange(uptr *begin, uptr *end);
|
|
|
|
// Wrappers for allocator's ForceLock()/ForceUnlock().
|
|
|
|
void LockAllocator();
|
|
|
|
void UnlockAllocator();
|
2013-12-09 21:12:10 +08:00
|
|
|
// Returns true if [addr, addr + sizeof(void *)) is poisoned.
|
|
|
|
bool WordIsPoisoned(uptr addr);
|
2013-05-20 19:06:50 +08:00
|
|
|
// Wrappers for ThreadRegistry access.
|
|
|
|
void LockThreadRegistry();
|
|
|
|
void UnlockThreadRegistry();
|
|
|
|
bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
|
|
|
|
uptr *tls_begin, uptr *tls_end,
|
|
|
|
uptr *cache_begin, uptr *cache_end);
|
2013-10-14 22:04:50 +08:00
|
|
|
void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback,
|
|
|
|
void *arg);
|
2013-07-08 20:57:24 +08:00
|
|
|
// If called from the main thread, updates the main thread's TID in the thread
|
|
|
|
// registry. We need this to handle processes that fork() without a subsequent
|
|
|
|
// exec(), which invalidates the recorded TID. To update it, we must call
|
|
|
|
// gettid() from the main thread. Our solution is to call this function before
|
|
|
|
// leak checking and also before every call to pthread_create() (to handle cases
|
|
|
|
// where leak checking is initiated from a non-main thread).
|
|
|
|
void EnsureMainThreadIDIsCorrect();
|
2013-06-24 16:34:50 +08:00
|
|
|
// If p points into a chunk that has been allocated to the user, returns its
|
|
|
|
// user-visible address. Otherwise, returns 0.
|
|
|
|
uptr PointsIntoChunk(void *p);
|
|
|
|
// Returns address of user-visible chunk contained in this allocator chunk.
|
|
|
|
uptr GetUserBegin(uptr chunk);
|
2013-06-06 22:17:56 +08:00
|
|
|
// Helper for __lsan_ignore_object().
|
|
|
|
IgnoreObjectResult IgnoreObjectLocked(const void *p);
|
2013-05-20 19:06:50 +08:00
|
|
|
// Wrapper for chunk metadata operations.
|
|
|
|
class LsanMetadata {
|
|
|
|
public:
|
2013-06-24 16:34:50 +08:00
|
|
|
// Constructor accepts address of user-visible chunk.
|
|
|
|
explicit LsanMetadata(uptr chunk);
|
2013-05-20 19:06:50 +08:00
|
|
|
bool allocated() const;
|
|
|
|
ChunkTag tag() const;
|
|
|
|
void set_tag(ChunkTag value);
|
|
|
|
uptr requested_size() const;
|
|
|
|
u32 stack_trace_id() const;
|
|
|
|
private:
|
|
|
|
void *metadata_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace __lsan
|
|
|
|
|
2013-06-27 17:35:50 +08:00
|
|
|
extern "C" {
|
2013-08-13 19:42:45 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
|
|
|
|
int __lsan_is_turned_off();
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
|
|
|
|
const char *__lsan_default_suppressions();
|
2013-06-27 17:35:50 +08:00
|
|
|
} // extern "C"
|
|
|
|
|
2013-05-20 19:06:50 +08:00
|
|
|
#endif // LSAN_COMMON_H
|