ubsan-unreachable.cpp
2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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.*}} }