Path: blob/main/crates/wizer/src/component/info.rs
2459 views
use crate::ModuleContext;1use std::collections::HashMap;23/// Wizer-specific contextual information about a component, returned from4/// [`Wizer::instrument_component`].5///6/// [`Wizer::instrument_component`]: crate::Wizer::instrument_component7#[derive(Default)]8pub struct ComponentContext<'a> {9/// Sections of the component, which are either raw bytes or a parsed module10/// using `ModuleContext`.11pub(crate) sections: Vec<RawSection<'a>>,1213/// Counts of each index space for what this component contains.14///15/// Note that these aren't all index spaces in the component, only those16/// needed at this time.17pub(crate) instances: u32,18pub(crate) funcs: u32,19pub(crate) types: u32,20pub(crate) core_instances: u32,21pub(crate) core_memories: u32,22pub(crate) core_funcs: u32,2324/// Map of which module index to the core instance index it's instantiated25/// as.26pub(crate) core_instantiations: HashMap<u32, u32>,2728/// Instrumentation injected to access internal state of globals/memories.29pub(crate) accessors: Option<Vec<Accessor>>,30}3132/// Generated accessors during instrumentation and the metadata about them.33pub(crate) enum Accessor {34/// This accessor retrieves the value of a wasm global.35Global {36/// The module index, within the parent component, that this global37/// belongs to.38module_index: u32,3940/// The wizer-instrumented name of the global export this is accessing.41core_export_name: String,4243/// The component level export name to access this global.44accessor_export_name: String,4546/// The content type of this global.47ty: wasmparser::ValType,48},4950/// This accessor retrieves the value of a wasm linear memory as a51/// `list<u8>` in WIT.52Memory {53/// The module index, within the parent component, that this memory54/// belongs to.55module_index: u32,5657/// The wizer-instrumented name of the memory export this is accessing.58core_export_name: String,5960/// The component level export name to access this memory.61accessor_export_name: String,62},63}6465/// A section of a component, learned during parsing.66pub(crate) enum RawSection<'a> {67/// A non-module section, whose raw contents are stored here.68Raw(wasm_encoder::RawSection<'a>),6970/// A module section, parsed as with Wizer's metadata.71Module(ModuleContext<'a>),72}7374impl<'a> ComponentContext<'a> {75pub(crate) fn push_raw_section(&mut self, section: wasm_encoder::RawSection<'a>) {76self.sections.push(RawSection::Raw(section));77}7879pub(crate) fn push_module_section(&mut self, module: ModuleContext<'a>) {80self.sections.push(RawSection::Module(module));81}8283pub(crate) fn core_modules(&self) -> impl Iterator<Item = (u32, &ModuleContext<'a>)> + '_ {84let mut i = 0;85self.sections.iter().filter_map(move |s| match s {86RawSection::Module(m) => Some((inc(&mut i), m)),87RawSection::Raw(_) => None,88})89}9091pub(crate) fn num_core_modules(&self) -> u32 {92u32::try_from(self.core_modules().count()).unwrap()93}9495pub(crate) fn inc(&mut self, kind: wasmparser::ComponentExternalKind) {96match kind {97wasmparser::ComponentExternalKind::Type => {98self.inc_types();99}100wasmparser::ComponentExternalKind::Instance => {101self.inc_instances();102}103wasmparser::ComponentExternalKind::Func => {104self.inc_funcs();105}106wasmparser::ComponentExternalKind::Component107| wasmparser::ComponentExternalKind::Module108| wasmparser::ComponentExternalKind::Value => {}109}110}111112pub(crate) fn inc_core(&mut self, kind: wasmparser::ExternalKind) {113match kind {114wasmparser::ExternalKind::Func | wasmparser::ExternalKind::FuncExact => {115self.inc_core_funcs();116}117wasmparser::ExternalKind::Memory => {118self.inc_core_memories();119}120wasmparser::ExternalKind::Table121| wasmparser::ExternalKind::Global122| wasmparser::ExternalKind::Tag => {}123}124}125126pub(crate) fn inc_instances(&mut self) -> u32 {127inc(&mut self.instances)128}129130pub(crate) fn inc_funcs(&mut self) -> u32 {131inc(&mut self.funcs)132}133134pub(crate) fn inc_core_memories(&mut self) -> u32 {135inc(&mut self.core_memories)136}137138pub(crate) fn inc_types(&mut self) -> u32 {139inc(&mut self.types)140}141142pub(crate) fn inc_core_instances(&mut self) -> u32 {143inc(&mut self.core_instances)144}145146pub(crate) fn inc_core_funcs(&mut self) -> u32 {147inc(&mut self.core_funcs)148}149}150151fn inc(count: &mut u32) -> u32 {152let current = *count;153*count += 1;154current155}156157158