Skip to content

Commit

Permalink
FDT configurator: specific FDT size check
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandra Iordache <[email protected]>
  • Loading branch information
Alexandra Iordache authored and andreeaflorescu committed May 18, 2020
1 parent 4db08d9 commit bd01b6d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion coverage_config_aarch64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 79.9,
"coverage_score": 80.3,
"exclude_path": "",
"crate_features": ""
}
25 changes: 15 additions & 10 deletions src/configurator/aarch64/fdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use crate::configurator::{BootConfigurator, BootParams, Error as BootConfigurato
/// Errors specific to the device tree boot protocol configuration.
#[derive(Debug, PartialEq)]
pub enum Error {
/// FDT does not fit in guest memory.
FDTPastRamEnd,
/// Error writing FDT in memory.
WriteFDTToMemory,
}
Expand All @@ -22,6 +24,7 @@ impl StdError for Error {
fn description(&self) -> &str {
use Error::*;
match self {
FDTPastRamEnd => "FDT does not fit in guest memory.",
WriteFDTToMemory => "Error writing FDT in guest memory.",
}
}
Expand Down Expand Up @@ -57,6 +60,10 @@ impl BootConfigurator for FdtBootConfigurator {
where
M: GuestMemory,
{
guest_memory
.checked_offset(params.header_start, params.header.len())
.ok_or(Error::FDTPastRamEnd)?;

// The VMM has filled an FDT and passed it as a `ByteValued` object.
guest_memory
.write_slice(params.header.as_slice(), params.header_start)
Expand Down Expand Up @@ -86,23 +93,17 @@ mod tests {
let guest_memory = create_guest_mem();

// Error case: FDT doesn't fit in guest memory.
let fdt_addr = guest_memory
.last_addr()
.checked_sub(FDT_MAX_SIZE as u64 - 2)
.unwrap();
let fdt_addr = GuestAddress(guest_memory.last_addr().raw_value() - FDT_MAX_SIZE as u64 + 1);
assert_eq!(
FdtBootConfigurator::write_bootparams::<GuestMemoryMmap>(
BootParams::new::<FdtPlaceholder>(&fdt, fdt_addr),
&guest_memory,
)
.err(),
Some(Error::WriteFDTToMemory.into())
Some(Error::FDTPastRamEnd.into())
);

let fdt_addr = guest_memory
.last_addr()
.checked_sub(FDT_MAX_SIZE as u64 - 1)
.unwrap();
let fdt_addr = GuestAddress(guest_memory.last_addr().raw_value() - FDT_MAX_SIZE as u64);
assert!(FdtBootConfigurator::write_bootparams::<GuestMemoryMmap>(
BootParams::new::<FdtPlaceholder>(&fdt, fdt_addr),
&guest_memory,
Expand All @@ -112,9 +113,13 @@ mod tests {

#[test]
fn test_error_messages() {
assert_eq!(
format!("{}", Error::FDTPastRamEnd),
"Device Tree Boot Configurator Error: FDT does not fit in guest memory."
);
assert_eq!(
format!("{}", Error::WriteFDTToMemory),
"Device Tree Boot Configurator Error: Error writing FDT in guest memory."
)
);
}
}

0 comments on commit bd01b6d

Please sign in to comment.