test(ffi-linkage): xfail export fn called from C via AOT (Phase 2.0)

Phase 2 of the extern/export stream verifies `export` (define + expose a
C-ABI sx symbol) end-to-end. C->sx-by-name linkage cannot work under the
corpus's `sx run` JIT mode — a JIT-resident symbol is invisible to a
dlopen'd C dylib's flat-namespace lookup — so this lands a new AOT
execution mode for the corpus: an `expected/<name>.aot` marker switches an
example from JIT `sx run` to a `sx build` + execute flow, linking the sx
object with its C `#source` companions into a native binary.

example/1226 defines `sx_square :: (n: i32) -> i32 export { ... }` and a
companion .c that declares `extern int sx_square(int)` and calls it back.
RED: with `export` not yet lowered, the AOT link fails with an undefined
`_sx_square` (the define path still emits it `internal` + with an implicit
ctx slot, and lazy lowering leaves an uncalled export fn as a bodiless
declare). Phase 2.1 greens it.

Also retires the standalone `tests/run_examples.sh` runner — `zig build
test` (src/corpus_run.test.zig) is now the sole corpus runner, and the
shell mirror would have needed its own AOT-mode port to stay in lockstep.
verify-step.sh drops its redundant step (zig build test already runs the
corpus); CLAUDE.md documents the `.aot` mode.
This commit is contained in:
agra
2026-06-14 14:41:33 +03:00
parent 6932426c41
commit 6a539ca057
11 changed files with 138 additions and 202 deletions

View File

@@ -1,11 +1,15 @@
const std = @import("std");
const corpus_paths = @import("corpus_paths");
// End-to-end example/issue regression runner — the pure-Zig replacement for
// `tests/run_examples.sh`. For every `<root>/expected/<name>.exit` marker under
// examples/ and issues/, spawn the installed `sx` binary on `<name>.sx`, capture
// stdout/stderr/exit, normalize, and diff against the stored snapshot. Optional
// `<name>.ir` snapshots additionally diff `sx ir` output.
// End-to-end example/issue regression runner. For every
// `<root>/expected/<name>.exit` marker under examples/ and issues/, spawn the
// installed `sx` binary on `<name>.sx`, capture stdout/stderr/exit, normalize,
// and diff against the stored snapshot. Optional `<name>.ir` snapshots
// additionally diff `sx ir` output; an `<name>.aot` marker switches the
// example from JIT `sx run` to a `sx build` + execute flow.
//
// This is the sole regression runner — `zig build test` is the only way to run
// the corpus (the legacy standalone `tests/run_examples.sh` was removed).
//
// Each example runs in its OWN subprocess (via std.process.run), so a crashing
// example reports its exit code (or 128+signal, matching a shell's `$?`) instead
@@ -21,9 +25,7 @@ const corpus_paths = @import("corpus_paths");
// reimplemented here.)
//
// Snapshots are regenerated in-build with `zig build test -Dupdate-goldens`
// (see the update-mode branch below) — no shell script needed. The legacy
// `bash tests/run_examples.sh --update` still works and produces byte-identical
// output; the two normalizers (here and in run_examples.sh) must stay in lockstep.
// (see the update-mode branch below) — no shell script needed.
const TIMEOUT_SECS = 10;
const MAX_OUTPUT = 16 * 1024 * 1024;
@@ -50,9 +52,9 @@ fn isLowerHex(c: u8) bool {
return (c >= '0' and c <= '9') or (c >= 'a' and c <= 'f');
}
/// Mirror of `normalize()` in run_examples.sh: collapse `0x` + 4-or-more
/// lowercase-hex digits to `0xADDR` so heap/fn addresses don't desync snapshots.
/// (The path-collapse sed rule is intentionally omitted — see file header.)
/// Collapse `0x` + 4-or-more lowercase-hex digits to `0xADDR` so heap/fn
/// addresses don't desync snapshots. (The path-collapse rule is intentionally
/// omitted — see file header.)
fn normalizeStd(arena: std.mem.Allocator, in: []const u8) ![]u8 {
var out: std.ArrayList(u8) = .empty;
var i: usize = 0;
@@ -115,7 +117,8 @@ fn appendIrSubst(arena: std.mem.Allocator, out: *std.ArrayList(u8), line: []cons
}
}
/// Mirror of `normalize_ir()` in run_examples.sh.
/// Normalize `sx ir` output for snapshot diffing: drop volatile module
/// header lines and collapse LLVM's auto-suffixed temporaries.
fn normalizeIr(arena: std.mem.Allocator, in: []const u8) ![]u8 {
var out: std.ArrayList(u8) = .empty;
var lines = std.mem.splitScalar(u8, in, '\n');
@@ -215,23 +218,74 @@ fn sweepRoot(
const err_raw = readOptional(io, a, try std.fmt.allocPrint(a, "{s}/{s}.stderr", .{ exp_dir, name })) orelse "";
const ir_raw = readOptional(io, a, try std.fmt.allocPrint(a, "{s}/{s}.ir", .{ exp_dir, name }));
// --- sx run ---
const run_res = std.process.run(a, io, .{
.argv = &.{ corpus_paths.sx_exe, "run", rel_path },
.cwd = .{ .path = repo_root },
.timeout = deadline(io),
}) catch |err| {
try failures.append(fail_gpa, try std.fmt.allocPrint(fail_gpa, "{s}: `sx run` {s}{s}", .{
name,
@errorName(err),
if (err == error.Timeout) " (>10s)" else "",
}));
continue;
};
// An `<name>.aot` marker switches the example from JIT `sx run` to a
// build+execute flow: `sx build` links the sx object with any C
// `#source` companions into a native binary, which is then executed.
// This is the ONLY way to exercise a C-ABI symbol exported FROM sx
// (an `export` fn): in JIT mode the sx symbol lives in JIT memory and
// is invisible to a dlopen'd C dylib's flat-namespace lookup, so a
// C→sx-by-name call can only be linked ahead-of-time.
const is_aot = readOptional(io, a, try std.fmt.allocPrint(a, "{s}/{s}.aot", .{ exp_dir, name })) != null;
const act_exit = termCode(run_res.term);
const act_out = trimNl(try normalizeStd(a, run_res.stdout));
const act_err = trimNl(try normalizeStd(a, run_res.stderr));
var act_exit: u32 = undefined;
var act_out: []const u8 = undefined;
var act_err: []const u8 = undefined;
if (is_aot) {
// Build a native executable, then run it. The build's own stderr
// ("compiled: <path>") is intentionally discarded — only the built
// program's streams are snapshotted. A build failure (e.g. an
// unresolved exported symbol) surfaces as a non-zero exit with the
// linker error on stderr.
const bin_path = try std.fmt.allocPrint(a, "/tmp/sx_aot_{s}", .{name});
const build_res = std.process.run(a, io, .{
.argv = &.{ corpus_paths.sx_exe, "build", rel_path, "-o", bin_path },
.cwd = .{ .path = repo_root },
.timeout = deadline(io),
}) catch |err| {
try failures.append(fail_gpa, try std.fmt.allocPrint(fail_gpa, "{s}: `sx build` {s}{s}", .{
name, @errorName(err), if (err == error.Timeout) " (>10s)" else "",
}));
continue;
};
if (termCode(build_res.term) != 0) {
act_exit = termCode(build_res.term);
act_out = "";
act_err = trimNl(try normalizeStd(a, build_res.stderr));
} else {
const exec_res = std.process.run(a, io, .{
.argv = &.{bin_path},
.cwd = .{ .path = repo_root },
.timeout = deadline(io),
}) catch |err| {
try failures.append(fail_gpa, try std.fmt.allocPrint(fail_gpa, "{s}: exec AOT binary {s}{s}", .{
name, @errorName(err), if (err == error.Timeout) " (>10s)" else "",
}));
continue;
};
act_exit = termCode(exec_res.term);
act_out = trimNl(try normalizeStd(a, exec_res.stdout));
act_err = trimNl(try normalizeStd(a, exec_res.stderr));
}
} else {
// --- sx run ---
const run_res = std.process.run(a, io, .{
.argv = &.{ corpus_paths.sx_exe, "run", rel_path },
.cwd = .{ .path = repo_root },
.timeout = deadline(io),
}) catch |err| {
try failures.append(fail_gpa, try std.fmt.allocPrint(fail_gpa, "{s}: `sx run` {s}{s}", .{
name,
@errorName(err),
if (err == error.Timeout) " (>10s)" else "",
}));
continue;
};
act_exit = termCode(run_res.term);
act_out = trimNl(try normalizeStd(a, run_res.stdout));
act_err = trimNl(try normalizeStd(a, run_res.stderr));
}
// --- sx ir (only when a snapshot already exists; mirrors the shell's
// `$has_ir` gate — update mode never CREATES new .ir files) ---