Skip to content

Commit f6a8774

Browse files
wrotkiMariusz Borsa
andcommitted
[Sanitizers] Don't inline unpoisoning of small stacks when inlining disabled (llvm#75555)
When ASan.MaxInlinePoisoningSize == 0 , it means that no shadow memory operations should be made via inlined instrumentation code, but only via calls to shadow setting functions. This change fixes one violation of this, which happened when the function allocas count was small, i.e. less than 5 - in the code modifying the shadow just before ret instruction. We now explicitly check ASan.MaxInlinePoisoningSize , and if it's 0 then we disallow inlining. It is required for the instrumentation emitting code suitable for handling by ABI implementation. rdar://119513720 Co-authored-by: Mariusz Borsa <[email protected]> (cherry picked from commit 7e4ae28)
1 parent 2a31eae commit f6a8774

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3503,7 +3503,7 @@ void FunctionStackPoisoner::processStaticAllocas() {
35033503
SplitBlockAndInsertIfThenElse(Cmp, Ret, &ThenTerm, &ElseTerm);
35043504

35053505
IRBuilder<> IRBPoison(ThenTerm);
3506-
if (StackMallocIdx <= 4) {
3506+
if (ASan.MaxInlinePoisoningSize != 0 && StackMallocIdx <= 4) {
35073507
int ClassSize = kMinStackMallocSize << StackMallocIdx;
35083508
ShadowAfterReturn.resize(ClassSize / L.Granularity,
35093509
kAsanStackUseAfterReturnMagic);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; RUN: opt < %s -passes=asan -asan-max-inline-poisoning-size=0 -asan-stack-dynamic-alloca=0 -S | FileCheck --check-prefix=OUTLINE %s
2+
; RUN: opt < %s -passes=asan -asan-max-inline-poisoning-size=999 -asan-stack-dynamic-alloca=0 -S | FileCheck --check-prefix=INLINE %s
3+
4+
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
5+
target triple = "arm64-apple-macosx13.0.0"
6+
7+
; Function Attrs: noinline nounwind optnone sanitize_address ssp uwtable(sync)
8+
define void @foo() #0 {
9+
entry:
10+
%array01 = alloca [1 x i8], align 1
11+
%array02 = alloca [2 x i8], align 1
12+
; OUTLINE: call void @__asan_set_shadow_f1(i64 %23, i64 4)
13+
; OUTLINE: call void @__asan_set_shadow_01(i64 %24, i64 1)
14+
; OUTLINE: call void @__asan_set_shadow_f2(i64 %25, i64 1)
15+
; OUTLINE: call void @__asan_set_shadow_02(i64 %26, i64 1)
16+
; OUTLINE: call void @__asan_set_shadow_f3(i64 %27, i64 1)
17+
; OUTLINE: call void @__asan_stack_free_0(i64 %7, i64 64)
18+
; OUTLINE: call void @__asan_set_shadow_00(i64 %55, i64 8)
19+
; INLINE: store i64 -935919682371587599, ptr %24, align 1
20+
; INLINE: store i64 -723401728380766731, ptr %52, align 1
21+
%arrayidx = getelementptr inbounds [1 x i8], ptr %array01, i64 0, i64 1
22+
store i8 1, ptr %arrayidx, align 1
23+
%arrayidx1 = getelementptr inbounds [2 x i8], ptr %array02, i64 0, i64 2
24+
store i8 2, ptr %arrayidx1, align 1
25+
ret void
26+
}
27+
attributes #0 = { noinline nounwind optnone sanitize_address ssp uwtable(sync) "frame-pointer"="non-leaf" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="apple-m1" "target-features"="+aes,+crc,+crypto,+dotprod,+fp-armv8,+fp16fml,+fullfp16,+lse,+neon,+ras,+rcpc,+rdm,+sha2,+sha3,+sm4,+v8.1a,+v8.2a,+v8.3a,+v8.4a,+v8.5a,+v8a,+zcm,+zcz" }
28+

llvm/test/Instrumentation/AddressSanitizer/calls-only.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ entry:
2929
; OUTLINE: call void @__asan_set_shadow_f2(i64 %45, i64 3)
3030
; OUTLINE: call void @__asan_set_shadow_07(i64 %46, i64 1)
3131
; OUTLINE: call void @__asan_set_shadow_f3(i64 %47, i64 3)
32-
; OUTLINE: call void @__asan_set_shadow_f5(i64 %134, i64 32)
33-
; OUTLINE: call void @__asan_set_shadow_00(i64 %140, i64 24)
32+
; OUTLINE: call void @__asan_stack_free_2(i64 %7, i64 192)
33+
; OUTLINE: call void @__asan_set_shadow_00(i64 %135, i64 24)
3434
; INLINE: store i64 -1007977276409515535, ptr %34, align 1
3535
; INLINE: store i64 -940423264817843709, ptr %36, align 1
3636
; INLINE: store i64 -868083087686045178, ptr %38, align 1

0 commit comments

Comments
 (0)