fix: call jet_mem_expand before jet.mem.load in MLOAD#74
Conversation
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
Changed Files
|
Summary of ChangesHello @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 Highlights
Changelog
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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_expandcall beforejet.mem.loadin 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
* 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>
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