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 |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 3 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 4 | // RUN: %clangxx_cfi -DB32 -o %t2 %s |
| 5 | // RUN: %expect_crash %t2 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 6 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 7 | // RUN: %clangxx_cfi -DB64 -o %t3 %s |
| 8 | // RUN: %expect_crash %t3 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 9 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 10 | // RUN: %clangxx_cfi -DBM -o %t4 %s |
| 11 | // RUN: %expect_crash %t4 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 -O1 -o %t5 %s |
| 14 | // RUN: %expect_crash %t5 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 15 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 16 | // RUN: %clangxx_cfi -O1 -DB32 -o %t6 %s |
| 17 | // RUN: %expect_crash %t6 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 18 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 19 | // RUN: %clangxx_cfi -O1 -DB64 -o %t7 %s |
| 20 | // RUN: %expect_crash %t7 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 21 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 22 | // RUN: %clangxx_cfi -O1 -DBM -o %t8 %s |
| 23 | // RUN: %expect_crash %t8 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 24 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 25 | // RUN: %clangxx_cfi -O2 -o %t9 %s |
| 26 | // RUN: %expect_crash %t9 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 27 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 28 | // RUN: %clangxx_cfi -O2 -DB32 -o %t10 %s |
| 29 | // RUN: %expect_crash %t10 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 30 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 31 | // RUN: %clangxx_cfi -O2 -DB64 -o %t11 %s |
| 32 | // RUN: %expect_crash %t11 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 33 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 34 | // RUN: %clangxx_cfi -O2 -DBM -o %t12 %s |
| 35 | // RUN: %expect_crash %t12 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 36 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 37 | // RUN: %clangxx_cfi -O3 -o %t13 %s |
| 38 | // RUN: %expect_crash %t13 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 39 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 40 | // RUN: %clangxx_cfi -O3 -DB32 -o %t14 %s |
| 41 | // RUN: %expect_crash %t14 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 42 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 43 | // RUN: %clangxx_cfi -O3 -DB64 -o %t15 %s |
| 44 | // RUN: %expect_crash %t15 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 45 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 46 | // RUN: %clangxx_cfi -O3 -DBM -o %t16 %s |
| 47 | // RUN: %expect_crash %t16 2>&1 | FileCheck --check-prefix=CFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 48 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 49 | // RUN: %clangxx_cfi_diag -o %t17 %s |
| 50 | // RUN: %t17 2>&1 | FileCheck --check-prefix=CFI-DIAG %s |
| 51 | |
| 52 | // RUN: %clangxx -o %t18 %s |
| 53 | // RUN: %t18 2>&1 | FileCheck --check-prefix=NCFI %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 54 | |
| 55 | // Tests that the CFI mechanism crashes the program when making a virtual call |
| 56 | // to an object of the wrong class but with a compatible vtable, by casting a |
| 57 | // pointer to such an object and attempting to make a call through it. |
| 58 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 59 | // REQUIRES: cxxabi |
| 60 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 61 | #include <stdio.h> |
| 62 | #include "utils.h" |
| 63 | |
| 64 | struct A { |
| 65 | virtual void f(); |
| 66 | }; |
| 67 | |
| 68 | void A::f() {} |
| 69 | |
| 70 | struct B { |
| 71 | virtual void f(); |
| 72 | }; |
| 73 | |
| 74 | void B::f() {} |
| 75 | |
| 76 | int main() { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 77 | create_derivers<B>(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 78 | |
| 79 | A *a = new A; |
| 80 | break_optimization(a); |
| 81 | |
| 82 | // CFI: 1 |
| 83 | // NCFI: 1 |
| 84 | fprintf(stderr, "1\n"); |
| 85 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 86 | // CFI-DIAG: runtime error: control flow integrity check for type 'B' failed during cast to unrelated type |
| 87 | // CFI-DIAG-NEXT: note: vtable is of type '{{(struct )?}}A' |
| 88 | // CFI-DIAG: runtime error: control flow integrity check for type 'B' failed during virtual call |
| 89 | // CFI-DIAG-NEXT: note: vtable is of type '{{(struct )?}}A' |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 90 | ((B *)a)->f(); // UB here |
| 91 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 92 | // CFI-NOT: {{^2$}} |
| 93 | // NCFI: {{^2$}} |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 94 | fprintf(stderr, "2\n"); |
| 95 | } |