[compiler-rt] [hwasan] Add fixed_shadow_base flag (PR #73980)

Thurston Dang via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 30 12:57:34 PST 2023


https://github.com/thurstond updated https://github.com/llvm/llvm-project/pull/73980

>From 294ed9bff3fd0d23d1b97c8a4be07b9168e69f69 Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Thu, 30 Nov 2023 20:14:00 +0000
Subject: [PATCH 1/2] [hwasan] Add fixed_shadow_base flag

When set to non-zero, the HWASan runtime will map the shadow base at the
specified constant address.

This is particularly useful in conjunction with the existing compiler option
'hwasan-mapping-offset', which bakes a hardcoded constant address into
the instrumentation.
---
 compiler-rt/lib/hwasan/hwasan_flags.inc |  7 +++++++
 compiler-rt/lib/hwasan/hwasan_linux.cpp | 10 ++++++++--
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/lib/hwasan/hwasan_flags.inc b/compiler-rt/lib/hwasan/hwasan_flags.inc
index 978fa46b705cb9e..bb224fbb8eaa8b9 100644
--- a/compiler-rt/lib/hwasan/hwasan_flags.inc
+++ b/compiler-rt/lib/hwasan/hwasan_flags.inc
@@ -84,3 +84,10 @@ HWASAN_FLAG(bool, malloc_bisect_dump, false,
 // are untagged before the call.
 HWASAN_FLAG(bool, fail_without_syscall_abi, true,
             "Exit if fail to request relaxed syscall ABI.")
+
+HWASAN_FLAG(
+    uptr, fixed_shadow_base, 0,
+    "If non-zero, HWASan will attempt to allocate the shadow at this address, "
+    "instead of choosing one dynamically."
+    "Tip: this can be combined with the compiler option, "
+    "-hwasan-mapping-offset, to optimize the instrumentation.")
diff --git a/compiler-rt/lib/hwasan/hwasan_linux.cpp b/compiler-rt/lib/hwasan/hwasan_linux.cpp
index 81226da976d1161..e7cf36ef3161cbf 100644
--- a/compiler-rt/lib/hwasan/hwasan_linux.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_linux.cpp
@@ -106,8 +106,14 @@ static uptr GetHighMemEnd() {
 }
 
 static void InitializeShadowBaseAddress(uptr shadow_size_bytes) {
-  __hwasan_shadow_memory_dynamic_address =
-      FindDynamicShadowStart(shadow_size_bytes);
+  // NULL is generally address zero, so it is not a valid location for the
+  // shadow.
+  if (flags()->fixed_shadow_base != 0) {
+    __hwasan_shadow_memory_dynamic_address = flags()->fixed_shadow_base;
+  } else {
+    __hwasan_shadow_memory_dynamic_address =
+        FindDynamicShadowStart(shadow_size_bytes);
+  }
 }
 
 static void MaybeDieIfNoTaggingAbi(const char *message) {

>From 48eb300c1a0ee2233f6c2e2e0a3899f591befa94 Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Thu, 30 Nov 2023 20:56:08 +0000
Subject: [PATCH 2/2] Change "non-fixed shadow" magic value from 0 to -1, per
 offline discussion with Vitaly

---
 compiler-rt/lib/hwasan/hwasan_flags.inc | 4 ++--
 compiler-rt/lib/hwasan/hwasan_linux.cpp | 4 +---
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/compiler-rt/lib/hwasan/hwasan_flags.inc b/compiler-rt/lib/hwasan/hwasan_flags.inc
index bb224fbb8eaa8b9..058a0457b9e7f65 100644
--- a/compiler-rt/lib/hwasan/hwasan_flags.inc
+++ b/compiler-rt/lib/hwasan/hwasan_flags.inc
@@ -86,8 +86,8 @@ HWASAN_FLAG(bool, fail_without_syscall_abi, true,
             "Exit if fail to request relaxed syscall ABI.")
 
 HWASAN_FLAG(
-    uptr, fixed_shadow_base, 0,
-    "If non-zero, HWASan will attempt to allocate the shadow at this address, "
+    uptr, fixed_shadow_base, -1,
+    "If not -1, HWASan will attempt to allocate the shadow at this address, "
     "instead of choosing one dynamically."
     "Tip: this can be combined with the compiler option, "
     "-hwasan-mapping-offset, to optimize the instrumentation.")
diff --git a/compiler-rt/lib/hwasan/hwasan_linux.cpp b/compiler-rt/lib/hwasan/hwasan_linux.cpp
index e7cf36ef3161cbf..f01fa427641347b 100644
--- a/compiler-rt/lib/hwasan/hwasan_linux.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_linux.cpp
@@ -106,9 +106,7 @@ static uptr GetHighMemEnd() {
 }
 
 static void InitializeShadowBaseAddress(uptr shadow_size_bytes) {
-  // NULL is generally address zero, so it is not a valid location for the
-  // shadow.
-  if (flags()->fixed_shadow_base != 0) {
+  if (flags()->fixed_shadow_base != (uptr)-1) {
     __hwasan_shadow_memory_dynamic_address = flags()->fixed_shadow_base;
   } else {
     __hwasan_shadow_memory_dynamic_address =



More information about the llvm-commits mailing list