Skip to content

Commit f82d892

Browse files
committed
[ASan] Do not instrument other runtime functions with __asan_handle_no_return
Summary: Currently, ASan inserts a call to `__asan_handle_no_return` before every `noreturn` function call/invoke. This is unnecessary for calls to other runtime funtions. This patch changes ASan to skip instrumentation for functions calls marked with `!nosanitize` metadata. Reviewers: TODO Differential Revision: https://reviews.llvm.org/D57489 llvm-svn: 352948
1 parent 7d53675 commit f82d892

File tree

4 files changed

+35
-27
lines changed

4 files changed

+35
-27
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4394,8 +4394,8 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
43944394

43954395
// Strip away the noreturn attribute to better diagnose unreachable UB.
43964396
if (SanOpts.has(SanitizerKind::Unreachable)) {
4397-
// Also remove from function since CI->hasFnAttr(..) also checks attributes
4398-
// of the called function.
4397+
// Also remove from function since CallBase::hasFnAttr additionally checks
4398+
// attributes of the called function.
43994399
if (auto *F = CI->getCalledFunction())
44004400
F->removeFnAttr(llvm::Attribute::NoReturn);
44014401
CI->removeAttribute(llvm::AttributeList::FunctionIndex,

clang/test/CodeGen/ubsan-asan-noreturn.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ void calls_noreturn() {
99
my_longjmp();
1010
// CHECK: @__asan_handle_no_return{{.*}} !nosanitize
1111
// CHECK-NEXT: @my_longjmp(){{[^#]*}}
12-
// CHECK: @__asan_handle_no_return()
13-
// CHECK-NEXT: @__ubsan_handle_builtin_unreachable{{.*}} !nosanitize
12+
// CHECK: @__ubsan_handle_builtin_unreachable{{.*}} !nosanitize
1413
// CHECK-NEXT: unreachable
1514
}
1615

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,7 +2554,8 @@ bool AddressSanitizer::runOnFunction(Function &F) {
25542554
if (CS) {
25552555
// A call inside BB.
25562556
TempsToInstrument.clear();
2557-
if (CS.doesNotReturn()) NoReturnCalls.push_back(CS.getInstruction());
2557+
if (CS.doesNotReturn() && !CS->getMetadata("nosanitize"))
2558+
NoReturnCalls.push_back(CS.getInstruction());
25582559
}
25592560
if (CallInst *CI = dyn_cast<CallInst>(&Inst))
25602561
maybeMarkSanitizerLibraryCallNoBuiltin(CI, TLI);
@@ -2591,7 +2592,7 @@ bool AddressSanitizer::runOnFunction(Function &F) {
25912592
FunctionStackPoisoner FSP(F, *this);
25922593
bool ChangedStack = FSP.runOnFunction();
25932594

2594-
// We must unpoison the stack before every NoReturn call (throw, _exit, etc).
2595+
// We must unpoison the stack before NoReturn calls (throw, _exit, etc).
25952596
// See e.g. https://github.com/google/sanitizers/issues/37
25962597
for (auto CI : NoReturnCalls) {
25972598
IRBuilder<> IRB(CI);
Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,45 @@
1-
; RUN: opt < %s -asan -asan-module -S | FileCheck %s
1+
; RUN: opt < %s -asan -S | FileCheck %s
22
; AddressSanitizer must insert __asan_handle_no_return
3-
; before every noreturn call or invoke.
3+
; before noreturn calls that aren't inserted by sanitizers.
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"
66
target triple = "x86_64-unknown-linux-gnu"
77

8-
declare void @MyNoReturnFunc(i32) noreturn
8+
declare void @NormalFunc()
9+
declare void @NoReturnFunc() noreturn
910

10-
define i32 @Call1(i8* nocapture %arg) uwtable sanitize_address {
11-
entry:
12-
call void @MyNoReturnFunc(i32 1) noreturn ; The call insn has noreturn attr.
13-
; CHECK: @Call1
14-
; CHECK: call void @__asan_handle_no_return
15-
; CHECK-NEXT: call void @MyNoReturnFunc
16-
; CHECK-NEXT: unreachable
11+
; Instrument noreturn callsites (regardless of function)
12+
define i32 @Call1() sanitize_address {
13+
call void @NormalFunc() noreturn
1714
unreachable
1815
}
16+
; CHECK-LABEL: @Call1
17+
; CHECK-NEXT: call void @__asan_handle_no_return
18+
; CHECK-NEXT: call void @NormalFunc
1919

20-
define i32 @Call2(i8* nocapture %arg) uwtable sanitize_address {
21-
entry:
22-
call void @MyNoReturnFunc(i32 1) ; No noreturn attribure on the call.
23-
; CHECK: @Call2
24-
; CHECK: call void @__asan_handle_no_return
25-
; CHECK-NEXT: call void @MyNoReturnFunc
26-
; CHECK-NEXT: unreachable
20+
; Instrument calls to noreturn functions (regardless of callsite)
21+
define i32 @Call2() sanitize_address {
22+
call void @NoReturnFunc()
23+
unreachable
24+
}
25+
; CHECK-LABEL: @Call2
26+
; CHECK-NEXT: call void @__asan_handle_no_return
27+
; CHECK-NEXT: call void @NoReturnFunc
28+
29+
; Do *not* instrument callsites marked !nosanitize
30+
define i32 @Call3() sanitize_address {
31+
call void @NoReturnFunc() noreturn, !nosanitize !{}
2732
unreachable
2833
}
34+
; CHECK-LABEL: @Call3
35+
; CHECK-NOT: call void @__asan_handle_no_return
36+
; CHECK: call void @NoReturnFunc
2937

3038
declare i32 @__gxx_personality_v0(...)
3139

32-
define i64 @Invoke1(i8** %esc) nounwind uwtable ssp sanitize_address personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
40+
define i64 @Invoke1() nounwind uwtable ssp sanitize_address personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
3341
entry:
34-
invoke void @MyNoReturnFunc(i32 1)
42+
invoke void @NoReturnFunc()
3543
to label %invoke.cont unwind label %lpad
3644

3745
invoke.cont:
@@ -42,8 +50,8 @@ lpad:
4250
filter [0 x i8*] zeroinitializer
4351
ret i64 1
4452
}
45-
; CHECK: @Invoke1
53+
; CHECK-LABEL: @Invoke1
4654
; CHECK: call void @__asan_handle_no_return
47-
; CHECK-NEXT: invoke void @MyNoReturnFunc
55+
; CHECK-NEXT: invoke void @NoReturnFunc
4856
; CHECK: ret i64 0
4957
; CHECK: ret i64 1

0 commit comments

Comments
 (0)