* Pull `Module` out of `ModuleTextBuilder` This commit is the first in what will likely be a number towards preparing for serializing a compiled component to bytes, a precompiled artifact. To that end my rough plan is to merge all of the compiled artifacts for a component into one large object file instead of having lots of separate object files and lots of separate mmaps to manage. To that end I plan on eventually using `ModuleTextBuilder` to build one large text section for all core wasm modules and trampolines, meaning that `ModuleTextBuilder` is no longer specific to one module. I've extracted out functionality such as function name calculation as well as relocation resolving (now a closure passed in) in preparation for this. For now this just keeps tests passing, and the trajectory for this should become more clear over the following commits. * Remove component-specific object emission This commit removes the `ComponentCompiler::emit_obj` function in favor of `Compiler::emit_obj`, now renamed `append_code`. This involved significantly refactoring code emission to take a flat list of functions into `append_code` and the caller is responsible for weaving together various "families" of functions and un-weaving them afterwards. * Consolidate ELF parsing in `CodeMemory` This commit moves the ELF file parsing and section iteration from `CompiledModule` into `CodeMemory` so one location keeps track of section ranges and such. This is in preparation for sharing much of this code with components which needs all the same sections to get tracked but won't be using `CompiledModule`. A small side benefit from this is that the section parsing done in `CodeMemory` and `CompiledModule` is no longer duplicated. * Remove separately tracked traps in components Previously components would generate an "always trapping" function and the metadata around which pc was allowed to trap was handled manually for components. With recent refactorings the Wasmtime-standard trap section in object files is now being generated for components as well which means that can be reused instead of custom-tracking this metadata. This commit removes the manual tracking for the `always_trap` functions and plumbs the necessary bits around to make components look more like modules. * Remove a now-unnecessary `Arc` in `Module` Not expected to have any measurable impact on performance, but complexity-wise this should make it a bit easier to understand the internals since there's no longer any need to store this somewhere else than its owner's location. * Merge compilation artifacts of components This commit is a large refactoring of the component compilation process to produce a single artifact instead of multiple binary artifacts. The core wasm compilation process is refactored as well to share as much code as necessary with the component compilation process. This method of representing a compiled component necessitated a few medium-sized changes internally within Wasmtime: * A new data structure was created, `CodeObject`, which represents metadata about a single compiled artifact. This is then stored as an `Arc` within a component and a module. For `Module` this is always uniquely owned and represents a shuffling around of data from one owner to another. For a `Component`, however, this is shared amongst all loaded modules and the top-level component. * The "module registry" which is used for symbolicating backtraces and for trap information has been updated to account for a single region of loaded code holding possibly multiple modules. This involved adding a second-level `BTreeMap` for now. This will likely slow down instantiation slightly but if it poses an issue in the future this should be able to be represented with a more clever data structure. This commit additionally solves a number of longstanding issues with components such as compiling only one host-to-wasm trampoline per signature instead of possibly once-per-module. Additionally the `SignatureCollection` registration now happens once-per-component instead of once-per-module-within-a-component. * Fix compile errors from prior commits * Support AOT-compiling components This commit adds support for AOT-compiled components in the same manner as `Module`, specifically adding: * `Engine::precompile_component` * `Component::serialize` * `Component::deserialize` * `Component::deserialize_file` Internally the support for components looks quite similar to `Module`. All the prior commits to this made adding the support here (unsurprisingly) easy. Components are represented as a single object file as are modules, and the functions for each module are all piled into the same object file next to each other (as are areas such as data sections). Support was also added here to quickly differentiate compiled components vs compiled modules via the `e_flags` field in the ELF header. * Prevent serializing exported modules on components The current representation of a module within a component means that the implementation of `Module::serialize` will not work if the module is exported from a component. The reason for this is that `serialize` doesn't actually do anything and simply returns the underlying mmap as a list of bytes. The mmap, however, has `.wasmtime.info` describing component metadata as opposed to this module's metadata. While rewriting this section could be implemented it's not so easy to do so and is otherwise seen as not super important of a feature right now anyway. * Fix windows build * Fix an unused function warning * Update crates/environ/src/compilation.rs Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com> Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
91 lines
3.2 KiB
Rust
91 lines
3.2 KiB
Rust
//! Module for System V ABI unwind registry.
|
|
|
|
use anyhow::Result;
|
|
|
|
/// Represents a registration of function unwind information for System V ABI.
|
|
pub struct UnwindRegistration {
|
|
registrations: Vec<usize>,
|
|
}
|
|
|
|
extern "C" {
|
|
// libunwind import
|
|
fn __register_frame(fde: *const u8);
|
|
fn __deregister_frame(fde: *const u8);
|
|
}
|
|
|
|
impl UnwindRegistration {
|
|
pub const SECTION_NAME: &str = ".eh_frame";
|
|
|
|
/// Registers precompiled unwinding information with the system.
|
|
///
|
|
/// The `_base_address` field is ignored here (only used on other
|
|
/// platforms), but the `unwind_info` and `unwind_len` parameters should
|
|
/// describe an in-memory representation of a `.eh_frame` section. This is
|
|
/// typically arranged for by the `wasmtime-obj` crate.
|
|
pub unsafe fn new(
|
|
_base_address: *const u8,
|
|
unwind_info: *const u8,
|
|
unwind_len: usize,
|
|
) -> Result<UnwindRegistration> {
|
|
debug_assert_eq!(
|
|
unwind_info as usize % wasmtime_runtime::page_size(),
|
|
0,
|
|
"The unwind info must always be aligned to a page"
|
|
);
|
|
|
|
let mut registrations = Vec::new();
|
|
if cfg!(any(
|
|
all(target_os = "linux", target_env = "gnu"),
|
|
target_os = "freebsd"
|
|
)) {
|
|
// On gnu (libgcc), `__register_frame` will walk the FDEs until an
|
|
// entry of length 0
|
|
__register_frame(unwind_info);
|
|
registrations.push(unwind_info as usize);
|
|
} else {
|
|
// For libunwind, `__register_frame` takes a pointer to a single
|
|
// FDE. Note that we subtract 4 from the length of unwind info since
|
|
// wasmtime-encode .eh_frame sections always have a trailing 32-bit
|
|
// zero for the platforms above.
|
|
let start = unwind_info;
|
|
let end = start.add(unwind_len - 4);
|
|
let mut current = start;
|
|
|
|
// Walk all of the entries in the frame table and register them
|
|
while current < end {
|
|
let len = std::ptr::read::<u32>(current as *const u32) as usize;
|
|
|
|
// Skip over the CIE
|
|
if current != start {
|
|
__register_frame(current);
|
|
registrations.push(current as usize);
|
|
}
|
|
|
|
// Move to the next table entry (+4 because the length itself is
|
|
// not inclusive)
|
|
current = current.add(len + 4);
|
|
}
|
|
}
|
|
|
|
Ok(UnwindRegistration { registrations })
|
|
}
|
|
}
|
|
|
|
impl Drop for UnwindRegistration {
|
|
fn drop(&mut self) {
|
|
unsafe {
|
|
// libgcc stores the frame entries as a linked list in decreasing
|
|
// sort order based on the PC value of the registered entry.
|
|
//
|
|
// As we store the registrations in increasing order, it would be
|
|
// O(N^2) to deregister in that order.
|
|
//
|
|
// To ensure that we just pop off the first element in the list upon
|
|
// every deregistration, walk our list of registrations backwards.
|
|
for fde in self.registrations.iter().rev() {
|
|
__deregister_frame(*fde as *const _);
|
|
}
|
|
}
|
|
}
|
|
}
|