Skip to content

fix(wasm): register functions in FunctionSection and use TypeSection builder#170

Closed
graydeon wants to merge 1 commit intosec/wave-2-wasm-hardeningfrom
fix/wasm-function-section-gra93
Closed

fix(wasm): register functions in FunctionSection and use TypeSection builder#170
graydeon wants to merge 1 commit intosec/wave-2-wasm-hardeningfrom
fix/wasm-function-section-gra93

Conversation

@graydeon
Copy link
Copy Markdown
Contributor

Summary

  • Fixes the WASM Function section regression introduced by the C-2 lazy WASI imports refactor in PR sec/wave-2: C-1 WASM allocator OOB + C-2 lazy WASI imports; remove wasm-unstable gate #168
  • emit_malloc_builtin, emit_println_builtin, and compile_function were all adding bodies to the CodeSection without registering corresponding entries in the FunctionSection — a WASM binary where Code count ≠ Function count is invalid and rejected by wasmtime
  • The hardcoded two-entry Type section in finish() is replaced with wasm_encoder::TypeSection, which is populated incrementally as each function is compiled, so all signatures (malloc, fd_write, println, user functions) are always present and consistent
  • Adds a RestoreDir Drop guard in agent_security_tests so the working directory is restored even if a test panics

Test plan

  • All 10 existing wasm_tests pass
  • test_e2e_wasm_validation passes (checks Type + Function + Code sections are present)
  • Full compiler test suite passes with no regressions

Fixes GRA-93

🤖 Generated with Claude Code

…builder

The C-2 lazy-WASI refactor broke WASM validation by adding function bodies
to the CodeSection without corresponding entries in the FunctionSection.
The Type section was also hardcoded as two entries, ignoring the actual
function types compiled by the module.

- emit_malloc_builtin: register malloc type in TypeSection and call
  self.functions.function(type_idx) so the FunctionSection entry exists
- emit_println_builtin: register fd_write type in TypeSection (used by
  the import), then register println type and its FunctionSection entry
- compile_function: derive param/return ValTypes from ir::Type and register
  each user function in both TypeSection and FunctionSection
- finish(): replace hardcoded manual type-section bytes with
  self.type_section.append_to() (wasm_encoder::TypeSection)
- Remove unused encode_leb128 helper
- runtime_security_regressions.rs: wrap CWD restoration in a Drop guard
  so that it is restored even when a test panics

Fixes GRA-93.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@graydeon
Copy link
Copy Markdown
Contributor Author

Closing — PR #168 (sec/wave-2-wasm-hardening) already contains the equivalent FunctionSection fix in commit 6d7051b. All fixes belong on that branch.

@graydeon graydeon closed this Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant