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 } < bool { < 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} operands: [1]Lir.LirAssemblyOperand = .{.{alias = "out", constraint = "=r"}} variant := Lir.LirAssemblyVariant.{architecture = "x86_64", os = "linux", abi = "sysv", template = "movl $42, %[out]", operands = operands[:], outputs = 1} variants: [1]Lir.LirAssemblyVariant = .{variant} functions: [2]Lir.LirFunction = .{ .{module = 0, name = "main", result = 1, values = values[:], blocks = blocks[:], entry = 0, origin = -1, variants = variants[:]}, .{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) } if decoded.functions[0].variants.count != 1 || decoded.functions[0].variants[0].template != variant.template || decoded.functions[0].variants[0].operands[0].alias != "out" { return Fail(59) } // 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) } tampered = CopyBytes(bytes[:]) tampered[176] = 1 read = Link.ZxExecRead(*owner, TextView(tampered[:]), Lir.LirDefaultLimits()) VecFree(tampered) if read.problem != .Variant || owner.valid { return Fail(58, cast(s32)read.problem) } variants[0].template = "movl $73, %[out]" alternate := CopyBytes(bytes[:]) defer VecFree(alternate) if Link.ZxExecWrite(program, no_assets[:], alternate[:]).problem != .None || SameBytes(bytes[192:224], alternate[192:224]) || SameBytes(bytes[256:288], alternate[256:288]) { return Fail(60) } variants[0].template = "movl $42, %[bad]" if Verify.LirVerify(program, Lir.LirDefaultLimits()).problem != .Assembly { return Fail(61) } variants[0] = variant variants[0].features = 0x8000000000000000 if Verify.LirVerify(program, Lir.LirDefaultLimits()).problem != .Assembly { return Fail(62) } variants[0] = variant invalid_operands: [1]Lir.LirAssemblyOperand = .{.{alias = "out", constraint = "=r,"}} variants[0].operands = invalid_operands[:] if Verify.LirVerify(program, Lir.LirDefaultLimits()).problem != .Assembly { return Fail(63) } variants[0] = variant variants[0].clobbers = "memory;;cc" if Verify.LirVerify(program, Lir.LirDefaultLimits()).problem != .Assembly { return Fail(64) } variants[0] = variant functions[0].assembly_required = true if Verify.LirVerify(program, Lir.LirDefaultLimits()).problem != .Assembly { return Fail(65) } functions[0].assembly_required = false // 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) // Kind 4 is a reserved number, not a second assembly implementation. // Replace the fourth directory entry (the optional debug attachment). bytes[160] = 4 for index: 1..3 { bytes[160 + index] = 0 } read = Link.ZxExecRead(*owner, TextView(bytes[:]), Lir.LirDefaultLimits()) if read.problem != .None || Link.ZxExecReadDebug(*owner, *debug, Lir.LirDefaultLimits()) != .Absent { return Fail(103, cast(s32)read.problem) } Link.ZxExecReadFree(*owner) bytes[164] = 1 read = Link.ZxExecRead(*owner, TextView(bytes[:]), Lir.LirDefaultLimits()) if read.problem != .Container || read.container != .RequiredSection || owner.valid { return Fail(104, cast(s32)read.container) } 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 = .{ "3063af6bc0a446f150795e06230f66041cdd2ed9115804a754d346dcd993eb74", "6a02a66615e06c66dd95d33e5ef86f197de511641ecdf7c2f3e7008cc0fd6e21", "34b6c58ae5d38a4bfb6d93e226b51d4561ec4b33a23fec9fb26879c7788f8533", "9b9dee13151ab0b2888fe218a5b5a0e6b8a2bfd9f2667b6b370f8035efa2eabe", "218a44cd1258dd07b71d8bb46376b8239ff95d4adfdb94a1f33e7e5c5ccf0be4", "3e9ae3638649e496ae720439a3aea8b63127770718f0f7deb300607721cbd279", "27835730df5abdea0caf59a1a50d6b1b33f46190b65406e37a22f804001166d6", "03e1c5398b0658a2b87bfebde25ef699bfc0851d863179151123d33ef5efdb6c", "c7779cb6f2c32c145d95a71ecdc19bb21a9a6f3d544cb695c0b1fb6a50f97c4c", "e1e024109bd50d6efb528d9f5da376271159b71782929b96f15d673c5ed9db2c", "777eb4ad0dcb2ed4723575ee86faebf5944de7f5184fc3477d74ec9b8ee354d9", "6d4a2bcdb6c4e8326d4886d2f0f836fd2e4d519823bac53884125a7137ea9ff4", "a731da8f8d68c86cfb94fb39f41bd54115594d67ce02c2f2ab4095dbec4ddd70", "0277ac2b472d19ae6867965639d8467b1e120a2a9964f7f4e64168cc24e83770", "5431c6de6b89036b265f68ddc88defb55cd85984dc00d854322b54011b8e9a9b", "2cae9a74e30b43184c1107416e65b7fa46d4162fee2adce4e5d0e5c4804693dc", "742acd72f6cf1abf6a470eb914ad9ebe1f835cf2c4a0ff20b2ed2cc2cbf0cb5c"} 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 != 29 || 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 }