-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.lua
465 lines (372 loc) · 11.1 KB
/
decoder.lua
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
local opcodes = require("opcodes")
local parsers = require("parsers")
local magic = "\x00\x61\x73\x6D\x01\x00\x00\x00"
local decoder = {}
-- Valid types for signatures n stuff
local types = {
[0x7f] = 1, -- i32
[0x7e] = 2, -- i64
[0x7d] = 3, -- f32
[0x7c] = 4, -- f64
[0x70] = 5, -- anyfunc
[0x60] = 6, -- func
[0x40] = 7 -- psuedo 'empty_block' type
}
local function nibble(stream)
return stream:sub(1, 1), stream:sub(2)
end
local kinds = {
Function = 0,
Table = 1,
Memory = 2,
Global = 3
}
local typeMap = {
NONE = 0,
I32 = 1,
I64 = 2,
F32 = 3,
F64 = 4,
BLOC = 7,
VUI1 = 10,
VUI3 = 11,
VUI6 = 12,
VSI3 = 13,
VSI6 = 14,
VF32 = 15,
VF64 = 16,
BRTB = 17,
CALI = 18,
MEMI = 19
}
local function decodeImmediate(type, stream)
local result
if type == typeMap.VUI1 then
result, stream = parsers.parseLEBu(stream, 1)
elseif type == typeMap.VUI3 then
result, stream = parsers.parseLEBu(stream, 4)
elseif type == typeMap.VSI3 then
result, stream = parsers.parseLEBs(stream, 4)
elseif type == typeMap.VSI6 then
result, stream = parsers.parseLEBs(stream, 8)
elseif type == typeMap.VF64 then
result, stream = parsers.parseFloat(stream, 8)
elseif type == typeMap.VF32 then
result, stream = parsers.parseFloat(stream, 4)
elseif type == typeMap.BLOC then
result, stream = parsers.parseLEBs(stream, 1)
elseif type == typeMap.BRTB then
local tab, len, default = {}
len, stream = parsers.parseLEBu(stream, 4)
for i = 1, len do
tab[i], stream = parsers.parseLEBu(stream, 4)
end
tab.default, stream = parsers.parseLEBu(stream, 4)
result = tab
elseif type == typeMap.CALI then
result, stream = parsers.parseLEBu(stream, 4)
-- There's another reserved varuint1
local reserved
reserved, stream = parsers.parseLEBu(stream, 1)
if reserved ~= 0 then
error("Reserved bit for CALI was not 0", 0) -- So that we know that we break after MVP
end
elseif type == typeMap.MEMI then
result = {}
result.align, stream = parsers.parseLEBu(stream, 1)
result.offset, stream = parsers.parseLEBu(stream, 1)
else
error("Unsupported immediate type required: '" .. type .. "'", 0)
end
return result, stream
end
local function decodeFunctionBody(stream)
local body = {}
while #stream > 1 do
local opcode
opcode, stream = nibble(stream)
opcode = opcode:byte()
local opcodeDef = opcodes.codes[opcode]
if not opcodeDef then
error(("Unsupported opcode: '%x'"):format(opcode), 0)
end
local instr = {
name = opcodeDef.textName,
enum = opcodeDef.enumName,
proto = opcodeDef
}
local immediate = opcodeDef.immediate
if immediate ~= typeMap.NONE then
instr.imVal, stream = decodeImmediate(immediate, stream)
end
body[#body + 1] = instr
end
if stream:byte() ~= opcodes.enum.End.opcode then
error("Function declaration did not end with 'End' opcode", 0)
end
return body
end
local function parseInitializerExpr(stream)
local opcode
opcode, stream = nibble(stream)
opcode = opcode:byte()
local def = opcodes.codes[opcode]
local initType = def.immediate
local initVal
initVal, stream = decodeImmediate(initType, stream)
local endByte
endByte, stream = nibble(stream)
if endByte:byte() ~= opcodes.enum.End.opcode then
error("getGlobal initializer expression NYI", 0)
end
return initVal, stream
end
local sections = {
[1] = function(stream) -- Types section
local count
count, stream = parsers.parseLEBu(stream, 4)
local typeDeclarations = {}
for i = 1, count do
local typeKind
typeKind, stream = nibble(stream)
typeKind = typeKind:byte()
if types[typeKind] == 6 then -- Function Type
local funcType = {
params = {},
returns = {}
}
-- Parse in the parameter types
local paramCount
paramCount, stream = nibble(stream)
paramCount = parsers.parseLEBu(paramCount, 4)
for j = 1, paramCount do
local paramType
paramType, stream = nibble(stream)
paramType = paramType:byte()
funcType.params[j] = types[paramType]
end
-- Parse in the return types
local returnCount
returnCount, stream = nibble(stream)
returnCount = parsers.parseLEBu(returnCount, 4)
for j = 1, returnCount do
local returnType
returnType, stream = nibble(stream)
returnType = returnType:byte()
funcType.returns[j] = types[returnType]
end
typeDeclarations[i - 1] = funcType
else
error("Unsupported type declaration '" .. typeKind .. "'", 0)
end
end
return typeDeclarations
end,
[2] = function(stream) -- Imports Section
local count
count, stream = parsers.parseLEBu(stream, 4)
local imports = {}
for i = 1, count do
local import = {}
import.module, stream = parsers.parseVLString(stream)
import.field, stream = parsers.parseVLString(stream)
import.kind, stream = nibble(stream)
import.kind = import.kind:byte()
if import.kind == kinds.Function then
import.typeIndex, stream = parsers.parseLEBu(stream, 4)
else
error("Unsupported import kind '" .. import.kind .. "'", 0)
end
imports[i - 1] = import
end
return imports
end,
[3] = function(stream) -- Function Declarations Dection
local count
count, stream = parsers.parseLEBu(stream, 4)
local typeIndexes = {}
for i = 1, count do
local type
type, stream = parsers.parseLEBu(stream, 4)
typeIndexes[i - 1] = type
end
return typeIndexes
end,
[4] = function(stream) -- Table Section
local count
count, stream = parsers.parseLEBu(stream, 4)
local tables = {}
for i = 1, count do
local type
type, stream = nibble(stream)
type = type:byte()
local limits, flag = {}
flag, stream = parsers.parseLEBu(stream, 1)
limits.initial = parsers.parseLEBu(stream, 4)
if flag == 1 then
limits.maximum = parsers.parseLEBu(stream, 4)
end
tables[i - 1] = {type = type, limits = limits}
end
return tables
end,
[5] = function(stream) -- Memory Section
local count
count, stream = parsers.parseLEBu(stream, 4)
local memories = {}
for i = 1, count do
local limits, flag = {}
flag, stream = parsers.parseLEBu(stream, 1)
limits.initial = parsers.parseLEBu(stream, 4)
if flag == 1 then
limits.maximum = parsers.parseLEBu(stream, 4)
end
memories[i - 1] = {limits = limits}
end
return memories
end,
[6] = function(stream) -- Global Declarations
local count
count, stream = parsers.parseLEBu(stream, 4)
local globals = {}
for i = 1, count do
local type
type, stream = nibble(stream)
type = type:byte()
local mutability
mutability, stream = parsers.parseLEBu(stream, 1)
local initVal
initVal, stream = parseInitializerExpr(stream)
globals[i - 1] = {
type = type,
mutability = mutability,
value = initVal
}
end
return globals
end,
[7] = function(stream) -- Exports Section
local count
count, stream = parsers.parseLEBu(stream, 4)
local exports = {}
for i = 1, count do
local name
name, stream = parsers.parseVLString(stream)
local kind
kind, stream = nibble(stream)
kind = kind:byte()
local index
index, stream = parsers.parseLEBu(stream, 4)
exports[name] = {index = index, kind = kind}
end
return exports
end,
[8] = function(stream) -- Start Function
local index
index, stream = parsers.parseLEBu(stream, 4)
return index
end,
[9] = function(stream) -- Table Elements Section
local count
count, stream = parsers.parseLEBu(stream, 4)
local tables = {}
for i = 1, count do
local tableIndex
tableIndex, stream = parsers.parseLEBu(stream, 4)
local offset
offset, stream = parseInitializerExpr(stream)
local elCount
elCount, stream = parsers.parseLEBu(stream, 4)
tables[tableIndex] = tables[tableIndex] or {}
for j = 1, elCount do
tables[tableIndex][offset + j - 1], stream = parsers.parseLEBu(stream, 4)
end
end
return tables
end,
[10] = function(stream) -- Function Bodies
local count
count, stream = parsers.parseLEBu(stream, 4)
local bodies = {}
for i = 1, count do
local func = {}
local bodySize
bodySize, stream = parsers.parseLEBu(stream, 4)
local workingStream
workingStream, stream = stream:sub(1, bodySize), stream:sub(bodySize + 1)
do -- Decode Body
local localCount
localCount, workingStream = parsers.parseLEBu(workingStream, 4)
-- Capture locals
func.locals = {}
for j = 1, localCount do
local typeCount
typeCount, workingStream = parsers.parseLEBu(workingStream, 4)
local type
type, workingStream = nibble(workingStream)
type = type:byte()
for k = 1, typeCount do
func.locals[#func.locals + 1] = type
end
end
-- Decode instructions
func.instructions = decodeFunctionBody(workingStream)
end
bodies[i - 1] = func
end
return bodies
end,
[11] = function(stream) -- Data Section
local count
count, stream = parsers.parseLEBu(stream, 4)
local segments = {}
for i = 1, count do
local memoryIndex
memoryIndex, stream = parsers.parseLEBu(stream, 4)
local offsetVal
offsetVal, stream = parseInitializerExpr(stream)
local data
data, stream = parsers.parseVLString(stream)
segments[i] = {index = memoryIndex, addr = offsetVal, data = data}
end
return segments
end
}
function decoder.decode(stream)
if stream:sub(1, #magic) ~= magic then
error("Not a valid wasm 1.0 binary", 0)
end
-- Discard magic identifier
stream = stream:sub(#magic + 1)
-- Parse each section
local sectionData = {[0] = {}}
while #stream > 0 do
local sectionID, sectionLength
sectionID, stream = nibble(stream)
sectionID = sectionID:byte()
sectionLength, stream = parsers.parseLEBu(stream, 4)
if sectionID == 0 then
local sectionName
sectionName = parsers.parseVLString(stream)
local sectionStream
sectionStream, stream = stream:sub(1, sectionLength), stream:sub(sectionLength + 1)
sectionData[0][#sectionData[0] + 1] = {
name = sectionName,
data = sectionStream
}
else
-- Well defined section
if sections[sectionID] then
local sectionStream
sectionStream, stream = stream:sub(1, sectionLength), stream:sub(sectionLength + 1)
sectionData[sectionID] = sections[sectionID](sectionStream, sectionData)
else
print("Invalid section id '" .. sectionID .. "'.. skipping..")
local sectionStream
sectionStream, stream = stream:sub(1, sectionLength), stream:sub(sectionLength + 1)
end
end
end
return sectionData
end
return decoder