diff options
author | Casper <casperneo@uchicago.edu> | 2021-01-11 15:24:52 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-11 15:24:52 -0500 |
commit | 408cf5802415e1dea65fef7489a6c2f3740fb381 (patch) | |
tree | 31a4303e3ac6c029e7a22c58bda09798d8fad7c5 /samples | |
parent | 39e115fdb468d614accc09d177f1b46900473481 (diff) | |
download | flatbuffers-408cf5802415e1dea65fef7489a6c2f3740fb381.tar.gz flatbuffers-408cf5802415e1dea65fef7489a6c2f3740fb381.tar.bz2 flatbuffers-408cf5802415e1dea65fef7489a6c2f3740fb381.zip |
Fix Rust UB problems (#6393)
* Fix miri problems by assuming alignment is 1 in rust
* Removed is_aligned fn from rust verifier.
* Add back is_aligned, but make it w.r.t. buffer[0]
* touch unused variable
* touch unused variable
* +nightly
* Move Rust miri testing into its own docker
* fix bash
* missing one endian conversion
* fix endianness2
* format stuff
Co-authored-by: Casper Neo <cneo@google.com>
Diffstat (limited to 'samples')
-rw-r--r-- | samples/monster_generated.rs | 92 |
1 files changed, 76 insertions, 16 deletions
diff --git a/samples/monster_generated.rs b/samples/monster_generated.rs index 175453d5..eb4f6c39 100644 --- a/samples/monster_generated.rs +++ b/samples/monster_generated.rs @@ -200,13 +200,9 @@ impl<'a> flatbuffers::Verifiable for Equipment { impl flatbuffers::SimpleToVerifyInSlice for Equipment {} // struct Vec3, aligned to 4 -#[repr(C, align(4))] +#[repr(transparent)] #[derive(Clone, Copy, PartialEq)] -pub struct Vec3 { - x_: f32, - y_: f32, - z_: f32, -} // pub struct Vec3 +pub struct Vec3(pub [u8; 12]); impl std::fmt::Debug for Vec3 { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { f.debug_struct("Vec3") @@ -266,23 +262,87 @@ impl<'a> flatbuffers::Verifiable for Vec3 { } impl Vec3 { #[allow(clippy::too_many_arguments)] - pub fn new(_x: f32, _y: f32, _z: f32) -> Self { - Vec3 { - x_: _x.to_little_endian(), - y_: _y.to_little_endian(), - z_: _z.to_little_endian(), - - } + pub fn new( + x: f32, + y: f32, + z: f32, + ) -> Self { + let mut s = Self([0; 12]); + s.set_x(x); + s.set_y(y); + s.set_z(z); + s } + pub fn x(&self) -> f32 { - self.x_.from_little_endian() + let mut mem = core::mem::MaybeUninit::<f32>::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[0..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::<f32>(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_x(&mut self, x: f32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const f32 as *const u8, + self.0[0..].as_mut_ptr(), + core::mem::size_of::<f32>(), + ); + } } + pub fn y(&self) -> f32 { - self.y_.from_little_endian() + let mut mem = core::mem::MaybeUninit::<f32>::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[4..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::<f32>(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_y(&mut self, x: f32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const f32 as *const u8, + self.0[4..].as_mut_ptr(), + core::mem::size_of::<f32>(), + ); + } } + pub fn z(&self) -> f32 { - self.z_.from_little_endian() + let mut mem = core::mem::MaybeUninit::<f32>::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[8..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::<f32>(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_z(&mut self, x: f32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const f32 as *const u8, + self.0[8..].as_mut_ptr(), + core::mem::size_of::<f32>(), + ); + } } + } pub enum MonsterOffset {} |