Skip to content

Commit d6c7c1d

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 d6c7c1d

File tree

10 files changed

+11
-151
lines changed

10 files changed

+11
-151
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: 4 additions & 44 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

@@ -212,27 +179,20 @@ mod tests {
212179
assert_eq!(&data.0.0, &[1, 2, 3, 4]);
213180
}
214181

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.
182+
/// This unit tests checks the [`make_boxed`] utility.
183+
///
184+
/// This test is especially useful when run by miri.
217185
#[test]
218186
fn test_make_boxed_utility() {
219187
let fetch_data_fn = |buf| uefi_function_stub_read(buf);
220188

221-
#[cfg(not(feature = "unstable"))]
222189
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();
226190
assert_eq!(&data.0, &[1, 2, 3, 4]);
227191

228192
let fetch_data_fn = |buf| uefi_function_stub_read(buf);
229193

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

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

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 & 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
}

uefi/src/proto/tcg/v1.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,7 @@ impl PcrEvent {
157157
digest: Sha1Digest,
158158
event_data: &[u8],
159159
) -> 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-
}
160+
make_boxed(|buf| Self::new_in_buffer(buf, pcr_index, event_type, digest, event_data))
171161
}
172162

173163
/// PCR index for the event.

uefi/src/proto/tcg/v2.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,7 @@ impl PcrEventInputs {
183183
event_type: EventType,
184184
event_data: &[u8],
185185
) -> 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-
}
186+
make_boxed(|buf| Self::new_in_buffer(buf, pcr_index, event_type, event_data))
197187
}
198188
}
199189

uefi/src/runtime.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,7 @@ pub fn get_variable_boxed(
187187
val
188188
})
189189
};
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-
}
190+
make_boxed(get_var).map(|val| (val, out_attr))
198191
}
199192

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

xtask/src/main.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,10 @@ fn run_host_tests(test_opt: &TestOpt) -> Result<()> {
214214
packages.push(Package::UefiMacros);
215215
}
216216

217-
// Run uefi-rs and uefi-macros tests.
217+
// Run uefi-rs and uefi-macros tests with `unstable` feature.
218218
let cargo = Cargo {
219219
action: CargoAction::Test,
220-
// At least one unit test, for make_boxed() currently, has different behaviour dependent on
221-
// the unstable feature. Because of this, we need to allow to test both variants. Runtime
222-
// features is set to no as it is not possible as as soon a #[global_allocator] is
223-
// registered, the Rust runtime executing the tests uses it as well.
220+
// Some tests may behave differently depending on the unstable feature.
224221
features: Feature::more_code(*test_opt.unstable, false),
225222
packages,
226223
release: false,

0 commit comments

Comments
 (0)