diff options
author | Peter Collingbourne <peter@pcc.me.uk> | 2015-03-14 02:42:39 +0000 |
---|---|---|
committer | Peter Collingbourne <peter@pcc.me.uk> | 2015-03-14 02:42:39 +0000 |
commit | 678e78ca950ec47f77d14aae9cc92883e31e65ae (patch) | |
tree | 8179c9efbc030a811ccdd62ab762e702eb5100c3 | |
parent | d2926c91d5135a95912fdc142d133b647668e918 (diff) | |
download | llvm-678e78ca950ec47f77d14aae9cc92883e31e65ae.zip llvm-678e78ca950ec47f77d14aae9cc92883e31e65ae.tar.gz llvm-678e78ca950ec47f77d14aae9cc92883e31e65ae.tar.bz2 |
CFI: Add test for bad cast checks.
llvm-svn: 232242
-rw-r--r-- | compiler-rt/test/cfi/bad-cast.cpp | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/compiler-rt/test/cfi/bad-cast.cpp b/compiler-rt/test/cfi/bad-cast.cpp new file mode 100644 index 0000000..39d8a35 --- /dev/null +++ b/compiler-rt/test/cfi/bad-cast.cpp @@ -0,0 +1,136 @@ +// RUN: %clangxx_cfi -o %t %s +// RUN: not --crash %t a 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t b 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t c 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: %t d 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t e 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t f 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: not --crash %t g 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: %t h 2>&1 | FileCheck --check-prefix=PASS %s + +// RUN: %clangxx_cfi -DB32 -o %t %s +// RUN: not --crash %t a 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t b 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t c 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: %t d 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t e 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t f 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: not --crash %t g 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: %t h 2>&1 | FileCheck --check-prefix=PASS %s + +// RUN: %clangxx_cfi -DB64 -o %t %s +// RUN: not --crash %t a 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t b 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t c 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: %t d 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t e 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t f 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: not --crash %t g 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: %t h 2>&1 | FileCheck --check-prefix=PASS %s + +// RUN: %clangxx_cfi -DBM -o %t %s +// RUN: not --crash %t a 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t b 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t c 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: %t d 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t e 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t f 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: not --crash %t g 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: %t h 2>&1 | FileCheck --check-prefix=PASS %s + +// RUN: %clangxx_cfi -fsanitize=cfi-cast-strict -o %t %s +// RUN: not --crash %t a 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t b 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t c 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t d 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t e 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t f 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t g 2>&1 | FileCheck --check-prefix=FAIL %s +// RUN: not --crash %t h 2>&1 | FileCheck --check-prefix=FAIL %s + +// RUN: %clangxx -o %t %s +// RUN: %t a 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t b 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t c 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t d 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t e 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t f 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t g 2>&1 | FileCheck --check-prefix=PASS %s +// RUN: %t h 2>&1 | FileCheck --check-prefix=PASS %s + +// Tests that the CFI enforcement detects bad casts. + +#include <stdio.h> +#include <utility> +#include "utils.h" + +struct A { + virtual void f(); +}; + +void A::f() {} + +struct B : A { + virtual void f(); +}; + +void B::f() {} + +struct C : A { +}; + +int main(int argc, char **argv) { +#ifdef B32 + break_optimization(new Deriver<B, 0>); +#endif + +#ifdef B64 + break_optimization(new Deriver<B, 0>); + break_optimization(new Deriver<B, 1>); +#endif + +#ifdef BM + break_optimization(new Deriver<B, 0>); + break_optimization(new Deriver<B, 1>); + break_optimization(new Deriver<B, 2>); +#endif + + B *b = new B; + break_optimization(b); + + // FAIL: 1 + // PASS: 1 + fprintf(stderr, "1\n"); + + A a; + switch (argv[1][0]) { + case 'a': + static_cast<B *>(&a); // UB + break; + case 'b': + static_cast<B &>(a); // UB + break; + case 'c': + static_cast<B &&>(a); // UB + break; + case 'd': + static_cast<C *>(&a); // UB, strict only + break; + case 'e': + static_cast<C &>(a); // UB, strict only + break; + case 'f': + static_cast<C &&>(a); // UB, strict only + break; + case 'g': + static_cast<B *>(static_cast<void *>(&a)); // Non-UB bad cast + break; + case 'h': + static_cast<C *>(static_cast<void *>(&a)); // Non-UB bad cast, strict only + break; + } + + // FAIL-NOT: 2 + // PASS: 2 + fprintf(stderr, "2\n"); +} |