Skip to content

Commit 205bd6f

Browse files
committed
Figure out what address range is available for testing
1 parent dfe44e6 commit 205bd6f

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/util/memory.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,25 @@ pub(crate) fn get_system_total_memory() -> u64 {
356356
sys.total_memory()
357357
}
358358

359+
/// Find the given bytes that can be mmapped. This will do an anonymous mapping, record the return
360+
/// address and then unmap the mapping. This is only used for testing.
361+
#[cfg(test)]
362+
pub fn find_usable_address(bytes: usize, align: usize) -> Option<Address> {
363+
let flags = libc::MAP_ANON | libc::MAP_PRIVATE;
364+
let prot = MmapStrategy::TEST.prot.into_native_flags();
365+
// Map extra to make sure that the return address is aligned.
366+
let size_to_map = bytes + align;
367+
let ret = unsafe { libc::mmap(std::ptr::null_mut(), size_to_map, prot, flags, -1, 0) };
368+
if ret as isize == -1 {
369+
None
370+
} else {
371+
let usable_addr = Address::from_mut_ptr(ret);
372+
munmap(usable_addr, size_to_map).unwrap();
373+
let ret = usable_addr.align_up(align);
374+
Some(ret)
375+
}
376+
}
377+
359378
#[cfg(test)]
360379
mod tests {
361380
use super::*;

src/vm/tests/mock_tests/mock_test_vm_layout_compressed_pointer.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::mock_test_prelude::*;
44
use super::mock_test_vm_layout_default::test_with_vm_layout;
55
use crate::util::conversions::*;
66
use crate::util::heap::vm_layout::VMLayout;
7+
use crate::util::heap::vm_layout::BYTES_IN_CHUNK;
78
use crate::util::Address;
89

910
// This test only run on 64bits.
@@ -13,21 +14,18 @@ fn test_vm_layout_compressed_pointer() {
1314
with_mockvm(
1415
default_setup,
1516
|| {
16-
let start = if cfg!(target_os = "macos") {
17-
// Impossible to map 0x4000_0000 on maocOS. SO choose a different address.
18-
0x60_0000_0000
19-
} else {
20-
0x4000_0000
21-
};
2217
let heap_size = 1024 * 1024;
23-
let end = match start + heap_size {
18+
let start = crate::util::memory::find_usable_address(heap_size, BYTES_IN_CHUNK)
19+
.expect("Cannot find usable address range");
20+
println!("Use {} as heap start", start);
21+
let end = match start.as_usize() + heap_size {
2422
end if end <= (4usize << 30) => 4usize << 30,
2523
end if end <= (32usize << 30) => 32usize << 30,
26-
_ => start + (32usize << 30),
24+
_ => start.as_usize() + (32usize << 30),
2725
};
2826
let layout = VMLayout {
2927
log_address_space: 35,
30-
heap_start: chunk_align_down(unsafe { Address::from_usize(start) }),
28+
heap_start: start,
3129
heap_end: chunk_align_up(unsafe { Address::from_usize(end) }),
3230
log_space_extent: 31,
3331
force_use_contiguous_spaces: false,

0 commit comments

Comments
 (0)