Skip to content
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

Fix the potential panics in crate pci #46

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/devices/pci/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bitflags::bitflags;
use core::convert::From;

use crate::mmio::{alloc_mmio32, alloc_mmio64};
use crate::{PciCommand, Result};
use crate::{PciCommand, PciError, Result};

pub const PCI_CONFIGURATION_ADDRESS_PORT: u16 = 0xCF8;
pub const PCI_CONFIGURATION_DATA_PORT: u16 = 0xCFC;
Expand Down Expand Up @@ -453,7 +453,7 @@ impl PciDevice {
self.bars[current_bar].address = addr & PCI_MEM64_BASE_ADDRESS_MASK;
current_bar_offset += 4;
}
_ => panic!("Unsupported BAR type"),
_ => return Err(PciError::InvalidBarType),
}
}

Expand Down
1 change: 1 addition & 0 deletions src/devices/pci/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ pub type Result<T> = core::result::Result<T, PciError>;
pub enum PciError {
InvalidParameter,
MmioOutofResource,
InvalidBarType,
}
20 changes: 11 additions & 9 deletions src/devices/pci/src/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ pub fn alloc_mmio32(size: u32) -> Result<u32> {

#[cfg(not(feature = "fuzz"))]
pub fn alloc_mmio32(size: u32) -> Result<u32> {
let addr = *MMIO32.lock();
let addr = align_up(addr as usize, size as usize);
let cur = *MMIO32.lock();
let addr = align_up(cur as u64, size as u64).ok_or(PciError::InvalidParameter)?;

if size > MMIO32_SIZE || addr > (MMIO32_START + MMIO32_SIZE - size) as usize {
if size > MMIO32_SIZE || addr > (MMIO32_START + MMIO32_SIZE - size) as u64 {
return Err(PciError::MmioOutofResource);
}

Expand All @@ -54,14 +54,16 @@ pub fn alloc_mmio64(size: u64) -> Result<u64> {

#[cfg(not(feature = "fuzz"))]
pub fn alloc_mmio64(size: u64) -> Result<u64> {
let addr = *MMIO64.lock();
addr.checked_add(size).ok_or(PciError::InvalidParameter)?;
let cur = *MMIO64.lock();
let addr = align_up(cur, size).ok_or(PciError::InvalidParameter)? as u64;

let addr = align_up(addr as usize, size as usize) as u64;
*MMIO64.lock() = addr + size;
*MMIO64.lock() = addr.checked_add(size).ok_or(PciError::InvalidParameter)?;
Ok(addr)
}

fn align_up(addr: usize, align: usize) -> usize {
(addr + align - 1) & !(align - 1)
fn align_up(addr: u64, align: u64) -> Option<u64> {
if align == 0 {
return None;
}
Some((addr.checked_add(align)? - 1) & !(align - 1))
}
Loading