Skip to content

Commit

Permalink
Print the address that we failed to mmap
Browse files Browse the repository at this point in the history
  • Loading branch information
qinsoon committed Oct 17, 2024
1 parent 25a96df commit 859211d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/policy/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub trait Space<VM: VMBinding>: 'static + SFT + Sync + Downcast {
.try_map_metadata_space(res.start, bytes),
)
{
memory::handle_mmap_error::<VM>(mmap_error, tls);
memory::handle_mmap_error::<VM>(mmap_error, tls, res.start, bytes);
}
};
let grow_space = || {
Expand Down
26 changes: 15 additions & 11 deletions src/util/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,20 @@ pub fn munmap(start: Address, size: usize) -> Result<()> {

/// Properly handle errors from a mmap Result, including invoking the binding code in the case of
/// an OOM error.
pub fn handle_mmap_error<VM: VMBinding>(error: Error, tls: VMThread) -> ! {
pub fn handle_mmap_error<VM: VMBinding>(
error: Error,
tls: VMThread,
addr: Address,
bytes: usize,
) -> ! {
use std::io::ErrorKind;

eprintln!("Failed to mmap {} - {}", addr, addr + bytes);
eprintln!("{}", get_process_memory_maps());

match error.kind() {
// From Rust nightly 2021-05-12, we started to see Rust added this ErrorKind.
ErrorKind::OutOfMemory => {
eprintln!("{}", get_process_memory_maps());
// Signal `MmapOutOfMemory`. Expect the VM to abort immediately.
trace!("Signal MmapOutOfMemory!");
VM::VMCollection::out_of_memory(tls, AllocationError::MmapOutOfMemory);
Expand All @@ -206,7 +213,6 @@ pub fn handle_mmap_error<VM: VMBinding>(error: Error, tls: VMThread) -> ! {
if let Some(os_errno) = error.raw_os_error() {
// If it is OOM, we invoke out_of_memory() through the VM interface.
if os_errno == libc::ENOMEM {
eprintln!("{}", get_process_memory_maps());
// Signal `MmapOutOfMemory`. Expect the VM to abort immediately.
trace!("Signal MmapOutOfMemory!");
VM::VMCollection::out_of_memory(tls, AllocationError::MmapOutOfMemory);
Expand All @@ -215,12 +221,10 @@ pub fn handle_mmap_error<VM: VMBinding>(error: Error, tls: VMThread) -> ! {
}
}
ErrorKind::AlreadyExists => {
eprintln!("{}", get_process_memory_maps());
panic!("Failed to mmap, the address is already mapped. Should MMTk quarantine the address range first?");
}
_ => {}
}
eprintln!("{}", get_process_memory_maps());
panic!("Unexpected mmap failure: {:?}", error)
}

Expand Down Expand Up @@ -306,20 +310,20 @@ pub fn get_process_memory_maps() -> String {

// Execute the vmmap command
let output = std::process::Command::new("vmmap")
.arg(pid.to_string()) // Pass the PID as an argument
.output() // Capture the output
.arg(pid.to_string()) // Pass the PID as an argument
.output() // Capture the output
.expect("Failed to execute vmmap command");

// Check if the command was successful
if output.status.success() {
// Convert the command output to a string
let output_str = std::str::from_utf8(&output.stdout)
.expect("Failed to convert output to string");
let output_str =
std::str::from_utf8(&output.stdout).expect("Failed to convert output to string");
output_str.to_string()
} else {
// Handle the error case
let error_message = std::str::from_utf8(&output.stderr)
.expect("Failed to convert error message to string");
let error_message =
std::str::from_utf8(&output.stderr).expect("Failed to convert error message to string");
panic!("Failed to get process memory map: {}", error_message)
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/vm/tests/mock_tests/mock_test_handle_mmap_conflict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub fn test_handle_mmap_conflict() {
memory::handle_mmap_error::<MockVM>(
mmap2_res.err().unwrap(),
VMThread::UNINITIALIZED,
start,
one_megabyte,
);
});

Expand Down
2 changes: 2 additions & 0 deletions src/vm/tests/mock_tests/mock_test_handle_mmap_oom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub fn test_handle_mmap_oom() {
memory::handle_mmap_error::<MockVM>(
mmap_res.err().unwrap(),
VMThread::UNINITIALIZED,
start,
LARGE_SIZE,
);
});
assert!(panic_res.is_err());
Expand Down

0 comments on commit 859211d

Please sign in to comment.