[PATCH] D50544: [hwasan] Add -hwasan-with-ifunc flag.

Evgenii Stepanov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 9 18:15:50 PDT 2018


eugenis created this revision.
eugenis added reviewers: vitalybuka, kcc.
Herald added subscribers: hiraditya, srhines.

Similar to asan's flag, it can be used to disable the use of ifunc to access hwasan shadow address.


https://reviews.llvm.org/D50544

Files:
  llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
  llvm/test/Instrumentation/HWAddressSanitizer/with-ifunc.ll


Index: llvm/test/Instrumentation/HWAddressSanitizer/with-ifunc.ll
===================================================================
--- /dev/null
+++ llvm/test/Instrumentation/HWAddressSanitizer/with-ifunc.ll
@@ -0,0 +1,30 @@
+; Test -hwasan-with-ifunc flag.
+;
+; RUN: opt -hwasan -S < %s | \
+; RUN:     FileCheck %s --check-prefixes=CHECK,CHECK-IFUNC
+; RUN: opt -hwasan -S -hwasan-with-ifunc=0 < %s | \
+; RUN:     FileCheck %s --check-prefixes=CHECK,CHECK-NOIFUNC
+; RUN: opt -hwasan -S -hwasan-with-ifunc=1 < %s | \
+; RUN:     FileCheck %s --check-prefixes=CHECK,CHECK-IFUNC
+
+target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
+target triple = "aarch64--linux-android22"
+
+; CHECK-IFUNC: @__hwasan_shadow = external global [0 x i8]
+; CHECK-NOIFUNC: @__hwasan_shadow_memory_dynamic_address = external global i64
+
+define i32 @test_load(i32* %a) sanitize_hwaddress {
+; First instrumentation in the function must be to load the dynamic shadow
+; address into a local variable.
+; CHECK-LABEL: @test_load
+; CHECK: entry:
+
+; CHECK-IFUNC:   %[[A:[^ ]*]] = call i64 asm "", "=r,0"([0 x i8]* @__hwasan_shadow)
+; CHECK-IFUNC:   add i64 %{{.*}}, %[[A]]
+
+; CHECK-NOIFUNC: load i64, i64* @__hwasan_shadow_memory_dynamic_address
+
+entry:
+  %x = load i32, i32* %a, align 4
+  ret i32 %x
+}
Index: llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -127,6 +127,11 @@
     cl::desc("HWASan shadow mapping offset [EXPERIMENTAL]"), cl::Hidden,
     cl::init(0));
 
+static cl::opt<bool>
+    ClWithIfunc("hwasan-with-ifunc",
+                cl::desc("Access dynamic shadow through an ifunc global on "
+                         "platforms that support this"),
+                cl::Hidden, cl::init(false));
 namespace {
 
 /// An instrumentation pass implementing detection of addressability bugs
@@ -751,13 +756,21 @@
       IsAndroid && !TargetTriple.isAndroidVersionLT(21);
 
   Scale = kDefaultShadowScale;
+  const bool WithIfunc = ClWithIfunc.getNumOccurrences() > 0
+                             ? ClWithIfunc
+                             : IsAndroidWithIfuncSupport;
 
-  if (ClEnableKhwasan || ClInstrumentWithCalls || !IsAndroidWithIfuncSupport)
+  if (ClMappingOffset.getNumOccurrences() > 0) {
+    InGlobal = false;
+    Offset = ClMappingOffset;
+  } else if (ClEnableKhwasan || ClInstrumentWithCalls) {
+    InGlobal = false;
     Offset = 0;
-  else
+  } else if (WithIfunc) {
+    InGlobal = true;
     Offset = kDynamicShadowSentinel;
-  if (ClMappingOffset.getNumOccurrences() > 0)
-    Offset = ClMappingOffset;
-
-  InGlobal = IsAndroidWithIfuncSupport;
+  } else {
+    InGlobal = false;
+    Offset = kDynamicShadowSentinel;
+  }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50544.160043.patch
Type: text/x-patch
Size: 2891 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180810/632e4477/attachment.bin>


More information about the llvm-commits mailing list