-
Notifications
You must be signed in to change notification settings - Fork 3
/
Parser.cs
693 lines (570 loc) · 19.2 KB
/
Parser.cs
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
using System;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading.Tasks;
namespace Wasm2CIL {
public static class WebassemblySection
{
public const int Custom = 0;
public const int Type = 1;
public const int Import = 2;
public const int Function = 3;
public const int Table = 4;
public const int Memory = 5;
public const int Global = 6;
public const int Export = 7;
public const int Start = 8;
public const int Element = 9;
public const int Code = 10;
public const int Data = 11;
}
public class WebassemblyResult
{
public static Type Convert (BinaryReader reader)
{
byte res = reader.ReadByte ();
if (res == 0x40)
return null;
else
return WebassemblyValueType.Convert (res);
}
}
public static class WebassemblyValueType
{
public const byte I32 = 0x7F;
public const byte I64 = 0x7E;
public const byte F32 = 0x7D;
public const byte F64 = 0x7C;
public static Type Convert (byte key)
{
switch (key) {
case 0x7F:
return typeof (int);
case 0x7E:
return typeof (long);
case 0x7D:
return typeof (float);
case 0x7C:
return typeof (double);
default:
throw new Exception (String.Format ("Illegal value type {0:X}", key));
}
}
}
public class WebassemblyLocal
{
public readonly int Count;
public readonly int valueTypeInit;
public WebassemblyLocal (int count, int valueTypeInit)
{
this.Count = count;
this.valueTypeInit = valueTypeInit;
}
public Type AsType () {
if (Count == 1) {
switch (valueTypeInit) {
case WebassemblyValueType.I32:
return typeof (int);
case WebassemblyValueType.I64:
return typeof (long);
case WebassemblyValueType.F32:
return typeof (float);
case WebassemblyValueType.F64:
return typeof (double);
}
}
switch (valueTypeInit) {
case WebassemblyValueType.I32:
return typeof (int);
case WebassemblyValueType.I64:
return typeof (long);
case WebassemblyValueType.F32:
return typeof (float);
case WebassemblyValueType.F64:
return typeof (double);
}
throw new Exception ("Illegal local type");
}
}
public class WebassemblyLimit
{
public readonly ulong min;
// 0 signifies unlimited
public readonly ulong max;
public WebassemblyLimit (ulong min, ulong max)
{
this.min = min;
this.max = max;
}
public WebassemblyLimit (BinaryReader reader)
{
int kind = Convert.ToInt32 (Parser.ParseLEBSigned (reader, 32));
this.min = Parser.ParseLEBUnsigned (reader, 32);
if (kind == 0x1)
this.max = Parser.ParseLEBUnsigned (reader, 32);
else if (kind == 0x0)
this.max = 0;
else
throw new Exception ("Wrong limit type");
}
}
public class WebassemblyImport
{
public readonly string module;
public readonly string name;
public readonly int kind;
public readonly ulong index;
public WebassemblyImport(BinaryReader reader)
{
module = Parser.ReadString(reader);
name = Parser.ReadString(reader);
kind = Convert.ToInt32(Parser.ParseLEBUnsigned(reader, 7));
if (kind == 0x0)
index = Parser.ParseLEBUnsigned(reader,32);
}
}
public class WebassemblyMemory
{
public readonly WebassemblyLimit limit;
public WebassemblyMemory (WebassemblyLimit limit)
{
this.limit = limit;
}
}
public class WebassemblyTable
{
public readonly WebassemblyLimit limit;
public readonly ulong elementType;
public WebassemblyTable (ulong elementType, WebassemblyLimit limit)
{
this.limit = limit;
this.elementType = elementType;
}
}
public class WebassemblyGlobal
{
// WebassemblyValueType
public readonly int valueType;
public readonly bool mutable;
public readonly WebassemblyExpression init;
public WebassemblyGlobal (BinaryReader reader)
{
valueType = Convert.ToInt32 (Parser.ParseLEBSigned (reader, 7));
long mut = Parser.ParseLEBSigned (reader, 7);
init = new WebassemblyExpression (reader);
}
}
public class WebassemblyExport
{
public readonly string name;
public readonly uint kind;
public readonly ulong index;
public WebassemblyExport (BinaryReader reader)
{
name = Parser.ReadString(reader);
kind = Convert.ToUInt32(Parser.ParseLEBUnsigned(reader, 8));
index = Parser.ParseLEBUnsigned(reader, 32);
}
}
public class WebassemblyElementInit
{
public readonly long index;
public readonly byte [] body;
public readonly WebassemblyExpression expr;
public WebassemblyElementInit (BinaryReader reader)
{
index = Parser.ParseLEBSigned (reader, 32);
// assert table index is 0, only one allowed in this version
if (index != 0)
throw new Exception ("At most one table allowed in this version of webassembly");
expr = new WebassemblyExpression (reader);
long body_size = Parser.ParseLEBSigned (reader, 32);
body = reader.ReadBytes (Convert.ToInt32 (body_size));
}
}
public class WebassemblyDataInit
{
public readonly long index;
public readonly byte [] body;
public readonly WebassemblyExpression expr;
public WebassemblyDataInit (BinaryReader reader)
{
index = Parser.ParseLEBSigned (reader, 32);
// assert table index is 0, only one allowed in this version
if (index != 0)
throw new Exception ("At most one memory allowed in this version of webassembly");
expr = new WebassemblyExpression (reader);
long body_size = Parser.ParseLEBSigned (reader, 32);
body = reader.ReadBytes (Convert.ToInt32 (body_size));
}
}
public class WebassemblyFunc
{
public readonly WebassemblyLocal [] locals;
public readonly int num_locals;
public readonly WebassemblyExpression expr;
public WebassemblyFunc (BinaryReader reader)
{
int num_local_types = Convert.ToInt32 (Parser.ParseLEBSigned (reader, 32));
this.locals = new WebassemblyLocal [num_local_types];
this.num_locals = 0;
for (int local=0; local < num_local_types; local++) {
// Size of local in count of 32-bit segments
int count_of_locals = Convert.ToInt32 (Parser.ParseLEBSigned (reader, 7));
int valueTypeInit = Convert.ToInt32 (Parser.ParseLEBSigned (reader, 7));
this.locals [local] = new WebassemblyLocal (count_of_locals, valueTypeInit);
this.num_locals += count_of_locals;
Console.WriteLine ("Parsed code section local: Count {0} Type {1}", count_of_locals, valueTypeInit);
}
Console.WriteLine ("Parsed code section one {0} locals", num_locals);
this.expr = new WebassemblyExpression (reader, true);
}
public MethodInfo Emit (WebassemblyFunctionType type, TypeBuilder tb, string name)
{
var param_types = type.EmitParams ();
var return_type = type.EmitReturn ();
var method = tb.DefineMethod (name, MethodAttributes.Public, return_type, param_types);
var ilgen = method.GetILGenerator ();
var outputLocals = new LocalBuilder [num_locals];
var index = 0;
for (int i=0; i < locals.Length; i++) {
var ty = locals [i].AsType ();
for (int j=0; j < locals [i].Count; j++)
outputLocals [index++] = ilgen.DeclareLocal (ty);
}
expr.Body.Emit (ilgen, param_types != null ? param_types.Length : 0);
return method;
}
}
public class WebassemblyExpression
{
public readonly WebassemblyCodeParser Body;
public WebassemblyExpression (BinaryReader reader): this (reader, false)
{
}
public WebassemblyExpression (BinaryReader reader, bool readToEnd)
{
Body = new WebassemblyCodeParser (reader);
Console.WriteLine ("Parsed: {0}, Body.ToString ()");
if (readToEnd && (reader.BaseStream.Position != reader.BaseStream.Length))
throw new Exception ("Didn't actually read to end");
}
}
public class WebassemblyFunctionType
{
readonly long form;
readonly ulong[] parameters;
readonly ulong[] results;
public Type [] EmitParams ()
{
if (parameters.Length == 0)
return null;
var accum = new List<Type> ();
foreach (var res in parameters)
accum.Add (WebassemblyValueType.Convert ((byte) res));
return accum.ToArray ();
}
public Type EmitReturn ()
{
if (results.Length == 0)
return null;
if (results.Length == 1)
return WebassemblyValueType.Convert ((byte) this.results [0]);
throw new Exception ("Result type array may only have length 1 in this version of Webassembly");
}
public WebassemblyFunctionType (long form, ulong[] parameters, ulong[] results)
{
this.form = form;
this.parameters = parameters;
this.results = results;
}
}
public class Parser {
const int WebassemblyMagic = 0x6d736100;
const int WebassemblyFunctionEnd = 0x0b;
const int WebassemblyVersion = 0x01;
// Function index is into table of both imported functions and
// defined functions. So fn_idx is not valid into types [], must subtract imported
WebassemblyFunctionType [] types;
WebassemblyFunc [] exprs;
// function_index_to_type_index_map. fn_to_type [fn_idx] = type_idx
ulong [] fn_to_type;
long start_idx;
WebassemblyGlobal [] globals;
WebassemblyElementInit [] elements;
WebassemblyDataInit [] data;
WebassemblyTable table;
WebassemblyMemory mem;
WebassemblyExport [] exports;
WebassemblyImport [] imports;
// The async tasks involved in parsing
Task [] work;
// Can only be called after all sections are done parsing
public Type
Emit (string outputName, string outputFilePath)
{
// Make sure we are finished parsing.
Task.WaitAll (work);
AssemblyName aName = new AssemblyName (String.Format ("{0}Proxy", outputName));
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aName, AssemblyBuilderAccess.RunAndSave);
// FIXME: always want to provide debug info or not?
ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, outputFilePath);
TypeBuilder tb = mb.DefineType (outputName, TypeAttributes.Public, typeof (WebassemblyModule));
var constructor = tb.DefineConstructor (MethodAttributes.Public, CallingConventions.Standard, new Type []{});
var ctor_gen = constructor.GetILGenerator();
// the constructor gets the "this" pointer
ctor_gen.Emit(OpCodes.Ldarg_0);
// Next arg is the size
if (mem != null)
ctor_gen.Emit(OpCodes.Ldc_I4, mem.limit.min);
else
ctor_gen.Emit(OpCodes.Ldc_I4_0);
ctor_gen.Emit(OpCodes.Call, typeof (WebassemblyModule).GetConstructor (new Type[] { typeof (int) }));
ctor_gen.Emit(OpCodes.Ret);
// Compute exports
var export_table = new Dictionary<int, string> ();
if (exports != null) {
foreach (var exp in exports) {
if (exp.kind == 0x0) // function export
export_table [Convert.ToInt32 (exp.index)] = exp.name;
}
}
// fixme: imports / exports?
for (int i=0; i < exprs.Length; i++) {
var fn = exprs [i];
var type = types [fn_to_type [i]];
string fn_name;
if (export_table.ContainsKey (i))
fn_name = export_table [i];
else
fn_name = String.Format ("Function{0}", i);
var emitted = fn.Emit (type, tb, fn_name);
}
var goal = tb.CreateType ();
if (outputFilePath != null)
ab.Save (outputFilePath);
return goal;
}
void ParseTypeSection (BinaryReader reader)
{
int num_types = Convert.ToInt32 (Parser.ParseLEBSigned (reader, 32));
Console.WriteLine ("Parse type section: #types: {0}", num_types);
this.types = new WebassemblyFunctionType [num_types];
for (int i = 0; i < num_types; i++) {
var form = Parser.ParseLEBSigned (reader, 7);
var parameters = Parser.ParseLEBUnsignedArray (reader);
var results = Parser.ParseLEBUnsignedArray (reader);
var type = new WebassemblyFunctionType (form, parameters, results);
this.types [i] = type;
}
}
void ParseFunctionSection (BinaryReader reader)
{
fn_to_type = ParseLEBUnsignedArray (reader);
Console.WriteLine ("Parse function section: #types: {0} ", fn_to_type.Length);
}
void ParseStartSection(BinaryReader reader)
{
start_idx = (long) Parser.ParseLEBUnsigned (reader, 32);
Console.WriteLine ("Parse start section: #index: {0} ", start_idx);
}
void ParseCodeSection (BinaryReader sectionReader)
{
int count = Convert.ToInt32 (Parser.ParseLEBSigned (sectionReader, 32));
exprs = new WebassemblyFunc [count];
// fixme: use tasks / parse in parallel
for (int i=0; i < count; i++) {
int size_of_entry = Convert.ToInt32 (Parser.ParseLEBUnsigned (sectionReader, 32));
// doing now so I can parallelize lower parsing later
byte [] entry = sectionReader.ReadBytes (size_of_entry);
using (BinaryReader bodyReader = new BinaryReader (new MemoryStream (entry))) {
exprs [i] = new WebassemblyFunc (bodyReader);
}
}
Console.WriteLine ("Parsed code section done");
}
void ParseMemorySection(BinaryReader reader)
{
var count = Convert.ToInt32 (Parser.ParseLEBSigned (reader, 32));
if (count != 1)
throw new Exception ("At most one memory allowed in this version of webassembly");
this.mem = new WebassemblyMemory (new WebassemblyLimit (reader));
Console.WriteLine ("Parsed memory section. Limit is {0} {1}", this.mem.limit.min, this.mem.limit.max);
}
void ParseTableSection(BinaryReader reader)
{
var count = Convert.ToInt32 (Parser.ParseLEBSigned (reader, 32));
if (count != 1)
throw new Exception ("At most one table allowed in this version of webassembly");
var elementType = Parser.ParseLEBUnsigned (reader, 7);
var limit = new WebassemblyLimit (reader);
this.table = new WebassemblyTable (elementType, limit);
Console.WriteLine ("Parsed table section. Limit is {0} {1}", limit.min, limit.max);
}
void ParseGlobalSection(BinaryReader reader)
{
var count = Convert.ToInt32 (Parser.ParseLEBSigned (reader, 32));
this.globals = new WebassemblyGlobal [count];
for (int i=0; i < count; i++)
this.globals [i] = new WebassemblyGlobal (reader);
Console.WriteLine ("Parsed global section, {0}", count);
}
void ParseElementSection(BinaryReader reader)
{
var count = Convert.ToInt32 (Parser.ParseLEBSigned (reader, 32));
this.elements = new WebassemblyElementInit [count];
for (int i=0; i < count; i++)
this.elements [i] = new WebassemblyElementInit (reader);
Console.WriteLine ("Parsed element section, {0}", count);
}
void ParseDataSection(BinaryReader reader)
{
var count = Convert.ToInt32 (Parser.ParseLEBSigned (reader, 32));
this.data = new WebassemblyDataInit [count];
for (int i=0; i < count; i++)
this.data [i] = new WebassemblyDataInit (reader);
Console.WriteLine ("Parsed data section, {0}", count);
}
void ParseImportSection(BinaryReader reader)
{
var count = Convert.ToInt32(Parser.ParseLEBUnsigned(reader, 32));
this.imports = new WebassemblyImport [count];
for (int i = 0; i < count; i++)
this.imports[i] = new WebassemblyImport(reader);
Console.WriteLine("Parsed import section, {0}", count);
}
void ParseExportSection(BinaryReader reader)
{
var count = Convert.ToInt32(Parser.ParseLEBUnsigned(reader,32));
this.exports = new WebassemblyExport [count];
for (int i = 0; i < count; i++)
this.exports[i] = new WebassemblyExport(reader);
Console.WriteLine("Parsed export section, {0}", count);
}
void ParseSection (int section_num, byte [] section)
{
Console.WriteLine ("Parse section {0} length {1}", section_num, section.Length);
var memory = new MemoryStream (section);
using (BinaryReader reader = new BinaryReader (memory)) {
switch (section_num) {
case WebassemblySection.Custom:
// We don't have anything here for mono
break;
case WebassemblySection.Type:
ParseTypeSection (reader);
break;
case WebassemblySection.Import:
ParseImportSection (reader);
break;
case WebassemblySection.Function:
ParseFunctionSection (reader);
break;
case WebassemblySection.Table:
ParseTableSection (reader);
break;
case WebassemblySection.Memory:
ParseMemorySection (reader);
break;
case WebassemblySection.Global:
ParseGlobalSection (reader);
break;
case WebassemblySection.Export:
ParseExportSection (reader);
break;
case WebassemblySection.Start:
ParseStartSection (reader);
break;
case WebassemblySection.Element:
ParseElementSection (reader);
break;
case WebassemblySection.Code:
ParseCodeSection (reader);
break;
case WebassemblySection.Data:
ParseDataSection (reader);
break;
}
}
}
public static ulong[] ParseLEBUnsignedArray (BinaryReader reader)
{
int len = Convert.ToInt32 (Parser.ParseLEBSigned (reader, 32));
var accum = new ulong [len];
for (int i=0; i < len; i++)
accum [i] = Parser.ParseLEBUnsigned (reader, 32);
return accum;
}
public static int ParseLEBSigned32 (BinaryReader reader) {
return (int) ParseLEB (reader, 32, true);
}
public static long ParseLEBSigned (BinaryReader reader, int size_bits) {
return (long) ParseLEB (reader, Convert.ToUInt32 (size_bits), true);
}
public static ulong ParseLEBUnsigned (BinaryReader reader, int size_bits) {
return (ulong) ParseLEB (reader, Convert.ToUInt32 (size_bits), false);
}
public static string ReadString (BinaryReader reader)
{
int length = Convert.ToInt32(Parser.ParseLEBUnsigned(reader, 32));
char[] chars = reader.ReadChars(length);
return new string(chars);
}
public static IntPtr ParseLEB (BinaryReader reader, uint size_bits, bool signed)
{
// Taken from pseudocode here: https://en.wikipedia.org/wiki/LEB128
ulong result = 0;
int shift = 0;
uint maxiters = (size_bits + 7 - 1) / 7;
bool done = false;
byte last_byte = 0;
for (int i=0; i < maxiters; i++) {
last_byte = reader.ReadByte ();
uint low_order_7_bits = Convert.ToUInt32 (last_byte & 0x7f);
uint high_order_bit = Convert.ToUInt32 (last_byte & 0x80);
result |= (low_order_7_bits << shift);
shift += 7;
if (high_order_bit == 0) {
done = true;
break;
}
}
if (!done)
throw new Exception ("Overflow when parsing leb");
// sign bit of byte is second high order bit (0x40)
bool sign_bit_set = (last_byte & 0x40) != 0;
// shift less than the entirety of the message
bool shift_partial = (shift < size_bits);
// Sign extend if all conditions met
if (signed && shift_partial && sign_bit_set) {
result |= (ulong) (((long) ~0) << shift);
}
return (IntPtr) result;
}
public Parser (Stream source)
{
start_idx = -1;
using (BinaryReader reader = new BinaryReader (source)) {
var magic_constant = reader.ReadInt32 ();
if (magic_constant != Parser.WebassemblyMagic)
throw new Exception (String.Format ("Unsupported magic number {0} != {1}", magic_constant, Parser.WebassemblyMagic));
var version_constant = reader.ReadInt32 ();
if (version_constant != Parser.WebassemblyVersion)
throw new Exception (String.Format ("Unsupported version number {0} != {1}", version_constant, Parser.WebassemblyVersion));
// Read the sections
var accum = new List<Task> ();
while (reader.BaseStream.Position != reader.BaseStream.Length) {
int id = (int) Parser.ParseLEBUnsigned (reader, 7);
int len = (int) Parser.ParseLEBUnsigned (reader, 32);
var this_section = reader.ReadBytes (len);
// Read slen number bytes into section bytes
// Process section
// asynchronously parse?
accum.Add (Task.Run (() => this.ParseSection (id, this_section)));
}
this.work = accum.ToArray ();
}
}
}
}