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 -o %t5 %s |
| 14 | // RUN: %t5 2>&1 | FileCheck --check-prefix=NCFI %s |
| 15 | |
| 16 | // RUN: %clangxx_cfi_diag -o %t6 %s |
| 17 | // RUN: %t6 2>&1 | FileCheck --check-prefix=CFI-DIAG %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 18 | |
| 19 | // Tests that the CFI mechanism crashes the program when a virtual table is |
| 20 | // replaced with a compatible table of function pointers that does not belong to |
| 21 | // any class, by manually overwriting the virtual table of an object and |
| 22 | // attempting to make a call through it. |
| 23 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 24 | // REQUIRES: cxxabi |
| 25 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include "utils.h" |
| 28 | |
| 29 | struct A { |
| 30 | virtual void f(); |
| 31 | }; |
| 32 | |
| 33 | void A::f() {} |
| 34 | |
| 35 | void foo() { |
| 36 | fprintf(stderr, "foo\n"); |
| 37 | } |
| 38 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 39 | void *fake_vtable[] = { 0, 0, (void *)&foo }; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 40 | |
| 41 | int main() { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 42 | create_derivers<A>(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 43 | |
| 44 | A *a = new A; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 45 | *((void **)a) = fake_vtable + 2; // UB here |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 46 | break_optimization(a); |
| 47 | |
| 48 | // CFI: 1 |
| 49 | // NCFI: 1 |
| 50 | fprintf(stderr, "1\n"); |
| 51 | |
| 52 | // CFI-NOT: foo |
| 53 | // NCFI: foo |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 54 | // CFI-DIAG: runtime error: control flow integrity check for type 'A' failed during virtual call |
| 55 | // CFI-DIAG-NEXT: note: invalid vtable |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 56 | a->f(); |
| 57 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 58 | // CFI-NOT: {{^2$}} |
| 59 | // NCFI: {{^2$}} |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 60 | fprintf(stderr, "2\n"); |
| 61 | } |