Peter Collingbourne | 521a01f | 2015-07-08 22:10:34 +0000 | [diff] [blame] | 1 | // RUN: %clangxx_cfi -o %t1 %s |
Filipe Cabecinhas | 4f725d6 | 2017-10-02 10:21:26 +0000 | [diff] [blame] | 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 |
Peter Collingbourne | 39a4a9d | 2015-02-20 20:31:18 +0000 | [diff] [blame] | 4 | |
Peter Collingbourne | 521a01f | 2015-07-08 22:10:34 +0000 | [diff] [blame] | 5 | // RUN: %clangxx_cfi -DB32 -o %t2 %s |
Filipe Cabecinhas | 4f725d6 | 2017-10-02 10:21:26 +0000 | [diff] [blame] | 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 |
Peter Collingbourne | 4f329a2 | 2015-02-21 01:36:08 +0000 | [diff] [blame] | 8 | |
Peter Collingbourne | 521a01f | 2015-07-08 22:10:34 +0000 | [diff] [blame] | 9 | // RUN: %clangxx_cfi -DB64 -o %t3 %s |
Filipe Cabecinhas | 4f725d6 | 2017-10-02 10:21:26 +0000 | [diff] [blame] | 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 |
Peter Collingbourne | 4f329a2 | 2015-02-21 01:36:08 +0000 | [diff] [blame] | 12 | |
Peter Collingbourne | 521a01f | 2015-07-08 22:10:34 +0000 | [diff] [blame] | 13 | // RUN: %clangxx_cfi -DBM -o %t4 %s |
Filipe Cabecinhas | 4f725d6 | 2017-10-02 10:21:26 +0000 | [diff] [blame] | 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 |
Peter Collingbourne | 4f329a2 | 2015-02-21 01:36:08 +0000 | [diff] [blame] | 16 | |
Peter Collingbourne | 521a01f | 2015-07-08 22:10:34 +0000 | [diff] [blame] | 17 | // RUN: %clangxx -o %t5 %s |
Filipe Cabecinhas | 4f725d6 | 2017-10-02 10:21:26 +0000 | [diff] [blame] | 18 | // RUN: %run %t5 2>&1 | FileCheck --check-prefix=NCFI %s |
| 19 | // RUN: %run %t5 x 2>&1 | FileCheck --check-prefix=NCFI %s |
Peter Collingbourne | 39a4a9d | 2015-02-20 20:31:18 +0000 | [diff] [blame] | 20 | |
Peter Collingbourne | 521a01f | 2015-07-08 22:10:34 +0000 | [diff] [blame] | 21 | // RUN: %clangxx_cfi_diag -o %t6 %s |
Filipe Cabecinhas | 4f725d6 | 2017-10-02 10:21:26 +0000 | [diff] [blame] | 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 |
Peter Collingbourne | 271d42a | 2015-06-19 01:52:55 +0000 | [diff] [blame] | 24 | |
Peter Collingbourne | 39a4a9d | 2015-02-20 20:31:18 +0000 | [diff] [blame] | 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 | |
Alexey Samsonov | 4415b42 | 2015-06-25 18:45:30 +0000 | [diff] [blame] | 28 | // REQUIRES: cxxabi |
| 29 | |
Peter Collingbourne | 39a4a9d | 2015-02-20 20:31:18 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
Peter Collingbourne | 4f329a2 | 2015-02-21 01:36:08 +0000 | [diff] [blame] | 31 | #include "utils.h" |
Peter Collingbourne | 39a4a9d | 2015-02-20 20:31:18 +0000 | [diff] [blame] | 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) { |
Peter Collingbourne | 87105ff | 2015-07-29 18:12:45 +0000 | [diff] [blame] | 49 | create_derivers<A>(); |
| 50 | create_derivers<B>(); |
Peter Collingbourne | 4f329a2 | 2015-02-21 01:36:08 +0000 | [diff] [blame] | 51 | |
Peter Collingbourne | 39a4a9d | 2015-02-20 20:31:18 +0000 | [diff] [blame] | 52 | C *c = new C; |
Peter Collingbourne | 4f329a2 | 2015-02-21 01:36:08 +0000 | [diff] [blame] | 53 | break_optimization(c); |
Peter Collingbourne | 39a4a9d | 2015-02-20 20:31:18 +0000 | [diff] [blame] | 54 | |
| 55 | // CFI: 1 |
| 56 | // NCFI: 1 |
| 57 | fprintf(stderr, "1\n"); |
| 58 | |
| 59 | if (argc > 1) { |
| 60 | A *a = c; |
Peter Collingbourne | 271d42a | 2015-06-19 01:52:55 +0000 | [diff] [blame] | 61 | // CFI-DIAG1: runtime error: control flow integrity check for type 'B' failed during cast to unrelated type |
Peter Collingbourne | 521a01f | 2015-07-08 22:10:34 +0000 | [diff] [blame] | 62 | // CFI-DIAG1-NEXT: note: vtable is of type '{{(struct )?}}C' |
Peter Collingbourne | 39a4a9d | 2015-02-20 20:31:18 +0000 | [diff] [blame] | 63 | ((B *)a)->g(); // UB here |
| 64 | } else { |
Peter Collingbourne | 271d42a | 2015-06-19 01:52:55 +0000 | [diff] [blame] | 65 | // CFI-DIAG2: runtime error: control flow integrity check for type 'A' failed during cast to unrelated type |
Peter Collingbourne | 521a01f | 2015-07-08 22:10:34 +0000 | [diff] [blame] | 66 | // CFI-DIAG2-NEXT: note: vtable is of type '{{(struct )?}}C' |
Peter Collingbourne | 39a4a9d | 2015-02-20 20:31:18 +0000 | [diff] [blame] | 67 | B *b = c; |
| 68 | ((A *)b)->f(); // UB here |
| 69 | } |
| 70 | |
Peter Collingbourne | 521a01f | 2015-07-08 22:10:34 +0000 | [diff] [blame] | 71 | // CFI-NOT: {{^2$}} |
| 72 | // NCFI: {{^2$}} |
Peter Collingbourne | 39a4a9d | 2015-02-20 20:31:18 +0000 | [diff] [blame] | 73 | fprintf(stderr, "2\n"); |
| 74 | } |