Home | History | Annotate | Download | only in cross-dso
      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: %expect_crash %t1 2>&1 | FileCheck --check-prefix=CFI %s
      4 // RUN: %expect_crash %t1 x 2>&1 | FileCheck --check-prefix=CFI-CAST %s
      5 
      6 // RUN: %clangxx_cfi_dso -DB32 -DSHARED_LIB %s -fPIC -shared -o %t2-so.so
      7 // RUN: %clangxx_cfi_dso -DB32 %s -o %t2 %t2-so.so
      8 // RUN: %expect_crash %t2 2>&1 | FileCheck --check-prefix=CFI %s
      9 // RUN: %expect_crash %t2 x 2>&1 | FileCheck --check-prefix=CFI-CAST %s
     10 
     11 // RUN: %clangxx_cfi_dso -DB64 -DSHARED_LIB %s -fPIC -shared -o %t3-so.so
     12 // RUN: %clangxx_cfi_dso -DB64 %s -o %t3 %t3-so.so
     13 // RUN: %expect_crash %t3 2>&1 | FileCheck --check-prefix=CFI %s
     14 // RUN: %expect_crash %t3 x 2>&1 | FileCheck --check-prefix=CFI-CAST %s
     15 
     16 // RUN: %clangxx_cfi_dso -DBM -DSHARED_LIB %s -fPIC -shared -o %t4-so.so
     17 // RUN: %clangxx_cfi_dso -DBM %s -o %t4 %t4-so.so
     18 // RUN: %expect_crash %t4 2>&1 | FileCheck --check-prefix=CFI %s
     19 // RUN: %expect_crash %t4 x 2>&1 | FileCheck --check-prefix=CFI-CAST %s
     20 
     21 // RUN: %clangxx -DBM -DSHARED_LIB %s -fPIC -shared -o %t5-so.so
     22 // RUN: %clangxx -DBM %s -o %t5 %t5-so.so
     23 // RUN: %t5 2>&1 | FileCheck --check-prefix=NCFI %s
     24 // RUN: %t5 x 2>&1 | FileCheck --check-prefix=NCFI %s
     25 
     26 // RUN: %clangxx -DBM -DSHARED_LIB %s -fPIC -shared -o %t6-so.so
     27 // RUN: %clangxx_cfi_dso -DBM %s -o %t6 %t6-so.so
     28 // RUN: %t6 2>&1 | FileCheck --check-prefix=NCFI %s
     29 // RUN: %t6 x 2>&1 | FileCheck --check-prefix=NCFI %s
     30 
     31 // RUN: %clangxx_cfi_dso_diag -DSHARED_LIB %s -fPIC -shared -o %t7-so.so
     32 // RUN: %clangxx_cfi_dso_diag %s -o %t7 %t7-so.so
     33 // RUN: %t7 2>&1 | FileCheck --check-prefix=CFI-DIAG-CALL %s
     34 // RUN: %t7 x 2>&1 | FileCheck --check-prefix=CFI-DIAG-CALL --check-prefix=CFI-DIAG-CAST %s
     35 
     36 // Tests that the CFI mechanism crashes the program when making a virtual call
     37 // to an object of the wrong class but with a compatible vtable, by casting a
     38 // pointer to such an object and attempting to make a call through it.
     39 
     40 // REQUIRES: cxxabi
     41 
     42 #include <stdio.h>
     43 #include <string.h>
     44 
     45 struct A {
     46   virtual void f();
     47 };
     48 
     49 void *create_B();
     50 
     51 #ifdef SHARED_LIB
     52 
     53 #include "../utils.h"
     54 struct B {
     55   virtual void f();
     56 };
     57 void B::f() {}
     58 
     59 void *create_B() {
     60   create_derivers<B>();
     61   return (void *)(new B());
     62 }
     63 
     64 #else
     65 
     66 void A::f() {}
     67 
     68 int main(int argc, char *argv[]) {
     69   void *p = create_B();
     70   A *a;
     71 
     72   // CFI: =0=
     73   // CFI-CAST: =0=
     74   // NCFI: =0=
     75   fprintf(stderr, "=0=\n");
     76 
     77   if (argc > 1 && argv[1][0] == 'x') {
     78     // Test cast. BOOM.
     79     // CFI-DIAG-CAST: runtime error: control flow integrity check for type 'A' failed during cast to unrelated type
     80     // CFI-DIAG-CAST-NEXT: note: vtable is of type '{{(struct )?}}B'
     81     a = (A*)p;
     82   } else {
     83     // Invisible to CFI. Test virtual call later.
     84     memcpy(&a, &p, sizeof(a));
     85   }
     86 
     87   // CFI: =1=
     88   // CFI-CAST-NOT: =1=
     89   // NCFI: =1=
     90   fprintf(stderr, "=1=\n");
     91 
     92   // CFI-DIAG-CALL: runtime error: control flow integrity check for type 'A' failed during virtual call
     93   // CFI-DIAG-CALL-NEXT: note: vtable is of type '{{(struct )?}}B'
     94   a->f(); // UB here
     95 
     96   // CFI-NOT: =2=
     97   // CFI-CAST-NOT: =2=
     98   // NCFI: =2=
     99   fprintf(stderr, "=2=\n");
    100 }
    101 #endif
    102