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

@@ -1,7 +1,6 @@
package gojs
import (
"context"
"errors"
"github.com/tetratelabs/wazero/api"
@@ -17,7 +16,7 @@ const (
// WriteArgsAndEnviron writes arguments and environment variables to memory, so
// they can be read by main, Go compiles as the function export "run".
func WriteArgsAndEnviron(ctx context.Context, mod api.Module) (argc, argv uint32, err error) {
func WriteArgsAndEnviron(mod api.Module) (argc, argv uint32, err error) {
mem := mod.Memory()
sysCtx := mod.(*wasm.CallContext).Sys
args := sysCtx.Args()
@@ -29,7 +28,7 @@ func WriteArgsAndEnviron(ctx context.Context, mod api.Module) (argc, argv uint32
strPtr := func(val []byte, field string, i int) (ptr uint32) {
// TODO: return err and format "%s[%d], field, i"
ptr = offset
mustWrite(ctx, mem, field, offset, append(val, 0))
mustWrite(mem, field, offset, append(val, 0))
offset += uint32(len(val) + 1)
if pad := offset % 8; pad != 0 {
offset += 8 - pad
@@ -50,7 +49,7 @@ func WriteArgsAndEnviron(ctx context.Context, mod api.Module) (argc, argv uint32
argv = offset
for _, ptr := range argvPtrs {
// TODO: return err and format "argvPtrs[%d], i"
mustWriteUint64Le(ctx, mem, "argvPtrs[i]", offset, uint64(ptr))
mustWriteUint64Le(mem, "argvPtrs[i]", offset, uint64(ptr))
offset += 8
}

View File

@@ -58,7 +58,7 @@ var WasmWrite = spfunc.MustCallFromSP(false, &wasm.HostFunc{
},
})
func wasmWrite(ctx context.Context, mod api.Module, stack []uint64) {
func wasmWrite(_ context.Context, mod api.Module, stack []uint64) {
fsc := mod.(*wasm.CallContext).Sys.FS()
fd, p, n := uint32(stack[0]), uint32(stack[1]), uint32(stack[2])
@@ -68,7 +68,7 @@ func wasmWrite(ctx context.Context, mod api.Module, stack []uint64) {
panic(fmt.Errorf("unexpected fd %d", fd))
}
if _, err := writer.Write(mustRead(ctx, mod.Memory(), "p", p, n)); err != nil {
if _, err := writer.Write(mustRead(mod.Memory(), "p", p, n)); err != nil {
panic(fmt.Errorf("error writing p: %w", err))
}
}
@@ -99,8 +99,8 @@ var Nanotime1 = spfunc.MustCallFromSP(false, &wasm.HostFunc{
},
})
func nanotime1(ctx context.Context, mod api.Module, stack []uint64) {
time := mod.(*wasm.CallContext).Sys.Nanotime(ctx)
func nanotime1(_ context.Context, mod api.Module, stack []uint64) {
time := mod.(*wasm.CallContext).Sys.Nanotime()
stack[0] = api.EncodeI64(time)
}
@@ -118,8 +118,8 @@ var Walltime = spfunc.MustCallFromSP(false, &wasm.HostFunc{
},
})
func walltime(ctx context.Context, mod api.Module, stack []uint64) {
sec, nsec := mod.(*wasm.CallContext).Sys.Walltime(ctx)
func walltime(_ context.Context, mod api.Module, stack []uint64) {
sec, nsec := mod.(*wasm.CallContext).Sys.Walltime()
stack[0] = api.EncodeI64(sec)
stack[1] = api.EncodeI32(nsec)
}
@@ -158,11 +158,11 @@ var GetRandomData = spfunc.MustCallFromSP(false, &wasm.HostFunc{
},
})
func getRandomData(ctx context.Context, mod api.Module, stack []uint64) {
func getRandomData(_ context.Context, mod api.Module, stack []uint64) {
randSource := mod.(*wasm.CallContext).Sys.RandSource()
buf, bufLen := uint32(stack[0]), uint32(stack[1])
r := mustRead(ctx, mod.Memory(), "r", buf, bufLen)
r := mustRead(mod.Memory(), "r", buf, bufLen)
if n, err := randSource.Read(r); err != nil {
panic(fmt.Errorf("RandSource.Read(r /* len=%d */) failed: %w", bufLen, err))

View File

@@ -248,7 +248,7 @@ func TestMustCallFromSP(t *testing.T) {
mod, err := r.InstantiateModuleFromBinary(testCtx, bin)
require.NoError(t, err)
memView, ok := mod.Memory().Read(testCtx, 0, uint32(len(spMem)))
memView, ok := mod.Memory().Read(0, uint32(len(spMem)))
require.True(t, ok)
copy(memView, spMem)

View File

@@ -107,7 +107,7 @@ func loadValue(ctx context.Context, ref ref) interface{} { // nolint
func loadArgs(ctx context.Context, mod api.Module, sliceAddr, sliceLen uint32) []interface{} { // nolint
result := make([]interface{}, 0, sliceLen)
for i := uint32(0); i < sliceLen; i++ { // nolint
iRef := mustReadUint64Le(ctx, mod.Memory(), "iRef", sliceAddr+i*8)
iRef := mustReadUint64Le(mod.Memory(), "iRef", sliceAddr+i*8)
result = append(result, loadValue(ctx, ref(iRef)))
}
return result

View File

@@ -75,7 +75,7 @@ var StringVal = spfunc.MustCallFromSP(false, &wasm.HostFunc{
func stringVal(ctx context.Context, mod api.Module, stack []uint64) {
xAddr, xLen := uint32(stack[0]), uint32(stack[1])
x := string(mustRead(ctx, mod.Memory(), "x", xAddr, xLen))
x := string(mustRead(mod.Memory(), "x", xAddr, xLen))
stack[0] = storeRef(ctx, x)
}
@@ -104,7 +104,7 @@ func valueGet(ctx context.Context, mod api.Module, stack []uint64) {
pAddr := uint32(stack[1])
pLen := uint32(stack[2])
p := string(mustRead(ctx, mod.Memory(), "p", pAddr, pLen))
p := string(mustRead(mod.Memory(), "p", pAddr, pLen))
v := loadValue(ctx, ref(vRef))
var result interface{}
@@ -150,7 +150,7 @@ func valueSet(ctx context.Context, mod api.Module, stack []uint64) {
xRef := stack[3]
v := loadValue(ctx, ref(vRef))
p := string(mustRead(ctx, mod.Memory(), "p", pAddr, pLen))
p := string(mustRead(mod.Memory(), "p", pAddr, pLen))
x := loadValue(ctx, ref(xRef))
if v == getState(ctx) {
switch p {
@@ -240,7 +240,7 @@ func valueCall(ctx context.Context, mod api.Module, stack []uint64) {
this := ref(vRef)
v := loadValue(ctx, this)
m := string(mustRead(ctx, mod.Memory(), "m", mAddr, mLen))
m := string(mustRead(mod.Memory(), "m", mAddr, mLen))
args := loadArgs(ctx, mod, argsArray, argsLen)
var xRef uint64
@@ -414,7 +414,7 @@ func valueLoadString(ctx context.Context, mod api.Module, stack []uint64) {
v := loadValue(ctx, ref(vRef))
s := valueString(v)
b := mustRead(ctx, mod.Memory(), "b", bAddr, bLen)
b := mustRead(mod.Memory(), "b", bAddr, bLen)
copy(b, s)
}
@@ -452,7 +452,7 @@ func copyBytesToGo(ctx context.Context, mod api.Module, stack []uint64) {
_ /* unknown */ = uint32(stack[2])
srcRef := stack[3]
dst := mustRead(ctx, mod.Memory(), "dst", dstAddr, dstLen) // nolint
dst := mustRead(mod.Memory(), "dst", dstAddr, dstLen) // nolint
v := loadValue(ctx, ref(srcRef))
var n, ok uint32
@@ -493,7 +493,7 @@ func copyBytesToJS(ctx context.Context, mod api.Module, stack []uint64) {
srcLen := uint32(stack[2])
_ /* unknown */ = uint32(stack[3])
src := mustRead(ctx, mod.Memory(), "src", srcAddr, srcLen) // nolint
src := mustRead(mod.Memory(), "src", srcAddr, srcLen) // nolint
v := loadValue(ctx, ref(dstRef))
var n, ok uint32

View File

@@ -1,7 +1,6 @@
package gojs
import (
"context"
"fmt"
"github.com/tetratelabs/wazero/api"
@@ -26,8 +25,8 @@ func stubFunction(name string) *wasm.HostFunc {
// mustRead is like api.Memory except that it panics if the offset and
// byteCount are out of range.
func mustRead(ctx context.Context, mem api.Memory, fieldName string, offset, byteCount uint32) []byte {
buf, ok := mem.Read(ctx, offset, byteCount)
func mustRead(mem api.Memory, fieldName string, offset, byteCount uint32) []byte {
buf, ok := mem.Read(offset, byteCount)
if !ok {
panic(fmt.Errorf("out of memory reading %s", fieldName))
}
@@ -36,8 +35,8 @@ func mustRead(ctx context.Context, mem api.Memory, fieldName string, offset, byt
// mustReadUint64Le is like api.Memory except that it panics if the offset
// is out of range.
func mustReadUint64Le(ctx context.Context, mem api.Memory, fieldName string, offset uint32) uint64 {
result, ok := mem.ReadUint64Le(ctx, offset)
func mustReadUint64Le(mem api.Memory, fieldName string, offset uint32) uint64 {
result, ok := mem.ReadUint64Le(offset)
if !ok {
panic(fmt.Errorf("out of memory reading %s", fieldName))
}
@@ -46,16 +45,16 @@ func mustReadUint64Le(ctx context.Context, mem api.Memory, fieldName string, off
// mustWrite is like api.Memory except that it panics if the offset
// is out of range.
func mustWrite(ctx context.Context, mem api.Memory, fieldName string, offset uint32, val []byte) {
if ok := mem.Write(ctx, offset, val); !ok {
func mustWrite(mem api.Memory, fieldName string, offset uint32, val []byte) {
if ok := mem.Write(offset, val); !ok {
panic(fmt.Errorf("out of memory writing %s", fieldName))
}
}
// mustWriteUint64Le is like api.Memory except that it panics if the offset
// is out of range.
func mustWriteUint64Le(ctx context.Context, mem api.Memory, fieldName string, offset uint32, val uint64) {
if ok := mem.WriteUint64Le(ctx, offset, val); !ok {
func mustWriteUint64Le(mem api.Memory, fieldName string, offset uint32, val uint64) {
if ok := mem.WriteUint64Le(offset, val); !ok {
panic(fmt.Errorf("out of memory writing %s", fieldName))
}
}