Optimize setting the ModuleInstance.DataInstances (#852)

Signed-off-by: Clifton Kaznocha <ckaznocha@users.noreply.github.com>
This commit is contained in:
Clifton Kaznocha
2022-11-12 14:52:11 -08:00
committed by GitHub
parent 76ed3047cf
commit d507d8666f
2 changed files with 6 additions and 10 deletions

View File

@@ -181,13 +181,6 @@ func (m *ModuleInstance) addSections(module *Module, importedFunctions, function
}
m.BuildExports(module.ExportSection)
m.buildDataInstances(module.DataSection)
}
func (m *ModuleInstance) buildDataInstances(segments []*DataSegment) {
for _, d := range segments {
m.DataInstances = append(m.DataInstances, d.Init)
}
}
func (m *ModuleInstance) buildElementInstances(elements []*ElementSegment) {
@@ -237,11 +230,13 @@ func (m *ModuleInstance) validateData(data []*DataSegment) (err error) {
return
}
// applyData uses the given data segments and mutate the memory according to the initial contents on it.
// This is called after all the validation phase passes and out of bounds memory access error here is
// not a validation error, but rather a runtime error.
// applyData uses the given data segments and mutate the memory according to the initial contents on it
// and populate the `DataInstances`. This is called after all the validation phase passes and out of
// bounds memory access error here is not a validation error, but rather a runtime error.
func (m *ModuleInstance) applyData(data []*DataSegment) error {
m.DataInstances = make([][]byte, len(data))
for i, d := range data {
m.DataInstances[i] = d.Init
if !d.IsPassive() {
offset := executeConstExpression(m.Globals, d.OffsetExpression).(int32)
if offset < 0 || int(offset)+len(d.Init) > len(m.Memory.Buffer) {

View File

@@ -788,6 +788,7 @@ func TestModuleInstance_applyData(t *testing.T) {
})
require.NoError(t, err)
require.Equal(t, []byte{0xa, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x5}, m.Memory.Buffer)
require.Equal(t, [][]byte{{0xa, 0xf}, {0x1, 0x5}}, m.DataInstances)
})
t.Run("error", func(t *testing.T) {
m := &ModuleInstance{Memory: &MemoryInstance{Buffer: make([]byte, 5)}}