Skip to content

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

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

Merged
merged 2 commits into from
Apr 26, 2021

Conversation

CasperN
Copy link
Collaborator

@CasperN CasperN commented Apr 26, 2021

Resubmitting #6583 and fixing associated problems.

Looks like the Miri test in RustTest.sh didn't catch errors in #6548 related to the unsoundness of SafeSliceAccess.
I removed SafeSliceAccess for Arrays, and probably should do so for Vectors in the future too.

Calling safe_slice() -> &[T] requires T to be

  1. readable on both LE and BE targets
  2. align_of::<T>() == 1.

I actually almost saw this error in #6548 but figured it was fine because I thought SafeSliceAccess was only implemented in endian-safe, alignment-1, types; u8, i8, bool, and flatbuffers structs; however we do conditional compilation that adds SafeSliceAccess to larger scalars on little-endian targets... 🤦 This is just wrong and callers of safe_slice() may get alignment errors.

I left in the conditional compilation since create_vector_direct depends on SafeSliceAccess but only requires property 1 (its doing memcpy, no dereferencing). We test and advertise that function for vectors of larger scalars so people may be using it. create_vector_direct is fine but if clients use flatbuffers::Vector<f32>::safe_slice(), that triggers UB.

In a future PR, I will split SafeSliceAccess into the two properties to deprecate the conditional compilation and make things safe.

@3ddi @krojew

Casper Neo added 2 commits April 26, 2021 13:36
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.
@github-actions github-actions bot added the rust label Apr 26, 2021
@CasperN CasperN merged commit c87179e into google:master Apr 26, 2021
@CasperN CasperN deleted the flag branch August 24, 2021 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant