fix: handle host parameter in call_indirect dispatch (issue #19)#21
Merged
fix: handle host parameter in call_indirect dispatch (issue #19)#21
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes issue #19: indirect calls (
call_indirect) were missing the host parameter for WASI/import functions.The bug involved three related issues:
call_indirectto dispatch were not marked as needing a host parameter, even when dispatching to import-calling functionscall_indirectwhen determining if a function needs the host parameterFix Details
assembly.rs (Bug 2)
function_calls_importsto also detectCallIndirectinstructions when the module has function importsneeds_host = trueinstruction.rs (Bugs 1 & 3)
caller_has_host: boolparameter togenerate_instruction_with_infohostwhen both the target function needs it AND the caller has it in scopefunction.rs
has_call_indirecthelper to detectcall_indirectinstructionscall_indirectwith importscaller_has_hostthrough instruction generationTests
Added regression tests:
crates/herkos-tests/data/wat/indirect_call_import.wat— WAT module with a dispatcher usingcall_indirectto reach an import-calling functioncrates/herkos-tests/tests/indirect_call_import.rs— integration test verifying the dispatch works correctlyVerification
All tests pass ✅
🤖 Generated with Claude Code