Skip to content

Properly handle allocation failure #90

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
Feb 6, 2019
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
1 change: 0 additions & 1 deletion uefi-exts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ edition = "2018"

[dependencies]
uefi = { path = ".." }
uefi-services = { path = "../uefi-services" }
14 changes: 7 additions & 7 deletions uefi-exts/src/file.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloc::{alloc::alloc, boxed::Box};
use core::{alloc::Layout, mem, slice};
use super::allocate_buffer;
use alloc::{alloc::Layout, boxed::Box};
use core::mem;
use uefi::prelude::*;
use uefi::proto::media::file::{File, FileProtocolInfo};
use uefi::Result;
Expand All @@ -19,15 +20,14 @@ pub trait FileExt: File {
(_, Some(size)) => size,
};

// These unsafe alloc APIs make sure our buffer is correctly aligned. We
// round up a size must always be a multiple of alignment. We turn the
// pointer into a Box<[u8]>, so it's always freed on error.
// We add trailing padding because the size of a rust structure must
// always be a multiple of alignment.
let layout = Layout::from_size_align(size, Info::alignment())
.unwrap()
.pad_to_align()
.unwrap();
let buffer_start = unsafe { alloc(layout) };
let mut buffer = unsafe { Box::from_raw(slice::from_raw_parts_mut(buffer_start, size)) };
let mut buffer = allocate_buffer(layout);
let buffer_start = buffer.as_ptr();

let info = self
.get_info(&mut buffer)
Expand Down
23 changes: 22 additions & 1 deletion uefi-exts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! which add utility functions to various UEFI objects.

#![no_std]
#![feature(alloc, alloc_layout_extra)]
#![feature(alloc, alloc_layout_extra, allocator_api)]

extern crate alloc;

Expand All @@ -13,3 +13,24 @@ mod file;

pub use self::boot::BootServicesExt;
pub use self::file::FileExt;

use alloc::{
alloc::{handle_alloc_error, Alloc, Global, Layout},
boxed::Box,
};
use core::slice;

/// Creates a boxed byte buffer using the standard allocator
/// # Panics
/// Calls handle_alloc_error if the layout has a size of zero or allocation fails.
pub fn allocate_buffer(layout: Layout) -> Box<[u8]> {
if layout.size() == 0 {
handle_alloc_error(layout);
}
unsafe {
match Global.alloc(layout) {
Ok(mem) => Box::from_raw(slice::from_raw_parts_mut(mem.as_ptr(), layout.size())),
Err(_) => handle_alloc_error(layout),
}
}
}