1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#ifdef __FreeBSD__
/* FreeBSD requires POSIX compatibility off for its syscalls (enables __BSD_VISIBLE)
* Without the below line, <sys/wait.h> cannot be imported (it requires __BSD_VISIBLE) */
#undef _POSIX_C_SOURCE
#else
/* For clone() */
#define _GNU_SOURCE
#endif
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <sys/param.h>
#include <sys/wait.h>
#include "s2n_test.h"
#include "utils/s2n_fork_detection.h"
#define NUMBER_OF_FGN_TEST_CASES 4
#define MAX_NUMBER_OF_TEST_THREADS 2
#define FORK_LEVEL_FOR_TESTS 2
/* Before calling s2n_get_fork_generation_number() set the argument to this
* value to avoid any unlucky collisions
*/
#define UNEXPECTED_RETURNED_FGN 0xFF
#define CLONE_TEST_NO 0
#define CLONE_TEST_YES 1
#define CLONE_TEST_DETERMINE_AT_RUNTIME 2
struct fgn_test_case {
const char *test_case_label;
int (*test_case_cb)(struct fgn_test_case *test_case);
int test_case_must_pass_clone_test;
};
static void s2n_verify_child_exit_status(pid_t proc_pid)
{
int status = 0;
#if defined(S2N_CLONE_SUPPORTED)
EXPECT_EQUAL(waitpid(proc_pid, &status, __WALL), proc_pid);
#else
/* __WALL is not relevant when clone() is not supported
* https://man7.org/linux/man-pages/man2/wait.2.html#NOTES
*/
EXPECT_EQUAL(waitpid(proc_pid, &status, 0), proc_pid);
#endif
/* Check that child exited with EXIT_SUCCESS. If not, this indicates
* that an error was encountered in the unit tests executed in that
* child process.
*/
EXPECT_NOT_EQUAL(WIFEXITED(status), 0);
EXPECT_EQUAL(WEXITSTATUS(status), EXIT_SUCCESS);
}
static void *s2n_unit_test_thread_get_fgn(void *expected_fork_generation_number)
{
uint64_t return_fork_generation_number = UNEXPECTED_RETURNED_FGN;
EXPECT_OK(s2n_get_fork_generation_number(&return_fork_generation_number));
EXPECT_EQUAL(return_fork_generation_number, *(uint64_t *) expected_fork_generation_number);
return NULL;
}
static int s2n_unit_test_thread(uint64_t expected_fork_generation_number)
{
pthread_t threads[MAX_NUMBER_OF_TEST_THREADS];
for (size_t thread_index = 0; thread_index < MAX_NUMBER_OF_TEST_THREADS; thread_index++) {
EXPECT_EQUAL(pthread_create(&threads[thread_index], NULL, &s2n_unit_test_thread_get_fgn, (void *) &expected_fork_generation_number), 0);
}
/* Wait for all threads to finish */
for (size_t thread_index = 0; thread_index < MAX_NUMBER_OF_TEST_THREADS; thread_index++) {
pthread_join(threads[thread_index], NULL);
}
return S2N_SUCCESS;
}
static int s2n_unit_test_fork(uint64_t parent_process_fgn, int fork_level)
{
pid_t proc_pid = fork();
EXPECT_TRUE(proc_pid >= 0);
fork_level = fork_level - 1;
if (proc_pid == 0) {
/* In child */
uint64_t return_fork_generation_number = UNEXPECTED_RETURNED_FGN;
EXPECT_OK(s2n_get_fork_generation_number(&return_fork_generation_number));
EXPECT_EQUAL(return_fork_generation_number, parent_process_fgn + 1);
/* Verify stability */
return_fork_generation_number = UNEXPECTED_RETURNED_FGN;
EXPECT_OK(s2n_get_fork_generation_number(&return_fork_generation_number));
EXPECT_EQUAL(return_fork_generation_number, parent_process_fgn + 1);
/* Verify in threads */
EXPECT_EQUAL(s2n_unit_test_thread(return_fork_generation_number), S2N_SUCCESS);
if (fork_level > 0) {
/* Fork again and verify fork generation number */
EXPECT_EQUAL(s2n_unit_test_fork(parent_process_fgn + 1, fork_level), S2N_SUCCESS);
}
/* Exit code EXIT_SUCCESS means that tests in this process finished
* successfully. Any errors would have exited the process with an
* exit code != EXIT_SUCCESS. We verify this in the parent process.
*/
exit(EXIT_SUCCESS);
} else {
s2n_verify_child_exit_status(proc_pid);
/* Verify stability */
uint64_t return_fork_generation_number = UNEXPECTED_RETURNED_FGN;
EXPECT_OK(s2n_get_fork_generation_number(&return_fork_generation_number));
EXPECT_EQUAL(return_fork_generation_number, parent_process_fgn);
}
return S2N_SUCCESS;
}
/* Similar test to unit_test_fork() but verify in threads first */
static int s2n_unit_test_fork_check_threads_first(uint64_t parent_process_fgn)
{
pid_t proc_pid = fork();
EXPECT_TRUE(proc_pid >= 0);
if (proc_pid == 0) {
/* In child. Verify threads first. */
EXPECT_EQUAL(s2n_unit_test_thread(parent_process_fgn + 1), S2N_SUCCESS);
/* Then in the thread spawned when forking */
uint64_t return_fork_generation_number = UNEXPECTED_RETURNED_FGN;
EXPECT_OK(s2n_get_fork_generation_number(&return_fork_generation_number));
EXPECT_EQUAL(return_fork_generation_number, parent_process_fgn + 1);
/* Verify stability */
return_fork_generation_number = UNEXPECTED_RETURNED_FGN;
EXPECT_OK(s2n_get_fork_generation_number(&return_fork_generation_number));
EXPECT_EQUAL(return_fork_generation_number, parent_process_fgn + 1);
/* Exit code EXIT_SUCCESS means that tests in this process finished
* successfully. Any errors would have exited the process with an
* exit code != EXIT_SUCCESS. We verify this in the parent process.
*/
exit(EXIT_SUCCESS);
} else {
s2n_verify_child_exit_status(proc_pid);
/* Verify stability */
uint64_t return_fork_generation_number = UNEXPECTED_RETURNED_FGN;
EXPECT_OK(s2n_get_fork_generation_number(&return_fork_generation_number));
EXPECT_EQUAL(return_fork_generation_number, parent_process_fgn);
}
return S2N_SUCCESS;
}
static int s2n_unit_test_clone_child_process(void *parent_process_fgn)
{
/* In child */
uint64_t local_parent_process_fgn = *(uint64_t *) parent_process_fgn;
uint64_t return_fork_generation_number = UNEXPECTED_RETURNED_FGN;
EXPECT_OK(s2n_get_fork_generation_number(&return_fork_generation_number));
EXPECT_EQUAL(return_fork_generation_number, local_parent_process_fgn + 1);
/* Verify stability */
return_fork_generation_number = UNEXPECTED_RETURNED_FGN;
EXPECT_OK(s2n_get_fork_generation_number(&return_fork_generation_number));
EXPECT_EQUAL(return_fork_generation_number, local_parent_process_fgn + 1);
/* Verify in threads */
EXPECT_EQUAL(s2n_unit_test_thread(return_fork_generation_number), S2N_SUCCESS);
/* This translates to the exit code for this child process */
return EXIT_SUCCESS;
}
#define PROCESS_CHILD_STACK_SIZE (1024 * 1024) /* Suggested by clone() man page... */
static int s2n_unit_test_clone(uint64_t parent_process_fgn)
{
#if defined(S2N_CLONE_SUPPORTED)
/* Verify stability */
uint64_t return_fork_generation_number = UNEXPECTED_RETURNED_FGN;
EXPECT_OK(s2n_get_fork_generation_number(&return_fork_generation_number));
EXPECT_EQUAL(return_fork_generation_number, parent_process_fgn);
/* Use stack memory for this... We don't exit unit_test_clone() before this
* memory has served its purpose.
* Why? Using dynamically allocated memory causes Valgrind to squat on the
* allocated memory when the child process exists.
*/
char process_child_stack[PROCESS_CHILD_STACK_SIZE];
EXPECT_NOT_NULL(process_child_stack);
int proc_pid = clone(s2n_unit_test_clone_child_process, (void *) (process_child_stack + PROCESS_CHILD_STACK_SIZE), 0, (void *) &return_fork_generation_number);
EXPECT_NOT_EQUAL(proc_pid, -1);
s2n_verify_child_exit_status(proc_pid);
/* Verify stability */
return_fork_generation_number = UNEXPECTED_RETURNED_FGN;
EXPECT_OK(s2n_get_fork_generation_number(&return_fork_generation_number));
EXPECT_EQUAL(return_fork_generation_number, parent_process_fgn);
#endif
return S2N_SUCCESS;
}
static int s2n_unit_tests_common(struct fgn_test_case *test_case)
{
uint64_t return_fork_generation_number = 0;
EXPECT_OK(s2n_get_fork_generation_number(&return_fork_generation_number));
EXPECT_EQUAL(return_fork_generation_number, 0);
/* Should be idempotent if no fork event occurred */
return_fork_generation_number = UNEXPECTED_RETURNED_FGN;
EXPECT_OK(s2n_get_fork_generation_number(&return_fork_generation_number));
EXPECT_EQUAL(return_fork_generation_number, 0);
/* Should be idempotent in threaded environment as well */
EXPECT_EQUAL(s2n_unit_test_thread(return_fork_generation_number), S2N_SUCCESS);
/* Cached FGN should increment if a fork event occurs */
EXPECT_EQUAL(s2n_unit_test_fork(return_fork_generation_number, FORK_LEVEL_FOR_TESTS), S2N_SUCCESS);
EXPECT_EQUAL(s2n_unit_test_fork_check_threads_first(return_fork_generation_number), S2N_SUCCESS);
/* Some fork detection mechanisms can also detect forks through clone() */
if (test_case->test_case_must_pass_clone_test == CLONE_TEST_YES) {
EXPECT_EQUAL((s2n_is_madv_wipeonfork_supported() == true) || (s2n_is_map_inherit_zero_supported() == true), true);
EXPECT_EQUAL(s2n_unit_test_clone(return_fork_generation_number), S2N_SUCCESS);
} else if (test_case->test_case_must_pass_clone_test == CLONE_TEST_DETERMINE_AT_RUNTIME) {
if ((s2n_is_madv_wipeonfork_supported() == true) || (s2n_is_map_inherit_zero_supported() == true)) {
EXPECT_EQUAL(s2n_unit_test_clone(return_fork_generation_number), S2N_SUCCESS);
}
}
return S2N_SUCCESS;
}
static int s2n_test_case_default_cb(struct fgn_test_case *test_case)
{
EXPECT_SUCCESS(s2n_init());
EXPECT_EQUAL(s2n_unit_tests_common(test_case), S2N_SUCCESS);
EXPECT_SUCCESS(s2n_cleanup());
return S2N_SUCCESS;
}
static int s2n_test_case_pthread_atfork_cb(struct fgn_test_case *test_case)
{
if (s2n_is_pthread_atfork_supported() == false) {
TEST_DEBUG_PRINT("s2n_fork_generation_number_test.c test case not supported. Skipping.\nTest case: %s\n", test_case->test_case_label);
return S2N_SUCCESS;
}
POSIX_GUARD_RESULT(s2n_ignore_wipeonfork_and_inherit_zero_for_testing());
EXPECT_SUCCESS(s2n_init());
EXPECT_EQUAL(s2n_unit_tests_common(test_case), S2N_SUCCESS);
EXPECT_SUCCESS(s2n_cleanup());
return S2N_SUCCESS;
}
static int s2n_test_case_madv_wipeonfork_cb(struct fgn_test_case *test_case)
{
if (s2n_is_madv_wipeonfork_supported() == false) {
TEST_DEBUG_PRINT("s2n_fork_generation_number_test.c test case not supported. Skipping.\nTest case: %s\n", test_case->test_case_label);
return S2N_SUCCESS;
}
POSIX_GUARD_RESULT(s2n_ignore_pthread_atfork_for_testing());
EXPECT_SUCCESS(s2n_init());
EXPECT_EQUAL(s2n_unit_tests_common(test_case), S2N_SUCCESS);
EXPECT_SUCCESS(s2n_cleanup());
return S2N_SUCCESS;
}
static int s2n_test_case_map_inherit_zero_cb(struct fgn_test_case *test_case)
{
if (s2n_is_map_inherit_zero_supported() == false) {
TEST_DEBUG_PRINT("s2n_fork_generation_number_test.c test case not supported. Skipping.\nTest case: %s\n", test_case->test_case_label);
return S2N_SUCCESS;
}
POSIX_GUARD_RESULT(s2n_ignore_pthread_atfork_for_testing());
EXPECT_SUCCESS(s2n_init());
EXPECT_EQUAL(s2n_unit_tests_common(test_case), S2N_SUCCESS);
EXPECT_SUCCESS(s2n_cleanup());
return S2N_SUCCESS;
}
struct fgn_test_case fgn_test_cases[NUMBER_OF_FGN_TEST_CASES] = {
{ "Default fork detect mechanisms.", s2n_test_case_default_cb, CLONE_TEST_DETERMINE_AT_RUNTIME },
{ "Only pthread_atfork fork detection mechanism.", s2n_test_case_pthread_atfork_cb, CLONE_TEST_NO },
{ "Only madv_wipeonfork fork detection mechanism.", s2n_test_case_madv_wipeonfork_cb, CLONE_TEST_YES },
{ "Only map_inherit_zero fork detection mechanism.", s2n_test_case_map_inherit_zero_cb, CLONE_TEST_YES }
};
int main(int argc, char **argv)
{
BEGIN_TEST_NO_INIT();
EXPECT_TRUE(s2n_array_len(fgn_test_cases) == NUMBER_OF_FGN_TEST_CASES);
/* Test: FreeBSD >= 12.0 should use map_inherit_zero */
#ifdef __FreeBSD__
#ifndef __FreeBSD_version
#error "Unknown FreeBSD version"
#endif
#if __FreeBSD_version >= 1200000
EXPECT_TRUE(s2n_is_map_inherit_zero_supported());
#endif
#endif
/* Create NUMBER_OF_FGN_TEST_CASES number of child processes that run each
* test case.
*
* Fork detection is lazily initialised on first invocation of
* s2n_get_fork_generation_number(). Hence, it is important that childs are
* created before calling into the fork detection code.
*/
pid_t proc_pids[NUMBER_OF_FGN_TEST_CASES] = { 0 };
for (size_t i = 0; i < NUMBER_OF_FGN_TEST_CASES; i++) {
proc_pids[i] = fork();
EXPECT_TRUE(proc_pids[i] >= 0);
if (proc_pids[i] == 0) {
/* In child */
EXPECT_EQUAL(fgn_test_cases[i].test_case_cb(&fgn_test_cases[i]), S2N_SUCCESS);
/* Exit code EXIT_SUCCESS means that tests in this process finished
* successfully. Any errors would have exited the process with an
* exit code != EXIT_SUCCESS. We verify this in the parent process.
* Also prevents child from creating more childs.
*/
exit(EXIT_SUCCESS);
} else {
s2n_verify_child_exit_status(proc_pids[i]);
}
}
END_TEST_NO_INIT();
}
|