/
structure.py
1971 lines (1664 loc) · 73.9 KB
/
structure.py
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# from __future__ import annotations
import typing
import types
import logging
from dataclasses import dataclass
from .executor import Executor
from collections import deque
from math import ceil
from wasm.immtypes import BranchImm, BranchTableImm, CallImm, CallIndirectImm
from .types import (
U32,
I32,
I64,
F32,
F64,
Value,
Value_t,
ValType,
FunctionType,
Name,
TypeIdx,
FuncIdx,
TableIdx,
MemIdx,
GlobalIdx,
WASMExpression,
Instruction,
TableType,
MemoryType,
GlobalType,
LimitType,
convert_instructions,
debug,
Trap,
ZeroDivisionTrap,
OverflowDivisionTrap,
NonExistentFunctionCallTrap,
TypeMismatchTrap,
OutOfBoundsMemoryTrap,
MissingExportException,
ConcretizeStack,
)
from .state import State
from ..core.smtlib import BitVec, Bool, issymbolic, Operators, Expression
from ..core.state import Concretize
from ..utils.event import Eventful
from ..utils import config
from wasm import decode_module, Section
from wasm.wasmtypes import (
SEC_TYPE,
SEC_IMPORT,
SEC_FUNCTION,
SEC_TABLE,
SEC_MEMORY,
SEC_GLOBAL,
SEC_EXPORT,
SEC_START,
SEC_ELEMENT,
SEC_CODE,
SEC_DATA,
SEC_UNK,
)
from ..core.smtlib.solver import Z3Solver
solver = Z3Solver.instance()
logger = logging.getLogger(__name__)
# logger.setLevel(logging.DEBUG)
consts = config.get_group("wasm")
consts.add(
"decode_names",
default=False,
description="Should Manticore attempt to decode custom name sections",
)
#: Size of a standard WASM memory page
PAGESIZE = 2 ** 16
# Wrappers around integers that we use for indexing the store.
class Addr(int):
pass
class FuncAddr(Addr):
pass
class TableAddr(Addr):
pass
class MemAddr(Addr):
pass
class GlobalAddr(Addr):
pass
ExternVal = typing.Union[FuncAddr, TableAddr, MemAddr, GlobalAddr]
FuncElem = typing.Optional[FuncAddr]
ExportDesc = typing.Union[FuncIdx, TableIdx, MemIdx, GlobalIdx]
ImportDesc = typing.Union[TypeIdx, TableType, MemoryType, GlobalType]
@dataclass
class Function:
"""
A WASM Function
https://www.w3.org/TR/wasm-core-1/#functions%E2%91%A0
"""
type: TypeIdx #: The index of a type defined in the module that corresponds to this function's type signature
locals: typing.List[ValType] #: Vector of mutable local variables (and their types)
body: WASMExpression #: Sequence of WASM instructions, should leave the appropriate type on the stack
def allocate(self, store: "Store", module: "ModuleInstance") -> FuncAddr:
"""
https://www.w3.org/TR/wasm-core-1/#functions%E2%91%A5
:param store: Destination Store that we'll insert this Function into after allocation
:param module: The module containing the type referenced by self.type
:return: The address of this within `store`
"""
a = FuncAddr(len(store.funcs))
store.funcs.append(FuncInst(module.types[self.type], module, self))
return a
@dataclass
class Table:
"""
Vector of opaque values of type self.type
https://www.w3.org/TR/wasm-core-1/#tables%E2%91%A0
"""
type: TableType #: union of a limit and a type (currently only supports funcref)s
def allocate(self, store: "Store") -> TableAddr:
"""
https://www.w3.org/TR/wasm-core-1/#tables%E2%91%A5
:param store: Destination Store that we'll insert this Table into after allocation
:return: The address of this within `store`
"""
a = TableAddr(len(store.tables))
store.tables.append(
TableInst([None for _i in range(self.type.limits.min)], self.type.limits.max)
)
return a
@dataclass
class Memory:
"""
Big chunk o' raw bytes
https://www.w3.org/TR/wasm-core-1/#memories%E2%91%A0
"""
type: MemoryType #: secretly a LimitType that specifies how big or small the memory can be
def allocate(self, store: "Store") -> MemAddr:
"""
https://www.w3.org/TR/wasm-core-1/#memories%E2%91%A5
:param store: Destination Store that we'll insert this Memory into after allocation
:return: The address of this within `store`
"""
a = MemAddr(len(store.mems))
store.mems.append(MemInst([0x0] * self.type.min * 64 * 1024, self.type.max))
return a
@dataclass
class Global:
"""
A global variable of a given type
https://www.w3.org/TR/wasm-core-1/#globals%E2%91%A0
"""
type: GlobalType #: The type of the variable
init: WASMExpression #: A (constant) sequence of WASM instructions that calculates the value for the global
def allocate(self, store: "Store", val: Value) -> GlobalAddr:
"""
https://www.w3.org/TR/wasm-core-1/#globals%E2%91%A5
:param store: Destination Store that we'll insert this Global into after allocation
:param val: The initial value of the new global
:return: The address of this within `store`
"""
a = GlobalAddr(len(store.globals))
store.globals.append(GlobalInst(val, self.type.mut))
return a
@dataclass
class Elem:
"""
List of functions to initialize part of a table
https://www.w3.org/TR/wasm-core-1/#element-segments%E2%91%A0
"""
table: TableIdx #: Which table to initialize
offset: WASMExpression #: WASM instructions that calculate an offset to add to the table index
init: typing.List[FuncIdx] #: list of function indices that get copied into the table
@dataclass
class Data:
"""
Vector of bytes that initializes part of a memory
https://www.w3.org/TR/wasm-core-1/#data-segments%E2%91%A0
"""
data: MemIdx #: Which memory to put the data in. Currently only supports 0
offset: WASMExpression #: WASM instructions that calculate offset into the memory
init: typing.List[int] #: List of bytes to copy into the memory
@dataclass
class Import:
"""
Something imported from another module (or the environment) that we need to instantiate a module
https://www.w3.org/TR/wasm-core-1/#imports%E2%91%A0
"""
module: Name #: The name of the module we're importing from
name: Name #: The name of the thing we're importing
desc: ImportDesc #: Specifies whether this is a function, table, memory, or global
@dataclass
class Export:
"""
Something the module exposes to the outside world once it's been instantiated
https://www.w3.org/TR/wasm-core-1/#exports%E2%91%A0
"""
name: Name #: The name of the thing we're exporting
desc: ExportDesc #: Whether this is a function, table, memory, or global
def strip_quotes(rough_name: str) -> Name:
"""
For some reason, the parser returns the function names with quotes around them
:param rough_name:
:return:
"""
return Name(rough_name[1:-1])
class Module:
"""
Internal representation of a WASM Module
"""
__slots__ = [
"types",
"funcs",
"tables",
"mems",
"globals",
"elem",
"data",
"start",
"imports",
"exports",
"function_names",
"local_names",
"_raw",
]
_raw: bytes
def __init__(self):
self.types: typing.List[FunctionType] = []
self.funcs: typing.List[Function] = []
self.tables: typing.List[Table] = []
self.mems: typing.List[Memory] = []
self.globals: typing.List[Global] = []
self.elem: typing.List[Elem] = []
self.data: typing.List[Data] = []
self.start: typing.Optional[
FuncIdx
] = None #: https://www.w3.org/TR/wasm-core-1/#start-function%E2%91%A0
self.imports: typing.List[Import] = []
self.exports: typing.List[Export] = []
self.function_names: typing.Dict[FuncAddr, str] = {}
self.local_names: typing.Dict[FuncAddr, typing.Dict[int, str]] = {}
def __getstate__(self):
state = {
"types": self.types,
"funcs": self.funcs,
"tables": self.tables,
"mems": self.mems,
"globals": self.globals,
"elem": self.elem,
"data": self.data,
"start": self.start,
"imports": self.imports,
"exports": self.exports,
"function_names": self.function_names,
"local_names": self.local_names,
"_raw": self._raw,
}
return state
def __setstate__(self, state):
self.types = state["types"]
self.funcs = state["funcs"]
self.tables = state["tables"]
self.mems = state["mems"]
self.globals = state["globals"]
self.elem = state["elem"]
self.data = state["data"]
self.start = state["start"]
self.imports = state["imports"]
self.exports = state["exports"]
self.function_names = state["function_names"]
self.local_names = state["local_names"]
self._raw = state["_raw"]
def get_funcnames(self) -> typing.List[Name]:
return [e.name for e in self.exports if isinstance(e.desc, FuncIdx)]
@classmethod
def load(cls, filename: str):
"""
Converts a WASM module in binary format into Python types that Manticore can understand
:param filename: name of the WASM module
:return: Module
"""
type_map = {-16: types.FunctionType, -4: F64, -3: F32, -2: I64, -1: I32}
m: Module = cls()
with open(filename, "rb") as wasm_file:
m._raw = wasm_file.read()
# Test modules break name subsection decoding. TODO: Find a better WASM importer
module_iter = decode_module(m._raw, decode_name_subsections=consts.decode_names)
_header = next(module_iter)
section: Section
# Parse the sections from the WASM module into internal types. For each section, the code usually resembles:
# `module.<something>.append(<InstanceOfSomething>(data, from, binary, module))`
for section, section_data in module_iter:
sec_id = getattr(section_data, "id", SEC_UNK)
if sec_id == SEC_TYPE: # https://www.w3.org/TR/wasm-core-1/#type-section%E2%91%A0
for ft in section_data.payload.entries:
m.types.append(
FunctionType(
[type_map[p_type] for p_type in ft.param_types],
[type_map[ft.return_type] for _i in range(ft.return_count)],
)
)
elif sec_id == SEC_IMPORT: # https://www.w3.org/TR/wasm-core-1/#import-section%E2%91%A0
for i in section_data.payload.entries:
ty_map = i.get_decoder_meta()["types"]
mod_name = strip_quotes(ty_map["module_str"].to_string(i.module_str))
field_name = strip_quotes(ty_map["field_str"].to_string(i.field_str))
if i.kind == 0:
m.imports.append(Import(mod_name, field_name, TypeIdx(i.type.type)))
elif i.kind == 1:
m.imports.append(
Import(
mod_name,
field_name,
TableType(
LimitType(i.type.limits.initial, i.type.limits.maximum),
type_map[i.type.element_type],
),
)
)
elif i.kind == 2:
m.imports.append(
Import(
mod_name,
field_name,
MemoryType(i.type.limits.initial, i.type.limits.maximum),
)
)
elif i.kind == 3:
m.imports.append(
Import(
mod_name,
field_name,
GlobalType(bool(i.type.mutability), type_map[i.type.content_type]),
)
)
else:
raise RuntimeError("Can't decode kind field of:", i.kind)
elif (
sec_id == SEC_FUNCTION
): # https://www.w3.org/TR/wasm-core-1/#function-section%E2%91%A0
for f in section_data.payload.types:
m.funcs.append(
Function(TypeIdx(f), [], [])
) # The expressions and locals are stored in the code section
elif sec_id == SEC_TABLE: # https://www.w3.org/TR/wasm-core-1/#table-section%E2%91%A0
for t in section_data.payload.entries:
m.tables.append(
Table(
TableType(LimitType(t.limits.initial, t.limits.maximum), FunctionType)
)
)
elif sec_id == SEC_MEMORY: # https://www.w3.org/TR/wasm-core-1/#memory-section%E2%91%A0
for mem in section_data.payload.entries:
m.mems.append(Memory(LimitType(mem.limits.initial, mem.limits.maximum)))
elif sec_id == SEC_GLOBAL: # https://www.w3.org/TR/wasm-core-1/#global-section%E2%91%A0
for g in section_data.payload.globals:
m.globals.append(
Global(
GlobalType(g.type.mutability, type_map[g.type.content_type]),
convert_instructions(g.init),
)
)
elif sec_id == SEC_EXPORT: # https://www.w3.org/TR/wasm-core-1/#export-section%E2%91%A0
mapping = (FuncIdx, TableIdx, MemIdx, GlobalIdx)
for e in section_data.payload.entries:
ty = e.get_decoder_meta()["types"]["field_str"]
m.exports.append(
Export(strip_quotes(ty.to_string(e.field_str)), mapping[e.kind](e.index))
)
elif sec_id == SEC_START: # https://www.w3.org/TR/wasm-core-1/#start-section%E2%91%A0
m.start = FuncIdx(section_data.payload.index)
elif (
sec_id == SEC_ELEMENT
): # https://www.w3.org/TR/wasm-core-1/#element-section%E2%91%A0
for e in section_data.payload.entries:
m.elem.append(
Elem(
TableIdx(e.index),
convert_instructions(e.offset),
[FuncIdx(i) for i in e.elems],
)
)
elif sec_id == SEC_CODE: # https://www.w3.org/TR/wasm-core-1/#code-section%E2%91%A0
for idx, c in enumerate(section_data.payload.bodies):
m.funcs[idx].locals = [
type_map[e.type] for e in c.locals for _i in range(e.count)
]
m.funcs[idx].body = convert_instructions(c.code)
elif sec_id == SEC_DATA: # https://www.w3.org/TR/wasm-core-1/#data-section%E2%91%A0
for d in section_data.payload.entries:
m.data.append(
Data(MemIdx(d.index), convert_instructions(d.offset), d.data.tolist())
)
elif sec_id == SEC_UNK:
# WASM module renders all types as `GeneratedStructureData` so `isinstance` doesn't work :(
if (
hasattr(section, "name_type")
and hasattr(section, "payload_len")
and hasattr(section, "payload")
):
# https://webassembly.github.io/spec/core/appendix/custom.html#subsections
name_type = section_data.name_type
if name_type == 0: # module name
pass
elif name_type == 1: # function names
for n in section_data.payload.names:
ty = n.get_decoder_meta()["types"]["name_str"]
m.function_names[FuncAddr(n.index)] = strip_quotes(
ty.to_string(n.name_str)
)
elif name_type == 2: # local variable names
for func in section_data.payload.funcs:
func_idx = func.index
for n in func.local_map.names:
ty = n.get_decoder_meta()["types"]["name_str"]
m.local_names.setdefault(FuncAddr(func_idx), {})[
n.index
] = strip_quotes(ty.to_string(n.name_str))
else:
logger.info("Encountered unknown section")
# TODO - other custom sections (https://www.w3.org/TR/wasm-core-1/#custom-section%E2%91%A0)
return m
@dataclass
class ProtoFuncInst:
"""
Groups FuncInst and HostFuncInst into the same category
"""
type: FunctionType #: The type signature of this function
@dataclass
class TableInst:
"""
Runtime representation of a table. Remember that the Table type stores the type of the data contained in the table
and basically nothing else, so if you're dealing with a table at runtime, it's probably a TableInst. The WASM
spec has a lot of similar-sounding names for different versions of one thing.
https://www.w3.org/TR/wasm-core-1/#table-instances%E2%91%A0
"""
#: A list of FuncAddrs (any of which can be None) that point to funcs in the Store
elem: typing.List[FuncElem]
max: typing.Optional[U32] #: Optional maximum size of the table
class MemInst(Eventful):
"""
Runtime representation of a memory. As with tables, if you're dealing with a memory at runtime, it's probably a
MemInst. Currently doesn't support any sort of symbolic indexing, although you can read and write symbolic bytes
using smtlib. There's a minor quirk where uninitialized data is stored as bytes, but smtlib tries to convert
concrete data into ints. That can cause problems if you try to read from the memory directly (without using smtlib)
but shouldn't break any of the built-in WASM instruction implementations.
Memory in WASM is broken up into 65536-byte pages. All pages behave the same way, but note that operations that
deal with memory size do so in terms of pages, not bytes.
TODO: We should implement some kind of symbolic memory model
https://www.w3.org/TR/wasm-core-1/#memory-instances%E2%91%A0
"""
_published_events = {"write_memory", "read_memory"}
_pages: typing.Dict[int, typing.List[int]]
max: typing.Optional[U32] #: Optional maximum number of pages the memory can contain
_current_size: int # Tracks the theoretical size of the memory instance, including unmapped pages
def __init__(self, starting_data, max=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self._current_size = ceil(len(starting_data) / PAGESIZE)
self.max = max
self._pages = {}
chunked = [starting_data[i : i + PAGESIZE] for i in range(0, len(starting_data), PAGESIZE)]
for idx, page in enumerate(chunked):
if len(page) < PAGESIZE:
page.extend([0x0] * (PAGESIZE - len(page)))
self._pages[idx] = page
def __getstate__(self):
state = super().__getstate__()
state["pages"] = self._pages
state["max"] = self.max
state["current"] = self._current_size
return state
def __setstate__(self, state):
super().__setstate__(state)
self._pages = state["pages"]
self.max = state["max"]
self._current_size = state["current"]
def __contains__(self, item):
return item in range(self.npages * PAGESIZE)
def _check_initialize_index(self, memidx):
page = memidx // PAGESIZE
if page not in range(self.npages):
raise OutOfBoundsMemoryTrap(memidx)
if page not in self._pages:
self._pages[page] = [0x0] * PAGESIZE
return divmod(memidx, PAGESIZE)
def _read_byte(self, addr):
page, idx = self._check_initialize_index(addr)
return self._pages[page][idx]
def _write_byte(self, addr, val):
page, idx = self._check_initialize_index(addr)
self._pages[page][idx] = val
@property
def npages(self):
return self._current_size
def grow(self, n: int) -> bool:
"""
Adds n blank pages to the current memory
See: https://www.w3.org/TR/wasm-core-1/#grow-mem
:param n: The number of pages to attempt to add
:return: True if the operation succeeded, otherwise False
"""
ln = n + self.npages
if ln > (PAGESIZE):
return False
if self.max is not None:
if ln > self.max:
return False
self._current_size = ln
return True
def write_int(self, base: int, expression: typing.Union[Expression, int], size: int = 32):
"""
Writes an integer into memory.
:param base: Index to write at
:param expression: integer to write
:param size: Optional size of the integer
"""
b = [
Operators.CHR(Operators.EXTRACT(expression, offset, 8)) for offset in range(0, size, 8)
]
self.write_bytes(base, b)
def write_bytes(
self, base: int, data: typing.Union[str, typing.Sequence[int], typing.Sequence[bytes]]
):
"""
Writes a stream of bytes into memory
:param base: Index to start writing at
:param data: Data to write
"""
self._publish("will_write_memory", base, base + len(data), data)
for idx, v in enumerate(data):
self._write_byte(base + idx, v)
self._publish("did_write_memory", base, data)
def read_int(self, base: int, size: int = 32) -> int:
"""
Reads bytes from memory and combines them into an int
:param base: Address to read the int from
:param size: Size of the int (in bits)
:return: The int in question
"""
return Operators.CONCAT(
size, *map(Operators.ORD, reversed(self.read_bytes(base, size // 8)))
)
def read_bytes(self, base: int, size: int) -> typing.List[typing.Union[int, bytes]]:
"""
Reads bytes from memory
:param base: Address to read from
:param size: number of bytes to read
:return: List of bytes
"""
self._publish("will_read_memory", base, base + size)
d = [self._read_byte(i) for i in range(base, base + size)]
self._publish("did_read_memory", base, d)
return d
def dump(self):
return self.read_bytes(0, self._current_size * PAGESIZE)
@dataclass
class GlobalInst:
"""
Instance of a global variable. Stores the value (calculated from evaluating a Global.init) and the mutable flag
(taken from GlobalType.mut)
https://www.w3.org/TR/wasm-core-1/#global-instances%E2%91%A0
"""
value: Value #: The actual value of this global
mut: bool #: Whether the global can be modified
@dataclass
class ExportInst:
"""
Runtime representation of any thing that can be exported
https://www.w3.org/TR/wasm-core-1/#export-instances%E2%91%A0
"""
name: Name #: The name to export under
value: ExternVal #: FuncAddr, TableAddr, MemAddr, or GlobalAddr
class Store:
"""
Implementation of the WASM store. Nothing fancy here, just collects lists of functions, tables, memories, and
globals. Because the store is not atomic, instructions SHOULD NOT make changes to the Store or any of its contents
(including memories and global variables) before raising a Concretize exception.
https://www.w3.org/TR/wasm-core-1/#store%E2%91%A0
"""
__slots__ = ["funcs", "tables", "mems", "globals"]
funcs: typing.List[ProtoFuncInst]
tables: typing.List[TableInst]
mems: typing.List[MemInst]
globals: typing.List[GlobalInst]
def __init__(self):
self.funcs = []
self.tables = []
self.mems = []
self.globals = []
def __getstate__(self):
state = {
"funcs": self.funcs,
"tables": self.tables,
"mems": self.mems,
"globals": self.globals,
}
return state
def __setstate__(self, state):
self.funcs = state["funcs"]
self.tables = state["tables"]
self.mems = state["mems"]
self.globals = state["globals"]
def _eval_maybe_symbolic(constraints, expression) -> bool:
if issymbolic(expression):
return solver.must_be_true(constraints, expression)
return True if expression else False
class ModuleInstance(Eventful):
"""
Runtime instance of a module. Stores function types, list of addresses within the store, and exports. In this
implementation, it's also responsible for managing the instruction queue and executing control-flow instructions.
https://www.w3.org/TR/wasm-core-1/#module-instances%E2%91%A0
"""
__slots__ = [
"types",
"funcaddrs",
"tableaddrs",
"memaddrs",
"globaladdrs",
"exports",
"export_map",
"executor",
"function_names",
"local_names",
"_instruction_queue",
"_block_depths",
"_advice",
"_state",
]
_published_events = {"execute_instruction", "call_hostfunc", "exec_expression", "raise_trap"}
#: Stores the type signatures of all the functions
types: typing.List[FunctionType]
#: Stores the *indices* of functions within the store
funcaddrs: typing.List[FuncAddr]
#: Stores the indices of tables
tableaddrs: typing.List[TableAddr]
#: Stores the indices of memories (at time of writing, WASM only allows one memory)
memaddrs: typing.List[MemAddr]
#: Stores the indices of globals
globaladdrs: typing.List[GlobalAddr]
#: Stores records of everything exported by this module
exports: typing.List[ExportInst]
#: Maps the names of exports to their index in the list of exports
export_map: typing.Dict[str, int]
#: Contains instruction implementations for all non-control-flow instructions
executor: Executor
#: Stores names of store functions, if available
function_names: typing.Dict[FuncAddr, str]
#: Stores names of local variables, if available
local_names: typing.Dict[FuncAddr, typing.Dict[int, str]]
#: Stores the unpacked sequence of instructions in the order they should be executed
_instruction_queue: typing.Deque[Instruction]
#: Keeps track of the current call depth, both for functions and for code blocks. Each function call is represented
# by appending another 0 to the list, and each code block is represented by incrementing the digit at the end of the
# list. This is necessary because we unroll the WASM S-Expressions (nested execution trees) into a single
# instruction queue, so the meaning of an `end` instruction could be otherwise ambiguous. Loosely put, the sum of
# all the digits in this list is the number of `end` instructions we need to see before execution is finished.
_block_depths: typing.List[int]
#: Stickies the advice conditions before each instruction.
_advice: typing.Optional[typing.List[bool]]
#: Prevents the user from invoking functions before instantiation
instantiated: bool
#: Stickies the current state before each instruction
_state: State
def __init__(self, constraints=None):
self.types = []
self.funcaddrs = []
self.tableaddrs = []
self.memaddrs = []
self.globaladdrs = []
self.exports = []
self.export_map = {}
self.executor = Executor(constraints)
self.function_names = {}
self.local_names = {}
self._instruction_queue = deque()
self._block_depths = [0]
self._advice = None
self._state = None
super().__init__()
def __getstate__(self):
state = super().__getstate__()
state.update(
{
"types": self.types,
"funcaddrs": self.funcaddrs,
"tableaddrs": self.tableaddrs,
"memaddrs": self.memaddrs,
"globaladdrs": self.globaladdrs,
"exports": self.exports,
"export_map": self.export_map,
"executor": self.executor,
"function_names": self.function_names,
"local_names": self.local_names,
"_instruction_queue": self._instruction_queue,
"_block_depths": self._block_depths,
}
)
return state
def __setstate__(self, state):
self.types = state["types"]
self.funcaddrs = state["funcaddrs"]
self.tableaddrs = state["tableaddrs"]
self.memaddrs = state["memaddrs"]
self.globaladdrs = state["globaladdrs"]
self.exports = state["exports"]
self.export_map = state["export_map"]
self.executor = state["executor"]
self.function_names = state["function_names"]
self.local_names = state["local_names"]
self._instruction_queue = state["_instruction_queue"]
self._block_depths = state["_block_depths"]
self._advice = None
self._state = None
super().__setstate__(state)
def reset_internal(self):
"""
Empties the instruction queue and clears the block depths
"""
self._instruction_queue.clear()
self._block_depths = [0]
def instantiate(
self,
store: Store,
module: "Module",
extern_vals: typing.List[ExternVal],
exec_start: bool = False,
):
"""
Type checks the module, evaluates globals, performs allocation, and puts the element and data sections into
their proper places. Optionally calls the start function _outside_ of a symbolic context if exec_start is true.
https://www.w3.org/TR/wasm-core-1/#instantiation%E2%91%A1
:param store: The store to place the allocated contents in
:param module: The WASM Module to instantiate in this instance
:param extern_vals: Imports needed to instantiate the module
:param exec_start: whether or not to execute the start section (if present)
"""
# #1 Assert: module is valid
assert module # close enough
# TODO: #2 Assert: module is valid with external types _externtype_ classifying its imports.
# for ext in module.imports:
# assert isinstance(ext, ExternType.__args__)
# #3 Assert the same number of imports and external values
assert len(module.imports) == len(
extern_vals
), f"Expected {len(module.imports)} imports, got {len(extern_vals)}"
# #4 TODO
# #5 - evaluate globals
stack = Stack()
aux_mod = ModuleInstance()
aux_mod.globaladdrs = [i for i in extern_vals if isinstance(i, GlobalAddr)]
aux_frame = Frame([], aux_mod)
stack.push(Activation(1, aux_frame))
vals = [self.exec_expression(store, stack, gb.init) for gb in module.globals]
last_frame = stack.pop()
assert isinstance(last_frame, Activation)
assert last_frame.frame == aux_frame
# #6, #7, #8 - Allocate all the things
self.allocate(store, module, extern_vals, vals)
f = Frame(locals=[], module=self)
stack.push(Activation(0, f))
# #9 & #13 - emplace element sections into tables
for elem in module.elem:
eoval = self.exec_expression(store, stack, elem.offset)
assert isinstance(eoval, I32)
assert elem.table in range(len(self.tableaddrs))
tableaddr: TableAddr = self.tableaddrs[elem.table]
assert tableaddr in range(len(store.tables))
tableinst: TableInst = store.tables[tableaddr]
eend = eoval + len(elem.init)
assert eend <= len(tableinst.elem)
func_idx: FuncIdx
for j, func_idx in enumerate(elem.init):
assert func_idx in range(len(self.funcaddrs))
funcaddr = self.funcaddrs[func_idx]
tableinst.elem[eoval + j] = funcaddr
# #10 & #14 - emplace data sections into memory
for data in module.data:
doval = self.exec_expression(store, stack, data.offset)
assert isinstance(doval, I32), f"{type(doval)} is not an I32"
assert data.data in range(len(self.memaddrs))
memaddr = self.memaddrs[data.data]
assert memaddr in range(len(store.mems))
meminst = store.mems[memaddr]
dend = doval + len(data.init)
assert dend <= meminst.npages * (PAGESIZE)
meminst.write_bytes(doval, data.init)
# #11 & #12
last_frame = stack.pop()
assert isinstance(last_frame, Activation)
assert last_frame.frame == f
# #15 run start function
if module.start is not None:
assert module.start in range(len(self.funcaddrs))
funcaddr = self.funcaddrs[module.start]
assert funcaddr in range(len(store.funcs))
self.invoke(stack, self.funcaddrs[module.start], store, [])
if exec_start:
stack.push(self.exec_expression(store, stack, []))
logger.info("Initialization Complete")
def allocate(
self,
store: Store,
module: "Module",
extern_vals: typing.List[ExternVal],
values: typing.List[Value],
):
"""
Inserts imports into the store, then creates and inserts function instances, table instances, memory instances,
global instances, and export instances.
https://www.w3.org/TR/wasm-core-1/#allocation%E2%91%A0
https://www.w3.org/TR/wasm-core-1/#modules%E2%91%A6
:param store: The Store to put all of the allocated subcomponents in
:param module: Tne Module containing all the items to allocate
:param extern_vals: Imported values
:param values: precalculated global values
"""
self.types = module.types
for ev in extern_vals:
if isinstance(ev, FuncAddr):
self.funcaddrs.append(ev)
if isinstance(ev, TableAddr):
self.tableaddrs.append(ev)
if isinstance(ev, MemAddr):
self.memaddrs.append(ev)
if isinstance(ev, GlobalAddr):
self.globaladdrs.append(ev)
for func in module.funcs:
addr = func.allocate(store, self)
self.funcaddrs.append(addr)
name = module.function_names.get(addr, None)
if name:
self.function_names[addr] = name
local_map = module.local_names.get(addr, None)
if local_map:
self.local_names[addr] = local_map.copy()
for table_i in module.tables:
self.tableaddrs.append(table_i.allocate(store))
for memory_i in module.mems:
self.memaddrs.append(memory_i.allocate(store))
for idx, global_i in enumerate(module.globals):