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