#include "../../experiments/frontend/gcc-worker/Trace.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { namespace fs = std::filesystem; namespace protocol = Void::LanguageServer::Protocol; using protocol::Json; using vls::gcc::ReplayRecording; using vls::gcc::ReplayValuePoint; using vls::gcc::Trace; using Void::Testing::expect; using Void::Testing::expect_eq; using Void::Testing::require; using namespace std::string_view_literals; constexpr auto nonce = "00112233445566778899aabbccddeeff"sv; constexpr auto digest = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"sv; constexpr std::int64_t pid = 4321; template T checked(std::expected result, std::source_location at = std::source_location::current()) { expect_eq(result ? ""sv : std::string_view{result.error()}, ""sv, at); require(result.has_value(), at); return *std::move(result); } std::string wire(std::uint32_t seq, std::string_view kind, std::string_view fields = {}) { return std::format(R"({{"v":1,"nonce":"{}","seq":{},"kind":"{}"{}}})", nonce, seq, kind, fields); } struct Writer { explicit Writer(const fs::path &path, std::string_view source = "int answer = 17;\n") : trace(checked(Trace::create(path, source, digest, nonce, pid))) {} void event(std::string_view kind, std::string_view fields = {}) { checked(trace->event(wire(sequence++, kind, fields))); } void hello() { event( "hello", R"(,"backend":"gcc-16.2.0/vls-eval-1","pid":4321,"capabilities":["integralReturn","cooperativeStep"],"kinds":["hello","enter","body","value","cached","stopped","resumed","leave","terminal","frontendEnd"])"); } void frame(std::string_view kind, std::uint32_t id, std::optional parent, std::string_view fields = {}) { event(kind, std::format(R"(,"frame":{},"parent":{}{})", id, parent ? std::to_string(*parent) : "null", fields)); } void enter(std::optional parent, std::string_view name) { frame( "enter", sequence, parent, std::format( R"(,"name":"{}","sourceKind":"logicalDeclaration","source":"input.cpp","line":5,"byteColumn":17)", name)); } void integer(std::uint32_t id, std::optional parent, unsigned value) { frame( "value", id, parent, std::format( R"(,"value":{{"kind":"integer","precision":32,"signed":true,"bitsHex":"{:08x}"}})", value)); } void inner_stop(unsigned value) { hello(); enter({}, "twice"); frame("body", 1, {}); enter(1, "reflected_value"); frame("body", 3, 1); integer(3, 1, value); frame("stopped", 3, 1); } void outer_stop(unsigned value) { checked(trace->authorize(6, "step")); frame("resumed", 3, 1, R"(,"stop":6,"action":"step")"); frame("leave", 3, 1, R"(,"outcome":"returned")"); integer(1, {}, value); frame("stopped", 1, {}); } void finish_pair(std::string_view action = "continue") { checked(trace->authorize(10, action)); frame("resumed", 1, {}, std::format(R"(,"stop":10,"action":"{}")", action)); frame("leave", 1, {}, R"(,"outcome":"returned")"); event("frontendEnd"); checked(trace->finish("completed", 0, "")); } std::unique_ptr trace; std::uint32_t sequence{}; }; void write_pair(const fs::path &path, unsigned inner, std::string_view action = "continue") { Writer writer{path, std::format("int answer = {};\n", inner)}; writer.inner_stop(inner); writer.outer_stop(inner * 2); writer.finish_pair(action); } const Json *member(const Json &json, std::string_view key) { const auto *object = protocol::as_object(&json); return object ? protocol::find_member(*object, key) : nullptr; } const Json &field(const Json &json, std::string_view key) { const auto *value = member(json, key); require(value != nullptr); return *value; } std::string_view text(const Json &json, std::string_view key) { const auto *string = protocol::as_string(&field(json, key)); require(string != nullptr); return *string; } const Json::Array &frames(const Json &json) { return std::get(field(json, "frames").value); } std::string_view value_bits(const Json &frame) { return text(field(frame, "value"), "bitsHex"); } void expect_active_value(const ReplayRecording &recording, std::uint32_t seq, std::string_view bits) { auto cursor = checked(recording.cursor_at(seq)); require(!frames(cursor).empty()); expect_eq(value_bits(frames(cursor).back()), bits); } void expect_point(std::expected, std::string> result, std::uint32_t value, std::optional stop) { const auto point = checked(std::move(result)); require(point.has_value()); expect_eq(point->value_sequence, value); expect_eq(point->stop_sequence, stop); } std::string read_bytes(const fs::path &path) { std::ifstream in(path, std::ios::binary); require(in.good()); return {std::istreambuf_iterator{in}, std::istreambuf_iterator{}}; } void write_bytes(const fs::path &path, std::string_view data, std::ios::openmode mode = std::ios::trunc) { std::ofstream out(path, mode | std::ios::binary); out.write(data.data(), static_cast(data.size())); out.close(); require(out.good()); } // Produce a checksum-valid suffix the real writer would reject. Keep hash failure distinct // from transcript failure when testing the immutable reader's verified prefix. void append_hashed_event(const fs::path &path, std::size_t previous_record, std::string_view event) { const auto bytes = read_bytes(path); const auto previous_digest = bytes.substr(previous_record + 5, 32); require(previous_digest.size() == 32); const auto size = event.size(); const std::array header{'E', static_cast(size >> 24), static_cast(size >> 16), static_cast(size >> 8), static_cast(size)}; auto preimage = previous_digest; preimage.append(header.data(), header.size()).append(event); const auto hash = Void::Crypto::sha256(preimage); require(hash && hash->size() == 32); std::string record{header.data(), header.size()}; record.append(reinterpret_cast(hash->data()), hash->size()).append(event); write_bytes(path, record, std::ios::app); } } // namespace VOID_TEST(RecordingTests, retains_values_and_source_across_overwrite_replacement_and_unlink) { Void::FileSystem::TempDir dir{"vls_recording_"}; const auto path = dir / "selected"; const auto edited = dir / "edited"; write_pair(path, 17); write_pair(edited, 41); const auto old = checked(ReplayRecording::open(path)); const auto old_page = checked(old->event_page(5, 2)); const auto saved_source = old->source_bytes(); require(fs::copy_file(edited, path, fs::copy_options::overwrite_existing)); const auto newer = checked(ReplayRecording::open(path)); expect_active_value(*old, 6, "00000011"); expect_active_value(*newer, 6, "00000029"); const auto replacement = dir / "replacement"; write_pair(replacement, 17, "step"); fs::rename(replacement, path); const auto replaced = checked(ReplayRecording::open(path)); require(fs::remove(path)); expect(!ReplayRecording::open(path)); expect_active_value(*old, 10, "00000022"); expect_active_value(*newer, 10, "00000052"); expect_eq(saved_source, "int answer = 17;\n"sv); expect_eq(newer->source_bytes(), "int answer = 41;\n"sv); expect(old->metadata().source_sha256 != newer->metadata().source_sha256); expect_eq(old->metadata().compiler_sha256, digest); expect_eq(old->metadata().pid, pid); expect_eq(old_page[1].kind, "stopped"sv); expect_eq(checked(replaced->event_page(11, 1))[0].acknowledged_action, std::optional{"step"sv}); } VOID_TEST(RecordingTests, separates_event_time_authorization_acknowledgement_and_wait) { Void::FileSystem::TempDir dir{"vls_recording_"}; const auto path = dir / "selected"; Writer writer{path}; const auto before_hello = checked(ReplayRecording::open(path)); writer.inner_stop(17); checked(writer.trace->authorize(6, "step")); const auto pending = checked(ReplayRecording::open(path)); writer.frame("resumed", 3, 1, R"(,"stop":6,"action":"step")"); writer.frame("leave", 3, 1, R"(,"outcome":"returned")"); writer.integer(1, {}, 34); writer.frame("stopped", 1, {}); writer.finish_pair("step"); const auto step = checked(ReplayRecording::open(path)); write_pair(dir / "continue", 17); const auto continued = checked(ReplayRecording::open(dir / "continue")); expect_eq(before_hello->event_count(), std::size_t{0}); expect(!before_hello->cursor_at(0)); expect(!checked(before_hello->next_value())); expect(std::holds_alternative( field(before_hello->final_cursor(), "sequence").value)); expect(before_hello->metadata().partial && !before_hello->metadata().corrupt); expect(!before_hello->metadata().outcome && !before_hello->metadata().journal_error); expect_eq(pending->event_count(), std::size_t{7}); const auto paused_cursor = checked(pending->cursor_at(6)); expect_eq(protocol::as_uint32(member(paused_cursor, "stop")), std::optional{6}); expect(std::holds_alternative(field(paused_cursor, "pendingAction").value)); const auto authorized = pending->final_cursor(); expect_eq(text(authorized, "pendingAction"), "step"sv); expect(std::holds_alternative(field(authorized, "stop").value)); expect(!pending->cursor_at(7)); expect(!checked(pending->event_page(6, 1))[0].acknowledged_action); expect(!pending->metadata().outcome); for (const auto &recording : {step, continued}) { const auto summary = checked(recording->event_page(11, 1))[0]; expect_eq(summary.kind, "resumed"sv); expect_eq(summary.frame, std::optional{1}); expect(!summary.parent); expect_eq(summary.acknowledged_stop, std::optional{10}); expect_eq(summary.acknowledged_action, std::optional{recording == step ? "step"sv : "continue"sv}); const auto historical = checked(recording->cursor_at(6)); expect(member(historical, "outcome") == nullptr && member(historical, "source") == nullptr); expect(member(historical, "diagnostics") == nullptr && member(historical, "partial") == nullptr); expect_eq(text(historical, "nonce"), nonce); expect_eq(text(frames(historical).back(), "sourceKind"), "logicalDeclaration"sv); expect_eq(recording->metadata().outcome, std::optional{"completed"sv}); expect_eq(recording->metadata().exit_code, std::optional{0}); expect(!recording->metadata().partial); } } VOID_TEST(RecordingTests, navigates_observed_integer_values_and_optional_stops_in_both_directions) { Void::FileSystem::TempDir dir{"vls_recording_"}; const auto path = dir / "selected"; Writer writer{path}; writer.inner_stop(17); writer.outer_stop(34); checked(writer.trace->authorize(10, "continue")); writer.frame("resumed", 1, {}, R"(,"stop":10,"action":"continue")"); writer.frame("leave", 1, {}, R"(,"outcome":"returned")"); writer.enter({}, "unavailable"); writer.frame("body", 13, {}); writer.frame("value", 13, {}, R"(,"value":{"kind":"unavailable","reason":"unsupportedScalar"})"); writer.frame("leave", 13, {}, R"(,"outcome":"returned")"); writer.enter({}, "unattended"); writer.frame("body", 17, {}); writer.integer(17, {}, 22); writer.frame("leave", 17, {}, R"(,"outcome":"returned")"); writer.event("frontendEnd"); const auto recording = checked(ReplayRecording::open(path)); expect_point(recording->next_value(), 5, 6); expect_point(recording->next_value(5), 9, 10); expect_point(recording->next_value(6), 9, 10); expect_point(recording->next_value(10), 19, {}); expect_point(recording->previous_value(), 19, {}); expect_point(recording->previous_value(19), 9, 10); expect_point(recording->previous_value(10), 5, 6); expect_point(recording->previous_value(9), 5, 6); expect(!checked(recording->previous_value(6))); expect(!checked(recording->next_value(19))); expect(!recording->previous_value(22)); expect(!recording->next_value(std::numeric_limits::max())); expect_eq(recording->event_count(), std::size_t{22}); const auto page = checked(recording->event_page(3, 3)); require(page.size() == 3); expect_eq(page[0].sequence, 3u); expect_eq(page[2].kind, "value"sv); expect_eq(page[2].parent, std::optional{1}); expect(&page[2] == checked(recording->event_page(5, 1)).data()); expect(checked(recording->event_page(22, 256)).empty()); expect(checked(recording->event_page(8, 0)).empty()); expect_eq(checked(recording->event_page(21, 256)).size(), std::size_t{1}); expect(!recording->event_page(23, 0)); expect(!recording->event_page(0, ReplayRecording::max_page_size + 1)); expect(!recording->event_page(std::numeric_limits::max(), 1)); expect(recording->metadata().partial && !recording->metadata().exit_code); expect(std::get(field(recording->final_cursor(), "frontendEnded").value)); } VOID_TEST(RecordingTests, preserves_provisional_value_failed_leave_and_raw_binary_bytes) { Void::FileSystem::TempDir dir{"vls_recording_"}; const auto path = dir / "selected"; constexpr auto source = "a\0\xffz"sv; constexpr auto diagnostics = "err\0\xfe"sv; Writer writer{path, source}; writer.hello(); writer.enter({}, "cleanup"); writer.frame("body", 1, {}); writer.integer(1, {}, 9); writer.frame("leave", 1, {}, R"(,"outcome":"failed")"); writer.event("frontendEnd"); const auto before_wait = checked(ReplayRecording::open(path)); checked(writer.trace->finish("failed", 1, diagnostics)); const auto recording = checked(ReplayRecording::open(path)); const auto prior = checked(recording->cursor_at(2)); expect(member(frames(prior).back(), "value") == nullptr); const auto produced = checked(recording->cursor_at(3)); expect_eq(value_bits(frames(produced).back()), "00000009"sv); expect(std::get(field(frames(produced).back(), "valueProvisional").value)); expect(member(frames(produced).back(), "outcome") == nullptr); const auto later = checked(recording->cursor_at(4)); expect(frames(later).empty()); const auto &leave = field(later, "lastLeave"); expect_eq(value_bits(leave), "00000009"sv); expect_eq(text(leave, "outcome"), "failed"sv); expect(std::get(field(leave, "valueProvisional").value)); expect(!before_wait->diagnostic_bytes()); expect(!before_wait->metadata().outcome); expect_eq(recording->source_bytes(), source); expect_eq(recording->diagnostic_bytes(), std::optional{diagnostics}); expect_eq(recording->metadata().outcome, std::optional{"failed"sv}); expect_eq(recording->metadata().exit_code, std::optional{1}); expect(!recording->metadata().partial); } VOID_TEST(RecordingTests, separates_truncated_corrupt_and_unverified_outcome_tails) { Void::FileSystem::TempDir dir{"vls_recording_"}; const auto path = dir / "selected"; write_pair(path, 17); const auto original = read_bytes(path); fs::resize_file(path, original.size() - 1); const auto torn = checked(ReplayRecording::open(path)); expect(torn->metadata().partial && !torn->metadata().corrupt); require(torn->metadata().journal_error.has_value()); expect_eq(*torn->metadata().journal_error, "truncated trace journal record"sv); expect_eq(torn->event_count(), std::size_t{14}); expect(!torn->metadata().outcome && !torn->diagnostic_bytes()); expect_active_value(*torn, 6, "00000011"); expect(!torn->cursor_at(14)); write_bytes(path, original + "x"); const auto extra = checked(ReplayRecording::open(path)); expect(extra->metadata().partial && extra->metadata().corrupt); expect(!extra->metadata().outcome && !extra->diagnostic_bytes()); expect_eq(extra->event_count(), torn->event_count()); expect_eq(extra->metadata().journal_error, std::optional{"trace journal has data after its process outcome"sv}); auto corrupt = original; const auto offset = corrupt.find("reflected_value"); require(offset != corrupt.npos); corrupt[offset] = 'X'; write_bytes(path, corrupt); const auto prefix = checked(ReplayRecording::open(path)); expect(prefix->metadata().corrupt); expect_eq(prefix->event_count(), std::size_t{3}); expect(!prefix->cursor_at(3) && !checked(prefix->next_value())); expect_eq(frames(prefix->final_cursor()).size(), std::size_t{1}); write_bytes(path, original.substr(0, 12)); expect(!ReplayRecording::open(path)); expect(!ReplayRecording::open(path.filename())); expect(!ReplayRecording::open(dir.path())); } VOID_TEST(RecordingTests, rejects_checksum_valid_duplicate_skipped_and_invalid_frame_events) { Void::FileSystem::TempDir dir{"vls_recording_"}; const auto path = dir / "selected"; Writer writer{path}; const auto hello_offset = fs::file_size(path); writer.hello(); const auto good = read_bytes(path); for (const auto &event : {wire(0, "frontendEnd"), wire(2, "frontendEnd"), wire(1, "body", R"(,"frame":1,"parent":null)")}) { write_bytes(path, good); append_hashed_event(path, hello_offset, event); const auto recording = checked(ReplayRecording::open(path)); expect_eq(recording->event_count(), std::size_t{1}); expect(recording->metadata().corrupt && recording->metadata().partial); expect(!recording->cursor_at(1)); require(recording->metadata().journal_error.has_value()); expect(recording->metadata().journal_error->starts_with("invalid compiler trace")); const auto first = checked(recording->event_page(0, 1))[0]; expect_eq(first.kind, "hello"sv); expect(!first.frame && !first.parent && !first.acknowledged_stop); } // The reader must bound allocation by verified record lengths even for a huge sparse file. write_bytes(path, good); fs::resize_file(path, 1ull << 30); const auto sparse = checked(ReplayRecording::open(path)); expect(sparse->metadata().corrupt && sparse->event_count() == 1); } VOID_TEST(RecordingTests, seeks_and_pages_at_the_event_limit_with_deep_nesting) { Void::FileSystem::TempDir dir{"vls_recording_"}; const auto path = dir / "selected"; Writer writer{path}; constexpr std::uint32_t depth = 2047; writer.hello(); for (std::uint32_t id = 1; id <= depth; ++id) { writer.enter(id == 1 ? std::nullopt : std::optional{id - 1}, "nested"); } for (auto id = depth; id > 0; --id) { writer.frame("leave", id, id == 1 ? std::nullopt : std::optional{id - 1}, R"(,"outcome":"returned")"); } writer.event("frontendEnd"); checked(writer.trace->finish("completed", 0, "")); const auto recording = checked(ReplayRecording::open(path)); expect_eq(recording->event_count(), std::size_t{4096}); expect(!recording->cursor_at(4096)); for (std::size_t offset = 0; offset < recording->event_count(); offset += 256) { const auto page = checked(recording->event_page(offset, 256)); require(page.size() == 256); expect_eq(page.front().sequence, static_cast(offset)); expect_eq(page.back().sequence, static_cast(offset + 255)); } const auto full = checked(recording->cursor_at(depth)); const auto &stack = frames(full); require(stack.size() == depth); expect(std::holds_alternative(field(stack.front(), "parent").value)); expect_eq(protocol::as_uint32(member(stack.back(), "frame")), std::optional{depth}); expect_eq(protocol::as_uint32(member(stack.back(), "parent")), std::optional{depth - 1}); const auto half = checked(recording->cursor_at(depth + 1024)); expect_eq(frames(half).size(), std::size_t{depth - 1024}); expect_eq(protocol::as_uint32(member(field(half, "lastLeave"), "frame")), std::optional{depth - 1023}); expect(frames(recording->final_cursor()).empty()); expect(!checked(recording->next_value())); } VOID_TEST(RecordingTests, concurrent_readers_keep_independent_cursors_after_removal) { Void::FileSystem::TempDir dir{"vls_recording_"}; const auto path = dir / "selected"; write_pair(path, 17); auto owner = checked(ReplayRecording::open(path)); require(fs::remove(path)); const auto at_inner = checked(protocol::serialize_json(checked(owner->cursor_at(6)))); const auto at_outer = checked(protocol::serialize_json(checked(owner->cursor_at(10)))); std::atomic consistent{true}; std::barrier start{9}; std::vector readers; for (unsigned i = 0; i < 8; ++i) { readers.emplace_back([owner, i, &at_inner, &at_outer, &consistent, &start] { start.arrive_and_wait(); for (unsigned run = 0; run < 64; ++run) { const bool inner = (run + i) % 2 == 0; auto state = owner->cursor_at(inner ? 6 : 10); auto page = owner->event_page(7, 1); if (!state || protocol::serialize_json(*state).value_or("") != (inner ? at_inner : at_outer) || !page || page->front().acknowledged_action != std::optional{"step"sv} || owner->source_bytes() != "int answer = 17;\n"sv) { consistent = false; return; } } }); } owner.reset(); start.arrive_and_wait(); readers.clear(); // Join each owner before checking its result. expect(consistent.load()); }