xref: /llvm-project/compiler-rt/test/asan/TestCases/Linux/preinit_test.cpp (revision 673dc3d4a0b0fbb3b9b34ae2ecbfa522627fe582)
1*673dc3d4SNico Weber // RUN: %clangxx -DFUNC=zzzz %s -shared -o %dynamiclib -fPIC
2*673dc3d4SNico Weber // RUN: %clangxx_asan -DFUNC=main %s -o %t %ld_flags_rpath_exe
3*673dc3d4SNico Weber // RUN: %run %t
4*673dc3d4SNico Weber 
5*673dc3d4SNico Weber // GNU driver doesn't handle .so files properly.
6*673dc3d4SNico Weber // REQUIRES: Clang
7*673dc3d4SNico Weber 
8*673dc3d4SNico Weber // This test ensures that we call __asan_init early enough.
9*673dc3d4SNico Weber // We build a shared library w/o asan instrumentation
10*673dc3d4SNico Weber // and the binary with asan instrumentation.
11*673dc3d4SNico Weber // Both files include the same header (emulated by -DFUNC here)
12*673dc3d4SNico Weber // with C++ template magic which runs global initializer at library load time.
13*673dc3d4SNico Weber // The function get() is instrumented with asan, but called
14*673dc3d4SNico Weber // before the usual constructors are run.
15*673dc3d4SNico Weber // So, we must make sure that __asan_init is executed even earlier.
16*673dc3d4SNico Weber //
17*673dc3d4SNico Weber // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56393
18*673dc3d4SNico Weber 
19*673dc3d4SNico Weber struct A {
fooA20*673dc3d4SNico Weber   int foo() const { return 0; }
21*673dc3d4SNico Weber };
get()22*673dc3d4SNico Weber A get () { return A(); }
23*673dc3d4SNico Weber template <class> struct O {
24*673dc3d4SNico Weber   static A const e;
25*673dc3d4SNico Weber };
26*673dc3d4SNico Weber template <class T> A const O <T>::e = get();
FUNC()27*673dc3d4SNico Weber int FUNC() {
28*673dc3d4SNico Weber   return O<int>::e.foo();
29*673dc3d4SNico Weber }
30*673dc3d4SNico Weber 
31