blob: 9d8f5f49f38da83e005d6343645fbf4dd69f44d8 [file] [log] [blame]
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001// RUN: %clangxx_cfi -o %t1 %s
2// RUN: %expect_crash %t1 2>&1 | FileCheck --check-prefix=CFI %s
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07003
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08004// RUN: %clangxx_cfi -DB32 -o %t2 %s
5// RUN: %expect_crash %t2 2>&1 | FileCheck --check-prefix=CFI %s
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07006
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08007// RUN: %clangxx_cfi -DB64 -o %t3 %s
8// RUN: %expect_crash %t3 2>&1 | FileCheck --check-prefix=CFI %s
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07009
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080010// RUN: %clangxx_cfi -DBM -o %t4 %s
11// RUN: %expect_crash %t4 2>&1 | FileCheck --check-prefix=CFI %s
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070012
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080013// 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
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070018
19// Tests that the CFI mechanism crashes the program when making a non-virtual
20// call to an object of the wrong class, by casting a pointer to such an object
21// and attempting to make a call through it.
22
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080023// REQUIRES: cxxabi
24
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070025#include <stdio.h>
26#include "utils.h"
27
28struct A {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080029 virtual void v();
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070030};
31
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080032void A::v() {}
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070033
34struct B {
35 void f();
36 virtual void g();
37};
38
39void B::f() {}
40void B::g() {}
41
42int main() {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080043 create_derivers<B>();
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070044
45 A *a = new A;
46 break_optimization(a);
47
48 // CFI: 1
49 // NCFI: 1
50 fprintf(stderr, "1\n");
51
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080052 // CFI-DIAG: runtime error: control flow integrity check for type 'B' failed during non-virtual call
53 // CFI-DIAG-NEXT: note: vtable is of type '{{(struct )?}}A'
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070054 ((B *)a)->f(); // UB here
55
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080056 // CFI-NOT: {{^2$}}
57 // NCFI: {{^2$}}
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070058 fprintf(stderr, "2\n");
59}