* 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>
195 lines
7.0 KiB
Rust
195 lines
7.0 KiB
Rust
use crate::obj::ELF_WASMTIME_TRAPS;
|
|
use object::write::{Object, StandardSegment};
|
|
use object::{Bytes, LittleEndian, SectionKind, U32Bytes};
|
|
use std::convert::TryFrom;
|
|
use std::ops::Range;
|
|
|
|
/// A helper structure to build the custom-encoded section of a wasmtime
|
|
/// compilation image which encodes trap information.
|
|
///
|
|
/// This structure is incrementally fed the results of compiling individual
|
|
/// functions and handles all the encoding internally, allowing usage of
|
|
/// `lookup_trap_code` below with the resulting section.
|
|
#[derive(Default)]
|
|
pub struct TrapEncodingBuilder {
|
|
offsets: Vec<U32Bytes<LittleEndian>>,
|
|
traps: Vec<u8>,
|
|
last_offset: u32,
|
|
}
|
|
|
|
/// Information about trap.
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
pub struct TrapInformation {
|
|
/// The offset of the trapping instruction in native code.
|
|
///
|
|
/// This is relative to the beginning of the function.
|
|
pub code_offset: u32,
|
|
|
|
/// Code of the trap.
|
|
pub trap_code: TrapCode,
|
|
}
|
|
|
|
/// A trap code describing the reason for a trap.
|
|
///
|
|
/// All trap instructions have an explicit trap code.
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
|
|
#[repr(u8)]
|
|
pub enum TrapCode {
|
|
/// The current stack space was exhausted.
|
|
StackOverflow,
|
|
|
|
/// A `heap_addr` instruction detected an out-of-bounds error.
|
|
///
|
|
/// Note that not all out-of-bounds heap accesses are reported this way;
|
|
/// some are detected by a segmentation fault on the heap unmapped or
|
|
/// offset-guard pages.
|
|
HeapOutOfBounds,
|
|
|
|
/// A wasm atomic operation was presented with a not-naturally-aligned linear-memory address.
|
|
HeapMisaligned,
|
|
|
|
/// A `table_addr` instruction detected an out-of-bounds error.
|
|
TableOutOfBounds,
|
|
|
|
/// Indirect call to a null table entry.
|
|
IndirectCallToNull,
|
|
|
|
/// Signature mismatch on indirect call.
|
|
BadSignature,
|
|
|
|
/// An integer arithmetic operation caused an overflow.
|
|
IntegerOverflow,
|
|
|
|
/// An integer division by zero.
|
|
IntegerDivisionByZero,
|
|
|
|
/// Failed float-to-int conversion.
|
|
BadConversionToInteger,
|
|
|
|
/// Code that was supposed to have been unreachable was reached.
|
|
UnreachableCodeReached,
|
|
|
|
/// Execution has potentially run too long and may be interrupted.
|
|
/// This trap is resumable.
|
|
Interrupt,
|
|
|
|
/// Used for the component model when functions are lifted/lowered in a way
|
|
/// that generates a function that always traps.
|
|
AlwaysTrapAdapter,
|
|
// if adding a variant here be sure to update the `check!` macro below
|
|
}
|
|
|
|
impl TrapEncodingBuilder {
|
|
/// Appends trap information about a function into this section.
|
|
///
|
|
/// This function is called to describe traps for the `func` range
|
|
/// specified. The `func` offsets are specified relative to the text section
|
|
/// itself, and the `traps` offsets are specified relative to the start of
|
|
/// `func`.
|
|
///
|
|
/// This is required to be called in-order for increasing ranges of `func`
|
|
/// to ensure the final array is properly sorted. Additionally `traps` must
|
|
/// be sorted.
|
|
pub fn push(&mut self, func: Range<u64>, traps: &[TrapInformation]) {
|
|
// NB: for now this only supports <=4GB text sections in object files.
|
|
// Alternative schemes will need to be created for >32-bit offsets to
|
|
// avoid making this section overly large.
|
|
let func_start = u32::try_from(func.start).unwrap();
|
|
let func_end = u32::try_from(func.end).unwrap();
|
|
|
|
// Sanity-check to ensure that functions are pushed in-order, otherwise
|
|
// the `offsets` array won't be sorted which is our goal.
|
|
assert!(func_start >= self.last_offset);
|
|
|
|
self.offsets.reserve(traps.len());
|
|
self.traps.reserve(traps.len());
|
|
for info in traps {
|
|
let pos = func_start + info.code_offset;
|
|
assert!(pos >= self.last_offset);
|
|
self.offsets.push(U32Bytes::new(LittleEndian, pos));
|
|
self.traps.push(info.trap_code as u8);
|
|
self.last_offset = pos;
|
|
}
|
|
|
|
self.last_offset = func_end;
|
|
}
|
|
|
|
/// Encodes this section into the object provided.
|
|
pub fn append_to(self, obj: &mut Object) {
|
|
let section = obj.add_section(
|
|
obj.segment_name(StandardSegment::Data).to_vec(),
|
|
ELF_WASMTIME_TRAPS.as_bytes().to_vec(),
|
|
SectionKind::ReadOnlyData,
|
|
);
|
|
|
|
// NB: this matches the encoding expected by `lookup` below.
|
|
let amt = u32::try_from(self.traps.len()).unwrap();
|
|
obj.append_section_data(section, &amt.to_le_bytes(), 1);
|
|
obj.append_section_data(section, object::bytes_of_slice(&self.offsets), 1);
|
|
obj.append_section_data(section, &self.traps, 1);
|
|
}
|
|
}
|
|
|
|
/// Decodes the provided trap information section and attempts to find the trap
|
|
/// code corresponding to the `offset` specified.
|
|
///
|
|
/// The `section` provided is expected to have been built by
|
|
/// `TrapEncodingBuilder` above. Additionally the `offset` should be a relative
|
|
/// offset within the text section of the compilation image.
|
|
pub fn lookup_trap_code(section: &[u8], offset: usize) -> Option<TrapCode> {
|
|
let mut section = Bytes(section);
|
|
// NB: this matches the encoding written by `append_to` above.
|
|
let count = section.read::<U32Bytes<LittleEndian>>().ok()?;
|
|
let count = usize::try_from(count.get(LittleEndian)).ok()?;
|
|
let (offsets, traps) =
|
|
object::slice_from_bytes::<U32Bytes<LittleEndian>>(section.0, count).ok()?;
|
|
debug_assert_eq!(traps.len(), count);
|
|
|
|
// The `offsets` table is sorted in the trap section so perform a binary
|
|
// search of the contents of this section to find whether `offset` is an
|
|
// entry in the section. Note that this is a precise search because trap pcs
|
|
// should always be precise as well as our metadata about them, which means
|
|
// we expect an exact match to correspond to a trap opcode.
|
|
//
|
|
// Once an index is found within the `offsets` array then that same index is
|
|
// used to lookup from the `traps` list of bytes to get the trap code byte
|
|
// corresponding to this offset.
|
|
let offset = u32::try_from(offset).ok()?;
|
|
let index = offsets
|
|
.binary_search_by_key(&offset, |val| val.get(LittleEndian))
|
|
.ok()?;
|
|
debug_assert!(index < traps.len());
|
|
let trap = *traps.get(index)?;
|
|
|
|
// FIXME: this could use some sort of derive-like thing to avoid having to
|
|
// deduplicate the names here.
|
|
//
|
|
// This simply converts from the `trap`, a `u8`, to the `TrapCode` enum.
|
|
macro_rules! check {
|
|
($($name:ident)*) => ($(if trap == TrapCode::$name as u8 {
|
|
return Some(TrapCode::$name);
|
|
})*);
|
|
}
|
|
|
|
check! {
|
|
StackOverflow
|
|
HeapOutOfBounds
|
|
HeapMisaligned
|
|
TableOutOfBounds
|
|
IndirectCallToNull
|
|
BadSignature
|
|
IntegerOverflow
|
|
IntegerDivisionByZero
|
|
BadConversionToInteger
|
|
UnreachableCodeReached
|
|
Interrupt
|
|
AlwaysTrapAdapter
|
|
}
|
|
|
|
if cfg!(debug_assertions) {
|
|
panic!("missing mapping for {}", trap);
|
|
} else {
|
|
None
|
|
}
|
|
}
|