Skip to content

Commit 78d42b8

Browse files
committed
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!
1 parent 4ee7884 commit 78d42b8

File tree

3 files changed

+16
-31
lines changed

3 files changed

+16
-31
lines changed

multiboot2/src/boot_information.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ impl fmt::Debug for BootInformation<'_> {
440440
if elf_sections_tag_entries_count > ELF_SECTIONS_LIMIT {
441441
debug.field("elf_sections (count)", &elf_sections_tag_entries_count);
442442
} else {
443-
debug.field("elf_sections", &self.elf_sections().unwrap_or_default());
443+
debug.field("elf_sections", &self.elf_sections());
444444
}
445445
}
446446

multiboot2/src/elf_sections.rs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use crate::{TagHeader, TagTrait, TagType};
44
use core::fmt::{Debug, Formatter};
5+
use core::marker::{PhantomData, PhantomPinned};
56
use core::mem;
67
use core::str::Utf8Error;
78
#[cfg(feature = "builder")]
@@ -34,21 +35,18 @@ impl ElfSectionsTag {
3435
}
3536

3637
/// Get an iterator of loaded ELF sections.
37-
pub(crate) const fn sections(&self) -> ElfSectionIter {
38+
pub(crate) fn sections(&self) -> ElfSectionIter {
3839
let string_section_offset = (self.shndx * self.entry_size) as isize;
3940
let string_section_ptr =
40-
unsafe { self.first_section().offset(string_section_offset) as *const _ };
41+
unsafe { self.sections.as_ptr().offset(string_section_offset) as *const _ };
4142
ElfSectionIter {
42-
current_section: self.first_section(),
43+
current_section: self.sections.as_ptr(),
4344
remaining_sections: self.number_of_sections,
4445
entry_size: self.entry_size,
4546
string_section: string_section_ptr,
47+
_phantom_data: PhantomData::default(),
4648
}
4749
}
48-
49-
const fn first_section(&self) -> *const u8 {
50-
&(self.sections[0]) as *const _
51-
}
5250
}
5351

5452
impl TagTrait for ElfSectionsTag {
@@ -75,23 +73,24 @@ impl Debug for ElfSectionsTag {
7573

7674
/// An iterator over some ELF sections.
7775
#[derive(Clone)]
78-
/// TODO make this memory safe with lifetime capture.
79-
pub struct ElfSectionIter {
76+
pub struct ElfSectionIter<'a> {
8077
current_section: *const u8,
8178
remaining_sections: u32,
8279
entry_size: u32,
8380
string_section: *const u8,
81+
_phantom_data: PhantomData<&'a ()>,
8482
}
8583

86-
impl Iterator for ElfSectionIter {
87-
type Item = ElfSection;
84+
impl<'a> Iterator for ElfSectionIter<'a> {
85+
type Item = ElfSection<'a>;
8886

89-
fn next(&mut self) -> Option<ElfSection> {
87+
fn next(&mut self) -> Option<ElfSection<'a>> {
9088
while self.remaining_sections != 0 {
9189
let section = ElfSection {
9290
inner: self.current_section,
9391
string_section: self.string_section,
9492
entry_size: self.entry_size,
93+
_phantom: PhantomData::default(),
9594
};
9695

9796
self.current_section = unsafe { self.current_section.offset(self.entry_size as isize) };
@@ -105,7 +104,7 @@ impl Iterator for ElfSectionIter {
105104
}
106105
}
107106

108-
impl Debug for ElfSectionIter {
107+
impl<'a> Debug for ElfSectionIter<'a> {
109108
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
110109
let mut debug = f.debug_list();
111110
self.clone().for_each(|ref e| {
@@ -115,23 +114,13 @@ impl Debug for ElfSectionIter {
115114
}
116115
}
117116

118-
impl Default for ElfSectionIter {
119-
fn default() -> Self {
120-
Self {
121-
current_section: core::ptr::null(),
122-
remaining_sections: 0,
123-
entry_size: 0,
124-
string_section: core::ptr::null(),
125-
}
126-
}
127-
}
128-
129117
/// A single generic ELF Section.
130118
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
131-
pub struct ElfSection {
119+
pub struct ElfSection<'a> {
132120
inner: *const u8,
133121
string_section: *const u8,
134122
entry_size: u32,
123+
_phantom: PhantomData<&'a ()>,
135124
}
136125

137126
#[derive(Clone, Copy, Debug)]
@@ -164,7 +153,7 @@ struct ElfSectionInner64 {
164153
entry_size: u64,
165154
}
166155

167-
impl ElfSection {
156+
impl<'a> ElfSection<'a> {
168157
/// Get the section type as a `ElfSectionType` enum variant.
169158
#[must_use]
170159
pub fn section_type(&self) -> ElfSectionType {

multiboot2/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,6 @@ mod tests {
537537
/// Tests to parse a MBI that was statically extracted from a test run with
538538
/// GRUB as bootloader.
539539
#[test]
540-
// TODO fix Miri
541-
#[cfg_attr(miri, ignore)]
542540
fn grub2() {
543541
let mut bytes = AlignedBytes([
544542
192, 3, 0, 0, // total_size
@@ -944,8 +942,6 @@ mod tests {
944942
}
945943

946944
#[test]
947-
// TODO fix Miri
948-
#[cfg_attr(miri, ignore)]
949945
fn elf_sections() {
950946
let mut bytes = AlignedBytes([
951947
168, 0, 0, 0, // total_size

0 commit comments

Comments
 (0)