Let unchecked executables fault on deep recursion as C does

With checks off by default, an AOT executable owns its process, so deep
recursion can fault on the guard page as C's does. Its functions check
the stack limit only when their frame is larger than the slack, which
could step past the guard page, and _start sets the limit only when one
does. Loaded code and --checks builds keep the check in every calling
function, since an overflow in a host process would take the host down.
MachineOptions.executable reaches MachineProgram.executable through
lowering and allocation.

An element address in the no-check form cannot trap, so it no longer
keeps a function's frame. Hello's main is now frameless: lea, xor, lea,
mov, mov and a tail jump to the write. The file is 254 bytes, with one
LOAD, and machine_elf.sh now fails above 320 bytes.

The private clone's make check passes 277 of 279 (zx_web_lifecycle fails
on clone path length; packages fails on a package-resolution case that is
not native code), and check-bootstrap matches.
This commit is contained in:
waozi committed 2026-11-16 04:28:52 -03:00
1 parent 6e3d216c68
commit 118ec0ff46
8 files changed
+45 -21

No files matched your search

+1
View File
@@ -2,6 +2,7 @@
## [Unreleased]
- An unchecked native executable checks the stack limit only in functions whose frame could step past the guard page, so deep recursion faults like C's; loaded code and `--checks` builds check every calling function. A function whose element addresses need no bound check can also run without a frame. `examples/hello.zi` is 254 bytes, with a 6-instruction main.
- `#checks` keeps defensive checks exactly where it is written in a build without `--checks`: alone at the top of a module, before a block or a statement, or before one index or slice access. Native x86-64 and AArch64 code, Wasm and generated C check those accesses and nothing else (`tests/checks_granularity_cases.zi`); Go emits Ziran's explicit range check there. Saved IR (ZIR 66) keeps the marks and ZX carries them per instruction, only in functions that have any.
- Ziran is manually safe like C by default. Native x86-64 and AArch64 code (in memory and as ELF), Wasm, generated C and the checks Ziran writes into Go carry no defensive runtime check: a data pointer is its address, indexes and ranges are raw, frame storage is plain arena bytes, `resize` hands the runtime the block's address, the native loader keeps no pointer table and hosts take views as they are. `--checks` builds the checked program as before: tokens resolved at every access, every bounds check, the same traps and diagnostics; the trap and hostile-twin tests build with it. The portable engine stays checked. On Omega (11 samples, loaded host) native Sieve drops from 108.9 to 48.4 µs, Vectors from 125.6 to 34.9 µs and Printing from 13.8 to 5.2 µs; generated C for the compiler itself loses 980 checks.
+5
View File
@@ -286,6 +286,9 @@ MachineProgram :: struct {
// Built with --checks: data pointers are tokens checked at every access.
// Otherwise a pointer is the address, as in C.
checks: bool
// An AOT executable, which owns its process: built without checks, its
// deep recursion faults on the guard page as C's does.
executable: bool
}
MachineLimits :: struct {
@@ -332,6 +335,8 @@ MachineOptions :: struct {
// Whether code charges fuel; an AOT executable with an unbounded budget
// never runs out, so it carries no fuel checks.
fuel: bool
// An AOT executable rather than code loaded into a host process.
executable: bool
}
MachineResult :: struct {
+2 -1
View File
@@ -1462,7 +1462,8 @@ Finalize :: (state: *LowerState) {
stack_objects = owner.stack_objects[:], relocations = owner.relocations[:],
calls = owner.calls[:], entry = state.lir.entry, stage = .Virtual,
frames = owner.frames[:], spills = owner.spills[:],
scratch_general = MachineNoId, scratch_float = MachineNoId, checks = state.checks}
scratch_general = MachineNoId, scratch_float = MachineNoId, checks = state.checks,
executable = state.options.executable}
}
Result :: (state: LowerState) -> MachineResult {
+1
View File
@@ -213,6 +213,7 @@ Build :: (lir: LirProgram, target: TargetDescription, limits: MachineLimits,
built: MachineNativeOwner
options := MachineDefaultOptions()
options.fuel = !emit_elf || fuel != MachineElfInitialFuel
options.executable = emit_elf
lowered := MachineLower(*built.machine, lir, target, options, limits)
if lowered.problem != .None {
diagnostic := LowerDiagnostic(lowered)
+4 -4
View File
@@ -2111,7 +2111,7 @@ Release :: (owner: *MachineOwner) {
}
Publish :: (owner: *MachineOwner, target: TargetDescription, entry: s32, scratch_general: s32, scratch_float: s32,
checks: bool) {
checks: bool, executable: bool) {
owner.program = .{version = MachineVersion, target = target,
text = owner.text[:], sources = owner.sources[:], origins = owner.origins[:],
types = owner.types[:], type_ids = owner.type_ids[:], data = owner.data[:],
@@ -2121,7 +2121,7 @@ Publish :: (owner: *MachineOwner, target: TargetDescription, entry: s32, scratch
copies = owner.copies[:], stack_objects = owner.stack_objects[:], relocations = owner.relocations[:],
calls = owner.calls[:], entry = entry, stage = .Allocated, frames = owner.frames[:],
spills = owner.spills[:], scratch_general = scratch_general, scratch_float = scratch_float,
checks = checks}
checks = checks, executable = executable}
owner.valid = true
}
@@ -2294,7 +2294,7 @@ RegAllocAllocate :: (owner: *MachineOwner, target: TargetDescription, limits: Ma
Release(*built)
return state.diagnostic
}
Publish(*built, target, program.entry, state.scratch[0], state.scratch[1], program.checks)
Publish(*built, target, program.entry, state.scratch[0], state.scratch[1], program.checks, program.executable)
verified := MachineVerifyAllocated(built.program, limits)
if verified.problem != .None {
located := InputLocation(*state, verified)
@@ -2304,7 +2304,7 @@ RegAllocAllocate :: (owner: *MachineOwner, target: TargetDescription, limits: Ma
}
FreeScratch(*state)
Exchange(owner, *built)
Publish(owner, target, program.entry, state.scratch[0], state.scratch[1], program.checks)
Publish(owner, target, program.entry, state.scratch[0], state.scratch[1], program.checks, program.executable)
Release(*built)
return diagnostic
}
+18 -5
View File
@@ -1863,13 +1863,21 @@ Frameless :: (program: MachineProgram, function_id: s32) -> bool {
end := block.instruction_begin + block.instruction_count
for at: block.instruction_begin..end - 1 {
if TailCallAt(program, block_id, at) { continue }
if !PlainOpcode(program.instructions[at].opcode) { return false }
if !PlainInstruction(program, program.instructions[at]) { return false }
}
}
return true
}
// Instructions that never trap, call or touch the frame.
// Whether INSTRUCTION never traps, calls or touches the frame: a plain
// opcode, or an element address whose bound needs no check.
PlainInstruction :: (program: MachineProgram, instruction: MachineInstruction) -> bool {
if instruction.opcode == .ElementAddress {
return instruction.operand_count == 4 && program.operands[instruction.operand_begin + 3].kind == .Immediate
}
return PlainOpcode(instruction.opcode)
}
PlainOpcode :: (opcode: MachineOpcode) -> bool {
return opcode == .Move || opcode == .MoveImmediate || opcode == .GlobalAddress || opcode == .FunctionAddress ||
opcode == .IntegerAdd || opcode == .IntegerSubtract || opcode == .IntegerMultiply ||
@@ -2591,14 +2599,19 @@ Process :: (program: MachineProgram, limits: MachineLimits, output: *MachineX64O
#scope_export
// Whether FUNCTION's prologue checks the stack limit: it calls Ziran code (or
// the host, which may call back), or its frame is larger than the slack the
// limit leaves.
// Whether FUNCTION's prologue checks the stack limit: its frame is larger
// than the slack the limit leaves, or it calls Ziran code (or the host,
// which may call back) in loaded or checked code.
MachineX64StackChecked :: (program: MachineProgram, function_id: s32) -> bool {
if function_id < 0 || function_id >= program.functions.count { return false }
function := program.functions[function_id]
if function.calling != .Internal { return false }
if function_id < program.frames.count && program.frames[function_id].size > MachineX64StackSlack { return true }
// An unchecked executable lets deep recursion fault on the guard page,
// as C does; a frame larger than the slack keeps its check, since it
// could step past the guard page. Loaded code always checks: an
// overflow there would take down its host.
if program.executable && !program.checks { return false }
for block_id: function.block_begin..function.block_begin + function.block_count - 1 {
block := program.blocks[block_id]
for instruction_id: block.instruction_begin..block.instruction_begin + block.instruction_count - 1 {
+13 -10
View File
@@ -477,16 +477,19 @@ headers, data follows the text a page higher, and an image with nothing
writable has one LOAD. Hello: 576 bytes. machine_elf.sh now fails above
640 bytes or with the bool formatter present.
Left for hello, after the manual-safety switch lands: the checked resolve
of the string (about 20 instructions, the pointer runtime, the RW segment
and the trap stub) becomes one lea, leaving _start (4) + main (8) + the
write loop with its partial-write, EINTR and failure handling (13).
Landed since: a call its function returns at once is a tail jump
(`leave; jmp`), and a function with no stack objects, no trap-capable
instruction and no call but such a tail call has no frame, so once the
resolve is gone hello's main is 4 instructions. Then tighten the retained
bound to about 300 bytes. Frames stay wherever a trap or call could
reach the loader's C handlers, which need the ABI's 16-byte alignment.
Done with the unchecked default: hello is 254 bytes in one LOAD, with
_start (4 instructions) and a frameless main (6) ending in a tail jump to
the write loop (14 with its partial-write, EINTR and failure handling).
A call its function returns at once is a tail jump, and a function with
no stack objects, no trap-capable instruction and no call but such a
tail call has no frame. Frames stay wherever a trap or call could reach
the loader's C handlers, which need the ABI's 16-byte alignment. An
unchecked executable checks its stack only for frames larger than the
slack (so it cannot step past the guard page). Loaded code always checks,
since an overflow there would take down its host. machine_elf.sh fails
above 320 bytes. Left: main still materializes its zero index (`xor ecx,
ecx; lea rsi, [rax+rcx]`); folding constant element indices into the
address would make it 4.
Compile time: MachineVerify's FuelFacts and CallsAndRelocations, the
allocated verifier's call check and x86 BuildMachineImage's ImageOperand
+1 -1
View File
@@ -81,7 +81,7 @@ grep -Fq 'Number of section headers: 0' "$work/hello.header"
# Hello carries only what it reaches: one direct write, no output buffer,
# terminal probe, formatter, fuel or stack check, and no page padding.
hello_size=$(wc -c < "$work/hello.elf")
if test "$hello_size" -gt 640 || grep -Fq truefalse "$work/hello.elf"; then
if test "$hello_size" -gt 320 || grep -Fq truefalse "$work/hello.elf"; then
echo "native hello grew to $hello_size bytes or carries unused runtime" >&2
exit 1
fi