Skip to content

Commit

Permalink
Figure out what address range is available for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
qinsoon committed Oct 17, 2024
1 parent dfe44e6 commit 5cad47e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
19 changes: 19 additions & 0 deletions src/util/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,25 @@ pub(crate) fn get_system_total_memory() -> u64 {
sys.total_memory()
}

/// Find the given bytes that can be mmapped. This will do an anonymous mapping, record the return
/// address and then unmap the mapping. This is only used for testing.
#[cfg(test)]
pub fn find_usable_address(bytes: usize, align: usize) -> Option<Address> {
let flags = libc::MAP_ANON | libc::MAP_PRIVATE;
let prot = MmapStrategy::TEST.prot.into_native_flags();
// Map extra to make sure that the return address is aligned.
let size_to_map = bytes + align;
let ret = unsafe { libc::mmap(std::ptr::null_mut(), size_to_map, prot, flags, -1, 0) };
if ret as isize == -1 {
None
} else {
let usable_addr = Address::from_mut_ptr(ret);
munmap(usable_addr, size_to_map).unwrap();
let ret = usable_addr.align_up(align);
Some(ret)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::mock_test_prelude::*;
use super::mock_test_vm_layout_default::test_with_vm_layout;
use crate::util::conversions::*;
use crate::util::heap::vm_layout::VMLayout;
use crate::util::heap::vm_layout::BYTES_IN_CHUNK;
use crate::util::Address;

// This test only run on 64bits.
Expand All @@ -13,21 +14,18 @@ fn test_vm_layout_compressed_pointer() {
with_mockvm(
default_setup,
|| {
let start = if cfg!(target_os = "macos") {
// Impossible to map 0x4000_0000 on maocOS. SO choose a different address.
0x60_0000_0000
} else {
0x4000_0000
};
let heap_size = 1024 * 1024;
let end = match start + heap_size {
let start = crate::util::memory::find_usable_address(heap_size, BYTES_IN_CHUNK)
.expect("Cannot find usable address range");
println!("Use {} as heap start", start);
let end = match start.as_usize() + heap_size {
end if end <= (4usize << 30) => 4usize << 30,
end if end <= (32usize << 30) => 32usize << 30,
_ => start + (32usize << 30),
_ => start.as_usize() + (32usize << 30),
};
let layout = VMLayout {
log_address_space: 35,
heap_start: chunk_align_down(unsafe { Address::from_usize(start) }),
heap_start: start,
heap_end: chunk_align_up(unsafe { Address::from_usize(end) }),
log_space_extent: 31,
force_use_contiguous_spaces: false,
Expand Down

0 comments on commit 5cad47e

Please sign in to comment.