Skip to content

fix: handle host parameter in call_indirect dispatch (issue #19)#21

Merged
arnoox merged 3 commits intomainfrom
fix/issue-19-call-indirect-host-param
Mar 15, 2026
Merged

fix: handle host parameter in call_indirect dispatch (issue #19)#21
arnoox merged 3 commits intomainfrom
fix/issue-19-call-indirect-host-param

Conversation

@arnoox
Copy link
Owner

@arnoox arnoox commented Mar 15, 2026

Summary

Fixes issue #19: indirect calls (call_indirect) were missing the host parameter for WASI/import functions.

The bug involved three related issues:

  1. Dispatch args mismatch: All dispatch arms used the same args string, but individual functions might differ in whether they need the host parameter
  2. Missing host in dispatcher: Functions using call_indirect to dispatch were not marked as needing a host parameter, even when dispatching to import-calling functions
  3. Signature codegen: The function signature generation didn't account for call_indirect when determining if a function needs the host parameter

Fix Details

assembly.rs (Bug 2)

  • Update function_calls_imports to also detect CallIndirect instructions when the module has function imports
  • This ensures dispatcher functions are marked as needs_host = true

instruction.rs (Bugs 1 & 3)

  • Add caller_has_host: bool parameter to generate_instruction_with_info
  • Replace the shared dispatch args string with per-arm generation
  • Each dispatch arm only passes host when both the target function needs it AND the caller has it in scope

function.rs

  • Add has_call_indirect helper to detect call_indirect instructions
  • Update signature generation to check for call_indirect with imports
  • Thread caller_has_host through instruction generation

Tests

Added regression tests:

  • crates/herkos-tests/data/wat/indirect_call_import.wat — WAT module with a dispatcher using call_indirect to reach an import-calling function
  • crates/herkos-tests/tests/indirect_call_import.rs — integration test verifying the dispatch works correctly

Verification

cargo test -p herkos-tests -- indirect_call_import
cargo test
cargo clippy --all-targets
cargo fmt --check

All tests pass ✅

🤖 Generated with Claude Code

Arnaud Riess and others added 3 commits March 15, 2026 08:04
Fix three related bugs causing call_indirect codegen to fail when dispatching
to functions that call imports or WASI functions:

**Bug 1 & 3 — Wrong dispatch args generation**
- generate_call_indirect used a shared args_str for all dispatch arms
- This caused functions with different needs_host to receive wrong arguments
- FIX: Generate per-arm dispatch arguments, only prepending 'host' when both
  the target function needs_host AND the caller has host in scope

**Bug 2 — Missing host parameter for dispatcher functions**
- function_calls_imports only checked for direct CallImport instructions
- Functions using call_indirect to dispatch to import-calling functions
  were not marked as needing the host parameter
- FIX: Also mark functions as needing_host if they use CallIndirect and
  the module has any function imports

**Changes:**
- assembly.rs: Update function_calls_imports to detect CallIndirect
  when module has function imports
- instruction.rs: Add caller_has_host parameter to
  generate_instruction_with_info; implement per-arm dispatch arg generation
- function.rs: Thread caller_has_host through instruction generation;
  also check for call_indirect in signature needs_host computation

**Tests:**
- Add indirect_call_import.wat: regression test with a dispatcher function
  using call_indirect to reach an import-calling function
- Add indirect_call_import.rs: integration test verifying the dispatch works

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
The cdylib crate-type was causing linker errors during the build script
execution. Since herkos-core is a build-time tool (not a runtime library
that needs C interop), only rlib is needed.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
@arnoox arnoox merged commit 220a7b4 into main Mar 15, 2026
1 check passed
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