Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory fixes for windows #138

Merged
merged 3 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/runtime-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
38 changes: 15 additions & 23 deletions lib/runtime-core/src/sys/windows/memory.rs
Original file line number Diff line number Diff line change
@@ -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 {}
Expand All @@ -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(String::from("unable to allocate memory"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer .to_string here, but it doesn't matter much.

} else {
Ok(Self {
ptr: ptr as *mut u8,
Expand All @@ -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() {
Expand All @@ -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(String::from("unable to protect memory"))
} else {
Ok(())
}
Expand All @@ -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) };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the winapi equivalent of munmap

assert_eq!(success, 0, "failed to unmap memory: {}", errno::errno());
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/runtime-core/src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
mod memory;

pub use self::memory::{Memory, Protect};