Skip to content

Commit 9f596f3

Browse files
committed
Change message type to slice for safety reasons.
1 parent b8f7789 commit 9f596f3

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

examples/ipcserver.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@ fn main() {
1515
let mut console = Console::new();
1616
console.write(String::from("Start service:\n"));
1717

18-
let cb = &mut |pid: usize, _: usize, message: &mut [u8; 32]| {
18+
#[allow(unused_variables)]
19+
let server = IpcServerDriver::start(|pid: usize, _: usize, message: &mut [u8]| {
1920
console.write(String::from("Server: \"Payload: "));
2021

2122
console.write(u32_as_hex(message[0] as u32));
2223
console.write(String::from("\"\n"));
2324
message[0] += 1;
2425
ipc_cs::notify_client(pid);
25-
};
26-
#[allow(unused_variables)]
27-
let server = IpcServerDriver::start(cb);
26+
});
2827

2928
loop {
3029
tock::syscalls::yieldk();

src/ipc_cs/server.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use callback::CallbackSubscription;
22
use callback::SubscribableCallback;
33
use core::marker::PhantomData;
4+
use core::slice;
45
use syscalls;
56

67
const DRIVER_NUMBER: usize = 0x10000;
@@ -10,12 +11,12 @@ mod ipc_commands {
1011
pub const NOTIFY_CLIENT: usize = 1;
1112
}
1213

13-
pub struct IpcServerCallback<S, CB> {
14+
pub struct IpcServerCallback<S: ?Sized, CB> {
1415
callback: CB,
1516
phantom_data: PhantomData<S>,
1617
}
1718

18-
impl<S, CB: FnMut(usize, usize, &mut S)> SubscribableCallback for IpcServerCallback<S, CB> {
19+
impl<CB: FnMut(usize, usize, &mut [u8])> SubscribableCallback for IpcServerCallback<[u8], CB> {
1920
fn driver_number(&self) -> usize {
2021
DRIVER_NUMBER
2122
}
@@ -25,8 +26,8 @@ impl<S, CB: FnMut(usize, usize, &mut S)> SubscribableCallback for IpcServerCallb
2526
}
2627

2728
fn call_rust(&mut self, arg0: usize, arg1: usize, arg2: usize) {
28-
let data = unsafe { &mut *(arg2 as *mut S) };
29-
(self.callback)(arg0, arg1, data);
29+
let mut v = unsafe { slice::from_raw_parts_mut(arg2 as *mut u8, arg1) };
30+
(self.callback)(arg0, arg1, &mut v);
3031
}
3132
}
3233

@@ -37,9 +38,9 @@ pub fn notify_client(pid: usize) {
3738
pub struct IpcServerDriver;
3839

3940
impl IpcServerDriver {
40-
pub fn start<S, CB: FnMut(usize, usize, &mut S)>(
41+
pub fn start<CB: FnMut(usize, usize, &mut [u8])>(
4142
callback: CB,
42-
) -> Result<CallbackSubscription<IpcServerCallback<S, CB>>, ()> {
43+
) -> Result<CallbackSubscription<IpcServerCallback<[u8], CB>>, ()> {
4344
let (_, subscription) = syscalls::subscribe_new(IpcServerCallback {
4445
callback,
4546
phantom_data: Default::default(),

0 commit comments

Comments
 (0)