Skip to content

Commit 40df277

Browse files
authored
Don't panic on OOM when allocating MmapVec (#12625)
For no_std scenarios gracefully handle allocation failure like OS-level failures are handled and propagate the error upwards.
1 parent 763ace0 commit 40df277

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

crates/wasmtime/src/runtime/vm/mmap_vec.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(not(has_virtual_memory))]
2+
use crate::error::OutOfMemory;
13
use crate::prelude::*;
24
use crate::runtime::vm::send_sync_ptr::SendSyncPtr;
35
#[cfg(has_virtual_memory)]
@@ -64,14 +66,16 @@ impl MmapVec {
6466
}
6567

6668
#[cfg(not(has_virtual_memory))]
67-
fn new_alloc(len: usize, alignment: usize) -> MmapVec {
69+
fn new_alloc(len: usize, alignment: usize) -> Result<MmapVec, OutOfMemory> {
6870
let layout = Layout::from_size_align(len, alignment)
6971
.expect("Invalid size or alignment for MmapVec allocation");
70-
let base = SendSyncPtr::new(
71-
NonNull::new(unsafe { alloc::alloc::alloc_zeroed(layout.clone()) })
72-
.expect("Allocation of MmapVec storage failed"),
73-
);
74-
MmapVec::Alloc { base, layout }
72+
match NonNull::new(unsafe { alloc::alloc::alloc_zeroed(layout.clone()) }) {
73+
Some(ptr) => {
74+
let base = SendSyncPtr::new(ptr);
75+
Ok(MmapVec::Alloc { base, layout })
76+
}
77+
None => return Err(OutOfMemory::new(layout.size())),
78+
}
7579
}
7680

7781
fn new_externally_owned(memory: NonNull<[u8]>) -> MmapVec {
@@ -93,7 +97,7 @@ impl MmapVec {
9397
}
9498
#[cfg(not(has_virtual_memory))]
9599
{
96-
return Ok(MmapVec::new_alloc(size, alignment));
100+
return Ok(MmapVec::new_alloc(size, alignment)?);
97101
}
98102
}
99103

0 commit comments

Comments
 (0)