Skip to content

fix(bytecode): avoid out-of-bounds pointer in analyze_legacy - #3752

Merged
rakita merged 1 commit into
bluealloy:mainfrom
anders94:fix-analyze-legacy-oob-pointer
Jun 11, 2026
Merged

rakita merged 1 commit into
bluealloy:mainfrom
anders94:fix-analyze-legacy-oob-pointer

Conversation

@anders94

Copy link
Copy Markdown
Contributor

What

analyze_legacy advances its cursor with raw-pointer arithmetic:

iterator = unsafe { iterator.add(push_offset as usize + 2) };

When bytecode ends mid-PUSH (e.g. a trailing PUSH32), this computes a pointer up to 33 bytes past the end of the bytecode allocation. The pointer is never dereferenced — the while iterator < end guard stops any read — but pointer::add (https://doc.rust-lang.org/std/primitive.pointer.html#method.add) requires the result to stay within (or one past) the same allocation, so forming the out-of-bounds pointer is undefined behavior in its own right.

Fix

Use wrapping_add, which is always defined to compute and is bit-identical on real targets:

iterator = iterator.wrapping_add(push_offset as usize + 2);

The push_overflow = (iterator as usize) - (end as usize) calculation that relies on the past-end value is unchanged, and the < end guard still prevents any out-of-bounds read.

Impact

No behavior change on current targets and no known exploit: the value is only compared and address-subtracted, never read out of bounds, and analyze_legacy's output is unaffected. This is a soundness fix — it removes UB that a future compiler (or a non-standard/zkVM backend) would be free to miscompile.

How it was found

Surfaced by a Kani (CBMC) proof harness for analyze_legacy, which flags the pointer::add precondition violation (Miri catches it too).

Testing

The existing analyze_legacy unit tests pass unchanged — the fix is behavior-preserving (including test_bytecode_with_max_push32).

@rakita rakita left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@codspeed

codspeed Bot commented Jun 11, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 181 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing anders94:fix-analyze-legacy-oob-pointer (085e2ce) with main (c2c08ae)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩

@rakita
rakita merged commit 59a5aea into bluealloy:main Jun 11, 2026
31 of 34 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 11, 2026
RealiCZ added a commit to megaeth-labs/revm that referenced this pull request Sep 18, 2026
…lloy#3746 bluealloy#3802 bluealloy#3864 (#54)

Co-authored-by: Prajjwal Chittori <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: Anders Brownworth <[email protected]>
Co-authored-by: Ivan Wang <[email protected]>
Co-authored-by: cui <[email protected]>
@RealiCZ RealiCZ mentioned this pull request Sep 18, 2026
14 tasks done
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.

2 participants