File: init_for_dlopen.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (47 lines) | stat: -rw-r--r-- 1,784 bytes parent folder | download | duplicates (21)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// RUN: %clangxx -g -O0 %s -o %t

// Check that trying to dlopen() the ASan dylib fails.
// We explicitly set `abort_on_error=0` because
// - By default the lit config sets this but we don't want this
//   test to implicitly depend on this.
// - It avoids requiring `--crash` to be passed to `not`.
// RUN: APPLE_ASAN_INIT_FOR_DLOPEN=0 %env_asan_opts=abort_on_error=0 not \
// RUN:   %run %t %shared_libasan 2>&1 | \
// RUN:   FileCheck -check-prefix=CHECK-DL-OPEN-FAIL %s
// RUN: env -u APPLE_ASAN_INIT_FOR_DLOPEN %env_asan_opts=abort_on_error=0 not \
// RUN:   %run %t %shared_libasan 2>&1 | \
// RUN:   FileCheck -check-prefix=CHECK-DL-OPEN-FAIL %s

// Check that we can successfully dlopen the ASan dylib when we set the right
// environment variable.
// RUN: env APPLE_ASAN_INIT_FOR_DLOPEN=1 %run %t %shared_libasan 2>&1 | \
// RUN:   FileCheck -check-prefix=CHECK-DL-OPEN-SUCCESS %s

#include <dlfcn.h>
#include <stdio.h>

// CHECK-DL-OPEN-FAIL: ERROR: Interceptors are not working
// CHECK-SAME-DL-OPEN-FAIL: Please launch the executable with: DYLD_INSERT_LIBRARIES={{.+}}/libclang_rt.asan_{{.+}}_dynamic.dylib

int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <dylib_path>\n", argv[0]);
    return 1;
  }
  const char *dylib_path = argv[1];
  void *handle = dlopen(dylib_path, RTLD_LAZY);
  if (!handle) {
    fprintf(stderr, "Failed to dlopen: %s\n", dlerror());
    return 1;
  }
  // Make sure we can find a function we expect to be in the dylib.
  void *fn = dlsym(handle, "__sanitizer_mz_size");
  if (!fn) {
    fprintf(stderr, "Failed to get symbol: %s\n", dlerror());
    return 1;
  }
  // TODO(dliew): Actually call a function from the dylib that is safe to call.
  // CHECK-DL-OPEN-SUCCESS: DONE
  printf("DONE\n");
  return 0;
}