|
| 1 | +/** |
| 2 | + * The call-site source range is carried on the invoke/return |
| 3 | + * contexts of a function call, so a debugger stepping onto the |
| 4 | + * caller JUMP or the continuation JUMPDEST maps back to the call |
| 5 | + * expression (not just the callee's definition). |
| 6 | + */ |
| 7 | +import { describe, it, expect } from "vitest"; |
| 8 | + |
| 9 | +import { compile } from "#compiler"; |
| 10 | +import { executeProgram } from "#test/evm/behavioral"; |
| 11 | +import { Program } from "@ethdebug/format"; |
| 12 | +import type * as Format from "@ethdebug/format"; |
| 13 | + |
| 14 | +const { Context } = Program; |
| 15 | + |
| 16 | +const source = `name Adder; |
| 17 | +define { |
| 18 | + function add(a: uint256, b: uint256) -> uint256 { return a + b; }; |
| 19 | +} |
| 20 | +storage { [0] r: uint256; } |
| 21 | +create { r = 0; } |
| 22 | +code { r = add(3, 4); }`; |
| 23 | + |
| 24 | +async function runtimeProgram(): Promise<Format.Program> { |
| 25 | + const result = await compile({ |
| 26 | + to: "bytecode", |
| 27 | + source, |
| 28 | + optimizer: { level: 0 }, |
| 29 | + }); |
| 30 | + if (!result.success) throw new Error("compile failed"); |
| 31 | + return result.value.bytecode.runtimeProgram; |
| 32 | +} |
| 33 | + |
| 34 | +function hasCodeRange(ctx: Record<string, unknown>): boolean { |
| 35 | + const code = ctx.code as { range?: { offset?: unknown; length?: unknown } }; |
| 36 | + return ( |
| 37 | + !!code && |
| 38 | + typeof code.range?.offset === "number" && |
| 39 | + typeof code.range?.length === "number" |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +describe("call-site source range on invoke/return contexts", () => { |
| 44 | + it("the caller JUMP invoke context carries a call-site code range", async () => { |
| 45 | + const program = await runtimeProgram(); |
| 46 | + const invokeJump = program.instructions.find( |
| 47 | + (i) => |
| 48 | + i.operation?.mnemonic === "JUMP" && |
| 49 | + i.context !== undefined && |
| 50 | + Context.isInvoke(i.context), |
| 51 | + ); |
| 52 | + expect(invokeJump, "invoke JUMP").toBeDefined(); |
| 53 | + expect(hasCodeRange(invokeJump!.context as Record<string, unknown>)).toBe( |
| 54 | + true, |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it("the continuation JUMPDEST return context carries a call-site code range", async () => { |
| 59 | + const program = await runtimeProgram(); |
| 60 | + const contJumpdest = program.instructions.find( |
| 61 | + (i) => |
| 62 | + i.operation?.mnemonic === "JUMPDEST" && |
| 63 | + i.context !== undefined && |
| 64 | + Context.isReturn(i.context), |
| 65 | + ); |
| 66 | + expect(contJumpdest, "continuation JUMPDEST").toBeDefined(); |
| 67 | + expect(hasCodeRange(contJumpdest!.context as Record<string, unknown>)).toBe( |
| 68 | + true, |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it("still identifies the invoke by name and keeps behavior", async () => { |
| 73 | + const program = await runtimeProgram(); |
| 74 | + const invokeJump = program.instructions.find( |
| 75 | + (i) => |
| 76 | + i.operation?.mnemonic === "JUMP" && |
| 77 | + i.context !== undefined && |
| 78 | + Context.isInvoke(i.context), |
| 79 | + ); |
| 80 | + const ctx = invokeJump!.context as Format.Program.Context.Invoke; |
| 81 | + expect(ctx.invoke.identifier).toBe("add"); |
| 82 | + |
| 83 | + const res = await executeProgram(source, { |
| 84 | + calldata: "", |
| 85 | + optimizationLevel: 0, |
| 86 | + }); |
| 87 | + expect(res.callSuccess).toBe(true); |
| 88 | + expect(await res.getStorage(0n)).toBe(7n); |
| 89 | + }); |
| 90 | +}); |
0 commit comments