feat(wasm): compare retained core-module interfaces (#2464)

Squash merge of https://github.com/morluto/rea/pull/2464.

Co-authored-by: Thang Luu <76550342+thangldw@users.noreply.github.com>
This commit is contained in:
Thang Luu
2026-10-13 23:49:04 +08:00
committed by GitHub
co-authored by Thang Luu
parent 75e7965dc3
commit 911894d758
29 changed files with 1757 additions and 54 deletions
+2
View File
@@ -1,3 +1,4 @@
import { wasmInterfaceSchema } from "./wasmInterface.js";
import { z } from "zod";
import { createHash } from "node:crypto";
import type { EvidenceSubjectTarget } from "../evidence.js";
@@ -32,6 +33,7 @@ export const wasmArtifactSchema = z
description: z.string(),
}),
),
interface: wasmInterfaceSchema.exactOptional(),
imports: z.array(z.string()),
exports: z.array(z.string()),
headers: z.string(),
+109
View File
@@ -0,0 +1,109 @@
import { z } from "zod";
const kind = z.enum(["func", "table", "memory", "global", "tag", "unknown"]);
const valueType = z.enum([
"i32",
"i64",
"f32",
"f64",
"v128",
"funcref",
"externref",
]);
const limit = z.string().regex(/^(?:0|[1-9][0-9]*)$/u);
const signature = z.discriminatedUnion("kind", [
z.object({
kind: z.literal("func"),
params: z.array(valueType),
results: z.array(valueType),
}),
z.object({
kind: z.literal("tag"),
params: z.array(valueType),
results: z.array(valueType),
}),
z.object({
kind: z.literal("table"),
element: z.enum(["funcref", "externref"]),
address_type: z.enum(["i32", "i64"]),
minimum: limit,
maximum: limit.nullable(),
shared: z.boolean(),
}),
z.object({
kind: z.literal("memory"),
page_size: limit,
address_type: z.enum(["i32", "i64"]),
minimum: limit,
maximum: limit.nullable(),
shared: z.boolean(),
}),
z.object({
kind: z.literal("global"),
value_type: valueType,
mutable: z.boolean(),
}),
]);
/** Typed observations decoded once at the WABT producer boundary. */
export const wasmInterfaceSchema = z.object({
state: z.enum(["complete", "partial"]),
entries: z.array(
z
.object({
direction: z.enum(["import", "export"]),
module: z.string().nullable(),
name: z.string(),
kind,
index: z.number().int().nonnegative().max(0xffff_ffff).nullable(),
signature: signature.nullable(),
wat_form: z.string(),
issue: z.string().nullable(),
})
.superRefine((entry, context) => {
if (
(entry.direction === "export" && entry.module !== null) ||
(entry.direction === "import" && entry.module === null)
)
context.addIssue({
code: "custom",
message: "Interface direction does not match module-name presence",
});
if (entry.signature !== null && entry.signature.kind !== entry.kind)
context.addIssue({
code: "custom",
message: "Interface kind does not match its typed signature",
});
}),
),
issues: z.array(z.string()),
});
export type WasmInterface = z.infer<typeof wasmInterfaceSchema>;
export type WasmInterfaceEntry = WasmInterface["entries"][number];
export type WasmInterfaceSignature = NonNullable<
WasmInterfaceEntry["signature"]
>;
export type WasmInterfaceValueType = z.infer<typeof valueType>;
/** Bound eager typed output and repeated signature expansion independently of WAT bytes. */
export const WASM_INTERFACE_REPRESENTATION_BYTES = 32 * 1024 * 1024;
/** Conservative JSON/retention charge; shared arrays still expand at every occurrence. */
export const wasmInterfaceEntryRepresentationBytes = (
entry: WasmInterfaceEntry,
): number => {
let bytes =
1024 +
[entry.module, entry.name, entry.wat_form, entry.issue].reduce<number>(
(sum, value) => sum + (value?.length ?? 0) * 6,
0,
);
const signature = entry.signature;
if (signature?.kind === "func" || signature?.kind === "tag")
bytes += (signature.params.length + signature.results.length) * 16;
else if (signature !== null)
for (const value of Object.values(signature))
if (typeof value === "string") bytes += value.length * 6;
return bytes;
};