Preserve global wasm module offset in SourceLoc.

This commit is contained in:
Yury Delendik
2019-01-22 16:48:31 -06:00
committed by Benjamin Bouvier
parent 2a519092a0
commit 27b0933a4a
4 changed files with 25 additions and 13 deletions

View File

@@ -458,7 +458,11 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
self.info.start_func = Some(func_index); 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 func = {
let mut func_environ = DummyFuncEnvironment::new(&self.info, self.return_mode); let mut func_environ = DummyFuncEnvironment::new(&self.info, self.return_mode);
let func_index = let func_index =
@@ -467,7 +471,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
let sig = func_environ.vmctx_sig(self.get_func_type(func_index)); let sig = func_environ.vmctx_sig(self.get_func_type(func_index));
let mut func = ir::Function::with_name_signature(name, sig); let mut func = ir::Function::with_name_signature(name, sig);
self.trans self.trans
.translate(body_bytes, &mut func, &mut func_environ)?; .translate(body_bytes, body_offset, &mut func, &mut func_environ)?;
func func
}; };
self.func_bytecode_sizes.push(body_bytes.len()); self.func_bytecode_sizes.push(body_bytes.len());

View File

@@ -343,7 +343,11 @@ pub trait ModuleEnvironment<'data> {
/// ///
/// Note there's no `reserve_function_bodies` function because the number of /// Note there's no `reserve_function_bodies` function because the number of
/// functions is already provided by `reserve_func_types`. /// 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 /// Provides the number of data initializers up front. By default this does nothing, but
/// implementations can use this to preallocate memory if desired. /// implementations can use this to preallocate memory if desired.

View File

@@ -54,10 +54,15 @@ impl FuncTranslator {
pub fn translate<FE: FuncEnvironment + ?Sized>( pub fn translate<FE: FuncEnvironment + ?Sized>(
&mut self, &mut self,
code: &[u8], code: &[u8],
code_offset: usize,
func: &mut ir::Function, func: &mut ir::Function,
environ: &mut FE, environ: &mut FE,
) -> WasmResult<()> { ) -> 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`. /// Translate a binary WebAssembly function from a `BinaryReader`.
@@ -222,11 +227,9 @@ fn parse_function_body<FE: FuncEnvironment + ?Sized>(
/// Get the current source location from a reader. /// Get the current source location from a reader.
fn cur_srcloc(reader: &BinaryReader) -> ir::SourceLoc { fn cur_srcloc(reader: &BinaryReader) -> ir::SourceLoc {
// We record source locations as byte code offsets relative to the beginning of the function. // We record source locations as byte code offsets relative to the beginning of the file.
// This will wrap around of a single function's byte code is larger than 4 GB, but a) the // This will wrap around if byte code is larger than 4 GB.
// WebAssembly format doesn't allow for that, and b) that would hit other Cranelift ir::SourceLoc::new(reader.original_position() as u32)
// implementation limits anyway.
ir::SourceLoc::new(reader.current_position() as u32)
} }
#[cfg(test)] #[cfg(test)]
@@ -270,7 +273,7 @@ mod tests {
ctx.func.signature.returns.push(ir::AbiParam::new(I32)); ctx.func.signature.returns.push(ir::AbiParam::new(I32));
trans trans
.translate(&BODY, &mut ctx.func, &mut runtime.func_env()) .translate(&BODY, 0, &mut ctx.func, &mut runtime.func_env())
.unwrap(); .unwrap();
debug!("{}", ctx.func.display(None)); debug!("{}", ctx.func.display(None));
ctx.verify(&flags).unwrap(); ctx.verify(&flags).unwrap();
@@ -308,7 +311,7 @@ mod tests {
ctx.func.signature.returns.push(ir::AbiParam::new(I32)); ctx.func.signature.returns.push(ir::AbiParam::new(I32));
trans trans
.translate(&BODY, &mut ctx.func, &mut runtime.func_env()) .translate(&BODY, 0, &mut ctx.func, &mut runtime.func_env())
.unwrap(); .unwrap();
debug!("{}", ctx.func.display(None)); debug!("{}", ctx.func.display(None));
ctx.verify(&flags).unwrap(); ctx.verify(&flags).unwrap();
@@ -354,7 +357,7 @@ mod tests {
ctx.func.signature.returns.push(ir::AbiParam::new(I32)); ctx.func.signature.returns.push(ir::AbiParam::new(I32));
trans trans
.translate(&BODY, &mut ctx.func, &mut runtime.func_env()) .translate(&BODY, 0, &mut ctx.func, &mut runtime.func_env())
.unwrap(); .unwrap();
debug!("{}", ctx.func.display(None)); debug!("{}", ctx.func.display(None));
ctx.verify(&flags).unwrap(); ctx.verify(&flags).unwrap();

View File

@@ -295,7 +295,8 @@ pub fn parse_code_section<'data>(
for body in code { for body in code {
let mut reader = body?.get_binary_reader(); let mut reader = body?.get_binary_reader();
let size = reader.bytes_remaining(); 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(()) Ok(())
} }