Files
ziran/tests/executable_bundle_cases.zi
waozi 2cafe84dc8
Alpha check / check (push) Successful in 10m8s
Integrate verified ZX modules and compiler improvements
Add typed separately compiled ZX libraries with independent instances and compatible state replacement. Tighten generated-name diagnostics, remove the fixed loader graph limit, and resolve platform conditions without eager enum-import cycles.

Resolve known function calls, bound specialization work, improve loop inlining, and copy borrowed Wasm records safely across overlapping pointers. Retain measured native vector growth and allocated-state verification improvements with startup phase profiling. Include focused regression coverage, synchronized bootstrap and site documentation, Lean research plans, and raw Alpha comparison receipts with rejected prototypes clearly identified.

Local validation: all 273 retained tests passed across the full run (272/273) and the audio rerun after refreshing stale disposable CMake compiler paths (1/1), with no implementation drift. Linux, Darwin and SIMD bootstrap seeds match. Current-tree Omega installation verified all 11 binary identities and ran Hello World through the installed compiler.

Alpha matched measurements support smaller Wasm modules; the native vector workload improves 4.62%, while printing is 1.41% slower. Separate application, controller and Moto integration remains outside this compiler milestone.
2026-11-19 00:01:23 -03:00

654 lines
34 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
}
// Independent hashlib SHA-256 vectors for the on-disk asset identity.
// The 40-byte prefix puts payload lengths 15/16/17 at SHA padding's 55/56/57
// boundary and 23/24/25 at the compression boundary. The larger inputs
// exercise direct blocks, remainders, and binary bytes through the reader.
DigestBoundaries :: (program: Lir.LirProgram) -> s32 {
sizes: [17]s64 = .{0, 1, 15, 16, 17, 23, 24, 25, 55, 56, 63, 64, 65, 127, 128, 129, 1048577}
expected: [17]string = .{
"6aff20131165b38c5d6fa435d77c6718794cc0e0aebb7130c0c6fc6422fe2093",
"3dbe104423e87dabdc8a093c5d9dc94dc5d931a8db7a0cf6c2abd0ba24b65cbb",
"9220f71ee4398abf1f41eea73168eebb1bfa1dca18ac08d10305c4034a3d5f4b",
"5a39bd30627cd0e198c7af27e161f34ea703145601f15382def91e73b376a0dc",
"968678ca2de935cd0a0dc55c2550a3ff4142d3d7f722b40e34f9f35c59a52636",
"8107b676d0068f7db143057a221b37b7da88d1fb1c2a58d83ac1775d76b4379b",
"eaf2033420b2bda12634de5b1d24d88eacfe2d1c288aba250a8816832d5e7b98",
"f9efbe65b18759715591d42a26791ec88f4307b2adea9c91fe64ca643d869f04",
"d68a9f4bda9b28926fdf9ce065283817a95a603cb4b746c53adf6a6df409a70c",
"ba1f303d8edd36c9be1ce93296701e8069d2dc67bcc0e720d380c408106ec564",
"5d43818c44f87852132b086027952507cc7fc0a234e536382538b02367b8598b",
"9e79413d20b4b9c5b65fe4b2a80ce833cb6cb98adb6ec5b9e2a3ed8dbfee5cbf",
"79fab954c45f9870c8c3791433e21082ac592118007e7fd4ff48d3f199097819",
"46a600a628f0e5e3f172d55496c5ae1efa95006f15b73d9d1ddbe1eb26daf04d",
"18ca8f87bdd2ee883914360fa14448d264193381056cf11498b841222b00a4ea",
"df8d59b9da1f1bb8e507beb95b25006423156afcefb4f6387a9459b37ac46bdc",
"7489cb52ff08e4732b830aa3fb82a9c1f09d985c0e76dc95cc9230cd64e5c202"}
payload: Vec(u8)
if !VecResize(payload, sizes[sizes.count - 1]) { return Fail(85) }
defer VecFree(payload)
for index: 0..payload.count - 1 { payload[index] = cast(u8)(index * 37 + 11) }
digits := "0123456789abcdef"
for index: 0..sizes.count - 1 {
assets: [1]Bundle.Asset = .{.{name = "data/payload.bin", data = TextView(VecSlice(payload, 0, sizes[index]))}}
empty: []u8
measured := Link.ZxExecWrite(program, assets[:], empty)
bytes: Vec(u8)
if measured.problem != .None || !VecResize(bytes, measured.size) ||
Link.ZxExecWrite(program, assets[:], bytes[:]).problem != .None { return Fail(86) }
defer VecFree(bytes)
decoded_assets: [1]Bundle.Asset
layout := Bundle.ExecutableDecode(TextView(bytes[:]), decoded_assets[:])
if layout.problem != .None || layout.assets_identity.count != 32 { return Fail(87) }
for byte: 0..31 {
value := layout.assets_identity[byte]
if digits[value >> 4] != expected[index][byte * 2] ||
digits[value & 15] != expected[index][byte * 2 + 1] { return Fail(88, cast(s32)index) }
}
owner: Link.ZxExecOwner
read := Link.ZxExecRead(*owner, TextView(bytes[:]), Lir.LirDefaultLimits())
Link.ZxExecReadFree(*owner)
if read.problem != .None { return Fail(89, cast(s32)read.problem) }
if sizes[index] > 0 {
bytes[bytes.count - 1] ^= 1
read = Link.ZxExecRead(*owner, TextView(bytes[:]), Lir.LirDefaultLimits())
Link.ZxExecReadFree(*owner)
if read.problem != .Identity { return Fail(90, cast(s32)read.problem) }
}
}
return 0
}
ModuleLinkage :: (program: Lir.LirProgram) -> s32 {
empty: []u8
no_assets: []Bundle.Asset
linkage := "M\x00L"
interface := "entry\t\ts32\n"
limits := Lir.LirDefaultLimits()
missing_interface := Link.ZxExecWriteModule(program, no_assets, empty, "", false, linkage)
if missing_interface.problem != .Container || missing_interface.container != .Section { return Fail(91) }
legacy := Link.ZxExecWrite(program, no_assets, empty, interface, true)
absent := Link.ZxExecWriteModule(program, no_assets, empty, interface, true, "")
app: Vec(u8)
app_from_module_writer: Vec(u8)
defer VecFree(app)
defer VecFree(app_from_module_writer)
if legacy.problem != .None || absent.problem != .None || legacy.size != absent.size ||
!VecResize(app, legacy.size) || !VecResize(app_from_module_writer, absent.size) ||
Link.ZxExecWrite(program, no_assets, app[:], interface, true).problem != .None ||
Link.ZxExecWriteModule(program, no_assets, app_from_module_writer[:], interface, true, "").problem != .None ||
!SameBytes(app[:], app_from_module_writer[:]) || ReadU64(app[:], 16) != 1 { return Fail(92) }
measured := Link.ZxExecWriteModule(program, no_assets, empty, interface, false, linkage)
bytes: Vec(u8)
defer VecFree(bytes)
if measured.problem != .None || !VecResize(bytes, measured.size) ||
Link.ZxExecWriteModule(program, no_assets, bytes[:], interface, false, linkage).problem != .None {
return Fail(93, cast(s32)measured.problem)
}
layout := Bundle.ExecutableDecode(TextView(bytes[:]), no_assets)
module_dir := 64 + 3 * 32
interface_dir := module_dir + 32
if layout.problem != .None || layout.found_version != 28 || layout.required_features != 3 ||
layout.module_linkage != linkage || layout.interface != interface || layout.optional_sections != 1 ||
ReadU64(bytes[:], module_dir) != ((cast(u64)1 << 32) | 5) ||
ReadU64(bytes[:], module_dir + 16) != cast(u64)linkage.count { return Fail(94) }
module_offset := cast(s64)ReadU64(bytes[:], module_dir + 8)
interface_offset := cast(s64)ReadU64(bytes[:], interface_dir + 8)
// Moving a byte across the linkage/interface boundary keeps their
// concatenation identical but changes the combined identity.
if !VecResize(app_from_module_writer, measured.size) { return Fail(103) }
reframed := Link.ZxExecWriteModule(program, no_assets, app_from_module_writer[:],
"ntry\t\ts32\n", false, "M\x00Le")
if reframed.problem != .None { return Fail(103, cast(s32)reframed.problem) }
other := Bundle.ExecutableDecode(TextView(VecSlice(app_from_module_writer, 0, reframed.size)), no_assets)
if other.problem != .None || other.program_identity != layout.program_identity ||
other.assets_identity != layout.assets_identity || other.content_identity == layout.content_identity {
return Fail(104)
}
owner: Link.ZxExecOwner
defer Link.ZxExecReadFree(*owner)
read := Link.ZxExecRead(*owner, TextView(bytes[:]), limits)
if read.problem != .None || Link.ZxExecReadModuleLinkage(*owner) != linkage { return Fail(95) }
bytes[module_offset + 1] ^= 1
if Link.ZxExecReadModuleLinkage(*owner) != linkage { return Fail(96) }
read = Link.ZxExecRead(*owner, TextView(bytes[:]), limits)
if read.problem != .Identity || owner.valid || Link.ZxExecReadModuleLinkage(*owner) != "" { return Fail(97) }
bytes[module_offset + 1] ^= 1
bytes[interface_offset] ^= 1
read = Link.ZxExecRead(*owner, TextView(bytes[:]), limits)
if read.problem != .Identity || owner.valid || Link.ZxExecReadModuleLinkage(*owner) != "" { return Fail(98) }
bytes[interface_offset] ^= 1
read = Link.ZxExecRead(*owner, TextView(app[:]), limits)
if read.problem != .None || Link.ZxExecReadModuleLinkage(*owner) != "" { return Fail(99) }
app[16] = 3 // Advertise linkage without section 5.
read = Link.ZxExecRead(*owner, TextView(app[:]), limits)
if read.problem != .Container || read.container != .Section || owner.valid { return Fail(100) }
// Remove the feature bit, required flag, linkage length or interface kind.
invalid_fields: [4]s64 = .{16, module_dir + 4, module_dir + 16, interface_dir}
replacements: [4]u8 = .{1, 0, 0, 4}
for index: 0..invalid_fields.count - 1 {
at := invalid_fields[index]
original := bytes[at]
bytes[at] = replacements[index]
read = Link.ZxExecRead(*owner, TextView(bytes[:]), limits)
if read.problem != .Container || read.container != .Section || owner.valid ||
Link.ZxExecReadModuleLinkage(*owner) != "" { return Fail(101, cast(s32)index) }
bytes[at] = original
}
limits.bytes = bytes.count - 1
read = Link.ZxExecRead(*owner, TextView(bytes[:]), limits)
if read.problem != .Limit || owner.valid { return Fail(102) }
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}
digest_result := DigestBoundaries(program)
if digest_result != 0 { return digest_result }
module_result := ModuleLinkage(program)
if module_result != 0 { return module_result }
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, module linkage and mutation local tests passed\n")
return 0
}