Skip to content

Commit 30d64f4

Browse files
Merge pull request #68 from Caduser2020/master
Improve usage of repr(packed)
2 parents 5ad3166 + e6aff7c commit 30d64f4

File tree

5 files changed

+57
-6
lines changed

5 files changed

+57
-6
lines changed

src/elf_sections.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub unsafe fn elf_sections_tag(tag: &Tag, offset: usize) -> ElfSectionsTag {
1313
assert_eq!(9, tag.typ);
1414
let es = ElfSectionsTag {
1515
inner: (tag as *const Tag).offset(1) as *const ElfSectionsTagInner,
16-
offset: offset,
16+
offset,
1717
};
1818
assert!((es.get().entry_size * es.get().shndx) <= tag.size);
1919
es

src/framebuffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub struct FramebufferField {
6666

6767
/// A framebuffer color descriptor in the palette.
6868
#[derive(Clone, Copy, Debug, PartialEq)]
69-
#[repr(C, packed)]
69+
#[repr(C, packed)] // only repr(C) would add unwanted padding at the end
7070
pub struct FramebufferColor {
7171
/// The Red component of the color.
7272
pub red: u8,

src/lib.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ pub struct BootInformation {
114114
offset: usize,
115115
}
116116

117-
#[repr(C, packed)]
117+
#[derive(Clone, Copy)]
118+
#[repr(C)]
118119
struct BootInformationInner {
119120
total_size: u32,
120121
_reserved: u32,
@@ -465,6 +466,15 @@ mod tests {
465466
assert!(bi.command_line_tag().is_none());
466467
}
467468

469+
#[test]
470+
/// Compile time test for `BootLoaderNameTag`.
471+
fn name_tag_size() {
472+
use BootLoaderNameTag;
473+
unsafe {
474+
core::mem::transmute::<[u8; 9], BootLoaderNameTag>([0u8; 9]);
475+
}
476+
}
477+
468478
#[test]
469479
fn framebuffer_tag_rgb() {
470480
// direct RGB mode test:
@@ -586,6 +596,16 @@ mod tests {
586596
}
587597
}
588598

599+
#[test]
600+
/// Compile time test for `FramebufferTag`.
601+
fn framebuffer_tag_size() {
602+
use crate::FramebufferTag;
603+
unsafe {
604+
// 24 for the start + 24 for `FramebufferType`.
605+
core::mem::transmute::<[u8; 48], FramebufferTag>([0u8; 48]);
606+
}
607+
}
608+
589609
#[test]
590610
fn vbe_info_tag() {
591611
//Taken from GRUB2 running in QEMU.
@@ -744,6 +764,16 @@ mod tests {
744764
}
745765
}
746766

767+
#[test]
768+
/// Compile time test for `VBEInfoTag`.
769+
fn vbe_info_tag_size() {
770+
use VBEInfoTag;
771+
unsafe {
772+
// 16 for the start + 512 from `VBEControlInfo` + 256 from `VBEModeInfo`.
773+
core::mem::transmute::<[u8; 784], VBEInfoTag>([0u8; 784]);
774+
}
775+
}
776+
747777
#[test]
748778
fn grub2() {
749779
#[repr(C, align(8))]
@@ -1224,6 +1254,16 @@ mod tests {
12241254
assert!(s.next().is_none());
12251255
}
12261256

1257+
#[test]
1258+
/// Compile time test for `ElfSectionsTag`.
1259+
fn elf_sections_tag_size() {
1260+
use super::ElfSectionsTag;
1261+
unsafe {
1262+
// `ElfSectionsTagInner` is 12 bytes + 4 in the offset.
1263+
core::mem::transmute::<[u8; 16], ElfSectionsTag>([0u8; 16]);
1264+
}
1265+
}
1266+
12271267
#[test]
12281268
fn efi_memory_map() {
12291269
use memory_map::EFIMemoryAreaType;
@@ -1290,4 +1330,15 @@ mod tests {
12901330
let efi_mmap = boot_info.efi_memory_map_tag();
12911331
assert!(efi_mmap.is_none());
12921332
}
1333+
1334+
#[test]
1335+
/// Compile time test for `EFIMemoryMapTag`.
1336+
fn efi_memory_map_tag_size() {
1337+
use super::EFIMemoryMapTag;
1338+
unsafe {
1339+
// `EFIMemoryMapTag` is 16 bytes + `EFIMemoryDesc` is 40 bytes.
1340+
core::mem::transmute::<[u8; 56], EFIMemoryMapTag>([0u8; 56]);
1341+
}
1342+
}
1343+
12931344
}

src/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use header::{Tag, TagIter};
33
/// This tag indicates to the kernel what boot module was loaded along with
44
/// the kernel image, and where it can be found.
55
#[derive(Clone, Copy, Debug)]
6-
#[repr(C, packed)]
6+
#[repr(C, packed)] // only repr(C) would add unwanted padding near name_byte.
77
pub struct ModuleTag {
88
typ: u32,
99
size: u32,

src/rsdp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const RSDPV1_LENGTH: usize = 20;
1313

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

3030
/// EFI system table in 64 bit mode
3131
#[derive(Clone, Copy, Debug)]
32-
#[repr(C, packed)]
32+
#[repr(C)]
3333
pub struct EFISdt64 {
3434
typ: u32,
3535
size: u32,

0 commit comments

Comments
 (0)