[compiler-rt] [HWASan] [compiler-rt] support non-4k pages on Android (PR #95069)
Florian Mayer via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 10 18:44:12 PDT 2024
https://github.com/fmayer updated https://github.com/llvm/llvm-project/pull/95069
>From d5ba9ad95b0ee3526cdcff4bebf4a6da558ac1d5 Mon Sep 17 00:00:00 2001
From: Florian Mayer <fmayer at google.com>
Date: Mon, 10 Jun 2024 18:36:50 -0700
Subject: [PATCH 1/2] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
=?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.4
---
compiler-rt/lib/asan/asan_linux.cpp | 3 ++-
compiler-rt/lib/asan/asan_mac.cpp | 3 ++-
compiler-rt/lib/asan/asan_premap_shadow.cpp | 3 ++-
.../lib/hwasan/hwasan_dynamic_shadow.cpp | 15 +++++++++------
compiler-rt/lib/memprof/memprof_linux.cpp | 3 ++-
.../lib/sanitizer_common/sanitizer_common.h | 17 ++++++++---------
.../lib/sanitizer_common/sanitizer_linux.cpp | 4 ++--
.../sanitizer_linux_libcdep.cpp | 5 ++---
.../lib/sanitizer_common/sanitizer_mac.cpp | 4 ++--
.../lib/sanitizer_common/sanitizer_win.cpp | 5 ++---
10 files changed, 33 insertions(+), 29 deletions(-)
diff --git a/compiler-rt/lib/asan/asan_linux.cpp b/compiler-rt/lib/asan/asan_linux.cpp
index a517de5af00dc..0b470db86748f 100644
--- a/compiler-rt/lib/asan/asan_linux.cpp
+++ b/compiler-rt/lib/asan/asan_linux.cpp
@@ -93,7 +93,8 @@ uptr FindDynamicShadowStart() {
# endif
return MapDynamicShadow(shadow_size_bytes, ASAN_SHADOW_SCALE,
- /*min_shadow_base_alignment*/ 0, kHighMemEnd);
+ /*min_shadow_base_alignment*/ 0, kHighMemEnd,
+ GetMmapGranularity());
}
void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
diff --git a/compiler-rt/lib/asan/asan_mac.cpp b/compiler-rt/lib/asan/asan_mac.cpp
index b250f796e165f..bfc349223258b 100644
--- a/compiler-rt/lib/asan/asan_mac.cpp
+++ b/compiler-rt/lib/asan/asan_mac.cpp
@@ -51,7 +51,8 @@ bool IsSystemHeapAddress (uptr addr) { return false; }
uptr FindDynamicShadowStart() {
return MapDynamicShadow(MemToShadowSize(kHighMemEnd), ASAN_SHADOW_SCALE,
- /*min_shadow_base_alignment*/ 0, kHighMemEnd);
+ /*min_shadow_base_alignment*/ 0, kHighMemEnd,
+ GetMmapGranularity());
}
// No-op. Mac does not support static linkage anyway.
diff --git a/compiler-rt/lib/asan/asan_premap_shadow.cpp b/compiler-rt/lib/asan/asan_premap_shadow.cpp
index bed2f62a22511..6e08b8f966507 100644
--- a/compiler-rt/lib/asan/asan_premap_shadow.cpp
+++ b/compiler-rt/lib/asan/asan_premap_shadow.cpp
@@ -33,7 +33,8 @@ uptr PremapShadowSize() {
// PremapShadowSize() bytes on the right of it are mapped r/o.
uptr PremapShadow() {
return MapDynamicShadow(PremapShadowSize(), /*mmap_alignment_scale*/ 3,
- /*min_shadow_base_alignment*/ 0, kHighMemEnd);
+ /*min_shadow_base_alignment*/ 0, kHighMemEnd,
+ GetMmapGranularity());
}
bool PremapShadowFailed() {
diff --git a/compiler-rt/lib/hwasan/hwasan_dynamic_shadow.cpp b/compiler-rt/lib/hwasan/hwasan_dynamic_shadow.cpp
index 7642ba6c0bf08..ac3360aa3400f 100644
--- a/compiler-rt/lib/hwasan/hwasan_dynamic_shadow.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_dynamic_shadow.cpp
@@ -36,15 +36,18 @@ decltype(__hwasan_shadow)* __hwasan_premap_shadow();
namespace __hwasan {
+// We cannot call anything in libc here (see comment above), so we need to assume
+// the biggest allowed page size.
+static constexpr uptr kMaxGranularity = 16384;
+
// Conservative upper limit.
static uptr PremapShadowSize() {
- return RoundUpTo(GetMaxVirtualAddress() >> kShadowScale,
- GetMmapGranularity());
+ return RoundUpTo(GetMaxVirtualAddress() >> kShadowScale, kMaxGranularity);
}
static uptr PremapShadow() {
return MapDynamicShadow(PremapShadowSize(), kShadowScale,
- kShadowBaseAlignment, kHighMemEnd);
+ kShadowBaseAlignment, kHighMemEnd, kMaxGranularity);
}
static bool IsPremapShadowAvailable() {
@@ -56,7 +59,7 @@ static bool IsPremapShadowAvailable() {
}
static uptr FindPremappedShadowStart(uptr shadow_size_bytes) {
- const uptr granularity = GetMmapGranularity();
+ const uptr granularity = kMaxGranularity;
const uptr shadow_start = reinterpret_cast<uptr>(&__hwasan_shadow);
const uptr premap_shadow_size = PremapShadowSize();
const uptr shadow_size = RoundUpTo(shadow_size_bytes, granularity);
@@ -109,7 +112,7 @@ uptr FindDynamicShadowStart(uptr shadow_size_bytes) {
if (IsPremapShadowAvailable())
return FindPremappedShadowStart(shadow_size_bytes);
return MapDynamicShadow(shadow_size_bytes, kShadowScale, kShadowBaseAlignment,
- kHighMemEnd);
+ kHighMemEnd, kMaxGranularity);
}
} // namespace __hwasan
@@ -135,7 +138,7 @@ uptr FindDynamicShadowStart(uptr shadow_size_bytes) {
RingBufferSize());
# endif
return MapDynamicShadow(shadow_size_bytes, kShadowScale, kShadowBaseAlignment,
- kHighMemEnd);
+ kHighMemEnd, GetMmapGranularity());
}
} // namespace __hwasan
diff --git a/compiler-rt/lib/memprof/memprof_linux.cpp b/compiler-rt/lib/memprof/memprof_linux.cpp
index 26a2b456b874e..fbe5d250f840b 100644
--- a/compiler-rt/lib/memprof/memprof_linux.cpp
+++ b/compiler-rt/lib/memprof/memprof_linux.cpp
@@ -58,7 +58,8 @@ void InitializePlatformExceptionHandlers() {}
uptr FindDynamicShadowStart() {
uptr shadow_size_bytes = MemToShadowSize(kHighMemEnd);
return MapDynamicShadow(shadow_size_bytes, SHADOW_SCALE,
- /*min_shadow_base_alignment*/ 0, kHighMemEnd);
+ /*min_shadow_base_alignment*/ 0, kHighMemEnd,
+ GetMmapGranularity());
}
void *MemprofDlSymNext(const char *sym) { return dlsym(RTLD_NEXT, sym); }
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h
index c451fc962c529..5c3de2bef0f79 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h
@@ -60,14 +60,10 @@ inline int Verbosity() {
return atomic_load(¤t_verbosity, memory_order_relaxed);
}
-#if SANITIZER_ANDROID
-inline uptr GetPageSize() {
-// Android post-M sysconf(_SC_PAGESIZE) crashes if called from .preinit_array.
- return 4096;
-}
-inline uptr GetPageSizeCached() {
- return 4096;
-}
+#if SANITIZER_ANDROID && !defined(__aarch64__)
+// 32-bit Android only has 4k pages.
+inline uptr GetPageSize() { return 4096; }
+inline uptr GetPageSizeCached() { return 4096; }
#else
uptr GetPageSize();
extern uptr PageSizeCached;
@@ -77,6 +73,7 @@ inline uptr GetPageSizeCached() {
return PageSizeCached;
}
#endif
+
uptr GetMmapGranularity();
uptr GetMaxVirtualAddress();
uptr GetMaxUserVirtualAddress();
@@ -91,6 +88,7 @@ void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
// Memory management
void *MmapOrDie(uptr size, const char *mem_type, bool raw_report = false);
+
inline void *MmapOrDieQuietly(uptr size, const char *mem_type) {
return MmapOrDie(size, mem_type, /*raw_report*/ true);
}
@@ -139,7 +137,8 @@ void UnmapFromTo(uptr from, uptr to);
// shadow_size_bytes bytes on the right, which on linux is mapped no access.
// The high_mem_end may be updated if the original shadow size doesn't fit.
uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
- uptr min_shadow_base_alignment, uptr &high_mem_end);
+ uptr min_shadow_base_alignment, uptr &high_mem_end,
+ const uptr granularity);
// Let S = max(shadow_size, num_aliases * alias_size, ring_buffer_size).
// Reserves 2*S bytes of address space to the right of the returned address and
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
index 5d2dd3a7a658f..d15caa76efb06 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
@@ -1136,7 +1136,7 @@ uptr GetMaxUserVirtualAddress() {
return addr;
}
-# if !SANITIZER_ANDROID
+# if !SANITIZER_ANDROID || defined(__aarch64__)
uptr GetPageSize() {
# if SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__)) && \
defined(EXEC_PAGESIZE)
@@ -1155,7 +1155,7 @@ uptr GetPageSize() {
return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy.
# endif
}
-# endif // !SANITIZER_ANDROID
+# endif
uptr ReadBinaryName(/*out*/ char *buf, uptr buf_len) {
# if SANITIZER_SOLARIS
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
index 6d05411222d9e..64503692af090 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
@@ -995,9 +995,8 @@ void UnmapFromTo(uptr from, uptr to) {
}
uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
- uptr min_shadow_base_alignment,
- UNUSED uptr &high_mem_end) {
- const uptr granularity = GetMmapGranularity();
+ uptr min_shadow_base_alignment, UNUSED uptr &high_mem_end,
+ const uptr granularity) {
const uptr alignment =
Max<uptr>(granularity << shadow_scale, 1ULL << min_shadow_base_alignment);
const uptr left_padding =
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
index 24e3d1112520e..fb84127bc3ded 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
@@ -1188,8 +1188,8 @@ uptr GetMaxVirtualAddress() {
}
uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
- uptr min_shadow_base_alignment, uptr &high_mem_end) {
- const uptr granularity = GetMmapGranularity();
+ uptr min_shadow_base_alignment, uptr &high_mem_end,
+ const uptr granularity) {
const uptr alignment =
Max<uptr>(granularity << shadow_scale, 1ULL << min_shadow_base_alignment);
const uptr left_padding =
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp
index 4e5ad8e4693b4..d3dc52bdbc2c4 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp
@@ -384,9 +384,8 @@ bool DontDumpShadowMemory(uptr addr, uptr length) {
}
uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
- uptr min_shadow_base_alignment,
- UNUSED uptr &high_mem_end) {
- const uptr granularity = GetMmapGranularity();
+ uptr min_shadow_base_alignment, UNUSED uptr &high_mem_end,
+ const uptr granularity) {
const uptr alignment =
Max<uptr>(granularity << shadow_scale, 1ULL << min_shadow_base_alignment);
const uptr left_padding =
>From fdff4fd1d45f994552d1a42fde2ad94c479d93a8 Mon Sep 17 00:00:00 2001
From: Florian Mayer <fmayer at google.com>
Date: Mon, 10 Jun 2024 18:43:58 -0700
Subject: [PATCH 2/2] fmt
Created using spr 1.3.4
---
compiler-rt/lib/hwasan/hwasan_dynamic_shadow.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/compiler-rt/lib/hwasan/hwasan_dynamic_shadow.cpp b/compiler-rt/lib/hwasan/hwasan_dynamic_shadow.cpp
index ac3360aa3400f..fadc5ff52a960 100644
--- a/compiler-rt/lib/hwasan/hwasan_dynamic_shadow.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_dynamic_shadow.cpp
@@ -36,8 +36,8 @@ decltype(__hwasan_shadow)* __hwasan_premap_shadow();
namespace __hwasan {
-// We cannot call anything in libc here (see comment above), so we need to assume
-// the biggest allowed page size.
+// We cannot call anything in libc here (see comment above), so we need to
+// assume the biggest allowed page size.
static constexpr uptr kMaxGranularity = 16384;
// Conservative upper limit.
More information about the llvm-commits
mailing list