Repository navigation
Expand file tree
/
Copy pathverify-bytecode.ts
More file actions
358 lines (347 loc) · 10.2 KB
/
Copy pathverify-bytecode.ts
File metadata and controls
358 lines (347 loc) · 10.2 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
import type { Code } from "../src/code.js"
import { CheckBaseKind, CondKind, ImportKind, Op, OpNames } from "../src/opcodes.js"
/**
* Stack-depth verifier (C5): runs abstract interpretation over packed bytecode independent of `CodeBuilder`,
* proving a single static operand depth at every reachable pc, `0 <= depth <= maxStack`, valid handler and
* `END_FINALLY` entry depths, T&S successor depths, and valid local/upvalue slot bounds.
*/
export const verifyCode = (c: Code): Array<string> => {
const errors: Array<string> = []
const label = `${c.source.id}#${c.index}(${c.name || "<anon>"})`
const words = c.code
const depth = new Int32Array(words.length).fill(-1)
const work: Array<number> = []
const at = (pc: number, d: number, from: number) => {
if (pc < 0 || pc >= words.length) {
errors.push(`${label}: pc ${pc} out of range (from ${from})`)
return
}
if (pc > 0 && (words[pc - 1]! & 0xff) === Op.EXTRA) {
errors.push(`${label}: jump into middle of EXTRA sequence at pc ${pc} (from ${from})`)
return
}
if (d < 0) {
errors.push(`${label}: negative depth ${d} at pc ${pc} (from ${from})`)
return
}
if (d > c.maxStack) {
errors.push(`${label}: depth ${d} > maxStack ${c.maxStack} at pc ${pc}`)
}
if (depth[pc] === -1) {
depth[pc] = d
work.push(pc)
} else if (depth[pc] !== d) {
errors.push(`${label}: depth conflict at pc ${pc}: ${depth[pc]} vs ${d} (from ${from})`)
}
}
if (words.length > 0) at(0, 0, -1)
const h = c.handlers
for (let i = 0; i < h.length; i += 4) {
at(h[i + 2]!, h[i + 3]! + 1, -2)
}
const checkSlot = (s: number, pc: number) => {
if (s < 0 || s >= c.locals) {
errors.push(`${label}: local slot ${s} out of [0, ${c.locals}) at pc ${pc}`)
}
}
const checkUpval = (u: number, pc: number) => {
const count = c.captures.length >>> 1
if (u < 0 || u >= count) {
errors.push(`${label}: upval ${u} out of [0, ${count}) at pc ${pc}`)
}
}
while (work.length > 0) {
const pc = work.pop()!
const d = depth[pc]!
const w = words[pc]!
let op = w & 0xff
let stride = 1
let a = (w >>> 8) & 0xff
let bx = w >>> 16
if (op === Op.EXTRA) {
if (pc + 1 >= words.length) {
errors.push(`${label}: trailing EXTRA at pc ${pc}`)
continue
}
const w1 = words[pc + 1]!
if ((w1 & 0xff) === Op.EXTRA) {
if (pc + 2 >= words.length) {
errors.push(`${label}: trailing EXTRA at pc ${pc + 1}`)
continue
}
const w2 = words[pc + 2]!
op = w2 & 0xff
a = ((w >>> 16) << 16) | (w1 & 0xff00) | ((w2 >>> 8) & 0xff)
bx = ((w1 >>> 16) << 16) | (w2 >>> 16)
stride = 3
} else {
op = w1 & 0xff
a = (w & 0xff00) | ((w1 >>> 8) & 0xff)
bx = ((w >>> 16) << 16) | (w1 >>> 16)
stride = 2
}
if (op === Op.EXTRA) {
errors.push(`${label}: more than two EXTRA prefixes at pc ${pc}`)
continue
}
}
const next = (delta: number) => at(pc + stride, d + delta, pc)
switch (op) {
case Op.PUSH_UNDEF:
case Op.PUSH_NULL:
case Op.PUSH_BOOL:
case Op.PUSH_INT:
case Op.PUSH_CONST:
case Op.DUP:
case Op.GET_INTRINSIC:
case Op.ARGS:
case Op.CLOSURE:
case Op.NEW_OBJECT:
case Op.TEMPLATE:
case Op.REGEXP:
next(1)
break
case Op.GET_GLOBAL:
next(a === 2 ? -1 : 1)
break
case Op.IMPORT:
next(a === ImportKind.SIDE_EFFECT ? 0 : a === ImportKind.DYNAMIC ? 0 : 1)
break
case Op.PICK:
if (a >= d) errors.push(`${label}: PICK ${a} at depth ${d} (pc ${pc})`)
next(1)
break
case Op.ROT:
if (a >= d) errors.push(`${label}: ROT ${a} at depth ${d} (pc ${pc})`)
next(0)
break
case Op.GET_LOCAL:
case Op.GET_CELL:
checkSlot(bx, pc)
next(1)
break
case Op.PUT_LOCAL:
case Op.PUT_CELL:
checkSlot(bx, pc)
next(-1)
break
case Op.HOLE_LOCAL:
case Op.NEW_CELL:
case Op.COPY_CELL:
checkSlot(bx, pc)
next(0)
break
case Op.GET_UPVAL:
checkUpval(bx, pc)
next(1)
break
case Op.PUT_UPVAL:
checkUpval(bx, pc)
next(-1)
break
case Op.POP:
case Op.PUT_PROP:
case Op.COPY_DATA_PROPS:
case Op.IN:
case Op.INSTANCEOF:
case Op.ADD:
case Op.SUB:
case Op.MUL:
case Op.LT:
case Op.LE:
case Op.GT:
case Op.GE:
case Op.SEQ:
case Op.SNE:
case Op.BINOP:
case Op.DELETE_ELEM:
case Op.NEW_SPREAD:
next(-1)
break
case Op.POPN:
case Op.COPY_REST:
next(-bx)
break
case Op.SET_NAME:
case Op.DELETE_PROP:
case Op.CHECK_BASE:
case Op.TO_KEY:
case Op.UNARY:
case Op.NOT:
case Op.TO_NUMERIC:
case Op.INC:
case Op.TO_STRING:
case Op.NOP:
case Op.FOR_IN:
case Op.ITER_INIT:
case Op.GENERATOR_START:
if (op === Op.CHECK_BASE && a === CheckBaseKind.READ && d < 2) {
errors.push(`${label}: CHECK_BASE.read at depth ${d} (pc ${pc})`)
}
next(0)
break
case Op.GET_PROP:
next(a === 1 ? 1 : 0)
break
case Op.GET_ELEM:
next(a === 1 ? 0 : -1)
break
case Op.PUT_ELEM:
case Op.CALL_SPREAD:
next(-2)
break
case Op.DEFINE_FIELD:
next(a === 0 ? -1 : -2)
break
case Op.NEW_ARRAY:
case Op.CONCAT:
next(1 - bx)
break
case Op.ARRAY_APPEND:
next(a === 1 ? 0 : -1)
break
case Op.JUMP:
case Op.LOOP:
at(bx, d, pc)
break
case Op.JUMP_IF_FALSE:
case Op.JUMP_IF_TRUE:
at(bx, d - 1, pc)
next(-1)
break
case Op.JUMP_COND:
if (a === CondKind.NULLISH) {
at(bx, d, pc)
next(0)
} else if (a === CondKind.NULLISH_METHOD) {
at(bx, d - 1, pc)
next(0)
} else {
at(bx, d, pc)
next(-1)
}
break
case Op.THROW:
case Op.RETURN:
case Op.END_PROGRAM:
if (d < 1) errors.push(`${label}: ${OpNames[op]} on empty stack at pc ${pc}`)
break
case Op.THROW_ERROR:
break
case Op.END_FINALLY: {
if (bx < 0 || bx >= c.finallyOffsets.length) {
errors.push(`${label}: END_FINALLY level ${bx} out of range at pc ${pc}`)
break
}
const off = c.finallyOffsets[bx]!
const fin = c.finallies
const tagSlot = fin[off]!
const valSlot = fin[off + 1]!
const entryDepth = fin[off + 2]!
const n = fin[off + 3]!
if (d !== entryDepth) {
errors.push(`${label}: END_FINALLY ${bx} at depth ${d}, expected ${entryDepth} (pc ${pc})`)
}
checkSlot(tagSlot, pc)
checkSlot(valSlot, pc)
for (let k = 0; k < n; k++) {
const target = fin[off + 4 + k]!
if (k !== 1 && target >= 0) at(target, entryDepth, pc)
}
break
}
case Op.CALL:
if (d < a + 2) errors.push(`${label}: CALL ${a} at depth ${d} (pc ${pc})`)
next(-(a + 1))
break
case Op.NEW:
if (a === 255) {
if (d < 1) errors.push(`${label}: NEW.check at depth ${d} (pc ${pc})`)
next(0)
} else {
if (d < a + 1) errors.push(`${label}: NEW ${a} at depth ${d} (pc ${pc})`)
next(-a)
}
break
case Op.ITER_OPEN: {
const jumpPc = pc + stride
const jumpStride = jumpPc < words.length && (words[jumpPc]! & 0xff) === Op.EXTRA ? 2 : 1
if (jumpPc + jumpStride > words.length || (words[jumpPc + jumpStride - 1]! & 0xff) !== Op.JUMP) {
errors.push(`${label}: T&S ITER_OPEN at pc ${pc} not followed by JUMP`)
break
}
at(jumpPc, d + 1, pc)
at(jumpPc + jumpStride, d, pc)
break
}
case Op.ITER_STEP: {
checkSlot(a, pc)
const jumpPc = pc + stride
const jumpStride = jumpPc < words.length && (words[jumpPc]! & 0xff) === Op.EXTRA ? 2 : 1
if (jumpPc + jumpStride > words.length || (words[jumpPc + jumpStride - 1]! & 0xff) !== Op.JUMP) {
errors.push(`${label}: T&S ITER_STEP at pc ${pc} not followed by JUMP`)
break
}
at(jumpPc, d + 2, pc)
at(jumpPc + jumpStride, d + 1, pc)
at(bx, d, pc)
break
}
case Op.ITER_RESULT:
checkSlot(a, pc)
next(0)
at(bx, d - 1, pc)
break
case Op.ITER_CLOSE:
checkSlot(a, pc)
next(2)
at(bx, d, pc)
break
case Op.ITER_APPEND_ALL:
checkSlot(a, pc)
next(0)
at(bx, d, pc)
break
case Op.AWAIT:
if (d < 1) errors.push(`${label}: AWAIT on empty stack at pc ${pc}`)
next(0)
break
case Op.YIELD:
if (d < 1) errors.push(`${label}: YIELD on empty stack at pc ${pc}`)
next(1)
break
case Op.RESUME:
if (d < 2) errors.push(`${label}: RESUME at depth ${d} (pc ${pc})`)
at(bx, d - 1, pc)
next(-1)
break
case Op.YSTAR:
checkSlot(a, pc)
if (d < 2) errors.push(`${label}: YSTAR at depth ${d} (pc ${pc})`)
at(bx, d + 1, pc)
next(-1)
break
case Op.YSTAR_RESULT:
checkSlot(a, pc)
if (d < 1) errors.push(`${label}: YSTAR_RESULT on empty stack at pc ${pc}`)
at(bx, d, pc)
next(0)
break
default:
errors.push(`${label}: unhandled op ${OpNames[op] ?? op} at pc ${pc}`)
break
}
}
for (let i = 0; i < h.length; i += 4) {
const start = h[i]!
const end = h[i + 1]!
const handlerDepth = h[i + 3]!
for (let pc = start; pc < end; pc++) {
if (depth[pc]! !== -1 && depth[pc]! < handlerDepth) {
errors.push(`${label}: pc ${pc} depth ${depth[pc]} < handler depth ${handlerDepth}`)
}
}
}
return errors
}
export const verifyAll = (codes: ReadonlyArray<Code>): Array<string> => codes.flatMap((c) => verifyCode(c))