-
Notifications
You must be signed in to change notification settings - Fork 1.4k
_ctypes pt. 5 #5653
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
Merged
Merged
_ctypes pt. 5 #5653
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| use crate::builtins::PyType; | ||
| use crate::builtins::PyTypeRef; | ||
| use crate::stdlib::ctypes::PyCData; | ||
| use crate::types::Constructor; | ||
| use crate::types::Representable; | ||
| use crate::{Py, PyResult, VirtualMachine}; | ||
|
|
||
| #[pyclass(name = "PyCFieldType", base = PyType, module = "_ctypes")] | ||
| #[derive(PyPayload, Debug)] | ||
| pub struct PyCFieldType { | ||
| #[allow(dead_code)] | ||
| pub(super) inner: PyCField, | ||
| } | ||
|
|
||
| #[pyclass] | ||
| impl PyCFieldType {} | ||
|
|
||
| #[pyclass( | ||
| name = "CField", | ||
| base = PyCData, | ||
| metaclass = "PyCFieldType", | ||
| module = "_ctypes" | ||
| )] | ||
| #[derive(Debug, PyPayload)] | ||
| pub struct PyCField { | ||
| byte_offset: usize, | ||
| byte_size: usize, | ||
| #[allow(unused)] | ||
| index: usize, | ||
| proto: PyTypeRef, | ||
| anonymous: bool, | ||
| bitfield_size: bool, | ||
| bit_offset: u8, | ||
| name: String, | ||
| } | ||
|
|
||
| impl Representable for PyCField { | ||
| fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> { | ||
| let tp_name = zelf.proto.name().to_string(); | ||
| if zelf.bitfield_size { | ||
| Ok(format!( | ||
| "<{} type={}, ofs={byte_offset}, bit_size={bitfield_size}, bit_offset={bit_offset}", | ||
| zelf.name, | ||
| tp_name, | ||
| byte_offset = zelf.byte_offset, | ||
| bitfield_size = zelf.bitfield_size, | ||
| bit_offset = zelf.bit_offset | ||
| )) | ||
| } else { | ||
| Ok(format!( | ||
| "<{} type={tp_name}, ofs={}, size={}", | ||
| zelf.name, zelf.byte_offset, zelf.byte_size | ||
| )) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, FromArgs)] | ||
| pub struct PyCFieldConstructorArgs { | ||
| // PyObject *name, PyObject *proto, | ||
| // Py_ssize_t byte_size, Py_ssize_t byte_offset, | ||
| // Py_ssize_t index, int _internal_use, | ||
| // PyObject *bit_size_obj, PyObject *bit_offset_obj | ||
| } | ||
|
|
||
| impl Constructor for PyCField { | ||
| type Args = PyCFieldConstructorArgs; | ||
|
|
||
| fn py_new(_cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult { | ||
| Err(vm.new_type_error("Cannot instantiate a PyCField".to_string())) | ||
| } | ||
| } | ||
|
|
||
| #[pyclass(flags(BASETYPE, IMMUTABLETYPE), with(Constructor, Representable))] | ||
| impl PyCField { | ||
| #[pygetset] | ||
| fn size(&self) -> usize { | ||
| self.byte_size | ||
| } | ||
|
|
||
| #[pygetset] | ||
| fn bit_size(&self) -> bool { | ||
| self.bitfield_size | ||
| } | ||
|
|
||
| #[pygetset] | ||
| fn is_bitfield(&self) -> bool { | ||
| self.bitfield_size | ||
| } | ||
|
|
||
| #[pygetset] | ||
| fn is_anonymous(&self) -> bool { | ||
| self.anonymous | ||
| } | ||
|
|
||
| #[pygetset] | ||
| fn name(&self) -> String { | ||
| self.name.clone() | ||
| } | ||
|
|
||
| #[pygetset(name = "type")] | ||
| fn type_(&self) -> PyTypeRef { | ||
| self.proto.clone() | ||
| } | ||
|
|
||
| #[pygetset] | ||
| fn offset(&self) -> usize { | ||
| self.byte_offset | ||
| } | ||
|
|
||
| #[pygetset] | ||
| fn byte_offset(&self) -> usize { | ||
| self.byte_offset | ||
| } | ||
|
|
||
| #[pygetset] | ||
| fn byte_size(&self) -> usize { | ||
| self.byte_size | ||
| } | ||
|
|
||
| #[pygetset] | ||
| fn bit_offset(&self) -> u8 { | ||
| self.bit_offset | ||
| } | ||
| } |
Oops, something went wrong.
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.
Use-after-free:
Arg::newreferences stack-local values that are immediately dropped.libffi::middle::Arg::new(&r)captures a reference tor, butris a local variable created within the closure passed tomap. Once the closure returns,ris dropped, leaving a dangling reference inside theArg. This is undefined behavior when theArgis later used in an FFI call.You need to store the converted values in a longer-lived location. Consider storing the value in a field or using a boxed/heap-allocated approach:
pub fn to_arg( &self, ty: libffi::middle::Type, vm: &VirtualMachine, - ) -> Option<libffi::middle::Arg> { + ) -> Option<(Box<dyn std::any::Any>, libffi::middle::Arg)> { let value = unsafe { (*self.value.as_ptr()).clone() }; if let Ok(i) = value.try_int(vm) { let i = i.as_bigint(); - return if std::ptr::eq(ty.as_raw_ptr(), libffi::middle::Type::u8().as_raw_ptr()) { - i.to_u8().map(|r: u8| libffi::middle::Arg::new(&r)) + if std::ptr::eq(ty.as_raw_ptr(), libffi::middle::Type::u8().as_raw_ptr()) { + return i.to_u8().map(|r| { + let boxed: Box<u8> = Box::new(r); + let arg = libffi::middle::Arg::new(boxed.as_ref()); + (boxed as Box<dyn std::any::Any>, arg) + }); + } // ... similar pattern for other typesAlternatively, refactor to return an owned representation that the caller can hold until the FFI call completes.
🤖 Prompt for AI Agents