One commit, because the parts below only build together. Allocator: an interval needed at the current instruction is never evicted, and the allocated-program verifier checks that every operand sits where the allocation says. AArch64 encoder: compiler_aarch64.zi encodes allocated machine IR with the shared ARM byte helpers, maps the function-address relocation, sign-extends signed 8- and 16-bit loads and rejects what it cannot encode with a named diagnostic. It has no AOT ELF writer yet. Native default with checked pointers and no fallback: on linux-x86_64, `ziran run app.zx`, `zi2zx run`, the embedding API's Auto and the Kryon players run a bundle's verified program as native code in W^X memory. Stray and freed pointers, double free, bounds and read-only stores trap. A native gap is a named preflight error, nothing falls back to the engine, and `--backend engine` picks it explicitly. zx2native uses the same preflight. Native code exchanges module state for hot reload, checks the stack, zeroes fresh locals, spends fuel at the engine's checkpoints and returns records, arrays and strings from host calls. `zi2zx --debug` source origins name lines in native traps and `zx2native --emit map`. The host value API drops its VM prefix (HostValue, HOST_*). A call with a record result loads the hidden result address into rax only after its stack arguments are stored, and an indirect target stays in r10: rax could hold a stack argument (Kryon's text-field clip guard arrived as an address) or the target (indirect calls returning records crashed). Self-hosting ports: compiler_backend_process, compiler_backend_cache, compiler_native_names and compiler_runtime_emit replace the zir_c_build, zir_c_cache and zir_c_process C, with checked bootstrap C and Makefile rules. zir_c.toolchain again reports "compiling OUT failed:" with the head of the compiler output and a truncation marker. The parallel scheduler no longer stalls on a held compiler after reaping a finished job, and poll or read errors now fail the build. A fresh BUILD_DIR (Kryon's runtime build) builds: generated files have pattern rules, objects wait for the headers they include, and engine flags are per object instead of leaking into zi2c. Legacy removals F1-F10 and N1-N4: the duplicate LirX64 encoder and its spec test, zir_c_*, zir_runtime and zir_native, nine zero-reference spec tests, vec_capacity, the zx_metadata and native_cache_names benchmarks, build-android-host.sh and two shell wrappers are deleted; zx2native is machine-only; `zi2zx` rejects `--opt` on ZX input; the bootstrap is regenerated. ASSEMBLY docs, the legacy-removal plan, the milestones and the remaining-work plan record the current position. Co-Authored-By: Claude Opus 5.5 <[email protected]>
502 lines
26 KiB
Ziran
502 lines
26 KiB
Ziran
using Bundle :: #import "compiler_bundle"
|
|
using Debug :: #import "compiler_exec_debug"
|
|
using Exec :: #import "compiler_exec"
|
|
using Link :: #import "compiler_exec_link"
|
|
using Lir :: #import "compiler_lowered_ir"
|
|
using Verify :: #import "compiler_lowered_verify"
|
|
using Vectors :: #import "std/vec"
|
|
|
|
Fail :: (code: s32, detail: s32 = 0) -> s32 {
|
|
print("executable bundle failure %:%\n", code, detail)
|
|
return code
|
|
}
|
|
|
|
CopyBytes :: (source: []u8) -> Vec(u8) {
|
|
result: Vec(u8)
|
|
if !VecResizeBounded(result, source.count, source.count) { return .{} }
|
|
for index: 0..source.count - 1 { result[index] = source[index] }
|
|
return result
|
|
}
|
|
|
|
SameBytes :: (left: []u8, right: []u8) -> bool {
|
|
if left.count != right.count { return false }
|
|
for index: 0..left.count - 1 { if left[index] != right[index] { return false } }
|
|
return true
|
|
}
|
|
|
|
ReadU64 :: (bytes: []u8, at: s64) -> u64 {
|
|
value: u64
|
|
for index: 0..7 { value |= cast(u64)bytes[at + index] << cast(u64)(index * 8) }
|
|
return value
|
|
}
|
|
|
|
BudgetContainer :: (program: Lir.LirProgram, code: s32) -> s32 {
|
|
empty: []u8
|
|
no_assets: []Bundle.Asset
|
|
measured := Link.ZxExecWrite(program, no_assets, empty)
|
|
if measured.problem != .None || measured.size < 1 { return Fail(code, cast(s32)measured.problem) }
|
|
first: Vec(u8)
|
|
second: Vec(u8)
|
|
if !VecResizeBounded(first, measured.size, measured.size) ||
|
|
!VecResizeBounded(second, measured.size, measured.size) { return Fail(code + 1) }
|
|
written := Link.ZxExecWrite(program, no_assets, first[:])
|
|
repeated := Link.ZxExecWrite(program, no_assets, second[:])
|
|
if written.problem != .None || repeated.problem != .None || !SameBytes(first[:], second[:]) {
|
|
VecFree(first); VecFree(second)
|
|
return Fail(code + 2)
|
|
}
|
|
|
|
owner: Link.ZxExecOwner
|
|
read := Link.ZxExecRead(*owner, TextView(first[:]), Lir.LirDefaultLimits())
|
|
if read.problem != .None || !owner.valid ||
|
|
Link.ZxExecReadProgram(*owner).functions.count != program.functions.count {
|
|
Link.ZxExecReadFree(*owner); VecFree(first); VecFree(second)
|
|
return Fail(code + 3, cast(s32)read.problem)
|
|
}
|
|
limited := Lir.LirDefaultLimits()
|
|
limited.work = first.count
|
|
read = Link.ZxExecRead(*owner, TextView(first[:]), limited)
|
|
if read.problem != .Limit || read.diagnostic.problem != .Limit || owner.valid {
|
|
VecFree(first); VecFree(second)
|
|
return Fail(code + 4, cast(s32)read.problem)
|
|
}
|
|
limited = Lir.LirDefaultLimits()
|
|
limited.bytes = first.count - 1
|
|
read = Link.ZxExecRead(*owner, TextView(first[:]), limited)
|
|
if read.problem != .Limit || owner.valid {
|
|
VecFree(first); VecFree(second)
|
|
return Fail(code + 5, cast(s32)read.problem)
|
|
}
|
|
read = Link.ZxExecRead(*owner, TextView(first[:]), Lir.LirDefaultLimits())
|
|
if read.problem != .None || !owner.valid ||
|
|
Link.ZxExecReadProgram(*owner).functions.count != program.functions.count {
|
|
Link.ZxExecReadFree(*owner); VecFree(first); VecFree(second)
|
|
return Fail(code + 6, cast(s32)read.problem)
|
|
}
|
|
read = Link.ZxExecRead(*owner, TextView(second[:]), Lir.LirDefaultLimits())
|
|
if read.problem != .None || !owner.valid { return Fail(code + 7, cast(s32)read.problem) }
|
|
Link.ZxExecReadFree(*owner)
|
|
VecFree(first)
|
|
VecFree(second)
|
|
return 0
|
|
}
|
|
|
|
BudgetManySymbols :: () -> s32 {
|
|
// Prefix names maximize compared name bytes while remaining unique.
|
|
types: [2]Lir.LirType = .{.{kind = .Void}, .{kind = .Integer, bits = 32, is_signed = true}}
|
|
modules: [1]string = .{"budget-symbols"}
|
|
constants: [1]Lir.LirConstant = .{.{type = 1, bits = 42}}
|
|
values: [1]s32 = .{1}
|
|
returned: [1]s32 = .{0}
|
|
instructions: [2]Lir.LirInstruction
|
|
instructions[0] = .{op = .Constant, result = 0, symbol = 0, origin = Lir.LirNoId}
|
|
instructions[1] = .{op = .Return, result = Lir.LirNoId, operands = returned[:],
|
|
symbol = Lir.LirNoId, origin = Lir.LirNoId}
|
|
blocks: [1]Lir.LirBlock = .{.{instructions = instructions[:]}}
|
|
names := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
|
functions: Vec(Lir.LirFunction)
|
|
if !VecResizeBounded(functions, names.count, names.count) { return Fail(30) }
|
|
for index: 0..functions.count - 1 {
|
|
functions[index] = .{module = 0, name = names[0:index + 1], result = 1,
|
|
values = values[:], blocks = blocks[:], entry = 0, origin = Lir.LirNoId}
|
|
}
|
|
program := Lir.LirProgram.{version = Lir.LirVersion, modules = modules[:], types = types[:],
|
|
constants = constants[:], functions = functions[:], entry = 0}
|
|
result := BudgetContainer(program, 31)
|
|
VecFree(functions)
|
|
return result
|
|
}
|
|
|
|
BudgetRepeatedIndirect :: () -> s32 {
|
|
// Every indirect call is reachable. Validation must add the conservative
|
|
// all-functions set once instead of rescanning it for every instruction.
|
|
types: [3]Lir.LirType = .{.{kind = .Void}, .{kind = .Integer, bits = 32, is_signed = true},
|
|
.{kind = .Function, result = 1}}
|
|
modules: [1]string = .{"budget-indirect"}
|
|
constants: [1]Lir.LirConstant = .{.{type = 1, bits = 42}}
|
|
simple_values: [1]s32 = .{1}
|
|
simple_returned: [1]s32 = .{0}
|
|
simple_instructions: [2]Lir.LirInstruction
|
|
simple_instructions[0] = .{op = .Constant, result = 0, symbol = 0, origin = Lir.LirNoId}
|
|
simple_instructions[1] = .{op = .Return, result = Lir.LirNoId, operands = simple_returned[:],
|
|
symbol = Lir.LirNoId, origin = Lir.LirNoId}
|
|
simple_blocks: [1]Lir.LirBlock = .{.{instructions = simple_instructions[:]}}
|
|
|
|
repeats: s64 = 128
|
|
entry_values: Vec(s32)
|
|
entry_instructions: Vec(Lir.LirInstruction)
|
|
if !VecResizeBounded(entry_values, repeats + 1, repeats + 1) ||
|
|
!VecResizeBounded(entry_instructions, repeats + 2, repeats + 2) { return Fail(40) }
|
|
entry_values[0] = 2
|
|
entry_instructions[0] = .{op = .FunctionAddress, result = 0, symbol = 1, origin = Lir.LirNoId}
|
|
indirect_operands: [1]s32 = .{0}
|
|
for index: 0..repeats - 1 {
|
|
entry_values[index + 1] = 1
|
|
entry_instructions[index + 1] = .{op = .CallIndirect, result = cast(s32)index + 1,
|
|
operands = indirect_operands[:], effect = cast(s32)index + 1,
|
|
symbol = Lir.LirNoId, origin = Lir.LirNoId}
|
|
}
|
|
entry_returned: [1]s32 = .{cast(s32)repeats}
|
|
entry_instructions[repeats + 1] = .{op = .Return, result = Lir.LirNoId,
|
|
operands = entry_returned[:], symbol = Lir.LirNoId, origin = Lir.LirNoId}
|
|
entry_blocks: [1]Lir.LirBlock = .{.{instructions = entry_instructions[:]}}
|
|
|
|
names := "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
|
|
functions: Vec(Lir.LirFunction)
|
|
if !VecResizeBounded(functions, names.count, names.count) { return Fail(41) }
|
|
functions[0] = .{module = 0, name = names[0:1], result = 1, values = entry_values[:],
|
|
blocks = entry_blocks[:], entry = 0, origin = Lir.LirNoId}
|
|
for index: 1..functions.count - 1 {
|
|
functions[index] = .{module = 0, name = names[0:index + 1], result = 1,
|
|
values = simple_values[:], blocks = simple_blocks[:], entry = 0, origin = Lir.LirNoId}
|
|
}
|
|
program := Lir.LirProgram.{version = Lir.LirVersion, modules = modules[:], types = types[:],
|
|
constants = constants[:], functions = functions[:], entry = 0}
|
|
result := BudgetContainer(program, 42)
|
|
VecFree(functions)
|
|
VecFree(entry_values)
|
|
VecFree(entry_instructions)
|
|
return result
|
|
}
|
|
|
|
Doubler :: (context: *void, function: s32, arguments: []u64, result: *u64) -> bool {
|
|
if function != 1 || arguments.count != 1 { return false }
|
|
<<result = arguments[0] * cast(u64)2
|
|
return true
|
|
}
|
|
|
|
Wide :: (context: *void, function: s32, arguments: []u64, result: *u64) -> bool {
|
|
<<result = cast(u64)1 << cast(u64)40
|
|
return true
|
|
}
|
|
|
|
Refuse :: (context: *void, function: s32, arguments: []u64, result: *u64) -> bool { return false }
|
|
|
|
HostRun :: (program: Lir.LirProgram, call: Exec.LirExecHostCall, bind: bool) -> Exec.LirExecResult {
|
|
plan: Exec.LirExecPlan
|
|
defer Exec.LirExecPlanFree(*plan)
|
|
built := Exec.LirExecPlanBuild(program, Lir.LirDefaultLimits(), *plan)
|
|
if built.problem != .None { return built }
|
|
instance: Exec.LirExecInstance
|
|
defer Exec.LirExecInstanceClose(*instance)
|
|
opened := Exec.LirExecInstanceOpen(*plan, Exec.LirExecDefaultLimits(), 1, *instance)
|
|
if opened.problem != .None { return opened }
|
|
if bind {
|
|
if Exec.LirExecBindHost(*instance, 1, call, null) != .None { return Exec.LirExecResult.{problem = .NotReady} }
|
|
// A capability is bound once, and only host signatures can be bound.
|
|
if Exec.LirExecBindHost(*instance, 1, call, null) != .Calling ||
|
|
Exec.LirExecBindHost(*instance, 0, call, null) != .Calling { return Exec.LirExecResult.{problem = .NotReady} }
|
|
}
|
|
return Exec.LirExecRun(*instance)
|
|
}
|
|
|
|
// One portable entry calls one bodiless host capability: s32 -> s32.
|
|
HostCapabilities :: () -> s32 {
|
|
types: [3]Lir.LirType = .{.{kind = .Void}, .{kind = .Integer, bits = 32, is_signed = true}, .{kind = .Pointer, element = 1}}
|
|
modules: [1]string = .{"executable"}
|
|
constants: [1]Lir.LirConstant = .{.{type = 1, bits = 21}}
|
|
values: [2]s32 = .{1, 1}
|
|
argument: [1]s32 = .{0}
|
|
returned: [1]s32 = .{1}
|
|
instructions: [3]Lir.LirInstruction
|
|
instructions[0] = .{op = .Constant, result = 0, symbol = 0, origin = -1}
|
|
instructions[1] = .{op = .HostCall, result = 1, operands = argument[:], symbol = 1, effect = 1, origin = -1}
|
|
instructions[2] = .{op = .Return, result = -1, operands = returned[:], symbol = -1, origin = -1}
|
|
blocks: [1]Lir.LirBlock = .{.{instructions = instructions[:]}}
|
|
parameters: [1]s32 = .{1}
|
|
functions: [2]Lir.LirFunction = .{
|
|
.{module = 0, name = "main", result = 1, values = values[:], blocks = blocks[:], entry = 0, origin = -1},
|
|
.{module = 0, name = "Input", parameters = parameters[:], result = 1, calling = .Host,
|
|
external = true, entry = -1, origin = -1}}
|
|
program := Lir.LirProgram.{version = Lir.LirVersion, modules = modules[:], types = types[:],
|
|
constants = constants[:], functions = functions[:], entry = 0}
|
|
if Verify.LirVerify(program, Lir.LirDefaultLimits()).problem != .None { return Fail(40) }
|
|
|
|
no_assets: [0]Bundle.Asset
|
|
empty: []u8
|
|
measured := Link.ZxExecWrite(program, no_assets[:], empty)
|
|
if measured.problem != .None { return Fail(41, cast(s32)measured.problem) }
|
|
bytes: Vec(u8)
|
|
defer VecFree(bytes)
|
|
if !VecResizeBounded(bytes, measured.size, measured.size) { return Fail(42) }
|
|
if Link.ZxExecWrite(program, no_assets[:], bytes[:]).problem != .None { return Fail(43) }
|
|
// The manifest starts at 160; its fourth u32 is the recomputed capability count.
|
|
if bytes[172] != 1 || bytes[173] != 0 || bytes[174] != 0 || bytes[175] != 0 { return Fail(44) }
|
|
owner: Link.ZxExecOwner
|
|
read := Link.ZxExecRead(*owner, TextView(bytes[:]), Lir.LirDefaultLimits())
|
|
if read.problem != .None { return Fail(45, cast(s32)read.problem) }
|
|
decoded := Link.ZxExecReadProgram(*owner)
|
|
if decoded.functions.count != 2 || !decoded.functions[1].external || decoded.functions[1].calling != .Host { return Fail(46) }
|
|
|
|
// A run never starts while a required capability is unbound.
|
|
missing := HostRun(decoded, Doubler, false)
|
|
if missing.problem != .Host || missing.function != 1 { return Fail(47, cast(s32)missing.problem) }
|
|
bound := HostRun(decoded, Doubler, true)
|
|
if bound.problem != .None || bound.value.bits != 42 { return Fail(48, cast(s32)bound.problem) }
|
|
// Host results are checked against the declared scalar type before use.
|
|
wide := HostRun(decoded, Wide, true)
|
|
if wide.problem != .Host { return Fail(49, cast(s32)wide.problem) }
|
|
refused := HostRun(decoded, Refuse, true)
|
|
if refused.problem != .Host { return Fail(50, cast(s32)refused.problem) }
|
|
Link.ZxExecReadFree(*owner)
|
|
|
|
// The reader recomputes the count instead of trusting the producer.
|
|
tampered := CopyBytes(bytes[:])
|
|
tampered[172] = 0
|
|
read = Link.ZxExecRead(*owner, TextView(tampered[:]), Lir.LirDefaultLimits())
|
|
VecFree(tampered)
|
|
if read.problem != .CapabilityCount || owner.valid { return Fail(51, cast(s32)read.problem) }
|
|
|
|
// Host signatures stay bodiless scalars; ordinary calls and procedure values
|
|
// cannot reach them, and portable code cannot be host-called.
|
|
instructions[1].op = .Call
|
|
if Verify.LirVerify(program, Lir.LirDefaultLimits()).problem != .Calling { return Fail(52) }
|
|
instructions[1].op = .HostCall
|
|
instructions[1].symbol = 0
|
|
if Verify.LirVerify(program, Lir.LirDefaultLimits()).problem != .Calling { return Fail(53) }
|
|
instructions[1].symbol = 1
|
|
functions[1].blocks = blocks[:]
|
|
if Verify.LirVerify(program, Lir.LirDefaultLimits()).problem != .Function { return Fail(54) }
|
|
functions[1].blocks = blocks[0:0]
|
|
// A pointer crosses as an opaque handle, so the declaration is valid; the
|
|
// call that passes it an integer is not.
|
|
pointer_parameters: [1]s32 = .{2}
|
|
functions[1].parameters = pointer_parameters[:]
|
|
if Verify.LirVerify(program, Lir.LirDefaultLimits()).problem != .Calling { return Fail(55) }
|
|
functions[1].parameters = parameters[:]
|
|
functions[1].calling = .C
|
|
if Verify.LirVerify(program, Lir.LirDefaultLimits()).problem != .Unsupported { return Fail(56) }
|
|
functions[1].calling = .Host
|
|
program.entry = 1
|
|
if Verify.LirVerify(program, Lir.LirDefaultLimits()).problem != .Calling { return Fail(57) }
|
|
return 0
|
|
}
|
|
|
|
// Read-only globals exist only for byte strings, and nothing can write them.
|
|
ReadOnlyBytes :: () -> s32 {
|
|
types: [5]Lir.LirType = .{.{kind = .Void}, .{kind = .Integer, bits = 8}, .{kind = .Array, element = 1, count = 2},
|
|
.{kind = .Pointer, element = 2}, .{kind = .Pointer, element = 1}}
|
|
modules: [1]string = .{"bytes"}
|
|
data: [2]u8 = .{104, 105}
|
|
constants: [3]Lir.LirConstant = .{.{type = 2, bytes = data[:]}, .{type = 1, bits = 0}, .{type = 1, bits = 7}}
|
|
globals: [1]Lir.LirGlobal = .{.{module = 0, name = "string.0", type = 2, constant = 0, writable = false}}
|
|
values: [5]s32 = .{3, 1, 4, 1, 1}
|
|
address: [2]s32 = .{0, 1}
|
|
read: [1]s32 = .{2}
|
|
stored: [2]s32 = .{2, 4}
|
|
returned: [1]s32 = .{3}
|
|
instructions: [6]Lir.LirInstruction
|
|
instructions[0] = .{op = .GlobalAddress, result = 0, symbol = 0, origin = -1}
|
|
instructions[1] = .{op = .Constant, result = 1, symbol = 1, origin = -1}
|
|
instructions[2] = .{op = .ElementAddress, result = 2, operands = address[:], symbol = -1, origin = -1}
|
|
instructions[3] = .{op = .Load, result = 3, operands = read[:], symbol = -1, effect = 1, origin = -1}
|
|
instructions[4] = .{op = .Constant, result = 4, symbol = 2, origin = -1}
|
|
instructions[5] = .{op = .Return, result = -1, operands = returned[:], symbol = -1, origin = -1}
|
|
blocks: [1]Lir.LirBlock = .{.{instructions = instructions[:]}}
|
|
functions: [1]Lir.LirFunction = .{.{module = 0, name = "main", result = 1, values = values[:], blocks = blocks[:], entry = 0, origin = -1}}
|
|
program := Lir.LirProgram.{version = Lir.LirVersion, modules = modules[:], types = types[:], constants = constants[:],
|
|
globals = globals[:], functions = functions[:], entry = 0}
|
|
no_hosts: Exec.LirExecHostCall = Refuse
|
|
loaded := HostRun(program, no_hosts, false)
|
|
if loaded.problem != .None || loaded.value.bits != 104 { return Fail(60, cast(s32)loaded.problem) }
|
|
// A store through read-only bytes traps instead of changing string data.
|
|
store_instructions: [7]Lir.LirInstruction
|
|
for index: 0..4 { store_instructions[index] = instructions[index] }
|
|
store_instructions[5] = .{op = .Store, result = -1, operands = stored[:], symbol = -1, effect = 2, origin = -1}
|
|
store_instructions[6] = instructions[5]
|
|
store_blocks: [1]Lir.LirBlock = .{.{instructions = store_instructions[:]}}
|
|
store_functions: [1]Lir.LirFunction = .{.{module = 0, name = "main", result = 1, values = values[:], blocks = store_blocks[:], entry = 0, origin = -1}}
|
|
store_program := program
|
|
store_program.functions = store_functions[:]
|
|
stored_result := HostRun(store_program, no_hosts, false)
|
|
if stored_result.problem != .Pointer { return Fail(61, cast(s32)stored_result.problem) }
|
|
short: [1]u8 = .{1}
|
|
constants[0].bytes = short[:]
|
|
if Verify.LirVerify(program, Lir.LirDefaultLimits()).problem != .Constant { return Fail(62) }
|
|
constants[0].bytes = data[:]
|
|
scalar_globals: [1]Lir.LirGlobal = .{.{module = 0, name = "scalar", type = 1, constant = 1, writable = false}}
|
|
program.globals = scalar_globals[:]
|
|
if Verify.LirVerify(program, Lir.LirDefaultLimits()).problem != .Unsupported { return Fail(63) }
|
|
return 0
|
|
}
|
|
|
|
// The optional debug attachment restores the source texts and origins the
|
|
// program section strips, and only onto the program it was written for.
|
|
DebugAttachment :: (program: Lir.LirProgram) -> s32 {
|
|
empty: []u8
|
|
no_assets: []Bundle.Asset
|
|
measured := Link.ZxExecWrite(program, no_assets, empty, "", true)
|
|
plain := Link.ZxExecWrite(program, no_assets, empty)
|
|
if measured.problem != .None || plain.problem != .None || measured.size <= plain.size {
|
|
return Fail(70, cast(s32)measured.problem)
|
|
}
|
|
bytes: Vec(u8)
|
|
if !VecResizeBounded(bytes, measured.size, measured.size) { return Fail(71) }
|
|
if Link.ZxExecWrite(program, no_assets, bytes[:], "", true).problem != .None { return Fail(72) }
|
|
owner: Link.ZxExecOwner
|
|
read := Link.ZxExecRead(*owner, TextView(bytes[:]), Lir.LirDefaultLimits())
|
|
stripped := Link.ZxExecReadProgram(*owner)
|
|
if read.problem != .None || stripped.origins.count != 0 || stripped.sources.count != 0 {
|
|
return Fail(73, cast(s32)read.problem)
|
|
}
|
|
debug: Debug.ZxDebugOwner
|
|
if Link.ZxExecReadDebug(*owner, *debug, Lir.LirDefaultLimits()) != .None { return Fail(74) }
|
|
restored := Debug.ZxDebugProgram(*debug)
|
|
if restored.sources.count != 1 || restored.sources[0] != program.sources[0] ||
|
|
restored.origins.count != program.origins.count || restored.origins[0].line != 1 ||
|
|
restored.functions[0].origin != 0 ||
|
|
restored.functions[0].blocks[0].instructions[1].origin != 0 { return Fail(75) }
|
|
|
|
// An attachment for other program bytes, or a damaged one, is refused and
|
|
// leaves no restored program.
|
|
attachment: Vec(u8)
|
|
other: [32]u8
|
|
if !Debug.ZxDebugEncode(program, TextView(other[:]), *attachment) { return Fail(76) }
|
|
if Debug.ZxDebugApply(*debug, stripped, TextView(attachment[:]), TextView(owner.identity[:]),
|
|
Lir.LirDefaultLimits()) != .Identity || debug.valid { return Fail(77) }
|
|
VecFree(attachment)
|
|
damaged: Vec(u8)
|
|
if !Debug.ZxDebugEncode(program, TextView(owner.identity[:]), *damaged) { return Fail(78) }
|
|
if Debug.ZxDebugApply(*debug, stripped, TextView(VecSlice(damaged, 0, damaged.count - 1)),
|
|
TextView(owner.identity[:]), Lir.LirDefaultLimits()) != .Format || debug.valid { return Fail(79) }
|
|
damaged[44] = 9
|
|
if Debug.ZxDebugApply(*debug, stripped, TextView(damaged[:]), TextView(owner.identity[:]),
|
|
Lir.LirDefaultLimits()) != .Format { return Fail(80) }
|
|
VecFree(damaged)
|
|
wrong_file := program
|
|
wrong_origins: [1]Lir.LirOrigin = .{program.origins[0]}
|
|
wrong_origins[0].file = 3
|
|
wrong_file.origins = wrong_origins[:]
|
|
inconsistent: Vec(u8)
|
|
if !Debug.ZxDebugEncode(wrong_file, TextView(owner.identity[:]), *inconsistent) { return Fail(81) }
|
|
if Debug.ZxDebugApply(*debug, stripped, TextView(inconsistent[:]), TextView(owner.identity[:]),
|
|
Lir.LirDefaultLimits()) != .Verify || debug.valid { return Fail(82) }
|
|
VecFree(inconsistent)
|
|
Debug.ZxDebugFree(*debug)
|
|
Link.ZxExecReadFree(*owner)
|
|
VecFree(bytes)
|
|
|
|
// Without the attachment there is nothing to restore.
|
|
plain_bytes: Vec(u8)
|
|
if !VecResizeBounded(plain_bytes, plain.size, plain.size) ||
|
|
Link.ZxExecWrite(program, no_assets, plain_bytes[:]).problem != .None { return Fail(83) }
|
|
read = Link.ZxExecRead(*owner, TextView(plain_bytes[:]), Lir.LirDefaultLimits())
|
|
if read.problem != .None || Link.ZxExecReadDebug(*owner, *debug, Lir.LirDefaultLimits()) != .Absent {
|
|
return Fail(84)
|
|
}
|
|
Link.ZxExecReadFree(*owner)
|
|
VecFree(plain_bytes)
|
|
return 0
|
|
}
|
|
|
|
main :: () -> s32 {
|
|
types: [2]Lir.LirType = .{.{kind = .Void}, .{kind = .Integer, bits = 32, is_signed = true}}
|
|
modules: [1]string = .{"executable"}
|
|
sources: [1]string = .{"executable_bundle_cases.zi"}
|
|
origins: [1]Lir.LirOrigin = .{.{file = 0, line = 1, column = 1,
|
|
end_line = 1, end_column = 2, begin = 0, end = 1, parent = -1}}
|
|
constants: [1]Lir.LirConstant = .{.{type = 1, bits = 42}}
|
|
values: [1]s32 = .{1}
|
|
returned: [1]s32 = .{0}
|
|
instructions: [2]Lir.LirInstruction
|
|
instructions[0] = .{op = .Constant, result = 0, symbol = 0, origin = 0}
|
|
instructions[1] = .{op = .Return, result = -1, operands = returned[:], symbol = -1, origin = 0}
|
|
blocks: [1]Lir.LirBlock = .{.{instructions = instructions[:]}}
|
|
functions: [1]Lir.LirFunction = .{.{module = 0, name = "main", result = 1,
|
|
values = values[:], blocks = blocks[:], entry = 0, origin = 0}}
|
|
program := Lir.LirProgram.{version = Lir.LirVersion, modules = modules[:], sources = sources[:],
|
|
types = types[:], constants = constants[:], functions = functions[:], origins = origins[:], entry = 0}
|
|
|
|
asset_bytes: [5]u8 = .{1, 0, 2, 0, 255}
|
|
assets: [1]Bundle.Asset = .{.{name = "data/binary.bin", data = TextView(asset_bytes[:])}}
|
|
empty: []u8
|
|
measured := Link.ZxExecWrite(program, assets[:], empty)
|
|
if measured.problem != .None || measured.size < 256 { return Fail(1, cast(s32)measured.problem) }
|
|
short: [64]u8
|
|
for index: 0..short.count - 1 { short[index] = 165 }
|
|
short_result := Link.ZxExecWrite(program, assets[:], short[:])
|
|
if short_result.problem != .Output || short_result.size != measured.size { return Fail(2) }
|
|
for byte: short { if byte != 165 { return Fail(3) } }
|
|
|
|
first: Vec(u8)
|
|
second: Vec(u8)
|
|
if !VecResizeBounded(first, measured.size, measured.size) ||
|
|
!VecResizeBounded(second, measured.size, measured.size) { return Fail(4) }
|
|
written := Link.ZxExecWrite(program, assets[:], first[:])
|
|
repeated := Link.ZxExecWrite(program, assets[:], second[:])
|
|
if written.problem != .None || repeated.problem != .None || !SameBytes(first[:], second[:]) { return Fail(5) }
|
|
|
|
owner: Link.ZxExecOwner
|
|
read := Link.ZxExecRead(*owner, TextView(first[:]), Lir.LirDefaultLimits())
|
|
if read.problem != .None || !owner.valid || owner.assets != 1 { return Fail(6, cast(s32)read.problem) }
|
|
decoded := Link.ZxExecReadProgram(*owner)
|
|
if decoded.entry != 0 || decoded.sources.count != 0 || decoded.origins.count != 0 ||
|
|
decoded.functions[0].origin != -1 || decoded.functions[0].blocks[0].instructions[0].origin != -1 {
|
|
return Fail(7)
|
|
}
|
|
// The decoded program owns its storage; changing the input after the read
|
|
// cannot change execution.
|
|
first[0] = 0
|
|
plan: Exec.LirExecPlan
|
|
instance: Exec.LirExecInstance
|
|
execution := Exec.LirExecPlanBuild(decoded, Lir.LirDefaultLimits(), *plan)
|
|
if execution.problem == .None {
|
|
execution = Exec.LirExecInstanceOpen(*plan, Exec.LirExecDefaultLimits(), 1, *instance)
|
|
}
|
|
if execution.problem == .None { execution = Exec.LirExecRun(*instance) }
|
|
if execution.problem != .None || execution.value.bits != 42 { return Fail(8, cast(s32)execution.problem) }
|
|
Exec.LirExecInstanceClose(*instance)
|
|
Exec.LirExecPlanFree(*plan)
|
|
Link.ZxExecReadFree(*owner)
|
|
Link.ZxExecReadFree(*owner)
|
|
|
|
mutation := CopyBytes(second[:])
|
|
mutation[4] = 26
|
|
read = Link.ZxExecRead(*owner, TextView(mutation[:]), Lir.LirDefaultLimits())
|
|
if read.problem != .Container || read.container != .Legacy || owner.valid { return Fail(9) }
|
|
VecFree(mutation)
|
|
mutation = CopyBytes(second[:])
|
|
mutation[16] = 2
|
|
read = Link.ZxExecRead(*owner, TextView(mutation[:]), Lir.LirDefaultLimits())
|
|
if read.problem != .Container || read.container != .Feature { return Fail(10) }
|
|
VecFree(mutation)
|
|
mutation = CopyBytes(second[:])
|
|
// Header 64 + three directory entries 96 places the manifest at 160;
|
|
// its first identity starts after eight u32 fields.
|
|
mutation[192] ^= 1
|
|
read = Link.ZxExecRead(*owner, TextView(mutation[:]), Lir.LirDefaultLimits())
|
|
if read.problem != .Identity { return Fail(11, cast(s32)read.problem) }
|
|
VecFree(mutation)
|
|
mutation = CopyBytes(second[:])
|
|
asset_offset := cast(s64)ReadU64(mutation[:], 64 + 2 * 32 + 8)
|
|
mutation[mutation.count - 1] ^= 1
|
|
if asset_offset < 0 || asset_offset >= mutation.count ||
|
|
Link.ZxExecRead(*owner, TextView(mutation[:]), Lir.LirDefaultLimits()).problem != .Identity {
|
|
return Fail(12)
|
|
}
|
|
VecFree(mutation)
|
|
read = Link.ZxExecRead(*owner, TextView(VecSlice(second, 0, second.count - 1)), Lir.LirDefaultLimits())
|
|
if read.problem != .Container || read.container != .Truncated { return Fail(13) }
|
|
mutation = CopyBytes(second[:])
|
|
if !VecResizeBounded(mutation, mutation.count + 1, mutation.count + 1) { return Fail(14) }
|
|
mutation[mutation.count - 1] = 0
|
|
read = Link.ZxExecRead(*owner, TextView(mutation[:]), Lir.LirDefaultLimits())
|
|
if read.problem != .Container || read.container != .Trailing { return Fail(15) }
|
|
Link.ZxExecReadFree(*owner)
|
|
VecFree(mutation)
|
|
VecFree(first)
|
|
VecFree(second)
|
|
budget_result := BudgetManySymbols()
|
|
if budget_result != 0 { return budget_result }
|
|
budget_result = BudgetRepeatedIndirect()
|
|
if budget_result != 0 { return budget_result }
|
|
host_result := HostCapabilities()
|
|
if host_result != 0 { return host_result }
|
|
host_result = ReadOnlyBytes()
|
|
if host_result != 0 { return host_result }
|
|
debug_result := DebugAttachment(program)
|
|
if debug_result != 0 { return debug_result }
|
|
print("executable bundle: deterministic stripped execution, bounded hostile symbols/indirect calls, host capabilities, read-only bytes, debug attachment and mutation local tests passed\n")
|
|
return 0
|
|
}
|