2013-05-20 19:06:50 +08:00
|
|
|
//=-- lsan_common.h -------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-05-20 19:06:50 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
2015-02-26 22:01:08 +08:00
|
|
|
#include "sanitizer_common/sanitizer_stoptheworld.h"
|
2013-05-20 19:06:50 +08:00
|
|
|
#include "sanitizer_common/sanitizer_symbolizer.h"
|
|
|
|
|
2017-01-31 15:15:37 +08:00
|
|
|
// LeakSanitizer relies on some Glibc's internals (e.g. TLS machinery) thus
|
|
|
|
// supported for Linux only. Also, LSan doesn't like 32 bit architectures
|
|
|
|
// because of "small" (4 bytes) pointer size that leads to high false negative
|
|
|
|
// ratio on large leaks. But we still want to have it for some 32 bit arches
|
|
|
|
// (e.g. x86), see https://github.com/google/sanitizers/issues/403.
|
2017-11-16 05:19:20 +08:00
|
|
|
// To enable LeakSanitizer on a new architecture, one needs to implement the
|
|
|
|
// internal_clone function as well as (probably) adjust the TLS machinery for
|
|
|
|
// the new architecture inside the sanitizer library.
|
2017-04-07 01:41:26 +08:00
|
|
|
#if (SANITIZER_LINUX && !SANITIZER_ANDROID || SANITIZER_MAC) && \
|
|
|
|
(SANITIZER_WORDSIZE == 64) && \
|
2017-04-22 05:59:53 +08:00
|
|
|
(defined(__x86_64__) || defined(__mips64) || defined(__aarch64__) || \
|
|
|
|
defined(__powerpc64__))
|
2013-05-21 22:12:11 +08:00
|
|
|
#define CAN_SANITIZE_LEAKS 1
|
2017-04-07 01:41:26 +08:00
|
|
|
#elif defined(__i386__) && \
|
|
|
|
(SANITIZER_LINUX && !SANITIZER_ANDROID || SANITIZER_MAC)
|
2017-03-29 05:56:44 +08:00
|
|
|
#define CAN_SANITIZE_LEAKS 1
|
2017-04-11 22:58:26 +08:00
|
|
|
#elif defined(__arm__) && \
|
|
|
|
SANITIZER_LINUX && !SANITIZER_ANDROID
|
|
|
|
#define CAN_SANITIZE_LEAKS 1
|
2013-05-21 22:12:11 +08:00
|
|
|
#else
|
|
|
|
#define CAN_SANITIZE_LEAKS 0
|
|
|
|
#endif
|
|
|
|
|
2015-02-18 02:50:30 +08:00
|
|
|
namespace __sanitizer {
|
|
|
|
class FlagParser;
|
2018-05-10 07:02:14 +08:00
|
|
|
class ThreadRegistry;
|
2016-01-15 02:50:09 +08:00
|
|
|
struct DTLS;
|
2015-02-18 02:50:30 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2017-02-17 11:23:07 +08:00
|
|
|
const u32 kInvalidTid = (u32) -1;
|
|
|
|
|
2013-05-20 19:06:50 +08:00
|
|
|
struct Flags {
|
2015-01-07 08:38:00 +08:00
|
|
|
#define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name;
|
|
|
|
#include "lsan_flags.inc"
|
|
|
|
#undef LSAN_FLAG
|
|
|
|
|
|
|
|
void SetDefaults();
|
2013-05-20 19:06:50 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
extern Flags lsan_flags;
|
|
|
|
inline Flags *flags() { return &lsan_flags; }
|
2015-02-18 02:50:30 +08:00
|
|
|
void RegisterLsanFlags(FlagParser *parser, Flags *f);
|
2013-05-20 19:06:50 +08:00
|
|
|
|
|
|
|
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:
|
2018-05-07 13:56:24 +08:00
|
|
|
LeakReport() {}
|
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);
|
|
|
|
|
2018-05-07 13:56:24 +08:00
|
|
|
u32 next_id_ = 0;
|
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);
|
2017-04-20 05:11:08 +08:00
|
|
|
|
|
|
|
struct RootRegion {
|
|
|
|
uptr begin;
|
|
|
|
uptr size;
|
|
|
|
};
|
|
|
|
|
|
|
|
InternalMmapVector<RootRegion> const *GetRootRegions();
|
|
|
|
void ScanRootRegion(Frontier *frontier, RootRegion const ®ion,
|
2017-07-12 02:54:00 +08:00
|
|
|
uptr region_begin, uptr region_end, bool is_readable);
|
2015-02-26 22:01:08 +08:00
|
|
|
// Run stoptheworld while holding any platform-specific locks.
|
|
|
|
void DoStopTheWorld(StopTheWorldCallback callback, void* argument);
|
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);
|
2017-04-14 02:40:19 +08:00
|
|
|
void ScanGlobalRange(uptr begin, uptr end, Frontier *frontier);
|
2013-05-20 19:06:50 +08:00
|
|
|
|
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.
|
2017-09-23 07:49:49 +08:00
|
|
|
const char *MaybeCallLsanDefaultOptions();
|
2015-02-18 02:50:30 +08:00
|
|
|
void InitCommonLsan();
|
2013-06-21 22:51:52 +08:00
|
|
|
void DoLeakCheck();
|
2017-09-22 15:11:43 +08:00
|
|
|
void DoRecoverableLeakCheckVoid();
|
Use pthreads to manage thread-local storage on darwin for leak sanitizer
Summary:
__thread is supported on Darwin, but is implemented dynamically via
function calls to __tls_get_addr. This causes two issues when combined
with leak sanitizer, due to malloc() interception.
- The dynamic loader calls malloc during the process of loading
the sanitizer dylib, while swapping a placeholder tlv_boostrap
function for __tls_get_addr. This will cause tlv_bootstrap to
be called in DisabledInThisThread() via the asan allocator.
- The first time __tls_get_addr is called, it allocates memory
for the thread-local object, during which it calls malloc(). This
call will be intercepted, leading to an infinite loop in the asan
allocator, in which the allocator calls DisabledInThisThread,
which calls tls_get_addr, which calls into the allocator again.
Reviewers: kcc, glider, kubamracek
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29786
llvm-svn: 294994
2017-02-14 06:20:07 +08:00
|
|
|
void DisableCounterUnderflow();
|
2013-06-21 22:51:52 +08:00
|
|
|
bool DisabledInThisThread();
|
|
|
|
|
2016-01-16 08:57:25 +08:00
|
|
|
// Used to implement __lsan::ScopedDisabler.
|
|
|
|
void DisableInThisThread();
|
|
|
|
void EnableInThisThread();
|
|
|
|
// Can be used to ignore memory allocated by an intercepted
|
|
|
|
// function.
|
|
|
|
struct ScopedInterceptorDisabler {
|
|
|
|
ScopedInterceptorDisabler() { DisableInThisThread(); }
|
|
|
|
~ScopedInterceptorDisabler() { EnableInThisThread(); }
|
|
|
|
};
|
|
|
|
|
2017-04-11 22:58:26 +08:00
|
|
|
// According to Itanium C++ ABI array cookie is a one word containing
|
|
|
|
// size of allocated array.
|
|
|
|
static inline bool IsItaniumABIArrayCookie(uptr chunk_beg, uptr chunk_size,
|
|
|
|
uptr addr) {
|
|
|
|
return chunk_size == sizeof(uptr) && chunk_beg + chunk_size == addr &&
|
|
|
|
*reinterpret_cast<uptr *>(chunk_beg) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// According to ARM C++ ABI array cookie consists of two words:
|
|
|
|
// struct array_cookie {
|
|
|
|
// std::size_t element_size; // element_size != 0
|
|
|
|
// std::size_t element_count;
|
|
|
|
// };
|
|
|
|
static inline bool IsARMABIArrayCookie(uptr chunk_beg, uptr chunk_size,
|
|
|
|
uptr addr) {
|
|
|
|
return chunk_size == 2 * sizeof(uptr) && chunk_beg + chunk_size == addr &&
|
|
|
|
*reinterpret_cast<uptr *>(chunk_beg + sizeof(uptr)) == 0;
|
|
|
|
}
|
|
|
|
|
2014-01-10 18:48:01 +08:00
|
|
|
// Special case for "new T[0]" where T is a type with DTOR.
|
2017-04-11 22:58:26 +08:00
|
|
|
// new T[0] will allocate a cookie (one or two words) for the array size (0)
|
|
|
|
// and store a pointer to the end of allocated chunk. The actual cookie layout
|
|
|
|
// varies between platforms according to their C++ ABI implementation.
|
2014-01-10 18:48:01 +08:00
|
|
|
inline bool IsSpecialCaseOfOperatorNew0(uptr chunk_beg, uptr chunk_size,
|
|
|
|
uptr addr) {
|
2017-04-11 22:58:26 +08:00
|
|
|
#if defined(__arm__)
|
|
|
|
return IsARMABIArrayCookie(chunk_beg, chunk_size, addr);
|
|
|
|
#else
|
|
|
|
return IsItaniumABIArrayCookie(chunk_beg, chunk_size, addr);
|
|
|
|
#endif
|
2014-01-10 18:48:01 +08:00
|
|
|
}
|
|
|
|
|
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();
|
2018-05-10 07:02:14 +08:00
|
|
|
ThreadRegistry *GetThreadRegistryLocked();
|
2017-04-18 02:17:38 +08:00
|
|
|
bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
|
2016-01-15 02:50:09 +08:00
|
|
|
uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
|
|
|
|
uptr *cache_end, DTLS **dtls);
|
2017-04-18 02:17:38 +08:00
|
|
|
void ForEachExtraStackRange(tid_t os_id, RangeIteratorCallback callback,
|
2013-10-14 22:04:50 +08:00
|
|
|
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);
|
2017-04-19 22:00:35 +08:00
|
|
|
|
|
|
|
// Return the linker module, if valid for the platform.
|
|
|
|
LoadedModule *GetLinker();
|
|
|
|
|
Don't call exit() from atexit handlers on Darwin
Summary:
Calling exit() from an atexit handler is undefined behavior.
On Linux, it's unavoidable, since we cannot intercept exit (_exit isn't called
if a user program uses return instead of exit()), and I haven't
seen it cause issues regardless.
However, on Darwin, I have a fairly complex internal test that hangs roughly
once in every 300 runs after leak reporting finishes, which is resolved with
this patch, and is presumably due to the undefined behavior (since the Die() is
the only thing that happens after the end of leak reporting).
In addition, this is the way TSan works as well, where an atexit handler+Die()
is used on Linux, and an _exit() interceptor is used on Darwin. I'm not sure if it's
intentionally structured that way in TSan, since TSan sets up the atexit handler and the
_exit() interceptor on both platforms, but I have observed that on Darwin, only the
_exit() interceptor is used, and on Linux the atexit handler is used.
There is some additional related discussion here: https://reviews.llvm.org/D35085
Reviewers: alekseyshl, kubamracek
Subscribers: eugenis, vsk, llvm-commits
Differential Revision: https://reviews.llvm.org/D35513
llvm-svn: 308353
2017-07-19 04:18:32 +08:00
|
|
|
// Return true if LSan has finished leak checking and reported leaks.
|
|
|
|
bool HasReportedLeaks();
|
|
|
|
|
|
|
|
// Run platform-specific leak handlers.
|
|
|
|
void HandleLeaks();
|
|
|
|
|
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" {
|
2017-09-23 07:49:49 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
|
|
|
|
const char *__lsan_default_options();
|
|
|
|
|
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
|