From d285a8177712e79fe5ba8c68b9ecc410d2179342 Mon Sep 17 00:00:00 2001 From: Val Packett Date: Tue, 24 Oct 2023 04:57:57 -0300 Subject: [PATCH] compiler: add munmap finalizer on cache hits to avoid memory leak Signed-off-by: Val Packett --- internal/engine/compiler/engine.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/engine/compiler/engine.go b/internal/engine/compiler/engine.go index d4e8dcd06fe..3a69933d4b5 100644 --- a/internal/engine/compiler/engine.go +++ b/internal/engine/compiler/engine.go @@ -523,7 +523,9 @@ func (e *engine) Close() (err error) { // CompileModule implements the same method as documented on wasm.Engine. func (e *engine) CompileModule(_ context.Context, module *wasm.Module, listeners []experimental.FunctionListener, ensureTermination bool) error { - if _, ok, err := e.getCompiledModule(module, listeners); ok { // cache hit! + if cm, ok, err := e.getCompiledModule(module, listeners); ok { // cache hit! + // As this uses mmap, we need to munmap on the compiled machine code when it's GCed. + e.setFinalizer(cm, releaseCompiledModule) return nil } else if err != nil { return err