1 // RUN: %clangxx_cfi -o %t1 %s
2 // RUN: %expect_crash %t1 2>&1 | FileCheck --check-prefix=CFI %s
3 
4 // RUN: %clangxx_cfi -DB32 -o %t2 %s
5 // RUN: %expect_crash %t2 2>&1 | FileCheck --check-prefix=CFI %s
6 
7 // RUN: %clangxx_cfi -DB64 -o %t3 %s
8 // RUN: %expect_crash %t3 2>&1 | FileCheck --check-prefix=CFI %s
9 
10 // RUN: %clangxx_cfi -DBM -o %t4 %s
11 // RUN: %expect_crash %t4 2>&1 | FileCheck --check-prefix=CFI %s
12 
13 // RUN: %clangxx -o %t5 %s
14 // RUN: %t5 2>&1 | FileCheck --check-prefix=NCFI %s
15 
16 // RUN: %clangxx_cfi_diag -o %t6 %s
17 // RUN: %t6 2>&1 | FileCheck --check-prefix=CFI-DIAG %s
18 
19 // Tests that the CFI enforcement also applies to virtual destructor calls made
20 // via 'delete'.
21 
22 // REQUIRES: cxxabi
23 
24 #include <stdio.h>
25 #include "utils.h"
26 
27 struct A {
28   virtual ~A();
29 };
30 
31 A::~A() {}
32 
33 struct B {
34   virtual ~B();
35 };
36 
37 B::~B() {}
38 
39 int main() {
40 #ifdef B32
41   break_optimization(new Deriver<B, 0>);
42 #endif
43 
44 #ifdef B64
45   break_optimization(new Deriver<B, 0>);
46   break_optimization(new Deriver<B, 1>);
47 #endif
48 
49 #ifdef BM
50   break_optimization(new Deriver<B, 0>);
51   break_optimization(new Deriver<B, 1>);
52   break_optimization(new Deriver<B, 2>);
53 #endif
54 
55   A *a = new A;
56   break_optimization(a);
57 
58   // CFI: 1
59   // NCFI: 1
60   fprintf(stderr, "1\n");
61 
62   // CFI-DIAG: runtime error: control flow integrity check for type 'B' failed during virtual call
63   // CFI-DIAG-NEXT: note: vtable is of type '{{(struct )?}}A'
64   delete (B *)a; // UB here
65 
66   // CFI-NOT: {{^2$}}
67   // NCFI: {{^2$}}
68   fprintf(stderr, "2\n");
69 }
70