Skip to content

Commit

Permalink
Merge #428
Browse files Browse the repository at this point in the history
428: Fix Windows crash on caching compiled artifact. r=syrusakbary a=repi

Memory clone function wasn't able to write to new reserved memory object.

Changed so allocating Memory objects with protection flag allocates commited memory. And added unit test to verify that it doedsn't crash anymore.

Not fully sure this is the best solution though, but fixes this specific clone crash.

Co-authored-by: Johan Andersson <repi@repi.se>
  • Loading branch information
bors[bot] and repi committed May 8, 2019
2 parents 1f028e9 + 36a78a2 commit 9a831b7
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion lib/runtime-core/src/sys/windows/memory.rs
Expand Up @@ -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())
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 9a831b7

Please sign in to comment.