diff --git a/internal/wasm/store.go b/internal/wasm/store.go index ed70b4ba..233e99d9 100644 --- a/internal/wasm/store.go +++ b/internal/wasm/store.go @@ -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) { diff --git a/internal/wasm/store_test.go b/internal/wasm/store_test.go index 48296f34..00d5420e 100644 --- a/internal/wasm/store_test.go +++ b/internal/wasm/store_test.go @@ -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)}}