Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Add `Component::{text,address_map,functions}` methods (#13073)

Nick Fitzgeraldcb6f392fa5d9
diff --git a/crates/wasmtime/src/lib.rs b/crates/wasmtime/src/lib.rs
index 51dc8e2537..8ae924c4fd 100644
--- a/crates/wasmtime/src/lib.rs
+++ b/crates/wasmtime/src/lib.rs
@@ -513,6 +513,7 @@ pub use wasmtime_environ::OperatorCost;
pub use wasmtime_environ::ToWasmtimeResult;
#[doc(inline)]
pub use wasmtime_environ::error;
+pub use wasmtime_environ::{FuncIndex, StaticModuleIndex};
// Only for use in `bindgen!`-generated code.
#[doc(hidden)]
diff --git a/crates/wasmtime/src/runtime.rs b/crates/wasmtime/src/runtime.rs
index db49f39762..d4201b1b1b 100644
--- a/crates/wasmtime/src/runtime.rs
+++ b/crates/wasmtime/src/runtime.rs
@@ -99,7 +99,7 @@ pub use instantiate::CompiledModule;
pub use limits::*;
pub use linker::*;
pub use memory::*;
-pub use module::{Module, ModuleExport};
+pub use module::{Module, ModuleExport, ModuleFunction};
pub use resources::*;
#[cfg(all(feature = "async", feature = "call-hook"))]
pub use store::CallHookHandler;
diff --git a/crates/wasmtime/src/runtime/component/component.rs b/crates/wasmtime/src/runtime/component/component.rs
index 672f61d574..9140cc8619 100644
--- a/crates/wasmtime/src/runtime/component/component.rs
+++ b/crates/wasmtime/src/runtime/component/component.rs
@@ -558,6 +558,31 @@ impl Component {
&self.inner.code
}
+ /// Get this component's code object's `.text` section, containing its
+ /// compiled executable code.
+ pub fn text(&self) -> &[u8] {
+ self.engine_code().text()
+ }
+
+ /// Get information about functions in this component's `.text` section:
+ /// their module index, function index, name, and offset+length.
+ pub fn functions(&self) -> impl Iterator<Item = crate::ModuleFunction> + '_ {
+ self.inner
+ .static_modules
+ .values()
+ .flat_map(|m| m.functions())
+ }
+
+ /// Get the address map for this component's `.text` section.
+ ///
+ /// See [`Module::address_map`] for more details.
+ pub fn address_map(&self) -> Option<impl Iterator<Item = (usize, Option<u32>)> + '_> {
+ Some(
+ wasmtime_environ::iterate_address_map(self.engine_code().address_map_data())?
+ .map(|(offset, file_pos)| (offset as usize, file_pos.file_offset())),
+ )
+ }
+
/// Same as [`Module::serialize`], except for a component.
///
/// Note that the artifact produced here must be passed to
diff --git a/crates/wasmtime/src/runtime/module.rs b/crates/wasmtime/src/runtime/module.rs
index beb5aa1f59..79202c64b8 100644
--- a/crates/wasmtime/src/runtime/module.rs
+++ b/crates/wasmtime/src/runtime/module.rs
@@ -22,7 +22,7 @@ use wasmparser::{Parser, ValidPayload, Validator};
use wasmtime_environ::FrameTable;
use wasmtime_environ::{
CompiledFunctionsTable, CompiledModuleInfo, EntityIndex, HostPtr, ModuleTypes, ObjectKind,
- TypeTrace, VMOffsets, VMSharedTypeIndex, WasmChecksum,
+ StaticModuleIndex, TypeTrace, VMOffsets, VMSharedTypeIndex, WasmChecksum,
};
#[cfg(feature = "gc")]
use wasmtime_unwinder::ExceptionTable;
@@ -1089,10 +1089,12 @@ impl Module {
/// Results are yielded in a ModuleFunction struct.
pub fn functions<'a>(&'a self) -> impl ExactSizeIterator<Item = ModuleFunction> + 'a {
let module = self.compiled_module();
- self.env_module().defined_func_indices().map(|idx| {
+ let module_index = self.env_module().module_index;
+ self.env_module().defined_func_indices().map(move |idx| {
let loc = module.func_loc(idx);
let idx = module.module().func_index(idx);
ModuleFunction {
+ module: module_index,
index: idx,
name: module.func_name(idx).map(|n| n.to_string()),
offset: loc.start as usize,
@@ -1213,9 +1215,15 @@ impl Module {
/// Describes a function for a given module.
pub struct ModuleFunction {
+ /// The static module index this function belongs to.
+ pub module: StaticModuleIndex,
+ /// The function index within the module.
pub index: wasmtime_environ::FuncIndex,
+ /// The display name of the function, if available.
pub name: Option<String>,
+ /// The byte offset of this function in the text section.
pub offset: usize,
+ /// The byte length of this function in the text section.
pub len: usize,
}