Skip to content
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

Copying inappropriately aligned buffer in ipc reader #2883

Merged
merged 10 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions arrow-array/src/array/list_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,16 +861,40 @@ mod tests {
}

#[test]
#[should_panic(expected = "memory is not aligned")]
#[should_panic(expected = "Need at least 8 bytes in buffers[0]")]
Copy link
Contributor

@tustvold tustvold Oct 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why MIRI isn't failing and the debug assertion isn't firing, the test panics before it does anything interesting... 🤦

fn test_primitive_array_alignment() {
let ptr = arrow_buffer::alloc::allocate_aligned(8);
let ptr = arrow_buffer::alloc::allocate_aligned_zeroed(8);
let buf = unsafe { Buffer::from_raw_parts(ptr, 8, 8) };
let buf1 = buf.slice(1);
let buf2 = buf.slice(1);

// Aligned
let array_data = ArrayData::builder(DataType::Int32)
.add_buffer(buf)
.len(2)
.build()
.unwrap();
let array = Int32Array::from(array_data);
assert_eq!(array.len(), 2);
assert_eq!(array.value(0), 0);
assert_eq!(array.value(1), 0);

// Not aligned.
// `ArrayData::build` checks buffer length.
let array_data = ArrayData::builder(DataType::Int32)
.add_buffer(buf1)
.len(1)
.build()
.unwrap();
let array = Int32Array::from(array_data);
assert_eq!(array.len(), 1);
assert_eq!(array.value(0), 0);
Copy link
Contributor

@tustvold tustvold Oct 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now UB, as this violates the safety requirement in PrimitiveArray::values.

I'm not sure why MIRI isn't catching this...

Edit: array.value doesn't call array.values so this test isn't UB. If you add a call to array.values() in this test, MIRI will fail


ArrayData::builder(DataType::Int32)
.add_buffer(buf2)
.len(2)
.build()
.unwrap();
drop(Int32Array::from(array_data));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion arrow-array/src/array/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ impl<T: ArrowPrimitiveType> From<ArrayData> for PrimitiveArray<T> {
data,
// SAFETY:
// ArrayData must be valid, and validated data type above
raw_values: unsafe { RawPtrBox::new(ptr) },
raw_values: unsafe { RawPtrBox::unchecked_new(ptr) },
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions arrow-array/src/raw_pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ impl<T> RawPtrBox<T> {
Self { ptr: ptr.cast() }
}

/// # Safety
/// The user must guarantee that:
/// * the contents where `ptr` points to are never `moved`. This is guaranteed when they are Pinned.
/// * the lifetime of this struct does not outlive the lifetime of `ptr`.
/// Failure to fulfill any the above conditions results in undefined behavior.
/// # Panic
/// This function panics if:
/// * `ptr` is null
///
/// Unlike `new`, this function does not check if `ptr` is aligned to a slice of type `T`.
pub(crate) unsafe fn unchecked_new(ptr: *const u8) -> Self {
let ptr = NonNull::new(ptr as *mut u8).expect("Pointer cannot be null");
Self { ptr: ptr.cast() }
}

pub(super) fn as_ptr(&self) -> *const T {
self.ptr.as_ptr()
}
Expand Down