1// RUN: %clangxx_cfi -o %t1 %s
2// RUN: %expect_crash %run %t1 2>&1 | FileCheck --check-prefix=CFI %s
3// RUN: %expect_crash %run %t1 x 2>&1 | FileCheck --check-prefix=CFI %s
4
5// RUN: %clangxx_cfi -DB32 -o %t2 %s
6// RUN: %expect_crash %run %t2 2>&1 | FileCheck --check-prefix=CFI %s
7// RUN: %expect_crash %run %t2 x 2>&1 | FileCheck --check-prefix=CFI %s
8
9// RUN: %clangxx_cfi -DB64 -o %t3 %s
10// RUN: %expect_crash %run %t3 2>&1 | FileCheck --check-prefix=CFI %s
11// RUN: %expect_crash %run %t3 x 2>&1 | FileCheck --check-prefix=CFI %s
12
13// RUN: %clangxx_cfi -DBM -o %t4 %s
14// RUN: %expect_crash %run %t4 2>&1 | FileCheck --check-prefix=CFI %s
15// RUN: %expect_crash %run %t4 x 2>&1 | FileCheck --check-prefix=CFI %s
16
17// RUN: %clangxx -o %t5 %s
18// RUN: %run %t5 2>&1 | FileCheck --check-prefix=NCFI %s
19// RUN: %run %t5 x 2>&1 | FileCheck --check-prefix=NCFI %s
20
21// RUN: %clangxx_cfi_diag -o %t6 %s
22// RUN: %run %t6 2>&1 | FileCheck --check-prefix=CFI-DIAG2 %s
23// RUN: %run %t6 x 2>&1 | FileCheck --check-prefix=CFI-DIAG1 %s
24
25// Tests that the CFI mechanism is sensitive to multiple inheritance and only
26// permits calls via virtual tables for the correct base class.
27
28// REQUIRES: cxxabi
29
30#include <stdio.h>
31#include "utils.h"
32
33struct A {
34 virtual void f() = 0;
35};
36
37struct B {
38 virtual void g() = 0;
39};
40
41struct C : A, B {
42 virtual void f(), g();
43};
44
45void C::f() {}
46void C::g() {}
47
48int main(int argc, char **argv) {
49 create_derivers<A>();
50 create_derivers<B>();
51
52 C *c = new C;
53 break_optimization(arg: c);
54
55 // CFI: 1
56 // NCFI: 1
57 fprintf(stderr, format: "1\n");
58
59 if (argc > 1) {
60 A *a = c;
61 // CFI-DIAG1: runtime error: control flow integrity check for type 'B' failed during cast to unrelated type
62 // CFI-DIAG1-NEXT: note: vtable is of type '{{(struct )?}}C'
63 ((B *)a)->g(); // UB here
64 } else {
65 // CFI-DIAG2: runtime error: control flow integrity check for type 'A' failed during cast to unrelated type
66 // CFI-DIAG2-NEXT: note: vtable is of type '{{(struct )?}}C'
67 B *b = c;
68 ((A *)b)->f(); // UB here
69 }
70
71 // CFI-NOT: {{^2$}}
72 // NCFI: {{^2$}}
73 fprintf(stderr, format: "2\n");
74}
75

source code of compiler-rt/test/cfi/multiple-inheritance.cpp