summaryrefslogtreecommitdiff
path: root/test/sanitizer_common/TestCases/get_module_and_offset_for_pc.cc
blob: 69ccb7234fabc035f74d4c320ef2a751ca528420 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// RUN: %clangxx -DSHARED %s -shared -o %T/get_module_and_offset_for_pc.so -fPIC
// RUN: %clangxx -DSO_DIR=\"%T\" -O0 %s -ldl -o %t
// RUN: %run %t 2>&1 | FileCheck %s

// UNSUPPORTED: i386-darwin
// XFAIL: android

// Tests __sanitizer_get_module_and_offset_for_pc.

#include <assert.h>
#include <dlfcn.h>
#include <sanitizer/common_interface_defs.h>
#include <stdio.h>

#ifdef SHARED
extern "C" {
int foo() { return 1; }
}
#else

void Test(void *pc, const char *name) {
  char module_name[1024];
  void *offset;
  int ok = __sanitizer_get_module_and_offset_for_pc(
      pc, module_name, sizeof(module_name), &offset);
  if (!ok) {
    printf("NOT FOUND %s: %p\n", name, pc);
  } else {
    printf("FOUND %s: %s %p\n", name, module_name, offset);
  }
}

void TestCallerPc() { Test(__builtin_return_address(0), "callerpc"); }

void TestDlsym() {
  void *handle = dlopen(SO_DIR "/get_module_and_offset_for_pc.so", RTLD_LAZY);
  assert(handle);
  void *foo = dlsym(handle, "foo");
  assert(foo);
  Test(foo, "foo");
  dlclose(handle);
}

// Call __sanitizer_get_module_and_offset_for_pc lots of times
// to make sure it is not too slow.
void TestLoop() {
  void *pc = __builtin_return_address(0);
  char module_name[1024];
  void *offset;
  for (int i = 0; i < 1000000; ++i) {
    __sanitizer_get_module_and_offset_for_pc(pc, module_name,
                                             sizeof(module_name), &offset);
  }
}

int main() {
  Test(0, "null");
  TestCallerPc();
  TestDlsym();
  TestLoop();
}
#endif
// CHECK: NOT FOUND null: {{.*}}
// CHECK-NEXT: FOUND callerpc: {{.*}}/get_module_and_offset_for_pc.cc.tmp {{.*}}
// CHECK-NEXT: FOUND foo: {{.*}}/get_module_and_offset_for_pc.so {{.*}}