1 // RUN: %clangxx_cfi_dso -DSHARED_LIB %s -fPIC -shared -o %t1-so.so
2 // RUN: %clangxx_cfi_dso %s -o %t1 %t1-so.so
3 // RUN: %t1 2>&1 | FileCheck --check-prefix=CFI %s
4
5 // RUN: %clangxx_cfi_dso -DB32 -DSHARED_LIB %s -fPIC -shared -o %t2-so.so
6 // RUN: %clangxx_cfi_dso -DB32 %s -o %t2 %t2-so.so
7 // RUN: %t2 2>&1 | FileCheck --check-prefix=CFI %s
8
9 // RUN: %clangxx_cfi_dso -DB64 -DSHARED_LIB %s -fPIC -shared -o %t3-so.so
10 // RUN: %clangxx_cfi_dso -DB64 %s -o %t3 %t3-so.so
11 // RUN: %t3 2>&1 | FileCheck --check-prefix=CFI %s
12
13 // RUN: %clangxx_cfi_dso -DBM -DSHARED_LIB %s -fPIC -shared -o %t4-so.so
14 // RUN: %clangxx_cfi_dso -DBM %s -o %t4 %t4-so.so
15 // RUN: %t4 2>&1 | FileCheck --check-prefix=CFI %s
16
17 // RUN: %clangxx -DBM -DSHARED_LIB %s -fPIC -shared -o %t5-so.so
18 // RUN: %clangxx -DBM %s -o %t5 %t5-so.so
19 // RUN: %t5 2>&1 | FileCheck --check-prefix=NCFI %s
20 // RUN: %t5 x 2>&1 | FileCheck --check-prefix=NCFI %s
21
22 // Tests that the CFI mechanism crashes the program when making a virtual call
23 // to an object of the wrong class but with a compatible vtable, by casting a
24 // pointer to such an object and attempting to make a call through it.
25
26 // REQUIRES: cxxabi
27
28 #include <stdio.h>
29 #include <string.h>
30
31 struct A {
32 virtual void f();
33 };
34
35 A *create_B();
36
37 #ifdef SHARED_LIB
38
39 #include "../utils.h"
40 struct B : public A {
41 virtual void f();
42 };
f()43 void B::f() {}
44
create_B()45 A *create_B() {
46 create_derivers<B>();
47 return new B();
48 }
49
50 #else
51
f()52 void A::f() {}
53
main(int argc,char * argv[])54 int main(int argc, char *argv[]) {
55 A *a = create_B();
56
57 // CFI: =1=
58 // NCFI: =1=
59 fprintf(stderr, "=1=\n");
60 a->f(); // OK
61 // CFI: =2=
62 // NCFI: =2=
63 fprintf(stderr, "=2=\n");
64 }
65 #endif
66