Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 107 additions & 14 deletions TBInit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from utils import Colors, init_interpret
from OpCodes import WASM_OP_Code
from section_structs import Code_Section, Func_Body
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.
Expand Down Expand Up @@ -183,13 +183,40 @@ def __init__(self, module):
self.module = module

def TypeSection(self):
pass
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):
pass
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):
pass
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
Expand All @@ -198,13 +225,69 @@ def MemorySection(self):
pass

def GlobalSection(self):
pass
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):
pass
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):
pass
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
Expand All @@ -219,14 +302,24 @@ def TBCustom(self):
pass

def ValidateAll(self):
self.TypeSection()
self.ImportSection()
self.FunctionSection()
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()
self.GlobalSection()
self.ExportSection()
self.StartSection()
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()
Expand Down
22 changes: 15 additions & 7 deletions argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ def ReadCodeSection(self):
if whatever[0] == 10:
code_section = whatever.copy()
section_exists = True
break

if not section_exists:
return None
Expand Down Expand Up @@ -677,6 +678,7 @@ def ReadDataSection(self):
if whatever[0] == 11:
data_section = whatever.copy()
section_exists = True
break

if not section_exists:
return None
Expand Down Expand Up @@ -751,11 +753,11 @@ def ReadImportSection(self):
temp_import_entry.kind = kind

# function type
if kind == 0:
if kind == External_Kind.FUNCTION:
import_type, offset, dummy = Read(import_section[6], offset, 'varuint32')
temp_import_entry.type = import_type
# table type
elif kind == 1:
elif kind == External_Kind.TABLE:
table_type = Table_Type()
table_type.elemet_type, offset, dummy = Read(import_section[6], offset, 'varint7')
rsz_limits = Resizable_Limits()
Expand All @@ -765,7 +767,7 @@ def ReadImportSection(self):
rsz_limits.maximum, offset, dummy = Read(import_section[6], offset, 'varuint32')
table_type.limit = rsz_limits
temp_import_entry.type = table_type
elif kind == 2:
elif kind == External_Kind.MEMORY:
memory_type = Memory_Type()
rsz_limits = Resizable_Limits()
rsz_limits.flags, offset, dummy = Read(import_section[6], offset, 'varuint1')
Expand All @@ -774,7 +776,7 @@ def ReadImportSection(self):
rsz_limits.maximum, offset, dummy = Read(import_section[6], offset, 'varuint32')
memory_type.limits = rsz_limits
temp_import_entry.type = memory_type
elif kind == 3:
elif kind == External_Kind.GLOBAL:
global_type = Global_Type()
global_type.content_type, offset, dummy = Read(import_section[6], offset, 'uint8')
global_type.mutability, offset, dummy = Read(import_section[6], offset, 'varuint1')
Expand All @@ -798,6 +800,7 @@ def ReadExportSection(self):
if whatever[0] == 7:
export_section = whatever.copy()
section_exists = True
break

if not section_exists:
return None
Expand All @@ -811,7 +814,7 @@ def ReadExportSection(self):

for i in range(0, field_length):
field_name.append(export_section[6][offset + i])
temp_export_entry.fiels_str = field_name
temp_export_entry.field_str = field_name
offset += field_length

kind, offset, dummy = Read(export_section[6], offset, 'uint8')
Expand All @@ -838,6 +841,7 @@ def ReadTypeSection(self):
if whatever[0] == 1:
type_section = whatever.copy()
section_exists = True
break

if not section_exists:
return None
Expand Down Expand Up @@ -883,6 +887,7 @@ def ReadFunctionSection(self):
if whatever[0] == 3:
function_section = whatever.copy()
section_exists = True
break

if not section_exists:
return None
Expand Down Expand Up @@ -911,6 +916,7 @@ def ReadElementSection(self):
if whatever[0] == 9:
element_section = whatever.copy()
section_exists = True
break

if not section_exists:
return None
Expand Down Expand Up @@ -958,6 +964,7 @@ def ReadMemorySection(self):
if whatever[0] == 5:
memory_section = whatever.copy()
section_exists = True
break

if not section_exists:
return None
Expand Down Expand Up @@ -1033,6 +1040,7 @@ def ReadGlobalSection(self):
if whatever[0] == 6:
global_section = whatever.copy()
section_exists = True
break

if not section_exists:
return None
Expand All @@ -1058,7 +1066,6 @@ def ReadGlobalSection(self):
temp_gl_var.global_type = temp_gl_type
GS.global_variables.append(deepcopy(temp_gl_var))


count -= 1
loop = True
init_expr = []
Expand All @@ -1074,6 +1081,7 @@ def ReadStartSection(self):
if whatever[0] == 8:
start_section = whatever.copy()
section_exists = True
break

if not section_exists:
return None
Expand Down Expand Up @@ -1288,7 +1296,7 @@ def dump_sections(self, module):

# palceholder for the validation tests
def runValidations(self):
modulevalidation = ModuleValidation(self.modules[0])
modulevalidation = ModuleValidation(self.modules[-1])
return(modulevalidation.ValidateAll())


Expand Down
46 changes: 41 additions & 5 deletions section_structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ def __init__(self):


class External_Kind():
def __init__(self):
self.Function = 0
self.Table = 1
self.Memory = 2
self.Global = 3
FUNCTION = 0
TABLE = 1
MEMORY = 2
GLOBAL = 3


class Global_Kind():
IMMUTABLE = 0
MUTABLE = 1


class Memory_Type():
Expand Down Expand Up @@ -224,6 +228,11 @@ class Module():
def __init__(self, type_section, import_section, function_section,
table_section, memory_section, global_section, export_section,
start_section, element_section, code_section, data_section):
self.function_index_space = []
self.global_index_space = []
self.memory_index_space = []
self.table_index_space = []

self.type_section = type_section
self.import_section = import_section
self.function_section = function_section
Expand All @@ -235,3 +244,30 @@ def __init__(self, type_section, import_section, function_section,
self.element_section = element_section
self.code_section = code_section
self.data_section = data_section

if self.import_section is not None:
for entry in self.import_section.import_entry:
if entry.kind == External_Kind.FUNCTION:
self.function_index_space.append(entry)
if entry.kind == External_Kind.TABLE:
self.table_index_space.append(entry)
if entry.kind == External_Kind.MEMORY:
self.memory_index_space.append(entry)
if entry.kind == External_Kind.GLOBAL:
self.global_index_space.append(entry)

if self.function_section is not None:
for entry in self.function_section.type_section_index:
self.function_index_space.append(entry)

if self.table_section is not None:
for entry in self.table_section.table_types:
self.table_index_space.append(entry)

if self.memory_section is not None:
for entry in self.memory_section.memory_types:
self.memory_index_space.append(entry)

if self.global_section is not None:
for entry in self.global_section.global_variables:
self.global_index_space.append(entry)
Loading