chore(interpreter): add debug assertions to unsafe ExtBytecode methods#3545
Open
0xMars42 wants to merge 1 commit intobluealloy:mainfrom
Open
chore(interpreter): add debug assertions to unsafe ExtBytecode methods#35450xMars42 wants to merge 1 commit intobluealloy:mainfrom
0xMars42 wants to merge 1 commit intobluealloy:mainfrom
Conversation
Closes bluealloy#3487 Add debug_assert! bounds checks to all unsafe pointer operations in Jumps and Immediates implementations for ExtBytecode. Zero cost in release builds, catches out-of-bounds arithmetic during development. Introduces bytecode_bounds() and pc_unchecked() helpers to keep assertions safe and avoid recursive panics in error messages.
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.
Closes #3487
Add
debug_assert!bounds checks to all unsafe operations inJumpsandImmediatesimplementations forExtBytecode. This catches out-of-bounds pointer arithmetic during development/testing without any runtime cost in release builds.The safe public API currently allows constructing UB via
relative_jump,absolute_jump, and theImmediatesmethods, confirmed by Miri. These assertions follow the same pattern established in #2832 forset_action.Two private helpers keep the assertions clean:
bytecode_bounds()returns(base_ptr, end_ptr)usingwrapping_add(no unsafe in assertions)pc_unchecked()computes the program counter via integer arithmetic to avoid recursive panics when an assertion fails inside a method that would callpc()Known limitations
debug_assert!only, the unsoundness technically remains reachable in release builds via the safe API. A full fix would require making the trait methodsunsafe(breaking change) or adding runtime checks.is_valid_legacy_jumpassertsjt.is_some()but theunwrap_uncheckedstill exists for release.