Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 35319d9

Browse files
Merge pull request #1666 from rust-osdev/uefi-alloc-misc
uefi-test-runner: streamline memory related tests
2 parents 262bc0b + 2fdcc70 commit 35319d9

File tree

1 file changed

+41
-44
lines changed

1 file changed

+41
-44
lines changed

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

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

3-
use alloc::vec::Vec;
4-
use uefi::boot;
5-
use uefi::mem::memory_map::{MemoryMap, MemoryMapMut};
6-
use uefi_raw::table::boot::MemoryType;
7-
83
pub fn test() {
94
info!("Testing memory functions");
105

116
bootservices::allocate_pages();
127
bootservices::allocate_pool();
8+
bootservices::memory_map();
139

1410
global::alloc_vec();
1511
global::alloc_alignment();
16-
17-
test_memory_map();
1812
}
1913

2014
/// Tests that directly use UEFI boot services to allocate memory.
2115
mod bootservices {
16+
use alloc::vec::Vec;
2217
use uefi::boot;
2318
use uefi::boot::AllocateType;
19+
use uefi::mem::memory_map::{MemoryMap, MemoryMapMut};
2420
use uefi_raw::table::boot::MemoryType;
2521

2622
/// Tests the `allocate_pages` boot service.
@@ -53,6 +49,44 @@ mod bootservices {
5349
}
5450
unsafe { boot::free_pool(ptr) }.unwrap();
5551
}
52+
53+
/// Tests getting the memory map and performing a few sanity checks on it.
54+
pub fn memory_map() {
55+
info!("Testing memory map functions");
56+
57+
let mut memory_map =
58+
boot::memory_map(MemoryType::LOADER_DATA).expect("Failed to retrieve UEFI memory map");
59+
60+
memory_map.sort();
61+
62+
// Collect the descriptors into a vector
63+
let descriptors = memory_map.entries().copied().collect::<Vec<_>>();
64+
65+
// Ensured we have at least one entry.
66+
// Real memory maps usually have dozens of entries.
67+
assert!(!descriptors.is_empty(), "Memory map is empty");
68+
69+
let mut curr_value = descriptors[0];
70+
71+
for value in descriptors.iter().skip(1) {
72+
if value.phys_start <= curr_value.phys_start {
73+
panic!("memory map sorting failed");
74+
}
75+
curr_value = *value;
76+
}
77+
78+
// This is pretty much a basic sanity test to ensure returned memory
79+
// isn't filled with random values.
80+
let first_desc = descriptors[0];
81+
82+
#[cfg(target_arch = "x86_64")]
83+
{
84+
let phys_start = first_desc.phys_start;
85+
assert_eq!(phys_start, 0, "Memory does not start at address 0");
86+
}
87+
let page_count = first_desc.page_count;
88+
assert!(page_count != 0, "Memory map entry has size zero");
89+
}
5690
}
5791

5892
/// Tests that use [`uefi::allocator::Allocator`], which is configured as the
@@ -97,40 +131,3 @@ mod global {
97131
}
98132
}
99133
}
100-
101-
fn test_memory_map() {
102-
info!("Testing memory map functions");
103-
104-
let mut memory_map =
105-
boot::memory_map(MemoryType::LOADER_DATA).expect("Failed to retrieve UEFI memory map");
106-
107-
memory_map.sort();
108-
109-
// Collect the descriptors into a vector
110-
let descriptors = memory_map.entries().copied().collect::<Vec<_>>();
111-
112-
// Ensured we have at least one entry.
113-
// Real memory maps usually have dozens of entries.
114-
assert!(!descriptors.is_empty(), "Memory map is empty");
115-
116-
let mut curr_value = descriptors[0];
117-
118-
for value in descriptors.iter().skip(1) {
119-
if value.phys_start <= curr_value.phys_start {
120-
panic!("memory map sorting failed");
121-
}
122-
curr_value = *value;
123-
}
124-
125-
// This is pretty much a basic sanity test to ensure returned memory
126-
// isn't filled with random values.
127-
let first_desc = descriptors[0];
128-
129-
#[cfg(target_arch = "x86_64")]
130-
{
131-
let phys_start = first_desc.phys_start;
132-
assert_eq!(phys_start, 0, "Memory does not start at address 0");
133-
}
134-
let page_count = first_desc.page_count;
135-
assert!(page_count != 0, "Memory map entry has size zero");
136-
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /