Skip to content

Commit 080220c

Browse files
authored
Merge branch 'master' into feature/llvm-cgapi
2 parents 54095fc + 9a831b7 commit 080220c

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

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

+29-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,25 @@ 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+
#[cfg(test)]
240+
mod tests {
241+
use super::*;
242+
243+
#[test]
244+
fn clone() {
245+
// these should work
246+
let _ = Memory::with_size_protect(200_000, Protect::Read)
247+
.unwrap()
248+
.clone();
249+
let _ = Memory::with_size_protect(200_000, Protect::ReadWrite)
250+
.unwrap()
251+
.clone();
252+
let _ = Memory::with_size_protect(200_000, Protect::ReadExec)
253+
.unwrap()
254+
.clone();
255+
256+
// this would cause segmentation fault as uncommited memory with no access
257+
//let _ = Memory::with_size_protect(200_000, Protect::None).unwrap().clone();
258+
}
259+
}

0 commit comments

Comments
 (0)