More PR feedback changes.

* More use of `anyhow`.
* Change `make_accessible` into `protect_linear_memory` to better demonstrate
  what it is used for; this will make the uffd implementation make a little
  more sense.
* Remove `create_memory_map` in favor of just creating the `Mmap` instances in
  the pooling allocator. This also removes the need for `MAP_NORESERVE` in the
  uffd implementation.
* Moar comments.
* Remove `BasePointerIterator` in favor of `impl Iterator`.
* The uffd implementation now only monitors linear memory pages and will only
  receive faults on pages that could potentially be accessible and never on a
  statically known guard page.
* Stop allocating memory or table pools if the maximum limit of the memory or
  table is 0.
This commit is contained in:
Peter Huene
2021-03-04 14:01:42 -08:00
parent a464465e2f
commit ff840b3d3b
11 changed files with 484 additions and 500 deletions

View File

@@ -1,22 +1,55 @@
use crate::Mmap;
use anyhow::{anyhow, Result};
use anyhow::{bail, Result};
use winapi::um::memoryapi::{VirtualAlloc, VirtualFree};
use winapi::um::winnt::{MEM_COMMIT, MEM_DECOMMIT, PAGE_READWRITE};
pub unsafe fn make_accessible(addr: *mut u8, len: usize) -> bool {
// This doesn't use the `region` crate because the memory needs to be committed
!VirtualAlloc(addr as _, len, MEM_COMMIT, PAGE_READWRITE).is_null()
pub fn commit(addr: *mut u8, len: usize) -> Result<()> {
if len == 0 {
return Ok(());
}
// Memory needs to be committed, so don't use the `region` crate
if unsafe { VirtualAlloc(addr as _, len, MEM_COMMIT, PAGE_READWRITE).is_null() } {
bail!("failed to commit memory as read/write");
}
Ok(())
}
pub unsafe fn decommit(addr: *mut u8, len: usize) {
assert!(
VirtualFree(addr as _, len, MEM_DECOMMIT) != 0,
"failed to decommit memory pages: {}",
std::io::Error::last_os_error()
);
pub fn decommit(addr: *mut u8, len: usize) -> Result<()> {
if len == 0 {
return Ok(());
}
if unsafe { VirtualFree(addr as _, len, MEM_DECOMMIT) } == 0 {
bail!(
"failed to decommit memory pages: {}",
std::io::Error::last_os_error()
);
}
Ok(())
}
pub fn create_memory_map(accessible_size: usize, mapping_size: usize) -> Result<Mmap> {
Mmap::accessible_reserved(accessible_size, mapping_size)
.map_err(|e| anyhow!("failed to allocate pool memory: {}", e))
pub fn commit_memory_pages(addr: *mut u8, len: usize) -> Result<()> {
commit(addr, len)
}
pub fn decommit_memory_pages(addr: *mut u8, len: usize) -> Result<()> {
decommit(addr, len)
}
pub fn commit_table_pages(addr: *mut u8, len: usize) -> Result<()> {
commit(addr, len)
}
pub fn decommit_table_pages(addr: *mut u8, len: usize) -> Result<()> {
decommit(addr, len)
}
pub fn commit_stack_pages(addr: *mut u8, len: usize) -> Result<()> {
commit(addr, len)
}
pub fn decommit_stack_pages(addr: *mut u8, len: usize) -> Result<()> {
decommit(addr, len)
}