18efc3ccaSDan Liew // RUN: %clangxx %s -g -DSHARED_LIB -shared -o %t_shared_lib.dylib
28efc3ccaSDan Liew // RUN: %clangxx %s -g -USHARED_LIB -o %t_loader
38efc3ccaSDan Liew // RUN: %env_tool_opts=verbosity=3 %run %t_loader %t_shared_lib.dylib > %t_loader_output.txt 2>&1
48efc3ccaSDan Liew // RUN: FileCheck -input-file=%t_loader_output.txt %s
58efc3ccaSDan Liew // RUN: FileCheck -check-prefix=CHECK-STACKTRACE -input-file=%t_loader_output.txt %s
6*c860262bSDan Liew // rdar://problem/61793759 and rdar://problem/62126022.
7*c860262bSDan Liew // UNSUPPORTED: lsan
88efc3ccaSDan Liew 
98efc3ccaSDan Liew #include <stdio.h>
108efc3ccaSDan Liew 
118efc3ccaSDan Liew #ifdef SHARED_LIB
128efc3ccaSDan Liew #include <sanitizer/common_interface_defs.h>
138efc3ccaSDan Liew 
PrintStack()148efc3ccaSDan Liew extern "C" void PrintStack() {
158efc3ccaSDan Liew   fprintf(stderr, "Calling __sanitizer_print_stack_trace\n");
168efc3ccaSDan Liew   // CHECK-STACKTRACE: #0{{( *0x.* *in *)?}}  __sanitizer_print_stack_trace
178efc3ccaSDan Liew   // CHECK-STACKTRACE: #1{{( *0x.* *in *)?}} PrintStack {{.*}}print-stack-trace-in-code-loaded-after-fork.cpp:[[@LINE+1]]
188efc3ccaSDan Liew   __sanitizer_print_stack_trace();
198efc3ccaSDan Liew }
208efc3ccaSDan Liew #else
218efc3ccaSDan Liew #include <assert.h>
228efc3ccaSDan Liew #include <dlfcn.h>
238efc3ccaSDan Liew #include <stdlib.h>
248efc3ccaSDan Liew #include <sys/wait.h>
258efc3ccaSDan Liew #include <unistd.h>
268efc3ccaSDan Liew 
278efc3ccaSDan Liew typedef void (*PrintStackFnPtrTy)(void);
288efc3ccaSDan Liew 
main(int argc,char ** argv)298efc3ccaSDan Liew int main(int argc, char **argv) {
308efc3ccaSDan Liew   assert(argc == 2);
318efc3ccaSDan Liew   pid_t pid = fork();
328efc3ccaSDan Liew   if (pid != 0) {
338efc3ccaSDan Liew     // Parent
348efc3ccaSDan Liew     pid_t parent_pid = getpid();
358efc3ccaSDan Liew     fprintf(stderr, "parent: %d\n", parent_pid);
368efc3ccaSDan Liew     int status = 0;
378efc3ccaSDan Liew     pid_t child = waitpid(pid, &status, /*options=*/0);
388efc3ccaSDan Liew     assert(pid == child);
398efc3ccaSDan Liew     bool clean_exit = WIFEXITED(status) && WEXITSTATUS(status) == 0;
408efc3ccaSDan Liew     return !clean_exit;
418efc3ccaSDan Liew   }
428efc3ccaSDan Liew   // Child.
438efc3ccaSDan Liew   pid = getpid();
448efc3ccaSDan Liew   // CHECK: child: [[CHILD_PID:[0-9]+]]
458efc3ccaSDan Liew   fprintf(stderr, "child: %d\n", pid);
468efc3ccaSDan Liew   // We load new code into the child process that isn't loaded into the parent.
478efc3ccaSDan Liew   // When we symbolize in `PrintStack` if the symbolizer is told to symbolize
488efc3ccaSDan Liew   // the parent (an old bug) rather than the child then symbolization will
498efc3ccaSDan Liew   // fail.
508efc3ccaSDan Liew   const char *library_to_load = argv[1];
518efc3ccaSDan Liew   void *handle = dlopen(library_to_load, RTLD_NOW | RTLD_LOCAL);
528efc3ccaSDan Liew   assert(handle);
538efc3ccaSDan Liew   PrintStackFnPtrTy PrintStackFnPtr = (PrintStackFnPtrTy)dlsym(handle, "PrintStack");
548efc3ccaSDan Liew   assert(PrintStackFnPtr);
558efc3ccaSDan Liew   // Check that the symbolizer is told examine the child process.
568efc3ccaSDan Liew   // CHECK: Launching Symbolizer process: {{.+}}atos -p [[CHILD_PID]]
578efc3ccaSDan Liew   // CHECK-STACKTRACE: #2{{( *0x.* *in *)?}} main {{.*}}print-stack-trace-in-code-loaded-after-fork.cpp:[[@LINE+1]]
588efc3ccaSDan Liew   PrintStackFnPtr();
598efc3ccaSDan Liew   return 0;
608efc3ccaSDan Liew }
618efc3ccaSDan Liew 
628efc3ccaSDan Liew #endif
63