Expose precise offset information in wasmtime::FrameInfo (#1495)

* Consolidate trap/frame information

This commit removes `TrapRegistry` in favor of consolidating this
information in the `FRAME_INFO` we already have in the `wasmtime` crate.
This allows us to keep information generally in one place and have one
canonical location for "map this PC to some original wasm stuff". The
intent for this is to next update with enough information to go from a
program counter to a position in the original wasm file.

* Expose module offset information in `FrameInfo`

This commit implements functionality for `FrameInfo`, the wasm stack
trace of a `Trap`, to return the module/function offset. This allows
knowing the precise wasm location of each stack frame, instead of only
the main trap itself. The intention here is to provide more visibility
into the wasm source when something traps, so you know precisely where
calls were and where traps were, in order to assist in debugging.
Eventually we might use this information for mapping back to native
source languages as well (given sufficient debug information).

This change makes a previously-optional artifact of compilation always
computed on the cranelift side of things. This `ModuleAddressMap` is
then propagated to the same store of information other frame information
is stored within. This also removes the need for passing a `SourceLoc`
with wasm traps or to wasm trap creation, since the backtrace's wasm
frames will be able to infer their own `SourceLoc` from the relevant
program counters.
This commit is contained in:
Alex Crichton
2020-04-15 08:00:15 -05:00
committed by GitHub
parent c5b6c57c34
commit be85242a3f
18 changed files with 338 additions and 482 deletions

View File

@@ -9,19 +9,17 @@ use cranelift_codegen::Context;
use cranelift_codegen::{binemit, ir};
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
use std::collections::HashMap;
use std::convert::TryFrom;
use wasmtime_debug::{emit_debugsections_image, DebugInfoData};
use wasmtime_environ::entity::{EntityRef, PrimaryMap};
use wasmtime_environ::isa::{TargetFrontendConfig, TargetIsa};
use wasmtime_environ::wasm::{DefinedFuncIndex, DefinedMemoryIndex, MemoryIndex};
use wasmtime_environ::{
CacheConfig, CompileError, CompiledFunction, CompiledFunctionUnwindInfo, Compiler as _C,
ModuleMemoryOffset, ModuleTranslation, ModuleVmctxInfo, Relocation, RelocationTarget,
Relocations, Traps, Tunables, VMOffsets,
ModuleAddressMap, ModuleMemoryOffset, ModuleTranslation, ModuleVmctxInfo, Relocation,
RelocationTarget, Relocations, Traps, Tunables, VMOffsets,
};
use wasmtime_runtime::{
InstantiationError, SignatureRegistry, TrapRegistration, TrapRegistry, VMFunctionBody,
VMSharedSignatureIndex, VMTrampoline,
InstantiationError, SignatureRegistry, VMFunctionBody, VMSharedSignatureIndex, VMTrampoline,
};
/// Select which kind of compilation to use.
@@ -50,7 +48,6 @@ pub struct Compiler {
isa: Box<dyn TargetIsa>,
code_memory: CodeMemory,
trap_registry: TrapRegistry,
signatures: SignatureRegistry,
strategy: CompilationStrategy,
cache_config: CacheConfig,
@@ -70,7 +67,6 @@ impl Compiler {
code_memory: CodeMemory::new(),
signatures: SignatureRegistry::new(),
strategy,
trap_registry: TrapRegistry::default(),
cache_config,
tunables,
}
@@ -85,7 +81,8 @@ pub struct Compilation {
pub trampoline_relocations: HashMap<VMSharedSignatureIndex, Vec<Relocation>>,
pub jt_offsets: PrimaryMap<DefinedFuncIndex, ir::JumpTableOffsets>,
pub dbg_image: Option<Vec<u8>>,
pub trap_registration: TrapRegistration,
pub traps: Traps,
pub address_transform: ModuleAddressMap,
}
impl Compiler {
@@ -144,11 +141,6 @@ impl Compiler {
)))
})?;
// Create a registration value for all traps in our allocated
// functions. This registration will allow us to map a trapping PC
// value to what the trap actually means if it came from JIT code.
let trap_registration = register_traps(&finished_functions, &traps, &self.trap_registry);
// Eagerly generate a entry trampoline for every type signature in the
// module. This should be "relatively lightweight" for most modules and
// guarantees that all functions (including indirect ones through
@@ -228,7 +220,8 @@ impl Compiler {
trampoline_relocations,
jt_offsets,
dbg_image,
trap_registration,
traps,
address_transform,
})
}
@@ -241,11 +234,6 @@ impl Compiler {
pub fn signatures(&self) -> &SignatureRegistry {
&self.signatures
}
/// Shared registration of trap information
pub fn trap_registry(&self) -> &TrapRegistry {
&self.trap_registry
}
}
/// Create a trampoline for invoking a function.
@@ -392,26 +380,6 @@ fn allocate_functions(
Ok(result)
}
fn register_traps(
allocated_functions: &PrimaryMap<DefinedFuncIndex, *mut [VMFunctionBody]>,
traps: &Traps,
registry: &TrapRegistry,
) -> TrapRegistration {
let traps =
allocated_functions
.values()
.zip(traps.values())
.flat_map(|(func_addr, func_traps)| {
func_traps.iter().map(move |trap_desc| {
let func_addr = *func_addr as *const u8 as usize;
let offset = usize::try_from(trap_desc.code_offset).unwrap();
let trap_addr = func_addr + offset;
(trap_addr, trap_desc.source_loc, trap_desc.trap_code)
})
});
registry.register_traps(traps)
}
/// We don't expect trampoline compilation to produce many relocations, so
/// this `RelocSink` just asserts that it doesn't recieve most of them, but
/// handles libcall ones.