Skip to content

Commit 79a3260

Browse files
authored
[hwasan] Remove memory attrs from instrumented functions. (llvm#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.
1 parent fd1e511 commit 79a3260

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,14 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
15891589

15901590
assert(!ShadowBase);
15911591

1592+
// Remove memory attributes that are about to become invalid.
1593+
// HWASan checks read from shadow, which invalidates memory(argmem: *)
1594+
// Short granule checks on function arguments read from the argument memory
1595+
// (last byte of the granule), which invalidates writeonly.
1596+
F.removeFnAttr(llvm::Attribute::Memory);
1597+
for (auto &A : F.args())
1598+
A.removeAttr(llvm::Attribute::WriteOnly);
1599+
15921600
BasicBlock::iterator InsertPt = F.getEntryBlock().begin();
15931601
IRBuilder<> EntryIRB(&F.getEntryBlock(), InsertPt);
15941602
emitPrologue(EntryIRB,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; Test that HWASan remove writeonly and memory(*) attributes from instrumented functions.
2+
; RUN: opt -S -passes=hwasan %s | FileCheck %s
3+
4+
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
5+
target triple = "aarch64-unknown-linux-android30"
6+
7+
; CHECK: define dso_local void @test_writeonly(ptr nocapture noundef %p) local_unnamed_addr #0
8+
define dso_local void @test_writeonly(ptr nocapture noundef writeonly %p) local_unnamed_addr #0 {
9+
entry:
10+
store i32 42, ptr %p, align 4
11+
ret void
12+
}
13+
14+
; CHECK: attributes #0 = { sanitize_hwaddress uwtable }
15+
attributes #0 = { sanitize_hwaddress memory(argmem: write) uwtable }

0 commit comments

Comments
 (0)