65 lines · cpp
1// RUN: %clangxx_cfi -o %t1 %s2// RUN: %expect_crash_unless_devirt %run %t1 2>&1 | FileCheck --check-prefix=CFI %s3 4// RUN: %clangxx_cfi -DB32 -o %t2 %s5// RUN: %expect_crash %run %t2 2>&1 | FileCheck --check-prefix=CFI %s6 7// RUN: %clangxx_cfi -DB64 -o %t3 %s8// RUN: %expect_crash %run %t3 2>&1 | FileCheck --check-prefix=CFI %s9 10// RUN: %clangxx_cfi -DBM -o %t4 %s11// RUN: %expect_crash %run %t4 2>&1 | FileCheck --check-prefix=CFI %s12 13// RUN: %clangxx -o %t5 %s14// RUN: %run %t5 2>&1 | FileCheck --check-prefix=NCFI %s15 16// RUN: %clangxx_cfi_diag -o %t6 %s17// RUN: %run %t6 2>&1 | FileCheck --check-prefix=CFI-DIAG %s18 19// Tests that the CFI mechanism crashes the program when a virtual table is20// replaced with a compatible table of function pointers that does not belong to21// any class, by manually overwriting the virtual table of an object and22// attempting to make a call through it.23 24// REQUIRES: cxxabi25 26#include <stdio.h>27#include "utils.h"28 29struct A {30 virtual void f();31};32 33void A::f() {}34 35void foo() {36 fprintf(stderr, "foo\n");37}38 39void *fake_vtable[] = { 0, 0, (void *)&foo };40 41int main() {42 create_derivers<A>();43 44 A *a = new A;45 *((void **)a) = fake_vtable + 2; // UB here46 break_optimization(a);47 48 // CFI: 149 // NCFI: 150 fprintf(stderr, "1\n");51 52 // CFI-NOT: foo53 // NCFI: foo54 // CFI-DIAG: runtime error: control flow integrity check for type 'A' failed during virtual call55 // CFI-DIAG-NEXT: note: invalid vtable56 a->f();57 58 // We don't check for the absence of a 2 here because under devirtualization59 // our virtual call may be devirtualized and we will proceed with execution60 // rather than crashing.61 62 // NCFI: {{^2$}}63 fprintf(stderr, "2\n");64}65