-
Notifications
You must be signed in to change notification settings - Fork 4.2k
ARROW-12202: [Rust] WIP Fix SEGFAULT/ SIGILL in child-data ffi #9887
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
Changes from 2 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,7 +81,7 @@ use std::{ | |
| ffi::CStr, | ||
| ffi::CString, | ||
| iter, | ||
| mem::{size_of, ManuallyDrop}, | ||
| mem::size_of, | ||
| os::raw::c_char, | ||
| ptr::{self, NonNull}, | ||
| sync::Arc, | ||
|
|
@@ -93,6 +93,11 @@ use crate::datatypes::{DataType, Field, TimeUnit}; | |
| use crate::error::{ArrowError, Result}; | ||
| use crate::util::bit_util; | ||
|
|
||
| const FLAG_NONE: i64 = 0; | ||
| const FLAG_DICTIONARY_ORDERED: i64 = 1; | ||
| const FLAG_NULLABLE: i64 = 2; | ||
| const FLAG_MAP_KEYS_SORTED: i64 = 4; | ||
|
|
||
| /// ABI-compatible struct for `ArrowSchema` from C Data Interface | ||
| /// See <https://arrow.apache.org/docs/format/CDataInterface.html#structure-definitions> | ||
| /// This was created by bindgen | ||
|
|
@@ -135,13 +140,14 @@ impl FFI_ArrowSchema { | |
| let n_children = children.len() as i64; | ||
| let children_ptr = children.as_ptr() as *mut *mut FFI_ArrowSchema; | ||
|
|
||
| let flags = if nullable { 2 } else { 0 }; | ||
| let flags = if nullable { FLAG_NULLABLE } else { FLAG_NONE }; | ||
|
|
||
| let private_data = Box::new(SchemaPrivateData { children }); | ||
| // <https://arrow.apache.org/docs/format/CDataInterface.html#c.ArrowSchema> | ||
| FFI_ArrowSchema { | ||
| format: CString::new(format).unwrap().into_raw(), | ||
| // For child data a non null string is expected and is called item | ||
| // TODO: get the field name for general nested data | ||
| // For List child data a non null string is expected and is called item | ||
| name: CString::new("item").unwrap().into_raw(), | ||
| metadata: std::ptr::null_mut(), | ||
| flags, | ||
|
|
@@ -159,7 +165,7 @@ impl FFI_ArrowSchema { | |
| format: std::ptr::null_mut(), | ||
| name: std::ptr::null_mut(), | ||
| metadata: std::ptr::null_mut(), | ||
| flags: 0, | ||
| flags: FLAG_NONE, | ||
| n_children: 0, | ||
| children: ptr::null_mut(), | ||
| dictionary: std::ptr::null_mut(), | ||
|
|
@@ -221,31 +227,27 @@ fn to_datatype( | |
| // at that point the child data is not yet known, but it is also not required to determine | ||
| // the buffer length of the list arrays. | ||
| "+l" => { | ||
| let nullable = schema.flags == 2; | ||
| let nullable = (schema.flags & FLAG_NULLABLE) != 0; | ||
| // Safety | ||
| // Should be set as this is expected from the C FFI definition | ||
| debug_assert!(!schema.name.is_null()); | ||
| let name = unsafe { CString::from_raw(schema.name as *mut c_char) } | ||
| .into_string() | ||
| let name = unsafe { CStr::from_ptr(schema.name as *const c_char) } | ||
| .to_str() | ||
| .unwrap(); | ||
| // prevent a double free | ||
| let name = ManuallyDrop::new(name); | ||
| DataType::List(Box::new(Field::new( | ||
| &name, | ||
| child_type.unwrap_or(DataType::Null), | ||
| nullable, | ||
| ))) | ||
| } | ||
| "+L" => { | ||
| let nullable = schema.flags == 2; | ||
| let nullable = (schema.flags & FLAG_NULLABLE) != 0; | ||
| // Safety | ||
| // Should be set as this is expected from the C FFI definition | ||
| debug_assert!(!schema.name.is_null()); | ||
| let name = unsafe { CString::from_raw(schema.name as *mut c_char) } | ||
| .into_string() | ||
| let name = unsafe { CStr::from_ptr(schema.name as *const c_char) } | ||
| .to_str() | ||
| .unwrap(); | ||
| // prevent a double free | ||
| let name = ManuallyDrop::new(name); | ||
| DataType::LargeList(Box::new(Field::new( | ||
| &name, | ||
| child_type.unwrap_or(DataType::Null), | ||
|
|
@@ -395,6 +397,12 @@ unsafe extern "C" fn release_array(array: *mut FFI_ArrowArray) { | |
| // take ownership of `private_data`, therefore dropping it | ||
| Box::from_raw(array.private_data as *mut PrivateData); | ||
|
|
||
| // release children | ||
| for i in 0..array.n_children { | ||
| let child = *array.children.add(i as usize); | ||
| release_array(child); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should call the release callback stored on
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note you must do the same thing in |
||
| } | ||
|
|
||
| array.release = None; | ||
| } | ||
|
|
||
|
|
@@ -654,7 +662,7 @@ impl ArrowArray { | |
| debug_assert_eq!(bits % 8, 0); | ||
| (self.array.length as usize + 1) * (bits / 8) | ||
| } | ||
| (DataType::Utf8, 2) | (DataType::Binary, 2) | (DataType::List(_), 2) => { | ||
| (DataType::Utf8, 2) | (DataType::Binary, 2) => { | ||
| // the len of the data buffer (buffer 2) equals the last value of the offset buffer (buffer 1) | ||
| let len = self.buffer_len(1)?; | ||
| // first buffer is the null buffer => add(1) | ||
|
|
@@ -666,9 +674,7 @@ impl ArrowArray { | |
| // get last offset | ||
| (unsafe { *offset_buffer.add(len / size_of::<i32>() - 1) }) as usize | ||
| } | ||
| (DataType::LargeUtf8, 2) | ||
| | (DataType::LargeBinary, 2) | ||
| | (DataType::LargeList(_), 2) => { | ||
| (DataType::LargeUtf8, 2) | (DataType::LargeBinary, 2) => { | ||
| // the len of the data buffer (buffer 2) equals the last value of the offset buffer (buffer 1) | ||
| let len = self.buffer_len(1)?; | ||
| // first buffer is the null buffer => add(1) | ||
|
|
||
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
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.
This should be released in
release_schema, likeformatalready is.