Skip to content

Commit d181830

Browse files
uefi: Return null from allocator if boot services are not active
1 parent 06b112e commit d181830

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

uefi/src/allocator.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ unsafe impl GlobalAlloc for Allocator {
6666
/// of type [`MemoryType::LOADER_DATA`] for UEFI applications, [`MemoryType::BOOT_SERVICES_DATA`]
6767
/// for UEFI boot drivers and [`MemoryType::RUNTIME_SERVICES_DATA`] for UEFI runtime drivers.
6868
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
69+
if !boot::are_boot_services_active() {
70+
return ptr::null_mut();
71+
}
72+
6973
let size = layout.size();
7074
let align = layout.align();
7175
let memory_type = MemoryType(MEMORY_TYPE.load(Ordering::Acquire));

uefi/src/boot.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ pub unsafe fn set_image_handle(image_handle: Handle) {
6060
IMAGE_HANDLE.store(image_handle.as_ptr(), Ordering::Release);
6161
}
6262

63+
/// Return true if boot services are active, false otherwise.
64+
pub(crate) fn are_boot_services_active() -> bool {
65+
let Some(st) = table::system_table_raw() else {
66+
return false;
67+
};
68+
69+
// SAFETY: valid per requirements of `set_system_table`.
70+
let st = unsafe { st.as_ref() };
71+
72+
!st.boot_services.is_null()
73+
}
74+
6375
fn boot_services_raw_panicking() -> NonNull<uefi_raw::table::boot::BootServices> {
6476
let st = table::system_table_raw_panicking();
6577
// SAFETY: valid per requirements of `set_system_table`.

0 commit comments

Comments
 (0)