Removes context parameter from instruction-scoped operations (#923)

We originally had a `context.Context` for anything that might be
traced, but it turned out to be only useful for lifecycle and host functions.

For instruction-scoped aspects like memory updates, a context parameter is too
fine-grained and also invisible in practice. For example, most users will use
the compiler engine, and its memory, global or table access will never use go's
context.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-12-14 16:08:07 +09:00
committed by GitHub
parent 0ed4002549
commit 126bd9050d
59 changed files with 526 additions and 578 deletions

View File

@@ -244,11 +244,11 @@ func createRuntime(b *testing.B, config wazero.RuntimeConfig) wazero.Runtime {
}
offset := uint32(results[0])
m.Memory().WriteUint32Le(ctx, retBufPtr, offset)
m.Memory().WriteUint32Le(ctx, retBufSize, 10)
m.Memory().WriteUint32Le(retBufPtr, offset)
m.Memory().WriteUint32Le(retBufSize, 10)
b := make([]byte, 10)
_, _ = rand.Read(b)
m.Memory().Write(ctx, offset, b)
m.Memory().Write(offset, b)
}
r := wazero.NewRuntimeWithConfig(testCtx, config)

View File

@@ -149,8 +149,8 @@ func setupHostCallBench(requireNoError func(error)) *wasm.ModuleInstance {
CodeSection: []*wasm.Code{
{
IsHostFunction: true,
GoFunc: api.GoModuleFunc(func(ctx context.Context, mod api.Module, stack []uint64) {
ret, ok := mod.Memory().ReadUint32Le(ctx, uint32(stack[0]))
GoFunc: api.GoModuleFunc(func(_ context.Context, mod api.Module, stack []uint64) {
ret, ok := mod.Memory().ReadUint32Le(uint32(stack[0]))
if !ok {
panic("couldn't read memory")
}
@@ -158,8 +158,8 @@ func setupHostCallBench(requireNoError func(error)) *wasm.ModuleInstance {
}),
},
wasm.MustParseGoReflectFuncCode(
func(ctx context.Context, m api.Module, pos uint32) float32 {
ret, ok := m.Memory().ReadUint32Le(ctx, pos)
func(_ context.Context, m api.Module, pos uint32) float32 {
ret, ok := m.Memory().ReadUint32Le(pos)
if !ok {
panic("couldn't read memory")
}

View File

@@ -8,13 +8,13 @@ import (
func BenchmarkMemory(b *testing.B) {
mem := &wasm.MemoryInstance{Buffer: make([]byte, wasm.MemoryPageSize), Min: 1}
if !mem.WriteByte(testCtx, 10, 16) {
if !mem.WriteByte(10, 16) {
b.Fail()
}
b.Run("ReadByte", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if v, ok := mem.ReadByte(testCtx, 10); !ok || v != 16 {
if v, ok := mem.ReadByte(10); !ok || v != 16 {
b.Fail()
}
}
@@ -22,7 +22,7 @@ func BenchmarkMemory(b *testing.B) {
b.Run("ReadUint32Le", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if v, ok := mem.ReadUint32Le(testCtx, 10); !ok || v != 16 {
if v, ok := mem.ReadUint32Le(10); !ok || v != 16 {
b.Fail()
}
}
@@ -30,7 +30,7 @@ func BenchmarkMemory(b *testing.B) {
b.Run("WriteByte", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if !mem.WriteByte(testCtx, 10, 16) {
if !mem.WriteByte(10, 16) {
b.Fail()
}
}
@@ -38,7 +38,7 @@ func BenchmarkMemory(b *testing.B) {
b.Run("WriteUint32Le", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if !mem.WriteUint32Le(testCtx, 10, 16) {
if !mem.WriteUint32Le(10, 16) {
b.Fail()
}
}

View File

@@ -211,7 +211,7 @@ func testRecursiveEntry(t *testing.T, r wazero.Runtime) {
func testHostFuncMemory(t *testing.T, r wazero.Runtime) {
var memory *wasm.MemoryInstance
storeInt := func(ctx context.Context, m api.Module, offset uint32, val uint64) uint32 {
if !m.Memory().WriteUint64Le(ctx, offset, val) {
if !m.Memory().WriteUint64Le(offset, val) {
return 1
}
// sneak a reference to the memory, so we can check it later
@@ -571,7 +571,7 @@ func testMemOps(t *testing.T, r wazero.Runtime) {
results, err := sizeFn.Call(testCtx)
require.NoError(t, err)
require.Zero(t, results[0])
require.Zero(t, memory.Size(testCtx))
require.Zero(t, memory.Size())
// Any offset should be out of bounds error even when it is less than memory capacity(=memoryCapacityPages).
_, err = storeFn.Call(testCtx, wasm.MemoryPagesToBytesNum(memoryCapacityPages)-8)
@@ -589,8 +589,8 @@ func testMemOps(t *testing.T, r wazero.Runtime) {
// Check the size command works!
results, err = sizeFn.Call(testCtx)
require.NoError(t, err)
require.Equal(t, uint64(1), results[0]) // 1 page
require.Equal(t, uint32(65536), memory.Size(testCtx)) // 64KB
require.Equal(t, uint64(1), results[0]) // 1 page
require.Equal(t, uint32(65536), memory.Size()) // 64KB
// Grow again so that the memory size matches memory capacity.
results, err = growFn.Call(testCtx, 1)
@@ -633,7 +633,7 @@ func testMultipleInstantiation(t *testing.T, r wazero.Runtime) {
defer module.Close(testCtx)
// Ensure that compilation cache doesn't cause race on memory instance.
before, ok := module.Memory().ReadUint64Le(testCtx, 1)
before, ok := module.Memory().ReadUint64Le(1)
require.True(t, ok)
// Value must be zero as the memory must not be affected by the previously instantiated modules.
require.Zero(t, before)
@@ -645,7 +645,7 @@ func testMultipleInstantiation(t *testing.T, r wazero.Runtime) {
require.NoError(t, err)
// After the call, the value must be set properly.
after, ok := module.Memory().ReadUint64Le(testCtx, 1)
after, ok := module.Memory().ReadUint64Le(1)
require.True(t, ok)
require.Equal(t, uint64(1000), after)
}

View File

@@ -48,7 +48,7 @@ func (fs *wasiFs) Open(name string) (fs.File, error) {
pathBytes := []byte(name)
// Pick anywhere in memory to write the path to.
pathPtr := uint32(0)
ok := fs.memory.Write(testCtx, pathPtr, pathBytes)
ok := fs.memory.Write(pathPtr, pathBytes)
require.True(fs.t, ok)
resultOpenedFd := pathPtr + uint32(len(pathBytes))
@@ -66,7 +66,7 @@ func (fs *wasiFs) Open(name string) (fs.File, error) {
require.NoError(fs.t, err)
require.Equal(fs.t, uint64(wasi_snapshot_preview1.ErrnoSuccess), res[0])
resFd, ok := fs.memory.ReadUint32Le(testCtx, resultOpenedFd)
resFd, ok := fs.memory.ReadUint32Le(resultOpenedFd)
require.True(fs.t, ok)
return &wasiFile{fd: resFd, fs: fs}, nil
@@ -96,10 +96,10 @@ func (f *wasiFile) Read(bytes []byte) (int, error) {
// iov starts at iovsOff + 8 because we first write four bytes for the offset itself, and
// four bytes for the length of the iov.
iovOff := iovsOff + uint32(8)
ok := f.fs.memory.WriteUint32Le(testCtx, iovsOff, iovOff)
ok := f.fs.memory.WriteUint32Le(iovsOff, iovOff)
require.True(f.fs.t, ok)
// next write the length.
ok = f.fs.memory.WriteUint32Le(testCtx, iovsOff+uint32(4), uint32(len(bytes)))
ok = f.fs.memory.WriteUint32Le(iovsOff+uint32(4), uint32(len(bytes)))
require.True(f.fs.t, ok)
res, err := f.fs.fdRead.Call(testCtx, uint64(f.fd), uint64(iovsOff), uint64(iovsCount), uint64(resultSizeOff))
@@ -107,7 +107,7 @@ func (f *wasiFile) Read(bytes []byte) (int, error) {
require.NotEqual(f.fs.t, uint64(wasi_snapshot_preview1.ErrnoFault), res[0])
numRead, ok := f.fs.memory.ReadUint32Le(testCtx, resultSizeOff)
numRead, ok := f.fs.memory.ReadUint32Le(resultSizeOff)
require.True(f.fs.t, ok)
if numRead == 0 {
@@ -121,7 +121,7 @@ func (f *wasiFile) Read(bytes []byte) (int, error) {
}
}
buf, ok := f.fs.memory.Read(testCtx, iovOff, numRead)
buf, ok := f.fs.memory.Read(iovOff, numRead)
require.True(f.fs.t, ok)
copy(bytes, buf)
return int(numRead), nil
@@ -142,7 +142,7 @@ func (f *wasiFile) Seek(offset int64, whence int) (int64, error) {
require.NoError(f.fs.t, err)
require.NotEqual(f.fs.t, uint64(wasi_snapshot_preview1.ErrnoFault), res[0])
newOffset, ok := f.fs.memory.ReadUint32Le(testCtx, resultNewoffsetOff)
newOffset, ok := f.fs.memory.ReadUint32Le(resultNewoffsetOff)
require.True(f.fs.t, ok)
return int64(newOffset), nil

View File

@@ -325,7 +325,7 @@ func Test733(t *testing.T) {
mem := mod.Memory()
require.NotNil(t, mem)
v, ok := mem.ReadUint64Le(ctx, 0x80000100)
v, ok := mem.ReadUint64Le(0x80000100)
require.True(t, ok)
require.Equal(t, uint64(0xffffffffffffffff), v)
})

View File

@@ -492,7 +492,7 @@ func Run(t *testing.T, testDataFS embed.FS, ctx context.Context, newEngine func(
expType = wasm.ValueTypeF64
}
require.Equal(t, expType, global.Type(), msg)
require.Equal(t, exps[0], global.Get(ctx), msg)
require.Equal(t, exps[0], global.Get(), msg)
default:
t.Fatalf("unsupported action type type: %v", c)
}

View File

@@ -46,7 +46,7 @@ func allocationCall(m Module, _ int) error {
}
// The pointer is a linear memory offset, which is where we write the name.
if err = m.WriteMemory(testCtx, namePtr, []byte(allocationParam)); err != nil {
if err = m.WriteMemory(namePtr, []byte(allocationParam)); err != nil {
return err
}

View File

@@ -40,7 +40,7 @@ type Module interface {
CallI32_V(ctx context.Context, funcName string, param uint32) error
CallV_V(ctx context.Context, funcName string) error
CallI64_I64(ctx context.Context, funcName string, param uint64) (uint64, error)
WriteMemory(ctx context.Context, offset uint32, bytes []byte) error
WriteMemory(offset uint32, bytes []byte) error
Memory() []byte
Close(context.Context) error
}
@@ -79,10 +79,10 @@ func (m *wazeroModule) Memory() []byte {
return m.mod.Memory().(*wasm.MemoryInstance).Buffer
}
func (r *wazeroRuntime) log(ctx context.Context, mod api.Module, stack []uint64) {
func (r *wazeroRuntime) log(_ context.Context, mod api.Module, stack []uint64) {
offset, byteCount := uint32(stack[0]), uint32(stack[1])
buf, ok := mod.Memory().Read(ctx, offset, byteCount)
buf, ok := mod.Memory().Read(offset, byteCount)
if !ok {
panic("out of memory reading log buffer")
}
@@ -197,8 +197,8 @@ func (m *wazeroModule) CallI64_I64(ctx context.Context, funcName string, param u
return 0, nil
}
func (m *wazeroModule) WriteMemory(ctx context.Context, offset uint32, bytes []byte) error {
if !m.mod.Memory().Write(ctx, offset, bytes) {
func (m *wazeroModule) WriteMemory(offset uint32, bytes []byte) error {
if !m.mod.Memory().Write(offset, bytes) {
return errors.New("out of memory writing name")
}
return nil

View File

@@ -173,7 +173,7 @@ func (m *wasmedgeModule) CallI64_I64(_ context.Context, funcName string, param u
}
}
func (m *wasmedgeModule) WriteMemory(_ context.Context, offset uint32, bytes []byte) error {
func (m *wasmedgeModule) WriteMemory(offset uint32, bytes []byte) error {
mod := m.vm.GetActiveModule()
mem := mod.FindMemory("memory")
if unsafeSlice, err := mem.GetData(uint(offset), uint(len(bytes))); err != nil {

View File

@@ -185,7 +185,7 @@ func (m *wasmerModule) CallI64_I64(_ context.Context, funcName string, param uin
}
}
func (m *wasmerModule) WriteMemory(_ context.Context, offset uint32, bytes []byte) error {
func (m *wasmerModule) WriteMemory(offset uint32, bytes []byte) error {
unsafeSlice := m.mem.Data()
copy(unsafeSlice[offset:], bytes)
return nil

View File

@@ -187,7 +187,7 @@ func (m *wasmtimeModule) CallI64_I64(_ context.Context, funcName string, param u
}
}
func (m *wasmtimeModule) WriteMemory(_ context.Context, offset uint32, bytes []byte) error {
func (m *wasmtimeModule) WriteMemory(offset uint32, bytes []byte) error {
unsafeSlice := m.mem.UnsafeData(m.store)
copy(unsafeSlice[offset:], bytes)
return nil