More code reorganization.

This commit is contained in:
Dan Gohman
2018-08-03 13:45:46 -07:00
parent 6659ef7018
commit 831b481f13
10 changed files with 177 additions and 180 deletions

113
lib/execute/src/execute.rs Normal file
View File

@@ -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<Compilation, 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<Relocation>]) {
// 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(())
}

View File

@@ -3,8 +3,7 @@
use cranelift_codegen::ir; use cranelift_codegen::ir;
use cranelift_wasm::GlobalIndex; use cranelift_wasm::GlobalIndex;
use environ::DataInitializer; use wasmtime_runtime::{DataInitializer, Module, TableElements};
use module::{Module, TableElements};
const PAGE_SIZE: usize = 65536; const PAGE_SIZE: usize = 65536;

View File

@@ -7,115 +7,8 @@ extern crate cranelift_wasm;
extern crate region; extern crate region;
extern crate wasmtime_runtime; extern crate wasmtime_runtime;
use cranelift_codegen::binemit::Reloc; mod execute;
use cranelift_codegen::isa::TargetIsa; mod instance;
use region::protect;
use region::Protection;
use std::mem::transmute;
use std::ptr::write_unaligned;
use wasmtime_runtime::{Compilation, Relocation, compile_module};
/// Executes a module that has been translated with the `standalone::Runtime` runtime implementation. pub use execute::{compile_and_link_module, execute};
pub fn compile_and_link_module<'data, 'module>( pub use instance::Instance;
isa: &TargetIsa,
translation: &wasmtime_runtime::ModuleTranslation<'data, 'module>,
) -> Result<wasmtime_runtime::Compilation<'module>, 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<Relocation>]) {
// 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(())
}

View File

@@ -5,14 +5,14 @@ use wasmtime_runtime;
/// Emits a module that has been emitted with the `WasmRuntime` runtime /// Emits a module that has been emitted with the `WasmRuntime` runtime
/// implementation to a native object file. /// implementation to a native object file.
pub fn emit_module<'module>( pub fn emit_module(
obj: &mut Artifact, obj: &mut Artifact,
compilation: &wasmtime_runtime::Compilation<'module>, module: &wasmtime_runtime::Module,
compilation: &wasmtime_runtime::Compilation,
relocations: &wasmtime_runtime::Relocations, relocations: &wasmtime_runtime::Relocations,
) -> Result<(), String> { ) -> Result<(), String> {
debug_assert!( debug_assert!(
compilation.module.start_func.is_none() module.start_func.is_none() || module.start_func.unwrap() >= module.imported_funcs.len(),
|| compilation.module.start_func.unwrap() >= compilation.module.imported_funcs.len(),
"imported start functions not supported yet" "imported start functions not supported yet"
); );
@@ -24,7 +24,7 @@ pub fn emit_module<'module>(
for (i, function_relocs) in relocations.iter().enumerate() { for (i, function_relocs) in relocations.iter().enumerate() {
assert!(function_relocs.is_empty(), "relocations not supported yet"); assert!(function_relocs.is_empty(), "relocations not supported yet");
let body = &compilation.functions[i]; 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); let string_name = format!("wasm_function[{}]", func_index);
obj.define(string_name, body.clone()) obj.define(string_name, body.clone())

View File

@@ -8,22 +8,18 @@ use cranelift_codegen::isa;
use cranelift_codegen::Context; use cranelift_codegen::Context;
use cranelift_wasm::{FuncTranslator, FunctionIndex}; use cranelift_wasm::{FuncTranslator, FunctionIndex};
use environ::{get_func_name, ModuleTranslation}; 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)] #[derive(Debug)]
pub struct Compilation<'module> { pub struct Compilation {
/// The module this `Compilation` is compiled from.
pub module: &'module Module,
/// Compiled machine code for the function bodies. /// Compiled machine code for the function bodies.
pub functions: Vec<Vec<u8>>, pub functions: Vec<Vec<u8>>,
} }
impl<'module> Compilation<'module> { impl Compilation {
/// Allocates the runtime data structures with the given flags. /// Allocates the runtime data structures with the given flags.
pub fn new(module: &'module Module, functions: Vec<Vec<u8>>) -> Self { pub fn new(functions: Vec<Vec<u8>>) -> Self {
Self { module, functions } Self { functions }
} }
} }
@@ -103,7 +99,7 @@ pub type Relocations = Vec<Vec<Relocation>>;
pub fn compile_module<'data, 'module>( pub fn compile_module<'data, 'module>(
translation: &ModuleTranslation<'data, 'module>, translation: &ModuleTranslation<'data, 'module>,
isa: &isa::TargetIsa, isa: &isa::TargetIsa,
) -> Result<(Compilation<'module>, Relocations), String> { ) -> Result<(Compilation, Relocations), String> {
let mut functions = Vec::new(); let mut functions = Vec::new();
let mut relocations = Vec::new(); let mut relocations = Vec::new();
for (i, input) in translation.lazy.function_body_inputs.iter().enumerate() { 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); functions.push(code_buf);
relocations.push(reloc_sink.func_relocs); relocations.push(reloc_sink.func_relocs);
} }
Ok((Compilation::new(translation.module, functions), relocations)) Ok((Compilation::new(functions), relocations))
} }

View File

@@ -13,8 +13,7 @@ use cranelift_wasm::{
FunctionIndex, Global, GlobalIndex, GlobalVariable, Memory, MemoryIndex, SignatureIndex, Table, FunctionIndex, Global, GlobalIndex, GlobalVariable, Memory, MemoryIndex, SignatureIndex, Table,
TableIndex, WasmResult, TableIndex, WasmResult,
}; };
use module; use module::{DataInitializer, Export, LazyContents, Module, TableElements};
use module::Module;
use target_lexicon::Triple; use target_lexicon::Triple;
/// Compute a `ir::ExternalName` for a given wasm function index. /// 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) 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<GlobalIndex>,
/// 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<DataInitializer<'data>>,
}
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 /// Object containing the standalone runtime information. To be passed after creation as argument
/// to `cranelift_wasm::translatemodule`. /// to `cranelift_wasm::translatemodule`.
pub struct ModuleEnvironment<'data, 'module> { pub struct ModuleEnvironment<'data, 'module> {
@@ -224,7 +192,7 @@ impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data>
elements: Vec<FunctionIndex>, elements: Vec<FunctionIndex>,
) { ) {
debug_assert!(base.is_none(), "global-value offsets not supported yet"); 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, table_index,
base, base,
offset, offset,
@@ -255,25 +223,25 @@ impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data>
fn declare_func_export(&mut self, func_index: FunctionIndex, name: &str) { fn declare_func_export(&mut self, func_index: FunctionIndex, name: &str) {
self.module self.module
.exports .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) { fn declare_table_export(&mut self, table_index: TableIndex, name: &str) {
self.module self.module
.exports .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) { fn declare_memory_export(&mut self, memory_index: MemoryIndex, name: &str) {
self.module self.module
.exports .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) { fn declare_global_export(&mut self, global_index: GlobalIndex, name: &str) {
self.module self.module
.exports .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) { fn declare_start_func(&mut self, func_index: FunctionIndex) {

View File

@@ -11,10 +11,8 @@ extern crate target_lexicon;
mod compilation; mod compilation;
mod environ; mod environ;
mod instance;
mod module; 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 environ::{ModuleEnvironment, ModuleTranslation};
pub use instance::Instance; pub use module::{DataInitializer, Module, TableElements};
pub use module::Module;

View File

@@ -1,5 +1,4 @@
//! A `Module` contains all the relevant information translated from a //! Data structures for representing decoded wasm modules.
//! WebAssembly module, except for the function bodies and data initializers.
use cranelift_codegen::ir; use cranelift_codegen::ir;
use cranelift_wasm::{ 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<GlobalIndex>,
/// 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<DataInitializer<'data>>,
}
impl<'data> LazyContents<'data> {
pub fn new() -> Self {
Self {
function_body_inputs: Vec::new(),
data_initializers: Vec::new(),
}
}
}

View File

@@ -29,8 +29,8 @@ use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{exit, Command}; use std::process::{exit, Command};
use tempdir::TempDir; use tempdir::TempDir;
use wasmtime_execute::{compile_and_link_module, execute}; use wasmtime_execute::{compile_and_link_module, execute, Instance};
use wasmtime_runtime::{Instance, Module, ModuleEnvironment}; use wasmtime_runtime::{Module, ModuleEnvironment};
const USAGE: &str = " const USAGE: &str = "
Wasm to Cranelift IL translation utility. 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) { let instance = match compile_and_link_module(isa, &translation) {
Ok(compilation) => { Ok(compilation) => {
let mut instance = let mut instance =
Instance::new(compilation.module, &translation.lazy.data_initializers); Instance::new(translation.module, &translation.lazy.data_initializers);
execute(&compilation, &mut instance)?; execute(&translation.module, &compilation, &mut instance)?;
instance instance
} }
Err(s) => { Err(s) => {

View File

@@ -108,10 +108,10 @@ fn handle_module(path: PathBuf, output: &str) -> Result<(), String> {
let (compilation, relocations) = compile_module(&translation, &*isa)?; 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 !translation.module.tables.is_empty() {
if compilation.module.tables.len() > 1 { if translation.module.tables.len() > 1 {
return Err(String::from("multiple tables not supported yet")); return Err(String::from("multiple tables not supported yet"));
} }
return Err(String::from("FIXME: implement tables")); return Err(String::from("FIXME: implement tables"));