chore(wave6a): reserve P2 error domains, guard IO pipe SIGPIPE, extend ProcessHelper, and add Wave 6A specs

This commit is contained in:
faumaray committed 2026-11-24 10:56:39 +03:00
1 parent 44799602c9
commit 3f67b1ecb7
6 files changed
+470 -2

No files matched your search

@@ -0,0 +1,27 @@
# System Process, Shared Watcher, Durable Filesystem & Temp Factories
Task ID: M-SYSTEM-01..05
Phase: P2 (Wave 6A)
Status: APPROVED
Contracts: C-01, C-02, C-03, C-04, C-05
## 1. Context and baseline
`Void::System` preserves 1.x APIs (`Void::Process::{run,run_streaming,shell,reexec}`, `Void::FileSystem::{FileWatcher,Path,Query,TempFile,TempDir}`, `Void::SystemInfo::CpuSampler`) while fixing legacy bugs (zero-timeout 30s cutoff, full-duplex pipe deadlock, unescalated `SIGTERM` wait hangs, `SIGPIPE` on pipes, TOCTOU temp creation, unsafe `atomic_write`) and adding `System` (`Types`, `OwnedFd`, `Child`, `spawn`, `communicate_blocking`, `replace_file`, bounded `read_file`/`list_directory`, valid `CpuSampler::sample`) plus `Void::SystemAsync` (`ProcessManager`, async `communicate`/`wait`, shared `WatcherService`, `WatchSubscription`).
## 2. Deliverables
- `src/System/module.cmake` (`INTERNAL_DEPS Functional Time LogState`, `EXCLUDE_HEADERS Void/System/AsyncProcess.hpp Void/System/AsyncWatcher.hpp`) & `src/SystemAsync/module.cmake` (`INTERNAL_DEPS System Async IO Time Functional LogState`).
- `include/Void/System/Types.hpp`: `kSystemErrorDomain = 10`, `SystemErrorCode`, `make_system_error`, move-only `OwnedFd`.
- `include/Void/System/Process.hpp` & `src/System/Process.cpp`: legacy compatibility fixes + `SpawnOptions`, `EnvironmentPolicy`, `CaptureLimits`, `OverflowPolicy`, `kMaxSpawnInputBytes = 256 KiB`, move-only `Child`, `spawn`, `communicate_blocking`, `reexec(..., close_extra_fds)`.
- `include/Void/System/{Path,Query,TempFile,TempDir,Metrics}.hpp`: `read_file_bounded`, `list_directory_bounded`, `replace_file` with `ReplaceDurability`/`ReplacePhase`/`ReplaceFaultHook`, atomic `0600`/`0700` `TempFile`/`TempDir` verified by `(dev, ino)` before removal, `CpuSample`.
- `include/Void/System/{AsyncProcess,AsyncWatcher}.hpp` & `src/SystemAsync/{AsyncProcess,AsyncWatcher}.cpp`: `ProcessManager`, async `communicate`/`wait`/`close_and_join`, shared `WatcherService`, `WatchSubscription`.
- `tests/helpers/ProcessHelper.cpp`, `tests/standalone/System/{ProcessStream,SystemHardening}.cpp`, `tests/standalone/SystemAsync/SystemAsync.cpp`, `tests/package/{SystemOnly,SystemAsyncOnly}/*`.
## 3. Edge cases
- Between `fork()` and `exec*()`, strictly async-signal-safe operations only; `exec_failed` preserves `errno` via `CLOEXEC` error pipe and never collides with child exit `127`.
- `replace_file` cleans only owned temp files before rename and reports `visible_on_disk = true, durability_unknown = true` if post-rename directory `fsync` fails.
- `WatcherService::close_and_join()` drains admitted callbacks and rejects self-join.
## 4. Verification commands
```sh
dev cpp-gcc -c bash -lc 'cmake -S . -B /tmp/void-w6a-system -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DVOID_ENABLED_MODULES="System;Testing" -DVOID_WARNINGS_AS_ERRORS=ON && cmake --build /tmp/void-w6a-system && /tmp/void-w6a-system/tests/VoidTests --filter "Process*|SystemHardening*|Path*|Query*|TempDir*|TempFile*|Metrics*|FileWatcher*|SignalWatcher*" && ctest --test-dir /tmp/void-w6a-system --output-on-failure --timeout 120'
dev cpp-gcc -c bash -lc 'cmake -S . -B /tmp/void-w6a-sysasync -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DVOID_ENABLED_MODULES="SystemAsync;Testing" -DVOID_WARNINGS_AS_ERRORS=ON && cmake --build /tmp/void-w6a-sysasync && /tmp/void-w6a-sysasync/tests/VoidTests --filter "SystemAsync*" && ctest --test-dir /tmp/void-w6a-sysasync --output-on-failure --timeout 120'
```
+27
View File
@@ -0,0 +1,27 @@
# IPC Wire Codec, Quota Reservations, Local UDS/Pipe Transport, and Session
Task ID: S-IPC-01..03
Phase: P2 (Wave 6A)
Status: APPROVED
Contracts: C-01, C-02, C-03, C-04, C-05
## 1. Context and baseline
New subsystem `Void::IPC` (`src/IPC/module.cmake`, `INTERNAL_DEPS Core Functional Memory Time Binary Async IO Resilience`) providing V1 `VDIP` 24-byte big-endian framed RPC/messaging over pathname `AF_UNIX` `SOCK_STREAM` and supervised child pipe pairs (`adopt_pipe_pair`).
## 2. Deliverables
- `include/Void/Functional/Error.hpp`: reserve `kIpcErrorDomain = 16` (plus remaining P2 domains `17..21`).
- `include/Void/IPC/{Types,Codec,Transport,Session,IPC}.hpp` & `src/IPC/{Codec,Transport,Session}.cpp`:
- `IpcErrorCode`, `IpcLimits`, `EffectStatus`, `CallFailure`, `PeerCredentials`, `PeerPolicy`, `HelloInfo`.
- V1 24B `VDIP` codec + `BufferQuota`/`QuotaPermit` + incremental `FrameDecoder` validating magic/version/flags/stream/request ID/limits and reserving global (`64 MiB`) + per-session quotas before payload allocation.
- `LocalListener` (0700 owned parent dir, `sun_path < 108`, startup `flock` + failed `connect` stale cleanup, `SOCK_NONBLOCK|SOCK_CLOEXEC`, `fchmod(0600)` before `bind()`, `SO_PEERCRED`), `connect_local`, `adopt_pipe_pair` (`PeerCredentials::available == false`).
- Full-duplex `Session` (`handshake`, `start`, `request`, `send_one_way`, `send_goaway`, `close_and_join` rejecting `self_join`), monotonic request IDs, `TokenBucket` per-peer rate limit, reserved control lane (`8 KiB`/`16` frames), `256` late-response tombstones, advisory `cancel`, single terminal completion, zero auto-replay.
- `tests/standalone/IPC/Ipc.cpp` & `tests/package/IPCOnly/*`.
## 3. Edge cases
- Oversize/corrupt headers fail before any payload allocation or quota charge.
- Saturated data lane still delivers reserved control-lane `cancel`/`overloaded`/`rate_limited` replies without deadlock.
- Peer disconnect faults active requests once with `transport_lost` and `EffectStatus::unknown`.
## 4. Verification commands
```sh
dev cpp-gcc -c bash -lc 'cmake -S . -B /tmp/void-w6a-ipc -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DVOID_ENABLED_MODULES="IPC;Testing" -DVOID_WARNINGS_AS_ERRORS=ON && cmake --build /tmp/void-w6a-ipc && /tmp/void-w6a-ipc/tests/VoidTests --filter "Ipc*" && ctest --test-dir /tmp/void-w6a-ipc --output-on-failure --timeout 120'
```
+6
View File
@@ -65,6 +65,12 @@ namespace Void::Result
inline constexpr std::uint32_t kStacktraceErrorDomain = 13;
inline constexpr std::uint32_t kObservabilityErrorDomain = 14;
inline constexpr std::uint32_t kResilienceErrorDomain = 15;
inline constexpr std::uint32_t kIpcErrorDomain = 16;
inline constexpr std::uint32_t kArchiveErrorDomain = 17;
inline constexpr std::uint32_t kCryptoErrorDomain = 18;
inline constexpr std::uint32_t kSecurityErrorDomain = 19;
inline constexpr std::uint32_t kUpdateErrorDomain = 20;
inline constexpr std::uint32_t kSqliteErrorDomain = 21;
struct ErrorCode {
std::uint32_t domain{kGenericErrorDomain};
+55
View File
@@ -14,6 +14,7 @@
#ifdef __linux__
#include <fcntl.h>
#include <signal.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/socket.h>
@@ -30,6 +31,55 @@ namespace
return std::error_code{err, std::generic_category()};
}
#ifdef __linux__
[[nodiscard]] ssize_t write_pipe_nosigpipe(
int fd, const void* buf, std::size_t len, int& out_errno) noexcept
{
sigset_t pending{};
::sigemptyset(&pending);
::sigpending(&pending);
const bool was_pending = (::sigismember(&pending, SIGPIPE) == 1);
sigset_t block{};
::sigemptyset(&block);
::sigaddset(&block, SIGPIPE);
sigset_t old_mask{};
const int mask_rc = ::pthread_sigmask(SIG_BLOCK, &block, &old_mask);
const bool was_blocked =
(mask_rc == 0) && (::sigismember(&old_mask, SIGPIPE) == 1);
ssize_t n = -1;
out_errno = 0;
for (;;) {
n = ::write(fd, buf, len);
if (n < 0) {
out_errno = errno;
if (out_errno == EINTR) {
continue;
}
}
break;
}
if (n < 0 && out_errno == EPIPE && !was_pending && !was_blocked) {
const struct timespec zero_wait{0, 0};
for (;;) {
if (::sigtimedwait(&block, nullptr, &zero_wait) >= 0) {
break;
}
if (errno != EINTR) {
break;
}
}
}
if (mask_rc == 0 && !was_blocked) {
(void)::pthread_sigmask(SIG_SETMASK, &old_mask, nullptr);
}
return n;
}
#endif
void dispatch_tickets(std::vector<Async::ResumeTicket>& tickets)
{
for (auto& t : tickets) {
@@ -1503,6 +1553,10 @@ private:
continue;
}
if (saved_errno == ENOTSOCK) {
#ifdef __linux__
n = write_pipe_nosigpipe(
fd, op->write_ptr, to_write, saved_errno);
#else
n = ::write(fd, op->write_ptr, to_write);
if (n < 0) {
saved_errno = errno;
@@ -1510,6 +1564,7 @@ private:
continue;
}
}
#endif
}
}
break;
+1 -1
View File
@@ -157,7 +157,7 @@ endif()
# ── Test-support executables & libraries ──────────────────────────────────────
# These are NOT test files; they are helper executables/libs referenced by tests.
if(TARGET VoidTests AND TARGET Void::System)
if(TARGET VoidTests AND (TARGET Void::System OR TARGET Void::IPC))
add_executable(VoidProcessHelper
"${CMAKE_CURRENT_SOURCE_DIR}/helpers/ProcessHelper.cpp")
set_target_properties(VoidProcessHelper PROPERTIES
+354 -1
View File
@@ -1,9 +1,23 @@
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <algorithm>
#include <array>
#include <cerrno>
#include <charconv>
#include <chrono>
#include <csignal>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits>
#include <string>
#include <string_view>
#include <thread>
#include <vector>
extern char** environ;
namespace
{
@@ -25,6 +39,45 @@ int usage_error(std::string_view msg)
return 2;
}
bool write_all_fd(int fd, const void* data, std::size_t len) noexcept
{
const auto* ptr = static_cast<const std::uint8_t*>(data);
std::size_t written = 0;
while (written < len) {
const ssize_t n = ::write(fd, ptr + written, len - written);
if (n < 0) {
if (errno == EINTR) {
continue;
}
return false;
}
written += static_cast<std::size_t>(n);
}
return true;
}
bool read_exact_fd(int fd, void* data, std::size_t len, bool& clean_eof) noexcept
{
auto* ptr = static_cast<std::uint8_t*>(data);
std::size_t got = 0;
clean_eof = false;
while (got < len) {
const ssize_t n = ::read(fd, ptr + got, len - got);
if (n < 0) {
if (errno == EINTR) {
continue;
}
return false;
}
if (n == 0) {
clean_eof = (got == 0);
return false;
}
got += static_cast<std::size_t>(n);
}
return true;
}
int mode_echo(int argc, char** argv)
{
for (int i = 2; i < argc; ++i) {
@@ -126,6 +179,282 @@ int mode_lines(int argc, char** argv)
return 0;
}
int mode_ignore_sigterm(int argc, char** argv)
{
std::int64_t sleep_ms = 5000;
if (argc >= 3 && !parse_i64(argv[2], sleep_ms)) {
return usage_error("ignore-sigterm duration must be an integer");
}
if (sleep_ms < 0) {
sleep_ms = 0;
}
std::signal(SIGTERM, SIG_IGN);
std::cout << "ready\n" << std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds{sleep_ms});
return 0;
}
int mode_simultaneous_io(int argc, char** argv)
{
if (argc != 3) {
return usage_error("simultaneous-io requires <bytes>");
}
std::int64_t total_i64 = 0;
if (!parse_i64(argv[2], total_i64) || total_i64 < 0) {
return usage_error("simultaneous-io <bytes> must be non-negative");
}
const auto total = static_cast<std::size_t>(total_i64);
for (int fd : {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}) {
const int fl = ::fcntl(fd, F_GETFL, 0);
if (fl >= 0) {
(void)::fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}
}
std::string chunk_out(std::min<std::size_t>(4096, std::max<std::size_t>(1, total)), 'o');
std::string chunk_err(std::min<std::size_t>(4096, std::max<std::size_t>(1, total)), 'e');
std::size_t out_sent = 0;
std::size_t err_sent = 0;
std::size_t in_read = 0;
bool in_eof = false;
std::array<char, 4096> in_buf{};
while (!in_eof || out_sent < total || err_sent < total) {
std::array<struct pollfd, 3> pfds{};
nfds_t nfds = 0;
if (!in_eof) {
pfds[nfds++] = {STDIN_FILENO, POLLIN, 0};
}
if (out_sent < total) {
pfds[nfds++] = {STDOUT_FILENO, POLLOUT, 0};
}
if (err_sent < total) {
pfds[nfds++] = {STDERR_FILENO, POLLOUT, 0};
}
const int rc = ::poll(pfds.data(), nfds, -1);
if (rc < 0) {
if (errno == EINTR) {
continue;
}
return 1;
}
for (nfds_t i = 0; i < nfds; ++i) {
if (pfds[i].fd == STDIN_FILENO &&
(pfds[i].revents & (POLLIN | POLLHUP | POLLERR)) != 0) {
for (;;) {
const ssize_t n = ::read(STDIN_FILENO, in_buf.data(), in_buf.size());
if (n > 0) {
in_read += static_cast<std::size_t>(n);
continue;
}
if (n == 0) {
in_eof = true;
break;
}
if (errno == EINTR) {
continue;
}
if (errno == EAGAIN || errno == EWOULDBLOCK) {
break;
}
in_eof = true;
break;
}
} else if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT) != 0) {
const std::size_t rem = std::min(chunk_out.size(), total - out_sent);
const ssize_t n = ::write(STDOUT_FILENO, chunk_out.data(), rem);
if (n > 0) {
out_sent += static_cast<std::size_t>(n);
} else if (n < 0 && errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
return 1;
}
} else if (pfds[i].fd == STDERR_FILENO && (pfds[i].revents & POLLOUT) != 0) {
const std::size_t rem = std::min(chunk_err.size(), total - err_sent);
const ssize_t n = ::write(STDERR_FILENO, chunk_err.data(), rem);
if (n > 0) {
err_sent += static_cast<std::size_t>(n);
} else if (n < 0 && errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
return 1;
}
}
}
}
return (in_read > 0 || total == 0) ? 0 : 0;
}
int mode_flood(int argc, char** argv)
{
if (argc != 4) {
return usage_error("flood requires <stdout_bytes> <stderr_bytes>");
}
std::int64_t out_n = 0;
std::int64_t err_n = 0;
if (!parse_i64(argv[2], out_n) || !parse_i64(argv[3], err_n) || out_n < 0 || err_n < 0) {
return usage_error("flood byte counts must be non-negative");
}
std::array<char, 4096> buf{};
buf.fill('O');
for (std::int64_t rem = out_n; rem > 0;) {
const auto chunk = static_cast<std::size_t>(std::min<std::int64_t>(rem, buf.size()));
if (!write_all_fd(STDOUT_FILENO, buf.data(), chunk)) {
return 1;
}
rem -= static_cast<std::int64_t>(chunk);
}
buf.fill('E');
for (std::int64_t rem = err_n; rem > 0;) {
const auto chunk = static_cast<std::size_t>(std::min<std::int64_t>(rem, buf.size()));
if (!write_all_fd(STDERR_FILENO, buf.data(), chunk)) {
return 1;
}
rem -= static_cast<std::int64_t>(chunk);
}
return 0;
}
int mode_long_line(int argc, char** argv)
{
if (argc != 3) {
return usage_error("long-line requires <bytes>");
}
std::int64_t len = 0;
if (!parse_i64(argv[2], len) || len < 0) {
return usage_error("long-line <bytes> must be non-negative");
}
std::array<char, 4096> buf{};
buf.fill('a');
for (std::int64_t rem = len; rem > 0;) {
const auto chunk = static_cast<std::size_t>(std::min<std::int64_t>(rem, buf.size()));
if (!write_all_fd(STDOUT_FILENO, buf.data(), chunk)) {
return 1;
}
rem -= static_cast<std::int64_t>(chunk);
}
const char nl = '\n';
return write_all_fd(STDOUT_FILENO, &nl, 1) ? 0 : 1;
}
int mode_check_fds()
{
std::vector<int> open_fds;
for (int fd = 3; fd <= 64; ++fd) {
if (::fcntl(fd, F_GETFD) != -1 || errno != EBADF) {
open_fds.push_back(fd);
}
}
if (open_fds.empty()) {
std::cout << "clean\n";
} else {
for (std::size_t i = 0; i < open_fds.size(); ++i) {
if (i > 0) {
std::cout << ' ';
}
std::cout << open_fds[i];
}
std::cout << '\n';
}
std::cout.flush();
return 0;
}
int mode_env_dump()
{
std::vector<std::string> vars;
if (environ != nullptr) {
for (char** p = environ; *p != nullptr; ++p) {
vars.emplace_back(*p);
}
}
std::sort(vars.begin(), vars.end());
for (const auto& entry : vars) {
std::cout << entry << '\n';
}
std::cout.flush();
return 0;
}
int mode_cwd()
{
std::array<char, 4096> buf{};
if (::getcwd(buf.data(), buf.size()) == nullptr) {
return 1;
}
std::cout << buf.data() << '\n' << std::flush;
return 0;
}
std::uint16_t read_u16_be(const std::uint8_t* p) noexcept
{
return static_cast<std::uint16_t>((static_cast<std::uint16_t>(p[0]) << 8) | p[1]);
}
std::uint32_t read_u32_be(const std::uint8_t* p) noexcept
{
return (static_cast<std::uint32_t>(p[0]) << 24) |
(static_cast<std::uint32_t>(p[1]) << 16) |
(static_cast<std::uint32_t>(p[2]) << 8) |
static_cast<std::uint32_t>(p[3]);
}
void write_u16_be(std::uint8_t* p, std::uint16_t v) noexcept
{
p[0] = static_cast<std::uint8_t>((v >> 8) & 0xffu);
p[1] = static_cast<std::uint8_t>(v & 0xffu);
}
void write_u32_be(std::uint8_t* p, std::uint32_t v) noexcept
{
p[0] = static_cast<std::uint8_t>((v >> 24) & 0xffu);
p[1] = static_cast<std::uint8_t>((v >> 16) & 0xffu);
p[2] = static_cast<std::uint8_t>((v >> 8) & 0xffu);
p[3] = static_cast<std::uint8_t>(v & 0xffu);
}
int mode_ipc_pipe_echo()
{
for (;;) {
std::array<std::uint8_t, 24> hdr{};
bool clean_eof = false;
if (!read_exact_fd(STDIN_FILENO, hdr.data(), hdr.size(), clean_eof)) {
return clean_eof ? 0 : 1;
}
if (hdr[0] != 'V' || hdr[1] != 'D' || hdr[2] != 'I' || hdr[3] != 'P') {
return 3;
}
const std::uint16_t version = read_u16_be(hdr.data() + 4);
const std::uint16_t flags_type = read_u16_be(hdr.data() + 6);
const std::uint32_t payload_len = read_u32_be(hdr.data() + 8);
const std::uint32_t stream_id = read_u32_be(hdr.data() + 12);
if (version != 1 || stream_id != 0 || payload_len > 2u * 1024u * 1024u) {
return 4;
}
std::vector<std::uint8_t> payload(payload_len);
if (payload_len > 0) {
bool eof2 = false;
if (!read_exact_fd(STDIN_FILENO, payload.data(), payload_len, eof2)) {
return 5;
}
}
const std::uint8_t frame_type = static_cast<std::uint8_t>(flags_type & 0xffu);
if (frame_type == 7) { // goaway
return 0;
}
if (frame_type == 1 || frame_type == 2) { // hello -> hello, request -> response
std::array<std::uint8_t, 24> out_hdr = hdr;
const std::uint8_t reply_type = (frame_type == 1) ? 1u : 3u;
write_u16_be(out_hdr.data() + 6, reply_type);
write_u32_be(out_hdr.data() + 8, payload_len);
if (!write_all_fd(STDOUT_FILENO, out_hdr.data(), out_hdr.size())) {
return 6;
}
if (payload_len > 0 && !write_all_fd(STDOUT_FILENO, payload.data(), payload.size())) {
return 7;
}
}
}
}
} // namespace
int main(int argc, char** argv)
@@ -153,5 +482,29 @@ int main(int argc, char** argv)
if (mode == "lines") {
return mode_lines(argc, argv);
}
if (mode == "ignore-sigterm") {
return mode_ignore_sigterm(argc, argv);
}
if (mode == "simultaneous-io") {
return mode_simultaneous_io(argc, argv);
}
if (mode == "flood") {
return mode_flood(argc, argv);
}
if (mode == "long-line") {
return mode_long_line(argc, argv);
}
if (mode == "check-fds") {
return mode_check_fds();
}
if (mode == "env-dump") {
return mode_env_dump();
}
if (mode == "cwd") {
return mode_cwd();
}
if (mode == "ipc-pipe-echo") {
return mode_ipc_pipe_echo();
}
return usage_error("unknown mode");
}
}