Jelajahi Sumber

Merge pull request #68 from Caduser2020/master

Improve usage of repr(packed)
Rob Gries 4 tahun lalu
induk
melakukan
30d64f419d
5 mengubah file dengan 57 tambahan dan 6 penghapusan
  1. 1 1
      src/elf_sections.rs
  2. 1 1
      src/framebuffer.rs
  3. 52 1
      src/lib.rs
  4. 1 1
      src/module.rs
  5. 2 2
      src/rsdp.rs

+ 1 - 1
src/elf_sections.rs

@@ -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

+ 1 - 1
src/framebuffer.rs

@@ -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,

+ 52 - 1
src/lib.rs

@@ -114,7 +114,8 @@ pub struct BootInformation {
     offset: usize,
 }
 
-#[repr(C, packed)]
+#[derive(Clone, Copy)]
+#[repr(C)]
 struct BootInformationInner {
     total_size: u32,
     _reserved: u32,
@@ -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:
@@ -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.
@@ -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))]
@@ -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;
@@ -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]);
+        }
+    }
+
 }

+ 1 - 1
src/module.rs

@@ -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,

+ 2 - 2
src/rsdp.rs

@@ -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,
@@ -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,