|
| 1 | +import assert from "node:assert/strict"; |
| 2 | + |
| 3 | +/** Verify decoded return evidence and distinct non-return flows on owned fixtures. */ |
| 4 | +export async function verifyGhidraInstructionFlow(call, names, cli) { |
| 5 | + const leaf = names.find((item) => |
| 6 | + item.value.endsWith("rea_ghidra_inventory_leaf"), |
| 7 | + ); |
| 8 | + assert.ok(leaf, "Source fixture lacks its returning leaf"); |
| 9 | + const leafInstructions = await inspectFunction(call, leaf.address); |
| 10 | + const returned = leafInstructions.find( |
| 11 | + (instruction) => instruction.flow.kind === "return", |
| 12 | + ); |
| 13 | + assert.ok(returned, "Returning source leaf lacks decoded RETURN evidence"); |
| 14 | + assertReturnEvidence(returned); |
| 15 | + assert.equal( |
| 16 | + (await call("resolve_native_call_targets", { address: returned.address })) |
| 17 | + .status, |
| 18 | + "not-call", |
| 19 | + ); |
| 20 | + assert.deepEqual( |
| 21 | + await cli("inspect-native-instruction", returned.address), |
| 22 | + returned, |
| 23 | + ); |
| 24 | + |
| 25 | + const controls = [ |
| 26 | + ["rea_ghidra_flow_return", "RET", "c3"], |
| 27 | + ["rea_ghidra_flow_cleanup_return", "RET", "c21000"], |
| 28 | + ["rea_ghidra_flow_shared_tail", "JMP", null], |
| 29 | + ["rea_ghidra_flow_trap", "UD2", "0f0b"], |
| 30 | + ]; |
| 31 | + let checked = 0; |
| 32 | + for (const [symbol, mnemonic, bytes] of controls) { |
| 33 | + const procedure = names.find((item) => item.value === symbol); |
| 34 | + if (procedure === undefined) continue; |
| 35 | + const before = await call("resolve_containing_procedure", { |
| 36 | + address: procedure.address, |
| 37 | + }); |
| 38 | + const instructions = await inspectFunction(call, procedure.address); |
| 39 | + const instruction = instructions.find((item) => item.mnemonic === mnemonic); |
| 40 | + assert.ok(instruction, `Source fixture lacks ${symbol}'s ${mnemonic}`); |
| 41 | + if (bytes !== null) assert.equal(instruction.bytes, bytes); |
| 42 | + if (mnemonic === "RET") { |
| 43 | + assertReturnEvidence(instruction); |
| 44 | + assert.equal(instruction.flow.kind, "return"); |
| 45 | + if (bytes === "c21000") |
| 46 | + assert.equal(instruction.operands[0]?.raw, "0x10"); |
| 47 | + } else { |
| 48 | + assert.notEqual(instruction.flow.kind, "return"); |
| 49 | + assert.ok( |
| 50 | + instruction.flow.classification_evidence.some( |
| 51 | + (item) => item.source === "ghidra-listing-flow-type", |
| 52 | + ), |
| 53 | + ); |
| 54 | + } |
| 55 | + assert.deepEqual( |
| 56 | + await call("resolve_containing_procedure", { |
| 57 | + address: procedure.address, |
| 58 | + }), |
| 59 | + before, |
| 60 | + ); |
| 61 | + checked++; |
| 62 | + } |
| 63 | + const unavailable = await call("inspect_native_instruction", { |
| 64 | + address: "0x0", |
| 65 | + }); |
| 66 | + assert.equal(unavailable.status, "outside-memory"); |
| 67 | + assert.equal(unavailable.flow.kind, "unavailable"); |
| 68 | + assert.deepEqual(unavailable.flow.classification_evidence, []); |
| 69 | + return { |
| 70 | + return_instruction: returned.address, |
| 71 | + decoded_pcode_return: true, |
| 72 | + return_cli_mcp_parity: true, |
| 73 | + explicit_x86_elf_controls: checked, |
| 74 | + explicit_control_scope: |
| 75 | + checked === 0 |
| 76 | + ? "Not present on this host-native fixture format/architecture" |
| 77 | + : "x86-64 ELF", |
| 78 | + generic_unknown_preserved: true, |
| 79 | + }; |
| 80 | +} |
| 81 | + |
| 82 | +async function inspectFunction(call, procedure) { |
| 83 | + const listing = await call("read_function_instructions", { procedure }); |
| 84 | + const instructions = []; |
| 85 | + for (const line of listing.instructions) { |
| 86 | + const delimiter = line.indexOf(": "); |
| 87 | + assert.ok(delimiter > 0, `Malformed provider instruction listing: ${line}`); |
| 88 | + instructions.push( |
| 89 | + await call("inspect_native_instruction", { |
| 90 | + address: line.slice(0, delimiter), |
| 91 | + }), |
| 92 | + ); |
| 93 | + } |
| 94 | + return instructions; |
| 95 | +} |
| 96 | + |
| 97 | +function assertReturnEvidence(instruction) { |
| 98 | + assert.equal(instruction.status, "decoded"); |
| 99 | + assert.ok( |
| 100 | + instruction.flow.classification_evidence.some( |
| 101 | + (item) => |
| 102 | + item.source === "ghidra-instruction-pcode" && item.value === "RETURN", |
| 103 | + ), |
| 104 | + ); |
| 105 | + assert.ok( |
| 106 | + !instruction.limitations.some((text) => |
| 107 | + text.includes("not classified as a return"), |
| 108 | + ), |
| 109 | + ); |
| 110 | +} |
0 commit comments