[llvm] 79a3260 - [hwasan] Remove memory attrs from instrumented functions. (#92974)

via llvm-commits llvm-commits at lists.llvm.org
Tue May 21 17:15:12 PDT 2024


Author: Evgenii Stepanov
Date: 2024-05-21T17:15:08-07:00
New Revision: 79a32609759af317a62184c2c7b1300263a336c8

URL: https://github.com/llvm/llvm-project/commit/79a32609759af317a62184c2c7b1300263a336c8
DIFF: https://github.com/llvm/llvm-project/commit/79a32609759af317a62184c2c7b1300263a336c8.diff

LOG: [hwasan] Remove memory attrs from instrumented functions. (#92974)

HWASan instrumentation makes writeonly attribute on function parameters,
as well as most memory(*) attributes invalid. This causes
miscompilations with LTO, when more optimizations are run after the
HWASan pass.

Added: 
    llvm/test/Instrumentation/HWAddressSanitizer/mem-attr.ll

Modified: 
    llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index 8d39217992c71..2aa21759d56e0 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -1589,6 +1589,14 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
 
   assert(!ShadowBase);
 
+  // Remove memory attributes that are about to become invalid.
+  // HWASan checks read from shadow, which invalidates memory(argmem: *)
+  // Short granule checks on function arguments read from the argument memory
+  // (last byte of the granule), which invalidates writeonly.
+  F.removeFnAttr(llvm::Attribute::Memory);
+  for (auto &A : F.args())
+    A.removeAttr(llvm::Attribute::WriteOnly);
+
   BasicBlock::iterator InsertPt = F.getEntryBlock().begin();
   IRBuilder<> EntryIRB(&F.getEntryBlock(), InsertPt);
   emitPrologue(EntryIRB,

diff  --git a/llvm/test/Instrumentation/HWAddressSanitizer/mem-attr.ll b/llvm/test/Instrumentation/HWAddressSanitizer/mem-attr.ll
new file mode 100644
index 0000000000000..c0e370f20213a
--- /dev/null
+++ b/llvm/test/Instrumentation/HWAddressSanitizer/mem-attr.ll
@@ -0,0 +1,15 @@
+; Test that HWASan remove writeonly and memory(*) attributes from instrumented functions.
+; RUN: opt -S -passes=hwasan %s | FileCheck %s
+
+target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
+target triple = "aarch64-unknown-linux-android30"
+
+; CHECK: define dso_local void @test_writeonly(ptr nocapture noundef %p) local_unnamed_addr #0
+define dso_local void @test_writeonly(ptr nocapture noundef writeonly %p) local_unnamed_addr #0 {
+entry:
+  store i32 42, ptr %p, align 4
+  ret void
+}
+
+; CHECK: attributes #0 = { sanitize_hwaddress uwtable }
+attributes #0 = { sanitize_hwaddress memory(argmem: write) uwtable }


        


More information about the llvm-commits mailing list