Skip to content

loadCore leaks export_fns and cached_fns when the start function traps #42

@jtakakura

Description

@jtakakura

Description

In WasmModule.loadCore (src/types.zig), export_fns and cached_fns are allocated before the Wasm start function is executed.
If the start function traps (or otherwise returns an error), the errdefer chain cleans up vm, instance, wasi_ctx, module, store, and self, but never frees export_fns or cached_fns.

This is a narrow, pre-existing resource leak discovered during the review of #40. It is unrelated to the OOM-on-VM fix in that PR.

Steps to reproduce

  1. Prepare a Wasm module whose start function always traps (e.g., unreachable) and has at least one exported function so that export_fns / cached_fns are non-empty.

Example WAT:

(module
  (func $start unreachable)
  (func (export "dummy") (result i32) i32.const 0)
  (start $start)
)
  1. Load it under a leak-checking allocator:
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
defer _ = gpa.deinit(); // reports leak
_ = WasmModule.loadFromWat(gpa.allocator(), wat_source) catch {};
  1. Observe that gpa.deinit() reports memory leaks for export_fns (including the inner param_types / result_types slices) and cached_fns.

zwasm version

1.8.0

Platform

Linux x86_64

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions