Skip to content

multiboot2: add test to ensure that boot info is send and sync #169

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 2 commits into from
Jul 14, 2023
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
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions integration-test/bins/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 24 additions & 4 deletions multiboot2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,6 @@ impl<'a> BootInformation<'a> {
}
}

// SAFETY: BootInformation contains a const ptr to memory that is never mutated.
// Sending this pointer to other threads is sound.
unsafe impl Send for BootInformation<'_> {}

impl fmt::Debug for BootInformation<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
/// Limit how many Elf-Sections should be debug-formatted.
Expand Down Expand Up @@ -528,6 +524,30 @@ mod tests {
use crate::memory_map::MemoryAreaType;
use core::str::Utf8Error;

/// Compile time test to check if the boot information is Send and Sync.
/// This test is relevant to give library users flexebility in passing the
/// struct around.
#[test]
fn boot_information_is_send_and_sync() {
fn accept<T: Send + Sync>(_: T) {}

#[repr(C, align(8))]
struct Bytes([u8; 16]);
let bytes: Bytes = Bytes([
16, 0, 0, 0, // total_size
0, 0, 0, 0, // reserved
0, 0, 0, 0, // end tag type
8, 0, 0, 0, // end tag size
]);
let ptr = bytes.0.as_ptr();
let addr = ptr as usize;
let bi = unsafe { BootInformation::load(ptr.cast()) };
let bi = bi.unwrap();

// compile time test
accept(bi);
}

#[test]
fn no_tags() {
#[repr(C, align(8))]
Expand Down