Skip to content

Commit d6e1e0a

Browse files
kstoimenovvitalybuka
authored andcommitted
[ASan] Use stack safety analysis to optimize allocas instrumentation.
Added alloca optimization which was missed during the implemenation of D112098. Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D130503
1 parent 704a6ea commit d6e1e0a

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,9 @@ bool AddressSanitizer::isInterestingAlloca(const AllocaInst &AI) {
12321232
// dynamic alloca instrumentation for them as well.
12331233
!AI.isUsedWithInAlloca() &&
12341234
// swifterror allocas are register promoted by ISel
1235-
!AI.isSwiftError());
1235+
!AI.isSwiftError() &&
1236+
// safe allocas are not interesting
1237+
!(SSGI && SSGI->isSafe(AI)));
12361238

12371239
ProcessedAllocas[&AI] = IsInteresting;
12381240
return IsInteresting;

llvm/test/Instrumentation/AddressSanitizer/asan-stack-safety.ll

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
; CHECK-LABEL: define i32 @load
77
define i32 @load() sanitize_address {
88
%buf = alloca [10 x i8], align 1
9-
; CHECK: call i64 @__asan_stack_malloc
9+
; NOSAFETY: call i64 @__asan_stack_malloc
1010
%arrayidx = getelementptr inbounds [10 x i8], [10 x i8]* %buf, i64 0, i64 0
1111
%1 = load i8, i8* %arrayidx, align 1
1212
; NOSAFETY: call void @__asan_load1
@@ -16,17 +16,30 @@ define i32 @load() sanitize_address {
1616
; CHECK-LABEL: define i32 @store
1717
define i32 @store() sanitize_address {
1818
%buf = alloca [10 x i8], align 1
19-
; CHECK: call i64 @__asan_stack_malloc
19+
; NOSAFETY: call i64 @__asan_stack_malloc
2020
%arrayidx = getelementptr inbounds [10 x i8], [10 x i8]* %buf, i64 0, i64 0
2121
store i8 0, i8* %arrayidx
2222
; NOSAFETY: call void @__asan_store1
2323
ret i32 0
2424
}
2525

26+
; CHECK-LABEL: define i32 @unsafe_alloca
27+
define i32 @unsafe_alloca(i32 %i) sanitize_address {
28+
%buf.sroa.0 = alloca [10 x i8], align 4
29+
; CHECK: call i64 @__asan_stack_malloc
30+
%ptr = getelementptr [10 x i8], [10 x i8]* %buf.sroa.0, i32 %i, i32 0
31+
store volatile i8 0, i8* %ptr, align 4
32+
; CHECK: call void @__asan_store1
33+
%ptr2 = getelementptr [10 x i8], [10 x i8]* %buf.sroa.0, i32 0, i32 0
34+
store volatile i8 0, i8* %ptr2, align 4
35+
; NOSAFETY: call void @__asan_store1
36+
ret i32 0
37+
}
38+
2639
; CHECK-LABEL: define void @atomicrmw
2740
define void @atomicrmw() sanitize_address {
2841
%buf = alloca [10 x i8], align 1
29-
; CHECK: call i64 @__asan_stack_malloc
42+
; NOSAFETY: call i64 @__asan_stack_malloc
3043
%arrayidx = getelementptr inbounds [10 x i8], [10 x i8]* %buf, i64 0, i64 0
3144
%1 = atomicrmw add i8* %arrayidx, i8 1 seq_cst
3245
; NOSAFETY: call void @__asan_store1
@@ -36,7 +49,7 @@ define void @atomicrmw() sanitize_address {
3649
; CHECK-LABEL: define void @cmpxchg
3750
define void @cmpxchg(i8 %compare_to, i8 %new_value) sanitize_address {
3851
%buf = alloca [10 x i8], align 1
39-
; CHECK: call i64 @__asan_stack_malloc
52+
; NOSAFETY: call i64 @__asan_stack_malloc
4053
%arrayidx = getelementptr inbounds [10 x i8], [10 x i8]* %buf, i64 0, i64 0
4154
%1 = cmpxchg i8* %arrayidx, i8 %compare_to, i8 %new_value seq_cst seq_cst
4255
; NOSAFETY: call void @__asan_store1

0 commit comments

Comments
 (0)