Skip to content

Commit

Permalink
Fix DMA writing to dangling pointers in ethmac.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Mar 26, 2018
1 parent 83629ca commit f067d0f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
13 changes: 8 additions & 5 deletions firmware/src/ethmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,17 @@ impl DeviceInner {
pub struct Device(RefCell<DeviceInner>);

impl Device {
pub fn new(mac: EthernetAddress) -> Device {
let mut inner = DeviceInner::new();
inner.init(mac);
Device(RefCell::new(inner))
pub fn new() -> Device {
Device(RefCell::new(DeviceInner::new()))
}

// After `init` is called, `Device` shall not be moved.
pub unsafe fn init(&mut self, mac: EthernetAddress) {
self.0.borrow_mut().init(mac);
}
}

impl<'a> phy::Device<'a> for Device {
impl<'a, 'b> phy::Device<'a> for &'b mut Device {
type RxToken = RxToken<'a>;
type TxToken = TxToken<'a>;

Expand Down
5 changes: 3 additions & 2 deletions firmware/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ fn main() {
println!("MAC {} IP {}", hardware_addr, ip_addrs[0]);
let mut neighbor_cache_storage = [None; 8];
let neighbor_cache = NeighborCache::new(&mut neighbor_cache_storage[..]);
let device = ethmac::Device::new(hardware_addr);
let mut iface = EthernetInterfaceBuilder::new(device)
let mut device = ethmac::Device::new();
unsafe { device.init(hardware_addr) };
let mut iface = EthernetInterfaceBuilder::new(&mut device)
.ethernet_addr(hardware_addr)
.neighbor_cache(neighbor_cache)
.ip_addrs(&mut ip_addrs[..])
Expand Down

0 comments on commit f067d0f

Please sign in to comment.