test(hopper): add semantic runtime conformance

This commit is contained in:
morluto
2026-07-18 04:50:23 +00:00
parent e6cfce3902
commit da1da3bfb8
17 changed files with 1251 additions and 71 deletions
+17
View File
@@ -1,13 +1,30 @@
import { readFile } from "node:fs/promises";
import { describe, expect, it } from "vitest";
import {
generateLargeFixture,
HOPPER_C_ORACLE,
LARGE_FIXTURE_COUNT,
sha256,
sourceDigest,
} from "../scripts/lib/conformance-fixtures.mjs";
describe("source-built conformance fixtures", () => {
it("grounds the Hopper semantic oracle in the source-owned C fixture", async () => {
const source = await readFile("tests/conformance/c/fixture.c", "utf8");
for (const procedure of [
HOPPER_C_ORACLE.mainProcedure,
HOPPER_C_ORACLE.entryProcedure,
HOPPER_C_ORACLE.branchProcedure,
HOPPER_C_ORACLE.leafProcedure,
])
expect(source).toContain(`int ${procedure}`);
expect(source).toContain(HOPPER_C_ORACLE.entryString);
expect(source).toContain(HOPPER_C_ORACLE.leafString);
expect(source).toContain(HOPPER_C_ORACLE.globalName);
});
it("generates a deterministic large pagination fixture", () => {
const first = generateLargeFixture();
const second = generateLargeFixture();
+155
View File
@@ -4,6 +4,7 @@ import {
firstProcedureAddress,
requireDistinctTargetHashes,
requireAddressArray,
requireFunctionDossierOracle,
requireFunctionDossier,
requirePseudocode,
} from "../src/application/RealHopperAssertions.js";
@@ -86,8 +87,161 @@ describe("real Hopper semantic assertions", () => {
};
expect(requireFunctionDossier(scanLimited, "0x1000")).toEqual(scanLimited);
});
it("requires fixture-specific positive dossier evidence", () => {
const entry = fixtureDossier({
address: "0x1000",
name: "rea_entry",
callee: { address: "0x2000", name: "rea_branch" },
caller: { address: "0x0900", name: "main" },
referencedString: "REA_C_ENTRY",
comment: "REA real-Hopper verifier",
successor: "0x1010",
});
expect(
requireFunctionDossierOracle(entry, {
procedure_address: "0x1000",
callee_address: "0x2000",
caller_address: "0x0900",
referenced_string: "REA_C_ENTRY",
referenced_name: "rea_c_global",
comment: "REA real-Hopper verifier",
require_cfg_successor: true,
require_assembly: true,
}),
).toEqual(entry);
for (const field of [
"callees",
"callers",
"outgoing_references",
"referenced_strings",
"referenced_names",
"comments",
"assembly",
] as const) {
expect(() =>
requireFunctionDossierOracle(
{ ...entry, [field]: emptyCollection() },
{
procedure_address: "0x1000",
callee_address: "0x2000",
caller_address: "0x0900",
referenced_string: "REA_C_ENTRY",
referenced_name: "rea_c_global",
comment: "REA real-Hopper verifier",
require_cfg_successor: true,
require_assembly: true,
},
),
).toThrow();
}
expect(() =>
requireFunctionDossierOracle(
{
...entry,
referenced_names: bounded([
{
address: "0x4000",
value: "_rea_c_global",
source_address: "0x2000",
},
]),
},
{
procedure_address: "0x1000",
referenced_name: "rea_c_global",
},
),
).toThrow();
expect(() =>
requireFunctionDossierOracle(
{
...entry,
basic_blocks: bounded([{ start: "0x1000", successors: [] }]),
},
{
procedure_address: "0x1000",
callee_address: "0x2000",
referenced_string: "REA_C_ENTRY",
comment: "REA real-Hopper verifier",
require_cfg_successor: true,
},
),
).toThrow(/CFG successor/u);
});
});
const fixtureDossier = (input: {
readonly address: string;
readonly name: string;
readonly callee: { readonly address: string; readonly name: string };
readonly caller: { readonly address: string; readonly name: string };
readonly referencedString: string;
readonly comment: string;
readonly successor: string;
}) => ({
...validDossier(),
procedure: { address: input.address, name: input.name },
assembly: bounded(["mov eax, eax"]),
comments: bounded([
{ address: input.address, kind: "comment", text: input.comment },
]),
callees: bounded([input.callee]),
callers: bounded([input.caller]),
outgoing_references: bounded([
{
source_address: input.address,
target_address: input.callee.address,
source_procedure: { address: input.address, name: input.name },
target_procedure: input.callee,
kind: { available: false, reason: "public API did not classify kind" },
},
{
source_address: "0x1004",
target_address: "0x3000",
source_procedure: { address: input.address, name: input.name },
target_procedure: null,
kind: { available: false, reason: "public API did not classify kind" },
},
{
source_address: "0x1008",
target_address: "0x4000",
source_procedure: { address: input.address, name: input.name },
target_procedure: null,
kind: { available: false, reason: "public API did not classify kind" },
},
]),
referenced_strings: bounded([
{
address: "0x3000",
value: input.referencedString,
source_address: "0x1004",
},
]),
referenced_names: bounded([
{
address: "0x4000",
value: "_rea_c_global",
source_address: "0x1008",
},
]),
basic_blocks: bounded([
{ start: input.address, successors: [input.successor] },
{ start: input.successor, successors: [] },
]),
});
const bounded = (items: readonly unknown[]) => ({
items,
total: items.length,
returned: items.length,
truncated: false,
next_offset: null,
});
const emptyCollection = () => bounded([]);
const validDossier = () => {
const collection = {
items: [],
@@ -99,6 +253,7 @@ const validDossier = () => {
return {
procedure: { address: "0x1000", name: "main" },
pseudocode: { text: "return 0;", returned_chars: 9, total_chars: 9 },
assembly: collection,
comments: collection,
callers: collection,
callees: collection,
+135
View File
@@ -0,0 +1,135 @@
import { createHash } from "node:crypto";
import { mkdtemp, readFile, rm, symlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { loadRealHopperFixtureTargets } from "../scripts/lib/real-hopper-fixture.mjs";
let root: string | undefined;
afterEach(async () => {
if (root !== undefined) await rm(root, { recursive: true, force: true });
root = undefined;
});
describe("real Hopper fixture binding", () => {
it("binds exact source-owned artifacts and their semantic oracle", async () => {
const manifest = await fixtureManifest();
await expect(loadRealHopperFixtureTargets(manifest)).resolves.toMatchObject(
{
primary: { path: join(root ?? "", "c") },
secondary: { path: join(root ?? "", "version-v2") },
large: { path: join(root ?? "", "large") },
largeOracle: {
symbolPrefix: "_rea_page_",
symbolCount: 1_205,
stringPrefix: "REA_PAGE_",
stringCount: 1_205,
},
oracle: {
mainProcedure: "main",
entryProcedure: "rea_entry",
branchProcedure: "rea_branch",
leafProcedure: "rea_leaf",
entryString: "REA_C_ENTRY",
leafString: "REA_C_LEAF",
globalName: "rea_c_global",
},
},
);
});
it("rejects digest drift and artifacts escaping the manifest root", async () => {
const manifest = await fixtureManifest();
await writeFile(join(root ?? "", "c"), "changed");
await expect(loadRealHopperFixtureTargets(manifest)).rejects.toThrow(
/digest/u,
);
const outside = await mkdtemp(join(tmpdir(), "rea-hopper-outside-"));
try {
await writeFile(join(outside, "c"), "primary");
await rm(join(root ?? "", "c"));
await symlink(join(outside, "c"), join(root ?? "", "c"));
await expect(loadRealHopperFixtureTargets(manifest)).rejects.toThrow(
/escapes/u,
);
} finally {
await rm(outside, { recursive: true, force: true });
}
});
it("rejects manifest entries that alias the same artifact", async () => {
const manifest = await fixtureManifest();
const parsed = JSON.parse(await readFile(manifest, "utf8")) as {
fixtures: { name: string; artifact: string; artifactSha256: string }[];
};
const primary = parsed.fixtures.find(({ name }) => name === "c");
const secondary = parsed.fixtures.find(({ name }) => name === "version-v2");
if (primary === undefined || secondary === undefined)
throw new Error("Fixture manifest setup failed");
secondary.artifact = primary.artifact;
secondary.artifactSha256 = primary.artifactSha256;
await writeFile(manifest, JSON.stringify(parsed));
await expect(loadRealHopperFixtureTargets(manifest)).rejects.toThrow(
/distinct artifacts/u,
);
});
});
const fixtureManifest = async (): Promise<string> => {
root = await mkdtemp(join(tmpdir(), "rea-hopper-fixture-"));
const primary = Buffer.from("primary");
const secondary = Buffer.from("secondary");
await Promise.all([
writeFile(join(root, "c"), primary),
writeFile(join(root, "version-v2"), secondary),
writeFile(join(root, "large"), Buffer.from("large")),
]);
const manifest = join(root, "manifest.json");
await writeFile(
manifest,
JSON.stringify({
schemaVersion: 1,
fixtures: [
{
name: "c",
artifact: "c",
artifactSha256: digest(primary),
expectations: {
hopperOracle: {
mainProcedure: "main",
entryProcedure: "rea_entry",
branchProcedure: "rea_branch",
leafProcedure: "rea_leaf",
entryString: "REA_C_ENTRY",
leafString: "REA_C_LEAF",
globalName: "rea_c_global",
},
},
},
{
name: "version-v2",
artifact: "version-v2",
artifactSha256: digest(secondary),
},
{
name: "large",
artifact: "large",
artifactSha256: digest(Buffer.from("large")),
expectations: {
symbolPrefix: "_rea_page_",
symbolCount: 1_205,
stringPrefix: "REA_PAGE_",
stringCount: 1_205,
},
},
],
}),
);
return manifest;
};
const digest = (value: Buffer): string =>
createHash("sha256").update(value).digest("hex");
+72
View File
@@ -0,0 +1,72 @@
import { describe, expect, it } from "vitest";
import { verifyLargeFixturePagination } from "../scripts/lib/real-hopper-pagination.mjs";
const normalize = (value: unknown): unknown => value;
describe("real Hopper large-fixture pagination", () => {
it("proves every procedure and string across exact search pages", async () => {
await expect(
verifyLargeFixturePagination({
client: fixtureClient(205),
options: {},
normalizedResult: normalize,
expectedCount: 205,
symbolPrefix: "_rea_page_",
stringPrefix: "REA_PAGE_",
}),
).resolves.toEqual({
procedures: { count: 205, pages: 3 },
strings: { count: 205, pages: 3 },
});
});
it.each(["truncated", "duplicate", "wrong-total"] as const)(
"rejects %s positive evidence",
async (fault) => {
await expect(
verifyLargeFixturePagination({
client: fixtureClient(205, fault),
options: {},
normalizedResult: normalize,
expectedCount: 205,
symbolPrefix: "_rea_page_",
stringPrefix: "REA_PAGE_",
}),
).rejects.toThrow();
},
);
});
const fixtureClient = (
count: number,
fault?: "truncated" | "duplicate" | "wrong-total",
) => ({
callTool: async (request: unknown) => {
const parsed = request as {
name: string;
arguments: { offset: number; limit: number };
};
const { offset, limit } = parsed.arguments;
const returned = Math.min(limit, count - offset);
const procedure = parsed.name === "search_procedures";
const items = Array.from({ length: returned }, (_, pageIndex) => {
const index = offset + pageIndex;
const evidenceIndex = fault === "duplicate" && index === 1 ? 0 : index;
return {
address: `0x${(0x1000 + index).toString(16)}`,
value: `${procedure ? "_rea_page_" : "REA_PAGE_"}${String(evidenceIndex).padStart(4, "0")}`,
value_truncated: fault === "truncated" && index === 0,
};
});
const next = offset + returned < count ? offset + returned : null;
return {
items,
offset,
limit,
total: fault === "wrong-total" ? count + 1 : count,
next_offset: next,
has_more: next !== null,
};
},
});
+106
View File
@@ -0,0 +1,106 @@
import { describe, expect, it } from "vitest";
import {
analyzeFixtureProcedure,
requireCurrentDocument,
resolveFixtureProcedure,
} from "../scripts/lib/real-hopper-semantic.mjs";
const normalize = (value: unknown): unknown => value;
describe("real Hopper semantic fixture resolution", () => {
it("requests assembly when analyzing fixture procedures", async () => {
const requests: unknown[] = [];
const result = await analyzeFixtureProcedure(
{
callTool: async (request: unknown) => {
requests.push(request);
return { dossier: true };
},
},
{},
{ address: "0x1000", name: "rea_entry" },
normalize,
);
expect(result).toEqual({ dossier: true });
expect(requests).toEqual([
{
name: "analyze_function",
arguments: { procedure: "0x1000", include_assembly: true },
},
]);
});
it("binds mutations to the active document, not the first listed one", async () => {
await expect(
requireCurrentDocument(
clientReturning("active"),
{},
["unrelated", "active"],
normalize,
),
).resolves.toBe("active");
await expect(
requireCurrentDocument(
clientReturning("unknown"),
{},
["unrelated", "active"],
normalize,
),
).rejects.toThrow(/current_document/u);
});
it("resolves the public search value and preserves its reported name", async () => {
const procedure = await resolveFixtureProcedure(
clientWithItems([
{
address: "0x1000",
value: "_rea_entry",
value_truncated: false,
},
]),
{},
"rea_entry",
normalize,
);
expect(procedure).toEqual({ address: "0x1000", name: "_rea_entry" });
});
it.each([
["empty", []],
[
"truncated",
[{ address: "0x1000", value: "rea_entry", value_truncated: true }],
],
[
"duplicate",
[
{ address: "0x1000", value: "rea_entry", value_truncated: false },
{ address: "0x2000", value: "_rea_entry", value_truncated: false },
],
],
[
"wrong name",
[{ address: "0x1000", value: "rea_other", value_truncated: false }],
],
])("rejects %s search evidence", async (_label, items) => {
await expect(
resolveFixtureProcedure(
clientWithItems(items),
{},
"rea_entry",
normalize,
),
).rejects.toThrow(/exactly one Hopper procedure/u);
});
});
const clientWithItems = (items: readonly unknown[]) => ({
callTool: async () => ({ items }),
});
const clientReturning = (value: unknown) => ({
callTool: async () => value,
});