Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1 | // RUN: %clangxx_cfi -o %t1 %s |
| 2 | // RUN: %expect_crash %t1 2>&1 | FileCheck --check-prefix=CFI %s |
| 3 | // RUN: %expect_crash %t1 x 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 4 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 5 | // RUN: %clangxx_cfi -DB32 -o %t2 %s |
| 6 | // RUN: %expect_crash %t2 2>&1 | FileCheck --check-prefix=CFI %s |
| 7 | // RUN: %expect_crash %t2 x 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 8 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 9 | // RUN: %clangxx_cfi -DB64 -o %t3 %s |
| 10 | // RUN: %expect_crash %t3 2>&1 | FileCheck --check-prefix=CFI %s |
| 11 | // RUN: %expect_crash %t3 x 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 12 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 13 | // RUN: %clangxx_cfi -DBM -o %t4 %s |
| 14 | // RUN: %expect_crash %t4 2>&1 | FileCheck --check-prefix=CFI %s |
| 15 | // RUN: %expect_crash %t4 x 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 16 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 17 | // RUN: %clangxx -o %t5 %s |
| 18 | // RUN: %t5 2>&1 | FileCheck --check-prefix=NCFI %s |
| 19 | // RUN: %t5 x 2>&1 | FileCheck --check-prefix=NCFI %s |
| 20 | |
| 21 | // RUN: %clangxx_cfi_diag -o %t6 %s |
| 22 | // RUN: %t6 2>&1 | FileCheck --check-prefix=CFI-DIAG2 %s |
| 23 | // RUN: %t6 x 2>&1 | FileCheck --check-prefix=CFI-DIAG1 %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 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 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 28 | // REQUIRES: cxxabi |
| 29 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include "utils.h" |
| 32 | |
| 33 | struct A { |
| 34 | virtual void f() = 0; |
| 35 | }; |
| 36 | |
| 37 | struct B { |
| 38 | virtual void g() = 0; |
| 39 | }; |
| 40 | |
| 41 | struct C : A, B { |
| 42 | virtual void f(), g(); |
| 43 | }; |
| 44 | |
| 45 | void C::f() {} |
| 46 | void C::g() {} |
| 47 | |
| 48 | int main(int argc, char **argv) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 49 | create_derivers<A>(); |
| 50 | create_derivers<B>(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 51 | |
| 52 | C *c = new C; |
| 53 | break_optimization(c); |
| 54 | |
| 55 | // CFI: 1 |
| 56 | // NCFI: 1 |
| 57 | fprintf(stderr, "1\n"); |
| 58 | |
| 59 | if (argc > 1) { |
| 60 | A *a = c; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 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' |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 63 | ((B *)a)->g(); // UB here |
| 64 | } else { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 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' |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 67 | B *b = c; |
| 68 | ((A *)b)->f(); // UB here |
| 69 | } |
| 70 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 71 | // CFI-NOT: {{^2$}} |
| 72 | // NCFI: {{^2$}} |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 73 | fprintf(stderr, "2\n"); |
| 74 | } |