1*7c3d14c8STreehugger Robot // RUN: %clangxx_cfi -o %t1 %s 2*7c3d14c8STreehugger Robot // RUN: %expect_crash %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 making a non-virtual 20*7c3d14c8STreehugger Robot // call to an object of the wrong class, by casting a pointer to such an object 21*7c3d14c8STreehugger Robot // and attempting to make a call through it. 22*7c3d14c8STreehugger Robot 23*7c3d14c8STreehugger Robot // REQUIRES: cxxabi 24*7c3d14c8STreehugger Robot 25*7c3d14c8STreehugger Robot #include <stdio.h> 26*7c3d14c8STreehugger Robot #include "utils.h" 27*7c3d14c8STreehugger Robot 28*7c3d14c8STreehugger Robot struct A { 29*7c3d14c8STreehugger Robot virtual void v(); 30*7c3d14c8STreehugger Robot }; 31*7c3d14c8STreehugger Robot v()32*7c3d14c8STreehugger Robotvoid A::v() {} 33*7c3d14c8STreehugger Robot 34*7c3d14c8STreehugger Robot struct B { 35*7c3d14c8STreehugger Robot void f(); 36*7c3d14c8STreehugger Robot virtual void g(); 37*7c3d14c8STreehugger Robot }; 38*7c3d14c8STreehugger Robot f()39*7c3d14c8STreehugger Robotvoid B::f() {} g()40*7c3d14c8STreehugger Robotvoid B::g() {} 41*7c3d14c8STreehugger Robot main()42*7c3d14c8STreehugger Robotint main() { 43*7c3d14c8STreehugger Robot create_derivers<B>(); 44*7c3d14c8STreehugger Robot 45*7c3d14c8STreehugger Robot A *a = new A; 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-DIAG: runtime error: control flow integrity check for type 'B' failed during non-virtual call 53*7c3d14c8STreehugger Robot // CFI-DIAG-NEXT: note: vtable is of type '{{(struct )?}}A' 54*7c3d14c8STreehugger Robot ((B *)a)->f(); // UB here 55*7c3d14c8STreehugger Robot 56*7c3d14c8STreehugger Robot // CFI-NOT: {{^2$}} 57*7c3d14c8STreehugger Robot // NCFI: {{^2$}} 58*7c3d14c8STreehugger Robot fprintf(stderr, "2\n"); 59*7c3d14c8STreehugger Robot } 60