[compiler-rt] 1c897e9 - [lsan] Share platform allocator settings between ASan and LSan
Roland McGrath via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 2 17:55:57 PDT 2020
Author: Roland McGrath
Date: 2020-10-02T17:55:46-07:00
New Revision: 1c897e9d72979730f7555e77dd54fe892a461637
URL: https://github.com/llvm/llvm-project/commit/1c897e9d72979730f7555e77dd54fe892a461637
DIFF: https://github.com/llvm/llvm-project/commit/1c897e9d72979730f7555e77dd54fe892a461637.diff
LOG: [lsan] Share platform allocator settings between ASan and LSan
This moves the platform-specific parameter logic from asan into
lsan_common.h to lsan can share it.
Reviewed By: vitalybuka
Differential Revision: https://reviews.llvm.org/D87795
Added:
Modified:
compiler-rt/lib/asan/asan_allocator.h
compiler-rt/lib/lsan/lsan_allocator.h
compiler-rt/lib/lsan/lsan_common.h
Removed:
################################################################################
diff --git a/compiler-rt/lib/asan/asan_allocator.h b/compiler-rt/lib/asan/asan_allocator.h
index 612799f90964..c5f6de123a62 100644
--- a/compiler-rt/lib/asan/asan_allocator.h
+++ b/compiler-rt/lib/asan/asan_allocator.h
@@ -15,13 +15,20 @@
#define ASAN_ALLOCATOR_H
#include "asan_flags.h"
-#include "asan_internal.h"
#include "asan_interceptors.h"
+#include "asan_internal.h"
+#include "lsan/lsan_common.h"
#include "sanitizer_common/sanitizer_allocator.h"
#include "sanitizer_common/sanitizer_list.h"
namespace __asan {
+// These are defined in lsan_common.h because they are shared between the asan
+// allocator and the standalone lsan allocator.
+using __lsan::AllocatorSizeClassMap;
+using __lsan::kAllocatorSize;
+using __lsan::kAllocatorSpace;
+
enum AllocType {
FROM_MALLOC = 1, // Memory block came from malloc, calloc, realloc, etc.
FROM_NEW = 2, // Memory block came from operator new.
@@ -97,9 +104,9 @@ AsanChunkView FindHeapChunkByAddress(uptr address);
AsanChunkView FindHeapChunkByAllocBeg(uptr address);
// List of AsanChunks with total size.
-class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
+class AsanChunkFifoList : public IntrusiveList<AsanChunk> {
public:
- explicit AsanChunkFifoList(LinkerInitialized) { }
+ explicit AsanChunkFifoList(LinkerInitialized) {}
AsanChunkFifoList() { clear(); }
void Push(AsanChunk *n);
void PushList(AsanChunkFifoList *q);
@@ -109,6 +116,7 @@ class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
IntrusiveList<AsanChunk>::clear();
size_ = 0;
}
+
private:
uptr size_;
};
@@ -118,39 +126,9 @@ struct AsanMapUnmapCallback {
void OnUnmap(uptr p, uptr size) const;
};
+using SizeClassMap = AllocatorSizeClassMap;
+
#if SANITIZER_CAN_USE_ALLOCATOR64
-# if SANITIZER_FUCHSIA
-const uptr kAllocatorSpace = ~(uptr)0;
-const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
-typedef DefaultSizeClassMap SizeClassMap;
-# elif defined(__powerpc64__)
-const uptr kAllocatorSpace = ~(uptr)0;
-const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
-typedef DefaultSizeClassMap SizeClassMap;
-# elif defined(__aarch64__) && SANITIZER_ANDROID
-// Android needs to support 39, 42 and 48 bit VMA.
-const uptr kAllocatorSpace = ~(uptr)0;
-const uptr kAllocatorSize = 0x2000000000ULL; // 128G.
-typedef VeryCompactSizeClassMap SizeClassMap;
-# elif defined(__aarch64__)
-// AArch64/SANITIZER_CAN_USE_ALLOCATOR64 is only for 42-bit VMA
-// so no need to
diff erent values for
diff erent VMA.
-const uptr kAllocatorSpace = 0x10000000000ULL;
-const uptr kAllocatorSize = 0x10000000000ULL; // 3T.
-typedef DefaultSizeClassMap SizeClassMap;
-#elif defined(__sparc__)
-const uptr kAllocatorSpace = ~(uptr)0;
-const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
-typedef DefaultSizeClassMap SizeClassMap;
-# elif SANITIZER_WINDOWS
-const uptr kAllocatorSpace = ~(uptr)0;
-const uptr kAllocatorSize = 0x8000000000ULL; // 500G
-typedef DefaultSizeClassMap SizeClassMap;
-# else
-const uptr kAllocatorSpace = 0x600000000000ULL;
-const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
-typedef DefaultSizeClassMap SizeClassMap;
-# endif
template <typename AddressSpaceViewTy>
struct AP64 { // Allocator64 parameters. Deliberately using a short name.
static const uptr kSpaceBeg = kAllocatorSpace;
@@ -179,7 +157,7 @@ struct AP32 {
static const uptr kFlags = 0;
};
template <typename AddressSpaceView>
-using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView> >;
+using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView>>;
using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
#endif // SANITIZER_CAN_USE_ALLOCATOR64
@@ -195,6 +173,7 @@ struct AsanThreadLocalMallocStorage {
uptr quarantine_cache[16];
AllocatorCache allocator_cache;
void CommitBack();
+
private:
// These objects are allocated via mmap() and are zero-initialized.
AsanThreadLocalMallocStorage() {}
diff --git a/compiler-rt/lib/lsan/lsan_allocator.h b/compiler-rt/lib/lsan/lsan_allocator.h
index 17e13cd014ba..7ee84e2cd5b9 100644
--- a/compiler-rt/lib/lsan/lsan_allocator.h
+++ b/compiler-rt/lib/lsan/lsan_allocator.h
@@ -14,10 +14,10 @@
#ifndef LSAN_ALLOCATOR_H
#define LSAN_ALLOCATOR_H
+#include "lsan_common.h"
#include "sanitizer_common/sanitizer_allocator.h"
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_internal_defs.h"
-#include "lsan_common.h"
namespace __lsan {
@@ -28,7 +28,7 @@ void *Reallocate(const StackTrace &stack, void *p, uptr new_size,
uptr alignment);
uptr GetMallocUsableSize(const void *p);
-template<typename Callable>
+template <typename Callable>
void ForEachChunk(const Callable &callback);
void GetAllocatorCacheRange(uptr *begin, uptr *end);
@@ -49,51 +49,36 @@ struct ChunkMetadata {
u32 stack_trace_id;
};
-#if defined(__mips64) || defined(__aarch64__) || defined(__i386__) || \
- defined(__arm__)
+#if SANITIZER_CAN_USE_ALLOCATOR64
template <typename AddressSpaceViewTy>
-struct AP32 {
- static const uptr kSpaceBeg = 0;
- static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
+struct AP64 { // Allocator64 parameters. Deliberately using a short name.
+ static const uptr kSpaceBeg = kAllocatorSpace;
+ static const uptr kSpaceSize = kAllocatorSize;
static const uptr kMetadataSize = sizeof(ChunkMetadata);
- typedef __sanitizer::CompactSizeClassMap SizeClassMap;
- static const uptr kRegionSizeLog = 20;
- using AddressSpaceView = AddressSpaceViewTy;
+ typedef AllocatorSizeClassMap SizeClassMap;
typedef NoOpMapUnmapCallback MapUnmapCallback;
static const uptr kFlags = 0;
+ using AddressSpaceView = AddressSpaceViewTy;
};
template <typename AddressSpaceView>
-using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView>>;
+using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;
using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
-#elif defined(__x86_64__) || defined(__powerpc64__) || defined(__s390x__)
-# if SANITIZER_FUCHSIA
-const uptr kAllocatorSpace = ~(uptr)0;
-const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
-# elif defined(__powerpc64__)
-const uptr kAllocatorSpace = 0xa0000000000ULL;
-const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
-#elif defined(__s390x__)
-const uptr kAllocatorSpace = 0x40000000000ULL;
-const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
-# else
-const uptr kAllocatorSpace = 0x600000000000ULL;
-const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
-# endif
+#else // !SANITIZER_CAN_USE_ALLOCATOR64
template <typename AddressSpaceViewTy>
-struct AP64 { // Allocator64 parameters. Deliberately using a short name.
- static const uptr kSpaceBeg = kAllocatorSpace;
- static const uptr kSpaceSize = kAllocatorSize;
+struct AP32 {
+ static const uptr kSpaceBeg = 0;
+ static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
static const uptr kMetadataSize = sizeof(ChunkMetadata);
- typedef DefaultSizeClassMap SizeClassMap;
+ typedef __sanitizer::CompactSizeClassMap SizeClassMap;
+ static const uptr kRegionSizeLog = 20;
+ using AddressSpaceView = AddressSpaceViewTy;
typedef NoOpMapUnmapCallback MapUnmapCallback;
static const uptr kFlags = 0;
- using AddressSpaceView = AddressSpaceViewTy;
};
-
template <typename AddressSpaceView>
-using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;
+using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView>>;
using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
-#endif
+#endif // SANITIZER_CAN_USE_ALLOCATOR64
template <typename AddressSpaceView>
using AllocatorASVT = CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>;
diff --git a/compiler-rt/lib/lsan/lsan_common.h b/compiler-rt/lib/lsan/lsan_common.h
index 3434beede828..c5ea1eb60694 100644
--- a/compiler-rt/lib/lsan/lsan_common.h
+++ b/compiler-rt/lib/lsan/lsan_common.h
@@ -37,8 +37,7 @@
#elif defined(__i386__) && \
(SANITIZER_LINUX && !SANITIZER_ANDROID || SANITIZER_MAC)
#define CAN_SANITIZE_LEAKS 1
-#elif defined(__arm__) && \
- SANITIZER_LINUX && !SANITIZER_ANDROID
+#elif defined(__arm__) && SANITIZER_LINUX && !SANITIZER_ANDROID
#define CAN_SANITIZE_LEAKS 1
#elif SANITIZER_NETBSD || SANITIZER_FUCHSIA
#define CAN_SANITIZE_LEAKS 1
@@ -50,10 +49,50 @@ namespace __sanitizer {
class FlagParser;
class ThreadRegistry;
struct DTLS;
-}
+} // namespace __sanitizer
namespace __lsan {
+// The platform-specific allocator parameters are shared by both
+// asan_allocator.h and lsan_allocator.h.
+#if SANITIZER_CAN_USE_ALLOCATOR64
+#if SANITIZER_FUCHSIA
+constexpr uptr kAllocatorSpace = ~(uptr)0;
+constexpr uptr kAllocatorSize = 0x40000000000ULL; // 4T.
+using AllocatorSizeClassMap = DefaultSizeClassMap;
+#elif defined(__powerpc64__)
+constexpr uptr kAllocatorSpace = ~(uptr)0;
+constexpr uptr kAllocatorSize = 0x20000000000ULL; // 2T.
+using AllocatorSizeClassMap = DefaultSizeClassMap;
+#elif defined(__aarch64__) && SANITIZER_ANDROID
+// Android needs to support 39, 42 and 48 bit VMA.
+constexpr uptr kAllocatorSpace = ~(uptr)0;
+constexpr uptr kAllocatorSize = 0x2000000000ULL; // 128G.
+using AllocatorSizeClassMap = VeryCompactSizeClassMap;
+#elif defined(__aarch64__)
+// AArch64/SANITIZER_CAN_USE_ALLOCATOR64 is only for 42-bit VMA
+// so no need to
diff erent values for
diff erent VMA.
+constexpr uptr kAllocatorSpace = 0x10000000000ULL;
+constexpr uptr kAllocatorSize = 0x10000000000ULL; // 3T.
+using AllocatorSizeClassMap = DefaultSizeClassMap;
+#elif defined(__sparc__)
+constexpr uptr kAllocatorSpace = ~(uptr)0;
+constexpr uptr kAllocatorSize = 0x20000000000ULL; // 2T.
+using AllocatorSizeClassMap = DefaultSizeClassMap;
+#elif SANITIZER_WINDOWS
+// On Windows 64-bit there is no easy way to find a large enough fixed address
+// space that is always available. Thus, a dynamically allocated address space
+// is used instead (i.e. ~(uptr)0).
+constexpr uptr kAllocatorSpace = ~(uptr)0;
+constexpr uptr kAllocatorSize = 0x8000000000ULL; // 500G
+using AllocatorSizeClassMap = DefaultSizeClassMap;
+#else
+constexpr uptr kAllocatorSpace = 0x600000000000ULL;
+constexpr uptr kAllocatorSize = 0x40000000000ULL; // 4T.
+using AllocatorSizeClassMap = DefaultSizeClassMap;
+#endif
+#endif // SANITIZER_CAN_USE_ALLOCATOR64
+
// Chunk tags.
enum ChunkTag {
kDirectlyLeaked = 0, // default
@@ -62,7 +101,7 @@ enum ChunkTag {
kIgnored = 3
};
-const u32 kInvalidTid = (u32) -1;
+const u32 kInvalidTid = (u32)-1;
struct Flags {
#define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name;
@@ -70,9 +109,7 @@ struct Flags {
#undef LSAN_FLAG
void SetDefaults();
- uptr pointer_alignment() const {
- return use_unaligned ? 1 : sizeof(uptr);
- }
+ uptr pointer_alignment() const { return use_unaligned ? 1 : sizeof(uptr); }
};
extern Flags lsan_flags;
@@ -139,14 +176,13 @@ struct CheckForLeaksParam {
InternalMmapVector<RootRegion> const *GetRootRegions();
void ScanRootRegion(Frontier *frontier, RootRegion const ®ion,
uptr region_begin, uptr region_end, bool is_readable);
-void ForEachExtraStackRangeCb(uptr begin, uptr end, void* arg);
+void ForEachExtraStackRangeCb(uptr begin, uptr end, void *arg);
// Run stoptheworld while holding any platform-specific locks, as well as the
// allocator and thread registry locks.
void LockStuffAndStopTheWorld(StopTheWorldCallback callback,
- CheckForLeaksParam* argument);
+ CheckForLeaksParam *argument);
-void ScanRangeForPointers(uptr begin, uptr end,
- Frontier *frontier,
+void ScanRangeForPointers(uptr begin, uptr end, Frontier *frontier,
const char *region_type, ChunkTag tag);
void ScanGlobalRange(uptr begin, uptr end, Frontier *frontier);
@@ -260,6 +296,7 @@ class LsanMetadata {
void set_tag(ChunkTag value);
uptr requested_size() const;
u32 stack_trace_id() const;
+
private:
void *metadata_;
};
@@ -267,14 +304,14 @@ class LsanMetadata {
} // namespace __lsan
extern "C" {
-SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
-const char *__lsan_default_options();
+SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE const char *
+__lsan_default_options();
-SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
-int __lsan_is_turned_off();
+SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE int
+__lsan_is_turned_off();
-SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
-const char *__lsan_default_suppressions();
+SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE const char *
+__lsan_default_suppressions();
} // extern "C"
#endif // LSAN_COMMON_H
More information about the llvm-commits
mailing list