Update to cranelift 0.20.0.
The biggest change is the split from FunctionIndex to DefinedFuncIndex to FuncIndex. Take better advantage of this by converting several Vecs to PrimaryMaps. Also, table_addr can now handle indices of the table index type, so we don't need to explicitly uextend them anymore.
This commit is contained in:
12
Cargo.toml
12
Cargo.toml
@@ -17,15 +17,15 @@ name = "wasm2obj"
|
|||||||
path = "src/wasm2obj.rs"
|
path = "src/wasm2obj.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cranelift-codegen = "0.18.1"
|
cranelift-codegen = "0.20.0"
|
||||||
cranelift-native = "0.18.1"
|
cranelift-native = "0.20.0"
|
||||||
wasmtime-environ = { path = "lib/environ" }
|
wasmtime-environ = { path = "lib/environ" }
|
||||||
wasmtime-execute = { path = "lib/execute" }
|
wasmtime-execute = { path = "lib/execute" }
|
||||||
wasmtime-obj = { path = "lib/obj" }
|
wasmtime-obj = { path = "lib/obj" }
|
||||||
docopt = "1.0.0"
|
docopt = "1.0.1"
|
||||||
serde = "1.0.55"
|
serde = "1.0.75"
|
||||||
serde_derive = "1.0.55"
|
serde_derive = "1.0.75"
|
||||||
tempdir = "*"
|
tempdir = "*"
|
||||||
faerie = "0.4.4"
|
faerie = "0.5.0"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ cargo-fuzz = true
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
wasmtime-environ = { path = "../lib/environ" }
|
wasmtime-environ = { path = "../lib/environ" }
|
||||||
wasmtime-execute = { path = "../lib/execute" }
|
wasmtime-execute = { path = "../lib/execute" }
|
||||||
cranelift-codegen = "0.18.1"
|
cranelift-codegen = "0.20.0"
|
||||||
cranelift-wasm = "0.18.1"
|
cranelift-wasm = "0.20.1"
|
||||||
cranelift-native = "0.18.1"
|
cranelift-native = "0.20.0"
|
||||||
libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git" }
|
libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git" }
|
||||||
wasmparser = { version = "0.17.2", default-features = false }
|
wasmparser = { version = "0.17.2", default-features = false }
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ license = "Apache-2.0 WITH LLVM-exception"
|
|||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cranelift-codegen = "0.18.1"
|
cranelift-codegen = "0.20.0"
|
||||||
cranelift-wasm = "0.18.1"
|
cranelift-entity = "0.20.1"
|
||||||
|
cranelift-wasm = "0.20.1"
|
||||||
target-lexicon = "0.0.3"
|
target-lexicon = "0.0.3"
|
||||||
|
|
||||||
[badges]
|
[badges]
|
||||||
|
|||||||
@@ -6,19 +6,20 @@ use cranelift_codegen::ir;
|
|||||||
use cranelift_codegen::ir::ExternalName;
|
use cranelift_codegen::ir::ExternalName;
|
||||||
use cranelift_codegen::isa;
|
use cranelift_codegen::isa;
|
||||||
use cranelift_codegen::Context;
|
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};
|
use environ::{get_func_name, ModuleTranslation};
|
||||||
|
|
||||||
/// The result of compiling a WebAssemby module's functions.
|
/// The result of compiling a WebAssemby module's functions.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Compilation {
|
pub struct Compilation {
|
||||||
/// Compiled machine code for the function bodies.
|
/// Compiled machine code for the function bodies.
|
||||||
pub functions: Vec<Vec<u8>>,
|
pub functions: PrimaryMap<DefinedFuncIndex, Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Compilation {
|
impl Compilation {
|
||||||
/// Allocates the compilation result with the given function bodies.
|
/// Allocates the compilation result with the given function bodies.
|
||||||
pub fn new(functions: Vec<Vec<u8>>) -> Self {
|
pub fn new(functions: PrimaryMap<DefinedFuncIndex, Vec<u8>>) -> Self {
|
||||||
Self { functions }
|
Self { functions }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -48,7 +49,7 @@ impl binemit::RelocSink for RelocSink {
|
|||||||
) {
|
) {
|
||||||
let reloc_target = if let ExternalName::User { namespace, index } = *name {
|
let reloc_target = if let ExternalName::User { namespace, index } = *name {
|
||||||
debug_assert!(namespace == 0);
|
debug_assert!(namespace == 0);
|
||||||
RelocationTarget::UserFunc(index as usize)
|
RelocationTarget::UserFunc(FuncIndex::new(index as usize))
|
||||||
} else if *name == ExternalName::testcase("grow_memory") {
|
} else if *name == ExternalName::testcase("grow_memory") {
|
||||||
RelocationTarget::GrowMemory
|
RelocationTarget::GrowMemory
|
||||||
} else if *name == ExternalName::testcase("current_memory") {
|
} else if *name == ExternalName::testcase("current_memory") {
|
||||||
@@ -82,7 +83,7 @@ impl RelocSink {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A record of a relocation to perform.
|
/// A record of a relocation to perform.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Relocation {
|
pub struct Relocation {
|
||||||
/// The relocation code.
|
/// The relocation code.
|
||||||
pub reloc: binemit::Reloc,
|
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.
|
/// Destination function. Can be either user function or some special one, like grow_memory.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub enum RelocationTarget {
|
pub enum RelocationTarget {
|
||||||
/// The user function index.
|
/// The user function index.
|
||||||
UserFunc(FunctionIndex),
|
UserFunc(FuncIndex),
|
||||||
/// Function for growing the default memory by the specified amount of pages.
|
/// Function for growing the default memory by the specified amount of pages.
|
||||||
GrowMemory,
|
GrowMemory,
|
||||||
/// Function for query current size of the default linear memory.
|
/// Function for query current size of the default linear memory.
|
||||||
@@ -106,7 +107,7 @@ pub enum RelocationTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Relocations to apply to function bodies.
|
/// Relocations to apply to function bodies.
|
||||||
pub type Relocations = Vec<Vec<Relocation>>;
|
pub type Relocations = PrimaryMap<DefinedFuncIndex, Vec<Relocation>>;
|
||||||
|
|
||||||
/// Compile the module, producing a compilation result with associated
|
/// Compile the module, producing a compilation result with associated
|
||||||
/// relocations.
|
/// relocations.
|
||||||
@@ -114,10 +115,10 @@ pub fn compile_module<'data, 'module>(
|
|||||||
translation: &ModuleTranslation<'data, 'module>,
|
translation: &ModuleTranslation<'data, 'module>,
|
||||||
isa: &isa::TargetIsa,
|
isa: &isa::TargetIsa,
|
||||||
) -> Result<(Compilation, Relocations), String> {
|
) -> Result<(Compilation, Relocations), String> {
|
||||||
let mut functions = Vec::new();
|
let mut functions = PrimaryMap::new();
|
||||||
let mut relocations = Vec::new();
|
let mut relocations = PrimaryMap::new();
|
||||||
for (i, input) in translation.lazy.function_body_inputs.iter().enumerate() {
|
for (i, input) in translation.lazy.function_body_inputs.iter() {
|
||||||
let func_index = i + translation.module.imported_funcs.len();
|
let func_index = translation.module.func_index(i);
|
||||||
let mut context = Context::new();
|
let mut context = Context::new();
|
||||||
context.func.name = get_func_name(func_index);
|
context.func.name = get_func_name(func_index);
|
||||||
context.func.signature =
|
context.func.signature =
|
||||||
|
|||||||
@@ -7,17 +7,18 @@ use cranelift_codegen::ir::{
|
|||||||
};
|
};
|
||||||
use cranelift_codegen::isa;
|
use cranelift_codegen::isa;
|
||||||
use cranelift_codegen::settings;
|
use cranelift_codegen::settings;
|
||||||
|
use cranelift_entity::EntityRef;
|
||||||
use cranelift_wasm::{
|
use cranelift_wasm::{
|
||||||
self, translate_module, FunctionIndex, Global, GlobalIndex, GlobalVariable, Memory,
|
self, translate_module, FuncIndex, Global, GlobalIndex, GlobalVariable, Memory, MemoryIndex,
|
||||||
MemoryIndex, SignatureIndex, Table, TableIndex, WasmResult,
|
SignatureIndex, Table, TableIndex, WasmResult,
|
||||||
};
|
};
|
||||||
use module::{DataInitializer, Export, LazyContents, Module, TableElements};
|
use module::{DataInitializer, Export, LazyContents, Module, TableElements};
|
||||||
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.
|
||||||
pub fn get_func_name(func_index: FunctionIndex) -> ir::ExternalName {
|
pub fn get_func_name(func_index: FuncIndex) -> ir::ExternalName {
|
||||||
debug_assert!(func_index as u32 as FunctionIndex == func_index);
|
debug_assert!(FuncIndex::new(func_index.index() as u32 as usize) == func_index);
|
||||||
ir::ExternalName::user(0, func_index as u32)
|
ir::ExternalName::user(0, func_index.index() as u32)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Object containing the standalone environment information. To be passed after creation as
|
/// 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>
|
impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data>
|
||||||
for ModuleEnvironment<'data, 'module>
|
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)
|
get_func_name(func_index)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,7 +165,7 @@ impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data>
|
|||||||
self.module.functions.push(sig_index);
|
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]
|
self.module.functions[func_index]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,7 +186,7 @@ impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data>
|
|||||||
table_index: TableIndex,
|
table_index: TableIndex,
|
||||||
base: Option<GlobalIndex>,
|
base: Option<GlobalIndex>,
|
||||||
offset: usize,
|
offset: usize,
|
||||||
elements: Vec<FunctionIndex>,
|
elements: Vec<FuncIndex>,
|
||||||
) {
|
) {
|
||||||
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(TableElements {
|
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
|
self.module
|
||||||
.exports
|
.exports
|
||||||
.insert(String::from(name), Export::Function(func_index));
|
.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));
|
.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());
|
debug_assert!(self.module.start_func.is_none());
|
||||||
self.module.start_func = Some(func_index);
|
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 {
|
let gv = func.create_global_value(ir::GlobalValueData::Deref {
|
||||||
base: globals_base,
|
base: globals_base,
|
||||||
offset: Offset32::new(offset32),
|
offset: Offset32::new(offset32),
|
||||||
|
memory_type: self.pointer_type(),
|
||||||
});
|
});
|
||||||
GlobalVariable::Memory {
|
GlobalVariable::Memory {
|
||||||
gv,
|
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 {
|
let heap_base_addr = func.create_global_value(ir::GlobalValueData::Deref {
|
||||||
base: memories_base,
|
base: memories_base,
|
||||||
offset: Offset32::new(offset32),
|
offset: Offset32::new(offset32),
|
||||||
|
memory_type: self.pointer_type(),
|
||||||
});
|
});
|
||||||
let heap_base = func.create_global_value(ir::GlobalValueData::Deref {
|
let heap_base = func.create_global_value(ir::GlobalValueData::Deref {
|
||||||
base: heap_base_addr,
|
base: heap_base_addr,
|
||||||
offset: Offset32::new(0),
|
offset: Offset32::new(0),
|
||||||
|
memory_type: self.pointer_type(),
|
||||||
});
|
});
|
||||||
func.create_heap(ir::HeapData {
|
func.create_heap(ir::HeapData {
|
||||||
base: heap_base,
|
base: heap_base,
|
||||||
@@ -309,6 +313,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
|
|||||||
style: ir::HeapStyle::Static {
|
style: ir::HeapStyle::Static {
|
||||||
bound: 0x1_0000_0000.into(),
|
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 {
|
let base_gv = func.create_global_value(ir::GlobalValueData::Deref {
|
||||||
base: base_gv_addr,
|
base: base_gv_addr,
|
||||||
offset: 0.into(),
|
offset: 0.into(),
|
||||||
|
memory_type: self.pointer_type(),
|
||||||
});
|
});
|
||||||
let bound_gv_addr = func.create_global_value(ir::GlobalValueData::VMContext {
|
let bound_gv_addr = func.create_global_value(ir::GlobalValueData::VMContext {
|
||||||
offset: Offset32::new(pointer_bytes as i32 * 3),
|
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 {
|
let bound_gv = func.create_global_value(ir::GlobalValueData::Deref {
|
||||||
base: bound_gv_addr,
|
base: bound_gv_addr,
|
||||||
offset: 0.into(),
|
offset: 0.into(),
|
||||||
|
memory_type: I32,
|
||||||
});
|
});
|
||||||
|
|
||||||
func.create_table(ir::TableData {
|
func.create_table(ir::TableData {
|
||||||
@@ -334,6 +341,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
|
|||||||
min_size: Imm64::new(0),
|
min_size: Imm64::new(0),
|
||||||
bound_gv,
|
bound_gv,
|
||||||
element_size: Imm64::new(i64::from(self.pointer_bytes() as i64)),
|
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())
|
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 sigidx = self.module.functions[index];
|
||||||
let signature = func.import_signature(self.module.signatures[sigidx].clone());
|
let signature = func.import_signature(self.module.signatures[sigidx].clone());
|
||||||
let name = get_func_name(index);
|
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.
|
// so we need to implement it ourselves.
|
||||||
debug_assert_eq!(table_index, 0, "non-default tables not supported yet");
|
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);
|
let table_entry_addr = pos.ins().table_addr(I64, table, callee, 0);
|
||||||
|
|
||||||
// Dereference table_entry_addr to get the function address.
|
// 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(
|
fn translate_call(
|
||||||
&mut self,
|
&mut self,
|
||||||
mut pos: FuncCursor,
|
mut pos: FuncCursor,
|
||||||
_callee_index: FunctionIndex,
|
_callee_index: FuncIndex,
|
||||||
callee: ir::FuncRef,
|
callee: ir::FuncRef,
|
||||||
call_args: &[ir::Value],
|
call_args: &[ir::Value],
|
||||||
) -> WasmResult<ir::Inst> {
|
) -> WasmResult<ir::Inst> {
|
||||||
@@ -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 grow_mem_func = self.grow_memory_extfunc.unwrap_or_else(|| {
|
||||||
let sig_ref = pos.func.import_signature(Signature {
|
let sig_ref = pos.func.import_signature(Signature {
|
||||||
call_conv: self.isa.flags().call_conv(),
|
call_conv: self.isa.flags().call_conv(),
|
||||||
argument_bytes: None,
|
|
||||||
params: vec![
|
params: vec![
|
||||||
AbiParam::new(I32),
|
AbiParam::new(I32),
|
||||||
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 cur_mem_func = self.current_memory_extfunc.unwrap_or_else(|| {
|
||||||
let sig_ref = pos.func.import_signature(Signature {
|
let sig_ref = pos.func.import_signature(Signature {
|
||||||
call_conv: self.isa.flags().call_conv(),
|
call_conv: self.isa.flags().call_conv(),
|
||||||
argument_bytes: None,
|
|
||||||
params: vec![
|
params: vec![
|
||||||
AbiParam::new(I32),
|
AbiParam::new(I32),
|
||||||
AbiParam::special(self.pointer_type(), ArgumentPurpose::VMContext),
|
AbiParam::special(self.pointer_type(), ArgumentPurpose::VMContext),
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
)]
|
)]
|
||||||
|
|
||||||
extern crate cranelift_codegen;
|
extern crate cranelift_codegen;
|
||||||
|
extern crate cranelift_entity;
|
||||||
extern crate cranelift_wasm;
|
extern crate cranelift_wasm;
|
||||||
extern crate target_lexicon;
|
extern crate target_lexicon;
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
//! Data structures for representing decoded wasm modules.
|
//! Data structures for representing decoded wasm modules.
|
||||||
|
|
||||||
use cranelift_codegen::ir;
|
use cranelift_codegen::ir;
|
||||||
|
use cranelift_entity::{EntityRef, PrimaryMap};
|
||||||
use cranelift_wasm::{
|
use cranelift_wasm::{
|
||||||
FunctionIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, TableIndex,
|
DefinedFuncIndex, FuncIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table,
|
||||||
|
TableIndex,
|
||||||
};
|
};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
@@ -16,14 +18,14 @@ pub struct TableElements {
|
|||||||
/// The offset to add to the base.
|
/// The offset to add to the base.
|
||||||
pub offset: usize,
|
pub offset: usize,
|
||||||
/// The values to write into the table elements.
|
/// The values to write into the table elements.
|
||||||
pub elements: Vec<FunctionIndex>,
|
pub elements: Vec<FuncIndex>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An entity to export.
|
/// An entity to export.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Export {
|
pub enum Export {
|
||||||
/// Function export.
|
/// Function export.
|
||||||
Function(FunctionIndex),
|
Function(FuncIndex),
|
||||||
/// Table export.
|
/// Table export.
|
||||||
Table(TableIndex),
|
Table(TableIndex),
|
||||||
/// Memory export.
|
/// Memory export.
|
||||||
@@ -43,7 +45,7 @@ pub struct Module {
|
|||||||
pub imported_funcs: Vec<(String, String)>,
|
pub imported_funcs: Vec<(String, String)>,
|
||||||
|
|
||||||
/// Types of functions, imported and local.
|
/// Types of functions, imported and local.
|
||||||
pub functions: Vec<SignatureIndex>,
|
pub functions: PrimaryMap<FuncIndex, SignatureIndex>,
|
||||||
|
|
||||||
/// WebAssembly tables.
|
/// WebAssembly tables.
|
||||||
pub tables: Vec<Table>,
|
pub tables: Vec<Table>,
|
||||||
@@ -58,7 +60,7 @@ pub struct Module {
|
|||||||
pub exports: HashMap<String, Export>,
|
pub exports: HashMap<String, Export>,
|
||||||
|
|
||||||
/// The module "start" function, if present.
|
/// The module "start" function, if present.
|
||||||
pub start_func: Option<FunctionIndex>,
|
pub start_func: Option<FuncIndex>,
|
||||||
|
|
||||||
/// WebAssembly table initializers.
|
/// WebAssembly table initializers.
|
||||||
pub table_elements: Vec<TableElements>,
|
pub table_elements: Vec<TableElements>,
|
||||||
@@ -70,7 +72,7 @@ impl Module {
|
|||||||
Self {
|
Self {
|
||||||
signatures: Vec::new(),
|
signatures: Vec::new(),
|
||||||
imported_funcs: Vec::new(),
|
imported_funcs: Vec::new(),
|
||||||
functions: Vec::new(),
|
functions: PrimaryMap::new(),
|
||||||
tables: Vec::new(),
|
tables: Vec::new(),
|
||||||
memories: Vec::new(),
|
memories: Vec::new(),
|
||||||
globals: Vec::new(),
|
globals: Vec::new(),
|
||||||
@@ -79,6 +81,23 @@ impl Module {
|
|||||||
table_elements: Vec::new(),
|
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<DefinedFuncIndex> {
|
||||||
|
if func.index() < self.imported_funcs.len() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(DefinedFuncIndex::new(
|
||||||
|
func.index() - self.imported_funcs.len(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A data initializer for linear memory.
|
/// A data initializer for linear memory.
|
||||||
@@ -97,7 +116,7 @@ pub struct DataInitializer<'data> {
|
|||||||
/// separately from the main module translation.
|
/// separately from the main module translation.
|
||||||
pub struct LazyContents<'data> {
|
pub struct LazyContents<'data> {
|
||||||
/// References to the function bodies.
|
/// References to the function bodies.
|
||||||
pub function_body_inputs: Vec<&'data [u8]>,
|
pub function_body_inputs: PrimaryMap<DefinedFuncIndex, &'data [u8]>,
|
||||||
|
|
||||||
/// References to the data initializers.
|
/// References to the data initializers.
|
||||||
pub data_initializers: Vec<DataInitializer<'data>>,
|
pub data_initializers: Vec<DataInitializer<'data>>,
|
||||||
@@ -106,7 +125,7 @@ pub struct LazyContents<'data> {
|
|||||||
impl<'data> LazyContents<'data> {
|
impl<'data> LazyContents<'data> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
function_body_inputs: Vec::new(),
|
function_body_inputs: PrimaryMap::new(),
|
||||||
data_initializers: Vec::new(),
|
data_initializers: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,9 @@ repository = "https://github.com/CraneStation/wasmtime"
|
|||||||
license = "Apache-2.0 WITH LLVM-exception"
|
license = "Apache-2.0 WITH LLVM-exception"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cranelift-codegen = "0.18.1"
|
cranelift-codegen = "0.20.0"
|
||||||
cranelift-wasm = "0.18.1"
|
cranelift-entity = "0.20.1"
|
||||||
|
cranelift-wasm = "0.20.1"
|
||||||
region = "0.3.0"
|
region = "0.3.0"
|
||||||
wasmtime-environ = { path = "../environ" }
|
wasmtime-environ = { path = "../environ" }
|
||||||
memmap = "0.6.2"
|
memmap = "0.6.2"
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use cranelift_codegen::binemit::Reloc;
|
use cranelift_codegen::binemit::Reloc;
|
||||||
use cranelift_codegen::isa::TargetIsa;
|
use cranelift_codegen::isa::TargetIsa;
|
||||||
use cranelift_wasm::MemoryIndex;
|
use cranelift_entity::PrimaryMap;
|
||||||
|
use cranelift_wasm::{DefinedFuncIndex, MemoryIndex};
|
||||||
use instance::Instance;
|
use instance::Instance;
|
||||||
use memory::LinearMemory;
|
use memory::LinearMemory;
|
||||||
use region::protect;
|
use region::protect;
|
||||||
@@ -17,28 +18,30 @@ pub fn compile_and_link_module<'data, 'module>(
|
|||||||
isa: &TargetIsa,
|
isa: &TargetIsa,
|
||||||
translation: &ModuleTranslation<'data, 'module>,
|
translation: &ModuleTranslation<'data, 'module>,
|
||||||
) -> Result<Compilation, String> {
|
) -> 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)?;
|
let (mut compilation, relocations) = compile_module(&translation, isa)?;
|
||||||
|
|
||||||
// Apply relocations, now that we have virtual addresses for everything.
|
// Apply relocations, now that we have virtual addresses for everything.
|
||||||
relocate(&mut compilation, &relocations);
|
relocate(&mut compilation, &relocations, &translation.module);
|
||||||
|
|
||||||
Ok(compilation)
|
Ok(compilation)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Performs the relocations inside the function bytecode, provided the necessary metadata
|
/// Performs the relocations inside the function bytecode, provided the necessary metadata
|
||||||
fn relocate(compilation: &mut Compilation, relocations: &[Vec<Relocation>]) {
|
fn relocate(
|
||||||
|
compilation: &mut Compilation,
|
||||||
|
relocations: &PrimaryMap<DefinedFuncIndex, Vec<Relocation>>,
|
||||||
|
module: &Module,
|
||||||
|
) {
|
||||||
// The relocations are relative to the relocation's address plus four bytes
|
// The relocations are relative to the relocation's address plus four bytes
|
||||||
// TODO: Support architectures other than x64, and other reloc kinds.
|
// 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 {
|
for r in function_relocs {
|
||||||
let target_func_address: isize = match r.reloc_target {
|
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::GrowMemory => grow_memory as isize,
|
||||||
RelocationTarget::CurrentMemory => current_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"))?;
|
.ok_or_else(|| String::from("No start function defined, aborting execution"))?;
|
||||||
// TODO: Put all the function bodies into a page-aligned memory region, and
|
// TODO: Put all the function bodies into a page-aligned memory region, and
|
||||||
// then make them ReadExecute rather than ReadWriteExecute.
|
// then make them ReadExecute rather than ReadWriteExecute.
|
||||||
for code_buf in &compilation.functions {
|
for code_buf in compilation.functions.values() {
|
||||||
match unsafe {
|
match unsafe {
|
||||||
protect(
|
protect(
|
||||||
code_buf.as_ptr(),
|
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.
|
// Collect all memory base addresses and Vec.
|
||||||
let mut mem_base_addrs = instance
|
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
|
// 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 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.
|
// Rust function and call it.
|
||||||
unsafe {
|
unsafe {
|
||||||
let start_func = transmute::<_, fn(*const *mut u8)>(code_buf.as_ptr());
|
let start_func = transmute::<_, fn(*const *mut u8)>(code_buf.as_ptr());
|
||||||
|
|||||||
@@ -57,7 +57,9 @@ impl Instance {
|
|||||||
let to_init =
|
let to_init =
|
||||||
&mut self.tables[init.table_index][init.offset..init.offset + init.elements.len()];
|
&mut self.tables[init.table_index][init.offset..init.offset + init.elements.len()];
|
||||||
for (i, func_idx) in init.elements.iter().enumerate() {
|
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;
|
to_init[i] = code_buf.as_ptr() as usize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
)]
|
)]
|
||||||
|
|
||||||
extern crate cranelift_codegen;
|
extern crate cranelift_codegen;
|
||||||
|
extern crate cranelift_entity;
|
||||||
extern crate cranelift_wasm;
|
extern crate cranelift_wasm;
|
||||||
extern crate memmap;
|
extern crate memmap;
|
||||||
extern crate region;
|
extern crate region;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ repository = "https://github.com/CraneStation/wasmtime"
|
|||||||
license = "Apache-2.0 WITH LLVM-exception"
|
license = "Apache-2.0 WITH LLVM-exception"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cranelift-codegen = "0.18.1"
|
cranelift-codegen = "0.20.0"
|
||||||
|
cranelift-entity = "0.20.1"
|
||||||
wasmtime-environ = { path = "../environ" }
|
wasmtime-environ = { path = "../environ" }
|
||||||
faerie = "0.4.4"
|
faerie = "0.5.0"
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use cranelift_codegen::settings;
|
use cranelift_codegen::settings;
|
||||||
use cranelift_codegen::settings::Configurable;
|
use cranelift_codegen::settings::Configurable;
|
||||||
|
use cranelift_entity::EntityRef;
|
||||||
use faerie::Artifact;
|
use faerie::Artifact;
|
||||||
use wasmtime_environ::{Compilation, Module, Relocations};
|
use wasmtime_environ::{Compilation, Module, Relocations};
|
||||||
|
|
||||||
@@ -12,7 +13,8 @@ pub fn emit_module(
|
|||||||
relocations: &Relocations,
|
relocations: &Relocations,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
debug_assert!(
|
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"
|
"imported start functions not supported yet"
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -21,11 +23,11 @@ pub fn emit_module(
|
|||||||
.enable("enable_verifier")
|
.enable("enable_verifier")
|
||||||
.expect("Missing enable_verifier setting");
|
.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");
|
assert!(function_relocs.is_empty(), "relocations not supported yet");
|
||||||
let body = &compilation.functions[i];
|
let body = &compilation.functions[i];
|
||||||
let func_index = module.imported_funcs.len() + i;
|
let func_index = module.func_index(i);
|
||||||
let string_name = format!("wasm_function[{}]", func_index);
|
let string_name = format!("wasm_function[{}]", func_index.index());
|
||||||
|
|
||||||
obj.define(string_name, body.clone())
|
obj.define(string_name, body.clone())
|
||||||
.map_err(|err| format!("{}", err))?;
|
.map_err(|err| format!("{}", err))?;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
)]
|
)]
|
||||||
|
|
||||||
extern crate cranelift_codegen;
|
extern crate cranelift_codegen;
|
||||||
|
extern crate cranelift_entity;
|
||||||
extern crate faerie;
|
extern crate faerie;
|
||||||
extern crate wasmtime_environ;
|
extern crate wasmtime_environ;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user