Skip to content

Commit abbbe77

Browse files
committed
Fix Windows crash on caching compiled artifact.
Memory clone function wasn't able to write to new reserved memory object. Changed so allocating Memory objects with protection flag allocates commited memory.
1 parent 1f028e9 commit abbbe77

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

lib/runtime-core/src/sys/windows/memory.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ impl Memory {
3333

3434
let protect = protection.to_protect_const();
3535

36-
let ptr = unsafe { VirtualAlloc(ptr::null_mut(), size, MEM_RESERVE, protect) };
36+
let flags = if protection == Protect::None {
37+
MEM_RESERVE
38+
} else {
39+
MEM_RESERVE|MEM_COMMIT
40+
};
41+
42+
let ptr = unsafe { VirtualAlloc(ptr::null_mut(), size, flags, protect) };
3743

3844
if ptr.is_null() {
3945
Err("unable to allocate memory".to_string())
@@ -229,3 +235,21 @@ fn round_up_to_page_size(size: usize, page_size: usize) -> usize {
229235
fn round_down_to_page_size(size: usize, page_size: usize) -> usize {
230236
size & !(page_size - 1)
231237
}
238+
239+
240+
#[cfg(test)]
241+
mod tests {
242+
use super::*;
243+
244+
#[test]
245+
fn clone() {
246+
// these should work
247+
let _ = Memory::with_size_protect(200_000, Protect::Read).unwrap().clone();
248+
let _ = Memory::with_size_protect(200_000, Protect::ReadWrite).unwrap().clone();
249+
let _ = Memory::with_size_protect(200_000, Protect::ReadExec).unwrap().clone();
250+
251+
252+
// this would cause segmentation fault as uncommited memory with no access
253+
//let _ = Memory::with_size_protect(200_000, Protect::None).unwrap().clone();
254+
}
255+
}

0 commit comments

Comments
 (0)