fix: diagnose missing 'main' instead of segfaulting on 'sx run' (issue 0137)

A program with no 'main' reached the JIT entry-point call with a garbage
address (ORC reports lookup success but leaves main_addr degenerate), then
called it -> SIGSEGV. Add a pre-JIT entry-point check in main.zig that emits
'error: no main function found' and exits non-zero before codegen, plus a
defensive main_addr==0 guard in target.zig runJITFromObject as a backstop.

Regression: examples/1188-diagnostics-run-no-main.sx
This commit is contained in:
agra
2026-06-21 09:10:30 +03:00
parent 11dc6a3299
commit 6ed29621ad
8 changed files with 72 additions and 0 deletions

View File

@@ -383,6 +383,17 @@ pub fn runJITFromObject(obj_buf: c.LLVMMemoryBufferRef, priority_dylibs: []const
return error.CompileError;
}
// Defensive backstop (issue 0137): ORC has been observed reporting
// success from the `main` lookup while leaving `main_addr` at 0 — calling
// @ptrFromInt(0) then segfaults. The real fix is the pre-JIT entry-point
// check in main.zig (which also catches the observed NON-zero garbage
// address case); this guard is a last line of defense so a null entry can
// never be called regardless of how we got here.
if (main_addr == 0) {
std.debug.print("error: no 'main' function found in JIT module\n", .{});
return error.CompileError;
}
// Cast to function pointer and call. The exit code is main's integer
// return truncated to u8 — matching the OS truncation an AOT binary's
// exit status already gets, so JIT and AOT agree (e.g. 1105 -> 81,