blob: 1772a25d003df8ce7050739be578b7ba7caf8f72 [file] [log] [blame]
David Benjamin6f415952024-12-10 21:28:37 -05001/* Copyright 2020 The BoringSSL Authors
David Benjaminb1086cd2019-02-01 00:45:23 +00002 *
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 Beckee79a932024-05-31 21:37:00 +000017#include "../bcm_support.h"
Bob Beckbfa83692023-05-17 17:43:35 -060018
Adam Langleycccfb9b2020-05-04 08:08:39 -070019// TSAN cannot cope with this test and complains that "starting new threads
20// after multi-threaded fork is not supported".
David Benjamina96e7372023-10-31 11:32:12 -040021#if defined(OPENSSL_FORK_DETECTION) && !defined(OPENSSL_TSAN) && \
22 !defined(OPENSSL_IOS)
David Benjaminb1086cd2019-02-01 00:45:23 +000023#include <errno.h>
24#include <inttypes.h>
25#include <stdio.h>
26#include <string.h>
David Benjaminc4b17382023-10-26 14:15:34 -040027#include <sys/wait.h>
David Benjaminb1086cd2019-02-01 00:45:23 +000028#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 Benjaminb1086cd2019-02-01 00:45:23 +000039
40static 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 Beckbfa83692023-05-17 17:43:35 -060052static void CheckGenerationAtLeastInChild(const char *name,
53 uint64_t minimum_expected) {
David Benjaminb1086cd2019-02-01 00:45:23 +000054 uint64_t generation = CRYPTO_get_fork_generation();
Bob Beckbfa83692023-05-17 17:43:35 -060055 if (generation < minimum_expected) {
David Benjaminb1086cd2019-02-01 00:45:23 +000056 fprintf(stderr, "%s generation (#1) was %" PRIu64 ", wanted %" PRIu64 ".\n",
Bob Beckbfa83692023-05-17 17:43:35 -060057 name, generation, minimum_expected);
David Benjaminb1086cd2019-02-01 00:45:23 +000058 _exit(1);
59 }
60
61 // The generation should be stable.
Bob Beckbfa83692023-05-17 17:43:35 -060062 uint64_t new_generation = CRYPTO_get_fork_generation();
63 if (new_generation != generation) {
David Benjaminb1086cd2019-02-01 00:45:23 +000064 fprintf(stderr, "%s generation (#2) was %" PRIu64 ", wanted %" PRIu64 ".\n",
Bob Beckbfa83692023-05-17 17:43:35 -060065 name, new_generation, generation);
David Benjaminb1086cd2019-02-01 00:45:23 +000066 _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.
72static 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
100TEST(ForkDetect, Test) {
Bob Beckbfa83692023-05-17 17:43:35 -0600101 uint64_t start = CRYPTO_get_fork_generation();
David Benjaminb1086cd2019-02-01 00:45:23 +0000102 if (start == 0) {
Bob Beckbfa83692023-05-17 17:43:35 -0600103 GTEST_SKIP() << "Fork detection not supported. Skipping test.\n";
David Benjaminb1086cd2019-02-01 00:45:23 +0000104 }
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 Beckbfa83692023-05-17 17:43:35 -0600116 ForkInChild(
117 [&] { CheckGenerationAtLeastInChild("Grandchild", start + 1); });
David Benjaminb1086cd2019-02-01 00:45:23 +0000118 }
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 Benjaminb1086cd2019-02-01 00:45:23 +0000122
Bob Beckbfa83692023-05-17 17:43:35 -0600123 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 Benjaminb1086cd2019-02-01 00:45:23 +0000127 // Forked grandchildren will now observe |start| + 2.
128 for (int i = 0; i < 2; i++) {
Bob Beckbfa83692023-05-17 17:43:35 -0600129 ForkInChild([&] {
130 CheckGenerationAtLeastInChild("Grandchild", child_generation + 1);
131 });
David Benjaminb1086cd2019-02-01 00:45:23 +0000132 }
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 Beckbfa83692023-05-17 17:43:35 -0600142 t = std::thread([&] {
143 CheckGenerationAtLeastInChild("Grandchild thread",
144 child_generation + 1);
145 });
David Benjaminb1086cd2019-02-01 00:45:23 +0000146 }
147 for (auto &t : threads) {
148 t.join();
149 }
150 }
151 });
152#endif // OPENSSL_THREADS
153
Bob Beckbfa83692023-05-17 17:43:35 -0600154 // 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 Benjaminb1086cd2019-02-01 00:45:23 +0000163 _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 Benjamina96e7372023-10-31 11:32:12 -0400177#endif // OPENSSL_FORK_DETECTION && !OPENSSL_TSAN && !OPENSSL_IOS