diff --git a/Cargo.toml b/Cargo.toml index 99a5df2730..b376ed93fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,15 +17,15 @@ name = "wasm2obj" path = "src/wasm2obj.rs" [dependencies] -cranelift-codegen = "0.18.1" -cranelift-native = "0.18.1" +cranelift-codegen = "0.20.0" +cranelift-native = "0.20.0" wasmtime-environ = { path = "lib/environ" } wasmtime-execute = { path = "lib/execute" } wasmtime-obj = { path = "lib/obj" } -docopt = "1.0.0" -serde = "1.0.55" -serde_derive = "1.0.55" +docopt = "1.0.1" +serde = "1.0.75" +serde_derive = "1.0.75" tempdir = "*" -faerie = "0.4.4" +faerie = "0.5.0" [workspace] diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 84e7623e19..3c588e327a 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -10,9 +10,9 @@ cargo-fuzz = true [dependencies] wasmtime-environ = { path = "../lib/environ" } wasmtime-execute = { path = "../lib/execute" } -cranelift-codegen = "0.18.1" -cranelift-wasm = "0.18.1" -cranelift-native = "0.18.1" +cranelift-codegen = "0.20.0" +cranelift-wasm = "0.20.1" +cranelift-native = "0.20.0" libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git" } wasmparser = { version = "0.17.2", default-features = false } diff --git a/lib/environ/Cargo.toml b/lib/environ/Cargo.toml index e655fe0992..1e36c3f05d 100644 --- a/lib/environ/Cargo.toml +++ b/lib/environ/Cargo.toml @@ -9,8 +9,9 @@ license = "Apache-2.0 WITH LLVM-exception" readme = "README.md" [dependencies] -cranelift-codegen = "0.18.1" -cranelift-wasm = "0.18.1" +cranelift-codegen = "0.20.0" +cranelift-entity = "0.20.1" +cranelift-wasm = "0.20.1" target-lexicon = "0.0.3" [badges] diff --git a/lib/environ/src/compilation.rs b/lib/environ/src/compilation.rs index a2f71298da..55a84eddfa 100644 --- a/lib/environ/src/compilation.rs +++ b/lib/environ/src/compilation.rs @@ -6,19 +6,20 @@ use cranelift_codegen::ir; use cranelift_codegen::ir::ExternalName; use cranelift_codegen::isa; use cranelift_codegen::Context; -use cranelift_wasm::{FuncTranslator, FunctionIndex}; +use cranelift_entity::{EntityRef, PrimaryMap}; +use cranelift_wasm::{DefinedFuncIndex, FuncIndex, FuncTranslator}; use environ::{get_func_name, ModuleTranslation}; /// The result of compiling a WebAssemby module's functions. #[derive(Debug)] pub struct Compilation { /// Compiled machine code for the function bodies. - pub functions: Vec>, + pub functions: PrimaryMap>, } impl Compilation { /// Allocates the compilation result with the given function bodies. - pub fn new(functions: Vec>) -> Self { + pub fn new(functions: PrimaryMap>) -> Self { Self { functions } } } @@ -48,7 +49,7 @@ impl binemit::RelocSink for RelocSink { ) { let reloc_target = if let ExternalName::User { namespace, index } = *name { debug_assert!(namespace == 0); - RelocationTarget::UserFunc(index as usize) + RelocationTarget::UserFunc(FuncIndex::new(index as usize)) } else if *name == ExternalName::testcase("grow_memory") { RelocationTarget::GrowMemory } else if *name == ExternalName::testcase("current_memory") { @@ -82,7 +83,7 @@ impl RelocSink { } /// A record of a relocation to perform. -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Relocation { /// The relocation code. pub reloc: binemit::Reloc, @@ -95,10 +96,10 @@ pub struct Relocation { } /// Destination function. Can be either user function or some special one, like grow_memory. -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] pub enum RelocationTarget { /// The user function index. - UserFunc(FunctionIndex), + UserFunc(FuncIndex), /// Function for growing the default memory by the specified amount of pages. GrowMemory, /// Function for query current size of the default linear memory. @@ -106,7 +107,7 @@ pub enum RelocationTarget { } /// Relocations to apply to function bodies. -pub type Relocations = Vec>; +pub type Relocations = PrimaryMap>; /// Compile the module, producing a compilation result with associated /// relocations. @@ -114,10 +115,10 @@ pub fn compile_module<'data, 'module>( translation: &ModuleTranslation<'data, 'module>, isa: &isa::TargetIsa, ) -> 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() { - let func_index = i + translation.module.imported_funcs.len(); + let mut functions = PrimaryMap::new(); + let mut relocations = PrimaryMap::new(); + for (i, input) in translation.lazy.function_body_inputs.iter() { + let func_index = translation.module.func_index(i); let mut context = Context::new(); context.func.name = get_func_name(func_index); context.func.signature = diff --git a/lib/environ/src/environ.rs b/lib/environ/src/environ.rs index 08583fb784..c3d6247548 100644 --- a/lib/environ/src/environ.rs +++ b/lib/environ/src/environ.rs @@ -7,17 +7,18 @@ use cranelift_codegen::ir::{ }; use cranelift_codegen::isa; use cranelift_codegen::settings; +use cranelift_entity::EntityRef; use cranelift_wasm::{ - self, translate_module, FunctionIndex, Global, GlobalIndex, GlobalVariable, Memory, - MemoryIndex, SignatureIndex, Table, TableIndex, WasmResult, + self, translate_module, FuncIndex, Global, GlobalIndex, GlobalVariable, Memory, MemoryIndex, + SignatureIndex, Table, TableIndex, WasmResult, }; use module::{DataInitializer, Export, LazyContents, Module, TableElements}; use target_lexicon::Triple; /// Compute a `ir::ExternalName` for a given wasm function index. -pub fn get_func_name(func_index: FunctionIndex) -> ir::ExternalName { - debug_assert!(func_index as u32 as FunctionIndex == func_index); - ir::ExternalName::user(0, func_index as u32) +pub fn get_func_name(func_index: FuncIndex) -> ir::ExternalName { + debug_assert!(FuncIndex::new(func_index.index() as u32 as usize) == func_index); + ir::ExternalName::user(0, func_index.index() as u32) } /// Object containing the standalone environment information. To be passed after creation as @@ -121,7 +122,7 @@ impl<'module_environment> FuncEnvironment<'module_environment> { impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data, 'module> { - fn get_func_name(&self, func_index: FunctionIndex) -> ir::ExternalName { + fn get_func_name(&self, func_index: FuncIndex) -> ir::ExternalName { get_func_name(func_index) } @@ -164,7 +165,7 @@ impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data> self.module.functions.push(sig_index); } - fn get_func_type(&self, func_index: FunctionIndex) -> SignatureIndex { + fn get_func_type(&self, func_index: FuncIndex) -> SignatureIndex { self.module.functions[func_index] } @@ -185,7 +186,7 @@ impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data> table_index: TableIndex, base: Option, offset: usize, - elements: Vec, + elements: Vec, ) { debug_assert!(base.is_none(), "global-value offsets not supported yet"); self.module.table_elements.push(TableElements { @@ -216,7 +217,7 @@ 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: FuncIndex, name: &str) { self.module .exports .insert(String::from(name), Export::Function(func_index)); @@ -240,7 +241,7 @@ impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data> .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: FuncIndex) { debug_assert!(self.module.start_func.is_none()); self.module.start_func = Some(func_index); } @@ -275,6 +276,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m let gv = func.create_global_value(ir::GlobalValueData::Deref { base: globals_base, offset: Offset32::new(offset32), + memory_type: self.pointer_type(), }); GlobalVariable::Memory { gv, @@ -297,10 +299,12 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m let heap_base_addr = func.create_global_value(ir::GlobalValueData::Deref { base: memories_base, offset: Offset32::new(offset32), + memory_type: self.pointer_type(), }); let heap_base = func.create_global_value(ir::GlobalValueData::Deref { base: heap_base_addr, offset: Offset32::new(0), + memory_type: self.pointer_type(), }); func.create_heap(ir::HeapData { base: heap_base, @@ -309,6 +313,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m style: ir::HeapStyle::Static { bound: 0x1_0000_0000.into(), }, + index_type: I32, }) } @@ -320,6 +325,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m let base_gv = func.create_global_value(ir::GlobalValueData::Deref { base: base_gv_addr, offset: 0.into(), + memory_type: self.pointer_type(), }); let bound_gv_addr = func.create_global_value(ir::GlobalValueData::VMContext { offset: Offset32::new(pointer_bytes as i32 * 3), @@ -327,6 +333,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m let bound_gv = func.create_global_value(ir::GlobalValueData::Deref { base: bound_gv_addr, offset: 0.into(), + memory_type: I32, }); func.create_table(ir::TableData { @@ -334,6 +341,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m min_size: Imm64::new(0), bound_gv, element_size: Imm64::new(i64::from(self.pointer_bytes() as i64)), + index_type: I32, }) } @@ -341,7 +349,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m func.import_signature(self.module.signatures[index].clone()) } - fn make_direct_func(&mut self, func: &mut ir::Function, index: FunctionIndex) -> ir::FuncRef { + fn make_direct_func(&mut self, func: &mut ir::Function, index: FuncIndex) -> ir::FuncRef { let sigidx = self.module.functions[index]; let signature = func.import_signature(self.module.signatures[sigidx].clone()); let name = get_func_name(index); @@ -369,16 +377,6 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m // so we need to implement it ourselves. debug_assert_eq!(table_index, 0, "non-default tables not supported yet"); - let callee_ty = pos.func.dfg.value_type(callee); - debug_assert_eq!(callee_ty, I32, "wasm call indirect index should be I32"); - let callee = if self.pointer_type() == I64 { - // The current limitation of `table_addr` is that the index should be - // the same type as `self.pointer_type()`. So we just extend the given - // index to 64-bit here. - pos.ins().uextend(I64, callee) - } else { - callee - }; let table_entry_addr = pos.ins().table_addr(I64, table, callee, 0); // Dereference table_entry_addr to get the function address. @@ -396,7 +394,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m fn translate_call( &mut self, mut pos: FuncCursor, - _callee_index: FunctionIndex, + _callee_index: FuncIndex, callee: ir::FuncRef, call_args: &[ir::Value], ) -> WasmResult { @@ -414,7 +412,6 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m let grow_mem_func = self.grow_memory_extfunc.unwrap_or_else(|| { let sig_ref = pos.func.import_signature(Signature { call_conv: self.isa.flags().call_conv(), - argument_bytes: None, params: vec![ AbiParam::new(I32), AbiParam::new(I32), @@ -448,7 +445,6 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m let cur_mem_func = self.current_memory_extfunc.unwrap_or_else(|| { let sig_ref = pos.func.import_signature(Signature { call_conv: self.isa.flags().call_conv(), - argument_bytes: None, params: vec![ AbiParam::new(I32), AbiParam::special(self.pointer_type(), ArgumentPurpose::VMContext), diff --git a/lib/environ/src/lib.rs b/lib/environ/src/lib.rs index 5b9bb49e04..b161c27445 100644 --- a/lib/environ/src/lib.rs +++ b/lib/environ/src/lib.rs @@ -16,6 +16,7 @@ )] extern crate cranelift_codegen; +extern crate cranelift_entity; extern crate cranelift_wasm; extern crate target_lexicon; diff --git a/lib/environ/src/module.rs b/lib/environ/src/module.rs index d9eaeacabd..15afc86768 100644 --- a/lib/environ/src/module.rs +++ b/lib/environ/src/module.rs @@ -1,8 +1,10 @@ //! Data structures for representing decoded wasm modules. use cranelift_codegen::ir; +use cranelift_entity::{EntityRef, PrimaryMap}; use cranelift_wasm::{ - FunctionIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, TableIndex, + DefinedFuncIndex, FuncIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, + TableIndex, }; use std::collections::HashMap; @@ -16,14 +18,14 @@ pub struct TableElements { /// The offset to add to the base. pub offset: usize, /// The values to write into the table elements. - pub elements: Vec, + pub elements: Vec, } /// An entity to export. #[derive(Clone, Debug)] pub enum Export { /// Function export. - Function(FunctionIndex), + Function(FuncIndex), /// Table export. Table(TableIndex), /// Memory export. @@ -43,7 +45,7 @@ pub struct Module { pub imported_funcs: Vec<(String, String)>, /// Types of functions, imported and local. - pub functions: Vec, + pub functions: PrimaryMap, /// WebAssembly tables. pub tables: Vec, @@ -58,7 +60,7 @@ pub struct Module { pub exports: HashMap, /// The module "start" function, if present. - pub start_func: Option, + pub start_func: Option, /// WebAssembly table initializers. pub table_elements: Vec, @@ -70,7 +72,7 @@ impl Module { Self { signatures: Vec::new(), imported_funcs: Vec::new(), - functions: Vec::new(), + functions: PrimaryMap::new(), tables: Vec::new(), memories: Vec::new(), globals: Vec::new(), @@ -79,6 +81,23 @@ impl Module { table_elements: Vec::new(), } } + + /// Convert a `DefinedFuncIndex` into a `FuncIndex`. + pub fn func_index(&self, defined_func: DefinedFuncIndex) -> FuncIndex { + FuncIndex::new(self.imported_funcs.len() + defined_func.index()) + } + + /// Convert a `FuncIndex` into a `DefinedFuncIndex`. Returns None if the + /// index is an imported function. + pub fn defined_func_index(&self, func: FuncIndex) -> Option { + if func.index() < self.imported_funcs.len() { + None + } else { + Some(DefinedFuncIndex::new( + func.index() - self.imported_funcs.len(), + )) + } + } } /// A data initializer for linear memory. @@ -97,7 +116,7 @@ pub struct DataInitializer<'data> { /// separately from the main module translation. pub struct LazyContents<'data> { /// References to the function bodies. - pub function_body_inputs: Vec<&'data [u8]>, + pub function_body_inputs: PrimaryMap, /// References to the data initializers. pub data_initializers: Vec>, @@ -106,7 +125,7 @@ pub struct LazyContents<'data> { impl<'data> LazyContents<'data> { pub fn new() -> Self { Self { - function_body_inputs: Vec::new(), + function_body_inputs: PrimaryMap::new(), data_initializers: Vec::new(), } } diff --git a/lib/execute/Cargo.toml b/lib/execute/Cargo.toml index 2613c17e9d..7f21748d6d 100644 --- a/lib/execute/Cargo.toml +++ b/lib/execute/Cargo.toml @@ -8,8 +8,9 @@ repository = "https://github.com/CraneStation/wasmtime" license = "Apache-2.0 WITH LLVM-exception" [dependencies] -cranelift-codegen = "0.18.1" -cranelift-wasm = "0.18.1" +cranelift-codegen = "0.20.0" +cranelift-entity = "0.20.1" +cranelift-wasm = "0.20.1" region = "0.3.0" wasmtime-environ = { path = "../environ" } memmap = "0.6.2" diff --git a/lib/execute/src/execute.rs b/lib/execute/src/execute.rs index dca7e8472c..7c5621caca 100644 --- a/lib/execute/src/execute.rs +++ b/lib/execute/src/execute.rs @@ -1,6 +1,7 @@ use cranelift_codegen::binemit::Reloc; use cranelift_codegen::isa::TargetIsa; -use cranelift_wasm::MemoryIndex; +use cranelift_entity::PrimaryMap; +use cranelift_wasm::{DefinedFuncIndex, MemoryIndex}; use instance::Instance; use memory::LinearMemory; use region::protect; @@ -17,28 +18,30 @@ 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); + relocate(&mut compilation, &relocations, &translation.module); Ok(compilation) } /// Performs the relocations inside the function bytecode, provided the necessary metadata -fn relocate(compilation: &mut Compilation, relocations: &[Vec]) { +fn relocate( + compilation: &mut Compilation, + relocations: &PrimaryMap>, + module: &Module, +) { // 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 (i, function_relocs) in relocations.iter() { for r in function_relocs { let target_func_address: isize = match r.reloc_target { - RelocationTarget::UserFunc(index) => compilation.functions[index].as_ptr() as isize, + RelocationTarget::UserFunc(index) => { + compilation.functions[module.defined_func_index(index).expect( + "relocation to imported function not supported yet", + )].as_ptr() as isize + } RelocationTarget::GrowMemory => grow_memory as isize, RelocationTarget::CurrentMemory => current_memory as isize, }; @@ -119,7 +122,7 @@ pub fn execute( .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 { + for code_buf in compilation.functions.values() { match unsafe { protect( code_buf.as_ptr(), @@ -137,7 +140,9 @@ pub fn execute( } } - let code_buf = &compilation.functions[start_index]; + let code_buf = &compilation.functions[module.defined_func_index(start_index).expect( + "imported start functions not supported yet", + )]; // Collect all memory base addresses and Vec. let mut mem_base_addrs = instance @@ -149,7 +154,7 @@ pub fn execute( // 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 + // 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()); diff --git a/lib/execute/src/instance.rs b/lib/execute/src/instance.rs index abec71a1ad..5c427527ac 100644 --- a/lib/execute/src/instance.rs +++ b/lib/execute/src/instance.rs @@ -57,7 +57,9 @@ impl Instance { let to_init = &mut self.tables[init.table_index][init.offset..init.offset + init.elements.len()]; for (i, func_idx) in init.elements.iter().enumerate() { - let code_buf = &compilation.functions[*func_idx]; + let code_buf = &compilation.functions[module.defined_func_index(*func_idx).expect( + "table element initializer with imported function not supported yet", + )]; to_init[i] = code_buf.as_ptr() as usize; } } diff --git a/lib/execute/src/lib.rs b/lib/execute/src/lib.rs index 82ec74e08c..04b7d254aa 100644 --- a/lib/execute/src/lib.rs +++ b/lib/execute/src/lib.rs @@ -13,6 +13,7 @@ )] extern crate cranelift_codegen; +extern crate cranelift_entity; extern crate cranelift_wasm; extern crate memmap; extern crate region; diff --git a/lib/obj/Cargo.toml b/lib/obj/Cargo.toml index ba755cc202..28668ab0d9 100644 --- a/lib/obj/Cargo.toml +++ b/lib/obj/Cargo.toml @@ -7,6 +7,7 @@ repository = "https://github.com/CraneStation/wasmtime" license = "Apache-2.0 WITH LLVM-exception" [dependencies] -cranelift-codegen = "0.18.1" +cranelift-codegen = "0.20.0" +cranelift-entity = "0.20.1" wasmtime-environ = { path = "../environ" } -faerie = "0.4.4" +faerie = "0.5.0" diff --git a/lib/obj/src/emit_module.rs b/lib/obj/src/emit_module.rs index 73e472f405..1c0d32994c 100644 --- a/lib/obj/src/emit_module.rs +++ b/lib/obj/src/emit_module.rs @@ -1,5 +1,6 @@ use cranelift_codegen::settings; use cranelift_codegen::settings::Configurable; +use cranelift_entity::EntityRef; use faerie::Artifact; use wasmtime_environ::{Compilation, Module, Relocations}; @@ -12,7 +13,8 @@ pub fn emit_module( relocations: &Relocations, ) -> Result<(), String> { debug_assert!( - module.start_func.is_none() || module.start_func.unwrap() >= module.imported_funcs.len(), + module.start_func.is_none() + || module.start_func.unwrap().index() >= module.imported_funcs.len(), "imported start functions not supported yet" ); @@ -21,11 +23,11 @@ pub fn emit_module( .enable("enable_verifier") .expect("Missing enable_verifier setting"); - for (i, function_relocs) in relocations.iter().enumerate() { + for (i, function_relocs) in relocations.iter() { assert!(function_relocs.is_empty(), "relocations not supported yet"); let body = &compilation.functions[i]; - let func_index = module.imported_funcs.len() + i; - let string_name = format!("wasm_function[{}]", func_index); + let func_index = module.func_index(i); + let string_name = format!("wasm_function[{}]", func_index.index()); obj.define(string_name, body.clone()) .map_err(|err| format!("{}", err))?; diff --git a/lib/obj/src/lib.rs b/lib/obj/src/lib.rs index d10f409700..5d7b949d70 100644 --- a/lib/obj/src/lib.rs +++ b/lib/obj/src/lib.rs @@ -13,6 +13,7 @@ )] extern crate cranelift_codegen; +extern crate cranelift_entity; extern crate faerie; extern crate wasmtime_environ;