Skip to content

Commit 565b1aa

Browse files
authored
Merge pull request #1763 from rust-osdev/allocator
uefi: remove support for unstable allocator_api feature
2 parents 82bf2b0 + ea7f4ce commit 565b1aa

File tree

11 files changed

+17
-170
lines changed

11 files changed

+17
-170
lines changed

uefi/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
- The documentation for UEFI protocols has been streamlined and improved.
2626
- Fixed memory safety bug in `SimpleNetwork::read_nv_data`. The `buffer`
2727
parameter is now mutable.
28+
- Removed all internal usages including public APIs using the unstable
29+
`allocator_api` feature. It may be reintroduced if it will have a chance of
30+
getting stabilized in stable Rust.
31+
- Removed `File::get_boxed_info_in`
32+
- Removed `Directory::read_entry_boxed_in`
2833

2934
# uefi - 0.35.0 (2025-05-04)
3035

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: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
55
use crate::data_types::Align;
66
use crate::{Error, Result, ResultExt, Status};
7+
use ::alloc::alloc::{alloc, dealloc};
78
use ::alloc::boxed::Box;
89
use core::alloc::Layout;
910
use core::fmt::Debug;
1011
use core::slice;
1112

12-
#[cfg(not(feature = "unstable"))]
13-
use ::alloc::alloc::{alloc, dealloc};
14-
15-
#[cfg(feature = "unstable")]
16-
use {core::alloc::Allocator, core::ptr::NonNull};
17-
1813
/// Helper to return owned versions of certain UEFI data structures on the heap in a [`Box`]. This
1914
/// function is intended to wrap low-level UEFI functions of this crate that
2015
/// - can consume an empty buffer without a panic to get the required buffer size in the errors
@@ -23,31 +18,14 @@ use {core::alloc::Allocator, core::ptr::NonNull};
2318
/// buffer size is sufficient, and
2419
/// - return a mutable typed reference that points to the same memory as the input buffer on
2520
/// 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
3921
pub(crate) fn make_boxed<
4022
'a,
4123
// The UEFI data structure.
4224
Data: Align + ?Sized + Debug + 'a,
4325
F: FnMut(&'a mut [u8]) -> Result<&'a mut Data, Option<usize>>,
44-
#[cfg(feature = "unstable")] A: Allocator,
4526
>(
4627
// A function to read the UEFI data structure into a provided buffer.
4728
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,
5129
) -> Result<Box<Data>> {
5230
let required_size = match fetch_data_fn(&mut []).map_err(Error::split) {
5331
// This is the expected case: the empty buffer passed in is too
@@ -70,21 +48,13 @@ pub(crate) fn make_boxed<
7048

7149
// Allocate the buffer on the heap.
7250
let heap_buf: *mut u8 = {
73-
#[cfg(not(feature = "unstable"))]
7451
{
7552
let ptr = unsafe { alloc(layout) };
7653
if ptr.is_null() {
7754
return Err(Status::OUT_OF_RESOURCES.into());
7855
}
7956
ptr
8057
}
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>()
8858
};
8959

9060
// Read the data into the provided buffer.
@@ -97,29 +67,19 @@ pub(crate) fn make_boxed<
9767
let data: &mut Data = match data {
9868
Ok(data) => data,
9969
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-
}
70+
unsafe { dealloc(heap_buf, layout) };
10871
return Err(err);
10972
}
11073
};
11174

11275
let data = unsafe { Box::from_raw(data) };
113-
11476
Ok(data)
11577
}
11678

11779
#[cfg(test)]
11880
mod tests {
11981
use super::*;
12082
use crate::{ResultExt, StatusExt};
121-
#[cfg(feature = "unstable")]
122-
use alloc::alloc::Global;
12383

12484
/// Some simple dummy type to test [`make_boxed`].
12585
#[derive(Debug)]
@@ -212,27 +172,20 @@ mod tests {
212172
assert_eq!(&data.0.0, &[1, 2, 3, 4]);
213173
}
214174

215-
/// This unit tests checks the [`make_boxed`] utility. The test has different code and behavior
216-
/// depending on whether the "unstable" feature is active or not.
175+
/// This unit tests checks the [`make_boxed`] utility.
176+
///
177+
/// This test is especially useful when run by miri.
217178
#[test]
218179
fn test_make_boxed_utility() {
219180
let fetch_data_fn = |buf| uefi_function_stub_read(buf);
220181

221-
#[cfg(not(feature = "unstable"))]
222182
let data: Box<SomeData> = make_boxed(fetch_data_fn).unwrap();
223-
224-
#[cfg(feature = "unstable")]
225-
let data: Box<SomeData> = make_boxed(fetch_data_fn, Global).unwrap();
226183
assert_eq!(&data.0, &[1, 2, 3, 4]);
227184

228185
let fetch_data_fn = |buf| uefi_function_stub_read(buf);
229186

230-
#[cfg(not(feature = "unstable"))]
231187
let data: Box<SomeDataAlign16> = make_boxed(fetch_data_fn).unwrap();
232188

233-
#[cfg(feature = "unstable")]
234-
let data: Box<SomeDataAlign16> = make_boxed(fetch_data_fn, Global).unwrap();
235-
236189
assert_eq!(&data.0.0, &[1, 2, 3, 4]);
237190
}
238191
}

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 & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use crate::data_types::Align;
66
use core::ffi::c_void;
77
#[cfg(feature = "alloc")]
88
use {crate::mem::make_boxed, alloc::boxed::Box};
9-
#[cfg(all(feature = "unstable", feature = "alloc"))]
10-
use {alloc::alloc::Global, core::alloc::Allocator};
119

1210
/// A `FileHandle` that is also a directory.
1311
///
@@ -80,42 +78,7 @@ impl Directory {
8078
maybe_info.expect("Should have more entries")
8179
})
8280
};
83-
84-
#[cfg(not(feature = "unstable"))]
8581
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-
11982
Ok(Some(file_info))
12083
}
12184

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ use core::fmt::Debug;
2020
use core::{mem, ptr};
2121
use uefi_raw::protocol::file_system::FileProtocolV1;
2222

23-
#[cfg(all(feature = "unstable", feature = "alloc"))]
24-
use {alloc::alloc::Global, core::alloc::Allocator};
25-
2623
#[cfg(feature = "alloc")]
2724
use {crate::mem::make_boxed, alloc::boxed::Box};
2825

@@ -198,21 +195,7 @@ pub trait File: Sized {
198195
#[cfg(feature = "alloc")]
199196
fn get_boxed_info<Info: FileProtocolInfo + ?Sized + Debug>(&mut self) -> Result<Box<Info>> {
200197
let fetch_data_fn = |buf| self.get_info::<Info>(buf);
201-
#[cfg(not(feature = "unstable"))]
202198
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)?;
216199
Ok(file_info)
217200
}
218201

uefi/src/proto/media/load_file.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#[cfg(doc)]
66
use crate::Status;
77
use crate::proto::unsafe_protocol;
8-
#[cfg(all(feature = "alloc", feature = "unstable"))]
9-
use alloc::alloc::Global;
108
use uefi_raw::protocol::media::{LoadFile2Protocol, LoadFileProtocol};
119
#[cfg(feature = "alloc")]
1210
use {
@@ -90,12 +88,7 @@ impl LoadFile {
9088
status.to_result_with_err(|_| Some(size)).map(|_| buf)
9189
};
9290

93-
#[cfg(not(feature = "unstable"))]
9491
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-
9992
Ok(file)
10093
}
10194
}
@@ -158,12 +151,8 @@ impl LoadFile2 {
158151
status.to_result_with_err(|_| Some(size)).map(|_| buf)
159152
};
160153

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

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

uefi/src/proto/tcg/v1.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ use uefi_raw::protocol::tcg::v1::{TcgBootServiceCapability, TcgProtocol};
2424
#[cfg(feature = "alloc")]
2525
use {crate::mem::make_boxed, alloc::boxed::Box};
2626

27-
#[cfg(all(feature = "unstable", feature = "alloc"))]
28-
use alloc::alloc::Global;
29-
3027
pub use uefi_raw::protocol::tcg::v1::TcgVersion as Version;
3128

3229
/// 20-byte SHA-1 digest.
@@ -157,17 +154,7 @@ impl PcrEvent {
157154
digest: Sha1Digest,
158155
event_data: &[u8],
159156
) -> Result<Box<Self>> {
160-
#[cfg(not(feature = "unstable"))]
161-
{
162-
make_boxed(|buf| Self::new_in_buffer(buf, pcr_index, event_type, digest, event_data))
163-
}
164-
#[cfg(feature = "unstable")]
165-
{
166-
make_boxed(
167-
|buf| Self::new_in_buffer(buf, pcr_index, event_type, digest, event_data),
168-
Global,
169-
)
170-
}
157+
make_boxed(|buf| Self::new_in_buffer(buf, pcr_index, event_type, digest, event_data))
171158
}
172159

173160
/// PCR index for the event.

uefi/src/proto/tcg/v2.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ use uefi_raw::protocol::tcg::v2::{Tcg2EventHeader as EventHeader, Tcg2Protocol};
2626
#[cfg(feature = "alloc")]
2727
use {crate::mem::make_boxed, alloc::boxed::Box};
2828

29-
#[cfg(all(feature = "unstable", feature = "alloc"))]
30-
use alloc::alloc::Global;
31-
3229
pub use uefi_raw::protocol::tcg::v2::{
3330
Tcg2EventLogFormat as EventLogFormat, Tcg2HashAlgorithmBitmap,
3431
Tcg2HashLogExtendEventFlags as HashLogExtendEventFlags, Tcg2Version as Version,
@@ -183,17 +180,7 @@ impl PcrEventInputs {
183180
event_type: EventType,
184181
event_data: &[u8],
185182
) -> Result<Box<Self>> {
186-
#[cfg(not(feature = "unstable"))]
187-
{
188-
make_boxed(|buf| Self::new_in_buffer(buf, pcr_index, event_type, event_data))
189-
}
190-
#[cfg(feature = "unstable")]
191-
{
192-
make_boxed(
193-
|buf| Self::new_in_buffer(buf, pcr_index, event_type, event_data),
194-
Global,
195-
)
196-
}
183+
make_boxed(|buf| Self::new_in_buffer(buf, pcr_index, event_type, event_data))
197184
}
198185
}
199186

uefi/src/runtime.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ use {
2424
alloc::{vec, vec::Vec},
2525
};
2626

27-
#[cfg(all(feature = "unstable", feature = "alloc"))]
28-
use alloc::alloc::Global;
29-
3027
pub use uefi_raw::capsule::{CapsuleBlockDescriptor, CapsuleFlags, CapsuleHeader};
3128
pub use uefi_raw::table::runtime::{
3229
ResetType, TimeCapabilities, VariableAttributes, VariableVendor,
@@ -187,14 +184,7 @@ pub fn get_variable_boxed(
187184
val
188185
})
189186
};
190-
#[cfg(not(feature = "unstable"))]
191-
{
192-
make_boxed(get_var).map(|val| (val, out_attr))
193-
}
194-
#[cfg(feature = "unstable")]
195-
{
196-
make_boxed(get_var, Global).map(|val| (val, out_attr))
197-
}
187+
make_boxed(get_var).map(|val| (val, out_attr))
198188
}
199189

200190
/// Gets each variable key (name and vendor) one at a time.

0 commit comments

Comments
 (0)