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
11extern "C" {
12void *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
23extern "C" const char *__lsan_default_suppressions() {
24 return "leak:^<unknown module>$";
25}
26# endif
27
28int main(int argc, char **argv) {
29
30 std::string path = std::string(argv[0]) + "-so.so";
31
32 dlerror();
33
34 void *handle = dlopen(file: path.c_str(), RTLD_LAZY);
35 assert(handle != 0);
36 typedef void *(*fn)(unsigned sz);
37 fn my_alloc = (fn)dlsym(handle: handle, name: "my_alloc");
38
39 for (int i = 0; i < 100; ++i)
40 my_alloc(i);
41
42 dlclose(handle: handle);
43 return 0;
44}
45
46#endif // BUILD_SO
47
48// CHECK: Direct leak
49

source code of compiler-rt/test/lsan/TestCases/Linux/dso-unknown.cpp