forked from BigfootDev/flatbuffers
Rust soundness fixes (#7518)
* Rust soundness fixes * Second pass * Make init_from_table unsafe * Remove SafeSliceAccess * Clippy * Remove create_vector_of_strings * More clippy * Remove deprecated root type accessors * More soundness fixes * Fix EndianScalar for bool * Add TriviallyTransmutable * Add debug assertions * Review comments * Review feedback
This commit is contained in:
committed by
GitHub
parent
dadbff5714
commit
374f8fb5fb
@@ -17,13 +17,10 @@
|
||||
use core::fmt::{Debug, Formatter, Result};
|
||||
use core::iter::{DoubleEndedIterator, ExactSizeIterator, FusedIterator};
|
||||
use core::marker::PhantomData;
|
||||
use core::mem::size_of;
|
||||
use core::slice::from_raw_parts;
|
||||
use core::mem::{align_of, size_of};
|
||||
use core::str::from_utf8_unchecked;
|
||||
|
||||
use crate::endian_scalar::read_scalar_at;
|
||||
#[cfg(target_endian = "little")]
|
||||
use crate::endian_scalar::EndianScalar;
|
||||
use crate::follow::Follow;
|
||||
use crate::primitives::*;
|
||||
|
||||
@@ -55,6 +52,7 @@ where
|
||||
// and Clone for `T: Copy` and `T: Clone` respectively. However `Vector<'a, T>`
|
||||
// can always be copied, no matter that `T` you have.
|
||||
impl<'a, T> Copy for Vector<'a, T> {}
|
||||
|
||||
impl<'a, T> Clone for Vector<'a, T> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
@@ -62,32 +60,46 @@ impl<'a, T> Clone for Vector<'a, T> {
|
||||
}
|
||||
|
||||
impl<'a, T: 'a> Vector<'a, T> {
|
||||
/// # Safety
|
||||
///
|
||||
/// `buf` contains a valid vector at `loc` consisting of
|
||||
///
|
||||
/// - UOffsetT element count
|
||||
/// - Consecutive list of `T` elements
|
||||
#[inline(always)]
|
||||
pub fn new(buf: &'a [u8], loc: usize) -> Self {
|
||||
Vector {
|
||||
0: buf,
|
||||
1: loc,
|
||||
2: PhantomData,
|
||||
}
|
||||
pub unsafe fn new(buf: &'a [u8], loc: usize) -> Self {
|
||||
Vector(buf, loc, PhantomData)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn len(&self) -> usize {
|
||||
// Safety:
|
||||
// Valid vector at time of construction starting with UOffsetT element count
|
||||
unsafe { read_scalar_at::<UOffsetT>(self.0, self.1) as usize }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn bytes(&self) -> &'a [u8] {
|
||||
let sz = size_of::<T>();
|
||||
let len = self.len();
|
||||
&self.0[self.1 + SIZE_UOFFSET..self.1 + SIZE_UOFFSET + sz * len]
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Follow<'a> + 'a> Vector<'a, T> {
|
||||
#[inline(always)]
|
||||
pub fn get(&self, idx: usize) -> T::Inner {
|
||||
assert!(idx < self.len() as usize);
|
||||
assert!(idx < self.len());
|
||||
let sz = size_of::<T>();
|
||||
debug_assert!(sz > 0);
|
||||
T::follow(self.0, self.1 as usize + SIZE_UOFFSET + sz * idx)
|
||||
// Safety:
|
||||
// Valid vector at time of construction, verified that idx < element count
|
||||
unsafe { T::follow(self.0, self.1 as usize + SIZE_UOFFSET + sz * idx) }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@@ -96,84 +108,40 @@ impl<'a, T: Follow<'a> + 'a> Vector<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SafeSliceAccess {}
|
||||
impl<'a, T: SafeSliceAccess + 'a> Vector<'a, T> {
|
||||
pub fn safe_slice(self) -> &'a [T] {
|
||||
let buf = self.0;
|
||||
let loc = self.1;
|
||||
let sz = size_of::<T>();
|
||||
debug_assert!(sz > 0);
|
||||
let len = unsafe { read_scalar_at::<UOffsetT>(buf, loc) } as usize;
|
||||
let data_buf = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len * sz];
|
||||
let ptr = data_buf.as_ptr() as *const T;
|
||||
let s: &'a [T] = unsafe { from_raw_parts(ptr, len) };
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
impl SafeSliceAccess for u8 {}
|
||||
impl SafeSliceAccess for i8 {}
|
||||
impl SafeSliceAccess for bool {}
|
||||
|
||||
// TODO(caspern): Get rid of this. Conditional compliation is unnecessary complexity.
|
||||
// Vectors of primitives just don't work on big endian machines!!!
|
||||
#[cfg(target_endian = "little")]
|
||||
mod le_safe_slice_impls {
|
||||
impl super::SafeSliceAccess for u16 {}
|
||||
impl super::SafeSliceAccess for u32 {}
|
||||
impl super::SafeSliceAccess for u64 {}
|
||||
|
||||
impl super::SafeSliceAccess for i16 {}
|
||||
impl super::SafeSliceAccess for i32 {}
|
||||
impl super::SafeSliceAccess for i64 {}
|
||||
|
||||
impl super::SafeSliceAccess for f32 {}
|
||||
impl super::SafeSliceAccess for f64 {}
|
||||
}
|
||||
|
||||
#[cfg(target_endian = "little")]
|
||||
pub use self::le_safe_slice_impls::*;
|
||||
|
||||
pub fn follow_cast_ref<'a, T: Sized + 'a>(buf: &'a [u8], loc: usize) -> &'a T {
|
||||
/// # Safety
|
||||
///
|
||||
/// `buf` must contain a value of T at `loc` and have alignment of 1
|
||||
pub unsafe fn follow_cast_ref<'a, T: Sized + 'a>(buf: &'a [u8], loc: usize) -> &'a T {
|
||||
assert_eq!(align_of::<T>(), 1);
|
||||
let sz = size_of::<T>();
|
||||
let buf = &buf[loc..loc + sz];
|
||||
let ptr = buf.as_ptr() as *const T;
|
||||
unsafe { &*ptr }
|
||||
// SAFETY
|
||||
// buf contains a value at loc of type T and T has no alignment requirements
|
||||
&*ptr
|
||||
}
|
||||
|
||||
impl<'a> Follow<'a> for &'a str {
|
||||
type Inner = &'a str;
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
let len = unsafe { read_scalar_at::<UOffsetT>(buf, loc) } as usize;
|
||||
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
let len = read_scalar_at::<UOffsetT>(buf, loc) as usize;
|
||||
let slice = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len];
|
||||
unsafe { from_utf8_unchecked(slice) }
|
||||
from_utf8_unchecked(slice)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_endian = "little")]
|
||||
fn follow_slice_helper<T>(buf: &[u8], loc: usize) -> &[T] {
|
||||
let sz = size_of::<T>();
|
||||
debug_assert!(sz > 0);
|
||||
let len = unsafe { read_scalar_at::<UOffsetT>(buf, loc) as usize };
|
||||
let data_buf = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len * sz];
|
||||
let ptr = data_buf.as_ptr() as *const T;
|
||||
let s: &[T] = unsafe { from_raw_parts(ptr, len) };
|
||||
s
|
||||
}
|
||||
|
||||
/// Implement direct slice access if the host is little-endian.
|
||||
#[cfg(target_endian = "little")]
|
||||
impl<'a, T: EndianScalar> Follow<'a> for &'a [T] {
|
||||
type Inner = &'a [T];
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
follow_slice_helper::<T>(buf, loc)
|
||||
impl<'a> Follow<'a> for &'a [u8] {
|
||||
type Inner = &'a [u8];
|
||||
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
let len = read_scalar_at::<UOffsetT>(buf, loc) as usize;
|
||||
&buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len]
|
||||
}
|
||||
}
|
||||
|
||||
/// Implement Follow for all possible Vectors that have Follow-able elements.
|
||||
impl<'a, T: Follow<'a> + 'a> Follow<'a> for Vector<'a, T> {
|
||||
type Inner = Vector<'a, T>;
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
Vector::new(buf, loc)
|
||||
}
|
||||
}
|
||||
@@ -201,8 +169,14 @@ impl<'a, T: 'a> VectorIter<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new `VectorIter` from the provided slice
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// buf must contain a contiguous sequence of `items_num` values of `T`
|
||||
///
|
||||
#[inline]
|
||||
pub fn from_slice(buf: &'a [u8], items_num: usize) -> Self {
|
||||
pub unsafe fn from_slice(buf: &'a [u8], items_num: usize) -> Self {
|
||||
VectorIter {
|
||||
buf,
|
||||
loc: 0,
|
||||
@@ -235,7 +209,10 @@ impl<'a, T: Follow<'a> + 'a> Iterator for VectorIter<'a, T> {
|
||||
if self.remaining == 0 {
|
||||
None
|
||||
} else {
|
||||
let result = T::follow(self.buf, self.loc);
|
||||
// Safety:
|
||||
// VectorIter can only be created from a contiguous sequence of `items_num`
|
||||
// And remaining is initialized to `items_num`
|
||||
let result = unsafe { T::follow(self.buf, self.loc) };
|
||||
self.loc += sz;
|
||||
self.remaining -= 1;
|
||||
Some(result)
|
||||
@@ -272,7 +249,10 @@ impl<'a, T: Follow<'a> + 'a> DoubleEndedIterator for VectorIter<'a, T> {
|
||||
None
|
||||
} else {
|
||||
self.remaining -= 1;
|
||||
Some(T::follow(self.buf, self.loc + sz * self.remaining))
|
||||
// Safety:
|
||||
// VectorIter can only be created from a contiguous sequence of `items_num`
|
||||
// And remaining is initialized to `items_num`
|
||||
Some(unsafe { T::follow(self.buf, self.loc + sz * self.remaining) })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -309,7 +289,7 @@ impl<'a, 'b, T: Follow<'a> + 'a> IntoIterator for &'b Vector<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature="serialize")]
|
||||
#[cfg(feature = "serialize")]
|
||||
impl<'a, T> serde::ser::Serialize for Vector<'a, T>
|
||||
where
|
||||
T: 'a + Follow<'a>,
|
||||
|
||||
Reference in New Issue
Block a user