Artifact 1e28680b32c6b3cee18285027d3105a527a11e3d359a9ee5740701221d5547af:
- File
src/platform.c
— part of check-in
[3f905be4f2]
at
2026-11-12 21:17:36
on branch trunk
— Every branch of suit's own code is tested, and the gates that keep it so
make cover fails unless every function, line, region and branch in src/ is run by a test. Every system call goes through a wrapper in platform.c that a test build can make fail, so failed-call branches are tested too. The release binary carries none of that code, and a test checks it.
Also: ensure() for invariants, a wider warning set as errors, the clang static analyzer, a second fuzz target for the stamp reader, a replay binary for saved fuzz inputs, a release build that the speed budget now measures, and make ci for the forge's Linux runner: the suite under gcc's sanitizers, then under a pinned Fil-C.
210 unit checks and 214 shell cases. The no-change run measured 0.56 to 0.60 ms over an empty program with the release build.
The Fil-C run and the Linux build have not been run yet. They run first on the forge's runner.
Decisions recorded: the wardrobe is two repositories, libfossil is used through its C API with suit's own file writer, and where each gate runs. (user: dmestas, size: 9048) [annotate] [blame] [check-ins using]
#include "platform.h" #include <errno.h> #include <fcntl.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> #include <time.h> #include <unistd.h> #include <spawn.h> #ifdef __APPLE__ #include <mach-o/dyld.h> #endif extern char **environ; // ---- output --------------------------------------------------------------- bool sys_write_all(int fd, const char *buf, size_t len) { while (len > 0) { ssize_t n = sys_write(fd, buf, len); if (n < 0 && errno == EINTR) continue; if (n < 0) return false; buf += n; len -= (size_t)n; } return true; } void say(int fd, const char *fmt, ...) { char buf[SAY_MAX]; va_list ap; va_start(ap, fmt); int n = vsnprintf(buf, sizeof buf, fmt, ap); va_end(ap); ensure(n >= 0); size_t len = (size_t)n; if (len >= sizeof buf) { len = sizeof buf - 1; buf[len - 1] = '\n'; } (void)sys_write_all(fd, buf, len); } #ifndef SUIT_COVER void ensure_fail(const char *cond, const char *file, int line) { say(STDERR_FILENO, "suit: invariant failed: %s (%s:%d)\n", cond, file, line); abort(); } #endif void out_init(Out *o, int fd) { o->fd = fd; o->err = 0; o->failed = false; o->len = 0; } static void out_flush(Out *o) { if (o->len > 0 && !o->failed && !sys_write_all(o->fd, o->buf, o->len)) { o->failed = true; o->err = errno; } o->len = 0; } void out_str(Out *o, const char *s) { size_t n = strlen(s); while (n > 0) { size_t room = sizeof o->buf - o->len; size_t k = n < room ? n : room; memcpy(o->buf + o->len, s, k); o->len += k; s += k; n -= k; if (o->len == sizeof o->buf) out_flush(o); } } bool out_finish(Out *o) { out_flush(o); errno = o->err; return !o->failed; } // ---- fault injection (test builds only) ----------------------------------- #ifdef SUIT_TEST typedef enum { F_OPEN, F_READ, F_WRITE, F_CLOSE, F_FSYNC, F_STAT, F_FSTATAT, F_RENAME, F_MKDIR, F_UNLINK, F_GETENV, F_SPAWN, F_WAIT4, F_CLOCK, F_SELF, F_COUNT } Fn; static const char *const fn_names[F_COUNT] = { "open", "read", "write", "close", "fsync", "stat", "fstatat", "rename", "mkdir", "unlink", "getenv", "spawn", "wait4", "clock_gettime", "self_path", }; typedef enum { HIT_NONE, HIT_FAIL, HIT_SHORT } Hit; static struct { bool parsed; Fn fn; unsigned long nth; int err; bool is_short; unsigned long calls[F_COUNT]; } fault; static const struct { const char *name; int value; } errno_names[] = { {"EIO", EIO}, {"ENOSPC", ENOSPC}, {"EACCES", EACCES}, {"ENOENT", ENOENT}, {"ENOTDIR", ENOTDIR}, {"EEXIST", EEXIST}, {"EINTR", EINTR}, {"EPERM", EPERM}, {"ENOMEM", ENOMEM}, {"EMFILE", EMFILE}, }; [[noreturn]] static void fault_die(const char *why) { say(STDERR_FILENO, "suit: SUIT_FAULT: %s\n", why); exit(99); } static void fault_kind(const char *kind) { if (strcmp(kind, "short") == 0) { if (fault.fn != F_READ && fault.fn != F_WRITE) fault_die("short applies only to read and write"); fault.is_short = true; return; } for (size_t i = 0; i < sizeof errno_names / sizeof errno_names[0]; i++) { if (strcmp(kind, errno_names[i].name) == 0) { fault.err = errno_names[i].value; return; } } char *end; long v = strtol(kind, &end, 10); if (*end != '\0' || v < 1) fault_die("unknown errno"); fault.err = (int)v; } static void fault_parse(const char *spec) { const char *colon = strchr(spec, ':'); if (colon == nullptr) fault_die("expected NAME:N[:ERRNO|short]"); size_t n = (size_t)(colon - spec); bool found = false; for (int i = 0; i < F_COUNT; i++) { if (strlen(fn_names[i]) == n && memcmp(fn_names[i], spec, n) == 0) { fault.fn = (Fn)i; found = true; } } if (!found) fault_die("unknown call name"); char *end; fault.nth = strtoul(colon + 1, &end, 10); if (end == colon + 1 || fault.nth == 0 || (*end != '\0' && *end != ':')) fault_die("N must be a positive number"); fault.err = EIO; if (*end == ':') fault_kind(end + 1); } void fault_reset(void) { memset(&fault, 0, sizeof fault); } static Hit inject(Fn fn, int *err) { if (!fault.parsed) { fault.parsed = true; const char *spec = getenv("SUIT_FAULT"); if (spec != nullptr) fault_parse(spec); } fault.calls[fn]++; if (fault.nth == 0 || fault.fn != fn || fault.calls[fn] != fault.nth) return HIT_NONE; *err = fault.err; return fault.is_short ? HIT_SHORT : HIT_FAIL; } // Fail the call, returning RET with errno set. #define FAULT(fn, ret) \ do { \ int err_; \ if (inject(fn, &err_) == HIT_FAIL) { \ errno = err_; \ return (ret); \ } \ } while (0) // Fail the call, or shrink N to between 1 and 4 bytes for a short transfer. #define FAULT_IO(fn, n) \ do { \ int err_; \ Hit hit_ = inject(fn, &err_); \ if (hit_ == HIT_FAIL) { \ errno = err_; \ return -1; \ } \ if (hit_ == HIT_SHORT) (n) = (((n) - 1) & 3) + 1; \ } while (0) #else #define FAULT(fn, ret) ((void)0) #define FAULT_IO(fn, n) ((void)0) #endif // ---- wrappers ------------------------------------------------------------- int sys_open(const char *path, int flags, mode_t mode) { FAULT(F_OPEN, -1); return open(path, flags, mode); } ssize_t sys_read(int fd, void *buf, size_t n) { ensure(n > 0); FAULT_IO(F_READ, n); return read(fd, buf, n); } ssize_t sys_write(int fd, const void *buf, size_t n) { ensure(n > 0); FAULT_IO(F_WRITE, n); return write(fd, buf, n); } int sys_close(int fd) { FAULT(F_CLOSE, -1); return close(fd); } int sys_fsync(int fd) { FAULT(F_FSYNC, -1); return fsync(fd); } int sys_stat(const char *path, struct stat *st) { FAULT(F_STAT, -1); return stat(path, st); } int sys_fstatat(int dirfd, const char *name, struct stat *st) { FAULT(F_FSTATAT, -1); return fstatat(dirfd, name, st, 0); } int sys_rename(const char *from, const char *to) { FAULT(F_RENAME, -1); return rename(from, to); } int sys_mkdir(const char *path, mode_t mode) { FAULT(F_MKDIR, -1); return mkdir(path, mode); } int sys_unlink(const char *path) { FAULT(F_UNLINK, -1); return unlink(path); } const char *sys_getenv(const char *name) { FAULT(F_GETENV, nullptr); return getenv(name); } int sys_spawn(pid_t *pid, const char *path, char *const argv[]) { #ifdef SUIT_TEST int err; if (inject(F_SPAWN, &err) == HIT_FAIL) return err; #endif return posix_spawn(pid, path, nullptr, nullptr, argv, environ); } pid_t sys_wait4(pid_t pid, int *status, struct rusage *ru) { FAULT(F_WAIT4, -1); return wait4(pid, status, 0, ru); } bool sys_now_ns(uint64_t *ns) { FAULT(F_CLOCK, false); struct timespec ts; [[maybe_unused]] int rc = clock_gettime(CLOCK_MONOTONIC, &ts); ensure(rc == 0); *ns = (uint64_t)ts.tv_sec * 1000000000u + (uint64_t)ts.tv_nsec; return true; } // ---- platform differences ------------------------------------------------- bool platform_self_path(char *buf, size_t cap) { FAULT(F_SELF, false); #ifdef __APPLE__ uint32_t size = (uint32_t)cap; return _NSGetExecutablePath(buf, &size) == 0; #else ssize_t n = readlink("/proc/self/exe", buf, cap - 1); if (n < 0) return false; buf[n] = '\0'; return (size_t)n < cap - 1; #endif } int64_t platform_mtime_sec(const struct stat *st) { #ifdef __APPLE__ return st->st_mtimespec.tv_sec; #else return st->st_mtim.tv_sec; #endif } int64_t platform_mtime_nsec(const struct stat *st) { #ifdef __APPLE__ return st->st_mtimespec.tv_nsec; #else return st->st_mtim.tv_nsec; #endif } uint64_t platform_peak_resident(const struct rusage *ru) { #ifdef __APPLE__ return (uint64_t)ru->ru_maxrss; // bytes #else return (uint64_t)ru->ru_maxrss * 1024; // kibibytes #endif }