Skip to content

Commit 83480e7

Browse files
committed
Add validation for ExportSection
* Each export's name is required to be unique among all the exports' names. * Each export's index is required to be within the bounds of its associated index space. Also populate module index spaces in Module constructor.
1 parent 02ab642 commit 83480e7

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

TBInit.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,28 @@ def GlobalSection(self):
214214
pass
215215

216216
def ExportSection(self):
217-
pass
217+
section = self.module.export_section
218+
name_list = {}
219+
for entry in section.export_entries:
220+
name = ''.join(chr(c) for c in entry.field_str)
221+
if name in name_list:
222+
return(False)
223+
name_list[name]=None
224+
225+
index = entry.index
226+
if entry.kind == External_Kind.FUNCTION:
227+
if index >= len(self.module.function_index_space):
228+
return(False)
229+
if entry.kind == External_Kind.TABLE:
230+
if index >= len(self.module.table_index_space):
231+
return(False)
232+
if entry.kind == External_Kind.MEMORY:
233+
if index >= len(self.module.memory_index_space):
234+
return(False)
235+
if entry.kind == External_Kind.GLOBAL:
236+
if index >= len(self.module.global_index_space):
237+
return(False)
238+
return(True)
218239

219240
def StartSection(self):
220241
pass
@@ -237,7 +258,8 @@ def ValidateAll(self):
237258
self.TableSection()
238259
self.MemorySection()
239260
self.GlobalSection()
240-
self.ExportSection()
261+
if not self.ExportSection():
262+
return(False)
241263
self.StartSection()
242264
self.ElementSection()
243265
self.CodeSection()

argparser.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -748,11 +748,11 @@ def ReadImportSection(self):
748748
temp_import_entry.kind = kind
749749

750750
# function type
751-
if kind == 0:
751+
if kind == External_Kind.FUNCTION:
752752
import_type, offset, dummy = Read(import_section[6], offset, 'varuint32')
753753
temp_import_entry.type = import_type
754754
# table type
755-
elif kind == 1:
755+
elif kind == External_Kind.TABLE:
756756
table_type = Table_Type()
757757
table_type.elemet_type, offset, dummy = Read(import_section[6], offset, 'varint7')
758758
rsz_limits = Resizable_Limits()
@@ -762,7 +762,7 @@ def ReadImportSection(self):
762762
rsz_limits.maximum, offset, dummy = Read(import_section[6], offset, 'varuint32')
763763
table_type.limit = rsz_limits
764764
temp_import_entry.type = table_type
765-
elif kind == 2:
765+
elif kind == External_Kind.MEMORY:
766766
memory_type = Memory_Type()
767767
rsz_limits = Resizable_Limits()
768768
rsz_limits.flags, offset, dummy = Read(import_section[6], offset, 'varuint1')
@@ -771,7 +771,7 @@ def ReadImportSection(self):
771771
rsz_limits.maximum, offset, dummy = Read(import_section[6], offset, 'varuint32')
772772
memory_type.limits = rsz_limits
773773
temp_import_entry.type = memory_type
774-
elif kind == 3:
774+
elif kind == External_Kind.GLOBAL:
775775
global_type = Global_Type()
776776
global_type.content_type, offset, dummy = Read(import_section[6], offset, 'uint8')
777777
global_type.mutability, offset, dummy = Read(import_section[6], offset, 'varuint1')
@@ -1061,7 +1061,6 @@ def ReadGlobalSection(self):
10611061
temp_gl_var.global_type = temp_gl_type
10621062
GS.global_variables.append(deepcopy(temp_gl_var))
10631063

1064-
10651064
count -= 1
10661065
loop = True
10671066
init_expr = []

section_structs.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ class Module():
227227
def __init__(self, type_section, import_section, function_section,
228228
table_section, memory_section, global_section, export_section,
229229
start_section, element_section, code_section, data_section):
230+
self.function_index_space = []
231+
self.global_index_space = []
232+
self.memory_index_space = []
233+
self.table_index_space = []
234+
230235
self.type_section = type_section
231236
self.import_section = import_section
232237
self.function_section = function_section
@@ -238,3 +243,18 @@ def __init__(self, type_section, import_section, function_section,
238243
self.element_section = element_section
239244
self.code_section = code_section
240245
self.data_section = data_section
246+
247+
for entry in self.import_section.import_entry:
248+
print(entry)
249+
if entry.kind == External_Kind.FUNCTION:
250+
self.function_index_space.append(entry)
251+
if entry.kind == External_Kind.TABLE:
252+
self.table_index_space.append(entry)
253+
if entry.kind == External_Kind.MEMORY:
254+
self.memory_index_space.append(entry)
255+
if entry.kind == External_Kind.GLOBAL:
256+
self.global_index_space.append(entry)
257+
258+
for entry in self.function_section.type_section_index:
259+
print(entry)
260+
self.function_index_space.append(entry)

0 commit comments

Comments
 (0)