Skip to content

chore(interpreter): add debug assertions to unsafe ExtBytecode methods#3545

Open
0xMars42 wants to merge 1 commit intobluealloy:mainfrom
0xMars42:fix-ext-bytecode-unsoundness-debug-asserts
Open

chore(interpreter): add debug assertions to unsafe ExtBytecode methods#3545
0xMars42 wants to merge 1 commit intobluealloy:mainfrom
0xMars42:fix-ext-bytecode-unsoundness-debug-asserts

Conversation

@0xMars42
Copy link
Copy Markdown

@0xMars42 0xMars42 commented Apr 5, 2026

Closes #3487

Add debug_assert! bounds checks to all unsafe operations in Jumps and Immediates implementations for ExtBytecode. 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 the Immediates methods, confirmed by Miri. These assertions follow the same pattern established in #2832 for set_action.

Two private helpers keep the assertions clean:

  • bytecode_bounds() returns (base_ptr, end_ptr) using wrapping_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 call pc()

Known limitations

  • These are debug_assert! only, the unsoundness technically remains reachable in release builds via the safe API. A full fix would require making the trait methods unsafe (breaking change) or adding runtime checks.
  • is_valid_legacy_jump asserts jt.is_some() but the unwrap_unchecked still exists for release.

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.
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.

Unsoundness in ExtBytecode::relative_jump: Safe API allows out-of-bounds pointer offset (UB)

1 participant