The tree now compiles on macOS. Each Linux-only call gets a Darwin branch, and the Linux branch stays as it was: - io::fdatasync uses fcntl(F_FULLFSYNC): fsync alone only reaches the drive cache on APFS, and the store's crash atomicity relies on it. - Control socket: FD_CLOEXEC set after socket/accept/recvmsg (no SOCK_CLOEXEC, accept4 or MSG_CMSG_CLOEXEC), SO_NOSIGPIPE, peer credentials from LOCAL_PEERCRED + LOCAL_PEERPID, getgrouplist with int groups and a growing buffer. launchd has no RuntimeDirectory, so the socket directory is created (0750, access group) when missing. Default socket path is /var/run/strawberry/control.sock. - CRC32C detects ARMv8 CRC with hw.optional.armv8_crc32 (no auxv). - stat mtime via st_mtimespec; the BPF shared header uses stdint typedefs off Linux. - Background threads join the PRIO_DARWIN_BG band (lower CPU and I/O); crash safety zeroes RLIMIT_CORE in place of PR_SET_DUMPABLE. - The desktop app drops Qt DBus off Linux and shows notifications as tray messages (app/notifier_tray.cpp). - make setup passes Homebrew's keg-only libarchive to meson. Tests: behavior_live (Linux ptrace API) builds on Linux only, the control-server fd count reads /dev/fd, the crash test checks the core limit on macOS, and the runtime pipeline test no longer references a YARA-only helper when YARA is absent (that also broke Linux builds without libyara). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
155 lines
5.3 KiB
C++
155 lines
5.3 KiB
C++
// 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 <cassert>
|
|
#include <cerrno>
|
|
#include <chrono>
|
|
#include <csignal>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
#include <fcntl.h>
|
|
#include <poll.h>
|
|
#if defined(__linux__)
|
|
#include <sys/prctl.h>
|
|
#else
|
|
#include <sys/resource.h>
|
|
#endif
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
|
|
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/<pid>/... 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;
|
|
}
|