David Benjamin | 6f41595 | 2024-12-10 21:28:37 -0500 | [diff] [blame] | 1 | /* Copyright 2020 The BoringSSL Authors |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <openssl/base.h> |
| 16 | |
Bob Beck | ee79a93 | 2024-05-31 21:37:00 +0000 | [diff] [blame] | 17 | #include "../bcm_support.h" |
Bob Beck | bfa8369 | 2023-05-17 17:43:35 -0600 | [diff] [blame] | 18 | |
Adam Langley | cccfb9b | 2020-05-04 08:08:39 -0700 | [diff] [blame] | 19 | // TSAN cannot cope with this test and complains that "starting new threads |
| 20 | // after multi-threaded fork is not supported". |
David Benjamin | a96e737 | 2023-10-31 11:32:12 -0400 | [diff] [blame] | 21 | #if defined(OPENSSL_FORK_DETECTION) && !defined(OPENSSL_TSAN) && \ |
| 22 | !defined(OPENSSL_IOS) |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <inttypes.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
David Benjamin | c4b1738 | 2023-10-26 14:15:34 -0400 | [diff] [blame] | 27 | #include <sys/wait.h> |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <functional> |
| 31 | |
| 32 | #if defined(OPENSSL_THREADS) |
| 33 | #include <thread> |
| 34 | #include <vector> |
| 35 | #endif |
| 36 | |
| 37 | #include <gtest/gtest.h> |
| 38 | |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 39 | |
| 40 | static pid_t WaitpidEINTR(pid_t pid, int *out_status, int options) { |
| 41 | pid_t ret; |
| 42 | do { |
| 43 | ret = waitpid(pid, out_status, options); |
| 44 | } while (ret < 0 && errno == EINTR); |
| 45 | |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | // The *InChild functions run inside a child process and must report errors via |
| 50 | // |stderr| and |_exit| rather than GTest. |
| 51 | |
Bob Beck | bfa8369 | 2023-05-17 17:43:35 -0600 | [diff] [blame] | 52 | static void CheckGenerationAtLeastInChild(const char *name, |
| 53 | uint64_t minimum_expected) { |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 54 | uint64_t generation = CRYPTO_get_fork_generation(); |
Bob Beck | bfa8369 | 2023-05-17 17:43:35 -0600 | [diff] [blame] | 55 | if (generation < minimum_expected) { |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 56 | fprintf(stderr, "%s generation (#1) was %" PRIu64 ", wanted %" PRIu64 ".\n", |
Bob Beck | bfa8369 | 2023-05-17 17:43:35 -0600 | [diff] [blame] | 57 | name, generation, minimum_expected); |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 58 | _exit(1); |
| 59 | } |
| 60 | |
| 61 | // The generation should be stable. |
Bob Beck | bfa8369 | 2023-05-17 17:43:35 -0600 | [diff] [blame] | 62 | uint64_t new_generation = CRYPTO_get_fork_generation(); |
| 63 | if (new_generation != generation) { |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 64 | fprintf(stderr, "%s generation (#2) was %" PRIu64 ", wanted %" PRIu64 ".\n", |
Bob Beck | bfa8369 | 2023-05-17 17:43:35 -0600 | [diff] [blame] | 65 | name, new_generation, generation); |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 66 | _exit(1); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // ForkInChild forks a child which runs |f|. If the child exits unsuccessfully, |
| 71 | // this function will also exit unsuccessfully. |
| 72 | static void ForkInChild(std::function<void()> f) { |
| 73 | fflush(stderr); // Avoid duplicating any buffered output. |
| 74 | |
| 75 | const pid_t pid = fork(); |
| 76 | if (pid < 0) { |
| 77 | perror("fork"); |
| 78 | _exit(1); |
| 79 | } else if (pid == 0) { |
| 80 | f(); |
| 81 | _exit(0); |
| 82 | } |
| 83 | |
| 84 | // Wait for the child and pass its exit code up. |
| 85 | int status; |
| 86 | if (WaitpidEINTR(pid, &status, 0) < 0) { |
| 87 | perror("waitpid"); |
| 88 | _exit(1); |
| 89 | } |
| 90 | if (!WIFEXITED(status)) { |
| 91 | fprintf(stderr, "Child did not exit cleanly.\n"); |
| 92 | _exit(1); |
| 93 | } |
| 94 | if (WEXITSTATUS(status) != 0) { |
| 95 | // Pass the failure up. |
| 96 | _exit(WEXITSTATUS(status)); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | TEST(ForkDetect, Test) { |
Bob Beck | bfa8369 | 2023-05-17 17:43:35 -0600 | [diff] [blame] | 101 | uint64_t start = CRYPTO_get_fork_generation(); |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 102 | if (start == 0) { |
Bob Beck | bfa8369 | 2023-05-17 17:43:35 -0600 | [diff] [blame] | 103 | GTEST_SKIP() << "Fork detection not supported. Skipping test.\n"; |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // The fork generation should be stable. |
| 107 | EXPECT_EQ(start, CRYPTO_get_fork_generation()); |
| 108 | |
| 109 | fflush(stderr); |
| 110 | const pid_t child = fork(); |
| 111 | |
| 112 | if (child == 0) { |
| 113 | // Fork grandchildren before observing the fork generation. The |
| 114 | // grandchildren will observe |start| + 1. |
| 115 | for (int i = 0; i < 2; i++) { |
Bob Beck | bfa8369 | 2023-05-17 17:43:35 -0600 | [diff] [blame] | 116 | ForkInChild( |
| 117 | [&] { CheckGenerationAtLeastInChild("Grandchild", start + 1); }); |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // Now the child also observes |start| + 1. This is fine because it has |
| 121 | // already diverged from the grandchild at this point. |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 122 | |
Bob Beck | bfa8369 | 2023-05-17 17:43:35 -0600 | [diff] [blame] | 123 | CheckGenerationAtLeastInChild("Child", start + 1); |
| 124 | |
| 125 | // In the pthread_atfork the value may have changed. |
| 126 | uint64_t child_generation = CRYPTO_get_fork_generation(); |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 127 | // Forked grandchildren will now observe |start| + 2. |
| 128 | for (int i = 0; i < 2; i++) { |
Bob Beck | bfa8369 | 2023-05-17 17:43:35 -0600 | [diff] [blame] | 129 | ForkInChild([&] { |
| 130 | CheckGenerationAtLeastInChild("Grandchild", child_generation + 1); |
| 131 | }); |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | #if defined(OPENSSL_THREADS) |
| 135 | // The fork generation logic itself must be thread-safe. We test this in a |
| 136 | // child process to capture the actual fork detection. This segment is meant |
| 137 | // to be tested in TSan. |
| 138 | ForkInChild([&] { |
| 139 | std::vector<std::thread> threads(4); |
| 140 | for (int i = 0; i < 2; i++) { |
| 141 | for (auto &t : threads) { |
Bob Beck | bfa8369 | 2023-05-17 17:43:35 -0600 | [diff] [blame] | 142 | t = std::thread([&] { |
| 143 | CheckGenerationAtLeastInChild("Grandchild thread", |
| 144 | child_generation + 1); |
| 145 | }); |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 146 | } |
| 147 | for (auto &t : threads) { |
| 148 | t.join(); |
| 149 | } |
| 150 | } |
| 151 | }); |
| 152 | #endif // OPENSSL_THREADS |
| 153 | |
Bob Beck | bfa8369 | 2023-05-17 17:43:35 -0600 | [diff] [blame] | 154 | // The child's observed value should be unchanged. |
| 155 | if (child_generation != CRYPTO_get_fork_generation()) { |
| 156 | fprintf(stderr, |
| 157 | "Child generation (final stable check) was %" PRIu64 |
| 158 | ", wanted %" PRIu64 ".\n", |
| 159 | child_generation, CRYPTO_get_fork_generation()); |
| 160 | _exit(1); |
| 161 | } |
| 162 | |
David Benjamin | b1086cd | 2019-02-01 00:45:23 +0000 | [diff] [blame] | 163 | _exit(0); |
| 164 | } |
| 165 | |
| 166 | ASSERT_GT(child, 0) << "Error in fork: " << strerror(errno); |
| 167 | int status; |
| 168 | ASSERT_EQ(child, WaitpidEINTR(child, &status, 0)) |
| 169 | << "Error in waitpid: " << strerror(errno); |
| 170 | ASSERT_TRUE(WIFEXITED(status)); |
| 171 | EXPECT_EQ(0, WEXITSTATUS(status)) << "Error in child process"; |
| 172 | |
| 173 | // We still observe |start|. |
| 174 | EXPECT_EQ(start, CRYPTO_get_fork_generation()); |
| 175 | } |
| 176 | |
David Benjamin | a96e737 | 2023-10-31 11:32:12 -0400 | [diff] [blame] | 177 | #endif // OPENSSL_FORK_DETECTION && !OPENSSL_TSAN && !OPENSSL_IOS |