[compiler-rt] [asan] Switch allocator to dynamic base address (PR #98511)
Thurston Dang via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 17 16:08:09 PDT 2024
https://github.com/thurstond updated https://github.com/llvm/llvm-project/pull/98511
>From 52467acf6f723b1b1583e47b056cbb23fd7dfa1d Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Thu, 11 Jul 2024 17:57:22 +0000
Subject: [PATCH 1/2] [asan] Switch allocator to dynamic base address
This ports a proposed memprof fix (https://github.com/llvm/llvm-project/pull/98510), which has a shadow memory and allocator layout that is similar to ASan. Although we have only observed the failure for memprof on a buildbot [*], it could theoretically happen for ASan.
asan_rtl.cpp calls InitializeShadowMemory() - which dynamically/"randomly" chooses a base address for the shadow mapping - prior to InitializeAllocator(). If we are unlucky, the shadow memory may be mapped in the same region where the allocator wants to be.
This patch fixes the issue by changing the allocator to dynamically choosing a base address, as suggested by Vitaly. For comparison, HWASan already dynamically chooses the base addresses for the shadow mapping and allocator.
[*] https://lab.llvm.org/buildbot/#/builders/66/builds/1361/steps/17/logs/stdio
---
compiler-rt/lib/asan/asan_allocator.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/compiler-rt/lib/asan/asan_allocator.h b/compiler-rt/lib/asan/asan_allocator.h
index c3c4fae85b129..8fb113dd62f98 100644
--- a/compiler-rt/lib/asan/asan_allocator.h
+++ b/compiler-rt/lib/asan/asan_allocator.h
@@ -214,7 +214,7 @@ const uptr kAllocatorSpace = 0x600000000000ULL;
const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
typedef DefaultSizeClassMap SizeClassMap;
# else
-const uptr kAllocatorSpace = 0x500000000000ULL;
+const uptr kAllocatorSpace = ~(uptr)0;
const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
typedef DefaultSizeClassMap SizeClassMap;
# endif
>From b195873f52437a030edd2c730b430afa9b3a9ee5 Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Wed, 17 Jul 2024 23:06:31 +0000
Subject: [PATCH 2/2] Aptly apply to Apple apps
---
compiler-rt/lib/asan/asan_allocator.h | 30 ++++++++++++---------------
1 file changed, 13 insertions(+), 17 deletions(-)
diff --git a/compiler-rt/lib/asan/asan_allocator.h b/compiler-rt/lib/asan/asan_allocator.h
index 8fb113dd62f98..b52cdeb730892 100644
--- a/compiler-rt/lib/asan/asan_allocator.h
+++ b/compiler-rt/lib/asan/asan_allocator.h
@@ -182,42 +182,38 @@ static_assert(SizeClassMap::kNumClassesRounded <= 32,
"allocator size and SizeClassMap tunings that allows us to "
"reliably run all bringup tests in a sanitized environment.");
-# else
+# else // SANITIZER_RISCV64
// These are the default allocator tunings for non-RISCV environments where the
// VMA is usually 48 bits and we have lots of space.
const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
typedef DefaultSizeClassMap SizeClassMap;
-# endif
-# elif defined(__powerpc64__)
+# endif // SANITIZER_RISCV64
+# else // SANITIZER_FUCHSIA
const uptr kAllocatorSpace = ~(uptr)0;
+# if defined(__powerpc64__)
const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
typedef DefaultSizeClassMap SizeClassMap;
-# elif defined(__aarch64__) && SANITIZER_ANDROID
+# 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 SANITIZER_RISCV64
-const uptr kAllocatorSpace = ~(uptr)0;
+# elif SANITIZER_RISCV64
const uptr kAllocatorSize = 0x2000000000ULL; // 128G.
typedef VeryDenseSizeClassMap SizeClassMap;
-# elif defined(__sparc__)
-const uptr kAllocatorSpace = ~(uptr)0;
+# elif defined(__sparc__)
const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
typedef DefaultSizeClassMap SizeClassMap;
-# elif SANITIZER_WINDOWS
-const uptr kAllocatorSpace = ~(uptr)0;
+# elif SANITIZER_WINDOWS
const uptr kAllocatorSize = 0x8000000000ULL; // 500G
typedef DefaultSizeClassMap SizeClassMap;
-# elif SANITIZER_APPLE
-const uptr kAllocatorSpace = 0x600000000000ULL;
+# elif SANITIZER_APPLE
const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
typedef DefaultSizeClassMap SizeClassMap;
-# else
-const uptr kAllocatorSpace = ~(uptr)0;
+# else
const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
typedef DefaultSizeClassMap SizeClassMap;
-# endif
+# endif // defined(__powerpc64__) etc.
+# endif // SANITIZER_FUCHSIA
template <typename AddressSpaceViewTy>
struct AP64 { // Allocator64 parameters. Deliberately using a short name.
static const uptr kSpaceBeg = kAllocatorSpace;
@@ -232,7 +228,7 @@ struct AP64 { // Allocator64 parameters. Deliberately using a short name.
template <typename AddressSpaceView>
using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;
using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
-#else // Fallback to SizeClassAllocator32.
+#else // SANITIZER_CAN_USE_ALLOCATOR64. Fallback to SizeClassAllocator32.
typedef CompactSizeClassMap SizeClassMap;
template <typename AddressSpaceViewTy>
struct AP32 {
More information about the llvm-commits
mailing list