Skip to content

fix: call jet_mem_expand before jet.mem.load in MLOAD#74

Merged
tcrypt25519 merged 3 commits into
mainfrom
claude/fix-memory-expansion-pointer-cI5lF
Feb 14, 2026
Merged

fix: call jet_mem_expand before jet.mem.load in MLOAD#74
tcrypt25519 merged 3 commits into
mainfrom
claude/fix-memory-expansion-pointer-cI5lF

Conversation

@tcrypt25519

Copy link
Copy Markdown
Owner

MLOAD was not calling jet_mem_expand before reading from memory, unlike
MSTORE and MSTORE8 which both expand first. If MLOAD was executed with
an offset at or near the current memory capacity, jet.mem.load would
compute memory_ptr + offset and jet.stack.push.ptr would then read 32
bytes (i256) from that address — potentially outside the allocated
region — causing an intermittent segfault.

The fix mirrors the existing pattern: call jet_mem_expand(ctx, offset, 32)
first so the buffer is guaranteed to cover [offset, offset+32). Since
jet_mem_expand may reallocate and update ctx.memory_ptr, jet.mem.load
then re-reads the fresh pointer from the context struct, so no dangling
pointer is ever used.

https://claude.ai/code/session_01XcNd32HQupXsdnVd4KmX6f

MLOAD was not calling jet_mem_expand before reading from memory, unlike
MSTORE and MSTORE8 which both expand first. If MLOAD was executed with
an offset at or near the current memory capacity, jet.mem.load would
compute memory_ptr + offset and jet.stack.push.ptr would then read 32
bytes (i256) from that address — potentially outside the allocated
region — causing an intermittent segfault.

The fix mirrors the existing pattern: call jet_mem_expand(ctx, offset, 32)
first so the buffer is guaranteed to cover [offset, offset+32). Since
jet_mem_expand may reallocate and update ctx.memory_ptr, jet.mem.load
then re-reads the fresh pointer from the context struct, so no dangling
pointer is ever used.

https://claude.ai/code/session_01XcNd32HQupXsdnVd4KmX6f
Copilot AI review requested due to automatic review settings February 14, 2026 09:53
@semanticdiff-com

semanticdiff-com Bot commented Feb 14, 2026

Copy link
Copy Markdown

Review changes with  SemanticDiff

Changed Files
File Status
  crates/jet/tests/roms/mod.rs  44% smaller
  crates/jet/tests/test_roms.rs  11% smaller
  crates/jet/src/builder/ops.rs  10% smaller

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @tcrypt25519, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical memory safety vulnerability in the MLOAD operation. Previously, MLOAD could attempt to read from memory without first ensuring that the required memory region was allocated, leading to potential segmentation faults. The fix introduces a necessary memory expansion step, aligning MLOAD's behavior with other memory operations like MSTORE and MSTORE8, thereby significantly improving the stability and reliability of memory access within the system.

Highlights

  • MLOAD Memory Expansion Fix: Implemented a call to jet_mem_expand before jet.mem.load within the MLOAD operation. This ensures that memory is sufficiently expanded to cover the read range, preventing potential out-of-bounds access and intermittent segfaults.
  • Memory Safety Alignment: The MLOAD behavior now mirrors MSTORE and MSTORE8, which already perform memory expansion before writing. This standardizes memory handling across related operations.
  • Context Pointer Update: Ensured that the jet.mem.load call re-reads the memory_ptr from the execution context after jet_mem_expand is called, accounting for potential memory reallocation and preventing the use of a stale pointer.
Changelog
  • crates/jet/src/builder/ops.rs
    • Added a call to jet_mem_expand before jet.mem.load in the mload function.
    • Included logic to re-read the memory_ptr from the context after jet_mem_expand to ensure it's up-to-date.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request aims to address a potential segfault in the MLOAD operation by ensuring memory expansion before loading, aligning with MSTORE and MSTORE8 patterns and the existing JIT architecture. However, a security audit revealed critical and high-severity vulnerabilities. Key issues include missing return code checks for memory expansion and contract calls, which can lead to out-of-bounds memory access and incorrect execution. An integer overflow in the memory expansion logic could also cause heap buffer overflows. Furthermore, CALL and KECCAK256 opcodes show incompatibilities with the EVM specification. Immediate remediation of these security concerns is crucial for runtime safety and reliability.

Comment thread crates/jet/src/builder/ops.rs Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a critical memory safety bug in the MLOAD opcode implementation. The MLOAD instruction was not expanding memory before reading, which could cause segmentation faults when reading from memory at or near the current capacity, as the read operation would access memory outside the allocated region.

Changes:

  • Added jet_mem_expand call before jet.mem.load in MLOAD implementation, matching the pattern already used in MSTORE and MSTORE8
  • Added comprehensive comments explaining why memory expansion must happen before the load operation

After ADDRESS_SIZE_BYTES was corrected from 2 to 20 (commit 595cf27),
the test harness still registered contracts with 2-byte addresses
("0x0000", "0x0001"), while jet_contract_call now reads 20 bytes from
the EVM stack word to build the lookup key — producing a 40-hex-char
address that never matched.

The mismatch caused jet_contract_fn_lookup to return 0 (not found),
so jet_contract_call returned early without initialising ctx.sub_call.
With sub_call still None (null), the subsequent RETURNDATASIZE
instruction dereferenced a null pointer → SIGSEGV.

Fix:
- _test_rom_body now generates 20-byte zero-padded addresses for both
  contract registration and the run_contract entry point, using
  ADDRESS_SIZE_BYTES as the single source of truth.
- basic_call_with_return_data upgrades PUSH2 to PUSH20 so the callee
  address on the EVM stack matches what jet_contract_call looks up.

https://claude.ai/code/session_01XcNd32HQupXsdnVd4KmX6f
All three memory ops were ignoring the i8 return code from jet_mem_expand.
If expansion fails (ArithmeticOverflow=-2, AllocationFailed=-3), the JIT
code proceeded to read/write through an unexpanded pointer, leading to
out-of-bounds memory access.

Add call_mem_expand_checked: emits the expand call, compares the result
to 0, and on error branches to a new block that returns ReturnCode::Invalid
from the contract function immediately. On success execution continues in
a new ok block. MLOAD, MSTORE, and MSTORE8 all use this helper instead of
the unchecked build_call.

https://claude.ai/code/session_01XcNd32HQupXsdnVd4KmX6f
@tcrypt25519
tcrypt25519 merged commit 6e431e2 into main Feb 14, 2026
1 check passed
@tcrypt25519 tcrypt25519 linked an issue Feb 14, 2026 that may be closed by this pull request
tcrypt25519 added a commit that referenced this pull request Feb 16, 2026
* fix: call jet_mem_expand before jet.mem.load in MLOAD

MLOAD was not calling jet_mem_expand before reading from memory, unlike
MSTORE and MSTORE8 which both expand first. If MLOAD was executed with
an offset at or near the current memory capacity, jet.mem.load would
compute memory_ptr + offset and jet.stack.push.ptr would then read 32
bytes (i256) from that address — potentially outside the allocated
region — causing an intermittent segfault.

The fix mirrors the existing pattern: call jet_mem_expand(ctx, offset, 32)
first so the buffer is guaranteed to cover [offset, offset+32). Since
jet_mem_expand may reallocate and update ctx.memory_ptr, jet.mem.load
then re-reads the fresh pointer from the context struct, so no dangling
pointer is ever used.

https://claude.ai/code/session_01XcNd32HQupXsdnVd4KmX6f

* fix: update test framework and CALL bytecode to use 20-byte addresses

After ADDRESS_SIZE_BYTES was corrected from 2 to 20 (commit a550f22),
the test harness still registered contracts with 2-byte addresses
("0x0000", "0x0001"), while jet_contract_call now reads 20 bytes from
the EVM stack word to build the lookup key — producing a 40-hex-char
address that never matched.

The mismatch caused jet_contract_fn_lookup to return 0 (not found),
so jet_contract_call returned early without initialising ctx.sub_call.
With sub_call still None (null), the subsequent RETURNDATASIZE
instruction dereferenced a null pointer → SIGSEGV.

Fix:
- _test_rom_body now generates 20-byte zero-padded addresses for both
  contract registration and the run_contract entry point, using
  ADDRESS_SIZE_BYTES as the single source of truth.
- basic_call_with_return_data upgrades PUSH2 to PUSH20 so the callee
  address on the EVM stack matches what jet_contract_call looks up.

https://claude.ai/code/session_01XcNd32HQupXsdnVd4KmX6f

* fix: check jet_mem_expand return value in MLOAD, MSTORE, MSTORE8

All three memory ops were ignoring the i8 return code from jet_mem_expand.
If expansion fails (ArithmeticOverflow=-2, AllocationFailed=-3), the JIT
code proceeded to read/write through an unexpanded pointer, leading to
out-of-bounds memory access.

Add call_mem_expand_checked: emits the expand call, compares the result
to 0, and on error branches to a new block that returns ReturnCode::Invalid
from the contract function immediately. On success execution continues in
a new ok block. MLOAD, MSTORE, and MSTORE8 all use this helper instead of
the unchecked build_call.

https://claude.ai/code/session_01XcNd32HQupXsdnVd4KmX6f

---------

Co-authored-by: Claude <noreply@anthropic.com>
@tcrypt25519
tcrypt25519 deleted the claude/fix-memory-expansion-pointer-cI5lF branch February 16, 2026 20:02
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.

Segfault in CI

3 participants