Skip to content

Commit 8680d6d

Browse files
committed
[mte] work around lifetime issue with setjmp.
setjmp can return twice, but PostDominatorTree is unaware of this. as such, it overestimates postdominance, leaving some cases where memory does not get untagged on return. this causes false positives later in the program execution. this is a workaround for now, in the longer term PostDominatorTree should be made aware of returns_twice, as this may cause problems elsewhere. See D118647 for equivalent fix to HWASan. Reviewed By: eugenis Differential Revision: https://reviews.llvm.org/D118749
1 parent 2b0b9b2 commit 8680d6d

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

llvm/lib/Target/AArch64/AArch64StackTagging.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,14 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
536536
SmallVector<Instruction *, 8> RetVec;
537537
SmallVector<Instruction *, 4> UnrecognizedLifetimes;
538538

539+
bool CallsReturnTwice = false;
539540
for (auto &BB : *F) {
540541
for (Instruction &I : BB) {
542+
if (CallInst *CI = dyn_cast<CallInst>(&I)) {
543+
if (CI->canReturnTwice()) {
544+
CallsReturnTwice = true;
545+
}
546+
}
541547
if (auto *AI = dyn_cast<AllocaInst>(&I)) {
542548
Allocas[AI].AI = AI;
543549
Allocas[AI].OldAI = AI;
@@ -639,8 +645,12 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
639645
Info.AI->replaceAllUsesWith(TagPCall);
640646
TagPCall->setOperand(0, Info.AI);
641647

648+
// Calls to functions that may return twice (e.g. setjmp) confuse the
649+
// postdominator analysis, and will leave us to keep memory tagged after
650+
// function return. Work around this by always untagging at every return
651+
// statement if return_twice functions are called.
642652
if (UnrecognizedLifetimes.empty() && Info.LifetimeStart.size() == 1 &&
643-
Info.LifetimeEnd.size() == 1) {
653+
Info.LifetimeEnd.size() == 1 && !CallsReturnTwice) {
644654
IntrinsicInst *Start = Info.LifetimeStart[0];
645655
IntrinsicInst *End = Info.LifetimeEnd[0];
646656
uint64_t Size =
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
; RUN: opt -S -aarch64-stack-tagging %s -o - | FileCheck %s
2+
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
3+
target triple = "aarch64-unknown-linux-android29"
4+
5+
@stackbuf = dso_local local_unnamed_addr global i8* null, align 8
6+
@jbuf = dso_local global [32 x i64] zeroinitializer, align 8
7+
8+
declare void @may_jump()
9+
10+
define dso_local noundef i1 @_Z6targetv() sanitize_memtag {
11+
entry:
12+
%buf = alloca [4096 x i8], align 1
13+
%call = call i32 @setjmp(i64* noundef getelementptr inbounds ([32 x i64], [32 x i64]* @jbuf, i64 0, i64 0))
14+
switch i32 %call, label %while.body [
15+
i32 1, label %return
16+
i32 2, label %sw.bb1
17+
]
18+
19+
sw.bb1: ; preds = %entry
20+
br label %return
21+
22+
while.body: ; preds = %entry
23+
%0 = getelementptr inbounds [4096 x i8], [4096 x i8]* %buf, i64 0, i64 0
24+
call void @llvm.lifetime.start.p0i8(i64 4096, i8* nonnull %0) #10
25+
store i8* %0, i8** @stackbuf, align 8
26+
; may_jump may call longjmp, going back to the switch (and then the return),
27+
; bypassing the lifetime.end. This is why we need to untag on the return,
28+
; rather than the lifetime.end.
29+
call void @may_jump()
30+
call void @llvm.lifetime.end.p0i8(i64 4096, i8* nonnull %0) #10
31+
br label %return
32+
33+
; CHECK-LABEL: return:
34+
; CHECK: call void @llvm.aarch64.settag
35+
return: ; preds = %entry, %while.body, %sw.bb1
36+
%retval.0 = phi i1 [ true, %while.body ], [ true, %sw.bb1 ], [ false, %entry ]
37+
ret i1 %retval.0
38+
}
39+
40+
declare i32 @setjmp(i64* noundef) returns_twice
41+
42+
declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture)
43+
declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture)
44+

0 commit comments

Comments
 (0)