ERR/E4.2: truncate integer main's return to u8 for the JIT exit code

A non-failable integer `main :: () -> T` must exit with its return value
truncated to u8 (matching C main / the OS exit-status byte), so `sx run`
(JIT) and an AOT binary agree. runJITMain clamped instead: any value outside
0..255 returned exit 1, so `return 1105` exited 1 (not 81), `return -1` exited
1 (not 255), and `return 256` exited 1 (not 0).

Fix: bit-cast the i32 return to u32 and @truncate to u8 — negatives wrap as
their two's-complement low byte rather than being clamped. The AOT path
already gets OS truncation, so it was already correct; this makes JIT match.

examples/238-main-exit-truncation.sx returns 1105 -> exit 81. Values <=255
(42, 200) still pass through unchanged.

Gates: zig build, zig build test, bash tests/run_examples.sh (275 passed; the
lone failure is the user's uncommitted 213-canonical-map pack WIP).
This commit is contained in:
agra
2026-06-01 01:42:53 +03:00
parent f9dd965b69
commit 94335f94d7
4 changed files with 22 additions and 2 deletions

View File

@@ -0,0 +1,14 @@
// Entry-point exit-code truncation (ERR step E4.2, non-failable integer main).
// `main :: () -> T` (integer) exits with the return value truncated to u8 —
// matching C `main` / the OS exit-status byte, so the JIT (`sx run`) and an AOT
// binary agree. Here 1105 & 0xFF == 81, so this program exits 81 (NOT 1, which
// was the old buggy "out of 0..255 range -> failure" behavior).
//
// Run: ./zig-out/bin/sx run examples/238-main-exit-truncation.sx ; echo $? # 81
#import "modules/std.sx";
main :: () -> s32 {
print("returning 1105 -> exit {}\n", 1105 & 0xFF); // 81
return 1105;
}

View File

@@ -235,10 +235,14 @@ pub fn runJITFromObject(obj_buf: c.LLVMMemoryBufferRef) !u8 {
return error.CompileError;
}
// Cast to function pointer and call
// 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,
// -1 -> 255, 256 -> 0). Bit-cast i32 -> u32 first so negatives wrap
// as two's-complement low byte rather than being clamped.
const main_fn: *const fn () callconv(.c) i32 = @ptrFromInt(main_addr);
const result = main_fn();
return if (result >= 0 and result <= 255) @intCast(result) else 1;
return @truncate(@as(u32, @bitCast(result)));
}
// Android APK bundling (createApk, compileJniMainSources,

View File

@@ -0,0 +1 @@
81

View File

@@ -0,0 +1 @@
returning 1105 -> exit 81