2015-07-09 06:10:34 +08:00
|
|
|
// RUN: %clangxx_cfi -o %t1 %s
|
2017-10-02 18:21:26 +08:00
|
|
|
// RUN: %expect_crash %run %t1 2>&1 | FileCheck --check-prefix=CFI %s
|
2015-04-02 08:33:36 +08:00
|
|
|
|
2015-07-09 06:10:34 +08:00
|
|
|
// RUN: %clangxx_cfi -DB32 -o %t2 %s
|
2017-10-02 18:21:26 +08:00
|
|
|
// RUN: %expect_crash %run %t2 2>&1 | FileCheck --check-prefix=CFI %s
|
2015-04-02 08:33:36 +08:00
|
|
|
|
2015-07-09 06:10:34 +08:00
|
|
|
// RUN: %clangxx_cfi -DB64 -o %t3 %s
|
2017-10-02 18:21:26 +08:00
|
|
|
// RUN: %expect_crash %run %t3 2>&1 | FileCheck --check-prefix=CFI %s
|
2015-04-02 08:33:36 +08:00
|
|
|
|
2015-07-09 06:10:34 +08:00
|
|
|
// RUN: %clangxx_cfi -DBM -o %t4 %s
|
2017-10-02 18:21:26 +08:00
|
|
|
// RUN: %expect_crash %run %t4 2>&1 | FileCheck --check-prefix=CFI %s
|
2015-04-02 08:33:36 +08:00
|
|
|
|
2015-07-09 06:10:34 +08:00
|
|
|
// RUN: %clangxx -o %t5 %s
|
2017-10-02 18:21:26 +08:00
|
|
|
// RUN: %run %t5 2>&1 | FileCheck --check-prefix=NCFI %s
|
2015-04-02 08:33:36 +08:00
|
|
|
|
2015-07-09 06:10:34 +08:00
|
|
|
// RUN: %clangxx_cfi_diag -o %t6 %s
|
2017-10-02 18:21:26 +08:00
|
|
|
// RUN: %run %t6 2>&1 | FileCheck --check-prefix=CFI-DIAG %s
|
2015-06-19 09:52:55 +08:00
|
|
|
|
2015-04-02 08:33:36 +08:00
|
|
|
// Tests that the CFI mechanism crashes the program when making a non-virtual
|
|
|
|
// call to an object of the wrong class, by casting a pointer to such an object
|
|
|
|
// and attempting to make a call through it.
|
|
|
|
|
2015-06-26 02:45:30 +08:00
|
|
|
// REQUIRES: cxxabi
|
|
|
|
|
2015-04-02 08:33:36 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
struct A {
|
2015-06-19 09:52:55 +08:00
|
|
|
virtual void v();
|
2015-04-02 08:33:36 +08:00
|
|
|
};
|
|
|
|
|
2015-06-19 09:52:55 +08:00
|
|
|
void A::v() {}
|
2015-04-02 08:33:36 +08:00
|
|
|
|
|
|
|
struct B {
|
|
|
|
void f();
|
|
|
|
virtual void g();
|
|
|
|
};
|
|
|
|
|
|
|
|
void B::f() {}
|
|
|
|
void B::g() {}
|
|
|
|
|
|
|
|
int main() {
|
2015-07-30 02:12:45 +08:00
|
|
|
create_derivers<B>();
|
2015-04-02 08:33:36 +08:00
|
|
|
|
|
|
|
A *a = new A;
|
|
|
|
break_optimization(a);
|
|
|
|
|
|
|
|
// CFI: 1
|
|
|
|
// NCFI: 1
|
|
|
|
fprintf(stderr, "1\n");
|
|
|
|
|
2015-06-19 09:52:55 +08:00
|
|
|
// CFI-DIAG: runtime error: control flow integrity check for type 'B' failed during non-virtual call
|
2015-07-09 06:10:34 +08:00
|
|
|
// CFI-DIAG-NEXT: note: vtable is of type '{{(struct )?}}A'
|
2015-04-02 08:33:36 +08:00
|
|
|
((B *)a)->f(); // UB here
|
|
|
|
|
2015-07-09 06:10:34 +08:00
|
|
|
// CFI-NOT: {{^2$}}
|
|
|
|
// NCFI: {{^2$}}
|
2015-04-02 08:33:36 +08:00
|
|
|
fprintf(stderr, "2\n");
|
|
|
|
}
|