print-stack-trace-in-code-loaded-after-fork.cpp
2.21 KB
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
// RUN: %clangxx %s -g -DSHARED_LIB -shared -o %t_shared_lib.dylib
// RUN: %clangxx %s -g -USHARED_LIB -o %t_loader
// RUN: %env_tool_opts=verbosity=3 %run %t_loader %t_shared_lib.dylib > %t_loader_output.txt 2>&1
// RUN: FileCheck -input-file=%t_loader_output.txt %s
// RUN: FileCheck -check-prefix=CHECK-STACKTRACE -input-file=%t_loader_output.txt %s
// rdar://problem/61793759 and rdar://problem/62126022.
// UNSUPPORTED: lsan
#include <stdio.h>
#ifdef SHARED_LIB
#include <sanitizer/common_interface_defs.h>
extern "C" void PrintStack() {
fprintf(stderr, "Calling __sanitizer_print_stack_trace\n");
// CHECK-STACKTRACE: #0{{( *0x.* *in *)?}} __sanitizer_print_stack_trace
// CHECK-STACKTRACE: #1{{( *0x.* *in *)?}} PrintStack {{.*}}print-stack-trace-in-code-loaded-after-fork.cpp:[[@LINE+1]]
__sanitizer_print_stack_trace();
}
#else
#include <assert.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
typedef void (*PrintStackFnPtrTy)(void);
int main(int argc, char **argv) {
assert(argc == 2);
pid_t pid = fork();
if (pid != 0) {
// Parent
pid_t parent_pid = getpid();
fprintf(stderr, "parent: %d\n", parent_pid);
int status = 0;
pid_t child = waitpid(pid, &status, /*options=*/0);
assert(pid == child);
bool clean_exit = WIFEXITED(status) && WEXITSTATUS(status) == 0;
return !clean_exit;
}
// Child.
pid = getpid();
// CHECK: child: [[CHILD_PID:[0-9]+]]
fprintf(stderr, "child: %d\n", pid);
// We load new code into the child process that isn't loaded into the parent.
// When we symbolize in `PrintStack` if the symbolizer is told to symbolize
// the parent (an old bug) rather than the child then symbolization will
// fail.
const char *library_to_load = argv[1];
void *handle = dlopen(library_to_load, RTLD_NOW | RTLD_LOCAL);
assert(handle);
PrintStackFnPtrTy PrintStackFnPtr = (PrintStackFnPtrTy)dlsym(handle, "PrintStack");
assert(PrintStackFnPtr);
// Check that the symbolizer is told examine the child process.
// CHECK: Launching Symbolizer process: {{.+}}atos -p [[CHILD_PID]]
// CHECK-STACKTRACE: #2{{( *0x.* *in *)?}} main {{.*}}print-stack-trace-in-code-loaded-after-fork.cpp:[[@LINE+1]]
PrintStackFnPtr();
return 0;
}
#endif