Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for symbol maps #294

Merged
merged 11 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ include = [
[dependencies]
structopt = "0.2.11"
wabt = "0.7.2"
hashbrown = "0.1.8"
wasmer-clif-backend = { path = "lib/clif-backend" }
wasmer-runtime = { path = "lib/runtime" }
wasmer-runtime-core = { path = "lib/runtime-core" }
Expand Down
11 changes: 8 additions & 3 deletions lib/clif-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use target_lexicon::Triple;

use wasmer_runtime_core::cache::{Artifact, Error as CacheError};
use wasmer_runtime_core::{
backend::{Compiler, Token},
backend::{Compiler, CompilerConfig, Token},
error::{CompileError, CompileResult},
module::ModuleInner,
};
Expand All @@ -39,12 +39,17 @@ impl CraneliftCompiler {

impl Compiler for CraneliftCompiler {
/// Compiles wasm binary to a wasmer module.
fn compile(&self, wasm: &[u8], _: Token) -> CompileResult<ModuleInner> {
fn compile(
&self,
wasm: &[u8],
compiler_config: CompilerConfig,
_: Token,
) -> CompileResult<ModuleInner> {
validate(wasm)?;

let isa = get_isa();

let mut module = module::Module::new();
let mut module = module::Module::new(&compiler_config);
let module_env = module_env::ModuleEnv::new(&mut module, &*isa);

let func_bodies = module_env.translate(wasm)?;
Expand Down
5 changes: 3 additions & 2 deletions lib/clif-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::Arc;
use wasmer_runtime_core::cache::{Artifact, Error as CacheError};

use wasmer_runtime_core::{
backend::Backend,
backend::{Backend, CompilerConfig},
error::CompileResult,
module::{ModuleInfo, ModuleInner, StringTable},
structures::{Map, TypedIndex},
Expand All @@ -25,7 +25,7 @@ pub struct Module {
}

impl Module {
pub fn new() -> Self {
pub fn new(compiler_config: &CompilerConfig) -> Self {
Self {
info: ModuleInfo {
memories: Map::new(),
Expand All @@ -50,6 +50,7 @@ impl Module {

namespace_table: StringTable::new(),
name_table: StringTable::new(),
em_symbol_map: compiler_config.symbol_map.clone(),
},
}
}
Expand Down
20 changes: 13 additions & 7 deletions lib/clif-backend/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,20 +374,26 @@ fn round_up(n: usize, multiple: usize) -> usize {
}

extern "C" fn i32_print(_ctx: &mut vm::Ctx, n: i32) {
print!(" i32: {},", n);
eprint!(" i32: {},", n);
}
extern "C" fn i64_print(_ctx: &mut vm::Ctx, n: i64) {
print!(" i64: {},", n);
eprint!(" i64: {},", n);
}
extern "C" fn f32_print(_ctx: &mut vm::Ctx, n: f32) {
print!(" f32: {},", n);
eprint!(" f32: {},", n);
}
extern "C" fn f64_print(_ctx: &mut vm::Ctx, n: f64) {
print!(" f64: {},", n);
eprint!(" f64: {},", n);
}
extern "C" fn start_debug(_ctx: &mut vm::Ctx, func_index: u32) {
print!("func ({}), args: [", func_index);
extern "C" fn start_debug(ctx: &mut vm::Ctx, func_index: u32) {
if let Some(symbol_map) = unsafe { ctx.borrow_symbol_map() } {
if let Some(fn_name) = symbol_map.get(&func_index) {
eprint!("func ({} ({})), args: [", fn_name, func_index);
return;
}
}
eprint!("func ({}), args: [", func_index);
}
extern "C" fn end_debug(_ctx: &mut vm::Ctx) {
println!(" ]");
eprintln!(" ]");
}
11 changes: 8 additions & 3 deletions lib/dynasm-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod stack;
use crate::codegen::{CodegenError, ModuleCodeGenerator};
use crate::parse::LoadError;
use wasmer_runtime_core::{
backend::{sys::Memory, Backend, CacheGen, Compiler, Token},
backend::{sys::Memory, Backend, CacheGen, Compiler, CompilerConfig, Token},
cache::{Artifact, Error as CacheError},
error::{CompileError, CompileResult},
module::{ModuleInfo, ModuleInner},
Expand All @@ -51,9 +51,14 @@ impl SinglePassCompiler {
}

impl Compiler for SinglePassCompiler {
fn compile(&self, wasm: &[u8], _: Token) -> CompileResult<ModuleInner> {
fn compile(
&self,
wasm: &[u8],
compiler_config: CompilerConfig,
_: Token,
) -> CompileResult<ModuleInner> {
let mut mcg = codegen_x64::X64ModuleCodeGenerator::new();
let info = parse::read_module(wasm, Backend::Dynasm, &mut mcg)?;
let info = parse::read_module(wasm, Backend::Dynasm, &mut mcg, &compiler_config)?;
let (ec, resolver) = mcg.finalize(&info)?;
Ok(ModuleInner {
cache_gen: Box::new(Placeholder),
Expand Down
5 changes: 4 additions & 1 deletion lib/dynasm-backend/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::codegen::{CodegenError, FunctionCodeGenerator, ModuleCodeGenerator};
use wasmer_runtime_core::{
backend::{Backend, FuncResolver, ProtectedCaller},
backend::{Backend, CompilerConfig, FuncResolver, ProtectedCaller},
module::{
DataInitializer, ExportIndex, ImportName, ModuleInfo, StringTable, StringTableBuilder,
TableInitializer,
Expand Down Expand Up @@ -70,6 +70,7 @@ pub fn read_module<
wasm: &[u8],
backend: Backend,
mcg: &mut MCG,
compiler_config: &CompilerConfig,
) -> Result<ModuleInfo, LoadError> {
validate(wasm)?;
let mut info = ModuleInfo {
Expand All @@ -95,6 +96,8 @@ pub fn read_module<

namespace_table: StringTable::new(),
name_table: StringTable::new(),

em_symbol_map: compiler_config.symbol_map.clone(),
};

let mut reader = ModuleReader::new(wasm)?;
Expand Down
13 changes: 9 additions & 4 deletions lib/llvm-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use inkwell::{
OptimizationLevel,
};
use wasmer_runtime_core::{
backend::{Compiler, Token},
backend::{Compiler, CompilerConfig, Token},
cache::{Artifact, Error as CacheError},
error::CompileError,
module::ModuleInner,
Expand All @@ -32,10 +32,15 @@ impl LLVMCompiler {
}

impl Compiler for LLVMCompiler {
fn compile(&self, wasm: &[u8], _: Token) -> Result<ModuleInner, CompileError> {
fn compile(
&self,
wasm: &[u8],
compiler_config: CompilerConfig,
_: Token,
) -> Result<ModuleInner, CompileError> {
validate(wasm)?;

let (info, code_reader) = read_info::read_module(wasm).unwrap();
let (info, code_reader) = read_info::read_module(wasm, compiler_config).unwrap();
let (module, intrinsics) = code::parse_function_bodies(&info, code_reader).unwrap();

let (backend, protected_caller) = backend::LLVMBackend::new(module, intrinsics);
Expand Down Expand Up @@ -121,7 +126,7 @@ fn test_read_module() {
"#;
let wasm = wat2wasm(wat).unwrap();

let (info, code_reader) = read_info::read_module(&wasm).unwrap();
let (info, code_reader) = read_info::read_module(&wasm, Default::default()).unwrap();

let (module, intrinsics) = code::parse_function_bodies(&info, code_reader).unwrap();

Expand Down
9 changes: 7 additions & 2 deletions lib/llvm-backend/src/read_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use wasmer_runtime_core::{
backend::Backend,
backend::{Backend, CompilerConfig},
module::{
DataInitializer, ExportIndex, ImportName, ModuleInfo, StringTable, StringTableBuilder,
TableInitializer,
Expand All @@ -18,7 +18,10 @@ use wasmparser::{
SectionCode, Type as WpType,
};

pub fn read_module(wasm: &[u8]) -> Result<(ModuleInfo, CodeSectionReader), BinaryReaderError> {
pub fn read_module(
wasm: &[u8],
compiler_config: CompilerConfig,
) -> Result<(ModuleInfo, CodeSectionReader), BinaryReaderError> {
let mut info = ModuleInfo {
memories: Map::new(),
globals: Map::new(),
Expand All @@ -42,6 +45,8 @@ pub fn read_module(wasm: &[u8]) -> Result<(ModuleInfo, CodeSectionReader), Binar

namespace_table: StringTable::new(),
name_table: StringTable::new(),

em_symbol_map: compiler_config.symbol_map.clone(),
};

let mut reader = ModuleReader::new(wasm)?;
Expand Down
21 changes: 20 additions & 1 deletion lib/runtime-core/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use crate::{
};
use std::{any::Any, ptr::NonNull};

use hashbrown::HashMap;

pub mod sys {
pub use crate::sys::*;
}
Expand All @@ -38,11 +40,28 @@ impl Token {
}
}

/// Configuration data for the compiler
pub struct CompilerConfig {
/// Symbol information generated from emscripten; used for more detailed debug messages
pub symbol_map: Option<HashMap<u32, String>>,
}

impl Default for CompilerConfig {
fn default() -> CompilerConfig {
CompilerConfig { symbol_map: None }
}
}

pub trait Compiler {
/// Compiles a `Module` from WebAssembly binary format.
/// The `CompileToken` parameter ensures that this can only
/// be called from inside the runtime.
fn compile(&self, wasm: &[u8], _: Token) -> CompileResult<ModuleInner>;
fn compile(
&self,
wasm: &[u8],
comp_conf: CompilerConfig,
_: Token,
) -> CompileResult<ModuleInner>;

unsafe fn from_cache(&self, cache: Artifact, _: Token) -> Result<ModuleInner, CacheError>;
}
Expand Down
15 changes: 14 additions & 1 deletion lib/runtime-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,20 @@ pub fn compile_with(
) -> CompileResult<module::Module> {
let token = backend::Token::generate();
compiler
.compile(wasm, token)
.compile(wasm, Default::default(), token)
.map(|inner| module::Module::new(Arc::new(inner)))
}

/// The same as `compile_with` but changes the compiler behavior
/// with the values in the `CompilerConfig`
pub fn compile_with_config(
wasm: &[u8],
compiler: &dyn backend::Compiler,
compiler_config: backend::CompilerConfig,
) -> CompileResult<module::Module> {
let token = backend::Token::generate();
compiler
.compile(wasm, compiler_config, token)
.map(|inner| module::Module::new(Arc::new(inner)))
}

Expand Down
3 changes: 3 additions & 0 deletions lib/runtime-core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pub struct ModuleInfo {

pub namespace_table: StringTable<NamespaceIndex>,
pub name_table: StringTable<NameIndex>,

/// Symbol information from emscripten
pub em_symbol_map: Option<HashMap<u32, String>>,
}

/// A compiled WebAssembly module.
Expand Down
9 changes: 9 additions & 0 deletions lib/runtime-core/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::{
};
use std::{ffi::c_void, mem, ptr};

use hashbrown::HashMap;

/// The context of the currently running WebAssembly instance.
///
///
Expand Down Expand Up @@ -156,6 +158,11 @@ impl Ctx {
},
}
}

/// Gives access to the emscripten symbol map, used for debugging
pub unsafe fn borrow_symbol_map(&self) -> &Option<HashMap<u32, String>> {
&(*self.module).info.em_symbol_map
}
}

#[doc(hidden)]
Expand Down Expand Up @@ -608,6 +615,8 @@ mod vm_ctx_tests {

namespace_table: StringTable::new(),
name_table: StringTable::new(),

em_symbol_map: None,
},
}
}
Expand Down
11 changes: 10 additions & 1 deletion lib/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub mod units {

pub mod cache;

use wasmer_runtime_core::backend::Compiler;
use wasmer_runtime_core::backend::{Compiler, CompilerConfig};

/// Compile WebAssembly binary code into a [`Module`].
/// This function is useful if it is necessary to
Expand All @@ -129,6 +129,15 @@ pub fn compile(wasm: &[u8]) -> error::CompileResult<Module> {
wasmer_runtime_core::compile_with(&wasm[..], default_compiler())
}

/// The same as `compile` but takes a `CompilerConfig` for the purpose of
/// changing the compiler's behavior
pub fn compile_with_config(
wasm: &[u8],
compiler_config: CompilerConfig,
) -> error::CompileResult<Module> {
wasmer_runtime_core::compile_with_config(&wasm[..], default_compiler(), compiler_config)
}

/// Compile and instantiate WebAssembly code without
/// creating a [`Module`].
///
Expand Down
Loading