xref: /aosp_15_r20/external/compiler-rt/test/asan/TestCases/Linux/odr-violation.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=316
2*7c3d14c8STreehugger Robot // XFAIL: android
3*7c3d14c8STreehugger Robot // XFAIL: mips64
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // We use fast_unwind_on_malloc=0 to have full unwinding even w/o frame
6*7c3d14c8STreehugger Robot // pointers. This setting is not on by default because it's too expensive.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot // Different size: detect a bug if detect_odr_violation>=1
9*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -DBUILD_SO=1 -fPIC -shared %s -o %t-ODR-SO.so
10*7c3d14c8STreehugger Robot // RUN: %clangxx_asan %s %t-ODR-SO.so -Wl,-R. -o %t-ODR-EXE
11*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=fast_unwind_on_malloc=0:detect_odr_violation=1 not %run %t-ODR-EXE 2>&1 | FileCheck %s
12*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=fast_unwind_on_malloc=0:detect_odr_violation=2 not %run %t-ODR-EXE 2>&1 | FileCheck %s
13*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=fast_unwind_on_malloc=0:detect_odr_violation=0     %run %t-ODR-EXE 2>&1 | FileCheck %s --check-prefix=DISABLED
14*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=fast_unwind_on_malloc=0                        not %run %t-ODR-EXE 2>&1 | FileCheck %s
15*7c3d14c8STreehugger Robot //
16*7c3d14c8STreehugger Robot // Same size: report a bug only if detect_odr_violation>=2.
17*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -DBUILD_SO=1 -fPIC -shared %s -o %t-ODR-SO.so -DSZ=100
18*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=fast_unwind_on_malloc=0:detect_odr_violation=1     %run %t-ODR-EXE 2>&1 | FileCheck %s --check-prefix=DISABLED
19*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=fast_unwind_on_malloc=0:detect_odr_violation=2 not %run %t-ODR-EXE 2>&1 | FileCheck %s
20*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=fast_unwind_on_malloc=0                        not %run %t-ODR-EXE 2>&1 | FileCheck %s
21*7c3d14c8STreehugger Robot // RUN: echo "odr_violation:foo::ZZZ" > %t.supp
22*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=fast_unwind_on_malloc=0:detect_odr_violation=2:suppressions=%t.supp  not %run %t-ODR-EXE 2>&1 | FileCheck %s
23*7c3d14c8STreehugger Robot // RUN: echo "odr_violation:foo::G" > %t.supp
24*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=fast_unwind_on_malloc=0:detect_odr_violation=2:suppressions=%t.supp      %run %t-ODR-EXE 2>&1 | FileCheck %s --check-prefix=DISABLED
25*7c3d14c8STreehugger Robot // RUN: rm -f %t.supp
26*7c3d14c8STreehugger Robot //
27*7c3d14c8STreehugger Robot // Use private aliases for global variables: use indicator symbol to detect ODR violation.
28*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -DBUILD_SO=1 -fPIC -shared -mllvm -asan-use-private-alias %s -o %t-ODR-SO.so -DSZ=100
29*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -mllvm -asan-use-private-alias %s %t-ODR-SO.so -Wl,-R. -o %t-ODR-EXE
30*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=fast_unwind_on_malloc=0                              %run %t-ODR-EXE 2>&1 | FileCheck %s --check-prefix=DISABLED
31*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=fast_unwind_on_malloc=0:use_odr_indicator=false      %run %t-ODR-EXE 2>&1 | FileCheck %s --check-prefix=DISABLED
32*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=fast_unwind_on_malloc=0:use_odr_indicator=true   not %run %t-ODR-EXE 2>&1 | FileCheck %s
33*7c3d14c8STreehugger Robot 
34*7c3d14c8STreehugger Robot // GNU driver doesn't handle .so files properly.
35*7c3d14c8STreehugger Robot // REQUIRES: Clang
36*7c3d14c8STreehugger Robot 
37*7c3d14c8STreehugger Robot #ifndef SZ
38*7c3d14c8STreehugger Robot # define SZ 4
39*7c3d14c8STreehugger Robot #endif
40*7c3d14c8STreehugger Robot 
41*7c3d14c8STreehugger Robot #if BUILD_SO
42*7c3d14c8STreehugger Robot namespace foo { char G[SZ]; }
43*7c3d14c8STreehugger Robot #else
44*7c3d14c8STreehugger Robot #include <stdio.h>
45*7c3d14c8STreehugger Robot namespace foo { char G[100]; }
46*7c3d14c8STreehugger Robot // CHECK: ERROR: AddressSanitizer: odr-violation
47*7c3d14c8STreehugger Robot // CHECK: size=100 'foo::G' {{.*}}odr-violation.cc:[[@LINE-2]]:22
48*7c3d14c8STreehugger Robot // CHECK: size={{4|100}} 'foo::G'
main(int argc,char ** argv)49*7c3d14c8STreehugger Robot int main(int argc, char **argv) {
50*7c3d14c8STreehugger Robot   printf("PASS: %p\n", &foo::G);
51*7c3d14c8STreehugger Robot }
52*7c3d14c8STreehugger Robot #endif
53*7c3d14c8STreehugger Robot 
54*7c3d14c8STreehugger Robot // CHECK: These globals were registered at these points:
55*7c3d14c8STreehugger Robot // CHECK: ODR-EXE
56*7c3d14c8STreehugger Robot // CHECK: ODR-SO
57*7c3d14c8STreehugger Robot // CHECK: SUMMARY: AddressSanitizer: odr-violation: global 'foo::G' at {{.*}}odr-violation.cc
58*7c3d14c8STreehugger Robot // DISABLED: PASS
59