-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTBInit.py
More file actions
382 lines (330 loc) · 13.3 KB
/
TBInit.py
File metadata and controls
382 lines (330 loc) · 13.3 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from utils import Colors, init_interpret
from OpCodes import WASM_OP_Code
from execute import *
from section_structs import Code_Section, Func_Body, External_Kind, Global_Kind
# handles the debug option --memdump. dumps the contents of linear memories.
def DumpLinearMems(linear_memories, threshold):
count = int()
strrep = []
linmem_cnt = int()
for lin_mem in linear_memories:
print('-----------------------------------------')
print(Colors.blue + Colors.BOLD +
'Linear Memory '+ repr(linmem_cnt)+ ' :' + Colors.ENDC)
for byte in lin_mem:
if count >= threshold:
break
if count%16 == 0:
for ch in strrep:
# @DEVI-line feed messes the pretty format up
if ord(ch) != 10:
print(Colors.green + ' ' + ch + Colors.ENDC, end = '')
else:
pass
print()
strrep = []
print(Colors.cyan + hex(count), ':\t' + Colors.ENDC, end='')
strrep.append(str(chr(byte)))
print(Colors.blue + format(byte, '02x') + ' ' + Colors.ENDC, end='')
else:
strrep += str(chr(byte))
print(Colors.blue + format(byte, '02x') + ' ' + Colors.ENDC, end='')
count += 1
count = 0
print()
# handles the debug options --idxspc. dumps the index spaces.
def DumpIndexSpaces(machinestate):
print('-----------------------------------------')
print(Colors.green + 'Function Index Space: ' + Colors.ENDC)
for iter in machinestate.Index_Space_Function:
print(Colors.blue + repr(iter) + Colors.ENDC)
print('-----------------------------------------')
print(Colors.green + 'Globa Index Space: ' + Colors.ENDC)
for iter in machinestate.Index_Space_Global:
print(Colors.blue + repr(iter) + Colors.ENDC)
print('-----------------------------------------')
print(Colors.green + 'Linear Memory Index Space: ' + Colors.ENDC)
for iter in machinestate.Index_Space_Linear:
print(Colors.blue + repr(iter) + Colors.ENDC)
print('-----------------------------------------')
print(Colors.green + 'Table Index Space: ' + Colors.ENDC)
for iter in machinestate.Index_Space_Table:
print(Colors.blue + repr(iter) + Colors.ENDC)
print('-----------------------------------------')
# WIP-the Truebit Machine class
class TBMachine():
def __init__(self):
# bytearray of size PAGE_SIZE
self.Linear_Memory = []
self.Stack_Control_Flow = list()
self.Stack_Call = list()
self.Stack_Value = list()
self.Stack_Omni = list()
self.Vector_Globals = list()
self.Index_Space_Function = list()
self.Index_Space_Global = list()
self.Index_Space_Linear = list()
self.Index_Space_Table = list()
# handles the initialization of the WASM machine
class TBInit():
def __init__(self, module, machinestate):
self.module = module
self.machinestate = machinestate
# a convenience function that runs the methods of the class. all methods
# can be called separately manually as well.
def run(self):
self.InitFuncIndexSpace()
self.InitGlobalIndexSpace()
self.InitLinearMemoryIndexSpace()
self.InitTableIndexSpace()
self.InitializeLinearMemory()
def InitFuncIndexSpace(self):
if self.module.import_section is not None:
for iter in self.module.import_section.import_entry:
if iter.kind == 0:
name = str()
for i in iter.field_str:
name += str(chr(i))
self.machinestate.Index_Space_Function.append(name)
if self.module.function_section is not None:
for iter in self.module.function_section.type_section_index:
self.machinestate.Index_Space_Function.append(iter)
def InitGlobalIndexSpace(self):
if self.module.import_section is not None:
for iter in self.module.import_section.import_entry:
if iter.kind == 3:
name = str()
for i in iter.field_str:
name += str(chr(i))
self.machinestate.Index_Space_Global.append(name)
if self.module.global_section is not None:
for iter in self.module.global_section.global_variables:
self.machinestate.Index_Space_Global.append(iter.init_expr)
def InitLinearMemoryIndexSpace(self):
if self.module.import_section is not None:
for iter in self.module.import_section.import_entry:
if iter.kind == 2:
name = str()
for i in iter.field_str:
name += str(chr(i))
self.machinestate.Index_Space_Linear.append(name)
if self.module.memory_section is not None:
for iter in self.module.memory_section.memory_types:
self.machinestate.Index_Space_Linear.append(iter.initial)
def InitTableIndexSpace(self):
if self.module.import_section is not None:
for iter in self.module.import_section.import_entry:
if iter.kind == 1:
name = str()
for i in iter.field_str:
name += str(chr(i))
self.machinestate.Index_Space_Table.append(name)
if self.module.table_section is not None:
for iter in self.module.table_section.table_types:
self.machinestate.Index_Space_Table.append(iter.element_type)
def InitializeLinearMemory(self):
# @DEVI-we could try to pack the data in the linear memory ourselve to
# decrease the machinestate size
if self.module.memory_section is not None:
for iter in self.module.memory_section.memory_types:
self.machinestate.Linear_Memory.append(bytearray(
WASM_OP_Code.PAGE_SIZE))
if self.module.data_section is not None:
for iter in self.module.data_section.data_segments:
count = int()
for byte in iter.data:
self.machinestate.Linear_Memory[iter.index][init_interpret(iter.offset) + count] = byte
count += 1
# returns the machinestate
def getInits(self):
return(self.machinestate)
# WIP-holds the run-rime data structures for a wasm machine
class RTE():
def __init__(self):
Stack_Control_Flow = list()
Stack_Value = list()
Vector_Locals = list()
Current_Position = int()
Local_Stacks = list()
def genFuncLocalStack(func_body):
pass
# palceholder for the class that holds the validation functions
class ModuleValidation():
def __init__(self, module):
self.module = module
def TypeSection(self):
section = self.module.type_section
if section is None:
return(True)
for func_sig in section.func_types:
if func_sig.form != -0x20 or func_sig.return_cnt > 1:
return(False)
return True
def ImportSection(self):
section = self.module.import_section
if section is None:
return(True)
for entry in section.import_entry:
if entry.kind == External_Kind.FUNCTION:
pass
if entry.kind == External_Kind.TABLE:
pass
if entry.kind == External_Kind.MEMORY:
pass
if entry.kind == External_Kind.GLOBAL:
if entry.mutability != Global_Kind.IMMUTABLE:
return(False)
return(True)
def FunctionSection(self):
section = self.module.function_section
if section is None:
return(True)
csection = self.module.code_section
if csection is None:
return(False)
if len(section.type_section_index) != len(csection.func_bodies):
return(False)
return(True)
def TableSection(self):
pass
def MemorySection(self):
pass
def GlobalSection(self):
section = self.module.global_section
if section is None:
return(True)
for entry in section.global_variables:
desc_type = entry.global_type.content_type
init_expr = entry.init_expr
#get_global
if init_expr[0] == 0x23:
index = init_expr[1]
glob = self.module.global_index_space[index]
if desc_type != glob.content_type:
return(False)
#const
elif init_expr[0] == 0x41:
if desc_type != 0x7f:
return(False)
elif init_expr[0] == 0x42:
if desc_type != 0x7e:
return(False)
elif init_expr[0] == 0x43:
if desc_type != 0x7d:
return(False)
elif init_expr[0] == 0x44:
if desc_type != 0x7c:
return(False)
return(True)
def ExportSection(self, machinestate):
section = self.module.export_section
if section is None:
return(True)
name_list = {}
for entry in section.export_entries:
name = ''.join(chr(c) for c in entry.field_str)
if name in name_list:
return(False)
name_list[name]=None
index = entry.index
if entry.kind == External_Kind.FUNCTION:
if index >= len(machinestate.Index_Space_Function):
return(False)
if entry.kind == External_Kind.TABLE:
if index >= len(machinestate.Index_Space_Table):
return(False)
if entry.kind == External_Kind.MEMORY:
if index >= len(machinestate.Index_Space_Linear):
return(False)
if entry.kind == External_Kind.GLOBAL:
if index >= len(machinestate.Index_Space_Global):
return(False)
return(True)
def StartSection(self):
section = self.module.start_section
if section is None:
return(True)
index = section.function_section_index[0]
csection = self.module.CodeSection
if index >= len(csection.func_bodies):
return(False)
#func = self.module.function_index_space[index]
return(True)
def ElementSection(self):
pass
def CodeSection(self):
pass
def DataSection(self):
pass
def TBCustom(self):
pass
def ValidateAll(self):
machinestate = TBMachine()
init = TBInit(self.module, machinestate)
init.run()
machinestate = init.getInits()
if not self.TypeSection():
return(False)
if not self.ImportSection():
return(False)
if not self.FunctionSection():
return(False)
self.TableSection()
self.MemorySection()
if not self.GlobalSection():
return(False)
if not self.ExportSection(machinestate):
return(False)
if not self.StartSection():
return(False)
self.ElementSection()
self.CodeSection()
self.DataSection()
self.TBCustom()
return(True)
# a convinience class that handles the initialization of the wasm machine and
# interpretation of the code.
class VM():
def __init__(self, modules):
self.modules = modules
self.machinestate = TBMachine()
# @DEVI-FIXME- the first implementation is single-module only
self.init = TBInit(self.modules[0], self.machinestate)
self.init.run()
self.machinestate = self.init.getInits()
self.start_function = Func_Body()
self.executewasm = Execute(self.machinestate)
def getState(self):
return(self.machinestate)
def getStartFunctionIndex(self):
if self.modules[0].start_section is None:
raise Exception(Colors.red + "module does not have a start section. quitting..." + Colors.ENDC)
else:
print(Colors.green + "found start section: " + Colors.ENDC, end = '')
start_index = self.modules[0].start_section.function_section_index
print(start_index)
return(start_index)
def getStartFunctionBody(self):
start_index = self.getStartFunctionIndex()
if isinstance(start_index, int):
self.start_function = self.modules[0].code_section.func_bodies[start_index]
elif isinstance(start_index, str):
# we have to import the function from another module/library. we
# assume sys calls are not present.:w
pass
else:
raise Exception(Colors.red + "invalid entry for start function index" + Colors.ENDC)
def execute(self):
print(Colors.blue + 'running module...' + Colors.ENDC)
'''
for ins in self.start_function.code:
print(ins.opcode + ' ' + ins.operands)
'''
for ins in self.start_function.code:
self.executewasm.getInstruction(ins.opcodeint, ins.operands)
self.executewasm.callExecuteMethod()
self.getState()
# a convinience method
def run(self):
self.getStartFunctionBody()
self.execute()