test: add deterministic parser fuzzing
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 4s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 10s

This commit is contained in:
faumaray committed 2026-12-05 08:17:46 +03:00
1 parent 7756bd9f60
commit 52ccd3e001
5 files changed
+94 -1

No files matched your search

+74
View File
@@ -4,7 +4,11 @@
#include <Void/Build/Execution/Scheduler.hpp>
#include <Void/Build/Graph/Graph.hpp>
#include <Void/Build/Platform/FileOps.hpp>
#include <Void/Build/Package/Archive.hpp>
#include <Void/Build/Package/Lockfile.hpp>
#include <Void/Build/Package/Toml.hpp>
#include <Void/Build/Platform/ProcessSupervisor.hpp>
#include <Void/Data/StrictJson.hpp>
#include <Void/Build/Snapshot/Snapshot.hpp>
#include <Void/Build/State/StateStore.hpp>
#include <Void/Build/Toolchain/Driver.hpp>
@@ -15,6 +19,8 @@
#include <Void/Testing/Test.hpp>
#include <chrono>
#include <cstdint>
#include <format>
#include <filesystem>
#include <stop_token>
#include <sys/stat.h>
@@ -495,3 +501,71 @@ void apply(Void::Build::Target t);
std::string::npos);
}
}
// === Fuzzing groundwork: deterministic mutation robustness over the parsers ===
VOID_TEST(CoreEngineTests, fuzz_mutated_inputs_never_crash_parsers) {
Void::FileSystem::TempDir tmp{"vbs_fuzz_"};
std::uint64_t rng = 0x9E3779B97F4A7C15ull;
const auto next = [&]() {
rng ^= rng << 13;
rng ^= rng >> 7;
rng ^= rng << 17;
return rng;
};
const auto mutate = [&](std::string_view seed) {
std::string out{seed};
switch (next() % 4) {
case 0: // flip one bit
if (!out.empty()) {
out[next() % out.size()] ^= static_cast<char>(1u << (next() % 8));
}
break;
case 1: // truncate
if (!out.empty()) {
out.resize(next() % out.size());
}
break;
case 2: // overwrite a run with a byte
if (!out.empty()) {
const std::size_t pos = next() % out.size();
const std::size_t len = 1 + (next() % (out.size() - pos));
out.replace(pos, len, len, static_cast<char>(next() % 256));
}
break;
default: // insert junk
out.insert(next() % (out.size() + 1), std::string(1 + (next() % 8),
static_cast<char>(next() % 256)));
break;
}
return out;
};
const std::string toml_seed =
"[dependencies.demo]\npath = \"vendor/demo\"\nbuilder = \"native\"\n";
const std::string json_seed = "{\"a\":[1,true,\"x\"],\"b\":{\"c\":null}}";
const std::string tar_seed = std::string(1024, '\0');
const std::string lock_seed = Package::serialize_lockfile(Package::LockfileDocument{});
constexpr int kIterations = 200;
for (int i = 0; i < kIterations; ++i) {
const auto toml_text = mutate(toml_seed);
const auto toml_path = tmp / std::format("f{}.toml", i);
write_text_file(toml_path, toml_text);
(void)Package::load_dependencies_toml(toml_path);
(void)Void::Json::Strict::parse(mutate(json_seed));
(void)Package::parse_lockfile(mutate(lock_seed));
const auto tar_path = tmp / std::format("f{}.tar", i);
write_text_file(tar_path, mutate(tar_seed));
(void)Package::extract_safe_tar(tar_path, tmp / std::format("x{}", i));
}
// The unmutated seeds must still parse (guards against a vacuous fuzz).
const auto seed_toml = tmp / "seed.toml";
write_text_file(seed_toml, toml_seed);
Void::Testing::expect(Package::load_dependencies_toml(seed_toml).has_value());
Void::Testing::expect(Void::Json::Strict::parse(json_seed).has_value());
Void::Testing::expect(Package::parse_lockfile(lock_seed).has_value());
}