Skip to content

[hwasan] Remove memory attrs from instrumented functions. #92974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2024

Conversation

eugenis
Copy link
Contributor

@eugenis eugenis commented May 21, 2024

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.

@llvmbot
Copy link
Member

llvmbot commented May 21, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

@llvm/pr-subscribers-llvm-transforms

Author: Evgenii Stepanov (eugenis)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/92974.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp (+4)
  • (added) llvm/test/Instrumentation/HWAddressSanitizer/mem-attr.ll (+15)
diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index 8d39217992c71..3b1a5fce85137 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -1589,6 +1589,10 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
 
   assert(!ShadowBase);
 
+  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 }

@eugenis eugenis requested a review from pcc May 21, 2024 23:36
Copy link

github-actions bot commented May 21, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@pcc
Copy link
Contributor

pcc commented May 21, 2024

Do we need to do the same thing for call sites?

Copy link
Contributor

@fmayer fmayer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run clang-format

@eugenis eugenis force-pushed the hwasan-fix branch 2 times, most recently from 1c9ee47 to 0f4004a Compare May 22, 2024 00:05
@eugenis
Copy link
Contributor Author

eugenis commented May 22, 2024

I do not think call site attributes are possible before hwasan - at I least I'm not sure how to cause them.
clang-format applied.

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.
@eugenis
Copy link
Contributor Author

eugenis commented May 22, 2024

more comments

@eugenis eugenis merged commit 79a3260 into llvm:main May 22, 2024
3 of 4 checks passed
qiaojbao pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Jun 26, 2024
…80328586e

Local branch amd-gfx aaa8032 Merged main:0012b1ea84e671a9e0c9f7f2d1564315ed9cbcca into amd-gfx:b8aa986939d6
Remote branch main 79a3260 [hwasan] Remove memory attrs from instrumented functions. (llvm#92974)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants