Rust Remove SafeSliceAccess for Arrays, and fix miri. (#6592)

* Fix Miri flag passing and bump Rust version.

* Fix Miri problems from Arrays PR.

SafeSliceAccess was removed for Arrays. It's kind of unsound.
It has two properties:
1. EndianSafe
2. Alignment 1

We only need 1. in create_vector_direct to memcpy data.
We both 1. and 2. for accessing things with slices as buffers are built on &[u8]
which is unaligned. Conditional compilation implements
SafeSliceAccess for >1byte scalars (like f32) on LittleEndian machines
which is wrong since they don't satisfy 2.

This UB is still accessible for Vectors (though not exercised our
tests) as it implements SafeSliceAccess. I'll fix this later by
splitting SafeSliceAccess into its 2 properties.

Co-authored-by: Casper Neo <cneo@google.com>
This commit is contained in:
Casper
2021-04-26 19:28:25 -04:00
committed by GitHub
parent c24031c36b
commit c87179e73e
5 changed files with 17 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "flatbuffers"
version = "0.8.4"
version = "0.8.5"
edition = "2018"
authors = ["Robert Winslow <hello@rwinslow.com>", "FlatBuffers Maintainers"]
license = "Apache-2.0"

View File

@@ -54,6 +54,9 @@ impl<'a, T: 'a, const N: usize> Array<'a, T, N> {
pub const fn len(&self) -> usize {
N
}
pub fn as_ptr(&self) -> *const u8 {
self.0.as_ptr()
}
}
impl<'a, T: Follow<'a> + 'a, const N: usize> Array<'a, T, N> {
@@ -77,14 +80,7 @@ impl<'a, T: Follow<'a> + Debug, const N: usize> Into<[T::Inner; N]> for Array<'a
}
}
impl<'a, T: SafeSliceAccess + 'a, const N: usize> Array<'a, T, N> {
pub fn safe_slice(self) -> &'a [T] {
let sz = size_of::<T>();
debug_assert!(sz > 0);
let ptr = self.0.as_ptr() as *const T;
unsafe { from_raw_parts(ptr, N) }
}
}
// TODO(caspern): Implement some future safe version of SafeSliceAccess.
/// Implement Follow for all possible Arrays that have Follow-able elements.
impl<'a, T: Follow<'a> + 'a, const N: usize> Follow<'a> for Array<'a, T, N> {
@@ -100,12 +96,16 @@ pub fn emplace_scalar_array<T: EndianScalar, const N: usize>(
loc: usize,
src: &[T; N],
) {
let mut buf_ptr = buf[loc..].as_mut_ptr() as *mut T;
let mut buf_ptr = buf[loc..].as_mut_ptr();
for item in src.iter() {
let item_le = item.to_little_endian();
unsafe {
buf_ptr.write(item_le);
buf_ptr = buf_ptr.add(1);
core::ptr::copy_nonoverlapping(
&item_le as *const T as *const u8,
buf_ptr,
size_of::<T>(),
);
buf_ptr = buf_ptr.add(size_of::<T>());
}
}
}