Skip to main content

wasmtime_wizer/component/
info.rs

1use crate::ModuleContext;
2use std::collections::HashMap;
3
4/// Wizer-specific contextual information about a component, returned from
5/// [`Wizer::instrument_component`].
6///
7/// [`Wizer::instrument_component`]: crate::Wizer::instrument_component
8#[derive(Clone, Default)]
9pub struct ComponentContext<'a> {
10    /// Sections of the component, which are either raw bytes or a parsed module
11    /// using `ModuleContext`.
12    pub(crate) sections: Vec<RawSection<'a>>,
13
14    /// Counts of each index space for what this component contains.
15    ///
16    /// Note that these aren't all index spaces in the component, only those
17    /// needed at this time.
18    pub(crate) instances: u32,
19    pub(crate) funcs: u32,
20    pub(crate) types: u32,
21    pub(crate) core_instances: u32,
22    pub(crate) core_memories: u32,
23    pub(crate) core_funcs: u32,
24
25    /// Map of which module index to the core instance index it's instantiated
26    /// as.
27    pub(crate) core_instantiations: HashMap<u32, u32>,
28
29    /// Instrumentation injected to access internal state of globals/memories.
30    pub(crate) accessors: Option<Vec<Accessor>>,
31}
32
33/// Generated accessors during instrumentation and the metadata about them.
34#[derive(Clone)]
35pub(crate) enum Accessor {
36    /// This accessor retrieves the value of a wasm global.
37    Global {
38        /// The module index, within the parent component, that this global
39        /// belongs to.
40        module_index: u32,
41
42        /// The wizer-instrumented name of the global export this is accessing.
43        core_export_name: String,
44
45        /// The component level export name to access this global.
46        accessor_export_name: String,
47
48        /// The content type of this global.
49        ty: wasmparser::ValType,
50    },
51
52    /// This accessor retrieves the value of a wasm linear memory as a
53    /// `list<u8>` in WIT.
54    Memory {
55        /// The module index, within the parent component, that this memory
56        /// belongs to.
57        module_index: u32,
58
59        /// The wizer-instrumented name of the memory export this is accessing.
60        core_export_name: String,
61
62        /// The component level export name to access this memory.
63        accessor_export_name: String,
64    },
65}
66
67/// A section of a component, learned during parsing.
68#[derive(Clone)]
69pub(crate) enum RawSection<'a> {
70    /// A non-module section, whose raw contents are stored here.
71    Raw(wasm_encoder::RawSection<'a>),
72
73    /// A module section, parsed as with Wizer's metadata.
74    Module(ModuleContext<'a>),
75}
76
77impl<'a> ComponentContext<'a> {
78    pub(crate) fn push_raw_section(&mut self, section: wasm_encoder::RawSection<'a>) {
79        self.sections.push(RawSection::Raw(section));
80    }
81
82    pub(crate) fn push_module_section(&mut self, module: ModuleContext<'a>) {
83        self.sections.push(RawSection::Module(module));
84    }
85
86    pub(crate) fn core_modules(&self) -> impl Iterator<Item = (u32, &ModuleContext<'a>)> + '_ {
87        let mut i = 0;
88        self.sections.iter().filter_map(move |s| match s {
89            RawSection::Module(m) => Some((inc(&mut i), m)),
90            RawSection::Raw(_) => None,
91        })
92    }
93
94    pub(crate) fn num_core_modules(&self) -> u32 {
95        u32::try_from(self.core_modules().count()).unwrap()
96    }
97
98    pub(crate) fn inc(&mut self, kind: wasmparser::ComponentExternalKind) {
99        match kind {
100            wasmparser::ComponentExternalKind::Type => {
101                self.inc_types();
102            }
103            wasmparser::ComponentExternalKind::Instance => {
104                self.inc_instances();
105            }
106            wasmparser::ComponentExternalKind::Func => {
107                self.inc_funcs();
108            }
109            wasmparser::ComponentExternalKind::Component
110            | wasmparser::ComponentExternalKind::Module
111            | wasmparser::ComponentExternalKind::Value => {}
112        }
113    }
114
115    pub(crate) fn inc_core(&mut self, kind: wasmparser::ExternalKind) {
116        match kind {
117            wasmparser::ExternalKind::Func | wasmparser::ExternalKind::FuncExact => {
118                self.inc_core_funcs();
119            }
120            wasmparser::ExternalKind::Memory => {
121                self.inc_core_memories();
122            }
123            wasmparser::ExternalKind::Table
124            | wasmparser::ExternalKind::Global
125            | wasmparser::ExternalKind::Tag => {}
126        }
127    }
128
129    pub(crate) fn inc_instances(&mut self) -> u32 {
130        inc(&mut self.instances)
131    }
132
133    pub(crate) fn inc_funcs(&mut self) -> u32 {
134        inc(&mut self.funcs)
135    }
136
137    pub(crate) fn inc_core_memories(&mut self) -> u32 {
138        inc(&mut self.core_memories)
139    }
140
141    pub(crate) fn inc_types(&mut self) -> u32 {
142        inc(&mut self.types)
143    }
144
145    pub(crate) fn inc_core_instances(&mut self) -> u32 {
146        inc(&mut self.core_instances)
147    }
148
149    pub(crate) fn inc_core_funcs(&mut self) -> u32 {
150        inc(&mut self.core_funcs)
151    }
152}
153
154fn inc(count: &mut u32) -> u32 {
155    let current = *count;
156    *count += 1;
157    current
158}