]> git.proxmox.com Git - rustc.git/blob - src/libcompiler_builtins/compiler-rt/test/sanitizer_common/TestCases/get_module_and_offset_for_pc.cc
New upstream version 1.20.0+dfsg1
[rustc.git] / src / libcompiler_builtins / compiler-rt / test / sanitizer_common / TestCases / get_module_and_offset_for_pc.cc
1 // RUN: %clangxx -DSHARED %s -shared -o %T/get_module_and_offset_for_pc.so -fPIC
2 // RUN: %clangxx -DSO_DIR=\"%T\" -O0 %s -ldl -o %t
3 // RUN: %run %t 2>&1 | FileCheck %s
4 // UNSUPPORTED: i386-darwin
5 //
6 // Tests __sanitizer_get_module_and_offset_for_pc.
7
8 #include <assert.h>
9 #include <dlfcn.h>
10 #include <sanitizer/common_interface_defs.h>
11 #include <stdio.h>
12
13 #ifdef SHARED
14 extern "C" {
15 int foo() { return 1; }
16 }
17 #else
18
19 void Test(void *pc, const char *name) {
20 char module_name[1024];
21 void *offset;
22 int ok = __sanitizer_get_module_and_offset_for_pc(
23 pc, module_name, sizeof(module_name), &offset);
24 if (!ok) {
25 printf("NOT FOUND %s: %p\n", name, pc);
26 } else {
27 printf("FOUND %s: %s %p\n", name, module_name, offset);
28 }
29 }
30
31 void TestCallerPc() { Test(__builtin_return_address(0), "callerpc"); }
32
33 void TestDlsym() {
34 void *handle = dlopen(SO_DIR "/get_module_and_offset_for_pc.so", RTLD_LAZY);
35 assert(handle);
36 void *foo = dlsym(handle, "foo");
37 assert(foo);
38 Test(foo, "foo");
39 dlclose(handle);
40 }
41
42 // Call __sanitizer_get_module_and_offset_for_pc lots of times
43 // to make sure it is not too slow.
44 void TestLoop() {
45 void *pc = __builtin_return_address(0);
46 char module_name[1024];
47 void *offset;
48 for (int i = 0; i < 1000000; ++i) {
49 __sanitizer_get_module_and_offset_for_pc(pc, module_name,
50 sizeof(module_name), &offset);
51 }
52 }
53
54 int main() {
55 Test(0, "null");
56 TestCallerPc();
57 TestDlsym();
58 TestLoop();
59 }
60 #endif
61 // CHECK: NOT FOUND null: {{.*}}
62 // CHECK-NEXT: FOUND callerpc: {{.*}}/get_module_and_offset_for_pc.cc.tmp {{.*}}
63 // CHECK-NEXT: FOUND foo: {{.*}}/get_module_and_offset_for_pc.so {{.*}}