1 // RUN: %clangxx_cfi -o %t1 %s
2 // RUN: %expect_crash_unless_devirt %run %t1 2>&1 | FileCheck --check-prefix=CFI %s
4 // RUN: %clangxx_cfi -DB32 -o %t2 %s
5 // RUN: %expect_crash %run %t2 2>&1 | FileCheck --check-prefix=CFI %s
7 // RUN: %clangxx_cfi -DB64 -o %t3 %s
8 // RUN: %expect_crash %run %t3 2>&1 | FileCheck --check-prefix=CFI %s
10 // RUN: %clangxx_cfi -DBM -o %t4 %s
11 // RUN: %expect_crash %run %t4 2>&1 | FileCheck --check-prefix=CFI %s
13 // RUN: %clangxx -o %t5 %s
14 // RUN: %run %t5 2>&1 | FileCheck --check-prefix=NCFI %s
16 // RUN: %clangxx_cfi_diag -o %t6 %s
17 // RUN: %run %t6 2>&1 | FileCheck --check-prefix=CFI-DIAG %s
19 // Tests that the CFI mechanism crashes the program when a virtual table is
20 // replaced with a compatible table of function pointers that does not belong to
21 // any class, by manually overwriting the virtual table of an object and
22 // attempting to make a call through it.
36 fprintf(stderr
, "foo\n");
39 void *fake_vtable
[] = { 0, 0, (void *)&foo
};
45 *((void **)a
) = fake_vtable
+ 2; // UB here
46 break_optimization(a
);
50 fprintf(stderr
, "1\n");
54 // CFI-DIAG: runtime error: control flow integrity check for type 'A' failed during virtual call
55 // CFI-DIAG-NEXT: note: invalid vtable
58 // We don't check for the absence of a 2 here because under devirtualization
59 // our virtual call may be devirtualized and we will proceed with execution
60 // rather than crashing.
63 fprintf(stderr
, "2\n");