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

spectest: ensure compiled functions can outlive module #2105

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions internal/engine/compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,9 @@ func (s nativeCallStatusCode) String() (ret string) {
return
}

// releaseCompiledModule is a runtime.SetFinalizer function that munmaps the compiledModule.executable.
func releaseCompiledModule(cm *compiledModule) {
if err := cm.executable.Unmap(); err != nil {
// releaseCompiledCode is a runtime.SetFinalizer function that munmaps the compiledCode.executable.
func releaseCompiledCode(code *compiledCode) {
if err := code.executable.Unmap(); err != nil {
// munmap failure cannot recover, and happen asynchronously on the
// finalizer thread. While finalizer functions can return errors,
// they are ignored.
Expand Down Expand Up @@ -555,7 +555,7 @@ func (e *engine) CompileModule(_ context.Context, module *wasm.Module, listeners
}

// As this uses mmap, we need to munmap on the compiled machine code when it's GCed.
e.setFinalizer(cm, releaseCompiledModule)
e.setFinalizer(cm.compiledCode, releaseCompiledCode)
ln := len(listeners)
cmp := newCompiler()
asmNodes := new(asmNodes)
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/compiler/engine_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (e *engine) getCompiledModule(module *wasm.Module, listeners []experimental
}

// As this uses mmap, we need to munmap on the compiled machine code when it's GCed.
e.setFinalizer(cm, releaseCompiledModule)
e.setFinalizer(cm.compiledCode, releaseCompiledCode)
}
return
}
Expand Down
12 changes: 5 additions & 7 deletions internal/engine/compiler/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ func requireSupportedOSArch(t *testing.T) {
}
}

type fakeFinalizer map[*compiledModule]func(module *compiledModule)
type fakeFinalizer map[*compiledCode]func(code *compiledCode)

func (f fakeFinalizer) setFinalizer(obj interface{}, finalizer interface{}) {
cf := obj.(*compiledModule)
cf := obj.(*compiledCode)
if _, ok := f[cf]; ok { // easier than adding a field for testing.T
panic(fmt.Sprintf("BUG: %v already had its finalizer set", cf))
}
f[cf] = finalizer.(func(*compiledModule))
f[cf] = finalizer.(func(*compiledCode))
}

func TestCompiler_CompileModule(t *testing.T) {
Expand Down Expand Up @@ -95,10 +95,8 @@ func TestCompiler_CompileModule(t *testing.T) {

func TestCompiler_Releasecode_Panic(t *testing.T) {
captured := require.CapturePanic(func() {
releaseCompiledModule(&compiledModule{
compiledCode: &compiledCode{
executable: makeCodeSegment(1, 2),
},
releaseCompiledCode(&compiledCode{
executable: makeCodeSegment(1, 2),
})
})
require.Contains(t, captured.Error(), "compiler: failed to munmap code segment")
Expand Down
Loading