97 lines · cpp
1// UNSUPPORTED: target={{.*windows-msvc.*}}2 3// RUN: %clangxx_cfi -o %t %s4// RUN: %expect_crash %run %t a5// RUN: %expect_crash %run %t b6// RUN: %expect_crash %run %t c7// RUN: %expect_crash %run %t d8// RUN: %expect_crash %run %t e9// RUN: %run %t f10// RUN: %run %t g11 12// RUN: %clangxx_cfi_diag -o %t2 %s13// RUN: %run %t2 a 2>&1 | FileCheck --check-prefix=A %s14// RUN: %run %t2 b 2>&1 | FileCheck --check-prefix=B %s15// RUN: %run %t2 c 2>&1 | FileCheck --check-prefix=C %s16// RUN: %run %t2 d 2>&1 | FileCheck --check-prefix=D %s17// RUN: %run %t2 e 2>&1 | FileCheck --check-prefix=E %s18 19#include <assert.h>20#include <string.h>21 22struct SBase1 {23 void b1() {}24};25 26struct SBase2 {27 void b2() {}28};29 30struct S : SBase1, SBase2 {31 void f1() {}32 int f2() { return 1; }33 virtual void g1() {}34 virtual int g2() { return 1; }35 virtual int g3() { return 1; }36};37 38struct T {39 void f1() {}40 int f2() { return 2; }41 virtual void g1() {}42 virtual int g2() { return 2; }43 virtual void g3() {}44};45 46typedef void (S::*S_void)();47 48typedef int (S::*S_int)();49typedef int (T::*T_int)();50 51template <typename To, typename From>52To bitcast(From f) {53 assert(sizeof(To) == sizeof(From));54 To t;55 memcpy(&t, &f, sizeof(f));56 return t;57}58 59int main(int argc, char **argv) {60 S s;61 T t;62 63 switch (argv[1][0]) {64 case 'a':65 // A: runtime error: control flow integrity check for type 'int (S::*)()' failed during non-virtual pointer to member function call66 // A: note: S::f1() defined here67 (s.*bitcast<S_int>(&S::f1))();68 break;69 case 'b':70 // B: runtime error: control flow integrity check for type 'int (T::*)()' failed during non-virtual pointer to member function call71 // B: note: S::f2() defined here72 (t.*bitcast<T_int>(&S::f2))();73 break;74 case 'c':75 // C: runtime error: control flow integrity check for type 'int (S::*)()' failed during virtual pointer to member function call76 // C: note: vtable is of type 'S'77 (s.*bitcast<S_int>(&S::g1))();78 break;79 case 'd':80 // D: runtime error: control flow integrity check for type 'int (S::*)()' failed during virtual pointer to member function call81 // D: note: vtable is of type 'T'82 (reinterpret_cast<S &>(t).*&S::g2)();83 break;84 case 'e':85 // E: runtime error: control flow integrity check for type 'void (S::*)()' failed during virtual pointer to member function call86 // E: note: vtable is of type 'S'87 (s.*bitcast<S_void>(&T::g3))();88 break;89 case 'f':90 (s.*&SBase1::b1)();91 break;92 case 'g':93 (s.*&SBase2::b2)();94 break;95 }96}97