Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / cfi / anon-namespace.cpp
blob2a7ed9c0ac5e72fa2b9abad5755c5d3cd6c528e3
1 // RUN: %clangxx_cfi -c -DTU1 -o %t1.o %s
2 // RUN: %clangxx_cfi -c -DTU2 -o %t2.o %S/../cfi/anon-namespace.cpp
3 // RUN: %clangxx_cfi -o %t1 %t1.o %t2.o
4 // RUN: %expect_crash %run %t1 2>&1 | FileCheck --check-prefix=CFI %s
6 // RUN: %clangxx_cfi -c -DTU1 -DB32 -o %t1.o %s
7 // RUN: %clangxx_cfi -c -DTU2 -DB32 -o %t2.o %S/../cfi/anon-namespace.cpp
8 // RUN: %clangxx_cfi -o %t2 %t1.o %t2.o
9 // RUN: %expect_crash %run %t2 2>&1 | FileCheck --check-prefix=CFI %s
11 // RUN: %clangxx_cfi -c -DTU1 -DB64 -o %t1.o %s
12 // RUN: %clangxx_cfi -c -DTU2 -DB64 -o %t2.o %S/../cfi/anon-namespace.cpp
13 // RUN: %clangxx_cfi -o %t3 %t1.o %t2.o
14 // RUN: %expect_crash %run %t3 2>&1 | FileCheck --check-prefix=CFI %s
16 // RUN: %clangxx_cfi -c -DTU1 -DBM -o %t1.o %s
17 // RUN: %clangxx_cfi -c -DTU2 -DBM -o %t2.o %S/../cfi/anon-namespace.cpp
18 // RUN: %clangxx_cfi -o %t4 %t1.o %t2.o
19 // RUN: %expect_crash %run %t4 2>&1 | FileCheck --check-prefix=CFI %s
21 // RUN: %clangxx -c -DTU1 -o %t1.o %s
22 // RUN: %clangxx -c -DTU2 -o %t2.o %S/../cfi/anon-namespace.cpp
23 // RUN: %clangxx -o %t5 %t1.o %t2.o
24 // RUN: %run %t5 2>&1 | FileCheck --check-prefix=NCFI %s
26 // RUN: %clangxx_cfi_diag -c -DTU1 -o %t1.o %s
27 // RUN: %clangxx_cfi_diag -c -DTU2 -o %t2.o %S/../cfi/anon-namespace.cpp
28 // RUN: %clangxx_cfi_diag -o %t6 %t1.o %t2.o
29 // RUN: %run %t6 2>&1 | FileCheck --check-prefix=CFI-DIAG %s
31 // Tests that the CFI mechanism treats classes in the anonymous namespace in
32 // different translation units as having distinct identities. This is done by
33 // compiling two translation units TU1 and TU2 containing a class named B in an
34 // anonymous namespace, and testing that the program crashes if TU2 attempts to
35 // use a TU1 B as a TU2 B.
37 // FIXME: This test should not require that the paths supplied to the compiler
38 // are different. It currently does so because bitset names have global scope
39 // so we have to mangle the file path into the bitset name.
41 // REQUIRES: cxxabi
43 #include <stdio.h>
44 #include "utils.h"
46 struct A {
47 virtual void f() = 0;
50 namespace {
52 struct B : A {
53 virtual void f() {}
58 A *mkb();
60 #ifdef TU1
62 A *mkb() {
63 return new B;
66 #endif // TU1
68 #ifdef TU2
70 int main() {
71 create_derivers<B>();
73 A *a = mkb();
74 break_optimization(a);
76 // CFI: 1
77 // NCFI: 1
78 fprintf(stderr, "1\n");
80 // CFI-DIAG: runtime error: control flow integrity check for type '(anonymous namespace)::B' failed during base-to-derived cast
81 // CFI-DIAG-NEXT: note: vtable is of type '{{.*}}anonymous namespace{{.*}}::B'
82 // CFI-DIAG: runtime error: control flow integrity check for type '(anonymous namespace)::B' failed during virtual call
83 // CFI-DIAG-NEXT: note: vtable is of type '{{.*}}anonymous namespace{{.*}}::B'
84 ((B *)a)->f(); // UB here
86 // CFI-NOT: {{^2$}}
87 // NCFI: {{^2$}}
88 fprintf(stderr, "2\n");
91 #endif // TU2