Code review feedback changes.

* Add `anyhow` dependency to `wasmtime-runtime`.
* Revert `get_data` back to `fn`.
* Remove `DataInitializer` and box the data in `Module` translation instead.
* Improve comments on `MemoryInitialization`.
* Remove `MemoryInitialization::OutOfBounds` in favor of proper bulk memory
  semantics.
* Use segmented memory initialization except for when the uffd feature is
  enabled on Linux.
* Validate modules with the allocator after translation.
* Updated various functions in the runtime to return `anyhow::Result`.
* Use a slice when copying pages instead of `ptr::copy_nonoverlapping`.
* Remove unnecessary casts in `OnDemandAllocator::deallocate`.
* Better document the `uffd` feature.
* Use WebAssembly page-sized pages in the paged initialization.
* Remove the stack pool from the uffd handler and simply protect just the guard
  pages.
This commit is contained in:
Peter Huene
2021-03-03 16:41:33 -08:00
parent 5ee2b8742a
commit a464465e2f
19 changed files with 569 additions and 791 deletions

View File

@@ -12,6 +12,7 @@ use super::{
InstanceAllocator, InstanceHandle, InstantiationError,
};
use crate::{instance::Instance, table::max_table_element_size, Memory, Mmap, Table, VMContext};
use anyhow::{anyhow, bail, Context, Result};
use rand::Rng;
use std::cell::RefCell;
use std::cmp::min;
@@ -20,7 +21,7 @@ use std::mem;
use std::sync::{Arc, Mutex};
use wasmtime_environ::{
entity::{EntitySet, PrimaryMap},
MemoryStyle, Module, ModuleTranslation, Tunables, VMOffsets, WASM_PAGE_SIZE,
MemoryStyle, Module, Tunables, VMOffsets, WASM_PAGE_SIZE,
};
cfg_if::cfg_if! {
@@ -30,10 +31,9 @@ cfg_if::cfg_if! {
} else if #[cfg(all(feature = "uffd", target_os = "linux"))] {
mod uffd;
use uffd as imp;
use imp::{PageFaultHandler, reset_guard_page};
use imp::PageFaultHandler;
use super::{check_init_bounds, initialize_tables};
use wasmtime_environ::MemoryInitialization;
use std::sync::atomic::{AtomicBool, Ordering};
} else if #[cfg(target_os = "linux")] {
mod linux;
use linux as imp;
@@ -105,73 +105,81 @@ pub struct ModuleLimits {
}
impl ModuleLimits {
fn validate_module(&self, module: &Module) -> Result<(), String> {
fn validate(&self, module: &Module) -> Result<()> {
if module.num_imported_funcs > self.imported_functions as usize {
return Err(format!(
bail!(
"imported function count of {} exceeds the limit of {}",
module.num_imported_funcs, self.imported_functions
));
module.num_imported_funcs,
self.imported_functions
);
}
if module.num_imported_tables > self.imported_tables as usize {
return Err(format!(
bail!(
"imported tables count of {} exceeds the limit of {}",
module.num_imported_tables, self.imported_tables
));
module.num_imported_tables,
self.imported_tables
);
}
if module.num_imported_memories > self.imported_memories as usize {
return Err(format!(
bail!(
"imported memories count of {} exceeds the limit of {}",
module.num_imported_memories, self.imported_memories
));
module.num_imported_memories,
self.imported_memories
);
}
if module.num_imported_globals > self.imported_globals as usize {
return Err(format!(
bail!(
"imported globals count of {} exceeds the limit of {}",
module.num_imported_globals, self.imported_globals
));
module.num_imported_globals,
self.imported_globals
);
}
if module.types.len() > self.types as usize {
return Err(format!(
bail!(
"defined types count of {} exceeds the limit of {}",
module.types.len(),
self.types
));
);
}
let functions = module.functions.len() - module.num_imported_funcs;
if functions > self.functions as usize {
return Err(format!(
bail!(
"defined functions count of {} exceeds the limit of {}",
functions, self.functions
));
functions,
self.functions
);
}
let tables = module.table_plans.len() - module.num_imported_tables;
if tables > self.tables as usize {
return Err(format!(
bail!(
"defined tables count of {} exceeds the limit of {}",
tables, self.tables
));
tables,
self.tables
);
}
let memories = module.memory_plans.len() - module.num_imported_memories;
if memories > self.memories as usize {
return Err(format!(
bail!(
"defined memories count of {} exceeds the limit of {}",
memories, self.memories
));
memories,
self.memories
);
}
let globals = module.globals.len() - module.num_imported_globals;
if globals > self.globals as usize {
return Err(format!(
bail!(
"defined globals count of {} exceeds the limit of {}",
globals, self.globals
));
globals,
self.globals
);
}
for (i, plan) in module.table_plans.values().as_slice()[module.num_imported_tables..]
@@ -179,10 +187,12 @@ impl ModuleLimits {
.enumerate()
{
if plan.table.minimum > self.table_elements {
return Err(format!(
bail!(
"table index {} has a minimum element size of {} which exceeds the limit of {}",
i, plan.table.minimum, self.table_elements
));
i,
plan.table.minimum,
self.table_elements
);
}
}
@@ -191,17 +201,19 @@ impl ModuleLimits {
.enumerate()
{
if plan.memory.minimum > self.memory_pages {
return Err(format!(
bail!(
"memory index {} has a minimum page size of {} which exceeds the limit of {}",
i, plan.memory.minimum, self.memory_pages
));
i,
plan.memory.minimum,
self.memory_pages
);
}
if let MemoryStyle::Dynamic = plan.style {
return Err(format!(
bail!(
"memory index {} has an unsupported dynamic memory plan style",
i,
));
);
}
}
@@ -353,7 +365,7 @@ struct InstancePool {
}
impl InstancePool {
fn new(module_limits: &ModuleLimits, instance_limits: &InstanceLimits) -> Result<Self, String> {
fn new(module_limits: &ModuleLimits, instance_limits: &InstanceLimits) -> Result<Self> {
let page_size = region::page::size();
// Calculate the maximum size of an Instance structure given the limits
@@ -373,7 +385,7 @@ impl InstancePool {
let instance_size = round_up_to_pow2(
mem::size_of::<Instance>()
.checked_add(offsets.size_of_vmctx() as usize)
.ok_or_else(|| "instance size exceeds addressable memory".to_string())?,
.ok_or_else(|| anyhow!("instance size exceeds addressable memory"))?,
page_size,
);
@@ -381,7 +393,7 @@ impl InstancePool {
let allocation_size = instance_size
.checked_mul(max_instances)
.ok_or_else(|| "total size of instance data exceeds addressable memory".to_string())?;
.ok_or_else(|| anyhow!("total size of instance data exceeds addressable memory"))?;
let pool = Self {
mapping: create_memory_map(allocation_size, allocation_size)?,
@@ -527,7 +539,7 @@ impl InstancePool {
#[cfg(all(feature = "uffd", target_os = "linux"))]
instance
.reset_guard_pages()
.map_err(InstantiationError::Resource)?;
.map_err(|e| InstantiationError::Resource(e.to_string()))?;
instance.memories.clear();
@@ -610,9 +622,9 @@ struct MemoryPool {
}
impl MemoryPool {
fn new(module_limits: &ModuleLimits, instance_limits: &InstanceLimits) -> Result<Self, String> {
fn new(module_limits: &ModuleLimits, instance_limits: &InstanceLimits) -> Result<Self> {
let memory_size = usize::try_from(instance_limits.memory_reservation_size)
.map_err(|_| "memory reservation size exceeds addressable memory".to_string())?;
.map_err(|_| anyhow!("memory reservation size exceeds addressable memory"))?;
debug_assert!(
memory_size % region::page::size() == 0,
@@ -627,7 +639,7 @@ impl MemoryPool {
.checked_mul(max_memories)
.and_then(|c| c.checked_mul(max_instances))
.ok_or_else(|| {
"total size of memory reservation exceeds addressable memory".to_string()
anyhow!("total size of memory reservation exceeds addressable memory")
})?;
Ok(Self {
@@ -670,13 +682,13 @@ struct TablePool {
}
impl TablePool {
fn new(module_limits: &ModuleLimits, instance_limits: &InstanceLimits) -> Result<Self, String> {
fn new(module_limits: &ModuleLimits, instance_limits: &InstanceLimits) -> Result<Self> {
let page_size = region::page::size();
let table_size = round_up_to_pow2(
max_table_element_size()
.checked_mul(module_limits.table_elements as usize)
.ok_or_else(|| "table size exceeds addressable memory".to_string())?,
.ok_or_else(|| anyhow!("table size exceeds addressable memory"))?,
page_size,
);
@@ -686,9 +698,7 @@ impl TablePool {
let allocation_size = table_size
.checked_mul(max_tables)
.and_then(|c| c.checked_mul(max_instances))
.ok_or_else(|| {
"total size of instance tables exceeds addressable memory".to_string()
})?;
.ok_or_else(|| anyhow!("total size of instance tables exceeds addressable memory"))?;
Ok(Self {
mapping: create_memory_map(0, allocation_size)?,
@@ -733,12 +743,10 @@ struct StackPool {
max_instances: usize,
page_size: usize,
free_list: Mutex<Vec<usize>>,
#[cfg(all(feature = "uffd", target_os = "linux"))]
faulted_guard_pages: Arc<[AtomicBool]>,
}
impl StackPool {
fn new(instance_limits: &InstanceLimits, stack_size: usize) -> Result<Self, String> {
fn new(instance_limits: &InstanceLimits, stack_size: usize) -> Result<Self> {
let page_size = region::page::size();
// On Windows, don't allocate any fiber stacks as native fibers are always used
@@ -748,26 +756,33 @@ impl StackPool {
} else {
round_up_to_pow2(stack_size, page_size)
.checked_add(page_size)
.ok_or_else(|| "stack size exceeds addressable memory".to_string())?
.ok_or_else(|| anyhow!("stack size exceeds addressable memory"))?
};
let max_instances = instance_limits.count as usize;
let allocation_size = stack_size.checked_mul(max_instances).ok_or_else(|| {
"total size of execution stacks exceeds addressable memory".to_string()
})?;
let allocation_size = stack_size
.checked_mul(max_instances)
.ok_or_else(|| anyhow!("total size of execution stacks exceeds addressable memory"))?;
let mapping = create_memory_map(allocation_size, allocation_size)?;
// Set up the stack guard pages
unsafe {
for i in 0..max_instances {
// Make the stack guard page inaccessible
let bottom_of_stack = mapping.as_mut_ptr().add(i * stack_size);
region::protect(bottom_of_stack, page_size, region::Protection::NONE)
.context("failed to protect stack guard page")?;
}
}
Ok(Self {
mapping: create_memory_map(0, allocation_size)?,
mapping,
stack_size,
max_instances,
page_size,
free_list: Mutex::new((0..max_instances).collect()),
#[cfg(all(feature = "uffd", target_os = "linux"))]
faulted_guard_pages: std::iter::repeat_with(|| false.into())
.take(max_instances)
.collect::<Vec<_>>()
.into(),
})
}
@@ -789,37 +804,8 @@ impl StackPool {
debug_assert!(index < self.max_instances);
unsafe {
// Remove the guard page from the size
let size_without_guard = self.stack_size - self.page_size;
let bottom_of_stack = self
.mapping
.as_mut_ptr()
.add((index * self.stack_size) + self.page_size);
cfg_if::cfg_if! {
if #[cfg(all(feature = "uffd", target_os = "linux"))] {
// Check to see if a guard page needs to be reset
if self.faulted_guard_pages[index].swap(false, Ordering::SeqCst) {
if !reset_guard_page(bottom_of_stack.sub(self.page_size), self.page_size) {
return Err(FiberStackError::Resource(
"failed to reset stack guard page".into(),
));
}
}
} else {
// Make the stack accessible (excluding the guard page)
if !make_accessible(bottom_of_stack, size_without_guard) {
return Err(FiberStackError::Resource(
"failed to make instance memory accessible".into(),
));
}
}
}
// The top of the stack should be returned
Ok(bottom_of_stack.add(size_without_guard))
// The top (end) of the stack should be returned
Ok(self.mapping.as_mut_ptr().add((index + 1) * self.stack_size))
}
}
@@ -872,9 +858,9 @@ impl PoolingInstanceAllocator {
module_limits: ModuleLimits,
mut instance_limits: InstanceLimits,
stack_size: usize,
) -> Result<Self, String> {
) -> Result<Self> {
if instance_limits.count == 0 {
return Err("the instance count limit cannot be zero".into());
bail!("the instance count limit cannot be zero");
}
// Round the memory reservation size to the nearest Wasm page size
@@ -890,28 +876,28 @@ impl PoolingInstanceAllocator {
// The maximum module memory page count cannot exceed 65536 pages
if module_limits.memory_pages > 0x10000 {
return Err(format!(
bail!(
"module memory page limit of {} exceeds the maximum of 65536",
module_limits.memory_pages
));
);
}
// The maximum module memory page count cannot exceed the memory reservation size
if (module_limits.memory_pages * WASM_PAGE_SIZE) as u64
> instance_limits.memory_reservation_size
{
return Err(format!(
bail!(
"module memory page limit of {} pages exeeds the memory reservation size limit of {} bytes",
module_limits.memory_pages,
instance_limits.memory_reservation_size
));
);
}
let instances = InstancePool::new(&module_limits, &instance_limits)?;
let stacks = StackPool::new(&instance_limits, stack_size)?;
#[cfg(all(feature = "uffd", target_os = "linux"))]
let _fault_handler = PageFaultHandler::new(&instances, &stacks)?;
let _fault_handler = PageFaultHandler::new(&instances)?;
Ok(Self {
strategy,
@@ -937,8 +923,8 @@ impl Drop for PoolingInstanceAllocator {
}
unsafe impl InstanceAllocator for PoolingInstanceAllocator {
fn validate_module(&self, translation: &ModuleTranslation) -> Result<(), String> {
self.module_limits.validate_module(&translation.module)
fn validate(&self, module: &Module) -> Result<()> {
self.module_limits.validate(module)
}
fn adjust_tunables(&self, tunables: &mut Tunables) {
@@ -976,8 +962,8 @@ unsafe impl InstanceAllocator for PoolingInstanceAllocator {
cfg_if::cfg_if! {
if #[cfg(all(feature = "uffd", target_os = "linux"))] {
match instance.module.memory_initialization {
Some(MemoryInitialization::Paged{ .. }) => {
match &instance.module.memory_initialization {
MemoryInitialization::Paged{ out_of_bounds, .. } => {
if !is_bulk_memory {
check_init_bounds(instance)?;
}
@@ -985,7 +971,15 @@ unsafe impl InstanceAllocator for PoolingInstanceAllocator {
// Initialize the tables
initialize_tables(instance)?;
// Don't initialize the memory; the fault handler will fill the pages when accessed
// Don't initialize the memory; the fault handler will back the pages when accessed
// If there was an out of bounds access observed in initialization, return a trap
if *out_of_bounds {
return Err(InstantiationError::Trap(crate::traphandlers::Trap::wasm(
wasmtime_environ::ir::TrapCode::HeapOutOfBounds,
)));
}
Ok(())
},
_ => initialize_instance(instance, is_bulk_memory)
@@ -1030,11 +1024,11 @@ mod test {
let mut module = Module::default();
module.functions.push(SignatureIndex::new(0));
assert_eq!(limits.validate_module(&module), Ok(()));
assert!(limits.validate(&module).is_ok());
module.num_imported_funcs = 1;
assert_eq!(
limits.validate_module(&module),
limits.validate(&module).map_err(|e| e.to_string()),
Err("imported function count of 1 exceeds the limit of 0".into())
);
}
@@ -1058,11 +1052,11 @@ mod test {
},
});
assert_eq!(limits.validate_module(&module), Ok(()));
assert!(limits.validate(&module).is_ok());
module.num_imported_tables = 1;
assert_eq!(
limits.validate_module(&module),
limits.validate(&module).map_err(|e| e.to_string()),
Err("imported tables count of 1 exceeds the limit of 0".into())
);
}
@@ -1086,11 +1080,11 @@ mod test {
offset_guard_size: 0,
});
assert_eq!(limits.validate_module(&module), Ok(()));
assert!(limits.validate(&module).is_ok());
module.num_imported_memories = 1;
assert_eq!(
limits.validate_module(&module),
limits.validate(&module).map_err(|e| e.to_string()),
Err("imported memories count of 1 exceeds the limit of 0".into())
);
}
@@ -1111,11 +1105,11 @@ mod test {
initializer: GlobalInit::I32Const(0),
});
assert_eq!(limits.validate_module(&module), Ok(()));
assert!(limits.validate(&module).is_ok());
module.num_imported_globals = 1;
assert_eq!(
limits.validate_module(&module),
limits.validate(&module).map_err(|e| e.to_string()),
Err("imported globals count of 1 exceeds the limit of 0".into())
);
}
@@ -1128,13 +1122,13 @@ mod test {
};
let mut module = Module::default();
assert_eq!(limits.validate_module(&module), Ok(()));
assert!(limits.validate(&module).is_ok());
module
.types
.push(ModuleType::Function(SignatureIndex::new(0)));
assert_eq!(
limits.validate_module(&module),
limits.validate(&module).map_err(|e| e.to_string()),
Err("defined types count of 1 exceeds the limit of 0".into())
);
}
@@ -1147,11 +1141,11 @@ mod test {
};
let mut module = Module::default();
assert_eq!(limits.validate_module(&module), Ok(()));
assert!(limits.validate(&module).is_ok());
module.functions.push(SignatureIndex::new(0));
assert_eq!(
limits.validate_module(&module),
limits.validate(&module).map_err(|e| e.to_string()),
Err("defined functions count of 1 exceeds the limit of 0".into())
);
}
@@ -1164,7 +1158,7 @@ mod test {
};
let mut module = Module::default();
assert_eq!(limits.validate_module(&module), Ok(()));
assert!(limits.validate(&module).is_ok());
module.table_plans.push(TablePlan {
style: TableStyle::CallerChecksSignature,
@@ -1176,7 +1170,7 @@ mod test {
},
});
assert_eq!(
limits.validate_module(&module),
limits.validate(&module).map_err(|e| e.to_string()),
Err("defined tables count of 1 exceeds the limit of 0".into())
);
}
@@ -1189,7 +1183,7 @@ mod test {
};
let mut module = Module::default();
assert_eq!(limits.validate_module(&module), Ok(()));
assert!(limits.validate(&module).is_ok());
module.memory_plans.push(MemoryPlan {
style: MemoryStyle::Static { bound: 0 },
@@ -1201,7 +1195,7 @@ mod test {
offset_guard_size: 0,
});
assert_eq!(
limits.validate_module(&module),
limits.validate(&module).map_err(|e| e.to_string()),
Err("defined memories count of 1 exceeds the limit of 0".into())
);
}
@@ -1214,7 +1208,7 @@ mod test {
};
let mut module = Module::default();
assert_eq!(limits.validate_module(&module), Ok(()));
assert!(limits.validate(&module).is_ok());
module.globals.push(Global {
wasm_ty: WasmType::I32,
@@ -1223,7 +1217,7 @@ mod test {
initializer: GlobalInit::I32Const(0),
});
assert_eq!(
limits.validate_module(&module),
limits.validate(&module).map_err(|e| e.to_string()),
Err("defined globals count of 1 exceeds the limit of 0".into())
);
}
@@ -1247,7 +1241,7 @@ mod test {
},
});
assert_eq!(
limits.validate_module(&module),
limits.validate(&module).map_err(|e| e.to_string()),
Err(
"table index 0 has a minimum element size of 11 which exceeds the limit of 10"
.into()
@@ -1274,7 +1268,7 @@ mod test {
offset_guard_size: 0,
});
assert_eq!(
limits.validate_module(&module),
limits.validate(&module).map_err(|e| e.to_string()),
Err("memory index 0 has a minimum page size of 6 which exceeds the limit of 5".into())
);
}
@@ -1298,7 +1292,7 @@ mod test {
offset_guard_size: 0,
});
assert_eq!(
limits.validate_module(&module),
limits.validate(&module).map_err(|e| e.to_string()),
Err("memory index 0 has an unsupported dynamic memory plan style".into())
);
}
@@ -1335,7 +1329,7 @@ mod test {
#[cfg(target_pointer_width = "64")]
#[test]
fn test_instance_pool() -> Result<(), String> {
fn test_instance_pool() -> Result<()> {
let module_limits = ModuleLimits {
imported_functions: 0,
imported_tables: 0,
@@ -1372,13 +1366,7 @@ mod test {
assert_eq!(instances.instance_size, 4096);
assert_eq!(instances.max_instances, 3);
assert_eq!(
&*instances
.free_list
.lock()
.map_err(|_| "failed to lock".to_string())?,
&[0, 1, 2],
);
assert_eq!(&*instances.free_list.lock().unwrap(), &[0, 1, 2],);
let mut handles = Vec::new();
let module = Arc::new(Module::default());
@@ -1409,13 +1397,7 @@ mod test {
);
}
assert_eq!(
&*instances
.free_list
.lock()
.map_err(|_| "failed to lock".to_string())?,
&[],
);
assert_eq!(&*instances.free_list.lock().unwrap(), &[],);
match instances.allocate(
PoolingAllocationStrategy::NextAvailable,
@@ -1443,20 +1425,14 @@ mod test {
instances.deallocate(&handle);
}
assert_eq!(
&*instances
.free_list
.lock()
.map_err(|_| "failed to lock".to_string())?,
&[2, 1, 0],
);
assert_eq!(&*instances.free_list.lock().unwrap(), &[2, 1, 0],);
Ok(())
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_memory_pool() -> Result<(), String> {
fn test_memory_pool() -> Result<()> {
let pool = MemoryPool::new(
&ModuleLimits {
imported_functions: 0,
@@ -1502,7 +1478,7 @@ mod test {
#[cfg(target_pointer_width = "64")]
#[test]
fn test_table_pool() -> Result<(), String> {
fn test_table_pool() -> Result<()> {
let pool = TablePool::new(
&ModuleLimits {
imported_functions: 0,
@@ -1549,7 +1525,7 @@ mod test {
#[cfg(all(unix, target_pointer_width = "64"))]
#[test]
fn test_stack_pool() -> Result<(), String> {
fn test_stack_pool() -> Result<()> {
let pool = StackPool::new(
&InstanceLimits {
count: 10,
@@ -1563,10 +1539,7 @@ mod test {
assert_eq!(pool.page_size, 4096);
assert_eq!(
&*pool
.free_list
.lock()
.map_err(|_| "failed to lock".to_string())?,
&*pool.free_list.lock().unwrap(),
&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
);
@@ -1581,13 +1554,7 @@ mod test {
stacks.push(stack);
}
assert_eq!(
&*pool
.free_list
.lock()
.map_err(|_| "failed to lock".to_string())?,
&[],
);
assert_eq!(&*pool.free_list.lock().unwrap(), &[],);
match pool
.allocate(PoolingAllocationStrategy::NextAvailable)
@@ -1602,10 +1569,7 @@ mod test {
}
assert_eq!(
&*pool
.free_list
.lock()
.map_err(|_| "failed to lock".to_string())?,
&*pool.free_list.lock().unwrap(),
&[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
);
@@ -1624,6 +1588,7 @@ mod test {
},
4096
)
.map_err(|e| e.to_string())
.expect_err("expected a failure constructing instance allocator"),
"the instance count limit cannot be zero"
);
@@ -1644,6 +1609,7 @@ mod test {
},
4096
)
.map_err(|e| e.to_string())
.expect_err("expected a failure constructing instance allocator"),
"module memory page limit of 65537 exceeds the maximum of 65536"
);
@@ -1664,6 +1630,7 @@ mod test {
},
4096,
)
.map_err(|e| e.to_string())
.expect_err("expected a failure constructing instance allocator"),
"module memory page limit of 2 pages exeeds the memory reservation size limit of 65536 bytes"
);
@@ -1672,7 +1639,7 @@ mod test {
#[cfg_attr(target_arch = "aarch64", ignore)] // https://github.com/bytecodealliance/wasmtime/pull/2518#issuecomment-747280133
#[cfg(all(unix, target_pointer_width = "64"))]
#[test]
fn test_stack_zeroed() -> Result<(), String> {
fn test_stack_zeroed() -> Result<()> {
let allocator = PoolingInstanceAllocator::new(
PoolingAllocationStrategy::NextAvailable,
ModuleLimits {
@@ -1695,9 +1662,7 @@ mod test {
unsafe {
for _ in 0..10 {
let stack = allocator
.allocate_fiber_stack()
.map_err(|e| format!("failed to allocate stack: {}", e))?;
let stack = allocator.allocate_fiber_stack()?;
// The stack pointer is at the top, so decerement it first
let addr = stack.sub(1);

View File

@@ -1,4 +1,5 @@
use crate::Mmap;
use anyhow::{anyhow, Result};
pub unsafe fn make_accessible(addr: *mut u8, len: usize) -> bool {
region::protect(addr, len, region::Protection::READ_WRITE).is_ok()
@@ -16,7 +17,7 @@ pub unsafe fn decommit(addr: *mut u8, len: usize) {
);
}
pub fn create_memory_map(accessible_size: usize, mapping_size: usize) -> Result<Mmap, String> {
pub fn create_memory_map(accessible_size: usize, mapping_size: usize) -> Result<Mmap> {
Mmap::accessible_reserved(accessible_size, mapping_size)
.map_err(|e| format!("failed to allocate pool memory: {}", e))
.map_err(|e| anyhow!("failed to allocate pool memory: {}", e))
}

View File

@@ -1,23 +1,40 @@
//! Implements user space page fault handling with the `userfaultfd` ("uffd") system call on Linux.
//! This module implements user space page fault handling with the `userfaultfd` ("uffd") system call on Linux.
//!
//! Handling page faults for memory accesses in regions relating to WebAssembly instances
//! enables the implementation of protecting guard pages in user space rather than kernel space.
//! enables the runtime to protect guard pages in user space rather than kernel space (i.e. without `mprotect`).
//!
//! This reduces the number of system calls and kernel locks needed to provide correct
//! WebAssembly memory semantics.
//! Additionally, linear memories can be lazy-initialized upon first access.
//!
//! Additionally, linear memories can be lazy-initialized upon access.
//! Handling faults in user space is slower than handling faults in the kernel. However,
//! in use cases where there is a high number of concurrently executing instances, handling the faults
//! in user space requires rarely changing memory protection levels. This can improve concurrency
//! by not taking kernel memory manager locks and may decrease TLB shootdowns as fewer page table entries need
//! to continually change.
//!
//! Here's how the `uffd` feature works:
//!
//! 1. A user fault file descriptor is created to monitor specific areas of the address space.
//! 2. A thread is spawned to continually read events from the user fault file descriptor.
//! 3. When a page fault event is received, the handler thread calculates where the fault occurred:
//! a) If the fault occurs on a table page, it is handled by zeroing the page.
//! b) If the fault occurs on a linear memory page, it is handled by either copying the page from
//! initialization data or zeroing it.
//! c) If the fault occurs on a guard page, the protection level of the guard page is changed to
//! force the kernel to signal SIGSEV on the next retry. The faulting page is recorded so the
//! protection level can be reset in the future.
//! 4. Faults to address space relating to an instance may occur from both Wasmtime (e.g. instance
//! initialization) or from WebAssembly code (e.g. reading from or writing to linear memory),
//! therefore the user fault handling must do as little work as possible to handle the fault.
//! 5. When the pooling allocator is dropped, it will drop the memory mappings relating to the pool; this
//! generates unmap events for the fault handling thread, which responds by decrementing the mapping
//! count. When the count reaches zero, the user fault handling thread will gracefully terminate.
//!
//! This feature requires a Linux kernel 4.11 or newer to use.
use super::{InstancePool, StackPool};
use super::InstancePool;
use crate::{instance::Instance, Mmap};
use std::convert::TryInto;
use anyhow::{bail, Context, Result};
use std::ptr;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use std::thread;
use userfaultfd::{Event, FeatureFlags, IoctlFlags, Uffd, UffdBuilder};
use wasmtime_environ::{entity::EntityRef, wasm::DefinedMemoryIndex, MemoryInitialization};
@@ -45,11 +62,11 @@ pub unsafe fn decommit(addr: *mut u8, len: usize) {
);
}
pub fn create_memory_map(_accessible_size: usize, mapping_size: usize) -> Result<Mmap, String> {
pub fn create_memory_map(_accessible_size: usize, mapping_size: usize) -> Result<Mmap> {
// Allocate a single read-write region at once
// As writable pages need to count towards commit charge, use MAP_NORESERVE to override.
// This implies that the kernel is configured to allow overcommit or else
// this allocation will almost certainly fail without a plethora of physical memory to back the allocation.
// This implies that the kernel is configured to allow overcommit or else this allocation
// will almost certainly fail without a plethora of physical memory to back the allocation.
// The consequence of not reserving is that our process may segfault on any write to a memory
// page that cannot be backed (i.e. out of memory conditions).
@@ -68,10 +85,10 @@ pub fn create_memory_map(_accessible_size: usize, mapping_size: usize) -> Result
);
if ptr as isize == -1_isize {
return Err(format!(
"failed to allocate pool memory: {}",
bail!(
"failed to allocate pool memory: mmap failed with {}",
std::io::Error::last_os_error()
));
);
}
Ok(Mmap::from_raw(ptr as usize, mapping_size))
@@ -98,22 +115,10 @@ enum AddressLocation<'a> {
/// The instance related to the memory page that was accessed.
instance: &'a Instance,
/// The index of the memory that was accessed.
memory_index: usize,
memory_index: DefinedMemoryIndex,
/// The Wasm page index to initialize if the access was not a guard page.
page_index: Option<usize>,
},
/// The address location is in an execution stack.
/// The fault handler will zero the page.
StackPage {
/// The address of the page being accessed.
page_addr: *mut u8,
/// The length of the page being accessed.
len: usize,
/// The index of the stack that was accessed.
index: usize,
/// Whether or not the access was to a guard page.
guard_page: bool,
},
}
/// Used to resolve fault addresses to address locations.
@@ -132,22 +137,16 @@ struct AddressLocator {
tables_start: usize,
tables_end: usize,
table_size: usize,
stacks_start: usize,
stacks_end: usize,
stack_size: usize,
page_size: usize,
}
impl AddressLocator {
fn new(instances: &InstancePool, stacks: &StackPool) -> Self {
fn new(instances: &InstancePool) -> Self {
let instances_start = instances.mapping.as_ptr() as usize;
let memories_start = instances.memories.mapping.as_ptr() as usize;
let memories_end = memories_start + instances.memories.mapping.len();
let tables_start = instances.tables.mapping.as_ptr() as usize;
let tables_end = tables_start + instances.tables.mapping.len();
let stacks_start = stacks.mapping.as_ptr() as usize;
let stacks_end = stacks_start + stacks.mapping.len();
let stack_size = stacks.stack_size;
// Should always have instances
debug_assert!(instances_start != 0);
@@ -163,9 +162,6 @@ impl AddressLocator {
tables_start,
tables_end,
table_size: instances.tables.table_size,
stacks_start,
stacks_end,
stack_size,
page_size: instances.tables.page_size,
}
}
@@ -191,25 +187,18 @@ impl AddressLocator {
// Check for a memory location
if addr >= self.memories_start && addr < self.memories_end {
let index = (addr - self.memories_start) / self.memory_size;
let memory_index = index % self.max_memories;
let memory_index = DefinedMemoryIndex::new(index % self.max_memories);
let memory_start = self.memories_start + (index * self.memory_size);
let page_index = (addr - memory_start) / WASM_PAGE_SIZE;
let instance = self.get_instance(index / self.max_memories);
let init_page_index = instance
.memories
.get(
DefinedMemoryIndex::from_u32(memory_index as u32)
.try_into()
.unwrap(),
)
.and_then(|m| {
if page_index < m.size() as usize {
Some(page_index)
} else {
None
}
});
let init_page_index = instance.memories.get(memory_index).and_then(|m| {
if page_index < m.size() as usize {
Some(page_index)
} else {
None
}
});
return Some(AddressLocation::MemoryPage {
page_addr: (memory_start + page_index * WASM_PAGE_SIZE) as _,
@@ -233,128 +222,125 @@ impl AddressLocator {
});
}
// Check for a stack location
if addr >= self.stacks_start && addr < self.stacks_end {
let index = (addr - self.stacks_start) / self.stack_size;
let stack_start = self.stacks_start + (index * self.stack_size);
let stack_offset = addr - stack_start;
let page_offset = (stack_offset / self.page_size) * self.page_size;
return Some(AddressLocation::StackPage {
page_addr: (stack_start + page_offset) as _,
len: self.page_size,
index,
guard_page: stack_offset < self.page_size,
});
}
None
}
}
unsafe fn wake_guard_page_access(
uffd: &Uffd,
page_addr: *const u8,
len: usize,
) -> Result<(), String> {
// Set the page to NONE to induce a SIGSEV for the access on the next retry
/// This is called following a fault on a guard page.
///
/// Because the region being monitored is protected read-write, this needs to set the
/// protection level to `NONE` before waking the page.
///
/// This will cause the kernel to raise a SIGSEGV when retrying the fault.
unsafe fn wake_guard_page_access(uffd: &Uffd, page_addr: *const u8, len: usize) -> Result<()> {
// Set the page to NONE to induce a SIGSEGV for the access on the next retry
region::protect(page_addr, len, region::Protection::NONE)
.map_err(|e| format!("failed to change guard page protection: {}", e))?;
.context("failed to change guard page protection")?;
uffd.wake(page_addr as _, len).map_err(|e| {
format!(
"failed to wake page at {:p} with length {}: {}",
page_addr, len, e
)
})?;
uffd.wake(page_addr as _, len)
.context("failed to wake guard page access")?;
Ok(())
}
/// This is called to initialize a linear memory page (64 KiB).
///
/// If paged initialization is used for the module, then we can instruct the kernel to back the page with
/// what is already stored in the initialization data; if the page isn't in the initialization data,
/// it will be zeroed instead.
///
/// If paged initialization isn't being used, we zero the page. Initialization happens
/// at module instantiation in this case and the segment data will be then copied to the zeroed page.
unsafe fn initialize_wasm_page(
uffd: &Uffd,
instance: &Instance,
page_addr: *const u8,
memory_index: usize,
memory_index: DefinedMemoryIndex,
page_index: usize,
) -> Result<(), String> {
if let Some(MemoryInitialization::Paged { page_size, map }) =
&instance.module.memory_initialization
{
let memory_index = DefinedMemoryIndex::new(memory_index);
let memory = instance.memory(memory_index);
) -> Result<()> {
// Check for paged initialization and copy the page if present in the initialization data
if let MemoryInitialization::Paged { map, .. } = &instance.module.memory_initialization {
let pages = &map[memory_index];
debug_assert_eq!(WASM_PAGE_SIZE % page_size, 0);
let count = WASM_PAGE_SIZE / page_size;
let start = page_index * count;
if let Some(Some(data)) = pages.get(page_index) {
debug_assert_eq!(data.len(), WASM_PAGE_SIZE);
for i in start..start + count {
let dst = memory.base.add(i * page_size);
log::trace!(
"copying linear memory page from {:p} to {:p}",
data.as_ptr(),
page_addr
);
match pages.get(i) {
Some(Some(data)) => {
log::trace!(
"copying page initialization data from {:p} to {:p} with length {}",
data,
dst,
page_size
);
uffd.copy(data.as_ptr() as _, page_addr as _, WASM_PAGE_SIZE, true)
.context("failed to copy linear memory page")?;
// Copy the page data without waking
uffd.copy(data.as_ptr() as _, dst as _, *page_size, false)
.map_err(|e| {
format!(
"failed to copy page from {:p} to {:p} with length {}: {}",
data, dst, page_size, e
)
})?;
return Ok(());
}
}
log::trace!("zeroing linear memory page at {:p}", page_addr);
uffd.zeropage(page_addr as _, WASM_PAGE_SIZE, true)
.context("failed to zero linear memory page")?;
Ok(())
}
unsafe fn handle_page_fault(
uffd: &Uffd,
locator: &AddressLocator,
addr: *mut std::ffi::c_void,
) -> Result<()> {
match locator.get_location(addr as usize) {
Some(AddressLocation::TablePage { page_addr, len }) => {
log::trace!(
"handling fault in table at address {:p} on page {:p}",
addr,
page_addr,
);
// Tables are always initialized upon instantiation, so zero the page
uffd.zeropage(page_addr as _, len, true)
.context("failed to zero table page")?;
}
Some(AddressLocation::MemoryPage {
page_addr,
len,
instance,
memory_index,
page_index,
}) => {
log::trace!(
"handling fault in linear memory at address {:p} on page {:p}",
addr,
page_addr
);
match page_index {
Some(page_index) => {
initialize_wasm_page(&uffd, instance, page_addr, memory_index, page_index)?;
}
_ => {
log::trace!("zeroing page at {:p} with length {}", dst, page_size);
None => {
log::trace!("out of bounds memory access at {:p}", addr);
// No data, zero the page without waking
uffd.zeropage(dst as _, *page_size, false).map_err(|e| {
format!(
"failed to zero page at {:p} with length {}: {}",
dst, page_size, e
)
})?;
// Record the guard page fault with the instance so it can be reset later.
instance.record_guard_page_fault(page_addr, len, reset_guard_page);
wake_guard_page_access(&uffd, page_addr, len)?;
}
}
}
// Finally wake the entire wasm page
uffd.wake(page_addr as _, WASM_PAGE_SIZE).map_err(|e| {
format!(
"failed to wake page at {:p} with length {}: {}",
page_addr, WASM_PAGE_SIZE, e
)
})
} else {
log::trace!(
"initialization data is not paged; zeroing Wasm page at {:p}",
page_addr
);
uffd.zeropage(page_addr as _, WASM_PAGE_SIZE, true)
.map_err(|e| {
format!(
"failed to zero page at {:p} with length {}: {}",
page_addr, WASM_PAGE_SIZE, e
)
})?;
Ok(())
None => {
bail!(
"failed to locate fault address {:p} in registered memory regions",
addr
);
}
}
Ok(())
}
fn handler_thread(
uffd: Uffd,
locator: AddressLocator,
mut registrations: usize,
faulted_stack_guard_pages: Arc<[AtomicBool]>,
) -> Result<(), String> {
fn handler_thread(uffd: Uffd, locator: AddressLocator, mut registrations: usize) -> Result<()> {
loop {
match uffd.read_event().expect("failed to read event") {
Some(Event::Unmap { start, end }) => {
@@ -364,7 +350,6 @@ fn handler_thread(
if (start == locator.memories_start && end == locator.memories_end)
|| (start == locator.tables_start && end == locator.tables_end)
|| (start == locator.stacks_start && end == locator.stacks_end)
{
registrations -= 1;
if registrations == 0 {
@@ -374,104 +359,11 @@ fn handler_thread(
panic!("unexpected memory region unmapped");
}
}
Some(Event::Pagefault {
addr: access_addr, ..
}) => {
unsafe {
match locator.get_location(access_addr as usize) {
Some(AddressLocation::TablePage { page_addr, len }) => {
log::trace!(
"handling fault in table at address {:p} on page {:p}",
access_addr,
page_addr,
);
// Tables are always initialized upon instantiation, so zero the page
uffd.zeropage(page_addr as _, len, true).map_err(|e| {
format!(
"failed to zero page at {:p} with length {}: {}",
page_addr, len, e
)
})?;
}
Some(AddressLocation::MemoryPage {
page_addr,
len,
instance,
memory_index,
page_index,
}) => {
log::trace!(
"handling fault in linear memory at address {:p} on page {:p}",
access_addr,
page_addr
);
match page_index {
Some(page_index) => {
initialize_wasm_page(
&uffd,
instance,
page_addr,
memory_index,
page_index,
)?;
}
None => {
log::trace!("out of bounds memory access at {:p}", access_addr);
// Record the guard page fault with the instance so it can be reset later.
instance.record_guard_page_fault(
page_addr,
len,
reset_guard_page,
);
wake_guard_page_access(&uffd, page_addr, len)?;
}
}
}
Some(AddressLocation::StackPage {
page_addr,
len,
index,
guard_page,
}) => {
log::trace!(
"handling fault in stack {} at address {:p}",
index,
access_addr,
);
if guard_page {
// Logging as trace as stack guard pages might be a trap condition in the future
log::trace!("stack overflow fault at {:p}", access_addr);
// Mark the stack as having a faulted guard page
// The next time the stack is used the guard page will be reset
faulted_stack_guard_pages[index].store(true, Ordering::SeqCst);
wake_guard_page_access(&uffd, page_addr, len)?;
continue;
}
// Always zero stack pages
uffd.zeropage(page_addr as _, len, true).map_err(|e| {
format!(
"failed to zero page at {:p} with length {}: {}",
page_addr, len, e
)
})?;
}
None => {
return Err(format!(
"failed to locate fault address {:p} in registered memory regions",
access_addr
));
}
}
}
}
Some(Event::Pagefault { addr, .. }) => unsafe {
handle_page_fault(&uffd, &locator, addr as _)?
},
Some(_) => continue,
None => break,
None => bail!("no event was read from the user fault descriptor"),
}
}
@@ -482,16 +374,16 @@ fn handler_thread(
#[derive(Debug)]
pub struct PageFaultHandler {
thread: Option<thread::JoinHandle<Result<(), String>>>,
thread: Option<thread::JoinHandle<Result<()>>>,
}
impl PageFaultHandler {
pub(super) fn new(instances: &InstancePool, stacks: &StackPool) -> Result<Self, String> {
pub(super) fn new(instances: &InstancePool) -> Result<Self> {
let uffd = UffdBuilder::new()
.close_on_exec(true)
.require_features(FeatureFlags::EVENT_UNMAP)
.create()
.map_err(|e| format!("failed to create user fault descriptor: {}", e))?;
.context("failed to create user fault descriptor")?;
// Register the ranges with the userfault fd
let mut registrations = 0;
@@ -504,7 +396,6 @@ impl PageFaultHandler {
instances.tables.mapping.as_ptr() as usize,
instances.tables.mapping.len(),
),
(stacks.mapping.as_ptr() as usize, stacks.mapping.len()),
] {
if *start == 0 || *len == 0 {
continue;
@@ -512,13 +403,13 @@ impl PageFaultHandler {
let ioctls = uffd
.register(*start as _, *len)
.map_err(|e| format!("failed to register user fault range: {}", e))?;
.context("failed to register user fault range")?;
if !ioctls.contains(IoctlFlags::WAKE | IoctlFlags::COPY | IoctlFlags::ZEROPAGE) {
return Err(format!(
bail!(
"required user fault ioctls not supported; found: {:?}",
ioctls,
));
);
}
registrations += 1;
@@ -533,17 +424,13 @@ impl PageFaultHandler {
registrations
);
let locator = AddressLocator::new(&instances, &stacks);
let faulted_stack_guard_pages = stacks.faulted_guard_pages.clone();
let locator = AddressLocator::new(&instances);
Some(
thread::Builder::new()
.name("page fault handler".into())
.spawn(move || {
handler_thread(uffd, locator, registrations, faulted_stack_guard_pages)
})
.map_err(|e| format!("failed to spawn page fault handler thread: {}", e))?,
.spawn(move || handler_thread(uffd, locator, registrations))
.context("failed to spawn page fault handler thread")?,
)
};
@@ -553,6 +440,9 @@ impl PageFaultHandler {
impl Drop for PageFaultHandler {
fn drop(&mut self) {
// The handler thread should terminate once all monitored regions of memory are unmapped.
// The pooling instance allocator ensures that the regions are unmapped prior to dropping
// the user fault handler.
if let Some(thread) = self.thread.take() {
thread
.join()
@@ -569,6 +459,7 @@ mod test {
table::max_table_element_size, Imports, InstanceAllocationRequest, InstanceLimits,
ModuleLimits, PoolingAllocationStrategy, VMSharedSignatureIndex,
};
use std::sync::Arc;
use wasmtime_environ::{
entity::PrimaryMap,
wasm::{Memory, Table, TableElementType, WasmType},
@@ -598,9 +489,8 @@ mod test {
let instances =
InstancePool::new(&module_limits, &instance_limits).expect("should allocate");
let stacks = StackPool::new(&instance_limits, 8192).expect("should allocate");
let locator = AddressLocator::new(&instances, &stacks);
let locator = AddressLocator::new(&instances);
assert_eq!(locator.instances_start, instances.mapping.as_ptr() as usize);
assert_eq!(locator.instance_size, 4096);
@@ -625,20 +515,10 @@ mod test {
);
assert_eq!(locator.table_size, 8192);
assert_eq!(locator.stacks_start, stacks.mapping.as_ptr() as usize);
assert_eq!(
locator.stacks_end,
locator.stacks_start + stacks.mapping.len()
);
assert_eq!(locator.stack_size, 12288);
unsafe {
assert!(locator.get_location(0).is_none());
assert!(locator
.get_location(std::cmp::max(
locator.memories_end,
std::cmp::max(locator.tables_end, locator.stacks_end)
))
.get_location(std::cmp::max(locator.memories_end, locator.tables_end))
.is_none());
let mut module = Module::new();
@@ -667,9 +547,7 @@ mod test {
});
}
module_limits
.validate_module(&module)
.expect("should validate");
module_limits.validate(&module).expect("should validate");
let mut handles = Vec::new();
let module = Arc::new(module);
@@ -719,7 +597,7 @@ mod test {
}) => {
assert_eq!(page_addr, memory_start as _);
assert_eq!(len, WASM_PAGE_SIZE);
assert_eq!(mem_index, memory_index);
assert_eq!(mem_index, DefinedMemoryIndex::new(memory_index));
assert_eq!(page_index, Some(0));
}
_ => panic!("expected a memory page location"),
@@ -736,7 +614,7 @@ mod test {
}) => {
assert_eq!(page_addr, (memory_start + WASM_PAGE_SIZE) as _);
assert_eq!(len, WASM_PAGE_SIZE);
assert_eq!(mem_index, memory_index);
assert_eq!(mem_index, DefinedMemoryIndex::new(memory_index));
assert_eq!(page_index, Some(1));
}
_ => panic!("expected a memory page location"),
@@ -753,7 +631,7 @@ mod test {
}) => {
assert_eq!(page_addr, (memory_start + (9 * WASM_PAGE_SIZE)) as _);
assert_eq!(len, WASM_PAGE_SIZE);
assert_eq!(mem_index, memory_index);
assert_eq!(mem_index, DefinedMemoryIndex::new(memory_index));
assert_eq!(page_index, None);
}
_ => panic!("expected a memory page location"),
@@ -788,43 +666,6 @@ mod test {
}
}
// Validate stack locations
for stack_index in 0..instances.max_instances {
let stack_start = locator.stacks_start + (stack_index * locator.stack_size);
// Check for stack page location
match locator.get_location(stack_start + locator.page_size * 2) {
Some(AddressLocation::StackPage {
page_addr,
len,
index,
guard_page,
}) => {
assert_eq!(page_addr, (stack_start + locator.page_size * 2) as _);
assert_eq!(len, locator.page_size);
assert_eq!(index, stack_index);
assert!(!guard_page);
}
_ => panic!("expected a stack page location"),
}
// Check for guard page
match locator.get_location(stack_start) {
Some(AddressLocation::StackPage {
page_addr,
len,
index,
guard_page,
}) => {
assert_eq!(page_addr, stack_start as _);
assert_eq!(len, locator.page_size);
assert_eq!(index, stack_index);
assert!(guard_page);
}
_ => panic!("expected a stack page location"),
}
}
for handle in handles.drain(..) {
instances.deallocate(&handle);
}

View File

@@ -1,4 +1,5 @@
use crate::Mmap;
use anyhow::{anyhow, Result};
pub unsafe fn make_accessible(addr: *mut u8, len: usize) -> bool {
region::protect(addr, len, region::Protection::READ_WRITE).is_ok()
@@ -20,7 +21,7 @@ pub unsafe fn decommit(addr: *mut u8, len: usize) {
);
}
pub fn create_memory_map(accessible_size: usize, mapping_size: usize) -> Result<Mmap, String> {
pub fn create_memory_map(accessible_size: usize, mapping_size: usize) -> Result<Mmap> {
Mmap::accessible_reserved(accessible_size, mapping_size)
.map_err(|e| format!("failed to allocate pool memory: {}", e))
.map_err(|e| anyhow!("failed to allocate pool memory: {}", e))
}

View File

@@ -1,4 +1,5 @@
use crate::Mmap;
use anyhow::{anyhow, Result};
use winapi::um::memoryapi::{VirtualAlloc, VirtualFree};
use winapi::um::winnt::{MEM_COMMIT, MEM_DECOMMIT, PAGE_READWRITE};
@@ -15,7 +16,7 @@ pub unsafe fn decommit(addr: *mut u8, len: usize) {
);
}
pub fn create_memory_map(accessible_size: usize, mapping_size: usize) -> Result<Mmap, String> {
pub fn create_memory_map(accessible_size: usize, mapping_size: usize) -> Result<Mmap> {
Mmap::accessible_reserved(accessible_size, mapping_size)
.map_err(|e| format!("failed to allocate pool memory: {}", e))
.map_err(|e| anyhow!("failed to allocate pool memory: {}", e))
}