llvm-project/clang/test/CodeGenCXX/ubsan-unreachable.cpp
Julian Lettner b6c06dc28f [Sanitizers] UBSan unreachable incompatible with ASan in the presence of noreturn calls
Summary:
UBSan wants to detect when unreachable code is actually reached, so it
adds instrumentation before every unreachable instruction. However, the
optimizer will remove code after calls to functions marked with
noreturn. To avoid this UBSan removes noreturn from both the call
instruction as well as from the function itself. Unfortunately, ASan
relies on this annotation to unpoison the stack by inserting calls to
_asan_handle_no_return before noreturn functions. This is important for
functions that do not return but access the the stack memory, e.g.,
unwinder functions *like* longjmp (longjmp itself is actually
"double-proofed" via its interceptor). The result is that when ASan and
UBSan are combined, the noreturn attributes are missing and ASan cannot
unpoison the stack, so it has false positives when stack unwinding is
used.

Changes:
Clang-CodeGen now directly insert calls to `__asan_handle_no_return`
when a call to a noreturn function is encountered and both
UBsan-unreachable and ASan are enabled. This allows UBSan to continue
removing the noreturn attribute from functions without any changes to
the ASan pass.

Previously generated code:
```
  call void @longjmp
  call void @__asan_handle_no_return
  call void @__ubsan_handle_builtin_unreachable
```

Generated code (for now):
```
  call void @__asan_handle_no_return
  call void @longjmp
  call void @__asan_handle_no_return
  call void @__ubsan_handle_builtin_unreachable
```

rdar://problem/40723397

Reviewers: delcypher, eugenis, vsk

Differential Revision: https://reviews.llvm.org/D57278

> llvm-svn: 352690

llvm-svn: 352829
2019-02-01 02:51:00 +00:00

84 lines
2.3 KiB
C++

// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -o - %s -fsanitize=unreachable | FileCheck %s
void abort() __attribute__((noreturn));
// CHECK-LABEL: define void @_Z14calls_noreturnv()
void calls_noreturn() {
// Check absence ([^#]*) of call site attributes (including noreturn)
// CHECK: call void @_Z5abortv(){{[^#]*}}
abort();
// CHECK: __ubsan_handle_builtin_unreachable
// CHECK: unreachable
}
struct A {
// CHECK: declare void @_Z5abortv() [[EXTERN_FN_ATTR:#[0-9]+]]
// CHECK-LABEL: define linkonce_odr void @_ZN1A5call1Ev
void call1() {
// CHECK: call void @_ZN1A16does_not_return2Ev({{.*}}){{[^#]*}}
does_not_return2();
// CHECK: __ubsan_handle_builtin_unreachable
// CHECK: unreachable
}
// Test static members. Checks are below after `struct A` scope ends.
static void does_not_return1() __attribute__((noreturn)) {
abort();
}
// CHECK-LABEL: define linkonce_odr void @_ZN1A5call2Ev
void call2() {
// CHECK: call void @_ZN1A16does_not_return1Ev(){{[^#]*}}
does_not_return1();
// CHECK: __ubsan_handle_builtin_unreachable
// CHECK: unreachable
}
// Test calls through pointers to non-static member functions.
typedef void (A::*MemFn)() __attribute__((noreturn));
// CHECK-LABEL: define linkonce_odr void @_ZN1A5call3Ev
void call3() {
MemFn MF = &A::does_not_return2;
// CHECK: call void %{{[0-9]+\(.*}}){{[^#]*}}
(this->*MF)();
// CHECK: __ubsan_handle_builtin_unreachable
// CHECK: unreachable
}
// Test regular members.
// CHECK-LABEL: define linkonce_odr void @_ZN1A16does_not_return2Ev({{.*}})
// CHECK-SAME: [[USER_FN_ATTR:#[0-9]+]]
void does_not_return2() __attribute__((noreturn)) {
// CHECK: call void @_Z5abortv(){{[^#]*}}
abort();
// CHECK: call void @__ubsan_handle_builtin_unreachable
// CHECK: unreachable
// CHECK: call void @__ubsan_handle_builtin_unreachable
// CHECK: unreachable
}
};
// CHECK-LABEL: define linkonce_odr void @_ZN1A16does_not_return1Ev()
// CHECK-SAME: [[USER_FN_ATTR]]
// CHECK: call void @_Z5abortv(){{[^#]*}}
void force_irgen() {
A a;
a.call1();
a.call2();
a.call3();
}
// `noreturn` should be removed from functions and call sites
// CHECK-LABEL: attributes
// CHECK-NOT: [[USER_FN_ATTR]] = { {{.*noreturn.*}} }
// CHECK-NOT: [[EXTERN_FN_ATTR]] = { {{.*noreturn.*}} }