-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathevaluate.test.ts
More file actions
309 lines (259 loc) · 8.36 KB
/
evaluate.test.ts
File metadata and controls
309 lines (259 loc) · 8.36 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
import { expect, describe, it, beforeEach } from "vitest";
import { keccak256 } from "ethereum-cryptography/keccak";
import { toHex } from "ethereum-cryptography/utils";
import { Pointer } from "@ethdebug/format";
import { Machine } from "#machine";
import { Data } from "#data";
import { Cursor } from "#cursor";
import { evaluate, type EvaluateOptions } from "./evaluate.js";
// Create a stub for the Machine.State interface
const state: Machine.State = {
traceIndex: Promise.resolve(0n),
opcode: Promise.resolve("PUSH1"),
programCounter: Promise.resolve(10n),
stack: {
length: 50n,
} as any,
memory: {} as any,
storage: {} as any,
calldata: {} as any,
returndata: {} as any,
transient: {} as any,
code: {} as any,
};
describe("evaluate", () => {
let regions: { [identifier: string]: Cursor.Region };
let variables: { [identifier: string]: Data };
let _cursor: Cursor;
let options: EvaluateOptions;
beforeEach(() => {
variables = {
foo: Data.fromNumber(42),
bar: Data.fromHex("0x1f"),
};
regions = {
stack: {
name: "stack",
location: "stack",
slot: Data.fromNumber(42),
offset: Data.fromNumber(0x60),
length: Data.fromNumber(0x1f / 2),
},
memory: {
name: "memory",
location: "memory",
offset: Data.fromNumber(0x20 * 0x05),
length: Data.fromNumber(42 - 0x1f),
},
};
options = {
state,
variables,
regions,
};
});
it("evaluates literal expressions", async () => {
expect(await evaluate(42, options)).toEqual(Data.fromNumber(42));
expect(await evaluate("0x1f", options)).toEqual(Data.fromHex("0x1f"));
});
it("evaluates constant expressions", async () => {
expect(await evaluate("$wordsize", options)).toEqual(Data.fromHex("0x20"));
});
it("evaluates variable expressions", async () => {
expect(await evaluate("foo", options)).toEqual(Data.fromNumber(42));
expect(await evaluate("bar", options)).toEqual(Data.fromHex("0x1f"));
});
it("evaluates sum expressions", async () => {
const expression: Pointer.Expression = {
$sum: [42, "0x1f", "foo", "bar"],
};
expect(await evaluate(expression, options)).toEqual(
Data.fromUint(42n + 0x1fn + 42n + 0x1fn),
);
});
it("evaluates difference expressions", async () => {
const expression: Pointer.Expression = {
$difference: ["foo", "bar"],
};
expect(await evaluate(expression, options)).toEqual(
Data.fromUint(42n - 0x1fn),
);
});
it("evaluates product expressions", async () => {
const expression: Pointer.Expression = {
$product: [42, "0x1f", "foo", "bar"],
};
expect(await evaluate(expression, options)).toEqual(
Data.fromUint(42n * 0x1fn * 42n * 0x1fn),
);
});
it("evaluates quotient expressions", async () => {
const expression: Pointer.Expression = {
$quotient: ["foo", "bar"],
};
expect(await evaluate(expression, options)).toEqual(
Data.fromUint(42n / 0x1fn),
);
});
it("evaluates remainder expressions", async () => {
const expression: Pointer.Expression = {
$remainder: ["foo", "bar"],
};
expect(await evaluate(expression, options)).toEqual(
Data.fromUint(42n % 0x1fn),
);
});
describe("evaluates concat expressions", () => {
it("concatenates hex literals", async () => {
const expression: Pointer.Expression = {
$concat: ["0x00", "0x00"],
};
expect(await evaluate(expression, options)).toEqual(
Data.fromHex("0x0000"),
);
});
it("concatenates multiple values preserving byte widths", async () => {
const expression: Pointer.Expression = {
$concat: ["0xdead", "0xbeef"],
};
expect(await evaluate(expression, options)).toEqual(
Data.fromHex("0xdeadbeef"),
);
});
it("returns empty data for empty operand list", async () => {
const expression: Pointer.Expression = {
$concat: [],
};
expect(await evaluate(expression, options)).toEqual(Data.zero());
});
it("preserves single operand unchanged", async () => {
const expression: Pointer.Expression = {
$concat: ["0xabcdef"],
};
expect(await evaluate(expression, options)).toEqual(
Data.fromHex("0xabcdef"),
);
});
it("concatenates variables", async () => {
const expression: Pointer.Expression = {
$concat: ["foo", "bar"],
};
// foo = 0x2a (42), bar = 0x1f
expect(await evaluate(expression, options)).toEqual(
Data.fromHex("0x2a1f"),
);
});
it("concatenates nested expressions", async () => {
const expression: Pointer.Expression = {
$concat: [
{ $sum: [1, 2] }, // 3 = 0x03
"0xff",
],
};
expect(await evaluate(expression, options)).toEqual(
Data.fromHex("0x03ff"),
);
});
it("preserves leading zeros in hex literals", async () => {
const expression: Pointer.Expression = {
$concat: ["0x0001", "0x0002"],
};
const result = await evaluate(expression, options);
expect(result).toEqual(Data.fromHex("0x00010002"));
expect(result.length).toBe(4);
});
});
// skipped because test does not perform proper padding
it.skip("evaluates keccak256 expressions", async () => {
const expression: Pointer.Expression = {
$keccak256: ["foo", "bar", 42, "0x1f"],
};
const expectedHash = keccak256(
new Uint8Array(
Buffer.from(
toHex(Data.fromNumber(42)).slice(2) +
toHex(Data.fromHex("0x1f")).slice(2) +
toHex(variables.foo).slice(2) +
toHex(variables.bar).slice(2),
"hex",
),
),
);
expect(await evaluate(expression, options)).toEqual(
Data.fromBytes(expectedHash),
);
});
it("evaluates offset lookup expressions", async () => {
const expression: Pointer.Expression = {
".offset": "stack",
};
expect(await evaluate(expression, options)).toEqual(Data.fromUint(0x60n));
});
it("evaluates offset lookup expressions with $this", async () => {
const expression: Pointer.Expression = {
".offset": "$this",
};
const $this = {
name: "$this",
location: "memory",
offset: Data.fromNumber(0x120),
length: Data.fromNumber(0x40),
} as const;
expect(
await evaluate(expression, {
...options,
regions: {
...regions,
$this,
},
}),
).toEqual(Data.fromUint(0x120n));
});
it("evaluates length lookup expressions", async () => {
const expression: Pointer.Expression = {
".length": "memory",
};
expect(await evaluate(expression, options)).toEqual(Data.fromUint(11n));
});
it("evaluates slot lookup expressions", async () => {
const expression: Pointer.Expression = {
".slot": "stack",
};
expect(await evaluate(expression, options)).toEqual(Data.fromNumber(42));
});
describe("resulting bytes widths", () => {
it("uses the fewest bytes necessary for a literal", async () => {
expect(await evaluate(0, options)).toHaveLength(0);
expect(await evaluate("0x00", options)).toHaveLength(1);
expect(await evaluate("0x0000", options)).toHaveLength(2);
expect(await evaluate(0xffff, options)).toHaveLength(2);
});
it("uses at least the largest bytes width amongst arithmetic operands", async () => {
expect(await evaluate({ $sum: [0, 0] }, options)).toHaveLength(0);
expect(
await evaluate({ $difference: ["0x00", "0x00"] }, options),
).toHaveLength(1);
expect(
await evaluate({ $remainder: ["0x0001", "0x01"] }, options),
).toHaveLength(2);
});
it("uses exactly as many bytes necessary to avoid arithmetic overflow", async () => {
expect(
await evaluate({ $product: ["0xffff", "0xff"] }, options),
).toHaveLength(3);
});
});
it("evaluates resize expressions", async () => {
expect(await evaluate({ $sized1: 0 }, options)).toHaveLength(1);
{
const data = await evaluate({ $sized1: "0xabcd" }, options);
expect(data).toHaveLength(1);
expect(data).toEqual(Data.fromNumber(0xcd));
}
{
const data = await evaluate({ $wordsized: "0xabcd" }, options);
expect(data).toHaveLength(32);
expect(data).toEqual(Data.fromNumber(0xabcd).resizeTo(32));
}
});
});