Skip to content

Commit 03313ee

Browse files
committed
uefi: remove support for unstable allocator_api feature
The allocator_api feature [0] is old and not developed in years. Since then, understanding of memory safety and best practises has evolved. It is unlikely that in its current form the functionality will ever be merged. Therefore, we drop the complexity we have from this feature for now, leading to simpler code. [0] rust-lang/rust#32838
1 parent 90c5ba4 commit 03313ee

File tree

6 files changed

+3
-101
lines changed

6 files changed

+3
-101
lines changed

uefi/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,8 @@
142142
//! - `log-debugcon`: Whether the logger set up by `logger` should also log
143143
//! to the debugcon device (available in QEMU or Cloud Hypervisor on x86).
144144
//! - `panic_handler`: Add a default panic handler that logs to `stdout`.
145-
//! - `unstable`: Enable functionality that depends on [unstable
146-
//! features] in the nightly compiler.
147-
//! As example, in conjunction with the `alloc`-feature, this gate allows
148-
//! the `allocator_api` on certain functions.
145+
//! - `unstable`: Enable functionality that depends on [unstable features] in
146+
//! the Rust compiler (nightly version).
149147
//! - `qemu`: Enable some code paths to adapt their execution when executed
150148
//! in QEMU, such as using the special `qemu-exit` device when the panic
151149
//! handler is called.
@@ -229,7 +227,6 @@
229227
//! [uefi-std-tr-issue]: https://github.com/rust-lang/rust/issues/100499
230228
//! [unstable features]: https://doc.rust-lang.org/unstable-book/
231229
232-
#![cfg_attr(all(feature = "unstable", feature = "alloc"), feature(allocator_api))]
233230
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
234231
#![no_std]
235232
#![deny(

uefi/src/mem/util.rs

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,14 @@ use {core::alloc::Allocator, core::ptr::NonNull};
2323
/// buffer size is sufficient, and
2424
/// - return a mutable typed reference that points to the same memory as the input buffer on
2525
/// success.
26-
///
27-
/// # Feature `unstable` / `allocator_api`
28-
/// By default, this function works with the allocator that is set as
29-
/// `#[global_allocator]`. This might be UEFI allocator but depends on your
30-
/// use case and how you set up the environment.
31-
///
32-
/// If you activate the `unstable`-feature, all allocations uses the provided
33-
/// allocator (via `allocator_api`) instead. In that case, the function takes an
34-
/// additional parameter describing the specific [`Allocator`]. You can use
35-
/// [`alloc::alloc::Global`] which defaults to the `#[global_allocator]`.
36-
///
37-
/// [`Allocator`]: https://doc.rust-lang.org/alloc/alloc/trait.Allocator.html
38-
/// [`alloc::alloc::Global`]: https://doc.rust-lang.org/alloc/alloc/struct.Global.html
3926
pub(crate) fn make_boxed<
4027
'a,
4128
// The UEFI data structure.
4229
Data: Align + ?Sized + Debug + 'a,
4330
F: FnMut(&'a mut [u8]) -> Result<&'a mut Data, Option<usize>>,
44-
#[cfg(feature = "unstable")] A: Allocator,
4531
>(
4632
// A function to read the UEFI data structure into a provided buffer.
4733
mut fetch_data_fn: F,
48-
#[cfg(feature = "unstable")]
49-
// Allocator of the `allocator_api` feature. You can use `Global` as default.
50-
allocator: A,
5134
) -> Result<Box<Data>> {
5235
let required_size = match fetch_data_fn(&mut []).map_err(Error::split) {
5336
// This is the expected case: the empty buffer passed in is too
@@ -70,21 +53,13 @@ pub(crate) fn make_boxed<
7053

7154
// Allocate the buffer on the heap.
7255
let heap_buf: *mut u8 = {
73-
#[cfg(not(feature = "unstable"))]
7456
{
7557
let ptr = unsafe { alloc(layout) };
7658
if ptr.is_null() {
7759
return Err(Status::OUT_OF_RESOURCES.into());
7860
}
7961
ptr
8062
}
81-
82-
#[cfg(feature = "unstable")]
83-
allocator
84-
.allocate(layout)
85-
.map_err(|_| <Status as Into<Error>>::into(Status::OUT_OF_RESOURCES))?
86-
.as_ptr()
87-
.cast::<u8>()
8863
};
8964

9065
// Read the data into the provided buffer.
@@ -97,20 +72,12 @@ pub(crate) fn make_boxed<
9772
let data: &mut Data = match data {
9873
Ok(data) => data,
9974
Err(err) => {
100-
#[cfg(not(feature = "unstable"))]
101-
unsafe {
102-
dealloc(heap_buf, layout)
103-
};
104-
#[cfg(feature = "unstable")]
105-
unsafe {
106-
allocator.deallocate(NonNull::new(heap_buf).unwrap(), layout)
107-
}
75+
unsafe { dealloc(heap_buf, layout) };
10876
return Err(err);
10977
}
11078
};
11179

11280
let data = unsafe { Box::from_raw(data) };
113-
11481
Ok(data)
11582
}
11683

uefi/src/proto/hii/database.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,8 @@ impl HiiDatabase {
4444
}
4545
}
4646

47-
#[cfg(not(feature = "unstable"))]
4847
let buf = make_boxed::<[u8], _>(|buf| fetch_data_fn(self, buf))?;
4948

50-
#[cfg(feature = "unstable")]
51-
let buf = make_boxed::<[u8], _, _>(|buf| fetch_data_fn(self, buf), alloc::alloc::Global)?;
52-
5349
Ok(buf)
5450
}
5551
}

uefi/src/proto/media/file/dir.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -80,42 +80,7 @@ impl Directory {
8080
maybe_info.expect("Should have more entries")
8181
})
8282
};
83-
84-
#[cfg(not(feature = "unstable"))]
8583
let file_info = make_boxed::<FileInfo, _>(fetch_data_fn)?;
86-
87-
#[cfg(feature = "unstable")]
88-
let file_info = make_boxed::<FileInfo, _, _>(fetch_data_fn, Global)?;
89-
90-
Ok(Some(file_info))
91-
}
92-
93-
/// Wrapper around [`Self::read_entry`] that returns an owned copy of the data. It has the same
94-
/// implications and requirements. On failure, the payload of `Err` is `()´.
95-
///
96-
/// It allows to use a custom allocator via the `allocator_api` feature.
97-
#[cfg(all(feature = "unstable", feature = "alloc"))]
98-
pub fn read_entry_boxed_in<A: Allocator>(
99-
&mut self,
100-
allocator: A,
101-
) -> Result<Option<Box<FileInfo>>> {
102-
let read_entry_res = self.read_entry(&mut []);
103-
104-
// If no more entries are available, return early.
105-
if read_entry_res == Ok(None) {
106-
return Ok(None);
107-
}
108-
109-
let fetch_data_fn = |buf| {
110-
self.read_entry(buf)
111-
// this is safe, as above, we checked that there are more entries
112-
.map(|maybe_info: Option<&mut FileInfo>| {
113-
maybe_info.expect("Should have more entries")
114-
})
115-
};
116-
117-
let file_info = make_boxed::<FileInfo, _, A>(fetch_data_fn, allocator)?;
118-
11984
Ok(Some(file_info))
12085
}
12186

uefi/src/proto/media/file/mod.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,7 @@ pub trait File: Sized {
198198
#[cfg(feature = "alloc")]
199199
fn get_boxed_info<Info: FileProtocolInfo + ?Sized + Debug>(&mut self) -> Result<Box<Info>> {
200200
let fetch_data_fn = |buf| self.get_info::<Info>(buf);
201-
#[cfg(not(feature = "unstable"))]
202201
let file_info = make_boxed::<Info, _>(fetch_data_fn)?;
203-
#[cfg(feature = "unstable")]
204-
let file_info = make_boxed::<Info, _, _>(fetch_data_fn, Global)?;
205-
Ok(file_info)
206-
}
207-
208-
/// Read the dynamically allocated info for a file.
209-
#[cfg(all(feature = "unstable", feature = "alloc"))]
210-
fn get_boxed_info_in<Info: FileProtocolInfo + ?Sized + Debug, A: Allocator>(
211-
&mut self,
212-
allocator: A,
213-
) -> Result<Box<Info>> {
214-
let fetch_data_fn = |buf| self.get_info::<Info>(buf);
215-
let file_info = make_boxed::<Info, _, A>(fetch_data_fn, allocator)?;
216202
Ok(file_info)
217203
}
218204

uefi/src/proto/media/load_file.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,7 @@ impl LoadFile {
9090
status.to_result_with_err(|_| Some(size)).map(|_| buf)
9191
};
9292

93-
#[cfg(not(feature = "unstable"))]
9493
let file: Box<[u8]> = make_boxed::<[u8], _>(fetch_data_fn)?;
95-
96-
#[cfg(feature = "unstable")]
97-
let file = make_boxed::<[u8], _, _>(fetch_data_fn, Global)?;
98-
9994
Ok(file)
10095
}
10196
}
@@ -158,12 +153,8 @@ impl LoadFile2 {
158153
status.to_result_with_err(|_| Some(size)).map(|_| buf)
159154
};
160155

161-
#[cfg(not(feature = "unstable"))]
162156
let file: Box<[u8]> = make_boxed::<[u8], _>(fetch_data_fn)?;
163157

164-
#[cfg(feature = "unstable")]
165-
let file = make_boxed::<[u8], _, _>(fetch_data_fn, Global)?;
166-
167158
Ok(file)
168159
}
169160
}

0 commit comments

Comments
 (0)