Skip to content

Merge error handling refactor#27

Merged
tcrypt25519 merged 13 commits into
masterfrom
ts/conflict
Feb 8, 2026
Merged

Merge error handling refactor#27
tcrypt25519 merged 13 commits into
masterfrom
ts/conflict

Conversation

@tcrypt25519

Copy link
Copy Markdown
Owner

Summary

Merges the copilot/refactor-error-handling branch which introduces comprehensive error handling improvements to the runtime.

Changes

  • Error module: New error.rs with RuntimeError and Result types
  • Improved error handling: Better error propagation in Context::new() and sub-context creation
  • Enhanced builtins: Refined error codes and documentation for contract calls
  • Runtime integration: Updated multiple builder and engine files to use new error types

Test plan

  • Verify compilation passes
  • Existing tests should continue to pass

🤖 Generated with Claude Code

Copilot AI and others added 7 commits February 6, 2026 04:33
Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>
Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>
Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>
Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>
…x error codes

Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>
Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>
Resolved merge conflicts in runtime error handling refactor:
- builtins.rs: Integrated improved error handling with proper enum mapping
- exec.rs: Fixed Context::new() return statement syntax
- lib.rs: Merged constant definitions with error exports

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 7, 2026 22:58
@semanticdiff-com

semanticdiff-com Bot commented Feb 7, 2026

Copy link
Copy Markdown

Review changes with  SemanticDiff

Changed Files
File Status
  crates/jet/src/builder/contract.rs  70% smaller
  crates/jet/src/builder/env.rs  64% smaller
  crates/jet/src/builder/manager.rs  43% smaller
  crates/jet_runtime/src/exec.rs  40% smaller
  crates/jet_runtime/src/builtins.rs  17% smaller
  crates/jet/src/engine/mod.rs  3% smaller
  crates/jet/src/bin/jetdbg.rs  1% smaller
  crates/jet/src/builder/mod.rs  0% smaller
  crates/jet_runtime/Cargo.toml Unsupported file format
  crates/jet_runtime/src/error.rs  0% smaller
  crates/jet_runtime/src/lib.rs  0% 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 significantly overhauls the error handling mechanisms within the runtime. The primary goal is to move away from abrupt panics towards a more robust and explicit error propagation model using Rust's Result type. This change enhances the stability and predictability of the system by providing clear error types and codes, particularly for contract interactions and core runtime operations, allowing for more controlled error recovery and debugging.

Highlights

  • Comprehensive Error Handling Refactor: Introduced a new error.rs module in jet_runtime defining RuntimeError and a custom Result type, centralizing error management.
  • Improved Error Propagation: Replaced unwrap() and panic! calls with Result and map_err across the codebase, particularly in Context::new() and sub-context creation, ensuring errors are propagated gracefully.
  • Enhanced Builtin Contract Call Error Codes: The jet_contract_call and jet_contract_call_return_data_copy functions in builtins.rs now return explicit, documented error codes via new ContractCallError and CopyError enums, improving clarity and debuggability for contract interactions.
  • Robustness in Builder and Engine Components: Updated various builder and engine files (contract.rs, env.rs, manager.rs, engine/mod.rs) to integrate and propagate the new RuntimeError types, making these components more resilient to underlying runtime failures.
  • Graceful Logger Initialization: The jetdbg.rs binary now handles logger initialization errors by printing a message to stderr and exiting, rather than panicking.
Changelog
  • crates/jet/src/bin/jetdbg.rs
    • Modified logger initialization to use map_err for graceful error handling and process exit instead of unwrap().
  • crates/jet/src/builder/contract.rs
    • Changed CodeBlocks::add to return Result<&mut CodeBlock, Error> for better error handling.
    • Replaced unwrap() with unwrap_or("<invalid UTF-8>") for safer string conversion in logging.
    • Updated code_blocks.first().unwrap() to use ok_or_else for explicit error handling.
    • Propagated errors from blocks.add calls using the ? operator.
  • crates/jet/src/builder/env.rs
    • Updated Env::new to return Result<Self, super::Error>.
    • Replaced panic! with ok_or_else to return an InvariantViolation error if runtime functions fail to load.
  • crates/jet/src/builder/manager.rs
    • Replaced unwrap() with map(...).unwrap_or(false) for safer function lookup.
    • Implemented match statements for SyntaxSet::load_from_folder and ps.find_syntax_by_extension to handle errors gracefully by printing warnings and returning early.
    • Added error handling for h.highlight_line using match, logging warnings and falling back to printing the original line.
  • crates/jet/src/builder/mod.rs
    • Added a Runtime(#[from] jet_runtime::RuntimeError) variant to the Error enum for transparent conversion of runtime errors.
  • crates/jet/src/engine/mod.rs
    • Updated load_runtime_module and Env::new calls to use the ? operator for error propagation.
    • Modified exec::Context::new() call to use map_err to convert jet_runtime::RuntimeError into builder::Error::Runtime.
  • crates/jet_runtime/Cargo.toml
    • Added thiserror = "1.0.61" dependency.
  • crates/jet_runtime/src/builtins.rs
    • Introduced ContractCallError and CopyError enums with explicit error codes for jet_contract_call and jet_contract_call_return_data_copy.
    • Implemented comprehensive pointer validation and match statements for error handling in jet_contract_call, returning specific ContractCallError codes.
    • Updated caller_ctx.init_sub_call() to handle its Result return type.
    • Added pointer validation and match statements for error handling in jet_contract_call_return_data_copy, returning specific CopyError codes.
    • Adjusted return values to align with the new error enums.
  • crates/jet_runtime/src/error.rs
    • New file added, defining the RuntimeError enum (using thiserror) for memory allocation, memory layout, null pointer, and invariant violation errors.
    • Defined Result<T> as a type alias for std::result::Result<T, RuntimeError>.
  • crates/jet_runtime/src/exec.rs
    • Imported RuntimeError and Result from the new error module.
    • Modified Context::new() to return Result<Self>, handling memory allocation and layout errors.
    • Updated init_sub_call() to return Result<&mut Context>, handling sub-context creation errors and invariant violations.
    • Changed Drop for Context to log errors during memory deallocation instead of panicking.
  • crates/jet_runtime/src/lib.rs
    • Added pub mod error; to expose the new error module.
    • Exported RuntimeError and Result from the error module.
Activity
  • The pull request description indicates that the changes were generated or assisted by 'Claude Code', an AI tool.
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.

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 refactors runtime and builder/engine codepaths to use structured, non-panicking error handling, introducing a shared RuntimeError/Result and improving FFI builtin pointer validation and error code documentation.

Changes:

  • Added crates/jet_runtime::error with RuntimeError + Result, and re-exported them from jet_runtime.
  • Made jet_runtime::exec::Context::new() and sub-context creation fallible and propagated errors through jet builder/engine APIs.
  • Refined contract-call builtins with explicit error-code enums and null-pointer checks.

Reviewed changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/jet_runtime/src/lib.rs Exposes new runtime error module + re-exports RuntimeError/Result.
crates/jet_runtime/src/exec.rs Makes context allocation fallible; makes sub-context creation return Result; avoids panics in Drop.
crates/jet_runtime/src/error.rs Introduces RuntimeError and a runtime Result<T> alias via thiserror.
crates/jet_runtime/src/builtins.rs Adds explicit FFI error codes + pointer validation and updates return-data-copy error handling/docs.
crates/jet_runtime/Cargo.toml Adds thiserror dependency for the new error module.
crates/jet/src/engine/mod.rs Propagates runtime/builder errors instead of unwrapping; adapts to fallible Context::new()/Env::new().
crates/jet/src/builder/mod.rs Adds Runtime error variant wrapping jet_runtime::RuntimeError.
crates/jet/src/builder/manager.rs Replaces unwraps in IR printing/verification with non-panicking handling.
crates/jet/src/builder/env.rs Makes Env::new fallible when runtime symbols can’t be resolved.
crates/jet/src/builder/contract.rs Replaces unwraps with invariant violations; makes code-block creation fallible.
crates/jet/src/bin/jetdbg.rs Avoids unwrap on logger init (but current control flow is awkward).
Comments suppressed due to low confidence (1)

crates/jet_runtime/src/builtins.rs:165

  • Several u32 additions here can overflow (src_offset + requested_ret_len, ret_offset + requested_ret_len, dest_offset + requested_ret_len). In release builds that can wrap and bypass bounds checks, leading to panics or out-of-bounds slicing. Use checked_add/checked_sub (or widen to u64/usize) and return an error code on overflow before computing ranges.
    if src_offset + requested_ret_len > ret_len {
        return CopyError::BoundsCheckFailed as u8;
    }
    let ret_offset_end = ret_offset + requested_ret_len;
    if ret_offset_end > ret_len {
        return CopyError::BoundsCheckFailed as u8;
    }
    // TODO: Enable this check after adding memory len handling
    // if ret_offset_end > mem_len {
    //     return 3;
    // }

    // Ensure memory is large enough for the write
    let required_memory_len = dest_offset + requested_ret_len;
    if ctx.memory_len() < required_memory_len {

Comment thread crates/jet_runtime/src/builtins.rs Outdated
Comment thread crates/jet_runtime/src/builtins.rs Outdated

@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 significantly refactors error handling across the jet and jet_runtime crates, replacing panics (unwrap, expect, panic!) with Result-based propagation and introducing structured RuntimeErrors, which greatly improves robustness. However, critical vulnerabilities were identified in the jet_contract_call_return_data_copy builtin function's bounds checking logic. These checks are susceptible to integer overflows and fail to validate the contract-supplied return length against memory capacity, potentially leading to information leaks (arbitrary memory reads) or denial of service (panics). Additionally, the review includes a suggestion to improve error handling consistency in the jetdbg binary and points out a potential logic bug in a bounds check within jet_runtime's built-in functions.

Comment thread crates/jet_runtime/src/builtins.rs Outdated
Comment thread crates/jet_runtime/src/builtins.rs Outdated
Comment thread crates/jet/src/bin/jetdbg.rs Outdated
MSTORE does not update memory_len, so checking ret_offset + ret_len <= memory_len
always fails. This check was deliberately omitted in the original code with a
TODO comment. Restore the TODO until memory tracking is implemented.
@tcrypt25519
tcrypt25519 merged commit f844206 into master Feb 8, 2026
1 check passed
tcrypt25519 added a commit that referenced this pull request Feb 16, 2026
* Add error handling to jet_runtime and jet crates

Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>

* Address code review feedback - optimize unreachable error checks

Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>

* Improve safety comments and error logging per code review

Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>

* Replace unwrap_unchecked with regular unwrap and document error codes

Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>

* Address PR review comments: remove unwrap, add pointer validation, fix error codes

Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>

* Convert error codes to enums and fix duplicate -1 values

Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>

* fix

* style: apply rustfmt formatting 🤖

* fix: remove unused jet_runtime self import in engine/mod.rs

* fix: remove unused Style import in builder/manager.rs

* fix: remove invalid memory_len bounds check in return_data_copy_impl

MSTORE does not update memory_len, so checking ret_offset + ret_len <= memory_len
always fails. This check was deliberately omitted in the original code with a
TODO comment. Restore the TODO until memory tracking is implemented.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: tcrypt25519 <212655132+tcrypt25519@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
@tcrypt25519
tcrypt25519 deleted the ts/conflict branch February 16, 2026 20:03
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.

3 participants