-
Notifications
You must be signed in to change notification settings - Fork 109
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
Put shared buffer into UnsafeCell #185
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,45 @@ | ||
use crate::syscalls; | ||
use core::cell::UnsafeCell; | ||
use core::ptr; | ||
|
||
#[must_use = "Shared memory risks being dropped too early. Drop it manually."] | ||
pub struct SharedMemory<'a> { | ||
pub struct SharedMemory<T> { | ||
driver_number: usize, | ||
allow_number: usize, | ||
buffer_to_share: &'a mut [u8], | ||
buffer_to_share: UnsafeCell<T>, | ||
} | ||
|
||
impl<'a> SharedMemory<'a> { | ||
pub fn new( | ||
driver_number: usize, | ||
allow_number: usize, | ||
buffer_to_share: &'a mut [u8], | ||
) -> SharedMemory<'a> { | ||
impl<T> SharedMemory<T> | ||
where | ||
T: AsMut<[u8]>, | ||
{ | ||
pub fn new(driver_number: usize, allow_number: usize, buffer_to_share: T) -> SharedMemory<T> { | ||
SharedMemory { | ||
driver_number, | ||
allow_number, | ||
buffer_to_share, | ||
buffer_to_share: UnsafeCell::new(buffer_to_share), | ||
} | ||
} | ||
|
||
pub fn read_bytes<T: AsMut<[u8]>>(&self, mut destination: T) { | ||
safe_copy(self.buffer_to_share, destination.as_mut()); | ||
pub fn read_bytes<D: AsMut<[u8]>>(&self, mut destination: D) { | ||
let buf = unsafe { (*self.buffer_to_share.get()).as_mut() }; | ||
safe_copy(buf, destination.as_mut()); | ||
} | ||
|
||
pub fn write_bytes<T: AsRef<[u8]>>(&mut self, source: T) { | ||
safe_copy(source.as_ref(), self.buffer_to_share); | ||
pub fn write_bytes<S: AsRef<[u8]>>(&mut self, source: S) { | ||
let buf = unsafe { (*self.buffer_to_share.get()).as_mut() }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not delegate this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea! |
||
safe_copy(source.as_ref(), buf); | ||
} | ||
|
||
pub(crate) unsafe fn operate_on_mut_ptr<R: Sized, F: FnOnce(*mut u8) -> R>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right! |
||
&self, | ||
func: F, | ||
) -> R { | ||
func((*self.buffer_to_share.get()).as_mut().as_mut_ptr()) | ||
} | ||
} | ||
|
||
impl<'a> Drop for SharedMemory<'a> { | ||
impl<T> Drop for SharedMemory<T> { | ||
fn drop(&mut self) { | ||
unsafe { | ||
syscalls::raw::allow(self.driver_number, self.allow_number, ptr::null_mut(), 0); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,12 @@ pub struct AdcBuffer { | |
buffer: [u8; BUFFER_SIZE], | ||
} | ||
|
||
impl AsMut<[u8]> for AdcBuffer { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You no longer need this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer if all |
||
fn as_mut(&mut self) -> &mut [u8] { | ||
&mut self.buffer | ||
} | ||
} | ||
|
||
impl Default for AdcBuffer { | ||
fn default() -> Self { | ||
AdcBuffer { | ||
|
@@ -67,13 +73,12 @@ impl<CB: FnMut(usize, usize)> Consumer<CB> for AdcEventConsumer { | |
} | ||
|
||
impl<'a> Adc<'a> { | ||
pub fn init_buffer(&self, buffer: &'a mut AdcBuffer) -> TockResult<SharedMemory> { | ||
syscalls::allow(DRIVER_NUMBER, allow_nr::BUFFER, &mut buffer.buffer).map_err(Into::into) | ||
pub fn init_buffer(&self, buffer: AdcBuffer) -> TockResult<SharedMemory<AdcBuffer>> { | ||
syscalls::allow(DRIVER_NUMBER, allow_nr::BUFFER, buffer).map_err(Into::into) | ||
} | ||
|
||
pub fn init_alt_buffer(&self, alt_buffer: &'a mut AdcBuffer) -> TockResult<SharedMemory> { | ||
syscalls::allow(DRIVER_NUMBER, allow_nr::BUFFER_ALT, &mut alt_buffer.buffer) | ||
.map_err(Into::into) | ||
pub fn init_alt_buffer(&self, alt_buffer: AdcBuffer) -> TockResult<SharedMemory<AdcBuffer>> { | ||
syscalls::allow(DRIVER_NUMBER, allow_nr::BUFFER_ALT, alt_buffer).map_err(Into::into) | ||
} | ||
|
||
/// Return the number of available channels | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,16 +53,24 @@ impl BleAdvertisingDriverFactory { | |
#[non_exhaustive] | ||
pub struct BleAdvertisingDriver; | ||
|
||
pub struct BleAdvertisingBuffer([u8; BUFFER_SIZE_ADVERTISE]); | ||
|
||
impl AsMut<[u8]> for BleAdvertisingBuffer { | ||
fn as_mut(&mut self) -> &mut [u8] { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl BleAdvertisingDriver { | ||
pub fn create_advertising_buffer() -> [u8; BUFFER_SIZE_ADVERTISE] { | ||
[0; BUFFER_SIZE_ADVERTISE] | ||
pub fn create_advertising_buffer() -> BleAdvertisingBuffer { | ||
BleAdvertisingBuffer([0; BUFFER_SIZE_ADVERTISE]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert this? |
||
} | ||
pub fn initialize<'a>( | ||
&'a mut self, | ||
interval: usize, | ||
service_payload: &BlePayload, | ||
advertising_buffer: &'a mut [u8; BUFFER_SIZE_ADVERTISE], | ||
) -> TockResult<SharedMemory<'a>> { | ||
advertising_buffer: BleAdvertisingBuffer, | ||
) -> TockResult<SharedMemory<BleAdvertisingBuffer>> { | ||
let mut shared_memory = syscalls::allow( | ||
DRIVER_NUMBER, | ||
allow_nr::ALLOW_ADVERTISMENT_BUFFER, | ||
|
@@ -86,12 +94,19 @@ impl BleAdvertisingDriver { | |
|
||
struct BleCallback<'a> { | ||
read_value: &'a Cell<Option<ScanBuffer>>, | ||
shared_buffer: SharedMemory<'a>, | ||
shared_buffer: SharedMemory<ScanBuffer>, | ||
} | ||
|
||
pub(crate) type ScanBuffer = [u8; BUFFER_SIZE_SCAN]; | ||
#[derive(Clone, Copy)] | ||
pub struct ScanBuffer([u8; BUFFER_SIZE_SCAN]); | ||
|
||
impl AsRef<[u8]> for ScanBuffer { | ||
fn as_ref(&self) -> &[u8] { | ||
&self.0 | ||
} | ||
} | ||
|
||
const EMPTY_SCAN_BUFFER: ScanBuffer = [0; BUFFER_SIZE_SCAN]; | ||
const EMPTY_SCAN_BUFFER: ScanBuffer = ScanBuffer([0; BUFFER_SIZE_SCAN]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert? |
||
|
||
#[non_exhaustive] | ||
pub struct BleScanningDriverFactory; | ||
|
@@ -130,13 +145,19 @@ pub struct BleScanningDriver { | |
read_value: Cell<Option<ScanBuffer>>, | ||
} | ||
|
||
impl AsMut<[u8]> for ScanBuffer { | ||
fn as_mut(&mut self) -> &mut [u8] { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl BleScanningDriver { | ||
/// Prepare Ble Scanning Driver to share memory with the ble capsule | ||
pub fn share_memory(&mut self) -> TockResult<BleScanningDriverShared> { | ||
let shared_buffer: SharedMemory = syscalls::allow( | ||
let shared_buffer = syscalls::allow( | ||
DRIVER_NUMBER, | ||
allow_nr::ALLOW_SCAN_BUFFER, | ||
&mut self.shared_buffer, | ||
self.shared_buffer, | ||
) | ||
.map_err(Into::<TockError>::into)?; | ||
Ok(BleScanningDriverShared { | ||
|
@@ -195,7 +216,7 @@ impl<'a> Consumer<Self> for BleCallback<'a> { | |
fn consume(callback: &mut Self, _: usize, _: usize, _: usize) { | ||
let mut temporary_buffer: ScanBuffer = EMPTY_SCAN_BUFFER; | ||
|
||
callback.shared_buffer.read_bytes(&mut temporary_buffer[..]); | ||
callback.shared_buffer.read_bytes(temporary_buffer.as_mut()); | ||
callback.read_value.set(Some(temporary_buffer)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is wrong! Instead of enforcing to pass an array reference which would pin the referenced array it is now possible to pass an entire array. This should result in UB once you move the
SharedMemory
object around.Why not change the signature to
UnsafeCell<&'a mut [u8]>
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I fixed this.