memory fixes for windows (#138)

This commit is contained in:
Mackenzie Clark 2019-02-05 16:07:55 -08:00 committed by GitHub
parent cb941ea7d3
commit 7bd609fd58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 23 deletions

1
Cargo.lock generated
View File

@ -735,6 +735,7 @@ dependencies = [
"field-offset 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)",
"nix 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"page_size 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -14,6 +14,8 @@ page_size = "0.4.1"
wasmparser = "0.23.0"
parking_lot = "0.7.1"
lazy_static = "1.2.0"
libc = "0.2.48"
errno = "0.2.4"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["memoryapi"] }

View File

@ -1,11 +1,11 @@
use winapi::um::memoryapi::{
VirtualAlloc,
MEM_RESERVE, MEM_COMMIT,
PAGE_NOACCESS, PAGE_EXECUTE_READ, PAGE_READWRITE, PAGE_READONLY,
};
use page_size;
use std::ops::{Bound, RangeBounds};
use std::{ptr, slice};
use winapi::um::memoryapi::{VirtualAlloc, VirtualFree};
use winapi::um::winnt::{
MEM_COMMIT, MEM_DECOMMIT, MEM_RESERVE, PAGE_EXECUTE_READ, PAGE_NOACCESS, PAGE_READONLY,
PAGE_READWRITE,
};
unsafe impl Send for Memory {}
unsafe impl Sync for Memory {}
@ -27,17 +27,10 @@ impl Memory {
let size = round_up_to_page_size(size, page_size::get());
let ptr = unsafe {
VirtualAlloc(
ptr::null_mut(),
size,
MEM_RESERVE,
PAGE_NOACCESS,
)
};
let ptr = unsafe { VirtualAlloc(ptr::null_mut(), size, MEM_RESERVE, PAGE_NOACCESS) };
if ptr.is_null() {
Err("unable to allocate memory")
Err("unable to allocate memory".to_string())
} else {
Ok(Self {
ptr: ptr as *mut u8,
@ -46,7 +39,11 @@ impl Memory {
}
}
pub unsafe fn protect(&mut self, range: impl RangeBounds<usize>, protect: Protect) -> Result<(), String> {
pub unsafe fn protect(
&mut self,
range: impl RangeBounds<usize>,
protect: Protect,
) -> Result<(), String> {
let protect = protect.to_protect_const();
let range_start = match range.start_bound() {
@ -69,15 +66,10 @@ impl Memory {
assert!(size <= self.size);
// Commit the virtual memory.
let ptr = VirtualAlloc(
start as _,
size,
MEM_COMMIT,
protect,
);
let ptr = VirtualAlloc(start as _, size, MEM_COMMIT, protect);
if ptr.is_null() {
Err("unable to protect memory")
Err("unable to protect memory".to_string())
} else {
Ok(())
}
@ -103,7 +95,7 @@ impl Memory {
impl Drop for Memory {
fn drop(&mut self) {
if !self.ptr.is_null() {
let success = unsafe { libc::munmap(self.ptr as _, self.size) };
let success = unsafe { VirtualFree(self.ptr as _, self.size, MEM_DECOMMIT) };
assert_eq!(success, 0, "failed to unmap memory: {}", errno::errno());
}
}

View File

@ -1 +1,3 @@
mod memory;
pub use self::memory::{Memory, Protect};