1*7c3d14c8STreehugger Robot // RUN: %clangxx_cfi -o %t1 %s 2*7c3d14c8STreehugger Robot // RUN: %expect_crash_unless_devirt %t1 2>&1 | FileCheck --check-prefix=CFI %s 3*7c3d14c8STreehugger Robot 4*7c3d14c8STreehugger Robot // RUN: %clangxx_cfi -DB32 -o %t2 %s 5*7c3d14c8STreehugger Robot // RUN: %expect_crash %t2 2>&1 | FileCheck --check-prefix=CFI %s 6*7c3d14c8STreehugger Robot 7*7c3d14c8STreehugger Robot // RUN: %clangxx_cfi -DB64 -o %t3 %s 8*7c3d14c8STreehugger Robot // RUN: %expect_crash %t3 2>&1 | FileCheck --check-prefix=CFI %s 9*7c3d14c8STreehugger Robot 10*7c3d14c8STreehugger Robot // RUN: %clangxx_cfi -DBM -o %t4 %s 11*7c3d14c8STreehugger Robot // RUN: %expect_crash %t4 2>&1 | FileCheck --check-prefix=CFI %s 12*7c3d14c8STreehugger Robot 13*7c3d14c8STreehugger Robot // RUN: %clangxx -o %t5 %s 14*7c3d14c8STreehugger Robot // RUN: %t5 2>&1 | FileCheck --check-prefix=NCFI %s 15*7c3d14c8STreehugger Robot 16*7c3d14c8STreehugger Robot // RUN: %clangxx_cfi_diag -o %t6 %s 17*7c3d14c8STreehugger Robot // RUN: %t6 2>&1 | FileCheck --check-prefix=CFI-DIAG %s 18*7c3d14c8STreehugger Robot 19*7c3d14c8STreehugger Robot // Tests that the CFI mechanism crashes the program when a virtual table is 20*7c3d14c8STreehugger Robot // replaced with a compatible table of function pointers that does not belong to 21*7c3d14c8STreehugger Robot // any class, by manually overwriting the virtual table of an object and 22*7c3d14c8STreehugger Robot // attempting to make a call through it. 23*7c3d14c8STreehugger Robot 24*7c3d14c8STreehugger Robot // REQUIRES: cxxabi 25*7c3d14c8STreehugger Robot 26*7c3d14c8STreehugger Robot #include <stdio.h> 27*7c3d14c8STreehugger Robot #include "utils.h" 28*7c3d14c8STreehugger Robot 29*7c3d14c8STreehugger Robot struct A { 30*7c3d14c8STreehugger Robot virtual void f(); 31*7c3d14c8STreehugger Robot }; 32*7c3d14c8STreehugger Robot f()33*7c3d14c8STreehugger Robotvoid A::f() {} 34*7c3d14c8STreehugger Robot foo()35*7c3d14c8STreehugger Robotvoid foo() { 36*7c3d14c8STreehugger Robot fprintf(stderr, "foo\n"); 37*7c3d14c8STreehugger Robot } 38*7c3d14c8STreehugger Robot 39*7c3d14c8STreehugger Robot void *fake_vtable[] = { 0, 0, (void *)&foo }; 40*7c3d14c8STreehugger Robot main()41*7c3d14c8STreehugger Robotint main() { 42*7c3d14c8STreehugger Robot create_derivers<A>(); 43*7c3d14c8STreehugger Robot 44*7c3d14c8STreehugger Robot A *a = new A; 45*7c3d14c8STreehugger Robot *((void **)a) = fake_vtable + 2; // UB here 46*7c3d14c8STreehugger Robot break_optimization(a); 47*7c3d14c8STreehugger Robot 48*7c3d14c8STreehugger Robot // CFI: 1 49*7c3d14c8STreehugger Robot // NCFI: 1 50*7c3d14c8STreehugger Robot fprintf(stderr, "1\n"); 51*7c3d14c8STreehugger Robot 52*7c3d14c8STreehugger Robot // CFI-NOT: foo 53*7c3d14c8STreehugger Robot // NCFI: foo 54*7c3d14c8STreehugger Robot // CFI-DIAG: runtime error: control flow integrity check for type 'A' failed during virtual call 55*7c3d14c8STreehugger Robot // CFI-DIAG-NEXT: note: invalid vtable 56*7c3d14c8STreehugger Robot a->f(); 57*7c3d14c8STreehugger Robot 58*7c3d14c8STreehugger Robot // We don't check for the absence of a 2 here because under devirtualization 59*7c3d14c8STreehugger Robot // our virtual call may be devirtualized and we will proceed with execution 60*7c3d14c8STreehugger Robot // rather than crashing. 61*7c3d14c8STreehugger Robot 62*7c3d14c8STreehugger Robot // NCFI: {{^2$}} 63*7c3d14c8STreehugger Robot fprintf(stderr, "2\n"); 64*7c3d14c8STreehugger Robot } 65