Optimize setting the ModuleInstance.DataInstances (#852)
Signed-off-by: Clifton Kaznocha <ckaznocha@users.noreply.github.com>
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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)}}
|
||||
|
||||
Reference in New Issue
Block a user