Skip to content

Commit

Permalink
ARROW-11222: [Rust] Catch up with flatbuffers 0.8.1 which had some UB…
Browse files Browse the repository at this point in the history
… problems fixed

The major change of [flatbuffers 0.8.1](https://docs.rs/flatbuffers/0.8.1/flatbuffers/index.html) since 0.8.0 is google/flatbuffers#6393, which fixed some possible memory alignment issues.

In this PR, the ipc/gen/*.rs files are generated by `regen.sh` as before, without any manual change.

Closes apache#9176 from mqy/flatbuffers-0.8.1

Authored-by: mqy <meng.qingyou@gmail.com>
Signed-off-by: Andrew Lamb <andrew@nerdnetworks.org>
  • Loading branch information
mqy authored and GeorgeAp committed Jun 7, 2021
1 parent 42a18b0 commit 59f35dd
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 209 deletions.
6 changes: 2 additions & 4 deletions rust/arrow/regen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Change to the toplevel Rust directory
pushd $DIR/../../

# As of 2020-12-06, the snapshot flatc version is not changed since "1.12.0",
# so let's build flatc from source.

echo "Build flatc from source ..."

FB_URL="https://github.com/google/flatbuffers"
FB_COMMIT="05192553f434d10c5f585aeb6a07a55a6ac702a5"
# https://github.com/google/flatbuffers/pull/6393
FB_COMMIT="408cf5802415e1dea65fef7489a6c2f3740fb381"
FB_DIR="rust/arrow/.flatbuffers"
FLATC="$FB_DIR/bazel-bin/flatc"

Expand Down
105 changes: 81 additions & 24 deletions rust/arrow/src/ipc/gen/File.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,9 @@ use std::{cmp::Ordering, mem};
// automatically generated by the FlatBuffers compiler, do not modify

// struct Block, aligned to 8
#[repr(C, align(8))]
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq)]
pub struct Block {
offset_: i64,
metaDataLength_: i32,
padding0__: u32,
bodyLength_: i64,
} // pub struct Block
pub struct Block(pub [u8; 24]);
impl std::fmt::Debug for Block {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Block")
Expand Down Expand Up @@ -82,36 +77,98 @@ impl<'b> flatbuffers::Push for &'b Block {

impl<'a> flatbuffers::Verifiable for Block {
#[inline]
fn run_verifier<'o, 'b>(
v: &mut flatbuffers::Verifier<'o, 'b>,
fn run_verifier(
v: &mut flatbuffers::Verifier,
pos: usize,
) -> Result<(), flatbuffers::InvalidFlatbuffer> {
use flatbuffers::Verifiable;
v.in_buffer::<Self>(pos)
}
}
impl Block {
pub fn new(_offset: i64, _metaDataLength: i32, _bodyLength: i64) -> Self {
Block {
offset_: _offset.to_little_endian(),
metaDataLength_: _metaDataLength.to_little_endian(),
bodyLength_: _bodyLength.to_little_endian(),

padding0__: 0,
}
#[allow(clippy::too_many_arguments)]
pub fn new(offset: i64, metaDataLength: i32, bodyLength: i64) -> Self {
let mut s = Self([0; 24]);
s.set_offset(offset);
s.set_metaDataLength(metaDataLength);
s.set_bodyLength(bodyLength);
s
}

/// Index to the start of the RecordBlock (note this is past the Message header)
pub fn offset(&self) -> i64 {
self.offset_.from_little_endian()
let mut mem = core::mem::MaybeUninit::<i64>::uninit();
unsafe {
core::ptr::copy_nonoverlapping(
self.0[0..].as_ptr(),
mem.as_mut_ptr() as *mut u8,
core::mem::size_of::<i64>(),
);
mem.assume_init()
}
.from_little_endian()
}

pub fn set_offset(&mut self, x: i64) {
let x_le = x.to_little_endian();
unsafe {
core::ptr::copy_nonoverlapping(
&x_le as *const i64 as *const u8,
self.0[0..].as_mut_ptr(),
core::mem::size_of::<i64>(),
);
}
}

/// Length of the metadata
pub fn metaDataLength(&self) -> i32 {
self.metaDataLength_.from_little_endian()
let mut mem = core::mem::MaybeUninit::<i32>::uninit();
unsafe {
core::ptr::copy_nonoverlapping(
self.0[8..].as_ptr(),
mem.as_mut_ptr() as *mut u8,
core::mem::size_of::<i32>(),
);
mem.assume_init()
}
.from_little_endian()
}

pub fn set_metaDataLength(&mut self, x: i32) {
let x_le = x.to_little_endian();
unsafe {
core::ptr::copy_nonoverlapping(
&x_le as *const i32 as *const u8,
self.0[8..].as_mut_ptr(),
core::mem::size_of::<i32>(),
);
}
}

/// Length of the data (this is aligned so there can be a gap between this and
/// the metadata).
pub fn bodyLength(&self) -> i64 {
self.bodyLength_.from_little_endian()
let mut mem = core::mem::MaybeUninit::<i64>::uninit();
unsafe {
core::ptr::copy_nonoverlapping(
self.0[16..].as_ptr(),
mem.as_mut_ptr() as *mut u8,
core::mem::size_of::<i64>(),
);
mem.assume_init()
}
.from_little_endian()
}

pub fn set_bodyLength(&mut self, x: i64) {
let x_le = x.to_little_endian();
unsafe {
core::ptr::copy_nonoverlapping(
&x_le as *const i64 as *const u8,
self.0[16..].as_mut_ptr(),
core::mem::size_of::<i64>(),
);
}
}
}

Expand Down Expand Up @@ -210,8 +267,8 @@ impl<'a> Footer<'a> {

impl flatbuffers::Verifiable for Footer<'_> {
#[inline]
fn run_verifier<'o, 'b>(
v: &mut flatbuffers::Verifier<'o, 'b>,
fn run_verifier(
v: &mut flatbuffers::Verifier,
pos: usize,
) -> Result<(), flatbuffers::InvalidFlatbuffer> {
use flatbuffers::Verifiable;
Expand Down Expand Up @@ -344,13 +401,13 @@ impl std::fmt::Debug for Footer<'_> {
}
}
#[inline]
#[deprecated(since = "1.13", note = "Deprecated in favor of `root_as...` methods.")]
#[deprecated(since = "2.0.0", note = "Deprecated in favor of `root_as...` methods.")]
pub fn get_root_as_footer<'a>(buf: &'a [u8]) -> Footer<'a> {
unsafe { flatbuffers::root_unchecked::<Footer<'a>>(buf) }
}

#[inline]
#[deprecated(since = "1.13", note = "Deprecated in favor of `root_as...` methods.")]
#[deprecated(since = "2.0.0", note = "Deprecated in favor of `root_as...` methods.")]
pub fn get_size_prefixed_root_as_footer<'a>(buf: &'a [u8]) -> Footer<'a> {
unsafe { flatbuffers::size_prefixed_root_unchecked::<Footer<'a>>(buf) }
}
Expand Down
Loading

0 comments on commit 59f35dd

Please sign in to comment.