Reimplement how unwind information is stored (#3180)
* Reimplement how unwind information is stored This commit is a major refactoring of how unwind information is stored after compilation of a function has finished. Previously we would store the raw `UnwindInfo` as a result of compilation and this would get serialized/deserialized alongside the rest of the ELF object that compilation creates. Whenever functions were registered with `CodeMemory` this would also result in registering unwinding information dynamically at runtime, which in the case of Unix, for example, would dynamically created FDE/CIE entries on-the-fly. Eventually I'd like to support compiling Wasmtime without Cranelift, but this means that `UnwindInfo` wouldn't be easily available to decode into and create unwinding information from. To solve this I've changed the ELF object created to have the unwinding information encoded into it ahead-of-time so loading code into memory no longer needs to create unwinding tables. This change has two different implementations for Windows/Unix: * On Windows the implementation was much easier. The unwinding information on Windows is already stored after the function itself in the text section. This was actually slightly duplicated in object building and in code memory allocation. Now the object building continues to do the same, recording unwinding information after functions, and code memory no longer manually tracks this. Additionally Wasmtime will emit a special custom section in the object file with unwinding information which is the list of `RUNTIME_FUNCTION` structures that `RtlAddFunctionTable` expects. This means that the object file has all the information precompiled into it and registration at runtime is simply passing a few pointers around to the runtime. * Unix was a little bit more difficult than Windows. Today a `.eh_frame` section is created on-the-fly with offsets in FDEs specified as the absolute address that functions are loaded at. This absolute address hindered the ability to precompile the FDE into the object file itself. I've switched how addresses are encoded, though, to using `DW_EH_PE_pcrel` which means that FDE addresses are now specified relative to the FDE itself. This means that we can maintain a fixed offset between the `.eh_frame` loaded in memory and the beginning of code memory. When doing so this enables precompiling the `.eh_frame` section into the object file and at runtime when loading an object no further construction of unwinding information is needed. The overall result of this commit is that unwinding information is no longer stored in its cranelift-data-structure form on disk. This means that this unwinding information format is only present during compilation, which will make it that much easier to compile out cranelift in the future. This commit also significantly refactors `CodeMemory` since the way unwinding information is handled is not much different from before. Previously `CodeMemory` was suitable for incrementally adding more and more functions to it, but nowadays a `CodeMemory` either lives per module (in which case all functions are known up front) or it's created once-per-`Func::new` with two trampolines. In both cases we know all functions up front so the functionality of incrementally adding more and more segments is no longer needed. This commit removes the ability to add a function-at-a-time in `CodeMemory` and instead it can now only load objects in their entirety. A small helper function is added to build a small object file for trampolines in `Func::new` to handle allocation there. Finally, this commit also folds the `wasmtime-obj` crate directly into the `wasmtime-cranelift` crate and its builder structure to be more amenable to this strategy of managing unwinding tables. It is not intentional to have any real functional change as a result of this commit. This might accelerate loading a module from cache slightly since less work is needed to manage the unwinding information, but that's just a side benefit from the main goal of this commit which is to remove the dependence on cranelift unwinding information being available at runtime. * Remove isa reexport from wasmtime-environ * Trim down reexports of `cranelift-codegen` Remove everything non-essential so that only the bits which will need to be refactored out of cranelift remain. * Fix debug tests * Review comments
This commit is contained in:
@@ -177,46 +177,25 @@ pub trait Compiler: Send + Sync {
|
||||
types: &TypeTables,
|
||||
) -> Result<CompiledFunction, CompileError>;
|
||||
|
||||
/// Creates a trampoline which the host can use to enter wasm.
|
||||
/// Collects the results of compilation and emits an in-memory ELF object
|
||||
/// which is the serialized representation of all compiler artifacts.
|
||||
///
|
||||
/// The generated trampoline will have the type `VMTrampoline` and will
|
||||
/// call a function of type `ty` specified.
|
||||
fn host_to_wasm_trampoline(&self, ty: &WasmFuncType) -> Result<CompiledFunction, CompileError>;
|
||||
|
||||
/// Creates a trampoline suitable for a wasm module to import.
|
||||
///
|
||||
/// The trampoline has the type specified by `ty` and will call the function
|
||||
/// `host_fn` which has type `VMTrampoline`. Note that `host_fn` is
|
||||
/// directly embedded into the generated code so this is not suitable for a
|
||||
/// cached value or if `host_fn` does not live as long as the compiled
|
||||
/// function.
|
||||
///
|
||||
/// This is primarily used for `Func::new` in `wasmtime`.
|
||||
fn wasm_to_host_trampoline(
|
||||
/// Note that ELF is used regardless of the target architecture.
|
||||
fn emit_obj(
|
||||
&self,
|
||||
ty: &WasmFuncType,
|
||||
host_fn: usize,
|
||||
) -> Result<CompiledFunction, CompileError>;
|
||||
|
||||
/// Creates DWARF debugging data for a compilation unit.
|
||||
///
|
||||
/// This function maps DWARF information found in a wasm module to native
|
||||
/// DWARF debugging information. This is currently implemented by the
|
||||
/// `wasmtime-debug` crate.
|
||||
fn emit_dwarf(
|
||||
&self,
|
||||
debuginfo_data: &crate::DebugInfoData,
|
||||
module: &ModuleTranslation,
|
||||
types: &TypeTables,
|
||||
funcs: &CompiledFunctions,
|
||||
memory_offset: &crate::ModuleMemoryOffset,
|
||||
) -> Result<Vec<DwarfSection>>;
|
||||
emit_dwarf: bool,
|
||||
) -> Result<Vec<u8>>;
|
||||
|
||||
/// Emits a small ELF object file in-memory which has two functions for the
|
||||
/// host-to-wasm and wasm-to-host trampolines for the wasm type given.
|
||||
fn emit_trampoline_obj(&self, ty: &WasmFuncType, host_fn: usize) -> Result<Vec<u8>>;
|
||||
|
||||
/// Returns the target triple that this compiler is compiling for.
|
||||
fn triple(&self) -> &target_lexicon::Triple;
|
||||
|
||||
/// If supported by the target creates a SystemV CIE used for dwarf
|
||||
/// unwinding information.
|
||||
fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry>;
|
||||
|
||||
/// Returns a list of configured settings for this compiler.
|
||||
fn flags(&self) -> HashMap<String, FlagValue>;
|
||||
|
||||
@@ -224,29 +203,6 @@ pub trait Compiler: Send + Sync {
|
||||
fn isa_flags(&self) -> HashMap<String, FlagValue>;
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub struct DwarfSection {
|
||||
pub name: &'static str,
|
||||
pub body: Vec<u8>,
|
||||
pub relocs: Vec<DwarfSectionReloc>,
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[derive(Clone)]
|
||||
pub struct DwarfSectionReloc {
|
||||
pub target: DwarfSectionRelocTarget,
|
||||
pub offset: u32,
|
||||
pub addend: i32,
|
||||
pub size: u8,
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[derive(Clone)]
|
||||
pub enum DwarfSectionRelocTarget {
|
||||
Func(usize),
|
||||
Section(&'static str),
|
||||
}
|
||||
|
||||
/// Value of a configured setting for a [`Compiler`]
|
||||
#[derive(Serialize, Deserialize, Hash, Eq, PartialEq)]
|
||||
pub enum FlagValue {
|
||||
|
||||
@@ -1,18 +1,8 @@
|
||||
#![doc(hidden)]
|
||||
|
||||
pub mod ir {
|
||||
pub use cranelift_codegen::binemit::{Reloc, StackMap};
|
||||
pub use cranelift_codegen::ir::{
|
||||
types, AbiParam, ArgumentPurpose, Endianness, JumpTableOffsets, LabelValueLoc, LibCall,
|
||||
Signature, SourceLoc, StackSlots, TrapCode, Type, ValueLabel, ValueLoc,
|
||||
};
|
||||
pub use cranelift_codegen::{ValueLabelsRanges, ValueLocRange};
|
||||
}
|
||||
|
||||
pub mod isa {
|
||||
pub use cranelift_codegen::isa::{
|
||||
lookup, unwind, Builder, CallConv, RegUnit, TargetFrontendConfig, TargetIsa,
|
||||
};
|
||||
pub use cranelift_codegen::binemit::StackMap;
|
||||
pub use cranelift_codegen::ir::{types, SourceLoc, TrapCode, Type};
|
||||
}
|
||||
|
||||
pub mod entity {
|
||||
|
||||
@@ -29,6 +29,7 @@ mod compilation;
|
||||
mod data_structures;
|
||||
mod module;
|
||||
mod module_environ;
|
||||
pub mod obj;
|
||||
mod tunables;
|
||||
mod vmoffsets;
|
||||
|
||||
|
||||
34
crates/environ/src/obj.rs
Normal file
34
crates/environ/src/obj.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
//! Utilities for working with object files that operate as Wasmtime's
|
||||
//! serialization and intermediate format for compiled modules.
|
||||
|
||||
use cranelift_entity::EntityRef;
|
||||
use cranelift_wasm::{FuncIndex, SignatureIndex};
|
||||
|
||||
const FUNCTION_PREFIX: &str = "_wasm_function_";
|
||||
const TRAMPOLINE_PREFIX: &str = "_trampoline_";
|
||||
|
||||
/// Returns the symbol name in an object file for the corresponding wasm
|
||||
/// function index in a module.
|
||||
pub fn func_symbol_name(index: FuncIndex) -> String {
|
||||
format!("{}{}", FUNCTION_PREFIX, index.index())
|
||||
}
|
||||
|
||||
/// Attempts to extract the corresponding function index from a symbol possibly produced by
|
||||
/// `func_symbol_name`.
|
||||
pub fn try_parse_func_name(name: &str) -> Option<FuncIndex> {
|
||||
let n = name.strip_prefix(FUNCTION_PREFIX)?.parse().ok()?;
|
||||
Some(FuncIndex::new(n))
|
||||
}
|
||||
|
||||
/// Returns the symbol name in an object file for the corresponding trampoline
|
||||
/// for the given signature in a module.
|
||||
pub fn trampoline_symbol_name(index: SignatureIndex) -> String {
|
||||
format!("{}{}", TRAMPOLINE_PREFIX, index.index())
|
||||
}
|
||||
|
||||
/// Attempts to extract the corresponding signature index from a symbol
|
||||
/// possibly produced by `trampoline_symbol_name`.
|
||||
pub fn try_parse_trampoline_name(name: &str) -> Option<SignatureIndex> {
|
||||
let n = name.strip_prefix(TRAMPOLINE_PREFIX)?.parse().ok()?;
|
||||
Some(SignatureIndex::new(n))
|
||||
}
|
||||
Reference in New Issue
Block a user