Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@lschuermann
Copy link
Member

Pull Request Overview

To support ADC operations on &'static mut [u16] slice references, while at the same time using DMA over &'static mut [u8] slice references, the MSP432's ADC implementation features conversion helpers. While these are unsafe and check some of the required invariants, these checks and the documentation have been insufficient.

In particular, it is not guaranteed that a given u8 will be well-aligned to the alignment constraints of a u16. Thus, simply transmuting a *mut u8 to a *mut 16 and converting this back to a slice can cause undefined behavior.

This does not add additional checks on these functions given they are already unsafe. Furthermore, this issue likely does not affect the existing codebase, given that only buffers which were &'static mut [u16] slice references get converted to &'static mut [u8], and back to &'static mut [u16] again. Nonetheless, the presence of checks for some of the required invariants could be interpreted as a function which is safe to use, even though it is marked unsafe.

Instead, this PR adds a clear indication of the required invariants and the consequences of a violation of any given requirement.

Testing Strategy

This pull request was tested by proofreading.

TODO or Help Wanted

This code seems weird. Also, is a conversion from &'static mut [u16] to &'static mut [u8] unsound in any case? If not, we shouldn't mark it unsafe.

Documentation Updated

  • Updated the relevant files in /docs, or no updates are required.

Formatting

  • Ran make prepush.

@lebakassemmerl

To support ADC operations on `&'static mut [u16]` slice references,
while at the same time using DMA over `&'static mut [u8]` slice
references, the MSP432's ADC implementation features conversion
helpers. While these are unsafe and check some of the required
invariants, these checks and the documentation have been insufficient.

In particular, it is not guaranteed that a given `u8` will be
well-aligned to the alignment constraints of a `u16`. Thus, simply
transmuting a `*mut u8` to a `*mut 16` and converting this back to a
slice can cause undefined behavior.

This does not add additional checks on these functions given they are
already unsafe. Furthermore, this issue likely does not affect the
existing codebase, given that only buffers which were `&'static mut
[u16]` slice references get converted to `&'static mut [u8]`, and back
to `&'static mut [u16]` again. Nonetheless, the presence of checks for
some of the required invariants could be interpreted as a function
which is safe to use, even though it is marked unsafe.

Instead, this PR adds a clear indication of the required invariants
and the consequences of a violation of any given requirement.

Signed-off-by: Leon Schuermann <[email protected]>
@phil-levis phil-levis added the tock-2.0-include This PR will hopefully be included in the Tock 2.0 release. label Jul 24, 2021
@bradjc
Copy link
Contributor

bradjc commented Jul 27, 2021

bors r+

@bors
Copy link
Contributor

bors bot commented Jul 27, 2021

@bors bors bot merged commit a9824f2 into tock:master Jul 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tock-2.0-include This PR will hopefully be included in the Tock 2.0 release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants