Files
Build-System/tests/CoreEngineTests.cpp
T
faumaray ab64147f9d
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 3s
Implement VoidBuildSystem v1 (Plans 01-19) with VoidLibrary integration
2026-11-26 20:45:54 +03:00

491 lines
20 KiB
C++

#include <Void/Build.hpp>
#include <Void/Build/BootstrapConfig.hpp>
#include <Void/Build/Config/Frontend.hpp>
#include <Void/Build/Execution/Scheduler.hpp>
#include <Void/Build/Graph/Graph.hpp>
#include <Void/Build/Platform/FileOps.hpp>
#include <Void/Build/Platform/ProcessSupervisor.hpp>
#include <Void/Build/Snapshot/Snapshot.hpp>
#include <Void/Build/State/StateStore.hpp>
#include <Void/Build/Toolchain/Driver.hpp>
#include <Void/Build/Util/Sha256.hpp>
#include <Void/System/Path.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <chrono>
#include <filesystem>
#include <stop_token>
#include <sys/stat.h>
#include <thread>
#include <unistd.h>
using namespace Void::Build;
namespace {
void write_text_file(const std::filesystem::path &path, std::string_view content) {
std::error_code ec;
if (path.has_parent_path()) {
std::filesystem::create_directories(path.parent_path(), ec);
}
auto r = Platform::atomic_write_file(path, content, false);
Void::Testing::expect(r.has_value());
}
} // namespace
Void::Testing::Test sha256_nist_vectors_and_files() {
Void::Testing::expect_eq(
Util::sha256_hex(""),
std::string{"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"});
Void::Testing::expect_eq(
Util::sha256_hex("abc"),
std::string{"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"});
Void::Testing::expect_eq(
Util::sha256_hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"),
std::string{"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"});
Void::FileSystem::TempDir tmp{"vbs_sha_"};
const auto f = tmp / "sample.bin";
write_text_file(f, "abc");
auto fh = Util::sha256_file_hex(f);
Void::Testing::expect(fh.has_value());
if (fh) {
Void::Testing::expect_eq(
*fh, std::string{"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"});
}
}
Void::Testing::Test process_supervisor_exec_failure_and_early_leader_grandchild_kill() {
// 1. Distinguish exec failure from exit code 127.
{
Platform::ProcessRequest req;
req.executable = "/nonexistent/vbs_definitely_missing_binary_xyz";
req.search_path = false;
auto res = Platform::run_supervised(req);
Void::Testing::expect(res.has_value());
if (res) {
Void::Testing::expect(res->exec_failed);
Void::Testing::expect(!res->succeeded());
}
}
// 2. Program explicitly exiting 127 is NOT exec_failed.
{
Platform::ProcessRequest req;
req.executable = "/bin/sh";
req.arguments = {"-c", "exit 127"};
auto res = Platform::run_supervised(req);
Void::Testing::expect(res.has_value());
if (res) {
Void::Testing::expect(!res->exec_failed);
Void::Testing::expect(res->exited);
Void::Testing::expect_eq(res->exit_code, 127);
}
}
// 3. Adversarial VL-001 test: leader spawns stubborn grandchild ignoring SIGTERM and exits
// immediately; timeout escalates to SIGKILL and prevents grandchild from writing sentinel file.
{
Void::FileSystem::TempDir tmp{"vbs_proc_"};
const auto sentinel = tmp / "grandchild_escaped.txt";
Platform::ProcessRequest req;
req.executable = "/bin/sh";
req.arguments = {"-c", "sh -c 'trap \"\" TERM; sleep 1.5; echo escaped > \"" +
sentinel.string() + "\"' >/dev/null 2>&1 & exit 0"};
req.timeout = std::chrono::milliseconds{180};
req.kill_grace = std::chrono::milliseconds{80};
auto res = Platform::run_supervised(req);
Void::Testing::expect(res.has_value());
if (res) {
Void::Testing::expect(res->timed_out);
Void::Testing::expect(!res->succeeded());
}
std::this_thread::sleep_for(std::chrono::milliseconds{300});
Void::Testing::expect(!std::filesystem::exists(sentinel));
}
}
Void::Testing::Test profile_inheritance_and_cycle_rejection() {
Profiles p = default_profiles();
p.inherit("debug-asan", "debug", {"-fno-omit-frame-pointer"});
auto resolved = p.resolve("debug-asan", CompilerFamily::Gcc, "Linux", "x86_64");
Void::Testing::expect_eq(resolved.compile_options.size(), std::size_t{3});
Void::Testing::expect_eq(resolved.compile_options.at(0), std::string{"-O0"});
Void::Testing::expect_eq(resolved.compile_options.at(2),
std::string{"-fno-omit-frame-pointer"});
Profiles cyclic;
cyclic.inherit("a", "b");
cyclic.inherit("b", "a");
Void::Testing::expect_throw<std::invalid_argument>(
[&] { (void)cyclic.resolve("a", CompilerFamily::Gcc, "Linux", "x86_64"); });
Profiles missing_parent;
missing_parent.inherit("child", "ghost");
Void::Testing::expect_throw<std::invalid_argument>(
[&] { (void)missing_parent.resolve("child", CompilerFamily::Gcc, "Linux", "x86_64"); });
}
Void::Testing::Test snapshot_content_mutations_symlinks_shadowing_and_isolation() {
Void::FileSystem::TempDir tmp{"vbs_snap_"};
const auto proj = tmp / "project";
const auto store = tmp / "store";
const auto outside = tmp / "outside_secret.txt";
write_text_file(outside, "TOP_SECRET");
write_text_file(proj / "src" / "main.cpp", "int main() { return 0; }\n");
write_text_file(proj / "inc2" / "cfg.hpp", "#define CFG_VAL 1\n");
write_text_file(proj / ".env", "API_KEY=do_not_capture\n");
write_text_file(proj / ".git" / "config", "[core]\n");
std::filesystem::create_directories(proj / "inc1");
Snapshot::SnapshotOptions opts;
opts.project_root = proj;
opts.snapshot_store_dir = store;
opts.reproducibility_epoch = 0;
auto snap1 =
Snapshot::capture_snapshot(opts, {proj / "src" / "main.cpp"},
{proj / "inc1", proj / "inc2"}, {{{"inc1", "inc2"}, "cfg.hpp"}});
Void::Testing::expect(snap1.has_value());
if (!snap1) {
return;
}
// Secrets and .git are excluded.
Void::Testing::expect(!snap1->entries.contains(".env"));
Void::Testing::expect(!snap1->entries.contains(".git/config"));
// Verify private copy / no hardlinks: distinct inode and nlink == 1.
const auto &main_entry = snap1->entries.at("src/main.cpp");
auto src_meta = Platform::stat_lstat(proj / "src" / "main.cpp");
auto mat_meta = Platform::stat_lstat(main_entry.materialized_path);
Void::Testing::expect(src_meta.has_value() && mat_meta.has_value());
if (src_meta && mat_meta) {
Void::Testing::expect(src_meta->ino != mat_meta->ino);
Void::Testing::expect_eq(mat_meta->nlink, std::uint64_t{1});
Void::Testing::expect_eq(mat_meta->mtime_sec, std::int64_t{0});
}
// 1. Fresh before edits.
auto fresh0 = Snapshot::is_snapshot_fresh(*snap1, opts);
Void::Testing::expect(fresh0.has_value() && *fresh0);
// 2. Same-size edit with restored mtime MUST be detected!
auto orig_meta = Platform::stat_lstat(proj / "src" / "main.cpp");
write_text_file(proj / "src" / "main.cpp", "int main() { return 1; }\n");
(void)Platform::set_canonical_mtime(proj / "src" / "main.cpp", orig_meta->mtime_sec);
auto fresh1 = Snapshot::is_snapshot_fresh(*snap1, opts);
Void::Testing::expect(fresh1.has_value() && !*fresh1);
// Sealed snapshot file was NOT mutated by editing live source!
auto sealed_bytes = Void::FileSystem::read_file(main_entry.materialized_path);
Void::Testing::expect(sealed_bytes.has_value() &&
*sealed_bytes == "int main() { return 0; }\n");
// 3. Restore main.cpp, then create a shadowing header in earlier include dir inc1!
write_text_file(proj / "src" / "main.cpp", "int main() { return 0; }\n");
auto fresh2 = Snapshot::is_snapshot_fresh(*snap1, opts);
Void::Testing::expect(fresh2.has_value() && *fresh2);
write_text_file(proj / "inc1" / "cfg.hpp", "#define CFG_VAL 99\n");
auto fresh3 = Snapshot::is_snapshot_fresh(*snap1, opts);
Void::Testing::expect(fresh3.has_value() && !*fresh3);
// 4. Symlink escape outside project root must be rejected.
std::filesystem::create_symlink(outside, proj / "src" / "escape.hpp");
auto esc_snap = Snapshot::capture_snapshot(opts, {proj / "src" / "escape.hpp"}, {});
Void::Testing::expect(!esc_snap.has_value());
// 5. Symlink loop must be rejected.
std::filesystem::create_symlink(proj / "src" / "loop_b.hpp", proj / "src" / "loop_a.hpp");
std::filesystem::create_symlink(proj / "src" / "loop_a.hpp", proj / "src" / "loop_b.hpp");
auto loop_snap = Snapshot::capture_snapshot(opts, {proj / "src" / "loop_a.hpp"}, {});
Void::Testing::expect(!loop_snap.has_value());
}
Void::Testing::Test state_store_cas_corruption_recovery_and_gc() {
Void::FileSystem::TempDir tmp{"vbs_state_"};
auto store = State::StateStore::open(tmp / ".vbs");
Void::Testing::expect(store.has_value());
if (!store) {
return;
}
const auto artifact = tmp / "out.o";
write_text_file(artifact, "ELF_OBJECT_BYTES_1");
auto blob1 = store->store_blob_from_file(artifact);
Void::Testing::expect(blob1.has_value());
if (!blob1) {
return;
}
// Store an unpinned blob to test GC removal.
const auto orphan_src = tmp / "orphan.o";
write_text_file(orphan_src, "ORPHAN_BYTES");
auto orphan_blob = store->store_blob_from_file(orphan_src);
Void::Testing::expect(orphan_blob.has_value());
// Refuse committing action referencing fake/missing blob.
State::ActionSuccessRecord bad_rec;
bad_rec.action_key = "bad_key";
bad_rec.outputs["x"] = State::BlobRef{std::string(64, 'a'), 10, 0644};
Void::Testing::expect(!store->record_action_success(bad_rec).has_value());
// Commit valid action referencing blob1.
State::ActionSuccessRecord good_rec;
good_rec.action_key = "good_key";
good_rec.profile_identity = "prof1";
good_rec.target_name = "app";
good_rec.outputs[artifact.string()] = *blob1;
Void::Testing::expect(store->record_action_success(good_rec).has_value());
// Simulate uncommitted staging debris + dead-PID lease.
write_text_file(tmp / ".vbs" / "staging" / "partial.stage", "half-written");
auto gc_stats = store->gc();
Void::Testing::expect(gc_stats.has_value());
if (gc_stats) {
Void::Testing::expect(gc_stats->removed_blobs >= 1);
Void::Testing::expect(gc_stats->retained_blobs >= 1);
}
Void::Testing::expect(store->verify_blob(blob1->sha256).value_or(false));
Void::Testing::expect(!store->verify_blob(orphan_blob->sha256).value_or(true));
// Corrupt blob1 on disk and verify lookup detects corruption!
const auto blob_path = store->cas_blob_path(blob1->sha256);
(void)::chmod(blob_path.c_str(), 0644);
write_text_file(blob_path, "CORRUPTED_BYTES!!");
Void::Testing::expect(!store->verify_blob(blob1->sha256).value_or(true));
auto lookup = store->lookup_action("good_key");
Void::Testing::expect(lookup.has_value() && !lookup->has_value());
}
Void::Testing::Test end_to_end_native_frontend_graph_execution_and_archive_member_removal() {
Void::FileSystem::TempDir tmp{"vbs_e2e_"};
const auto proj = tmp / "app_proj";
const auto build_dir = tmp / "build";
// Notice: NO .git directory exists in `proj` (archive-without-Git requirement!).
write_text_file(proj / "build-support" / "Flags.hpp", R"cpp(#pragma once
#include <Void/Build.hpp>
namespace Helper {
void apply(Void::Build::Target t);
}
)cpp");
write_text_file(proj / "build-support" / "Flags.cpp", R"cpp(#include "Flags.hpp"
namespace Helper {
void apply(Void::Build::Target t) {
t.compile_option("-Wall");
}
}
)cpp");
write_text_file(proj / "build.profiles.cpp", R"cpp(#include <Void/Build.hpp>
void profiles(Void::Build::Profiles &p) {
p = Void::Build::default_profiles();
p.inherit("fast", "release", {"-DFAST_MODE=1"});
}
)cpp");
write_text_file(proj / "build.cpp", R"cpp(#include "build-support/Flags.hpp"
#include <iostream>
void build(Void::Build::Project &project) {
// Deliberate stdout noise to verify result channel isolation!
std::cout << "noisy user diagnostic that must not break JSON protocol\n";
auto gen = project.custom_action("gen_header");
gen.command({"/bin/sh", "-c", "mkdir -p \"$1\" && printf '#pragma once\\ninline int gen_val(){return 40;}\\n' > \"$1/generated.hpp\"",
"sh", "$GEN_DIR"})
.outputs({"generated.hpp"});
auto priv_lib = project.static_library("priv_math");
priv_lib.sources(project.glob("src/math/*.cpp"))
.include_directory("include", Void::Build::Visibility::Public);
auto mid_lib = project.static_library("mid");
mid_lib.sources({"src/mid.cpp"})
.include_directory("include", Void::Build::Visibility::Public)
.link(priv_lib, Void::Build::Visibility::Private); // Private static dep must propagate to app link!
auto app = project.executable("demo");
app.sources({"src/main.cpp"})
.link(mid_lib)
.include_directory("include", Void::Build::Visibility::Private)
.include_directory("$GEN/", Void::Build::Visibility::Private)
.depends_on(gen);
Helper::apply(app);
auto extra = project.executable("unused_tool");
extra.sources({"src/tool.cpp"});
}
)cpp");
write_text_file(proj / "include" / "math.hpp", "int add_two();\nint mid_compute();\n");
write_text_file(proj / "src" / "math" / "a.cpp", "int add_two() { return 2; }\n");
write_text_file(proj / "src" / "math" / "removable.cpp",
"extern \"C\" int vbs_removable_symbol() { return 777; }\n");
write_text_file(proj / "src" / "mid.cpp",
"#include \"math.hpp\"\nint mid_compute() { return add_two(); }\n");
write_text_file(proj / "src" / "main.cpp", R"cpp(#include "math.hpp"
#include "generated.hpp"
#include <iostream>
int main() {
std::cout << (gen_val() + mid_compute()) << "|" << __FILE__ << "|" << __DATE__ << "|" << __TIME__ << "\n";
return 0;
}
)cpp");
write_text_file(proj / "src" / "tool.cpp", "int main() { return 0; }\n");
// 1. Compile configuration descriptor out-of-process.
Config::FrontendOptions fopts;
fopts.project_root = proj;
fopts.build_root = build_dir;
fopts.sdk_include_dir = std::filesystem::path{Bootstrap::sdk_include_dir};
fopts.sdk_source_dir = std::filesystem::path{Bootstrap::sdk_source_dir};
auto compiled1 = Config::compile_descriptor(fopts);
Void::Testing::expect(compiled1.has_value());
if (!compiled1) {
return;
}
Void::Testing::expect(compiled1->recompiled);
// Second compile without edits MUST reuse cached executable!
auto compiled2 = Config::compile_descriptor(fopts);
Void::Testing::expect(compiled2.has_value());
if (compiled2) {
Void::Testing::expect(!compiled2->recompiled);
}
// Editing included helper header MUST trigger recompilation!
write_text_file(proj / "build-support" / "Flags.hpp", R"cpp(#pragma once
#include <Void/Build.hpp>
namespace Helper {
// edited comment to verify header depfile invalidation
void apply(Void::Build::Target t);
}
)cpp");
auto compiled3 = Config::compile_descriptor(fopts);
Void::Testing::expect(compiled3.has_value());
if (compiled3) {
Void::Testing::expect(compiled3->recompiled);
}
// 2. Discover custom inherited profiles and evaluate for "fast" profile.
auto discovered_profiles = Config::discover_profiles(*compiled3, fopts);
Void::Testing::expect(discovered_profiles.has_value());
if (!discovered_profiles) {
return;
}
auto ctx = discovered_profiles->resolve("fast", CompilerFamily::Gcc, Toolchain::host_os_name(),
Toolchain::host_architecture_name());
auto tc = Toolchain::discover_and_probe(ctx, build_dir / ".vbs" / "probes");
Void::Testing::expect(tc.has_value());
if (!tc) {
return;
}
auto desc = Config::evaluate_project(*compiled3, ctx, fopts);
Void::Testing::expect(desc.has_value());
if (!desc) {
return;
}
// Replace $GEN_DIR placeholder in custom_action command with profile gen dir for test.
const auto gen_dir = build_dir / "profiles" / "fast" / "gen";
for (auto &t : desc->targets) {
for (auto &arg : t.command_argv) {
if (arg == "$GEN_DIR") {
arg = gen_dir.string();
}
}
}
// 3. Capture immutable snapshot + lower graph + execute ONLY target "demo"!
Snapshot::SnapshotOptions sopts;
sopts.project_root = proj;
sopts.snapshot_store_dir = build_dir / ".vbs" / "snapshots";
sopts.reproducibility_epoch = 0;
auto snap = Snapshot::capture_snapshot(sopts, {}, {proj / "src", proj / "include"});
Void::Testing::expect(snap.has_value());
if (!snap) {
return;
}
Graph::GraphLowerOptions gopts;
gopts.project_root = proj;
gopts.build_root = build_dir;
gopts.snapshot = &(*snap);
auto graph = Graph::lower_project_graph(*desc, *tc, gopts);
Void::Testing::expect(graph.has_value());
if (!graph) {
return;
}
// Graph contains ALL configured targets (including unused_tool for compdb!),
// while executing selected target "demo" skips unused_tool!
Void::Testing::expect_eq(graph->targets.size(), std::size_t{5});
auto state = State::StateStore::open(build_dir / ".vbs");
Void::Testing::expect(state.has_value());
if (!state) {
return;
}
Execution::ExecutionOptions eopts;
eopts.jobs = 2;
eopts.selected_targets = {"demo"};
auto rep1 = Execution::execute_graph(*graph, *state, eopts);
Void::Testing::expect(rep1.has_value() && rep1->succeeded);
if (!rep1) {
return;
}
Void::Testing::expect(rep1->executed_count > 0);
Void::Testing::expect(
!std::filesystem::exists(build_dir / "profiles" / "fast" / "out" / "bin" / "unused_tool"));
// Run built demo executable and verify deterministic output & mapped __FILE__ path!
const auto demo_bin = build_dir / "profiles" / "fast" / "out" / "bin" / "demo";
Platform::ProcessRequest run_demo;
run_demo.executable = demo_bin;
auto demo_out = Platform::run_supervised(run_demo);
Void::Testing::expect(demo_out.has_value() && demo_out->succeeded());
if (demo_out) {
Void::Testing::expect(demo_out->stdout_data.starts_with("42|"));
// __FILE__ was remapped from snapshot path back to logical project path!
Void::Testing::expect(demo_out->stdout_data.find(proj.string()) != std::string::npos);
Void::Testing::expect(demo_out->stdout_data.find("snapshots/trees") == std::string::npos);
// SOURCE_DATE_EPOCH=0 produces deterministic Jan 1 1970|00:00:00!
Void::Testing::expect(demo_out->stdout_data.find("1970|00:00:00") != std::string::npos);
}
// 4. Warm no-op rebuild must execute ZERO actions and hit cache for all!
auto rep2 = Execution::execute_graph(*graph, *state, eopts);
Void::Testing::expect(rep2.has_value() && rep2->succeeded);
if (rep2) {
Void::Testing::expect_eq(rep2->executed_count, std::size_t{0});
Void::Testing::expect_eq(rep2->cached_count, rep2->total_selected);
}
// 5. Delete src/math/removable.cpp and rebuild: verify symbol disappears from libpriv_math.a!
std::filesystem::remove(proj / "src" / "math" / "removable.cpp");
auto snap2 = Snapshot::capture_snapshot(sopts, {}, {proj / "src", proj / "include"});
Void::Testing::expect(snap2.has_value());
gopts.snapshot = &(*snap2);
auto graph2 = Graph::lower_project_graph(*desc, *tc, gopts);
Void::Testing::expect(graph2.has_value());
auto rep3 = Execution::execute_graph(*graph2, *state, eopts);
Void::Testing::expect(rep3.has_value() && rep3->succeeded);
const auto archive_path = build_dir / "profiles" / "fast" / "out" / "libpriv_math.a";
Platform::ProcessRequest nm_req;
nm_req.executable = "nm";
nm_req.arguments = {archive_path.string()};
auto nm_out = Platform::run_supervised(nm_req);
Void::Testing::expect(nm_out.has_value() && nm_out->succeeded());
if (nm_out) {
Void::Testing::expect(nm_out->stdout_data.find("vbs_removable_symbol") ==
std::string::npos);
}
}