Skip to content

uefi: BootServices::allocate_pool now returns NonZero<u8> instead of *mut u8 #1176

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 1 commit into from
May 27, 2024
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
6 changes: 4 additions & 2 deletions uefi-test-runner/src/boot/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ fn test_install_protocol_interface(bt: &BootServices) {
mem::size_of::<TestProtocol>(),
)
.unwrap()
.cast();
.cast()
.as_ptr();
unsafe { alloc.write(TestProtocol { data: 123 }) };

let _ = unsafe {
Expand Down Expand Up @@ -187,7 +188,8 @@ fn test_install_configuration_table(st: &SystemTable<Boot>) {
let config = st
.boot_services()
.allocate_pool(MemoryType::ACPI_RECLAIM, 1)
.expect("Failed to allocate config table");
.expect("Failed to allocate config table")
.as_ptr();
unsafe { config.write(42) };

let count = st.config_table().len();
Expand Down
2 changes: 2 additions & 0 deletions uefi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
## Changed
- `SystemTable::exit_boot_services` is now `unsafe`. See that method's
documentation for details of obligations for callers.
- `BootServices::allocate_pool` now returns `NonZero<u8>` instead of
`*mut u8`.

## Removed
- Removed the `panic-on-logger-errors` feature of the `uefi` crate. Logger
Expand Down
3 changes: 2 additions & 1 deletion uefi/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ unsafe impl GlobalAlloc for Allocator {
// within the allocation.
let full_alloc_ptr =
if let Ok(ptr) = boot_services.allocate_pool(memory_type, size + align) {
ptr
ptr.as_ptr()
} else {
return ptr::null_mut();
};
Expand Down Expand Up @@ -116,6 +116,7 @@ unsafe impl GlobalAlloc for Allocator {
// use `allocate_pool` directly.
boot_services
.allocate_pool(memory_type, size)
.map(|ptr| ptr.as_ptr())
.unwrap_or(ptr::null_mut())
}
}
Expand Down
8 changes: 6 additions & 2 deletions uefi/src/table/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,13 @@ impl BootServices {
///
/// * [`uefi::Status::OUT_OF_RESOURCES`]
/// * [`uefi::Status::INVALID_PARAMETER`]
pub fn allocate_pool(&self, mem_ty: MemoryType, size: usize) -> Result<*mut u8> {
pub fn allocate_pool(&self, mem_ty: MemoryType, size: usize) -> Result<NonNull<u8>> {
let mut buffer = ptr::null_mut();
unsafe { (self.0.allocate_pool)(mem_ty, size, &mut buffer) }.to_result_with_val(|| buffer)
let ptr = unsafe { (self.0.allocate_pool)(mem_ty, size, &mut buffer) }
.to_result_with_val(|| buffer)?;

Ok(NonNull::new(ptr)
.expect("UEFI should return error if an allocation failed but never a null pointer"))
}

/// Frees memory allocated from a pool.
Expand Down
2 changes: 1 addition & 1 deletion uefi/src/table/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl SystemTable<Boot> {
// Allocate a byte slice to hold the memory map. If the
// allocation fails treat it as an unrecoverable error.
let buf: *mut u8 = match boot_services.allocate_pool(memory_type, buf_size) {
Ok(buf) => buf,
Ok(buf) => buf.as_ptr(),
Err(err) => reset(err.status()),
};

Expand Down