bad-cast.cpp
5.03 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// RUN: %clangxx_cfi -o %t1 %s
// RUN: %expect_crash %run %t1 a 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t1 b 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t1 c 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %run %t1 d 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t1 e 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t1 f 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %expect_crash %run %t1 g 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %run %t1 h 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %clangxx_cfi -DB32 -o %t2 %s
// RUN: %expect_crash %run %t2 a 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t2 b 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t2 c 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %run %t2 d 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t2 e 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t2 f 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %expect_crash %run %t2 g 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %run %t2 h 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %clangxx_cfi -DB64 -o %t3 %s
// RUN: %expect_crash %run %t3 a 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t3 b 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t3 c 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %run %t3 d 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t3 e 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t3 f 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %expect_crash %run %t3 g 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %run %t3 h 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %clangxx_cfi -DBM -o %t4 %s
// RUN: %expect_crash %run %t4 a 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t4 b 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t4 c 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %run %t4 d 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t4 e 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t4 f 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %expect_crash %run %t4 g 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %run %t4 h 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %clangxx_cfi -fsanitize=cfi-cast-strict -o %t5 %s
// RUN: %expect_crash %run %t5 a 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t5 b 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t5 c 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t5 d 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t5 e 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t5 f 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t5 g 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %expect_crash %run %t5 h 2>&1 | FileCheck --check-prefix=FAIL %s
// RUN: %clangxx -o %t6 %s
// RUN: %run %t6 a 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t6 b 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t6 c 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t6 d 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t6 e 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t6 f 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t6 g 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %run %t6 h 2>&1 | FileCheck --check-prefix=PASS %s
// RUN: %clangxx_cfi_diag -o %t7 %s
// RUN: %run %t7 a 2>&1 | FileCheck --check-prefix=CFI-DIAG-D %s
// RUN: %run %t7 b 2>&1 | FileCheck --check-prefix=CFI-DIAG-D %s
// RUN: %run %t7 c 2>&1 | FileCheck --check-prefix=CFI-DIAG-D %s
// RUN: %run %t7 g 2>&1 | FileCheck --check-prefix=CFI-DIAG-U %s
// Tests that the CFI enforcement detects bad casts.
// REQUIRES: cxxabi
#include <stdio.h>
#include "utils.h"
struct A {
virtual void f();
};
void A::f() {}
struct B : A {
virtual void f();
};
void B::f() {}
struct C : A {
};
int main(int argc, char **argv) {
create_derivers<B>();
B *b = new B;
break_optimization(b);
// FAIL: 1
// PASS: 1
fprintf(stderr, "1\n");
A a;
// CFI-DIAG-D: runtime error: control flow integrity check for type 'B' failed during base-to-derived cast
// CFI-DIAG-D-NEXT: note: vtable is of type '{{(struct )?}}A'
// CFI-DIAG-U: runtime error: control flow integrity check for type 'B' failed during cast to unrelated type
// CFI-DIAG-U-NEXT: note: vtable is of type '{{(struct )?}}A'
switch (argv[1][0]) {
case 'a':
static_cast<B *>(&a); // UB
break;
case 'b':
static_cast<B &>(a); // UB
break;
case 'c':
static_cast<B &&>(a); // UB
break;
case 'd':
static_cast<C *>(&a); // UB, strict only
break;
case 'e':
static_cast<C &>(a); // UB, strict only
break;
case 'f':
static_cast<C &&>(a); // UB, strict only
break;
case 'g':
static_cast<B *>(static_cast<void *>(&a)); // Non-UB bad cast
break;
case 'h':
static_cast<C *>(static_cast<void *>(&a)); // Non-UB bad cast, strict only
break;
}
// FAIL-NOT: {{^2$}}
// PASS: {{^2$}}
fprintf(stderr, "2\n");
}