wazero icon indicating copy to clipboard operation
wazero copied to clipboard

Compiled modules may share state

Open emcfarlane opened this issue 11 months ago • 0 comments

Describe the bug Closing a CompiledModule will affect another CompiledModule if they are compiled to the same runtime using the same wasm bytes.

To Reproduce

package main

import (
	"context"
	"log"

	"github.com/tetratelabs/wazero"
)

func main() {
	ctx := context.Background()
	rt := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig())

	wasm := []byte{0, 'a', 's', 'm', 1, 0, 0, 0}
	compiled1, _ := rt.CompileModule(ctx, wasm) // Original.
	compiled2, _ := rt.CompileModule(ctx, wasm) // Duplicate.

	// Instantiating the module then closing the compiled module should not affect the other compiled module.
	if _, err := rt.InstantiateModule(ctx, compiled1, wazero.NewModuleConfig().WithName("foo")); err != nil {
		log.Fatal(err)
	}
	_ = compiled1.Close(ctx)

	// This compiled2 module should still be usable.
	if _, err := rt.InstantiateModule(ctx, compiled2, wazero.NewModuleConfig().WithName("bar")); err != nil {
		log.Fatal(err)
	}
	_ = compiled2.Close(ctx)
}

This causes the following error on the second log line:

source module must be compiled before instantiation

https://github.com/tetratelabs/wazero/compare/main...emcfarlane:wazero:ed/dupCompCache

Expected behavior The CompiledModule should still be able to be instantiated as the caller isn't aware that the module is shared between multiple instances.

Screenshots N/A

Environment (please complete the relevant information):

  • Go version: go version go1.23.2 darwin/arm64
  • wazero Version: dc08732e57d575a240774bc64e87cbed59f81c34
  • Host architecture: arm64
  • Runtime mode: compiler

Additional context The current behaviour makes it difficult to bound the number of active modules. The caller needs to manage access to the CompiledModule and must infer the cache key (ModuleID could be provided by CompiledModule interface?). To Close a compiled module shared between multiple viewers we must wait for all viewers to go away and then syncronize the call of Close to happen before anymore calls to CompileModule to avoid a race.

Related issues https://github.com/tetratelabs/wazero/issues/1913

emcfarlane avatar Nov 06 '24 23:11 emcfarlane