Implement memory.grow and memory.current (#9)
* Implement. * Clean and doc * Collect base addresses instead of leaking them * Fix code for 1.25. * Simplify $assert * Use AbiParam::special. * Use &mut self in base_addr
This commit is contained in:
committed by
Dan Gohman
parent
5379605737
commit
e7c8d23a42
@@ -12,3 +12,4 @@ cranelift-codegen = "0.18.1"
|
||||
cranelift-wasm = "0.18.1"
|
||||
region = "0.3.0"
|
||||
wasmtime-environ = { path = "../environ" }
|
||||
memmap = "0.6.2"
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
use cranelift_codegen::binemit::Reloc;
|
||||
use cranelift_codegen::isa::TargetIsa;
|
||||
use instance::Instance;
|
||||
use memory::LinearMemory;
|
||||
use region::protect;
|
||||
use region::Protection;
|
||||
use std::mem::transmute;
|
||||
use std::ptr::write_unaligned;
|
||||
use wasmtime_environ::{compile_module, Compilation, Module, ModuleTranslation, Relocation};
|
||||
use wasmtime_environ::{
|
||||
compile_module, Compilation, Module, ModuleTranslation, Relocation, RelocationTarget,
|
||||
};
|
||||
|
||||
/// Executes a module that has been translated with the `wasmtime-environ` environment
|
||||
/// implementation.
|
||||
@@ -33,7 +36,12 @@ fn relocate(compilation: &mut Compilation, relocations: &[Vec<Relocation>]) {
|
||||
// TODO: Support architectures other than x64, and other reloc kinds.
|
||||
for (i, function_relocs) in relocations.iter().enumerate() {
|
||||
for r in function_relocs {
|
||||
let target_func_address: isize = compilation.functions[r.func_index].as_ptr() as isize;
|
||||
let target_func_address: isize = match r.reloc_target {
|
||||
RelocationTarget::UserFunc(index) => compilation.functions[index].as_ptr() as isize,
|
||||
RelocationTarget::GrowMemory => grow_memory as isize,
|
||||
RelocationTarget::CurrentMemory => current_memory as isize,
|
||||
};
|
||||
|
||||
let body = &mut compilation.functions[i];
|
||||
match r.reloc {
|
||||
Reloc::Abs8 => unsafe {
|
||||
@@ -56,16 +64,30 @@ fn relocate(compilation: &mut Compilation, relocations: &[Vec<Relocation>]) {
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn grow_memory(size: u32, vmctx: *mut *mut u8) -> u32 {
|
||||
unsafe {
|
||||
let instance = (*vmctx.offset(2)) as *mut Instance;
|
||||
(*instance)
|
||||
.memory_mut(0)
|
||||
.grow(size)
|
||||
.unwrap_or(u32::max_value())
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn current_memory(vmctx: *mut *mut u8) -> u32 {
|
||||
unsafe {
|
||||
let instance = (*vmctx.offset(2)) as *mut Instance;
|
||||
(*instance).memory_mut(0).current_size()
|
||||
}
|
||||
}
|
||||
|
||||
/// Create the VmCtx data structure for the JIT'd code to use. This must
|
||||
/// match the VmCtx layout in the environment.
|
||||
fn make_vmctx(instance: &mut Instance) -> Vec<*mut u8> {
|
||||
let mut memories = Vec::new();
|
||||
fn make_vmctx(instance: &mut Instance, mem_base_addrs: &mut [*mut u8]) -> Vec<*mut u8> {
|
||||
let mut vmctx = Vec::new();
|
||||
vmctx.push(instance.globals.as_mut_ptr());
|
||||
for mem in &mut instance.memories {
|
||||
memories.push(mem.as_mut_ptr());
|
||||
}
|
||||
vmctx.push(memories.as_mut_ptr() as *mut u8);
|
||||
vmctx.push(mem_base_addrs.as_mut_ptr() as *mut u8);
|
||||
vmctx.push(instance as *mut Instance as *mut u8);
|
||||
vmctx
|
||||
}
|
||||
|
||||
@@ -100,7 +122,13 @@ pub fn execute(
|
||||
|
||||
let code_buf = &compilation.functions[start_index];
|
||||
|
||||
let vmctx = make_vmctx(instance);
|
||||
// Collect all memory base addresses and Vec.
|
||||
let mut mem_base_addrs = instance
|
||||
.memories
|
||||
.iter_mut()
|
||||
.map(LinearMemory::base_addr)
|
||||
.collect::<Vec<_>>();
|
||||
let vmctx = make_vmctx(instance, &mut mem_base_addrs);
|
||||
|
||||
// Rather than writing inline assembly to jump to the code region, we use the fact that
|
||||
// the Rust ABI for calling a function with no arguments and no return matches the one of
|
||||
|
||||
@@ -3,10 +3,9 @@
|
||||
|
||||
use cranelift_codegen::ir;
|
||||
use cranelift_wasm::GlobalIndex;
|
||||
use memory::LinearMemory;
|
||||
use wasmtime_environ::{DataInitializer, Module, TableElements};
|
||||
|
||||
const PAGE_SIZE: usize = 65536;
|
||||
|
||||
/// An Instance of a WebAssemby module.
|
||||
#[derive(Debug)]
|
||||
pub struct Instance {
|
||||
@@ -14,7 +13,7 @@ pub struct Instance {
|
||||
pub tables: Vec<Vec<usize>>,
|
||||
|
||||
/// WebAssembly linear memory data.
|
||||
pub memories: Vec<Vec<u8>>,
|
||||
pub memories: Vec<LinearMemory>,
|
||||
|
||||
/// WebAssembly global variable data.
|
||||
pub globals: Vec<u8>,
|
||||
@@ -58,15 +57,13 @@ impl Instance {
|
||||
// Allocate the underlying memory and initialize it to all zeros.
|
||||
self.memories.reserve_exact(module.memories.len());
|
||||
for memory in &module.memories {
|
||||
let len = memory.pages_count * PAGE_SIZE;
|
||||
let mut v = Vec::with_capacity(len);
|
||||
v.resize(len, 0);
|
||||
let v = LinearMemory::new(memory.pages_count as u32, memory.maximum.map(|m| m as u32));
|
||||
self.memories.push(v);
|
||||
}
|
||||
for init in data_initializers {
|
||||
debug_assert!(init.base.is_none(), "globalvar base not supported yet");
|
||||
let to_init =
|
||||
&mut self.memories[init.memory_index][init.offset..init.offset + init.data.len()];
|
||||
let mem_mut = self.memories[init.memory_index].as_mut();
|
||||
let to_init = &mut mem_mut[init.offset..init.offset + init.data.len()];
|
||||
to_init.copy_from_slice(init.data);
|
||||
}
|
||||
}
|
||||
@@ -80,13 +77,20 @@ impl Instance {
|
||||
self.globals.resize(globals_data_size, 0);
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to a linear memory under the specified index.
|
||||
pub fn memory_mut(&mut self, memory_index: usize) -> &mut LinearMemory {
|
||||
self.memories
|
||||
.get_mut(memory_index)
|
||||
.unwrap_or_else(|| panic!("no memory for index {}", memory_index))
|
||||
}
|
||||
|
||||
/// Returns a slice of the contents of allocated linear memory.
|
||||
pub fn inspect_memory(&self, memory_index: usize, address: usize, len: usize) -> &[u8] {
|
||||
&self
|
||||
.memories
|
||||
.get(memory_index)
|
||||
.unwrap_or_else(|| panic!("no memory for index {}", memory_index))
|
||||
[address..address + len]
|
||||
.as_ref()[address..address + len]
|
||||
}
|
||||
|
||||
/// Shows the value of a global variable.
|
||||
|
||||
@@ -14,11 +14,13 @@
|
||||
|
||||
extern crate cranelift_codegen;
|
||||
extern crate cranelift_wasm;
|
||||
extern crate memmap;
|
||||
extern crate region;
|
||||
extern crate wasmtime_environ;
|
||||
|
||||
mod execute;
|
||||
mod instance;
|
||||
mod memory;
|
||||
|
||||
pub use execute::{compile_and_link_module, execute};
|
||||
pub use instance::Instance;
|
||||
|
||||
90
lib/execute/src/memory.rs
Normal file
90
lib/execute/src/memory.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
use memmap;
|
||||
use std::fmt;
|
||||
|
||||
const PAGE_SIZE: u32 = 65536;
|
||||
const MAX_PAGES: u32 = 65536;
|
||||
|
||||
/// A linear memory instance.
|
||||
///
|
||||
/// This linear memory has a stable base address and at the same time allows
|
||||
/// for dynamical growing.
|
||||
pub struct LinearMemory {
|
||||
mmap: memmap::MmapMut,
|
||||
current: u32,
|
||||
maximum: u32,
|
||||
}
|
||||
|
||||
impl LinearMemory {
|
||||
/// Create a new linear memory instance with specified initial and maximum number of pages.
|
||||
///
|
||||
/// `maximum` cannot be set to more than `65536` pages. If `maximum` is `None` then it
|
||||
/// will be treated as `65336`.
|
||||
pub fn new(initial: u32, maximum: Option<u32>) -> Self {
|
||||
let maximum = maximum.unwrap_or(MAX_PAGES);
|
||||
|
||||
assert!(initial <= MAX_PAGES);
|
||||
assert!(maximum <= MAX_PAGES);
|
||||
|
||||
let len = maximum.saturating_mul(MAX_PAGES);
|
||||
let mmap = memmap::MmapMut::map_anon(len as usize).unwrap();
|
||||
Self {
|
||||
mmap,
|
||||
current: initial,
|
||||
maximum,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns an base address of this linear memory.
|
||||
pub fn base_addr(&mut self) -> *mut u8 {
|
||||
self.mmap.as_mut_ptr()
|
||||
}
|
||||
|
||||
/// Returns a number of allocated wasm pages.
|
||||
pub fn current_size(&self) -> u32 {
|
||||
self.current
|
||||
}
|
||||
|
||||
/// Grow memory by the specified amount of pages.
|
||||
///
|
||||
/// Returns `None` if memory can't be grown by the specified amount
|
||||
/// of pages.
|
||||
pub fn grow(&mut self, add_pages: u32) -> Option<u32> {
|
||||
let new_pages = match self.current.checked_add(add_pages) {
|
||||
Some(new_pages) => new_pages,
|
||||
None => return None,
|
||||
};
|
||||
|
||||
let prev_pages = self.current;
|
||||
self.current = new_pages;
|
||||
|
||||
// Ensure that newly allocated area is zeroed.
|
||||
let new_start_offset = (prev_pages * PAGE_SIZE) as usize;
|
||||
let new_end_offset = (new_pages * PAGE_SIZE) as usize;
|
||||
for i in new_start_offset..new_end_offset - 1 {
|
||||
self.mmap[i] = 0;
|
||||
}
|
||||
|
||||
Some(prev_pages)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for LinearMemory {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_struct("LinearMemory")
|
||||
.field("current", &self.current)
|
||||
.field("maximum", &self.maximum)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for LinearMemory {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
&self.mmap
|
||||
}
|
||||
}
|
||||
|
||||
impl AsMut<[u8]> for LinearMemory {
|
||||
fn as_mut(&mut self) -> &mut [u8] {
|
||||
&mut self.mmap
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user