Skip to content

Commit bd3e22a

Browse files
committed
uefi-test-runner: restructure alloc test into modules
This is a prerequisite for the next commit.
1 parent a595057 commit bd3e22a

File tree

1 file changed

+60
-46
lines changed

1 file changed

+60
-46
lines changed

uefi-test-runner/src/boot/memory.rs

+60-46
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,88 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

33
use alloc::vec::Vec;
4-
use uefi::boot::{self, AllocateType};
5-
use uefi::mem::memory_map::{MemoryMap, MemoryMapMut, MemoryType};
4+
use uefi::boot;
5+
use uefi::mem::memory_map::{MemoryMap, MemoryMapMut};
6+
use uefi_raw::table::boot::MemoryType;
67

78
pub fn test() {
89
info!("Testing memory functions");
910

10-
test_allocate_pages();
11-
test_allocate_pool();
11+
bootservices::allocate_pages();
12+
bootservices::allocate_pool();
1213

13-
vec_alloc();
14-
alloc_alignment();
14+
global::alloc_vec();
15+
global::alloc_alignment();
1516

1617
test_memory_map();
1718
}
1819

19-
fn test_allocate_pages() {
20-
let num_pages = 1;
21-
let ptr =
22-
boot::allocate_pages(AllocateType::AnyPages, MemoryType::LOADER_DATA, num_pages).unwrap();
23-
let addr = ptr.as_ptr() as usize;
24-
assert_eq!(addr % 4096, 0, "Page pointer is not page-aligned");
20+
/// Tests that directly use UEFI boot services to allocate memory.
21+
mod bootservices {
22+
use uefi::boot;
23+
use uefi::boot::AllocateType;
24+
use uefi_raw::table::boot::MemoryType;
25+
26+
/// Tests the `allocate_pages` boot service.
27+
pub fn allocate_pages() {
28+
let num_pages = 1;
29+
let ptr = boot::allocate_pages(AllocateType::AnyPages, MemoryType::LOADER_DATA, num_pages)
30+
.unwrap();
31+
let addr = ptr.as_ptr() as usize;
32+
assert_eq!(addr % 4096, 0, "Page pointer is not page-aligned");
33+
34+
// Verify the page can be written to.
35+
{
36+
let ptr = ptr.as_ptr();
37+
unsafe { ptr.write_volatile(0xff) };
38+
unsafe { ptr.add(4095).write_volatile(0xff) };
39+
}
2540

26-
// Verify the page can be written to.
27-
{
28-
let ptr = ptr.as_ptr();
29-
unsafe { ptr.write_volatile(0xff) };
30-
unsafe { ptr.add(4095).write_volatile(0xff) };
41+
unsafe { boot::free_pages(ptr, num_pages) }.unwrap();
3142
}
3243

33-
unsafe { boot::free_pages(ptr, num_pages) }.unwrap();
34-
}
44+
/// Tests the `allocate_pool` boot service.
45+
pub fn allocate_pool() {
46+
let ptr = boot::allocate_pool(MemoryType::LOADER_DATA, 10).unwrap();
3547

36-
fn test_allocate_pool() {
37-
let ptr = boot::allocate_pool(MemoryType::LOADER_DATA, 10).unwrap();
38-
39-
// Verify the allocation can be written to.
40-
{
41-
let ptr = ptr.as_ptr();
42-
unsafe { ptr.write_volatile(0xff) };
43-
unsafe { ptr.add(9).write_volatile(0xff) };
48+
// Verify the allocation can be written to.
49+
{
50+
let ptr = ptr.as_ptr();
51+
unsafe { ptr.write_volatile(0xff) };
52+
unsafe { ptr.add(9).write_volatile(0xff) };
53+
}
54+
unsafe { boot::free_pool(ptr) }.unwrap();
4455
}
45-
unsafe { boot::free_pool(ptr) }.unwrap();
4656
}
4757

48-
// Simple test to ensure our custom allocator works with the `alloc` crate.
49-
fn vec_alloc() {
50-
info!("Allocating a vector through the `alloc` crate");
58+
/// Tests that use [`uefi::allocator::Allocator`], which is configured as the
59+
/// global allocator.
60+
mod global {
61+
/// Simple test to ensure our custom allocator works with the `alloc` crate.
62+
pub fn alloc_vec() {
63+
info!("Allocating a vector through the `alloc` crate");
5164

52-
#[allow(clippy::useless_vec)]
53-
let mut values = vec![-5, 16, 23, 4, 0];
65+
#[allow(clippy::useless_vec)]
66+
let mut values = vec![-5, 16, 23, 4, 0];
5467

55-
values.sort_unstable();
68+
values.sort_unstable();
5669

57-
assert_eq!(values[..], [-5, 0, 4, 16, 23], "Failed to sort vector");
58-
}
70+
assert_eq!(values[..], [-5, 0, 4, 16, 23], "Failed to sort vector");
71+
}
5972

60-
// Simple test to ensure our custom allocator works with correct alignment.
61-
fn alloc_alignment() {
62-
info!("Allocating a structure with alignment to 0x100");
73+
/// Simple test to ensure our custom allocator works with correct alignment.
74+
pub fn alloc_alignment() {
75+
info!("Allocating a structure with alignment to 0x100");
6376

64-
#[repr(align(0x100))]
65-
struct Block(
66-
// Ignore warning due to field not being read.
67-
#[allow(dead_code)] [u8; 0x100],
68-
);
77+
#[repr(align(0x100))]
78+
struct Block(
79+
// Ignore warning due to field not being read.
80+
#[allow(dead_code)] [u8; 0x100],
81+
);
6982

70-
let value = vec![Block([1; 0x100])];
71-
assert_eq!(value.as_ptr() as usize % 0x100, 0, "Wrong alignment");
83+
let value = vec![Block([1; 0x100])];
84+
assert_eq!(value.as_ptr() as usize % 0x100, 0, "Wrong alignment");
85+
}
7286
}
7387

7488
fn test_memory_map() {

0 commit comments

Comments
 (0)