-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add c-api error handling #7784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add c-api error handling #7784
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ff5c178
Add minimal capi lifecycle support
bschoenmaeckers 131bb0e
Mark refcount methods as unsafe
bschoenmaeckers 14f3d51
Fix clippy warnings
bschoenmaeckers d1984b1
Force enable `threading` on `stdlib`
bschoenmaeckers 20e75be
Fix more clippy warnings
bschoenmaeckers e6cfbb1
Add capi support to `rustpython` executable
bschoenmaeckers e276364
Use `VM_CURRENT` when already active
bschoenmaeckers 9206893
Test that we do not create second vm when calling `PyGILState_Ensure`
bschoenmaeckers 4e88888
Fix warnings
bschoenmaeckers 3e65cec
Unify C API thread VM tracking
youknowone 0441648
Add `PYGILSTATE` to dictionary
bschoenmaeckers 61f4387
Add basic capi error support
bschoenmaeckers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Unify C API thread VM tracking
- Loading branch information
commit 3e65cecea1c60fa2303454117bda99a67eadbe9f
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
In Rust, if a raw pointer is copied from aBoxand theBoxis dropped while that raw pointer is still stored elsewhere, is it valid to dereference that pointer during or after the drop of the boxed value?💡 Result:
No. If you copy a raw pointer derived from a Box and then drop the Box while that pointer is still stored elsewhere, dereferencing the pointer during or after the drop is invalid (it triggers undefined behavior). [1][2] Reasoning (grounded in Rust’s rules for pointers and “dangling”): 1) Raw pointers have no automatic lifetime/liveness guarantees; copying/dropping the pointer itself doesn’t keep the referent alive. [3] 2) A pointer is “dangling” if it is null or if not all bytes it points to are part of the same live allocation; dereferencing a dangling pointer is one of the ways Rust characterizes incorrect behavior. [1][4] 3) After Box is dropped, the heap allocation backing it is deallocated; the raw pointer no longer has provenance/dereferenceability for non-zero-sized accesses, so reads through it require that the pointer be valid for reads to the relevant memory range, which it won’t be after free. [1][2][5] Practical takeaway: if you need a pointer that remains dereferenceable beyond the Box’s drop, you must ensure the allocation stays alive (e.g., by transferring ownership via Box::into_raw and later reconstructing via Box::from_raw), rather than dropping the Box while retaining a raw pointer to its contents. [6]
Citations:
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 624
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 621
release_current_thread()drops the VM before removing its raw pointer fromVM_STACK, violating Rust's memory safety.The function drops
GILSTATE_VMat line 181 while the corresponding raw pointer remains inVM_STACK(popped at line 185). If any destructor in the Box runs during this drop and callswith_current_vm(), it dereferences a pointer into freed memory—undefined behavior in Rust.Swap the order: pop from
VM_STACKfirst, then drop the boxed VM.🤖 Prompt for AI Agents