diff --git a/lib/execute/src/execute.rs b/lib/execute/src/execute.rs new file mode 100644 index 0000000000..6642da4d3c --- /dev/null +++ b/lib/execute/src/execute.rs @@ -0,0 +1,113 @@ +use cranelift_codegen::binemit::Reloc; +use cranelift_codegen::isa::TargetIsa; +use instance::Instance; +use region::protect; +use region::Protection; +use std::mem::transmute; +use std::ptr::write_unaligned; +use wasmtime_runtime::{compile_module, Compilation, Module, ModuleTranslation, Relocation}; + +/// Executes a module that has been translated with the `standalone::Runtime` runtime implementation. +pub fn compile_and_link_module<'data, 'module>( + isa: &TargetIsa, + translation: &ModuleTranslation<'data, 'module>, +) -> Result { + debug_assert!( + translation.module.start_func.is_none() + || translation.module.start_func.unwrap() >= translation.module.imported_funcs.len(), + "imported start functions not supported yet" + ); + + let (mut compilation, relocations) = compile_module(&translation, isa)?; + + // Apply relocations, now that we have virtual addresses for everything. + relocate(&mut compilation, &relocations); + + Ok(compilation) +} + +/// Performs the relocations inside the function bytecode, provided the necessary metadata +fn relocate(compilation: &mut Compilation, relocations: &[Vec]) { + // The relocations are relative to the relocation's address plus four bytes + // 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 body = &mut compilation.functions[i]; + match r.reloc { + Reloc::Abs8 => unsafe { + let reloc_address = body.as_mut_ptr().offset(r.offset as isize) as i64; + let reloc_addend = r.addend as i64; + let reloc_abs = target_func_address as i64 + reloc_addend; + write_unaligned(reloc_address as *mut i64, reloc_abs); + }, + Reloc::X86PCRel4 => unsafe { + let reloc_address = body.as_mut_ptr().offset(r.offset as isize) as isize; + let reloc_addend = r.addend as isize; + // TODO: Handle overflow. + let reloc_delta_i32 = + (target_func_address - reloc_address + reloc_addend) as i32; + write_unaligned(reloc_address as *mut i32, reloc_delta_i32); + }, + _ => panic!("unsupported reloc kind"), + } + } + } +} + +/// Create the VmCtx data structure for the JIT'd code to use. This must +/// match the VmCtx layout in the runtime. +fn make_vmctx(instance: &mut Instance) -> Vec<*mut u8> { + let mut memories = Vec::new(); + 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 +} + +/// Jumps to the code region of memory and execute the start function of the module. +pub fn execute( + module: &Module, + compilation: &Compilation, + instance: &mut Instance, +) -> Result<(), String> { + let start_index = module + .start_func + .ok_or_else(|| String::from("No start function defined, aborting execution"))?; + // TODO: Put all the function bodies into a page-aligned memory region, and + // then make them ReadExecute rather than ReadWriteExecute. + for code_buf in &compilation.functions { + match unsafe { + protect( + code_buf.as_ptr(), + code_buf.len(), + Protection::ReadWriteExecute, + ) + } { + Ok(()) => (), + Err(err) => { + return Err(format!( + "failed to give executable permission to code: {}", + err + )) + } + } + } + + let code_buf = &compilation.functions[start_index]; + + let vmctx = make_vmctx(instance); + + // 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 + // the generated code.Thanks to this, we can transmute the code region into a first-class + // Rust function and call it. + unsafe { + let start_func = transmute::<_, fn(*const *mut u8)>(code_buf.as_ptr()); + start_func(vmctx.as_ptr()); + } + Ok(()) +} diff --git a/lib/runtime/src/instance.rs b/lib/execute/src/instance.rs similarity index 98% rename from lib/runtime/src/instance.rs rename to lib/execute/src/instance.rs index 5b9b72d4d9..9ec7f1000b 100644 --- a/lib/runtime/src/instance.rs +++ b/lib/execute/src/instance.rs @@ -3,8 +3,7 @@ use cranelift_codegen::ir; use cranelift_wasm::GlobalIndex; -use environ::DataInitializer; -use module::{Module, TableElements}; +use wasmtime_runtime::{DataInitializer, Module, TableElements}; const PAGE_SIZE: usize = 65536; diff --git a/lib/execute/src/lib.rs b/lib/execute/src/lib.rs index 72657df20e..9a669da581 100644 --- a/lib/execute/src/lib.rs +++ b/lib/execute/src/lib.rs @@ -7,115 +7,8 @@ extern crate cranelift_wasm; extern crate region; extern crate wasmtime_runtime; -use cranelift_codegen::binemit::Reloc; -use cranelift_codegen::isa::TargetIsa; -use region::protect; -use region::Protection; -use std::mem::transmute; -use std::ptr::write_unaligned; -use wasmtime_runtime::{Compilation, Relocation, compile_module}; +mod execute; +mod instance; -/// Executes a module that has been translated with the `standalone::Runtime` runtime implementation. -pub fn compile_and_link_module<'data, 'module>( - isa: &TargetIsa, - translation: &wasmtime_runtime::ModuleTranslation<'data, 'module>, -) -> Result, String> { - debug_assert!( - translation.module.start_func.is_none() - || translation.module.start_func.unwrap() >= translation.module.imported_funcs.len(), - "imported start functions not supported yet" - ); - - let (mut compilation, relocations) = compile_module(&translation, isa)?; - - // Apply relocations, now that we have virtual addresses for everything. - relocate(&mut compilation, &relocations); - - Ok(compilation) -} - -/// Performs the relocations inside the function bytecode, provided the necessary metadata -fn relocate(compilation: &mut Compilation, relocations: &[Vec]) { - // The relocations are relative to the relocation's address plus four bytes - // TODO: Support architectures other than x64, and other reloc kinds. - for (i, function_relocs) in relocations.iter().enumerate() { - for ref r in function_relocs { - let target_func_address: isize = compilation.functions[r.func_index].as_ptr() as isize; - let body = &mut compilation.functions[i]; - match r.reloc { - Reloc::Abs8 => unsafe { - let reloc_address = body.as_mut_ptr().offset(r.offset as isize) as i64; - let reloc_addend = r.addend as i64; - let reloc_abs = target_func_address as i64 + reloc_addend; - write_unaligned(reloc_address as *mut i64, reloc_abs); - }, - Reloc::X86PCRel4 => unsafe { - let reloc_address = body.as_mut_ptr().offset(r.offset as isize) as isize; - let reloc_addend = r.addend as isize; - // TODO: Handle overflow. - let reloc_delta_i32 = - (target_func_address - reloc_address + reloc_addend) as i32; - write_unaligned(reloc_address as *mut i32, reloc_delta_i32); - }, - _ => panic!("unsupported reloc kind"), - } - } - } -} - -/// Create the VmCtx data structure for the JIT'd code to use. This must -/// match the VmCtx layout in the runtime. -fn make_vmctx(instance: &mut wasmtime_runtime::Instance) -> Vec<*mut u8> { - let mut memories = Vec::new(); - 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 -} - -/// Jumps to the code region of memory and execute the start function of the module. -pub fn execute( - compilation: &wasmtime_runtime::Compilation, - instance: &mut wasmtime_runtime::Instance, -) -> Result<(), String> { - let start_index = compilation - .module - .start_func - .ok_or_else(|| String::from("No start function defined, aborting execution"))?; - // TODO: Put all the function bodies into a page-aligned memory region, and - // then make them ReadExecute rather than ReadWriteExecute. - for code_buf in &compilation.functions { - match unsafe { - protect( - code_buf.as_ptr(), - code_buf.len(), - Protection::ReadWriteExecute, - ) - } { - Ok(()) => (), - Err(err) => { - return Err(format!( - "failed to give executable permission to code: {}", - err - )) - } - } - } - - let code_buf = &compilation.functions[start_index]; - - let vmctx = make_vmctx(instance); - - // 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 - // the generated code.Thanks to this, we can transmute the code region into a first-class - // Rust function and call it. - unsafe { - let start_func = transmute::<_, fn(*const *mut u8)>(code_buf.as_ptr()); - start_func(vmctx.as_ptr()); - } - Ok(()) -} +pub use execute::{compile_and_link_module, execute}; +pub use instance::Instance; diff --git a/lib/obj/src/emit_module.rs b/lib/obj/src/emit_module.rs index c54a151151..2e1d6d0cdd 100644 --- a/lib/obj/src/emit_module.rs +++ b/lib/obj/src/emit_module.rs @@ -5,14 +5,14 @@ use wasmtime_runtime; /// Emits a module that has been emitted with the `WasmRuntime` runtime /// implementation to a native object file. -pub fn emit_module<'module>( +pub fn emit_module( obj: &mut Artifact, - compilation: &wasmtime_runtime::Compilation<'module>, + module: &wasmtime_runtime::Module, + compilation: &wasmtime_runtime::Compilation, relocations: &wasmtime_runtime::Relocations, ) -> Result<(), String> { debug_assert!( - compilation.module.start_func.is_none() - || compilation.module.start_func.unwrap() >= compilation.module.imported_funcs.len(), + module.start_func.is_none() || module.start_func.unwrap() >= module.imported_funcs.len(), "imported start functions not supported yet" ); @@ -24,7 +24,7 @@ pub fn emit_module<'module>( for (i, function_relocs) in relocations.iter().enumerate() { assert!(function_relocs.is_empty(), "relocations not supported yet"); let body = &compilation.functions[i]; - let func_index = compilation.module.imported_funcs.len() + i; + let func_index = module.imported_funcs.len() + i; let string_name = format!("wasm_function[{}]", func_index); obj.define(string_name, body.clone()) diff --git a/lib/runtime/src/compilation.rs b/lib/runtime/src/compilation.rs index 12687b1d5d..0f0ce6e557 100644 --- a/lib/runtime/src/compilation.rs +++ b/lib/runtime/src/compilation.rs @@ -8,22 +8,18 @@ use cranelift_codegen::isa; use cranelift_codegen::Context; use cranelift_wasm::{FuncTranslator, FunctionIndex}; use environ::{get_func_name, ModuleTranslation}; -use module::Module; -/// An Instance of a WebAssemby module. +/// The result of compiling a WebAssemby module's functions. #[derive(Debug)] -pub struct Compilation<'module> { - /// The module this `Compilation` is compiled from. - pub module: &'module Module, - +pub struct Compilation { /// Compiled machine code for the function bodies. pub functions: Vec>, } -impl<'module> Compilation<'module> { +impl Compilation { /// Allocates the runtime data structures with the given flags. - pub fn new(module: &'module Module, functions: Vec>) -> Self { - Self { module, functions } + pub fn new(functions: Vec>) -> Self { + Self { functions } } } @@ -103,7 +99,7 @@ pub type Relocations = Vec>; pub fn compile_module<'data, 'module>( translation: &ModuleTranslation<'data, 'module>, isa: &isa::TargetIsa, -) -> Result<(Compilation<'module>, Relocations), String> { +) -> Result<(Compilation, Relocations), String> { let mut functions = Vec::new(); let mut relocations = Vec::new(); for (i, input) in translation.lazy.function_body_inputs.iter().enumerate() { @@ -127,5 +123,5 @@ pub fn compile_module<'data, 'module>( functions.push(code_buf); relocations.push(reloc_sink.func_relocs); } - Ok((Compilation::new(translation.module, functions), relocations)) + Ok((Compilation::new(functions), relocations)) } diff --git a/lib/runtime/src/environ.rs b/lib/runtime/src/environ.rs index 4895f5f86e..0afb892000 100644 --- a/lib/runtime/src/environ.rs +++ b/lib/runtime/src/environ.rs @@ -13,8 +13,7 @@ use cranelift_wasm::{ FunctionIndex, Global, GlobalIndex, GlobalVariable, Memory, MemoryIndex, SignatureIndex, Table, TableIndex, WasmResult, }; -use module; -use module::Module; +use module::{DataInitializer, Export, LazyContents, Module, TableElements}; use target_lexicon::Triple; /// Compute a `ir::ExternalName` for a given wasm function index. @@ -23,37 +22,6 @@ pub fn get_func_name(func_index: FunctionIndex) -> ir::ExternalName { ir::ExternalName::user(0, func_index as u32) } -/// A data initializer for linear memory. -pub struct DataInitializer<'data> { - /// The index of the memory to initialize. - pub memory_index: MemoryIndex, - /// Optionally a globalvar base to initialize at. - pub base: Option, - /// A constant offset to initialize at. - pub offset: usize, - /// The initialization data. - pub data: &'data [u8], -} - -/// References to the input wasm data buffer to be decoded and processed later. -/// separately from the main module translation. -pub struct LazyContents<'data> { - /// References to the function bodies. - pub function_body_inputs: Vec<&'data [u8]>, - - /// References to the data initializers. - pub data_initializers: Vec>, -} - -impl<'data> LazyContents<'data> { - fn new() -> Self { - Self { - function_body_inputs: Vec::new(), - data_initializers: Vec::new(), - } - } -} - /// Object containing the standalone runtime information. To be passed after creation as argument /// to `cranelift_wasm::translatemodule`. pub struct ModuleEnvironment<'data, 'module> { @@ -224,7 +192,7 @@ impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data> elements: Vec, ) { debug_assert!(base.is_none(), "global-value offsets not supported yet"); - self.module.table_elements.push(module::TableElements { + self.module.table_elements.push(TableElements { table_index, base, offset, @@ -255,25 +223,25 @@ impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data> fn declare_func_export(&mut self, func_index: FunctionIndex, name: &str) { self.module .exports - .insert(String::from(name), module::Export::Function(func_index)); + .insert(String::from(name), Export::Function(func_index)); } fn declare_table_export(&mut self, table_index: TableIndex, name: &str) { self.module .exports - .insert(String::from(name), module::Export::Table(table_index)); + .insert(String::from(name), Export::Table(table_index)); } fn declare_memory_export(&mut self, memory_index: MemoryIndex, name: &str) { self.module .exports - .insert(String::from(name), module::Export::Memory(memory_index)); + .insert(String::from(name), Export::Memory(memory_index)); } fn declare_global_export(&mut self, global_index: GlobalIndex, name: &str) { self.module .exports - .insert(String::from(name), module::Export::Global(global_index)); + .insert(String::from(name), Export::Global(global_index)); } fn declare_start_func(&mut self, func_index: FunctionIndex) { diff --git a/lib/runtime/src/lib.rs b/lib/runtime/src/lib.rs index aec78f8b45..a60da84b1f 100644 --- a/lib/runtime/src/lib.rs +++ b/lib/runtime/src/lib.rs @@ -11,10 +11,8 @@ extern crate target_lexicon; mod compilation; mod environ; -mod instance; mod module; -pub use compilation::{Compilation, compile_module, Relocation, Relocations}; +pub use compilation::{compile_module, Compilation, Relocation, Relocations}; pub use environ::{ModuleEnvironment, ModuleTranslation}; -pub use instance::Instance; -pub use module::Module; +pub use module::{DataInitializer, Module, TableElements}; diff --git a/lib/runtime/src/module.rs b/lib/runtime/src/module.rs index 9c7bfbc725..d9eaeacabd 100644 --- a/lib/runtime/src/module.rs +++ b/lib/runtime/src/module.rs @@ -1,5 +1,4 @@ -//! A `Module` contains all the relevant information translated from a -//! WebAssembly module, except for the function bodies and data initializers. +//! Data structures for representing decoded wasm modules. use cranelift_codegen::ir; use cranelift_wasm::{ @@ -81,3 +80,34 @@ impl Module { } } } + +/// A data initializer for linear memory. +pub struct DataInitializer<'data> { + /// The index of the memory to initialize. + pub memory_index: MemoryIndex, + /// Optionally a globalvar base to initialize at. + pub base: Option, + /// A constant offset to initialize at. + pub offset: usize, + /// The initialization data. + pub data: &'data [u8], +} + +/// References to the input wasm data buffer to be decoded and processed later, +/// separately from the main module translation. +pub struct LazyContents<'data> { + /// References to the function bodies. + pub function_body_inputs: Vec<&'data [u8]>, + + /// References to the data initializers. + pub data_initializers: Vec>, +} + +impl<'data> LazyContents<'data> { + pub fn new() -> Self { + Self { + function_body_inputs: Vec::new(), + data_initializers: Vec::new(), + } + } +} diff --git a/src/main.rs b/src/main.rs index f82ce4d223..6a685425a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,8 +29,8 @@ use std::path::Path; use std::path::PathBuf; use std::process::{exit, Command}; use tempdir::TempDir; -use wasmtime_execute::{compile_and_link_module, execute}; -use wasmtime_runtime::{Instance, Module, ModuleEnvironment}; +use wasmtime_execute::{compile_and_link_module, execute, Instance}; +use wasmtime_runtime::{Module, ModuleEnvironment}; const USAGE: &str = " Wasm to Cranelift IL translation utility. @@ -125,8 +125,8 @@ fn handle_module(args: &Args, path: PathBuf, isa: &TargetIsa) -> Result<(), Stri let instance = match compile_and_link_module(isa, &translation) { Ok(compilation) => { let mut instance = - Instance::new(compilation.module, &translation.lazy.data_initializers); - execute(&compilation, &mut instance)?; + Instance::new(translation.module, &translation.lazy.data_initializers); + execute(&translation.module, &compilation, &mut instance)?; instance } Err(s) => { diff --git a/src/wasm2obj.rs b/src/wasm2obj.rs index 25856058ba..96b2f265cf 100644 --- a/src/wasm2obj.rs +++ b/src/wasm2obj.rs @@ -108,10 +108,10 @@ fn handle_module(path: PathBuf, output: &str) -> Result<(), String> { let (compilation, relocations) = compile_module(&translation, &*isa)?; - emit_module(&mut obj, &compilation, &relocations)?; + emit_module(&mut obj, &translation.module, &compilation, &relocations)?; - if !compilation.module.tables.is_empty() { - if compilation.module.tables.len() > 1 { + if !translation.module.tables.is_empty() { + if translation.module.tables.len() > 1 { return Err(String::from("multiple tables not supported yet")); } return Err(String::from("FIXME: implement tables"));