Propagates context to all api interface methods that aren't constant (#502)

This prepares for exposing operations like Memory.Grow while keeping the
ability to trace what did that, by adding a `context.Context` initial
parameter. This adds this to all API methods that mutate or return
mutated data.

Before, we made a change to trace functions and general lifecycle
commands, but we missed this part. Ex. We track functions, but can't
track what closed the module, changed memory or a mutable constant.
Changing to do this now is not only more consistent, but helps us
optimize at least the interpreter to help users identify otherwise
opaque code that can cause harm. This is critical before we add more
functions that can cause harm, such as Memory.Grow.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-04-25 08:13:18 +08:00
committed by GitHub
parent 98676fbc64
commit 45ff2fe12f
44 changed files with 1059 additions and 746 deletions

View File

@@ -34,7 +34,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
defer env.Close()
defer env.Close(ctx)
// Instantiate a WebAssembly module that imports the "log" function defined
// in "env" and exports "memory" and functions we'll use in this example.
@@ -42,7 +42,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
defer mod.Close()
defer mod.Close(ctx)
// Get references to WebAssembly functions we'll use in this example.
greet := mod.ExportedFunction("greet")
@@ -67,9 +67,9 @@ func main() {
defer deallocate.Call(ctx, namePtr, nameSize)
// The pointer is a linear memory offset, which is where we write the name.
if !mod.Memory().Write(uint32(namePtr), []byte(name)) {
if !mod.Memory().Write(ctx, uint32(namePtr), []byte(name)) {
log.Fatalf("Memory.Write(%d, %d) out of range of memory size %d",
namePtr, nameSize, mod.Memory().Size())
namePtr, nameSize, mod.Memory().Size(ctx))
}
// Now, we can call "greet", which reads the string we wrote to memory!
@@ -91,16 +91,16 @@ func main() {
defer deallocate.Call(ctx, uint64(greetingPtr), uint64(greetingSize))
// The pointer is a linear memory offset, which is where we write the name.
if bytes, ok := mod.Memory().Read(greetingPtr, greetingSize); !ok {
if bytes, ok := mod.Memory().Read(ctx, greetingPtr, greetingSize); !ok {
log.Fatalf("Memory.Read(%d, %d) out of range of memory size %d",
greetingPtr, greetingSize, mod.Memory().Size())
greetingPtr, greetingSize, mod.Memory().Size(ctx))
} else {
fmt.Println("go >>", string(bytes))
}
}
func logString(m api.Module, offset, byteCount uint32) {
buf, ok := m.Memory().Read(offset, byteCount)
func logString(ctx context.Context, m api.Module, offset, byteCount uint32) {
buf, ok := m.Memory().Read(ctx, offset, byteCount)
if !ok {
log.Fatalf("Memory.Read(%d, %d) out of range", offset, byteCount)
}