From 36a78a23b6159064d4fdf7038f5149fd533f8252 Mon Sep 17 00:00:00 2001 From: Johan Andersson Date: Wed, 8 May 2019 15:36:24 +0200 Subject: [PATCH] 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. --- lib/runtime-core/src/sys/windows/memory.rs | 30 +++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/runtime-core/src/sys/windows/memory.rs b/lib/runtime-core/src/sys/windows/memory.rs index d47388170..888aeff4d 100644 --- a/lib/runtime-core/src/sys/windows/memory.rs +++ b/lib/runtime-core/src/sys/windows/memory.rs @@ -33,7 +33,13 @@ impl Memory { let protect = protection.to_protect_const(); - let ptr = unsafe { VirtualAlloc(ptr::null_mut(), size, MEM_RESERVE, protect) }; + let flags = if protection == Protect::None { + MEM_RESERVE + } else { + MEM_RESERVE | MEM_COMMIT + }; + + let ptr = unsafe { VirtualAlloc(ptr::null_mut(), size, flags, protect) }; if ptr.is_null() { Err("unable to allocate memory".to_string()) @@ -229,3 +235,25 @@ fn round_up_to_page_size(size: usize, page_size: usize) -> usize { fn round_down_to_page_size(size: usize, page_size: usize) -> usize { size & !(page_size - 1) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn clone() { + // these should work + let _ = Memory::with_size_protect(200_000, Protect::Read) + .unwrap() + .clone(); + let _ = Memory::with_size_protect(200_000, Protect::ReadWrite) + .unwrap() + .clone(); + let _ = Memory::with_size_protect(200_000, Protect::ReadExec) + .unwrap() + .clone(); + + // this would cause segmentation fault as uncommited memory with no access + //let _ = Memory::with_size_protect(200_000, Protect::None).unwrap().clone(); + } +}