blob: b8520d8b08b106864953b8bc31b796f052b8fb15 [file] [log] [blame]
Peter Collingbourne521a01f2015-07-08 22:10:34 +00001// RUN: %clangxx_cfi -o %t1 %s
Filipe Cabecinhas4f725d62017-10-02 10:21:26 +00002// 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
Peter Collingbourne39a4a9d2015-02-20 20:31:18 +00004
Peter Collingbourne521a01f2015-07-08 22:10:34 +00005// RUN: %clangxx_cfi -DB32 -o %t2 %s
Filipe Cabecinhas4f725d62017-10-02 10:21:26 +00006// 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
Peter Collingbourne4f329a22015-02-21 01:36:08 +00008
Peter Collingbourne521a01f2015-07-08 22:10:34 +00009// RUN: %clangxx_cfi -DB64 -o %t3 %s
Filipe Cabecinhas4f725d62017-10-02 10:21:26 +000010// 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
Peter Collingbourne4f329a22015-02-21 01:36:08 +000012
Peter Collingbourne521a01f2015-07-08 22:10:34 +000013// RUN: %clangxx_cfi -DBM -o %t4 %s
Filipe Cabecinhas4f725d62017-10-02 10:21:26 +000014// 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
Peter Collingbourne4f329a22015-02-21 01:36:08 +000016
Peter Collingbourne521a01f2015-07-08 22:10:34 +000017// RUN: %clangxx -o %t5 %s
Filipe Cabecinhas4f725d62017-10-02 10:21:26 +000018// RUN: %run %t5 2>&1 | FileCheck --check-prefix=NCFI %s
19// RUN: %run %t5 x 2>&1 | FileCheck --check-prefix=NCFI %s
Peter Collingbourne39a4a9d2015-02-20 20:31:18 +000020
Peter Collingbourne521a01f2015-07-08 22:10:34 +000021// RUN: %clangxx_cfi_diag -o %t6 %s
Filipe Cabecinhas4f725d62017-10-02 10:21:26 +000022// RUN: %run %t6 2>&1 | FileCheck --check-prefix=CFI-DIAG2 %s
23// RUN: %run %t6 x 2>&1 | FileCheck --check-prefix=CFI-DIAG1 %s
Peter Collingbourne271d42a2015-06-19 01:52:55 +000024
Peter Collingbourne39a4a9d2015-02-20 20:31:18 +000025// Tests that the CFI mechanism is sensitive to multiple inheritance and only
26// permits calls via virtual tables for the correct base class.
27
Alexey Samsonov4415b422015-06-25 18:45:30 +000028// REQUIRES: cxxabi
29
Peter Collingbourne39a4a9d2015-02-20 20:31:18 +000030#include <stdio.h>
Peter Collingbourne4f329a22015-02-21 01:36:08 +000031#include "utils.h"
Peter Collingbourne39a4a9d2015-02-20 20:31:18 +000032
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) {
Peter Collingbourne87105ff2015-07-29 18:12:45 +000049 create_derivers<A>();
50 create_derivers<B>();
Peter Collingbourne4f329a22015-02-21 01:36:08 +000051
Peter Collingbourne39a4a9d2015-02-20 20:31:18 +000052 C *c = new C;
Peter Collingbourne4f329a22015-02-21 01:36:08 +000053 break_optimization(c);
Peter Collingbourne39a4a9d2015-02-20 20:31:18 +000054
55 // CFI: 1
56 // NCFI: 1
57 fprintf(stderr, "1\n");
58
59 if (argc > 1) {
60 A *a = c;
Peter Collingbourne271d42a2015-06-19 01:52:55 +000061 // CFI-DIAG1: runtime error: control flow integrity check for type 'B' failed during cast to unrelated type
Peter Collingbourne521a01f2015-07-08 22:10:34 +000062 // CFI-DIAG1-NEXT: note: vtable is of type '{{(struct )?}}C'
Peter Collingbourne39a4a9d2015-02-20 20:31:18 +000063 ((B *)a)->g(); // UB here
64 } else {
Peter Collingbourne271d42a2015-06-19 01:52:55 +000065 // CFI-DIAG2: runtime error: control flow integrity check for type 'A' failed during cast to unrelated type
Peter Collingbourne521a01f2015-07-08 22:10:34 +000066 // CFI-DIAG2-NEXT: note: vtable is of type '{{(struct )?}}C'
Peter Collingbourne39a4a9d2015-02-20 20:31:18 +000067 B *b = c;
68 ((A *)b)->f(); // UB here
69 }
70
Peter Collingbourne521a01f2015-07-08 22:10:34 +000071 // CFI-NOT: {{^2$}}
72 // NCFI: {{^2$}}
Peter Collingbourne39a4a9d2015-02-20 20:31:18 +000073 fprintf(stderr, "2\n");
74}