xref: /llvm-project/compiler-rt/test/cfi/cross-dso/simple-pass.cpp (revision f156c932ae2090779767d45572ffd704489086a8)
1 // RUN: %clangxx_cfi_dso -DSHARED_LIB %s -fPIC -shared -o %dynamiclib %ld_flags_rpath_so
2 // RUN: %clangxx_cfi_dso -g %s -o %t %ld_flags_rpath_exe
3 // RUN: %t 2>&1 | FileCheck --check-prefix=CFI %s
4 
5 // RUN: %clangxx_cfi_dso -DB32 -DSHARED_LIB %s -fPIC -shared -o %dynamiclib %ld_flags_rpath_so
6 // RUN: %clangxx_cfi_dso -DB32 %s -o %t %ld_flags_rpath_exe
7 // RUN: %t 2>&1 | FileCheck --check-prefix=CFI %s
8 
9 // RUN: %clangxx_cfi_dso -DB64 -DSHARED_LIB %s -fPIC -shared -o %dynamiclib %ld_flags_rpath_so
10 // RUN: %clangxx_cfi_dso -DB64 %s -o %t %ld_flags_rpath_exe
11 // RUN: %t 2>&1 | FileCheck --check-prefix=CFI %s
12 
13 // RUN: %clangxx_cfi_dso -DBM -DSHARED_LIB %s -fPIC -shared -o %dynamiclib %ld_flags_rpath_so
14 // RUN: %clangxx_cfi_dso -DBM %s -o %t %ld_flags_rpath_exe
15 // RUN: %t 2>&1 | FileCheck --check-prefix=CFI %s
16 
17 // RUN: %clangxx -DBM -DSHARED_LIB %s -fPIC -shared -o %dynamiclib %ld_flags_rpath_so
18 // RUN: %clangxx -DBM %s -o %t %ld_flags_rpath_exe
19 // RUN: %t 2>&1 | FileCheck --check-prefix=NCFI %s
20 // RUN: %t 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