Skip to content

Commit

Permalink
Implement memory.grow and memory.current (#9)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pepyakin authored and sunfishcode committed Aug 11, 2018
1 parent 5379605 commit e7c8d23
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 38 deletions.
28 changes: 28 additions & 0 deletions filetests/grow.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(module
(memory 1)
(func $assert (param i32)
(block $ok
(br_if $ok
(get_local 0)
)
(unreachable)
)
)
(func $main (local i32)
(call $assert
(i32.eq
(grow_memory (i32.const 1))
(i32.const 1)
)
)
(call $assert
(i32.eq
(current_memory)
(i32.const 2)
)
)
)
(start $main)
(data (i32.const 0) "\04\03\02\01")
)

28 changes: 21 additions & 7 deletions lib/environ/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@ impl binemit::RelocSink for RelocSink {
name: &ExternalName,
addend: binemit::Addend,
) {
// FIXME: Handle grow_memory/current_memory.
let func_index = if let ExternalName::User { namespace, index } = *name {
let reloc_target = if let ExternalName::User { namespace, index } = *name {
debug_assert!(namespace == 0);
index
RelocationTarget::UserFunc(index as usize)
} else if *name == ExternalName::testcase("grow_memory") {
RelocationTarget::GrowMemory
} else if *name == ExternalName::testcase("current_memory") {
RelocationTarget::CurrentMemory
} else {
panic!("unrecognized external name")
} as usize;
};
self.func_relocs.push(Relocation {
reloc,
func_index,
reloc_target,
offset,
addend,
});
Expand Down Expand Up @@ -83,14 +86,25 @@ impl RelocSink {
pub struct Relocation {
/// The relocation code.
pub reloc: binemit::Reloc,
/// The function index.
pub func_index: FunctionIndex,
/// Relocation target.
pub reloc_target: RelocationTarget,
/// The offset where to apply the relocation.
pub offset: binemit::CodeOffset,
/// The addend to add to the relocation value.
pub addend: binemit::Addend,
}

/// Destination function. Can be either user function or some special one, like grow_memory.
#[derive(Debug)]
pub enum RelocationTarget {
/// The user function index.
UserFunc(FunctionIndex),
/// Function for growing the default memory by the specified amount of pages.
GrowMemory,
/// Function for query current size of the default linear memory.
CurrentMemory,
}

/// Relocations to apply to function bodies.
pub type Relocations = Vec<Vec<Relocation>>;

Expand Down
29 changes: 17 additions & 12 deletions lib/environ/src/environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use cranelift_codegen::ir;
use cranelift_codegen::ir::immediates::Offset32;
use cranelift_codegen::ir::types::*;
use cranelift_codegen::ir::{
AbiParam, ArgumentExtension, ArgumentLoc, ArgumentPurpose, ExtFuncData, ExternalName, FuncRef,
Function, InstBuilder, Signature,
AbiParam, ArgumentPurpose, ExtFuncData, ExternalName, FuncRef, Function, InstBuilder, Signature,
};
use cranelift_codegen::isa;
use cranelift_codegen::settings;
Expand Down Expand Up @@ -132,12 +131,10 @@ impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data>

fn declare_signature(&mut self, sig: &ir::Signature) {
let mut sig = sig.clone();
sig.params.push(AbiParam {
value_type: self.pointer_type(),
purpose: ArgumentPurpose::VMContext,
extension: ArgumentExtension::None,
location: ArgumentLoc::Unassigned,
});
sig.params.push(AbiParam::special(
self.pointer_type(),
ArgumentPurpose::VMContext,
));
// TODO: Deduplicate signatures.
self.module.signatures.push(sig);
}
Expand Down Expand Up @@ -377,7 +374,10 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
let sig_ref = pos.func.import_signature(Signature {
call_conv: self.isa.flags().call_conv(),
argument_bytes: None,
params: vec![AbiParam::new(I32)],
params: vec![
AbiParam::new(I32),
AbiParam::special(self.pointer_type(), ArgumentPurpose::VMContext),
],
returns: vec![AbiParam::new(I32)],
});
// We currently allocate all code segments independently, so nothing
Expand All @@ -391,7 +391,8 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
})
});
self.grow_memory_extfunc = Some(grow_mem_func);
let call_inst = pos.ins().call(grow_mem_func, &[val]);
let vmctx = pos.func.special_param(ArgumentPurpose::VMContext).unwrap();
let call_inst = pos.ins().call(grow_mem_func, &[val, vmctx]);
Ok(*pos.func.dfg.inst_results(call_inst).first().unwrap())
}

Expand All @@ -406,7 +407,10 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
let sig_ref = pos.func.import_signature(Signature {
call_conv: self.isa.flags().call_conv(),
argument_bytes: None,
params: Vec::new(),
params: vec![AbiParam::special(
self.pointer_type(),
ArgumentPurpose::VMContext,
)],
returns: vec![AbiParam::new(I32)],
});
// We currently allocate all code segments independently, so nothing
Expand All @@ -420,7 +424,8 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
})
});
self.current_memory_extfunc = Some(cur_mem_func);
let call_inst = pos.ins().call(cur_mem_func, &[]);
let vmctx = pos.func.special_param(ArgumentPurpose::VMContext).unwrap();
let call_inst = pos.ins().call(cur_mem_func, &[vmctx]);
Ok(*pos.func.dfg.inst_results(call_inst).first().unwrap())
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/environ/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ mod compilation;
mod environ;
mod module;

pub use compilation::{compile_module, Compilation, Relocation, Relocations};
pub use compilation::{compile_module, Compilation, Relocation, RelocationTarget, Relocations};
pub use environ::{ModuleEnvironment, ModuleTranslation};
pub use module::{DataInitializer, Module, TableElements};
1 change: 1 addition & 0 deletions lib/execute/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
46 changes: 37 additions & 9 deletions lib/execute/src/execute.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions lib/execute/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@

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 {
/// WebAssembly table data.
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>,
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions lib/execute/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Loading

0 comments on commit e7c8d23

Please sign in to comment.