Skip to content

Commit a46a644

Browse files
committed
build: Remove heap allocator
PR #92 introduced heap allocator to implement EFI variable feature. However, none of other components uses heap. This commit removes the heap allocator and use the `heapless` crate instead of `Vec`. This change simplifies the build arguments. Signed-off-by: Akira Moroo <[email protected]>
1 parent 6018ec4 commit a46a644

File tree

12 files changed

+73
-84
lines changed

12 files changed

+73
-84
lines changed

.github/workflows/build.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ jobs:
2828
- name: Install Rust components
2929
run: rustup component add rust-src clippy rustfmt
3030
- name: Build (debug)
31-
run: cargo build --target ${{ matrix.target }} -Zbuild-std=core,alloc -Zbuild-std-features=compiler-builtins-mem
31+
run: cargo build --target ${{ matrix.target }} -Zbuild-std=core -Zbuild-std-features=compiler-builtins-mem
3232
- name: Build (release)
33-
run: cargo build --release --target ${{ matrix.target }} -Zbuild-std=core,alloc -Zbuild-std-features=compiler-builtins-mem
33+
run: cargo build --release --target ${{ matrix.target }} -Zbuild-std=core -Zbuild-std-features=compiler-builtins-mem
3434
- name: Clippy (default)
35-
run: cargo clippy --target ${{ matrix.target }} -Zbuild-std=core,alloc
35+
run: cargo clippy --target ${{ matrix.target }} -Zbuild-std=core
3636
- name: Clippy (all targets, all features)
3737
run: cargo clippy --all-targets --all-features
3838
- name: Formatting

.github/workflows/release.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ jobs:
1919
- name: Install rust-src
2020
run: rustup component add rust-src
2121
- name: Build (release) for x86_64
22-
run: cargo build --release --target x86_64-unknown-none.json -Zbuild-std=core,alloc -Zbuild-std-features=compiler-builtins-mem
22+
run: cargo build --release --target x86_64-unknown-none.json -Zbuild-std=core -Zbuild-std-features=compiler-builtins-mem
2323
- name: Build (release) for aarch64
24-
run: cargo build --release --target aarch64-unknown-none.json -Zbuild-std=core,alloc -Zbuild-std-features=compiler-builtins-mem
24+
run: cargo build --release --target aarch64-unknown-none.json -Zbuild-std=core -Zbuild-std-features=compiler-builtins-mem
2525
- name: Create release
2626
id: create_release
2727
uses: actions/create-release@v1

Cargo.lock

Lines changed: 32 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ efi-var = []
2929
bitflags = "2.5.0"
3030
atomic_refcell = "0.1.13"
3131
r-efi = { version = "4.4.0", features = ["efiapi"] }
32-
linked_list_allocator = "0.10.4"
32+
heapless = "0.8.0"
3333

3434
[target.'cfg(target_arch = "aarch64")'.dependencies]
3535
tock-registers = "0.9.0"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ all the way into the OS.
4141
To compile:
4242

4343
```
44-
cargo build --release --target x86_64-unknown-none.json -Zbuild-std=core,alloc -Zbuild-std-features=compiler-builtins-mem
44+
cargo build --release --target x86_64-unknown-none.json -Zbuild-std=core -Zbuild-std-features=compiler-builtins-mem
4545
```
4646

4747
The result will be in:
@@ -101,7 +101,7 @@ $ qemu-system-x86_64 -machine q35,accel=kvm -cpu host,-vmx -m 1G\
101101
To compile:
102102

103103
```
104-
cargo build --release --target aarch64-unknown-none.json -Zbuild-std=core,alloc -Zbuild-std-features=compiler-builtins-mem
104+
cargo build --release --target aarch64-unknown-none.json -Zbuild-std=core -Zbuild-std-features=compiler-builtins-mem
105105
```
106106

107107
The result will be in:
@@ -121,7 +121,7 @@ will become available in the future.
121121
To compile:
122122

123123
```
124-
cargo build --release --target riscv64gcv-unknown-none-elf.json -Zbuild-std=core,alloc -Zbuild-std-features=compiler-builtins-mem
124+
cargo build --release --target riscv64gcv-unknown-none-elf.json -Zbuild-std=core -Zbuild-std-features=compiler-builtins-mem
125125
```
126126

127127
The result will be in:

scripts/dev_cli.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ cmd_build() {
274274
--env RUSTFLAGS="$rustflags" \
275275
"$CTR_IMAGE" \
276276
cargo build --target "$target" \
277-
-Zbuild-std=core,alloc \
277+
-Zbuild-std=core \
278278
-Zbuild-std-features=compiler-builtins-mem \
279279
--target-dir "$CTR_RHF_CARGO_TARGET" \
280280
"${cargo_args[@]}" && say "Binary placed under $RHF_CARGO_TARGET/target/$build"

scripts/run_cargo_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export RUSTFLAGS="-D warnings"
99
arch="$(uname -m)"
1010

1111
do_cargo_tests() {
12-
local cargo_args=("-Zbuild-std=core,alloc" "-Zbuild-std-features=compiler-builtins-mem")
12+
local cargo_args=("-Zbuild-std=core" "-Zbuild-std-features=compiler-builtins-mem")
1313
local cmd="$1"
1414
local target="$2"
1515
local features="$3"

scripts/run_coreboot_integration_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fetch_disk_images "$WORKLOADS_DIR" "$arch"
1616
[ "$arch" = "x86_64" ] && target="x86_64-unknown-none"
1717

1818
rustup component add rust-src
19-
cargo build --release --target "$target.json" -Zbuild-std=core,alloc -Zbuild-std-features=compiler-builtins-mem --features "coreboot"
19+
cargo build --release --target "$target.json" -Zbuild-std=core -Zbuild-std-features=compiler-builtins-mem --features "coreboot"
2020

2121
RHF_BIN="$RHF_ROOT_DIR/target/$target/release/hypervisor-fw"
2222
COREBOOT_CONFIG_IN="$RHF_ROOT_DIR/resources/coreboot/qemu-q35-config.in"

scripts/run_integration_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fetch_disk_images "$WORKLOADS_DIR" "$arch"
1818
[ "$arch" = "x86_64" ] && target="x86_64-unknown-none"
1919

2020
rustup component add rust-src
21-
cargo build --release --target "$target.json" -Zbuild-std=core,alloc -Zbuild-std-features=compiler-builtins-mem
21+
cargo build --release --target "$target.json" -Zbuild-std=core -Zbuild-std-features=compiler-builtins-mem
2222

2323
export RUST_BACKTRACE=1
2424
time cargo test --features "integration_tests" "integration::tests::linux::$arch" -- --test-threads=1

scripts/run_integration_tests_windows.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dmsetup mknodes
3131
[ "$arch" = "x86_64" ] && target="x86_64-unknown-none"
3232

3333
rustup component add rust-src
34-
cargo build --release --target "$target.json" -Zbuild-std=core,alloc -Zbuild-std-features=compiler-builtins-mem
34+
cargo build --release --target "$target.json" -Zbuild-std=core -Zbuild-std-features=compiler-builtins-mem
3535

3636
export RUST_BACKTRACE=1
3737
time cargo test --features "integration_tests" "integration::tests::windows::$arch"

src/efi/mod.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// Copyright © 2019 Intel Corporation
33

4-
#[cfg(all(not(test), not(feature = "integration_tests")))]
5-
use core::alloc as heap_alloc;
6-
74
use core::{
85
ffi::c_void,
96
mem::{size_of, transmute},
@@ -12,7 +9,6 @@ use core::{
129
};
1310

1411
use atomic_refcell::AtomicRefCell;
15-
use linked_list_allocator::LockedHeap;
1612
use r_efi::{
1713
efi::{
1814
self, AllocateType, Boolean, CapsuleHeader, Char16, Event, EventNotify, Guid, Handle,
@@ -64,16 +60,6 @@ struct HandleWrapper {
6460

6561
pub static ALLOCATOR: AtomicRefCell<Allocator> = AtomicRefCell::new(Allocator::new());
6662

67-
#[cfg(not(test))]
68-
#[global_allocator]
69-
pub static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
70-
71-
#[cfg(all(not(test), not(feature = "integration_tests")))]
72-
#[alloc_error_handler]
73-
fn heap_alloc_error_handler(layout: heap_alloc::Layout) -> ! {
74-
panic!("heap allocation error: {:?}", layout);
75-
}
76-
7763
pub static VARIABLES: AtomicRefCell<VariableAllocator> =
7864
AtomicRefCell::new(VariableAllocator::new());
7965

@@ -1078,7 +1064,6 @@ impl DevicePath {
10781064
}
10791065

10801066
const PAGE_SIZE: u64 = 4096;
1081-
const HEAP_SIZE: usize = 2 << 20; /* 2MiB */
10821067

10831068
// Populate allocator from E820, fixed ranges for the firmware and the loaded binary.
10841069
fn populate_allocator(info: &dyn bootinfo::Info, image_address: u64, image_size: u64) {
@@ -1128,28 +1113,8 @@ fn populate_allocator(info: &dyn bootinfo::Info, image_address: u64, image_size:
11281113
image_size / PAGE_SIZE,
11291114
image_address,
11301115
);
1131-
1132-
// Initialize heap allocator
1133-
init_heap_allocator(HEAP_SIZE);
11341116
}
11351117

1136-
#[cfg(not(test))]
1137-
fn init_heap_allocator(size: usize) {
1138-
let (status, heap_start) = ALLOCATOR.borrow_mut().allocate_pages(
1139-
efi::ALLOCATE_ANY_PAGES,
1140-
efi::BOOT_SERVICES_CODE,
1141-
size as u64 / PAGE_SIZE,
1142-
0,
1143-
);
1144-
assert!(status == Status::SUCCESS);
1145-
unsafe {
1146-
HEAP_ALLOCATOR.lock().init(heap_start as *mut _, size);
1147-
}
1148-
}
1149-
1150-
#[cfg(test)]
1151-
fn init_heap_allocator(_: usize) {}
1152-
11531118
#[repr(C)]
11541119
struct LoadedImageWrapper {
11551120
hw: HandleWrapper,

src/efi/var.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// Copyright (C) 2021 Akira Moroo
33

4-
#[cfg(not(test))]
5-
extern crate alloc;
6-
7-
#[cfg(not(test))]
8-
use alloc::vec::Vec;
9-
4+
use heapless::Vec;
105
use r_efi::efi;
116

7+
const MAX_VAR_NAME: usize = 64;
8+
const MAX_VAR_DATA: usize = 1024;
9+
const MAX_VAR_NUM: usize = 128;
10+
1211
#[derive(Debug)]
1312
struct Descriptor {
14-
name: Vec<u16>,
13+
name: Vec<u16, MAX_VAR_NAME>,
1514
guid: efi::Guid,
1615
attr: u32,
17-
data: Vec<u8>,
16+
data: Vec<u8, MAX_VAR_DATA>,
1817
}
1918

2019
impl Descriptor {
@@ -29,7 +28,7 @@ impl Descriptor {
2928
}
3029

3130
pub struct VariableAllocator {
32-
allocations: Vec<Descriptor>,
31+
allocations: Vec<Descriptor, MAX_VAR_NUM>,
3332
}
3433

3534
impl VariableAllocator {
@@ -49,8 +48,10 @@ impl VariableAllocator {
4948
}
5049

5150
let s = unsafe { core::slice::from_raw_parts(name, len + 1) };
52-
let mut name: Vec<u16> = Vec::new();
53-
name.extend_from_slice(s);
51+
let mut name: Vec<u16, MAX_VAR_NAME> = Vec::new();
52+
if name.extend_from_slice(s).is_err() {
53+
return None;
54+
}
5455
let guid = unsafe { &*guid };
5556
(0..self.allocations.len())
5657
.find(|&i| name == self.allocations[i].name && guid == &self.allocations[i].guid)
@@ -119,13 +120,19 @@ impl VariableAllocator {
119120
}
120121
let mut a = Descriptor::new();
121122
let name = unsafe { core::slice::from_raw_parts(name, len + 1) };
122-
a.name.extend_from_slice(name);
123+
if a.name.extend_from_slice(name).is_err() {
124+
return efi::Status::OUT_OF_RESOURCES;
125+
}
123126
a.guid = unsafe { *guid };
124127
a.attr = attr & !efi::VARIABLE_APPEND_WRITE;
125128
let src = unsafe { core::slice::from_raw_parts(data as *const u8, size) };
126-
a.data.extend_from_slice(src);
129+
if a.data.extend_from_slice(src).is_err() {
130+
return efi::Status::OUT_OF_RESOURCES;
131+
}
127132

128-
self.allocations.push(a);
133+
if self.allocations.push(a).is_err() {
134+
return efi::Status::OUT_OF_RESOURCES;
135+
}
129136

130137
return efi::Status::SUCCESS;
131138
}
@@ -144,7 +151,9 @@ impl VariableAllocator {
144151
return efi::Status::INVALID_PARAMETER;
145152
}
146153
let src = unsafe { core::slice::from_raw_parts(data as *const u8, size) };
147-
a.data.extend_from_slice(src);
154+
if a.data.extend_from_slice(src).is_err() {
155+
return efi::Status::OUT_OF_RESOURCES;
156+
}
148157
return efi::Status::SUCCESS;
149158
}
150159

@@ -159,7 +168,9 @@ impl VariableAllocator {
159168
}
160169
a.data.clear();
161170
let src = unsafe { core::slice::from_raw_parts(data as *const u8, size) };
162-
a.data.extend_from_slice(src);
171+
if a.data.extend_from_slice(src).is_err() {
172+
return efi::Status::OUT_OF_RESOURCES;
173+
}
163174

164175
efi::Status::SUCCESS
165176
}

0 commit comments

Comments
 (0)