Skip to content

Commit

Permalink
uefi-raw: add test for MemoryDescriptor to catch Miri problems
Browse files Browse the repository at this point in the history
The underlying issue is that if someone wants to cast a slice of descriptors to a
byte slice, the uninitialized **implicit** padding bytes are UB. Miri complains
about that.
  • Loading branch information
phip1611 committed Oct 1, 2024
1 parent 7e8b55c commit 8f43158
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions uefi-raw/src/table/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,34 @@ pub enum Tpl: usize => {
/// Note that this is not necessarily the processor's page size. The UEFI page
/// size is always 4 KiB.
pub const PAGE_SIZE: usize = 4096;

#[cfg(test)]
mod tests {
use core::{mem, slice};
use super::*;

// This tests succeeds if Miri doesn't complain about uninitialized memory.
#[test]
fn memory_descriptor_trivial_serialization() {
let descs = [
MemoryDescriptor {
ty: MemoryType::CONVENTIONAL,
phys_start: 0x1000,
virt_start: 0x1000,
page_count: 1,
att: Default::default(),
},
];


let raw_bytes: &[u8] = {
let ptr = descs.as_ptr().cast::<u8>();
let len = mem::size_of_val(&descs);
unsafe { slice::from_raw_parts(ptr.cast(), len) }
};

// Test succeeds if Miri doesn't complain about initialized memory
// (implicit padding fields).
dbg!(&raw_bytes);
}
}

0 comments on commit 8f43158

Please sign in to comment.