strawberry/fuzz/fuzz_io_faults.cpp
trvon 6628ea9aea
Harden against crashes and I/O faults
Fix for the 2026-10-25 host freeze (an uncaught iostream exception on a
/proc read aborted the daemon; its core dump deadlocked with the exec
guard): non-throwing /proc reads, crash handlers that release fanotify
permission fds, guarded thread entries, from_chars parsing.

Adds the io fault-injection seam with property tests, source invariant
checks (R1-R6) with self-test fixtures, libFuzzer targets and seeds
(including I/O-fault fuzzing), the fold changed-files scan scripts and
the agent workflow docs (CLAUDE.md, prompts).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:17:17 -06:00

118 lines
4 KiB
C++

// I/O-failure fuzzing (built with STRAWBERRY_FAULT_INJECTION=1).
//
// The input is both a fault schedule and file contents: the fuzzer decides, per
// I/O call, whether it fails (and with which errno) or returns short, and what
// the /proc-like files and the executable image contain. Any exception that
// escapes is a crash (the class of bug behind the 2026-10-25 freeze).
#include "strawberry/hash_utils.hpp"
#include "strawberry/proc_read.hpp"
#include "strawberry/resource_governor.hpp"
#include "strawberry/sys_io.hpp"
#include "../src/image_file.hpp"
#include <array>
#include <cerrno>
#include <cstdint>
#include <cstdio>
#include <filesystem>
#include <string>
#include <vector>
#include <fcntl.h>
#include <unistd.h>
namespace fs = std::filesystem;
namespace {
class ScheduleFaults final : public strawberry::io::FaultInjector {
public:
ScheduleFaults(const uint8_t* data, size_t size) : data_(data), size_(size) {}
strawberry::io::FaultDecision decide(strawberry::io::Op) noexcept override {
strawberry::io::FaultDecision d;
if (size_ == 0) {
return d;
}
const uint8_t b = data_[pos_++ % size_];
static constexpr std::array<int, 6> errors{ESRCH, EIO, ENOENT, EINTR, EAGAIN, ENOSPC};
if ((b & 0xC0) == 0xC0) {
d.error = errors[b % errors.size()];
} else if ((b & 0xC0) == 0x80) {
d.short_bytes = b & 0x3F;
}
return d;
}
private:
const uint8_t* data_;
size_t size_;
size_t pos_ = 0;
};
void write_raw(const fs::path& path, const std::string& content) {
fs::create_directories(path.parent_path());
if (FILE* f = std::fopen(path.c_str(), "wb")) {
std::fwrite(content.data(), 1, content.size(), f);
std::fclose(f);
}
}
} // namespace
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size < 4) {
return 0;
}
// First quarter: fault schedule. Rest: file contents split by 0xFF bytes.
const size_t schedule_len = size / 4;
const std::string body(reinterpret_cast<const char*>(data) + schedule_len, size - schedule_len);
std::vector<std::string> parts{""};
for (char c : body) {
if (static_cast<unsigned char>(c) == 0xFF && parts.size() < 9) {
parts.emplace_back();
} else {
parts.back().push_back(c);
}
}
parts.resize(9);
static const fs::path root = fs::temp_directory_path() / ("sb-fuzz-io-" + std::to_string(::getpid()));
write_raw(root / "proc" / "stat", parts[0]);
write_raw(root / "proc" / "self" / "stat", parts[1]);
write_raw(root / "proc" / "self" / "status", parts[2]);
write_raw(root / "proc" / "self" / "cgroup", parts[3]);
write_raw(root / "proc" / "pressure" / "cpu", parts[4]);
write_raw(root / "proc" / "meminfo", parts[5]);
write_raw(root / "proc" / "loadavg", parts[6]);
write_raw(root / "image", parts[7]);
write_raw(root / "cg" / "memory.max", parts[8]);
ScheduleFaults faults(data, schedule_len);
strawberry::io::ScopedInjector scope(&faults);
try {
(void) strawberry::read_small_file((root / "proc" / "self" / "stat").string(), 4096);
strawberry::SystemSampler sampler((root / "proc").string(), (root / "cg").string());
(void) sampler.sample();
(void) sampler.sample();
const int fd = ::open((root / "image").c_str(), O_RDONLY | O_CLOEXEC);
if (fd >= 0) {
std::vector<unsigned char> bytes;
std::string error;
const bool ok = strawberry::detail::read_fd_bounded(fd, 1u << 20, bytes, error);
if (ok && bytes.size() != parts[7].size()) {
__builtin_trap(); // success must mean the exact file
}
(void) strawberry::HashUtils::sha256_fd(fd);
::close(fd);
}
(void) strawberry::HashUtils::sha256_file((root / "image").string());
(void) strawberry::HashUtils::md5_file((root / "image").string());
} catch (...) {
__builtin_trap(); // nothing may escape under I/O failure
}
return 0;
}