Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Preserve global wasm module offset in SourceLoc.
Browse files Browse the repository at this point in the history
  • Loading branch information
yurydelendik authored and bnjbvr committed Mar 5, 2019
1 parent 5173264 commit 37dffda
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
8 changes: 6 additions & 2 deletions cranelift-wasm/src/environ/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,11 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
self.info.start_func = Some(func_index);
}

fn define_function_body(&mut self, body_bytes: &'data [u8]) -> WasmResult<()> {
fn define_function_body(
&mut self,
body_bytes: &'data [u8],
body_offset: usize,
) -> WasmResult<()> {
let func = {
let mut func_environ = DummyFuncEnvironment::new(&self.info, self.return_mode);
let func_index =
Expand All @@ -467,7 +471,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
let sig = func_environ.vmctx_sig(self.get_func_type(func_index));
let mut func = ir::Function::with_name_signature(name, sig);
self.trans
.translate(body_bytes, &mut func, &mut func_environ)?;
.translate(body_bytes, body_offset, &mut func, &mut func_environ)?;
func
};
self.func_bytecode_sizes.push(body_bytes.len());
Expand Down
6 changes: 5 additions & 1 deletion cranelift-wasm/src/environ/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,11 @@ pub trait ModuleEnvironment<'data> {
///
/// Note there's no `reserve_function_bodies` function because the number of
/// functions is already provided by `reserve_func_types`.
fn define_function_body(&mut self, body_bytes: &'data [u8]) -> WasmResult<()>;
fn define_function_body(
&mut self,
body_bytes: &'data [u8],
body_offset: usize,
) -> WasmResult<()>;

/// Provides the number of data initializers up front. By default this does nothing, but
/// implementations can use this to preallocate memory if desired.
Expand Down
21 changes: 12 additions & 9 deletions cranelift-wasm/src/func_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,15 @@ impl FuncTranslator {
pub fn translate<FE: FuncEnvironment + ?Sized>(
&mut self,
code: &[u8],
code_offset: usize,
func: &mut ir::Function,
environ: &mut FE,
) -> WasmResult<()> {
self.translate_from_reader(BinaryReader::new(code), func, environ)
self.translate_from_reader(
BinaryReader::new_with_offset(code, code_offset),
func,
environ,
)
}

/// Translate a binary WebAssembly function from a `BinaryReader`.
Expand Down Expand Up @@ -222,11 +227,9 @@ fn parse_function_body<FE: FuncEnvironment + ?Sized>(

/// Get the current source location from a reader.
fn cur_srcloc(reader: &BinaryReader) -> ir::SourceLoc {
// We record source locations as byte code offsets relative to the beginning of the function.
// This will wrap around of a single function's byte code is larger than 4 GB, but a) the
// WebAssembly format doesn't allow for that, and b) that would hit other Cranelift
// implementation limits anyway.
ir::SourceLoc::new(reader.current_position() as u32)
// We record source locations as byte code offsets relative to the beginning of the file.
// This will wrap around if byte code is larger than 4 GB.
ir::SourceLoc::new(reader.original_position() as u32)
}

#[cfg(test)]
Expand Down Expand Up @@ -270,7 +273,7 @@ mod tests {
ctx.func.signature.returns.push(ir::AbiParam::new(I32));

trans
.translate(&BODY, &mut ctx.func, &mut runtime.func_env())
.translate(&BODY, 0, &mut ctx.func, &mut runtime.func_env())
.unwrap();
debug!("{}", ctx.func.display(None));
ctx.verify(&flags).unwrap();
Expand Down Expand Up @@ -308,7 +311,7 @@ mod tests {
ctx.func.signature.returns.push(ir::AbiParam::new(I32));

trans
.translate(&BODY, &mut ctx.func, &mut runtime.func_env())
.translate(&BODY, 0, &mut ctx.func, &mut runtime.func_env())
.unwrap();
debug!("{}", ctx.func.display(None));
ctx.verify(&flags).unwrap();
Expand Down Expand Up @@ -354,7 +357,7 @@ mod tests {
ctx.func.signature.returns.push(ir::AbiParam::new(I32));

trans
.translate(&BODY, &mut ctx.func, &mut runtime.func_env())
.translate(&BODY, 0, &mut ctx.func, &mut runtime.func_env())
.unwrap();
debug!("{}", ctx.func.display(None));
ctx.verify(&flags).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion cranelift-wasm/src/sections_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ pub fn parse_code_section<'data>(
for body in code {
let mut reader = body?.get_binary_reader();
let size = reader.bytes_remaining();
environ.define_function_body(reader.read_bytes(size)?)?;
let offset = reader.original_position();
environ.define_function_body(reader.read_bytes(size)?, offset)?;
}
Ok(())
}
Expand Down

0 comments on commit 37dffda

Please sign in to comment.