Skip to content

Centralize exception ownership and fix exception unwinding to prevent leaks#551

Open
nbeerbower wants to merge 2 commits into
mainfrom
codex/audit-interpreter-for-memory-leaks
Open

Centralize exception ownership and fix exception unwinding to prevent leaks#551
nbeerbower wants to merge 2 commits into
mainfrom
codex/audit-interpreter-for-memory-leaks

Conversation

@nbeerbower

Copy link
Copy Markdown
Collaborator

Motivation

  • The interpreter had many ad-hoc places that set ctx->exception_state.* and value_retain/VALUE_RELEASE manually, causing inconsistent ownership and potential leaks when an active exception is superseded during unwinding (defer/finally) or when contexts are reused.
  • Ensure the runtime never leaks exception values and that overridden saved exceptions are released predictably during control-flow unwinding.

Description

  • Add centralized helpers exception_set_value(ExecutionContext*, Value) and exception_clear(ExecutionContext*) to manage exception-state ownership and release any replaced value. (Added to internal.h and implemented in runtime/context.c.)
  • Replace direct ad-hoc exception assignments across the interpreter (builtins, IO, FFI, environment, websockets, net, os, etc.) to call exception_set_value / exception_clear, and update runtime helpers (runtime_error*) to use the centralized APIs instead of manual retain patterns.
  • Fix unwinding behavior so defer and finally restore or release saved exceptions correctly (saved exceptions are released when a defer/finally overrides them) by saving/restoring or releasing saved_exception in defer_stack_execute and try/finally logic in runtime/statements.c.
  • Make throw transfer ownership through the new helper and protect against double-ownership during nested throws.
  • Update the persistent WASM context cleanup paths to use exception_clear so a WasmContext becomes reusable after a runtime error.
  • Add a regression test tests/memory/exception_override_cleanup.hml that verifies exception values are released when overridden by defer/finally, and wire it into tests/memory/regression/run_leak_tests.sh.
  • Numerous files updated to use the new APIs (representative files include src/backends/interpreter/runtime/context.c, src/backends/interpreter/runtime/statements.c, src/backends/interpreter/internal.h, src/backends/interpreter/ffi.c, src/backends/interpreter/environment.c, and many builtin/io/wasm sources). See diff for full list.

Testing

  • Built AddressSanitizer instrumented interpreter with make asan -j2 (ASAN build completed successfully). — succeeded.
  • Ran the memory-leak regression suite with ./tests/memory/regression/run_leak_tests.sh (quick and full runs): all regression tests passed, including new exception_override_cleanup.hml. — succeeded (no leaks detected).
  • Performed full rebuilds (make clean && make -j2) and non-ASAN builds to validate integration; initial ASAN linking hiccup observed during incremental build but resolved by clean rebuild. — final full build succeeded.
  • Ran the new exception override test directly under ASAN to reproduce and then validate the fix; test passed and showed no leaks. — succeeded.

Codex Task

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant