xref: /llvm-project/compiler-rt/test/lsan/TestCases/Linux/dso-unknown.cpp (revision 678f88e0e23f68c3e0f3d45c8b62ce4d3f1c10f6)
1 // Build a library with origin tracking and an executable w/o origin tracking.
2 // Test that origin tracking is enabled at runtime.
3 // RUN: %clangxx_lsan -O0 %s -DBUILD_SO -fPIC -shared -o %t-so.so
4 // RUN: %clangxx_lsan -O0 %s -ldl -o %t && not %run %t 2>&1 | FileCheck %s
5 // RUN: %clangxx_lsan -O0 %s -ldl -o %t -DSUPPRESS_LEAK && %run %t
6 
7 #ifdef BUILD_SO
8 
9 #  include <stdlib.h>
10 
11 extern "C" {
my_alloc(unsigned sz)12 void *my_alloc(unsigned sz) { return malloc(sz); }
13 } // extern "C"
14 
15 #else // BUILD_SO
16 
17 #  include <assert.h>
18 #  include <dlfcn.h>
19 #  include <stdlib.h>
20 #  include <string>
21 
22 #  ifdef SUPPRESS_LEAK
__lsan_default_suppressions()23 extern "C" const char *__lsan_default_suppressions() {
24   return "leak:^<unknown module>$";
25 }
26 #  endif
27 
main(int argc,char ** argv)28 int main(int argc, char **argv) {
29 
30   std::string path = std::string(argv[0]) + "-so.so";
31 
32   dlerror();
33 
34   void *handle = dlopen(path.c_str(), RTLD_LAZY);
35   assert(handle != 0);
36   typedef void *(*fn)(unsigned sz);
37   fn my_alloc = (fn)dlsym(handle, "my_alloc");
38 
39   for (int i = 0; i < 100; ++i)
40     my_alloc(i);
41 
42   dlclose(handle);
43   return 0;
44 }
45 
46 #endif // BUILD_SO
47 
48 // CHECK: Direct leak
49