Skip to content

Commit 51fbab1

Browse files
authored
[asan] Enable StackSafetyAnalysis by default
StackSafetyAnalysis determines whether stack-allocated variables are guaranteed to be safe from memory access bugs and enables the removal of certain unneeded instrumentations. (hwasan enables StackSafetyAnalysis in https://reviews.llvm.org/D108381) Test updates: * asan-stack-safety.ll: test the -asan-use-stack-safety=1 default * lifetime-uar-uas.ll: switch to an indexed store to prevent StackSafetyAnalysis from optimizing out instrumentation for %c * alloca_vla_interact.cpp: add a load to prevent StackSafetyAnalysis from optimizing out `__asan_alloca_poison` for the VLA `array` * scariness_score_test.cpp: add -asan-use-stack-safety=0 to make a load of a `__asan_poison_memory_region`-poisoned local variable fail as intended. * other .ll tests: add -asan-use-stack-safety=0 Reviewers: kstoimenov, eugenis, vitalybuka Reviewed By: kstoimenov Pull Request: #77210
1 parent 408dce8 commit 51fbab1

File tree

10 files changed

+26
-19
lines changed

10 files changed

+26
-19
lines changed

compiler-rt/test/asan/TestCases/alloca_vla_interact.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ __attribute__((noinline)) void foo(int len) {
3333
if (i) assert(!__asan_region_is_poisoned(bot, 96));
3434
// VLA is unpoisoned at the end of iteration.
3535
volatile char array[i];
36+
// Ensure that asan-use-stack-safety does not optimize out the poisoning.
37+
if (i) array[0] = 0;
3638
assert(!(reinterpret_cast<uintptr_t>(array) & 31L));
3739
// Alloca is unpoisoned at the end of iteration,
3840
// because dominated by VLA.

compiler-rt/test/asan/TestCases/scariness_score_test.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Test how we produce the scariness score.
22

33
// UAR Mode: runtime
4-
// RUN: %clangxx_asan -O0 %s -o %t
4+
// Case 26 loads a __asan_poison_memory_region-poisoned local variable, which is
5+
// only instrumented when StackSafetyAnalysis is disabled.
6+
// RUN: %clangxx_asan -O0 -mllvm -asan-use-stack-safety=0 %s -o %t
57
// On OSX and Windows, alloc_dealloc_mismatch=1 isn't 100% reliable, so it's
68
// off by default. It's safe for these tests, though, so we turn it on.
79
// RUN: export %env_asan_opts=symbolize=0:detect_stack_use_after_return=1:handle_abort=1:print_scariness=1:alloc_dealloc_mismatch=1
@@ -36,7 +38,7 @@
3638
// RUN: not %run %t 27 2>&1 | FileCheck %s --check-prefix=CHECK27
3739
//
3840
// UAR Mode: always
39-
// RUN: %clangxx_asan -O0 %s -o %t -fsanitize-address-use-after-return=always
41+
// RUN: %clangxx_asan -O0 %s -o %t -fsanitize-address-use-after-return=always -mllvm -asan-use-stack-safety=0
4042
// On OSX and Windows, alloc_dealloc_mismatch=1 isn't 100% reliable, so it's
4143
// off by default. It's safe for these tests, though, so we turn it on.
4244
// RUN: export %env_asan_opts=symbolize=0:handle_abort=1:print_scariness=1:alloc_dealloc_mismatch=1

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ static cl::opt<bool> ClInstrumentWrites(
216216
cl::Hidden, cl::init(true));
217217

218218
static cl::opt<bool>
219-
ClUseStackSafety("asan-use-stack-safety", cl::Hidden, cl::init(false),
219+
ClUseStackSafety("asan-use-stack-safety", cl::Hidden, cl::init(true),
220220
cl::Hidden, cl::desc("Use Stack Safety analysis results"),
221221
cl::Optional);
222222

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; REQUIRES: x86-registered-target
22

33
; RUN: opt < %s -S -asan-instrumentation-with-call-threshold=0 -passes=asan -asan-use-stack-safety=0 -o - | FileCheck %s --implicit-check-not="call {{.*}} @__asan_{{load|store|stack}}" --check-prefixes=CHECK,NOSAFETY
4-
; RUN: opt < %s -S -asan-instrumentation-with-call-threshold=0 -passes=asan -asan-use-stack-safety=1 -o - | FileCheck %s --implicit-check-not="call {{.*}} @__asan_{{load|store|stack}}"
4+
; RUN: opt < %s -S -asan-instrumentation-with-call-threshold=0 -passes=asan | FileCheck %s --implicit-check-not="call {{.*}} @__asan_{{load|store|stack}}"
55

66
; CHECK-LABEL: define i32 @load
77
define i32 @load() sanitize_address {

llvm/test/Instrumentation/AddressSanitizer/debug_info.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: opt < %s -passes=asan -asan-use-after-return=never -S | FileCheck %s
1+
; RUN: opt < %s -passes=asan -asan-use-after-return=never -asan-use-stack-safety=0 -S | FileCheck %s
22

33
; Checks that llvm.dbg.declare instructions are updated
44
; accordingly as we merge allocas.

llvm/test/Instrumentation/AddressSanitizer/lifetime-uar-uas.ll

+8-5
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,29 @@ target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3
1111
declare void @llvm.lifetime.start.p0(i64, ptr nocapture) nounwind
1212
declare void @llvm.lifetime.end.p0(i64, ptr nocapture) nounwind
1313

14-
define i32 @basic_test() sanitize_address {
15-
; CHECK-LABEL: define i32 @basic_test()
14+
define i32 @basic_test(i64 %i) sanitize_address {
15+
; CHECK-LABEL: define i32 @basic_test(
1616

1717
entry:
1818
%retval = alloca i32, align 4
19-
%c = alloca i8, align 1
19+
%c = alloca [2 x i8], align 1
2020

2121
; Memory is poisoned in prologue: F1F1F1F104F3F8F2
2222
; CHECK-UAS: store i64 -866676825215864335, ptr %{{[0-9]+}}
23+
; CHECK-UAS-SS-NOT: store i64
2324

2425
call void @llvm.lifetime.start.p0(i64 1, ptr %c)
2526
; Memory is unpoisoned at llvm.lifetime.start: 01
26-
; CHECK-UAS: store i8 1, ptr %{{[0-9]+}}
27+
; CHECK-UAS: store i8 2, ptr %{{[0-9]+}}
2728

29+
%ci = getelementptr inbounds [2 x i8], ptr %c, i64 0, i64 %i
2830
store volatile i32 0, ptr %retval
29-
store volatile i8 0, ptr %c, align 1
31+
store volatile i8 0, ptr %ci, align 1
3032

3133
call void @llvm.lifetime.end.p0(i64 1, ptr %c)
3234
; Memory is poisoned at llvm.lifetime.end: F8
3335
; CHECK-UAS: store i8 -8, ptr %{{[0-9]+}}
36+
; CHECK-UAS-SS-NOT: store i8 -8,
3437

3538
; Unpoison memory at function exit in UAS mode.
3639
; CHECK-UAS: store i64 0, ptr %{{[0-9]+}}

llvm/test/Instrumentation/AddressSanitizer/lifetime.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; Test handling of llvm.lifetime intrinsics.
2-
; RUN: opt < %s -passes=asan -asan-use-after-scope -asan-use-after-return=never -S | FileCheck %s --check-prefixes=CHECK,CHECK-DEFAULT
3-
; RUN: opt < %s -passes=asan -asan-use-after-scope -asan-use-after-return=never -asan-instrument-dynamic-allocas=0 -S | FileCheck %s --check-prefixes=CHECK,CHECK-NO-DYNAMIC
2+
; RUN: opt < %s -passes=asan -asan-use-after-scope -asan-use-after-return=never -asan-use-stack-safety=0 -S | FileCheck %s --check-prefixes=CHECK,CHECK-DEFAULT
3+
; RUN: opt < %s -passes=asan -asan-use-after-scope -asan-use-after-return=never -asan-use-stack-safety=0 -asan-instrument-dynamic-allocas=0 -S | FileCheck %s --check-prefixes=CHECK,CHECK-NO-DYNAMIC
44

55
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
66
target triple = "x86_64-unknown-linux-gnu"

llvm/test/Instrumentation/AddressSanitizer/local_stack_base.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: opt -S -passes=asan -asan-skip-promotable-allocas=0 %s -o - | FileCheck %s
1+
; RUN: opt -S -passes=asan -asan-use-stack-safety=0 -asan-skip-promotable-allocas=0 %s -o - | FileCheck %s
22
; Generated from:
33
; int bar(int y) {
44
; return y + 2;

llvm/test/Instrumentation/AddressSanitizer/stack_dynamic_alloca.ll

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
; RUN: opt < %s -passes=asan -asan-stack-dynamic-alloca \
1+
; RUN: opt < %s -passes=asan -asan-stack-dynamic-alloca -asan-use-stack-safety=0 \
22
; RUN: -asan-use-after-return=runtime -S | FileCheck %s \
33
; RUN: --check-prefixes=CHECK,CHECK-RUNTIME
4-
; RUN: opt < %s -passes=asan -asan-stack-dynamic-alloca -asan-mapping-scale=5 \
4+
; RUN: opt < %s -passes=asan -asan-stack-dynamic-alloca -asan-mapping-scale=5 -asan-use-stack-safety=0 \
55
; RUN: -asan-use-after-return=runtime -S | FileCheck %s \
66
; RUN: --check-prefixes=CHECK,CHECK-RUNTIME
7-
; RUN: opt < %s -passes=asan -asan-stack-dynamic-alloca \
7+
; RUN: opt < %s -passes=asan -asan-stack-dynamic-alloca -asan-use-stack-safety=0 \
88
; RUN: -asan-use-after-return=always -S | FileCheck %s \
99
; RUN: --check-prefixes=CHECK,CHECK-ALWAYS \
1010
; RUN: --implicit-check-not=__asan_option_detect_stack_use_after_return
11-
; RUN: opt < %s -passes=asan -asan-stack-dynamic-alloca \
11+
; RUN: opt < %s -passes=asan -asan-stack-dynamic-alloca -asan-use-stack-safety=0 \
1212
; RUN: -asan-use-after-return=always -S | FileCheck %s \
1313
; RUN: --check-prefixes=CHECK,CHECK-ALWAYS \
1414
; RUN: --implicit-check-not=__asan_option_detect_stack_use_after_return

llvm/test/Instrumentation/AddressSanitizer/stack_layout.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
; Test the ASan's stack layout.
22
; More tests in tests/Transforms/Utils/ASanStackFrameLayoutTest.cpp
3-
; RUN: opt < %s -passes=asan -asan-stack-dynamic-alloca=0 -asan-use-after-scope -S \
3+
; RUN: opt < %s -passes=asan -asan-use-stack-safety=0 -asan-stack-dynamic-alloca=0 -asan-use-after-scope -S \
44
; RUN: | FileCheck %s --check-prefixes=CHECK,CHECK-STATIC
5-
; RUN: opt < %s -passes=asan -asan-stack-dynamic-alloca=1 -asan-use-after-scope -S \
5+
; RUN: opt < %s -passes=asan -asan-use-stack-safety=0 -asan-stack-dynamic-alloca=1 -asan-use-after-scope -S \
66
; RUN: | FileCheck %s --check-prefixes=CHECK,CHECK-DYNAMIC
77

88
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"

0 commit comments

Comments
 (0)