瀏覽代碼

multiboot2: fix Miri issues in elf_sections() test

Miri still outputs a warning tho: "warning: integer-to-pointer cast"

Nevertheless, no we are UB free!
Philipp Schuster 8 月之前
父節點
當前提交
78d42b86e9
共有 3 個文件被更改,包括 16 次插入31 次删除
  1. 1 1
      multiboot2/src/boot_information.rs
  2. 15 26
      multiboot2/src/elf_sections.rs
  3. 0 4
      multiboot2/src/lib.rs

+ 1 - 1
multiboot2/src/boot_information.rs

@@ -440,7 +440,7 @@ impl fmt::Debug for BootInformation<'_> {
             if elf_sections_tag_entries_count > ELF_SECTIONS_LIMIT {
                 debug.field("elf_sections (count)", &elf_sections_tag_entries_count);
             } else {
-                debug.field("elf_sections", &self.elf_sections().unwrap_or_default());
+                debug.field("elf_sections", &self.elf_sections());
             }
         }
 

+ 15 - 26
multiboot2/src/elf_sections.rs

@@ -2,6 +2,7 @@
 
 use crate::{TagHeader, TagTrait, TagType};
 use core::fmt::{Debug, Formatter};
+use core::marker::{PhantomData, PhantomPinned};
 use core::mem;
 use core::str::Utf8Error;
 #[cfg(feature = "builder")]
@@ -34,21 +35,18 @@ impl ElfSectionsTag {
     }
 
     /// Get an iterator of loaded ELF sections.
-    pub(crate) const fn sections(&self) -> ElfSectionIter {
+    pub(crate) fn sections(&self) -> ElfSectionIter {
         let string_section_offset = (self.shndx * self.entry_size) as isize;
         let string_section_ptr =
-            unsafe { self.first_section().offset(string_section_offset) as *const _ };
+            unsafe { self.sections.as_ptr().offset(string_section_offset) as *const _ };
         ElfSectionIter {
-            current_section: self.first_section(),
+            current_section: self.sections.as_ptr(),
             remaining_sections: self.number_of_sections,
             entry_size: self.entry_size,
             string_section: string_section_ptr,
+            _phantom_data: PhantomData::default(),
         }
     }
-
-    const fn first_section(&self) -> *const u8 {
-        &(self.sections[0]) as *const _
-    }
 }
 
 impl TagTrait for ElfSectionsTag {
@@ -75,23 +73,24 @@ impl Debug for ElfSectionsTag {
 
 /// An iterator over some ELF sections.
 #[derive(Clone)]
-/// TODO make this memory safe with lifetime capture.
-pub struct ElfSectionIter {
+pub struct ElfSectionIter<'a> {
     current_section: *const u8,
     remaining_sections: u32,
     entry_size: u32,
     string_section: *const u8,
+    _phantom_data: PhantomData<&'a ()>,
 }
 
-impl Iterator for ElfSectionIter {
-    type Item = ElfSection;
+impl<'a> Iterator for ElfSectionIter<'a> {
+    type Item = ElfSection<'a>;
 
-    fn next(&mut self) -> Option<ElfSection> {
+    fn next(&mut self) -> Option<ElfSection<'a>> {
         while self.remaining_sections != 0 {
             let section = ElfSection {
                 inner: self.current_section,
                 string_section: self.string_section,
                 entry_size: self.entry_size,
+                _phantom: PhantomData::default(),
             };
 
             self.current_section = unsafe { self.current_section.offset(self.entry_size as isize) };
@@ -105,7 +104,7 @@ impl Iterator for ElfSectionIter {
     }
 }
 
-impl Debug for ElfSectionIter {
+impl<'a> Debug for ElfSectionIter<'a> {
     fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
         let mut debug = f.debug_list();
         self.clone().for_each(|ref e| {
@@ -115,23 +114,13 @@ impl Debug for ElfSectionIter {
     }
 }
 
-impl Default for ElfSectionIter {
-    fn default() -> Self {
-        Self {
-            current_section: core::ptr::null(),
-            remaining_sections: 0,
-            entry_size: 0,
-            string_section: core::ptr::null(),
-        }
-    }
-}
-
 /// A single generic ELF Section.
 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
-pub struct ElfSection {
+pub struct ElfSection<'a> {
     inner: *const u8,
     string_section: *const u8,
     entry_size: u32,
+    _phantom: PhantomData<&'a ()>,
 }
 
 #[derive(Clone, Copy, Debug)]
@@ -164,7 +153,7 @@ struct ElfSectionInner64 {
     entry_size: u64,
 }
 
-impl ElfSection {
+impl<'a> ElfSection<'a> {
     /// Get the section type as a `ElfSectionType` enum variant.
     #[must_use]
     pub fn section_type(&self) -> ElfSectionType {

+ 0 - 4
multiboot2/src/lib.rs

@@ -537,8 +537,6 @@ mod tests {
     /// Tests to parse a MBI that was statically extracted from a test run with
     /// GRUB as bootloader.
     #[test]
-    // TODO fix Miri
-    #[cfg_attr(miri, ignore)]
     fn grub2() {
         let mut bytes = AlignedBytes([
             192, 3, 0, 0, // total_size
@@ -944,8 +942,6 @@ mod tests {
     }
 
     #[test]
-    // TODO fix Miri
-    #[cfg_attr(miri, ignore)]
     fn elf_sections() {
         let mut bytes = AlignedBytes([
             168, 0, 0, 0, // total_size