blob: 712262d13d34d768a7464d83d2ba194e9a700ddc [file] [log] [blame]
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -07001// RUN: %clangxx_cfi -o %t %s
2// RUN: not --crash %t a 2>&1 | FileCheck --check-prefix=FAIL %s
3// RUN: not --crash %t b 2>&1 | FileCheck --check-prefix=FAIL %s
4// RUN: not --crash %t c 2>&1 | FileCheck --check-prefix=FAIL %s
5// RUN: %t d 2>&1 | FileCheck --check-prefix=PASS %s
6// RUN: %t e 2>&1 | FileCheck --check-prefix=PASS %s
7// RUN: %t f 2>&1 | FileCheck --check-prefix=PASS %s
8// RUN: not --crash %t g 2>&1 | FileCheck --check-prefix=FAIL %s
9// RUN: %t h 2>&1 | FileCheck --check-prefix=PASS %s
10
11// RUN: %clangxx_cfi -DB32 -o %t %s
12// RUN: not --crash %t a 2>&1 | FileCheck --check-prefix=FAIL %s
13// RUN: not --crash %t b 2>&1 | FileCheck --check-prefix=FAIL %s
14// RUN: not --crash %t c 2>&1 | FileCheck --check-prefix=FAIL %s
15// RUN: %t d 2>&1 | FileCheck --check-prefix=PASS %s
16// RUN: %t e 2>&1 | FileCheck --check-prefix=PASS %s
17// RUN: %t f 2>&1 | FileCheck --check-prefix=PASS %s
18// RUN: not --crash %t g 2>&1 | FileCheck --check-prefix=FAIL %s
19// RUN: %t h 2>&1 | FileCheck --check-prefix=PASS %s
20
21// RUN: %clangxx_cfi -DB64 -o %t %s
22// RUN: not --crash %t a 2>&1 | FileCheck --check-prefix=FAIL %s
23// RUN: not --crash %t b 2>&1 | FileCheck --check-prefix=FAIL %s
24// RUN: not --crash %t c 2>&1 | FileCheck --check-prefix=FAIL %s
25// RUN: %t d 2>&1 | FileCheck --check-prefix=PASS %s
26// RUN: %t e 2>&1 | FileCheck --check-prefix=PASS %s
27// RUN: %t f 2>&1 | FileCheck --check-prefix=PASS %s
28// RUN: not --crash %t g 2>&1 | FileCheck --check-prefix=FAIL %s
29// RUN: %t h 2>&1 | FileCheck --check-prefix=PASS %s
30
31// RUN: %clangxx_cfi -DBM -o %t %s
32// RUN: not --crash %t a 2>&1 | FileCheck --check-prefix=FAIL %s
33// RUN: not --crash %t b 2>&1 | FileCheck --check-prefix=FAIL %s
34// RUN: not --crash %t c 2>&1 | FileCheck --check-prefix=FAIL %s
35// RUN: %t d 2>&1 | FileCheck --check-prefix=PASS %s
36// RUN: %t e 2>&1 | FileCheck --check-prefix=PASS %s
37// RUN: %t f 2>&1 | FileCheck --check-prefix=PASS %s
38// RUN: not --crash %t g 2>&1 | FileCheck --check-prefix=FAIL %s
39// RUN: %t h 2>&1 | FileCheck --check-prefix=PASS %s
40
41// RUN: %clangxx_cfi -fsanitize=cfi-cast-strict -o %t %s
42// RUN: not --crash %t a 2>&1 | FileCheck --check-prefix=FAIL %s
43// RUN: not --crash %t b 2>&1 | FileCheck --check-prefix=FAIL %s
44// RUN: not --crash %t c 2>&1 | FileCheck --check-prefix=FAIL %s
45// RUN: not --crash %t d 2>&1 | FileCheck --check-prefix=FAIL %s
46// RUN: not --crash %t e 2>&1 | FileCheck --check-prefix=FAIL %s
47// RUN: not --crash %t f 2>&1 | FileCheck --check-prefix=FAIL %s
48// RUN: not --crash %t g 2>&1 | FileCheck --check-prefix=FAIL %s
49// RUN: not --crash %t h 2>&1 | FileCheck --check-prefix=FAIL %s
50
51// RUN: %clangxx -o %t %s
52// RUN: %t a 2>&1 | FileCheck --check-prefix=PASS %s
53// RUN: %t b 2>&1 | FileCheck --check-prefix=PASS %s
54// RUN: %t c 2>&1 | FileCheck --check-prefix=PASS %s
55// RUN: %t d 2>&1 | FileCheck --check-prefix=PASS %s
56// RUN: %t e 2>&1 | FileCheck --check-prefix=PASS %s
57// RUN: %t f 2>&1 | FileCheck --check-prefix=PASS %s
58// RUN: %t g 2>&1 | FileCheck --check-prefix=PASS %s
59// RUN: %t h 2>&1 | FileCheck --check-prefix=PASS %s
60
61// Tests that the CFI enforcement detects bad casts.
62
63#include <stdio.h>
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070064#include "utils.h"
65
66struct A {
67 virtual void f();
68};
69
70void A::f() {}
71
72struct B : A {
73 virtual void f();
74};
75
76void B::f() {}
77
78struct C : A {
79};
80
81int main(int argc, char **argv) {
82#ifdef B32
83 break_optimization(new Deriver<B, 0>);
84#endif
85
86#ifdef B64
87 break_optimization(new Deriver<B, 0>);
88 break_optimization(new Deriver<B, 1>);
89#endif
90
91#ifdef BM
92 break_optimization(new Deriver<B, 0>);
93 break_optimization(new Deriver<B, 1>);
94 break_optimization(new Deriver<B, 2>);
95#endif
96
97 B *b = new B;
98 break_optimization(b);
99
100 // FAIL: 1
101 // PASS: 1
102 fprintf(stderr, "1\n");
103
104 A a;
105 switch (argv[1][0]) {
106 case 'a':
107 static_cast<B *>(&a); // UB
108 break;
109 case 'b':
110 static_cast<B &>(a); // UB
111 break;
112 case 'c':
113 static_cast<B &&>(a); // UB
114 break;
115 case 'd':
116 static_cast<C *>(&a); // UB, strict only
117 break;
118 case 'e':
119 static_cast<C &>(a); // UB, strict only
120 break;
121 case 'f':
122 static_cast<C &&>(a); // UB, strict only
123 break;
124 case 'g':
125 static_cast<B *>(static_cast<void *>(&a)); // Non-UB bad cast
126 break;
127 case 'h':
128 static_cast<C *>(static_cast<void *>(&a)); // Non-UB bad cast, strict only
129 break;
130 }
131
132 // FAIL-NOT: 2
133 // PASS: 2
134 fprintf(stderr, "2\n");
135}