Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] [Experimental] Add support for tracking dirty memory pages #968

Closed
wants to merge 17 commits into from
2 changes: 1 addition & 1 deletion builder.go
Expand Up @@ -326,7 +326,7 @@ func (b *hostModuleBuilder) Compile(ctx context.Context) (CompiledModule, error)
return nil, err
}

if err = b.r.store.Engine.CompileModule(ctx, module, listeners); err != nil {
if err = b.r.store.Engine.CompileModule(ctx, module, wasm.CompileModuleOptions{}, listeners); err != nil {
return nil, err
}

Expand Down
9 changes: 5 additions & 4 deletions cmd/wazero/wazero.go
Expand Up @@ -17,6 +17,7 @@ import (
gojs "github.com/tetratelabs/wazero/imports/go"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/internal/version"
"github.com/tetratelabs/wazero/internal/wasm"
"github.com/tetratelabs/wazero/sys"
)

Expand Down Expand Up @@ -83,7 +84,7 @@ func doCompile(args []string, stdErr io.Writer, exit func(code int)) {
}
wasmPath := flags.Arg(0)

wasm, err := os.ReadFile(wasmPath)
wasmBytes, err := os.ReadFile(wasmPath)
if err != nil {
fmt.Fprintf(stdErr, "error reading wasm binary: %v\n", err)
exit(1)
Expand All @@ -94,7 +95,7 @@ func doCompile(args []string, stdErr io.Writer, exit func(code int)) {
rt := wazero.NewRuntime(ctx)
defer rt.Close(ctx)

if _, err = rt.CompileModule(ctx, wasm); err != nil {
if _, err = rt.CompileModule(ctx, wasmBytes, wasm.CompileModuleOptions{}); err != nil {
fmt.Fprintf(stdErr, "error compiling wasm binary: %v\n", err)
exit(1)
} else {
Expand Down Expand Up @@ -187,7 +188,7 @@ func doRun(args []string, stdOut io.Writer, stdErr io.Writer, exit func(code int
mountFS = cfs
}

wasm, err := os.ReadFile(wasmPath)
wasmBytes, err := os.ReadFile(wasmPath)
if err != nil {
fmt.Fprintf(stdErr, "error reading wasm binary: %v\n", err)
exit(1)
Expand Down Expand Up @@ -218,7 +219,7 @@ func doRun(args []string, stdOut io.Writer, stdErr io.Writer, exit func(code int
conf = conf.WithFS(mountFS)
}

code, err := rt.CompileModule(ctx, wasm)
code, err := rt.CompileModule(ctx, wasmBytes, wasm.CompileModuleOptions{})
if err != nil {
fmt.Fprintf(stdErr, "error compiling wasm binary: %v\n", err)
exit(1)
Expand Down
2 changes: 1 addition & 1 deletion config_test.go
Expand Up @@ -603,7 +603,7 @@ func Test_compiledModule_Close(t *testing.T) {
var cs []*compiledModule
for i := 0; i < 10; i++ {
m := &wasm.Module{}
err := e.CompileModule(ctx, m, nil)
err := e.CompileModule(ctx, m, wasm.CompileModuleOptions{}, nil)
require.NoError(t, err)
cs = append(cs, &compiledModule{module: m, compiledEngine: e})
}
Expand Down
3 changes: 2 additions & 1 deletion dwarf_test.go
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/tetratelabs/wazero/internal/platform"
"github.com/tetratelabs/wazero/internal/testing/dwarftestdata"
"github.com/tetratelabs/wazero/internal/testing/require"
"github.com/tetratelabs/wazero/internal/wasm"
)

func TestWithDebugInfo(t *testing.T) {
Expand Down Expand Up @@ -163,7 +164,7 @@ wasm stack trace:
if len(lang.bin) == 0 {
t.Skip()
}
compiled, err := r.CompileModule(ctx, lang.bin)
compiled, err := r.CompileModule(ctx, lang.bin, wasm.CompileModuleOptions{})
require.NoError(t, err)

_, err = r.InstantiateModule(ctx, compiled, wazero.NewModuleConfig())
Expand Down
3 changes: 2 additions & 1 deletion example_test.go
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/internal/wasm"
)

// addWasm was generated by the following:
Expand Down Expand Up @@ -39,7 +40,7 @@ func Example() {

// Instantiate the guest Wasm into the same runtime. It exports the `add`
// function, implemented in WebAssembly.
mod, err := r.InstantiateModuleFromBinary(ctx, addWasm)
mod, err := r.InstantiateModuleFromBinary(ctx, addWasm, wasm.CompileModuleOptions{})
if err != nil {
log.Panicln(err)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/allocation/rust/greet.go
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/wasm"
)

// greetWasm was compiled using `cargo build --release --target wasm32-unknown-unknown`
Expand Down Expand Up @@ -39,7 +40,7 @@ func main() {

// Instantiate a WebAssembly module that imports the "log" function defined
// in "env" and exports "memory" and functions we'll use in this example.
mod, err := r.InstantiateModuleFromBinary(ctx, greetWasm)
mod, err := r.InstantiateModuleFromBinary(ctx, greetWasm, wasm.CompileModuleOptions{})
if err != nil {
log.Panicln(err)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/allocation/tinygo/greet.go
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/internal/wasm"
)

// greetWasm was compiled using `tinygo build -o greet.wasm -scheduler=none --no-debug -target=wasi greet.go`
Expand Down Expand Up @@ -44,7 +45,7 @@ func main() {

// Instantiate a WebAssembly module that imports the "log" function defined
// in "env" and exports "memory" and functions we'll use in this example.
mod, err := r.InstantiateModuleFromBinary(ctx, greetWasm)
mod, err := r.InstantiateModuleFromBinary(ctx, greetWasm, wasm.CompileModuleOptions{})
if err != nil {
log.Panicln(err)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/allocation/zig/greet.go
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/wasm"
)

// greetWasm was compiled using `zig build`
Expand Down Expand Up @@ -45,7 +46,7 @@ func run() error {

// Instantiate a WebAssembly module that imports the "log" function defined
// in "env" and exports "memory" and functions we'll use in this example.
compiled, err := r.CompileModule(ctx, greetWasm)
compiled, err := r.CompileModule(ctx, greetWasm, wasm.CompileModuleOptions{})
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion examples/basic/add.go
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/internal/wasm"
)

// addWasm was generated by the following:
Expand Down Expand Up @@ -38,7 +39,7 @@ func main() {

// Instantiate the guest Wasm into the same runtime. It exports the `add`
// function, implemented in WebAssembly.
mod, err := r.InstantiateModuleFromBinary(ctx, addWasm)
mod, err := r.InstantiateModuleFromBinary(ctx, addWasm, wasm.CompileModuleOptions{})
if err != nil {
log.Panicln(err)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/import-go/age-calculator.go
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/internal/wasm"
)

// ageCalculatorWasm was generated by the following:
Expand Down Expand Up @@ -62,7 +63,7 @@ func main() {
//
// Note: The import syntax in both Text and Binary format is the same
// regardless of if the function was defined in Go or WebAssembly.
ageCalculator, err := r.InstantiateModuleFromBinary(ctx, ageCalculatorWasm)
ageCalculator, err := r.InstantiateModuleFromBinary(ctx, ageCalculatorWasm, wasm.CompileModuleOptions{})
if err != nil {
log.Panicln(err)
}
Expand Down
7 changes: 4 additions & 3 deletions examples/multiple-results/multiple-results.go
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/wasm"
)

// main implements functions with multiple returns values, using both an
Expand Down Expand Up @@ -82,7 +83,7 @@ var resultOffsetWasm []byte
// resultOffsetWasmFunctions are the WebAssembly equivalent of the Go-defined
// resultOffsetHostFunctions. The source is in testdata/result_offset.wat
func resultOffsetWasmFunctions(ctx context.Context, r wazero.Runtime) (api.Module, error) {
return r.InstantiateModuleFromBinary(ctx, resultOffsetWasm)
return r.InstantiateModuleFromBinary(ctx, resultOffsetWasm, wasm.CompileModuleOptions{})
}

// multiValueWasm was generated by the following:
Expand All @@ -95,7 +96,7 @@ var multiValueWasm []byte
// multiValueWasmFunctions are the WebAssembly equivalent of the Go-defined
// multiValueHostFunctions. The source is in testdata/multi_value.wat
func multiValueWasmFunctions(ctx context.Context, r wazero.Runtime) (api.Module, error) {
return r.InstantiateModuleFromBinary(ctx, multiValueWasm)
return r.InstantiateModuleFromBinary(ctx, multiValueWasm, wasm.CompileModuleOptions{})
}

// multiValueWasm was generated by the following:
Expand All @@ -122,5 +123,5 @@ func multiValueFromImportedHostWasmFunctions(ctx context.Context, r wazero.Runti
return nil, err
}
// Then, creates the module which imports the `get_age` function from the `multi-value/host` module above.
return r.InstantiateModuleFromBinary(ctx, multiValueFromImportedHostWasm)
return r.InstantiateModuleFromBinary(ctx, multiValueFromImportedHostWasm, wasm.CompileModuleOptions{})
}
3 changes: 2 additions & 1 deletion examples/namespace/counter.go
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/wasm"
)

// counterWasm was generated by the following:
Expand All @@ -29,7 +30,7 @@ func main() {
defer r.Close(ctx) // This closes everything this Runtime created.

// Compile WebAssembly that requires its own "env" module.
compiled, err := r.CompileModule(ctx, counterWasm)
compiled, err := r.CompileModule(ctx, counterWasm, wasm.CompileModuleOptions{})
if err != nil {
log.Panicln(err)
}
Expand Down
3 changes: 2 additions & 1 deletion experimental/compilation_cache_example_test.go
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/internal/wasm"
)

// This is a basic example of using the file system compilation cache via WithCompilationCacheDirName.
Expand Down Expand Up @@ -49,7 +50,7 @@ func newRuntimeCompileClose(ctx context.Context) {
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created except the file system cache.

_, err := r.CompileModule(ctx, fsWasm)
_, err := r.CompileModule(ctx, fsWasm, wasm.CompileModuleOptions{})
if err != nil {
log.Panicln(err)
}
Expand Down
3 changes: 2 additions & 1 deletion experimental/listener_example_test.go
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/tetratelabs/wazero/api"
. "github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/internal/wasm"
)

// listenerWasm was generated by the following:
Expand Down Expand Up @@ -64,7 +65,7 @@ func Example_customListenerFactory() {
wasi_snapshot_preview1.MustInstantiate(ctx, r)

// Compile the WebAssembly module using the default configuration.
code, err := r.CompileModule(ctx, listenerWasm)
code, err := r.CompileModule(ctx, listenerWasm, wasm.CompileModuleOptions{})
if err != nil {
log.Panicln(err)
}
Expand Down
2 changes: 1 addition & 1 deletion experimental/listener_test.go
Expand Up @@ -76,7 +76,7 @@ func TestFunctionListenerFactory(t *testing.T) {
// Ensure the imported function was converted to a listener.
require.Equal(t, map[string]struct{}{"": {}}, factory.m)

compiled, err := r.CompileModule(ctx, bin)
compiled, err := r.CompileModule(ctx, bin, wasm.CompileModuleOptions{})
require.NoError(t, err)

// Ensure each function was converted to a listener eagerly
Expand Down
5 changes: 3 additions & 2 deletions experimental/logging/log_listener_example_test.go
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/experimental/logging"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/internal/wasm"
)

// listenerWasm was generated by the following:
Expand All @@ -31,7 +32,7 @@ func Example_newHostLoggingListenerFactory() {
wasi_snapshot_preview1.MustInstantiate(ctx, r)

// Compile the WebAssembly module using the default configuration.
code, err := r.CompileModule(ctx, listenerWasm)
code, err := r.CompileModule(ctx, listenerWasm, wasm.CompileModuleOptions{})
if err != nil {
log.Panicln(err)
}
Expand Down Expand Up @@ -69,7 +70,7 @@ func Example_newLoggingListenerFactory() {
wasi_snapshot_preview1.MustInstantiate(ctx, r)

// Compile the WebAssembly module using the default configuration.
code, err := r.CompileModule(ctx, listenerWasm)
code, err := r.CompileModule(ctx, listenerWasm, wasm.CompileModuleOptions{})
if err != nil {
log.Panicln(err)
}
Expand Down
2 changes: 1 addition & 1 deletion imports/assemblyscript/assemblyscript_test.go
Expand Up @@ -420,7 +420,7 @@ func requireProxyModule(t *testing.T, fns FunctionExporter, config wazero.Module

proxyBin := proxy.NewModuleBinary("env", envCompiled)

proxyCompiled, err := r.CompileModule(ctx, proxyBin)
proxyCompiled, err := r.CompileModule(ctx, proxyBin, wasm.CompileModuleOptions{})
require.NoError(t, err)

mod, err := r.InstantiateModule(ctx, proxyCompiled, config)
Expand Down
3 changes: 2 additions & 1 deletion imports/assemblyscript/example/assemblyscript.go
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/assemblyscript"
"github.com/tetratelabs/wazero/internal/wasm"
)

// asWasm compiled using `npm install && npm run build`
Expand Down Expand Up @@ -37,7 +38,7 @@ func main() {
}

// Compile the WebAssembly module using the default configuration.
code, err := r.CompileModule(ctx, asWasm)
code, err := r.CompileModule(ctx, asWasm, wasm.CompileModuleOptions{})
if err != nil {
log.Panicln(err)
}
Expand Down
5 changes: 3 additions & 2 deletions imports/emscripten/emscripten_test.go
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/tetratelabs/wazero/experimental/logging"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/internal/testing/require"
"github.com/tetratelabs/wazero/internal/wasm"
"github.com/tetratelabs/wazero/sys"
)

Expand Down Expand Up @@ -45,7 +46,7 @@ func TestGrow(t *testing.T) {
require.NoError(t, err)

// Emscripten exits main with zero by default
_, err = r.InstantiateModuleFromBinary(ctx, growWasm)
_, err = r.InstantiateModuleFromBinary(ctx, growWasm, wasm.CompileModuleOptions{})
require.Error(t, err)
require.Zero(t, err.(*sys.ExitError).ExitCode())

Expand All @@ -65,7 +66,7 @@ func TestInvoke(t *testing.T) {
_, err := Instantiate(ctx, r)
require.NoError(t, err)

mod, err := r.InstantiateModuleFromBinary(ctx, invokeWasm)
mod, err := r.InstantiateModuleFromBinary(ctx, invokeWasm, wasm.CompileModuleOptions{})
require.NoError(t, err)

tests := []struct {
Expand Down
3 changes: 2 additions & 1 deletion imports/go/example/stars.go
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/experimental"
gojs "github.com/tetratelabs/wazero/imports/go"
"github.com/tetratelabs/wazero/internal/wasm"
"github.com/tetratelabs/wazero/sys"
)

Expand Down Expand Up @@ -53,7 +54,7 @@ func main() {

// Compile the WebAssembly module using the default configuration.
start = time.Now()
compiled, err := r.CompileModule(ctx, bin)
compiled, err := r.CompileModule(ctx, bin, wasm.CompileModuleOptions{})
if err != nil {
log.Panicln(err)
}
Expand Down
3 changes: 2 additions & 1 deletion imports/go/example/stars_test.go
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/tetratelabs/wazero/internal/platform"
"github.com/tetratelabs/wazero/internal/testing/maintester"
"github.com/tetratelabs/wazero/internal/testing/require"
"github.com/tetratelabs/wazero/internal/wasm"
"github.com/tetratelabs/wazero/sys"
)

Expand Down Expand Up @@ -71,7 +72,7 @@ func Benchmark_main(b *testing.B) {
if err != nil {
b.Fatal(err)
}
compiled, err := r.CompileModule(ctx, bin)
compiled, err := r.CompileModule(ctx, bin, wasm.CompileModuleOptions{})
if err != nil {
b.Fatal(err)
}
Expand Down