Remove dependency on the region crate (#4407)
This commit removes Wasmtime's dependency on the `region` crate. The motivation for this came about when I was updating dependencies and saw that `region` had a new major version at 3.0.0 as opposed to our currently used 2.3 track. In reviewing the use cases of `region` within Wasmtime I found two trends in particular which motivated this commit: * Some unix-specific areas of `wasmtime_runtime` use `rustix::mm::mprotect` instead of `region::protect` already. This means that the usage of `region::protect` for changing virtual memory protections was already inconsistent. * Many uses of `region::protect` were already in unix-specific regions which could make use of `rustix`. Overall I opted to remove the dependency on the `region` crate to avoid chasing its versions over time. Unix-specific changes of protections were easily changed to `rustix::mm::mprotect`. There were two locations where a windows/unix split is now required and I subjectively ruled "that seems ok". Finally removing `region` also meant that the "what is the current page size" query needed to be inlined into `wasmtime_runtime`, which I have also subjectively ruled "that seems fine". Finally one final refactoring here was that the `unix.rs` and `linux.rs` split for the pooling allocator was merged. These two files already only differed in one function so I slapped a `cfg_if!` in there to help reduce the duplication.
This commit is contained in:
@@ -41,7 +41,8 @@ impl Mmap {
|
||||
|
||||
/// Create a new `Mmap` pointing to at least `size` bytes of page-aligned accessible memory.
|
||||
pub fn with_at_least(size: usize) -> Result<Self> {
|
||||
let rounded_size = region::page::ceil(size);
|
||||
let page_size = crate::page_size();
|
||||
let rounded_size = (size + (page_size - 1)) & !(page_size - 1);
|
||||
Self::accessible_reserved(rounded_size, rounded_size)
|
||||
}
|
||||
|
||||
@@ -158,7 +159,7 @@ impl Mmap {
|
||||
/// must be native page-size multiples.
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
pub fn accessible_reserved(accessible_size: usize, mapping_size: usize) -> Result<Self> {
|
||||
let page_size = region::page::size();
|
||||
let page_size = crate::page_size();
|
||||
assert_le!(accessible_size, mapping_size);
|
||||
assert_eq!(mapping_size & (page_size - 1), 0);
|
||||
assert_eq!(accessible_size & (page_size - 1), 0);
|
||||
@@ -226,7 +227,7 @@ impl Mmap {
|
||||
return Ok(Self::new());
|
||||
}
|
||||
|
||||
let page_size = region::page::size();
|
||||
let page_size = crate::page_size();
|
||||
assert_le!(accessible_size, mapping_size);
|
||||
assert_eq!(mapping_size & (page_size - 1), 0);
|
||||
assert_eq!(accessible_size & (page_size - 1), 0);
|
||||
@@ -278,16 +279,22 @@ impl Mmap {
|
||||
/// `self`'s reserved memory.
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
pub fn make_accessible(&mut self, start: usize, len: usize) -> Result<()> {
|
||||
let page_size = region::page::size();
|
||||
use rustix::mm::{mprotect, MprotectFlags};
|
||||
|
||||
let page_size = crate::page_size();
|
||||
assert_eq!(start & (page_size - 1), 0);
|
||||
assert_eq!(len & (page_size - 1), 0);
|
||||
assert_le!(len, self.len);
|
||||
assert_le!(start, self.len - len);
|
||||
|
||||
// Commit the accessible size.
|
||||
let ptr = self.ptr as *const u8;
|
||||
let ptr = self.ptr as *mut u8;
|
||||
unsafe {
|
||||
region::protect(ptr.add(start), len, region::Protection::READ_WRITE)?;
|
||||
mprotect(
|
||||
ptr.add(start).cast(),
|
||||
len,
|
||||
MprotectFlags::READ | MprotectFlags::WRITE,
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -303,7 +310,7 @@ impl Mmap {
|
||||
use std::io;
|
||||
use windows_sys::Win32::System::Memory::*;
|
||||
|
||||
let page_size = region::page::size();
|
||||
let page_size = crate::page_size();
|
||||
assert_eq!(start & (page_size - 1), 0);
|
||||
assert_eq!(len & (page_size - 1), 0);
|
||||
assert_le!(len, self.len);
|
||||
@@ -370,11 +377,11 @@ impl Mmap {
|
||||
assert!(range.end <= self.len());
|
||||
assert!(range.start <= range.end);
|
||||
assert!(
|
||||
range.start % region::page::size() == 0,
|
||||
range.start % crate::page_size() == 0,
|
||||
"changing of protections isn't page-aligned",
|
||||
);
|
||||
|
||||
let base = self.as_ptr().add(range.start);
|
||||
let base = self.as_ptr().add(range.start) as *mut _;
|
||||
let len = range.end - range.start;
|
||||
|
||||
// On Windows when we have a file mapping we need to specifically use
|
||||
@@ -385,19 +392,23 @@ impl Mmap {
|
||||
use std::io;
|
||||
use windows_sys::Win32::System::Memory::*;
|
||||
|
||||
if self.file.is_some() {
|
||||
let mut old = 0;
|
||||
if VirtualProtect(base as *mut _, len, PAGE_WRITECOPY, &mut old) == 0 {
|
||||
return Err(io::Error::last_os_error())
|
||||
.context("failed to change pages to `PAGE_WRITECOPY`");
|
||||
}
|
||||
return Ok(());
|
||||
let mut old = 0;
|
||||
let result = if self.file.is_some() {
|
||||
VirtualProtect(base, len, PAGE_WRITECOPY, &mut old)
|
||||
} else {
|
||||
VirtualProtect(base, len, PAGE_READWRITE, &mut old)
|
||||
};
|
||||
if result == 0 {
|
||||
return Err(io::Error::last_os_error().into());
|
||||
}
|
||||
}
|
||||
|
||||
// If we're not on Windows or if we're on Windows with an anonymous
|
||||
// mapping then we can use the `region` crate.
|
||||
region::protect(base, len, region::Protection::READ_WRITE)?;
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
use rustix::mm::{mprotect, MprotectFlags};
|
||||
mprotect(base, len, MprotectFlags::READ | MprotectFlags::WRITE)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -407,15 +418,29 @@ impl Mmap {
|
||||
assert!(range.end <= self.len());
|
||||
assert!(range.start <= range.end);
|
||||
assert!(
|
||||
range.start % region::page::size() == 0,
|
||||
range.start % crate::page_size() == 0,
|
||||
"changing of protections isn't page-aligned",
|
||||
);
|
||||
let base = self.as_ptr().add(range.start) as *mut _;
|
||||
let len = range.end - range.start;
|
||||
|
||||
region::protect(
|
||||
self.as_ptr().add(range.start),
|
||||
range.end - range.start,
|
||||
region::Protection::READ_EXECUTE,
|
||||
)?;
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::io;
|
||||
use windows_sys::Win32::System::Memory::*;
|
||||
|
||||
let mut old = 0;
|
||||
let result = VirtualProtect(base, len, PAGE_EXECUTE_READ, &mut old);
|
||||
if result == 0 {
|
||||
return Err(io::Error::last_os_error().into());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
use rustix::mm::{mprotect, MprotectFlags};
|
||||
mprotect(base, len, MprotectFlags::READ | MprotectFlags::EXEC)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user