// Regression tests for the 2026-10-25 freeze: an uncaught /proc read exception // aborted the daemon, and its core dump deadlocked with the exec guard. #include "strawberry/crash_safety.hpp" #include "strawberry/proc_read.hpp" #include "strawberry/swallowed.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #if defined(__linux__) #include #else #include #endif #include #include namespace fs = std::filesystem; namespace { void test_read_small_file() { std::cout << "Test read_small_file never throws:" << std::endl; const auto path = fs::temp_directory_path() / ("sb-read-" + std::to_string(::getpid())); std::ofstream(path) << "hello world"; assert(*strawberry::read_small_file(path.string()) == "hello world"); assert(*strawberry::read_small_file(path.string(), 5) == "hello"); // bounded fs::remove(path); assert(!strawberry::read_small_file(path.string())); // missing assert(strawberry::read_small_file_or_empty("/nonexistent/x").empty()); // A process that is gone: the original crash read /proc//... of an // exiting process. Every variant must return nothing, not throw. const pid_t child = ::fork(); if (child == 0) { ::_exit(0); } ::waitpid(child, nullptr, 0); const auto dir = "/proc/" + std::to_string(child); assert(!strawberry::read_small_file(dir + "/cmdline")); assert(!strawberry::read_small_file(dir + "/stat")); // Racing reads against processes that exit while being read. for (int i = 0; i < 200; ++i) { const pid_t short_lived = ::fork(); if (short_lived == 0) { ::_exit(0); } (void) strawberry::read_small_file("/proc/" + std::to_string(short_lived) + "/cmdline"); (void) strawberry::read_small_file("/proc/" + std::to_string(short_lived) + "/status"); ::waitpid(short_lived, nullptr, 0); } std::cout << " PASSED" << std::endl; } void test_crash_releases_permission_fds() { std::cout << "Test a crash releases permission fds and skips the core dump:" << std::endl; int pipe_fds[2]; assert(::pipe(pipe_fds) == 0); const pid_t child = ::fork(); if (child == 0) { ::close(pipe_fds[0]); strawberry::install_crash_safety(); // Non-dumpable: the kernel will not start a core-dump helper. #if defined(__linux__) if (::prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) != 0) { ::_exit(3); } #else rlimit core{}; if (::getrlimit(RLIMIT_CORE, &core) != 0 || core.rlim_cur != 0) { ::_exit(3); } #endif strawberry::register_permission_fd(pipe_fds[1]); // The registered fd is the only writer: EOF in the parent proves the // handler closed it before the process died. std::abort(); } ::close(pipe_fds[1]); // The parent sees EOF as soon as the handler closed the registered fd. pollfd pfd{pipe_fds[0], POLLIN, 0}; assert(::poll(&pfd, 1, 5000) == 1); char byte; assert(::read(pipe_fds[0], &byte, 1) == 0); int status = 0; assert(::waitpid(child, &status, 0) == child); assert(WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT); assert(!WCOREDUMP(status)); // no core dump, so no helper exec to deadlock on ::close(pipe_fds[0]); // std::terminate (an uncaught exception) takes the same path. assert(::pipe(pipe_fds) == 0); const pid_t thrower = ::fork(); if (thrower == 0) { ::close(pipe_fds[0]); strawberry::install_crash_safety(); strawberry::register_permission_fd(pipe_fds[1]); throw std::runtime_error("uncaught, like the /proc read on 2026-10-25"); } ::close(pipe_fds[1]); pfd = {pipe_fds[0], POLLIN, 0}; assert(::poll(&pfd, 1, 5000) == 1); assert(::read(pipe_fds[0], &byte, 1) == 0); assert(::waitpid(thrower, &status, 0) == thrower); assert(WIFSIGNALED(status) && !WCOREDUMP(status)); ::close(pipe_fds[0]); // Register / unregister bookkeeping. assert(::pipe(pipe_fds) == 0); strawberry::register_permission_fd(pipe_fds[0]); strawberry::unregister_permission_fd(pipe_fds[0]); strawberry::release_permission_fds_for_crash(); assert(::fcntl(pipe_fds[0], F_GETFD) != -1); // unregistered: still open ::close(pipe_fds[0]); ::close(pipe_fds[1]); std::cout << " PASSED" << std::endl; } } // namespace // An exception that cannot be handled is counted, never silently dropped. void test_swallowed_exceptions_are_counted() { std::cout << "Test swallowed exceptions are counted:" << std::endl; const auto before = strawberry::swallowed_exceptions().load(); for (int i = 0; i < 3; ++i) { try { throw std::runtime_error("x"); } catch (...) { STRAWBERRY_NOTE_SWALLOWED(); } } assert(strawberry::swallowed_exceptions().load() == before + 3); std::cout << " PASSED" << std::endl; } int main() { std::cout << "Running crash-safety tests..." << std::endl << std::endl; test_swallowed_exceptions_are_counted(); test_read_small_file(); test_crash_releases_permission_fds(); std::cout << std::endl << "All tests passed!" << std::endl; return 0; }