| 1 | // Test that thread local data is handled correctly after forking without |
|---|---|
| 2 | // exec(). In this test leak checking is initiated from a non-main thread. |
| 3 | // RUN: %clangxx_lsan %s -o %t |
| 4 | // RUN: %run %t 2>&1 |
| 5 | |
| 6 | #include <assert.h> |
| 7 | #include <pthread.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <sys/wait.h> |
| 11 | #include <unistd.h> |
| 12 | |
| 13 | __thread void *thread_local_var; |
| 14 | |
| 15 | void *exit_thread_func(void *arg) { |
| 16 | exit(status: 0); |
| 17 | } |
| 18 | |
| 19 | void ExitFromThread() { |
| 20 | pthread_t tid; |
| 21 | int res; |
| 22 | res = pthread_create(newthread: &tid, attr: 0, start_routine: exit_thread_func, arg: 0); |
| 23 | assert(res == 0); |
| 24 | pthread_join(th: tid, thread_return: 0); |
| 25 | } |
| 26 | |
| 27 | int main() { |
| 28 | int status = 0; |
| 29 | thread_local_var = malloc(size: 1337); |
| 30 | pid_t pid = fork(); |
| 31 | assert(pid >= 0); |
| 32 | if (pid > 0) { |
| 33 | waitpid(pid: pid, stat_loc: &status, options: 0); |
| 34 | assert(WIFEXITED(status)); |
| 35 | return WEXITSTATUS(status); |
| 36 | } else { |
| 37 | // Spawn a thread and call exit() from there, to check that we track main |
| 38 | // thread's pid correctly even if leak checking is initiated from another |
| 39 | // thread. |
| 40 | ExitFromThread(); |
| 41 | } |
| 42 | return 0; |
| 43 | } |
| 44 |
