xref: /llvm-project/compiler-rt/test/tysan/anon-ns.cpp (revision 641fbf1524338c86c952ebb1ec8d2b497ada3cef)
1 // RUN: %clangxx_tysan -O0 %s -c -o %t.o
2 // RUN: %clangxx_tysan -O0 %s -DPMAIN -c -o %tm.o
3 // RUN: %clangxx_tysan -O0 %t.o %tm.o -o %t
4 // RUN: %run %t >%t.out 2>&1
5 // RUN: FileCheck %s < %t.out
6 
7 #include <iostream>
8 
9 // This test demonstrates that the types from anonymous namespaces are
10 // different in different translation units (while the char* type is the same).
11 
12 namespace {
13 struct X {
14   X(int i, int j) : a(i), b(j) {}
15   int a;
16   int b;
17 };
18 } // namespace
19 
20 #ifdef PMAIN
21 void foo(void *context, int i);
22 char fbyte(void *context);
23 
24 int main() {
25   X x(5, 6);
26   foo((void *)&x, 8);
27   std::cout << "fbyte: " << fbyte((void *)&x) << "\n";
28 }
29 #else
30 void foo(void *context, int i) {
31   X *x = (X *)context;
32   x->b = i;
33   // CHECK: ERROR: TypeSanitizer: type-aliasing-violation
34   // CHECK: WRITE of size 4 at {{.*}} with type int (in (anonymous namespace)::X at offset 4) accesses an existing object of type int (in (anonymous namespace)::X at offset 4)
35   // CHECK: {{#0 0x.* in foo\(void\*, int\) .*anon-ns.cpp:}}[[@LINE-3]]
36 }
37 
38 char fbyte(void *context) { return *(char *)context; }
39 #endif
40 
41 // CHECK-NOT: ERROR: TypeSanitizer: type-aliasing-violation
42