Merged
Conversation
peter-jerry-ye
approved these changes
Mar 2, 2026
alexcrichton
approved these changes
Mar 2, 2026
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.
The primary goal of this PR is to fix a use-after-free bug I've discovered that happens in some nested cases. Reproducer test case included.
In details:
The
Instruction::Returncodegen emitted cleanup code (callingmbt_ffi_free) before using return operands. When return operands contained inline expressions that load fromreturn_areaor other freed memory (e.g.mbt_ffi_load32(return_area + 0)), the generated code would free the memory first and then read from it.The fix binds all return operands to local variables before running cleanup, so the values are captured before any memory is freed.
Similarly,
finish_blockwas callingmbt_ffi_freeimmediately on cleanup addresses instead of deferring them to the cleanup list, causing the same class of issue. Fixed to usecleanup_list.push(...)consistently.A runtime test (
tests/runtime/list-in-variant/) is included that exercises this pattern.These problems were causing crashes for me while working on a MoonBit SDK for Golem.
Once the tests fixed, there were a couple of things in the existing moonbit test runner to fix (due to compiler updates - using the latest 0.8.2: