Skip to content

Upgrade libffi 5 - #6473

Merged
youknowone merged 1 commit into
RustPython:mainfrom
youknowone:libffi
Dec 24, 2025
Merged

youknowone merged 1 commit into
RustPython:mainfrom
youknowone:libffi

Conversation

@youknowone

@youknowone youknowone commented Dec 23, 2025 •

Copy link
Copy Markdown
Member

close #6397

Summary by CodeRabbit

Release Notes

  • Dependency Updates

    • Updated libffi dependency from version 4.1 to 5.
  • Breaking Changes

    • Removed the PyCArray.to_arg() method, which previously allowed conversion of array objects to FFI-compatible arguments.
  • Internal Improvements

    • Enhanced FFI subsystem with improved argument handling, stronger type safety guarantees, and better lifetime management for more robust foreign function interface operations.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Dec 23, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The libffi dependency is upgraded from version 4.1 to 5.0, requiring codebase adaptations to accommodate new lifetime annotations on the libffi Arg type. Changes propagate across JIT and ctypes modules, including argument handling refactoring and method signature updates.

Changes

Cohort / File(s) Summary
Dependency update
Cargo.toml
libffi version bumped from "4.1" to "5"
JIT core refactoring
crates/jit/src/lib.rs
Args struct refactored to store values: Vec<AbiValue> and code: &'a CompiledCode (instead of _values and cif_args); invoke now builds cif_args on-the-fly via AbiValue::to_libffi_arg; ArgsBuilder::into_args enforces all values are set; invoke_raw and to_libffi_arg signatures updated with lifetime bounds Arg<'_>
ctypes array & base modules
crates/vm/src/stdlib/ctypes/array.rs, crates/vm/src/stdlib/ctypes/base.rs
Removed PyCArray::to_arg method; updated FfiArgValue::as_arg return type to libffi::middle::Arg<'_>
ctypes argument handling
crates/vm/src/stdlib/ctypes.rs, crates/vm/src/stdlib/ctypes/function.rs
Type adjustments to use Arg<'_> with explicit lifetime annotations in ffi_args vectors and related argument processing

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Suggested reviewers

  • arihant2math

Poem

🐰 A hop, skip, and libffi leap!
Lifetimes spring where Arguments creep,
From four to five, we've made the bound,
New Arg<'_> dances around,
RustyVM's ctypes now leap deep! ✨

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Upgrade libffi 5' directly and concisely describes the main change: updating the libffi dependency from version 4.1 to 5.
Linked Issues check ✅ Passed The PR successfully addresses issue #6397 by updating libffi from 4.1.2 to 5.0.0, with all necessary code adjustments for the new version's API changes including lifetime annotations.
Out of Scope Changes check ✅ Passed All changes are directly related to the libffi 5 upgrade: dependency version bump and necessary API compatibility adjustments across JIT and ctypes modules.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2785bd8 and 7c7e55f.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (6)
  • Cargo.toml
  • crates/jit/src/lib.rs
  • crates/vm/src/stdlib/ctypes.rs
  • crates/vm/src/stdlib/ctypes/array.rs
  • crates/vm/src/stdlib/ctypes/base.rs
  • crates/vm/src/stdlib/ctypes/function.rs
💤 Files with no reviewable changes (1)
  • crates/vm/src/stdlib/ctypes/array.rs
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.rs: Follow the default rustfmt code style by running cargo fmt to format Rust code
Always run clippy to lint Rust code (cargo clippy) before completing tasks and fix any warnings or lints introduced by changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass, pymodule, pyfunction, etc.) when implementing Python functionality in Rust

Files:

  • crates/vm/src/stdlib/ctypes/function.rs
  • crates/vm/src/stdlib/ctypes.rs
  • crates/vm/src/stdlib/ctypes/base.rs
  • crates/jit/src/lib.rs
🔇 Additional comments (8)
crates/vm/src/stdlib/ctypes/function.rs (1)

1449-1464: Lifetime annotation is correct.

The Arg<'_> lifetime is properly scoped: the ffi_args vector borrows from the arguments slice which lives through the entire ctypes_callproc function, ensuring the FFI call in cif.call operates on valid references.

crates/vm/src/stdlib/ctypes/base.rs (1)

1822-1840: Lifetime annotation correctly ties Arg to the borrowed FfiArgValue.

The return type Arg<'_> properly captures that the argument borrows from &self. Each match arm passes a reference to the stored value (v), ensuring the Arg cannot outlive the FfiArgValue it references.

crates/vm/src/stdlib/ctypes.rs (1)

1105-1141: Lifetime handling is sound.

The ffi_args: Vec<Arg<'_>> borrows from arg_values which remains in scope through the cif.call. The two-phase approach (first collect values, then create Arg references) ensures the referenced data outlives the FFI call.

crates/jit/src/lib.rs (4)

160-169: Lifetime annotation correctly applied to invoke_raw.

The Arg<'_> slice parameter ensures the arguments remain valid for the duration of the unsafe FFI call block.


221-229: to_libffi_arg correctly returns Arg<'_> tied to &self.

Each variant creates an Arg that borrows from the AbiValue's inner field, ensuring the returned Arg cannot outlive the source value.


352-361: Improved into_args with explicit completeness check.

The refactored logic now explicitly checks that all values are set before unwrapping, making the invariant clearer. Previously, the code may have had a different structure. This is a clean approach that avoids potential panics from unwrapping None.


364-373: Args struct and invoke method correctly restructured for lifetime safety.

Storing Vec<AbiValue> instead of pre-built Arg values is the correct approach with libffi 5. Building cif_args on-demand in invoke() ensures the Arg<'_> references are created in the same scope where they're used, avoiding lifetime issues that would occur if Arg values were stored across method boundaries.

Cargo.toml (1)

181-181: Version bump correctly updated with matching API changes.

The upgrade to libffi 5 is properly reflected in Cargo.toml and the dependent code has been updated accordingly. The lifetime annotation changes (Arg<'_>) in ctypes and JIT modules are consistent with libffi 5.0.0's API, which now uses anonymous lifetimes instead of named lifetime parameters.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@youknowone
youknowone marked this pull request as ready for review December 24, 2025 00:44
@youknowone
youknowone merged commit c35cb89 into RustPython:main Dec 24, 2025
@youknowone
youknowone deleted the libffi branch December 24, 2025 01:35
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.

1 participant