|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. This product includes software |
| 3 | +// developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present |
| 4 | +// Datadog, Inc. |
| 5 | + |
| 6 | +// Standalone test (no gtest) to verify TLS availability across fork(). |
| 7 | +// Gtest is avoided because forking inside gtest is fragile. |
| 8 | + |
| 9 | +#include "lib/allocation_tracker.hpp" |
| 10 | +#include "loghandle.hpp" |
| 11 | + |
| 12 | +#include <pthread.h> |
| 13 | +#include <sys/wait.h> |
| 14 | +#include <unistd.h> |
| 15 | + |
| 16 | +using ddprof::AllocationTracker; |
| 17 | + |
| 18 | +#define CHECK_OR_RETURN(cond, ...) \ |
| 19 | + do { \ |
| 20 | + if (!(cond)) { \ |
| 21 | + LG_ERR(__VA_ARGS__); \ |
| 22 | + return 1; \ |
| 23 | + } \ |
| 24 | + } while (0) |
| 25 | + |
| 26 | +#define CHECK_OR_EXIT(cond, ...) \ |
| 27 | + do { \ |
| 28 | + if (!(cond)) { \ |
| 29 | + LG_ERR(__VA_ARGS__); \ |
| 30 | + _exit(1); \ |
| 31 | + } \ |
| 32 | + } while (0) |
| 33 | + |
| 34 | +void *thread_check_null_tls(void *arg) { |
| 35 | + auto *result = static_cast<int *>(arg); |
| 36 | + auto *state = AllocationTracker::get_tl_state(); |
| 37 | + if (state != nullptr) { |
| 38 | + LG_ERR("new thread expected NULL TLS, got %p", static_cast<void *>(state)); |
| 39 | + *result = 1; |
| 40 | + return nullptr; |
| 41 | + } |
| 42 | + *result = 0; |
| 43 | + return nullptr; |
| 44 | +} |
| 45 | + |
| 46 | +int run_child(void *parent_state) { |
| 47 | + // After fork, the __thread buffer is inherited: the child's main thread |
| 48 | + // sees the initialized state at the same virtual address. |
| 49 | + auto *child_inherited = AllocationTracker::get_tl_state(); |
| 50 | + CHECK_OR_EXIT(child_inherited == parent_state, |
| 51 | + "expected inherited TLS %p, got %p", parent_state, |
| 52 | + static_cast<void *>(child_inherited)); |
| 53 | + |
| 54 | + // Re-init to get a fresh state for the child |
| 55 | + auto *child_state = AllocationTracker::init_tl_state(); |
| 56 | + CHECK_OR_EXIT(child_state != nullptr, "init_tl_state() returned NULL"); |
| 57 | + |
| 58 | + auto *retrieved = AllocationTracker::get_tl_state(); |
| 59 | + CHECK_OR_EXIT(retrieved == child_state, "get/init mismatch: %p vs %p", |
| 60 | + static_cast<void *>(retrieved), |
| 61 | + static_cast<void *>(child_state)); |
| 62 | + |
| 63 | + // A new thread in the child must start with NULL TLS (zero-initialized) |
| 64 | + pthread_t thread; |
| 65 | + int thread_result = -1; |
| 66 | + CHECK_OR_EXIT(pthread_create(&thread, nullptr, thread_check_null_tls, |
| 67 | + &thread_result) == 0, |
| 68 | + "pthread_create failed"); |
| 69 | + pthread_join(thread, nullptr); |
| 70 | + CHECK_OR_EXIT(thread_result == 0, "child thread TLS was not NULL"); |
| 71 | + |
| 72 | + _exit(0); |
| 73 | +} |
| 74 | + |
| 75 | +int main() { |
| 76 | + ddprof::LogHandle log_handle(LL_NOTICE); |
| 77 | + LG_NTC("allocation_tracker_fork_test starting"); |
| 78 | + |
| 79 | + // Before any init, main thread's TLS must be zero-initialized by libc, |
| 80 | + // so get_tl_state() should return NULL (initialized == false). |
| 81 | + auto *pre_init = AllocationTracker::get_tl_state(); |
| 82 | + CHECK_OR_RETURN(pre_init == nullptr, |
| 83 | + "main thread TLS not zero-initialized before init (got %p)", |
| 84 | + static_cast<void *>(pre_init)); |
| 85 | + |
| 86 | + // Verify the same zero-initialization contract on a new thread |
| 87 | + { |
| 88 | + pthread_t thread; |
| 89 | + int thread_result = -1; |
| 90 | + CHECK_OR_RETURN(pthread_create(&thread, nullptr, thread_check_null_tls, |
| 91 | + &thread_result) == 0, |
| 92 | + "pthread_create failed (pre-fork thread)"); |
| 93 | + pthread_join(thread, nullptr); |
| 94 | + CHECK_OR_RETURN(thread_result == 0, |
| 95 | + "pre-fork thread TLS was not zero-initialized"); |
| 96 | + } |
| 97 | + |
| 98 | + // Create TLS in parent |
| 99 | + auto *parent_state = AllocationTracker::init_tl_state(); |
| 100 | + CHECK_OR_RETURN(parent_state != nullptr, |
| 101 | + "parent init_tl_state() returned NULL"); |
| 102 | + |
| 103 | + auto *retrieved = AllocationTracker::get_tl_state(); |
| 104 | + CHECK_OR_RETURN(retrieved == parent_state, "parent get/init mismatch"); |
| 105 | + |
| 106 | + fflush(stdout); |
| 107 | + fflush(stderr); |
| 108 | + |
| 109 | + pid_t pid = fork(); |
| 110 | + CHECK_OR_RETURN(pid != -1, "fork failed: %s", strerror(errno)); |
| 111 | + |
| 112 | + if (pid == 0) { |
| 113 | + run_child(parent_state); |
| 114 | + _exit(0); |
| 115 | + } |
| 116 | + |
| 117 | + // Parent: wait for child |
| 118 | + int status; |
| 119 | + waitpid(pid, &status, 0); |
| 120 | + |
| 121 | + if (!WIFEXITED(status)) { |
| 122 | + if (WIFSIGNALED(status)) { |
| 123 | + LG_ERR("child killed by signal %d", WTERMSIG(status)); |
| 124 | + } else { |
| 125 | + LG_ERR("child did not exit normally"); |
| 126 | + } |
| 127 | + return 1; |
| 128 | + } |
| 129 | + |
| 130 | + int exit_code = WEXITSTATUS(status); |
| 131 | + CHECK_OR_RETURN(exit_code == 0, "child exited with code %d", exit_code); |
| 132 | + |
| 133 | + // Parent TLS should be unaffected by the fork |
| 134 | + auto *parent_after = AllocationTracker::get_tl_state(); |
| 135 | + CHECK_OR_RETURN(parent_after == parent_state, |
| 136 | + "parent TLS corrupted after fork (%p vs %p)", |
| 137 | + static_cast<void *>(parent_after), |
| 138 | + static_cast<void *>(parent_state)); |
| 139 | + |
| 140 | + LG_NTC("allocation_tracker_fork_test passed"); |
| 141 | + return 0; |
| 142 | +} |
0 commit comments