Skip to content

Improve usage of repr(packed) #68

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

Merged
merged 5 commits into from Aug 26, 2020
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
2 changes: 1 addition & 1 deletion src/elf_sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub unsafe fn elf_sections_tag(tag: &Tag, offset: usize) -> ElfSectionsTag {
assert_eq!(9, tag.typ);
let es = ElfSectionsTag {
inner: (tag as *const Tag).offset(1) as *const ElfSectionsTagInner,
offset: offset,
offset,
};
assert!((es.get().entry_size * es.get().shndx) <= tag.size);
es
Expand Down
2 changes: 1 addition & 1 deletion src/framebuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub struct FramebufferField {

/// A framebuffer color descriptor in the palette.
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C, packed)]
#[repr(C, packed)] // only repr(C) would add unwanted padding at the end
pub struct FramebufferColor {
/// The Red component of the color.
pub red: u8,
Expand Down
53 changes: 52 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ pub struct BootInformation {
offset: usize,
}

#[repr(C, packed)]
#[derive(Clone, Copy)]
#[repr(C)]
struct BootInformationInner {
total_size: u32,
_reserved: u32,
Expand Down Expand Up @@ -465,6 +466,15 @@ mod tests {
assert!(bi.command_line_tag().is_none());
}

#[test]
/// Compile time test for `BootLoaderNameTag`.
fn name_tag_size() {
use BootLoaderNameTag;
unsafe {
core::mem::transmute::<[u8; 9], BootLoaderNameTag>([0u8; 9]);
}
}

#[test]
fn framebuffer_tag_rgb() {
// direct RGB mode test:
Expand Down Expand Up @@ -586,6 +596,16 @@ mod tests {
}
}

#[test]
/// Compile time test for `FramebufferTag`.
fn framebuffer_tag_size() {
use crate::FramebufferTag;
unsafe {
// 24 for the start + 24 for `FramebufferType`.
core::mem::transmute::<[u8; 48], FramebufferTag>([0u8; 48]);
}
}

#[test]
fn vbe_info_tag() {
//Taken from GRUB2 running in QEMU.
Expand Down Expand Up @@ -744,6 +764,16 @@ mod tests {
}
}

#[test]
/// Compile time test for `VBEInfoTag`.
fn vbe_info_tag_size() {
use VBEInfoTag;
unsafe {
// 16 for the start + 512 from `VBEControlInfo` + 256 from `VBEModeInfo`.
core::mem::transmute::<[u8; 784], VBEInfoTag>([0u8; 784]);
}
}

#[test]
fn grub2() {
#[repr(C, align(8))]
Expand Down Expand Up @@ -1224,6 +1254,16 @@ mod tests {
assert!(s.next().is_none());
}

#[test]
/// Compile time test for `ElfSectionsTag`.
fn elf_sections_tag_size() {
use super::ElfSectionsTag;
unsafe {
// `ElfSectionsTagInner` is 12 bytes + 4 in the offset.
core::mem::transmute::<[u8; 16], ElfSectionsTag>([0u8; 16]);
}
}

#[test]
fn efi_memory_map() {
use memory_map::EFIMemoryAreaType;
Expand Down Expand Up @@ -1290,4 +1330,15 @@ mod tests {
let efi_mmap = boot_info.efi_memory_map_tag();
assert!(efi_mmap.is_none());
}

#[test]
/// Compile time test for `EFIMemoryMapTag`.
fn efi_memory_map_tag_size() {
use super::EFIMemoryMapTag;
unsafe {
// `EFIMemoryMapTag` is 16 bytes + `EFIMemoryDesc` is 40 bytes.
core::mem::transmute::<[u8; 56], EFIMemoryMapTag>([0u8; 56]);
}
}

}
2 changes: 1 addition & 1 deletion src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use header::{Tag, TagIter};
/// This tag indicates to the kernel what boot module was loaded along with
/// the kernel image, and where it can be found.
#[derive(Clone, Copy, Debug)]
#[repr(C, packed)]
#[repr(C, packed)] // only repr(C) would add unwanted padding near name_byte.
pub struct ModuleTag {
typ: u32,
size: u32,
Expand Down
4 changes: 2 additions & 2 deletions src/rsdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const RSDPV1_LENGTH: usize = 20;

/// EFI system table in 32 bit mode
#[derive(Clone, Copy, Debug)]
#[repr(C, packed)]
#[repr(C, packed)] // only repr(C) would add unwanted padding before first_section
pub struct EFISdt32 {
typ: u32,
size: u32,
Expand All @@ -29,7 +29,7 @@ impl EFISdt32 {

/// EFI system table in 64 bit mode
#[derive(Clone, Copy, Debug)]
#[repr(C, packed)]
#[repr(C)]
pub struct EFISdt64 {
typ: u32,
size: u32,
Expand Down