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 %t %t1.o %t2.o
4 // RUN: not --crash %t 2>&1 | FileCheck --check-prefix=CFI %s
5 
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 %t %t1.o %t2.o
9 // RUN: not --crash %t 2>&1 | FileCheck --check-prefix=CFI %s
10 
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 %t %t1.o %t2.o
14 // RUN: not --crash %t 2>&1 | FileCheck --check-prefix=CFI %s
15 
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 %t %t1.o %t2.o
19 // RUN: not --crash %t 2>&1 | FileCheck --check-prefix=CFI %s
20 
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 %t %t1.o %t2.o
24 // RUN: %t 2>&1 | FileCheck --check-prefix=NCFI %s
25 
26 // Tests that the CFI mechanism treats classes in the anonymous namespace in
27 // different translation units as having distinct identities. This is done by
28 // compiling two translation units TU1 and TU2 containing a class named B in an
29 // anonymous namespace, and testing that the program crashes if TU2 attempts to
30 // use a TU1 B as a TU2 B.
31 
32 // FIXME: This test should not require that the paths supplied to the compiler
33 // are different. It currently does so because bitset names have global scope
34 // so we have to mangle the file path into the bitset name.
35 
36 #include <stdio.h>
37 #include "utils.h"
38 
39 struct A {
40   virtual void f() = 0;
41 };
42 
43 namespace {
44 
45 struct B : A {
f__anon96b387750111::B46   virtual void f() {}
47 };
48 
49 }
50 
51 A *mkb();
52 
53 #ifdef TU1
54 
mkb()55 A *mkb() {
56   return new B;
57 }
58 
59 #endif  // TU1
60 
61 #ifdef TU2
62 
main()63 int main() {
64 #ifdef B32
65   break_optimization(new Deriver<B, 0>);
66 #endif
67 
68 #ifdef B64
69   break_optimization(new Deriver<B, 0>);
70   break_optimization(new Deriver<B, 1>);
71 #endif
72 
73 #ifdef BM
74   break_optimization(new Deriver<B, 0>);
75   break_optimization(new Deriver<B, 1>);
76   break_optimization(new Deriver<B, 2>);
77 #endif
78 
79   A *a = mkb();
80   break_optimization(a);
81 
82   // CFI: 1
83   // NCFI: 1
84   fprintf(stderr, "1\n");
85 
86   ((B *)a)->f(); // UB here
87 
88   // CFI-NOT: 2
89   // NCFI: 2
90   fprintf(stderr, "2\n");
91 }
92 
93 #endif  // TU2
94