-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcall-contexts.test.ts
More file actions
406 lines (331 loc) · 10.3 KB
/
call-contexts.test.ts
File metadata and controls
406 lines (331 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import { describe, it, expect } from "vitest";
import { compile } from "#compiler";
import type * as Format from "@ethdebug/format";
import { Pointer, Program } from "@ethdebug/format";
const { Context } = Program;
const { Invocation } = Context.Invoke;
type InternalCall = Format.Program.Context.Invoke.Invocation.InternalCall;
/**
* Compile a BUG source and return the runtime program
*/
async function compileProgram(source: string): Promise<Format.Program> {
const result = await compile({
to: "bytecode",
source,
});
if (!result.success) {
const errors = result.messages.error ?? [];
const msgs = errors
.map((e: { message?: string }) => e.message ?? String(e))
.join("\n");
throw new Error(`Compilation failed:\n${msgs}`);
}
return result.value.bytecode.runtimeProgram;
}
/**
* Find instructions with a given mnemonic whose context
* satisfies a type guard
*/
function findInstructionsWithContext<C extends Format.Program.Context>(
program: Format.Program,
mnemonic: string,
guard: (value: unknown) => value is C,
): (Format.Program.Instruction & { context: C })[] {
return program.instructions.filter(
(
instr,
): instr is Format.Program.Instruction & {
context: C;
} => instr.operation?.mnemonic === mnemonic && guard(instr.context),
);
}
describe("function call debug contexts", () => {
const source = `name CallContextTest;
define {
function add(a: uint256, b: uint256) -> uint256 {
return a + b;
};
}
storage {
[0] result: uint256;
}
create {
result = 0;
}
code {
result = add(10, 20);
}`;
it(
"should emit invoke context on caller JUMP " +
"(identity + code target, no args)",
async () => {
const program = await compileProgram(source);
const invokeJumps = findInstructionsWithContext(
program,
"JUMP",
Context.isInvoke,
);
expect(invokeJumps.length).toBeGreaterThanOrEqual(1);
const { invoke } = invokeJumps[0].context;
expect(Invocation.isInternalCall(invoke)).toBe(true);
const call = invoke as InternalCall;
expect(call.jump).toBe(true);
expect(call.identifier).toBe("add");
// Should have declaration source range
expect(invoke.declaration).toBeDefined();
expect(invoke.declaration!.source).toEqual({ id: "0" });
expect(invoke.declaration!.range).toBeDefined();
expect(typeof invoke.declaration!.range!.offset).toBe("number");
expect(typeof invoke.declaration!.range!.length).toBe("number");
// Target should be a code pointer (not stack)
expect(Pointer.Region.isCode(call.target.pointer)).toBe(true);
// Caller JUMP should NOT have argument pointers
// (args live on the callee JUMPDEST invoke context)
expect(call.arguments).toBeUndefined();
},
);
it("should emit return context on continuation JUMPDEST", async () => {
const program = await compileProgram(source);
const returnJumpdests = findInstructionsWithContext(
program,
"JUMPDEST",
Context.isReturn,
);
expect(returnJumpdests.length).toBeGreaterThanOrEqual(1);
const { return: ret } = returnJumpdests[0].context;
expect(ret.identifier).toBe("add");
// Should have declaration source range
expect(ret.declaration).toBeDefined();
expect(ret.declaration!.source).toEqual({ id: "0" });
// Should have data pointer to return value at
// TOS (stack slot 0)
expect(ret.data.pointer).toEqual({
location: "stack",
slot: 0,
});
});
it(
"should emit invoke context on callee entry " +
"JUMPDEST with args and code target",
async () => {
const program = await compileProgram(source);
// The callee entry point, not the continuation
const invokeJumpdests = findInstructionsWithContext(
program,
"JUMPDEST",
Context.isInvoke,
);
expect(invokeJumpdests.length).toBeGreaterThanOrEqual(1);
const { invoke } = invokeJumpdests[0].context;
expect(Invocation.isInternalCall(invoke)).toBe(true);
const call = invoke as InternalCall;
expect(call.jump).toBe(true);
expect(call.identifier).toBe("add");
// Target should be a code pointer
expect(Pointer.Region.isCode(call.target.pointer)).toBe(true);
// Should have argument pointers matching
// function parameters
expect(call.arguments).toBeDefined();
const group = (call.arguments!.pointer as { group: unknown[] }).group;
expect(group).toHaveLength(2);
// First arg (a) is deepest on stack
expect(group[0]).toEqual({
name: "a",
location: "stack",
slot: 1,
});
// Second arg (b) is on top
expect(group[1]).toEqual({
name: "b",
location: "stack",
slot: 0,
});
},
);
it("should emit contexts in correct instruction order", async () => {
const program = await compileProgram(source);
// The caller JUMP should come before the
// continuation JUMPDEST
const invokeJump = findInstructionsWithContext(
program,
"JUMP",
Context.isInvoke,
)[0];
const returnJumpdest = findInstructionsWithContext(
program,
"JUMPDEST",
Context.isReturn,
)[0];
expect(invokeJump).toBeDefined();
expect(returnJumpdest).toBeDefined();
// Invoke JUMP offset should be less than
// return JUMPDEST offset (caller comes first
// in bytecode)
expect(Number(invokeJump.offset)).toBeLessThan(
Number(returnJumpdest.offset),
);
});
describe("void function calls", () => {
const voidSource = `name VoidCallTest;
define {
function setVal(
s: uint256, v: uint256
) -> uint256 {
return v;
};
}
storage {
[0] result: uint256;
}
create {
result = 0;
}
code {
result = setVal(0, 42);
}`;
it(
"should emit return context with data pointer " +
"for value-returning functions",
async () => {
const program = await compileProgram(voidSource);
const returnJumpdests = findInstructionsWithContext(
program,
"JUMPDEST",
Context.isReturn,
);
expect(returnJumpdests.length).toBeGreaterThanOrEqual(1);
const { return: ret } = returnJumpdests[0].context;
expect(ret.identifier).toBe("setVal");
// Since setVal returns a value, data should
// be present
expect(ret.data).toBeDefined();
},
);
});
describe("nested function calls", () => {
const nestedSource = `name NestedCallTest;
define {
function add(
a: uint256, b: uint256
) -> uint256 {
return a + b;
};
function addThree(
x: uint256, y: uint256, z: uint256
) -> uint256 {
let sum1 = add(x, y);
let sum2 = add(sum1, z);
return sum2;
};
}
storage {
[0] result: uint256;
}
create {
result = 0;
}
code {
result = addThree(1, 2, 3);
}`;
it("should emit invoke/return contexts for " + "nested calls", async () => {
const program = await compileProgram(nestedSource);
// Should have invoke contexts for:
// 1. main -> addThree
// 2. addThree -> add (first call)
// 3. addThree -> add (second call)
// Plus callee entry JUMPDESTs
const invokeJumps = findInstructionsWithContext(
program,
"JUMP",
Context.isInvoke,
);
// At least 3 invoke JUMPs (main->addThree,
// addThree->add x2)
expect(invokeJumps.length).toBeGreaterThanOrEqual(3);
// Check we have invokes for both functions
const invokeIds = invokeJumps.map(
(instr) => instr.context.invoke.identifier,
);
expect(invokeIds).toContain("addThree");
expect(invokeIds).toContain("add");
// Should have return contexts for all
// continuation points
const returnJumpdests = findInstructionsWithContext(
program,
"JUMPDEST",
Context.isReturn,
);
expect(returnJumpdests.length).toBeGreaterThanOrEqual(3);
});
});
describe("single-arg function", () => {
const singleArgSource = `name SingleArgTest;
define {
function double(x: uint256) -> uint256 {
return x + x;
};
}
storage {
[0] result: uint256;
}
create {
result = 0;
}
code {
result = double(7);
}`;
it(
"should emit single-element argument group " + "on callee JUMPDEST",
async () => {
const program = await compileProgram(singleArgSource);
// Args are on the callee JUMPDEST, not the
// caller JUMP
const invokeJumpdests = findInstructionsWithContext(
program,
"JUMPDEST",
Context.isInvoke,
);
expect(invokeJumpdests.length).toBeGreaterThanOrEqual(1);
const { invoke } = invokeJumpdests[0].context;
expect(Invocation.isInternalCall(invoke)).toBe(true);
const call = invoke as InternalCall;
expect(call.arguments).toBeDefined();
const group = (call.arguments!.pointer as { group: unknown[] }).group;
// Single arg at stack slot 0
expect(group).toHaveLength(1);
expect(group[0]).toEqual({
name: "x",
location: "stack",
slot: 0,
});
},
);
});
describe("return epilogue source maps", () => {
it(
"should map return epilogue instructions " + "to source location",
async () => {
const program = await compileProgram(source);
// The return epilogue is PUSH/MLOAD/JUMP inside
// user functions. Find JUMP instructions that are
// NOT invoke contexts — these are return jumps.
const returnJumps = program.instructions.filter(
(instr) =>
instr.operation?.mnemonic === "JUMP" &&
!Context.isInvoke(instr.context),
);
// Should have at least one return JUMP (from add)
expect(returnJumps.length).toBeGreaterThanOrEqual(1);
// The return JUMP should have a code context with
// source location (not just a remark)
const returnJump = returnJumps[0];
expect(returnJump.context).toBeDefined();
const ctx = returnJump.context as Record<string, unknown>;
expect(ctx.code).toBeDefined();
const code = ctx.code as Record<string, unknown>;
expect(code.source).toEqual({ id: "0" });
expect(code.range).toBeDefined();
},
);
});
});