wasmtime_environ/compile/
module_environ.rs

1use crate::module::{
2    FuncRefIndex, Initializer, MemoryInitialization, MemoryInitializer, Module, TableSegment,
3    TableSegmentElements,
4};
5use crate::prelude::*;
6use crate::{
7    ConstExpr, ConstOp, DataIndex, DefinedFuncIndex, ElemIndex, EngineOrModuleTypeIndex,
8    EntityIndex, EntityType, FuncIndex, GlobalIndex, IndexType, InitMemory, MemoryIndex,
9    ModuleInternedTypeIndex, ModuleTypesBuilder, PrimaryMap, SizeOverflow, StaticMemoryInitializer,
10    TableIndex, TableInitialValue, Tunables, TypeConvert, TypeIndex, Unsigned, WasmError,
11    WasmHeapTopType, WasmHeapType, WasmResult, WasmValType, WasmparserTypeConverter,
12};
13use anyhow::{bail, Result};
14use cranelift_entity::packed_option::ReservedValue;
15use std::borrow::Cow;
16use std::collections::HashMap;
17use std::mem;
18use std::path::PathBuf;
19use std::sync::Arc;
20use wasmparser::{
21    types::Types, CustomSectionReader, DataKind, ElementItems, ElementKind, Encoding, ExternalKind,
22    FuncToValidate, FunctionBody, KnownCustom, NameSectionReader, Naming, Parser, Payload, TypeRef,
23    Validator, ValidatorResources,
24};
25
26/// Object containing the standalone environment information.
27pub struct ModuleEnvironment<'a, 'data> {
28    /// The current module being translated
29    result: ModuleTranslation<'data>,
30
31    /// Intern'd types for this entire translation, shared by all modules.
32    types: &'a mut ModuleTypesBuilder,
33
34    // Various bits and pieces of configuration
35    validator: &'a mut Validator,
36    tunables: &'a Tunables,
37}
38
39/// The result of translating via `ModuleEnvironment`. Function bodies are not
40/// yet translated, and data initializers have not yet been copied out of the
41/// original buffer.
42#[derive(Default)]
43pub struct ModuleTranslation<'data> {
44    /// Module information.
45    pub module: Module,
46
47    /// The input wasm binary.
48    ///
49    /// This can be useful, for example, when modules are parsed from a
50    /// component and the embedder wants access to the raw wasm modules
51    /// themselves.
52    pub wasm: &'data [u8],
53
54    /// References to the function bodies.
55    pub function_body_inputs: PrimaryMap<DefinedFuncIndex, FunctionBodyData<'data>>,
56
57    /// A list of type signatures which are considered exported from this
58    /// module, or those that can possibly be called. This list is sorted, and
59    /// trampolines for each of these signatures are required.
60    pub exported_signatures: Vec<ModuleInternedTypeIndex>,
61
62    /// DWARF debug information, if enabled, parsed from the module.
63    pub debuginfo: DebugInfoData<'data>,
64
65    /// Set if debuginfo was found but it was not parsed due to `Tunables`
66    /// configuration.
67    pub has_unparsed_debuginfo: bool,
68
69    /// List of data segments found in this module which should be concatenated
70    /// together for the final compiled artifact.
71    ///
72    /// These data segments, when concatenated, are indexed by the
73    /// `MemoryInitializer` type.
74    pub data: Vec<Cow<'data, [u8]>>,
75
76    /// The desired alignment of `data` in the final data section of the object
77    /// file that we'll emit.
78    ///
79    /// Note that this is 1 by default but `MemoryInitialization::Static` might
80    /// switch this to a higher alignment to facilitate mmap-ing data from
81    /// an object file into a linear memory.
82    pub data_align: Option<u64>,
83
84    /// Total size of all data pushed onto `data` so far.
85    total_data: u32,
86
87    /// List of passive element segments found in this module which will get
88    /// concatenated for the final artifact.
89    pub passive_data: Vec<&'data [u8]>,
90
91    /// Total size of all passive data pushed into `passive_data` so far.
92    total_passive_data: u32,
93
94    /// When we're parsing the code section this will be incremented so we know
95    /// which function is currently being defined.
96    code_index: u32,
97
98    /// The type information of the current module made available at the end of the
99    /// validation process.
100    types: Option<Types>,
101}
102
103impl<'data> ModuleTranslation<'data> {
104    /// Returns a reference to the type information of the current module.
105    pub fn get_types(&self) -> &Types {
106        self.types
107            .as_ref()
108            .expect("module type information to be available")
109    }
110}
111
112/// Contains function data: byte code and its offset in the module.
113pub struct FunctionBodyData<'a> {
114    /// The body of the function, containing code and locals.
115    pub body: FunctionBody<'a>,
116    /// Validator for the function body
117    pub validator: FuncToValidate<ValidatorResources>,
118}
119
120#[derive(Debug, Default)]
121#[allow(missing_docs, reason = "self-describing fields")]
122pub struct DebugInfoData<'a> {
123    pub dwarf: Dwarf<'a>,
124    pub name_section: NameSection<'a>,
125    pub wasm_file: WasmFileInfo,
126    pub debug_loc: gimli::DebugLoc<Reader<'a>>,
127    pub debug_loclists: gimli::DebugLocLists<Reader<'a>>,
128    pub debug_ranges: gimli::DebugRanges<Reader<'a>>,
129    pub debug_rnglists: gimli::DebugRngLists<Reader<'a>>,
130    pub debug_cu_index: gimli::DebugCuIndex<Reader<'a>>,
131    pub debug_tu_index: gimli::DebugTuIndex<Reader<'a>>,
132}
133
134#[allow(missing_docs, reason = "self-describing")]
135pub type Dwarf<'input> = gimli::Dwarf<Reader<'input>>;
136
137type Reader<'input> = gimli::EndianSlice<'input, gimli::LittleEndian>;
138
139#[derive(Debug, Default)]
140#[allow(missing_docs, reason = "self-describing fields")]
141pub struct NameSection<'a> {
142    pub module_name: Option<&'a str>,
143    pub func_names: HashMap<FuncIndex, &'a str>,
144    pub locals_names: HashMap<FuncIndex, HashMap<u32, &'a str>>,
145}
146
147#[derive(Debug, Default)]
148#[allow(missing_docs, reason = "self-describing fields")]
149pub struct WasmFileInfo {
150    pub path: Option<PathBuf>,
151    pub code_section_offset: u64,
152    pub imported_func_count: u32,
153    pub funcs: Vec<FunctionMetadata>,
154}
155
156#[derive(Debug)]
157#[allow(missing_docs, reason = "self-describing fields")]
158pub struct FunctionMetadata {
159    pub params: Box<[WasmValType]>,
160    pub locals: Box<[(u32, WasmValType)]>,
161}
162
163impl<'a, 'data> ModuleEnvironment<'a, 'data> {
164    /// Allocates the environment data structures.
165    pub fn new(
166        tunables: &'a Tunables,
167        validator: &'a mut Validator,
168        types: &'a mut ModuleTypesBuilder,
169    ) -> Self {
170        Self {
171            result: ModuleTranslation::default(),
172            types,
173            tunables,
174            validator,
175        }
176    }
177
178    /// Translate a wasm module using this environment.
179    ///
180    /// This function will translate the `data` provided with `parser`,
181    /// validating everything along the way with this environment's validator.
182    ///
183    /// The result of translation, [`ModuleTranslation`], contains everything
184    /// necessary to compile functions afterwards as well as learn type
185    /// information about the module at runtime.
186    pub fn translate(
187        mut self,
188        parser: Parser,
189        data: &'data [u8],
190    ) -> Result<ModuleTranslation<'data>> {
191        self.result.wasm = data;
192
193        for payload in parser.parse_all(data) {
194            self.translate_payload(payload?)?;
195        }
196
197        Ok(self.result)
198    }
199
200    fn translate_payload(&mut self, payload: Payload<'data>) -> Result<()> {
201        match payload {
202            Payload::Version {
203                num,
204                encoding,
205                range,
206            } => {
207                self.validator.version(num, encoding, &range)?;
208                match encoding {
209                    Encoding::Module => {}
210                    Encoding::Component => {
211                        bail!("expected a WebAssembly module but was given a WebAssembly component")
212                    }
213                }
214            }
215
216            Payload::End(offset) => {
217                self.result.types = Some(self.validator.end(offset)?);
218
219                // With the `escaped_funcs` set of functions finished
220                // we can calculate the set of signatures that are exported as
221                // the set of exported functions' signatures.
222                self.result.exported_signatures = self
223                    .result
224                    .module
225                    .functions
226                    .iter()
227                    .filter_map(|(_, func)| {
228                        if func.is_escaping() {
229                            Some(func.signature)
230                        } else {
231                            None
232                        }
233                    })
234                    .collect();
235                self.result.exported_signatures.sort_unstable();
236                self.result.exported_signatures.dedup();
237            }
238
239            Payload::TypeSection(types) => {
240                self.validator.type_section(&types)?;
241
242                let count = self.validator.types(0).unwrap().core_type_count_in_module();
243                log::trace!("interning {count} Wasm types");
244
245                let capacity = usize::try_from(count).unwrap();
246                self.result.module.types.reserve(capacity);
247                self.types.reserve_wasm_signatures(capacity);
248
249                // Iterate over each *rec group* -- not type -- defined in the
250                // types section. Rec groups are the unit of canonicalization
251                // and therefore the unit at which we need to process at a
252                // time. `wasmparser` has already done the hard work of
253                // de-duplicating and canonicalizing the rec groups within the
254                // module for us, we just need to translate them into our data
255                // structures. Note that, if the Wasm defines duplicate rec
256                // groups, we need copy the duplicates over (shallowly) as well,
257                // so that our types index space doesn't have holes.
258                let mut type_index = 0;
259                while type_index < count {
260                    let validator_types = self.validator.types(0).unwrap();
261
262                    // Get the rec group for the current type index, which is
263                    // always the first type defined in a rec group.
264                    log::trace!("looking up wasmparser type for index {type_index}");
265                    let core_type_id = validator_types.core_type_at_in_module(type_index);
266                    log::trace!(
267                        "  --> {core_type_id:?} = {:?}",
268                        validator_types[core_type_id],
269                    );
270                    let rec_group_id = validator_types.rec_group_id_of(core_type_id);
271                    debug_assert_eq!(
272                        validator_types
273                            .rec_group_elements(rec_group_id)
274                            .position(|id| id == core_type_id),
275                        Some(0)
276                    );
277
278                    // Intern the rec group and then fill in this module's types
279                    // index space.
280                    let interned = self.types.intern_rec_group(validator_types, rec_group_id)?;
281                    let elems = self.types.rec_group_elements(interned);
282                    let len = elems.len();
283                    self.result.module.types.reserve(len);
284                    for ty in elems {
285                        self.result.module.types.push(ty);
286                    }
287
288                    // Advance `type_index` to the start of the next rec group.
289                    type_index += u32::try_from(len).unwrap();
290                }
291            }
292
293            Payload::ImportSection(imports) => {
294                self.validator.import_section(&imports)?;
295
296                let cnt = usize::try_from(imports.count()).unwrap();
297                self.result.module.initializers.reserve(cnt);
298
299                for entry in imports {
300                    let import = entry?;
301                    let ty = match import.ty {
302                        TypeRef::Func(index) => {
303                            let index = TypeIndex::from_u32(index);
304                            let interned_index = self.result.module.types[index];
305                            self.result.module.num_imported_funcs += 1;
306                            self.result.debuginfo.wasm_file.imported_func_count += 1;
307                            EntityType::Function(EngineOrModuleTypeIndex::Module(interned_index))
308                        }
309                        TypeRef::Memory(ty) => {
310                            self.result.module.num_imported_memories += 1;
311                            EntityType::Memory(ty.into())
312                        }
313                        TypeRef::Global(ty) => {
314                            self.result.module.num_imported_globals += 1;
315                            EntityType::Global(self.convert_global_type(&ty))
316                        }
317                        TypeRef::Table(ty) => {
318                            self.result.module.num_imported_tables += 1;
319                            EntityType::Table(self.convert_table_type(&ty)?)
320                        }
321
322                        // doesn't get past validation
323                        TypeRef::Tag(_) => unreachable!(),
324                    };
325                    self.declare_import(import.module, import.name, ty);
326                }
327            }
328
329            Payload::FunctionSection(functions) => {
330                self.validator.function_section(&functions)?;
331
332                let cnt = usize::try_from(functions.count()).unwrap();
333                self.result.module.functions.reserve_exact(cnt);
334
335                for entry in functions {
336                    let sigindex = entry?;
337                    let ty = TypeIndex::from_u32(sigindex);
338                    let interned_index = self.result.module.types[ty];
339                    self.result.module.push_function(interned_index);
340                }
341            }
342
343            Payload::TableSection(tables) => {
344                self.validator.table_section(&tables)?;
345                let cnt = usize::try_from(tables.count()).unwrap();
346                self.result.module.tables.reserve_exact(cnt);
347
348                for entry in tables {
349                    let wasmparser::Table { ty, init } = entry?;
350                    let table = self.convert_table_type(&ty)?;
351                    self.result.module.tables.push(table);
352                    let init = match init {
353                        wasmparser::TableInit::RefNull => TableInitialValue::Null {
354                            precomputed: Vec::new(),
355                        },
356                        wasmparser::TableInit::Expr(expr) => {
357                            let (init, escaped) = ConstExpr::from_wasmparser(expr)?;
358                            for f in escaped {
359                                self.flag_func_escaped(f);
360                            }
361                            TableInitialValue::Expr(init)
362                        }
363                    };
364                    self.result
365                        .module
366                        .table_initialization
367                        .initial_values
368                        .push(init);
369                }
370            }
371
372            Payload::MemorySection(memories) => {
373                self.validator.memory_section(&memories)?;
374
375                let cnt = usize::try_from(memories.count()).unwrap();
376                self.result.module.memories.reserve_exact(cnt);
377
378                for entry in memories {
379                    let memory = entry?;
380                    self.result.module.memories.push(memory.into());
381                }
382            }
383
384            Payload::TagSection(tags) => {
385                self.validator.tag_section(&tags)?;
386
387                // This feature isn't enabled at this time, so we should
388                // never get here.
389                unreachable!();
390            }
391
392            Payload::GlobalSection(globals) => {
393                self.validator.global_section(&globals)?;
394
395                let cnt = usize::try_from(globals.count()).unwrap();
396                self.result.module.globals.reserve_exact(cnt);
397
398                for entry in globals {
399                    let wasmparser::Global { ty, init_expr } = entry?;
400                    let (initializer, escaped) = ConstExpr::from_wasmparser(init_expr)?;
401                    for f in escaped {
402                        self.flag_func_escaped(f);
403                    }
404                    let ty = self.convert_global_type(&ty);
405                    self.result.module.globals.push(ty);
406                    self.result.module.global_initializers.push(initializer);
407                }
408            }
409
410            Payload::ExportSection(exports) => {
411                self.validator.export_section(&exports)?;
412
413                let cnt = usize::try_from(exports.count()).unwrap();
414                self.result.module.exports.reserve(cnt);
415
416                for entry in exports {
417                    let wasmparser::Export { name, kind, index } = entry?;
418                    let entity = match kind {
419                        ExternalKind::Func => {
420                            let index = FuncIndex::from_u32(index);
421                            self.flag_func_escaped(index);
422                            EntityIndex::Function(index)
423                        }
424                        ExternalKind::Table => EntityIndex::Table(TableIndex::from_u32(index)),
425                        ExternalKind::Memory => EntityIndex::Memory(MemoryIndex::from_u32(index)),
426                        ExternalKind::Global => EntityIndex::Global(GlobalIndex::from_u32(index)),
427
428                        // this never gets past validation
429                        ExternalKind::Tag => unreachable!(),
430                    };
431                    self.result
432                        .module
433                        .exports
434                        .insert(String::from(name), entity);
435                }
436            }
437
438            Payload::StartSection { func, range } => {
439                self.validator.start_section(func, &range)?;
440
441                let func_index = FuncIndex::from_u32(func);
442                self.flag_func_escaped(func_index);
443                debug_assert!(self.result.module.start_func.is_none());
444                self.result.module.start_func = Some(func_index);
445            }
446
447            Payload::ElementSection(elements) => {
448                self.validator.element_section(&elements)?;
449
450                for (index, entry) in elements.into_iter().enumerate() {
451                    let wasmparser::Element {
452                        kind,
453                        items,
454                        range: _,
455                    } = entry?;
456
457                    // Build up a list of `FuncIndex` corresponding to all the
458                    // entries listed in this segment. Note that it's not
459                    // possible to create anything other than a `ref.null
460                    // extern` for externref segments, so those just get
461                    // translated to the reserved value of `FuncIndex`.
462                    let elements = match items {
463                        ElementItems::Functions(funcs) => {
464                            let mut elems =
465                                Vec::with_capacity(usize::try_from(funcs.count()).unwrap());
466                            for func in funcs {
467                                let func = FuncIndex::from_u32(func?);
468                                self.flag_func_escaped(func);
469                                elems.push(func);
470                            }
471                            TableSegmentElements::Functions(elems.into())
472                        }
473                        ElementItems::Expressions(_ty, items) => {
474                            let mut exprs =
475                                Vec::with_capacity(usize::try_from(items.count()).unwrap());
476                            for expr in items {
477                                let (expr, escaped) = ConstExpr::from_wasmparser(expr?)?;
478                                exprs.push(expr);
479                                for func in escaped {
480                                    self.flag_func_escaped(func);
481                                }
482                            }
483                            TableSegmentElements::Expressions(exprs.into())
484                        }
485                    };
486
487                    match kind {
488                        ElementKind::Active {
489                            table_index,
490                            offset_expr,
491                        } => {
492                            let table_index = TableIndex::from_u32(table_index.unwrap_or(0));
493                            let (offset, escaped) = ConstExpr::from_wasmparser(offset_expr)?;
494                            debug_assert!(escaped.is_empty());
495
496                            self.result
497                                .module
498                                .table_initialization
499                                .segments
500                                .push(TableSegment {
501                                    table_index,
502                                    offset,
503                                    elements: elements.into(),
504                                });
505                        }
506
507                        ElementKind::Passive => {
508                            let elem_index = ElemIndex::from_u32(index as u32);
509                            let index = self.result.module.passive_elements.len();
510                            self.result.module.passive_elements.push(elements.into());
511                            self.result
512                                .module
513                                .passive_elements_map
514                                .insert(elem_index, index);
515                        }
516
517                        ElementKind::Declared => {}
518                    }
519                }
520            }
521
522            Payload::CodeSectionStart { count, range, .. } => {
523                self.validator.code_section_start(count, &range)?;
524                let cnt = usize::try_from(count).unwrap();
525                self.result.function_body_inputs.reserve_exact(cnt);
526                self.result.debuginfo.wasm_file.code_section_offset = range.start as u64;
527            }
528
529            Payload::CodeSectionEntry(body) => {
530                let validator = self.validator.code_section_entry(&body)?;
531                let func_index =
532                    self.result.code_index + self.result.module.num_imported_funcs as u32;
533                let func_index = FuncIndex::from_u32(func_index);
534
535                if self.tunables.generate_native_debuginfo {
536                    let sig_index = self.result.module.functions[func_index].signature;
537                    let sig = self.types[sig_index].unwrap_func();
538                    let mut locals = Vec::new();
539                    for pair in body.get_locals_reader()? {
540                        let (cnt, ty) = pair?;
541                        let ty = self.convert_valtype(ty);
542                        locals.push((cnt, ty));
543                    }
544                    self.result
545                        .debuginfo
546                        .wasm_file
547                        .funcs
548                        .push(FunctionMetadata {
549                            locals: locals.into_boxed_slice(),
550                            params: sig.params().into(),
551                        });
552                }
553                self.result
554                    .function_body_inputs
555                    .push(FunctionBodyData { validator, body });
556                self.result.code_index += 1;
557            }
558
559            Payload::DataSection(data) => {
560                self.validator.data_section(&data)?;
561
562                let initializers = match &mut self.result.module.memory_initialization {
563                    MemoryInitialization::Segmented(i) => i,
564                    _ => unreachable!(),
565                };
566
567                let cnt = usize::try_from(data.count()).unwrap();
568                initializers.reserve_exact(cnt);
569                self.result.data.reserve_exact(cnt);
570
571                for (index, entry) in data.into_iter().enumerate() {
572                    let wasmparser::Data {
573                        kind,
574                        data,
575                        range: _,
576                    } = entry?;
577                    let mk_range = |total: &mut u32| -> Result<_, WasmError> {
578                        let range = u32::try_from(data.len())
579                            .ok()
580                            .and_then(|size| {
581                                let start = *total;
582                                let end = start.checked_add(size)?;
583                                Some(start..end)
584                            })
585                            .ok_or_else(|| {
586                                WasmError::Unsupported(format!(
587                                    "more than 4 gigabytes of data in wasm module",
588                                ))
589                            })?;
590                        *total += range.end - range.start;
591                        Ok(range)
592                    };
593                    match kind {
594                        DataKind::Active {
595                            memory_index,
596                            offset_expr,
597                        } => {
598                            let range = mk_range(&mut self.result.total_data)?;
599                            let memory_index = MemoryIndex::from_u32(memory_index);
600                            let (offset, escaped) = ConstExpr::from_wasmparser(offset_expr)?;
601                            debug_assert!(escaped.is_empty());
602
603                            initializers.push(MemoryInitializer {
604                                memory_index,
605                                offset,
606                                data: range,
607                            });
608                            self.result.data.push(data.into());
609                        }
610                        DataKind::Passive => {
611                            let data_index = DataIndex::from_u32(index as u32);
612                            let range = mk_range(&mut self.result.total_passive_data)?;
613                            self.result.passive_data.push(data);
614                            self.result
615                                .module
616                                .passive_data_map
617                                .insert(data_index, range);
618                        }
619                    }
620                }
621            }
622
623            Payload::DataCountSection { count, range } => {
624                self.validator.data_count_section(count, &range)?;
625
626                // Note: the count passed in here is the *total* segment count
627                // There is no way to reserve for just the passive segments as
628                // they are discovered when iterating the data section entries
629                // Given that the total segment count might be much larger than
630                // the passive count, do not reserve anything here.
631            }
632
633            Payload::CustomSection(s)
634                if s.name() == "webidl-bindings" || s.name() == "wasm-interface-types" =>
635            {
636                bail!(
637                    "\
638Support for interface types has temporarily been removed from `wasmtime`.
639
640For more information about this temporary change you can read on the issue online:
641
642    https://github.com/bytecodealliance/wasmtime/issues/1271
643
644and for re-adding support for interface types you can see this issue:
645
646    https://github.com/bytecodealliance/wasmtime/issues/677
647"
648                )
649            }
650
651            Payload::CustomSection(s) => {
652                self.register_custom_section(&s);
653            }
654
655            // It's expected that validation will probably reject other
656            // payloads such as `UnknownSection` or those related to the
657            // component model. If, however, something gets past validation then
658            // that's a bug in Wasmtime as we forgot to implement something.
659            other => {
660                self.validator.payload(&other)?;
661                panic!("unimplemented section in wasm file {other:?}");
662            }
663        }
664        Ok(())
665    }
666
667    fn register_custom_section(&mut self, section: &CustomSectionReader<'data>) {
668        match section.as_known() {
669            KnownCustom::Name(name) => {
670                let result = self.name_section(name);
671                if let Err(e) = result {
672                    log::warn!("failed to parse name section {:?}", e);
673                }
674            }
675            _ => {
676                let name = section.name().trim_end_matches(".dwo");
677                if name.starts_with(".debug_") {
678                    self.dwarf_section(name, section);
679                }
680            }
681        }
682    }
683
684    fn dwarf_section(&mut self, name: &str, section: &CustomSectionReader<'data>) {
685        if !self.tunables.generate_native_debuginfo && !self.tunables.parse_wasm_debuginfo {
686            self.result.has_unparsed_debuginfo = true;
687            return;
688        }
689        let info = &mut self.result.debuginfo;
690        let dwarf = &mut info.dwarf;
691        let endian = gimli::LittleEndian;
692        let data = section.data();
693        let slice = gimli::EndianSlice::new(data, endian);
694
695        match name {
696            // `gimli::Dwarf` fields.
697            ".debug_abbrev" => dwarf.debug_abbrev = gimli::DebugAbbrev::new(data, endian),
698            ".debug_addr" => dwarf.debug_addr = gimli::DebugAddr::from(slice),
699            ".debug_info" => {
700                dwarf.debug_info = gimli::DebugInfo::new(data, endian);
701            }
702            ".debug_line" => dwarf.debug_line = gimli::DebugLine::new(data, endian),
703            ".debug_line_str" => dwarf.debug_line_str = gimli::DebugLineStr::from(slice),
704            ".debug_str" => dwarf.debug_str = gimli::DebugStr::new(data, endian),
705            ".debug_str_offsets" => dwarf.debug_str_offsets = gimli::DebugStrOffsets::from(slice),
706            ".debug_str_sup" => {
707                let mut dwarf_sup: Dwarf<'data> = Default::default();
708                dwarf_sup.debug_str = gimli::DebugStr::from(slice);
709                dwarf.sup = Some(Arc::new(dwarf_sup));
710            }
711            ".debug_types" => dwarf.debug_types = gimli::DebugTypes::from(slice),
712
713            // Additional fields.
714            ".debug_loc" => info.debug_loc = gimli::DebugLoc::from(slice),
715            ".debug_loclists" => info.debug_loclists = gimli::DebugLocLists::from(slice),
716            ".debug_ranges" => info.debug_ranges = gimli::DebugRanges::new(data, endian),
717            ".debug_rnglists" => info.debug_rnglists = gimli::DebugRngLists::new(data, endian),
718
719            // DWARF package fields
720            ".debug_cu_index" => info.debug_cu_index = gimli::DebugCuIndex::new(data, endian),
721            ".debug_tu_index" => info.debug_tu_index = gimli::DebugTuIndex::new(data, endian),
722
723            // We don't use these at the moment.
724            ".debug_aranges" | ".debug_pubnames" | ".debug_pubtypes" => return,
725            other => {
726                log::warn!("unknown debug section `{}`", other);
727                return;
728            }
729        }
730
731        dwarf.ranges = gimli::RangeLists::new(info.debug_ranges, info.debug_rnglists);
732        dwarf.locations = gimli::LocationLists::new(info.debug_loc, info.debug_loclists);
733    }
734
735    /// Declares a new import with the `module` and `field` names, importing the
736    /// `ty` specified.
737    ///
738    /// Note that this method is somewhat tricky due to the implementation of
739    /// the module linking proposal. In the module linking proposal two-level
740    /// imports are recast as single-level imports of instances. That recasting
741    /// happens here by recording an import of an instance for the first time
742    /// we see a two-level import.
743    ///
744    /// When the module linking proposal is disabled, however, disregard this
745    /// logic and instead work directly with two-level imports since no
746    /// instances are defined.
747    fn declare_import(&mut self, module: &'data str, field: &'data str, ty: EntityType) {
748        let index = self.push_type(ty);
749        self.result.module.initializers.push(Initializer::Import {
750            name: module.to_owned(),
751            field: field.to_owned(),
752            index,
753        });
754    }
755
756    fn push_type(&mut self, ty: EntityType) -> EntityIndex {
757        match ty {
758            EntityType::Function(ty) => EntityIndex::Function({
759                let func_index = self
760                    .result
761                    .module
762                    .push_function(ty.unwrap_module_type_index());
763                // Imported functions can escape; in fact, they've already done
764                // so to get here.
765                self.flag_func_escaped(func_index);
766                func_index
767            }),
768            EntityType::Table(ty) => EntityIndex::Table(self.result.module.tables.push(ty)),
769            EntityType::Memory(ty) => EntityIndex::Memory(self.result.module.memories.push(ty)),
770            EntityType::Global(ty) => EntityIndex::Global(self.result.module.globals.push(ty)),
771            EntityType::Tag(_) => unimplemented!(),
772        }
773    }
774
775    fn flag_func_escaped(&mut self, func: FuncIndex) {
776        let ty = &mut self.result.module.functions[func];
777        // If this was already assigned a funcref index no need to re-assign it.
778        if ty.is_escaping() {
779            return;
780        }
781        let index = self.result.module.num_escaped_funcs as u32;
782        ty.func_ref = FuncRefIndex::from_u32(index);
783        self.result.module.num_escaped_funcs += 1;
784    }
785
786    /// Parses the Name section of the wasm module.
787    fn name_section(&mut self, names: NameSectionReader<'data>) -> WasmResult<()> {
788        for subsection in names {
789            match subsection? {
790                wasmparser::Name::Function(names) => {
791                    for name in names {
792                        let Naming { index, name } = name?;
793                        // Skip this naming if it's naming a function that
794                        // doesn't actually exist.
795                        if (index as usize) >= self.result.module.functions.len() {
796                            continue;
797                        }
798
799                        // Store the name unconditionally, regardless of
800                        // whether we're parsing debuginfo, since function
801                        // names are almost always present in the
802                        // final compilation artifact.
803                        let index = FuncIndex::from_u32(index);
804                        self.result
805                            .debuginfo
806                            .name_section
807                            .func_names
808                            .insert(index, name);
809                    }
810                }
811                wasmparser::Name::Module { name, .. } => {
812                    self.result.module.name = Some(name.to_string());
813                    if self.tunables.generate_native_debuginfo {
814                        self.result.debuginfo.name_section.module_name = Some(name);
815                    }
816                }
817                wasmparser::Name::Local(reader) => {
818                    if !self.tunables.generate_native_debuginfo {
819                        continue;
820                    }
821                    for f in reader {
822                        let f = f?;
823                        // Skip this naming if it's naming a function that
824                        // doesn't actually exist.
825                        if (f.index as usize) >= self.result.module.functions.len() {
826                            continue;
827                        }
828                        for name in f.names {
829                            let Naming { index, name } = name?;
830
831                            self.result
832                                .debuginfo
833                                .name_section
834                                .locals_names
835                                .entry(FuncIndex::from_u32(f.index))
836                                .or_insert(HashMap::new())
837                                .insert(index, name);
838                        }
839                    }
840                }
841                wasmparser::Name::Label(_)
842                | wasmparser::Name::Type(_)
843                | wasmparser::Name::Table(_)
844                | wasmparser::Name::Global(_)
845                | wasmparser::Name::Memory(_)
846                | wasmparser::Name::Element(_)
847                | wasmparser::Name::Data(_)
848                | wasmparser::Name::Tag(_)
849                | wasmparser::Name::Field(_)
850                | wasmparser::Name::Unknown { .. } => {}
851            }
852        }
853        Ok(())
854    }
855}
856
857impl TypeConvert for ModuleEnvironment<'_, '_> {
858    fn lookup_heap_type(&self, index: wasmparser::UnpackedIndex) -> WasmHeapType {
859        WasmparserTypeConverter::new(&self.types, |idx| self.result.module.types[idx])
860            .lookup_heap_type(index)
861    }
862
863    fn lookup_type_index(&self, index: wasmparser::UnpackedIndex) -> EngineOrModuleTypeIndex {
864        WasmparserTypeConverter::new(&self.types, |idx| self.result.module.types[idx])
865            .lookup_type_index(index)
866    }
867}
868
869impl ModuleTranslation<'_> {
870    /// Attempts to convert segmented memory initialization into static
871    /// initialization for the module that this translation represents.
872    ///
873    /// If this module's memory initialization is not compatible with paged
874    /// initialization then this won't change anything. Otherwise if it is
875    /// compatible then the `memory_initialization` field will be updated.
876    ///
877    /// Takes a `page_size` argument in order to ensure that all
878    /// initialization is page-aligned for mmap-ability, and
879    /// `max_image_size_always_allowed` to control how we decide
880    /// whether to use static init.
881    ///
882    /// We will try to avoid generating very sparse images, which are
883    /// possible if e.g. a module has an initializer at offset 0 and a
884    /// very high offset (say, 1 GiB). To avoid this, we use a dual
885    /// condition: we always allow images less than
886    /// `max_image_size_always_allowed`, and the embedder of Wasmtime
887    /// can set this if desired to ensure that static init should
888    /// always be done if the size of the module or its heaps is
889    /// otherwise bounded by the system. We also allow images with
890    /// static init data bigger than that, but only if it is "dense",
891    /// defined as having at least half (50%) of its pages with some
892    /// data.
893    ///
894    /// We could do something slightly better by building a dense part
895    /// and keeping a sparse list of outlier/leftover segments (see
896    /// issue #3820). This would also allow mostly-static init of
897    /// modules that have some dynamically-placed data segments. But,
898    /// for now, this is sufficient to allow a system that "knows what
899    /// it's doing" to always get static init.
900    pub fn try_static_init(&mut self, page_size: u64, max_image_size_always_allowed: u64) {
901        // This method only attempts to transform a `Segmented` memory init
902        // into a `Static` one, no other state.
903        if !self.module.memory_initialization.is_segmented() {
904            return;
905        }
906
907        // First a dry run of memory initialization is performed. This
908        // collects information about the extent of memory initialized for each
909        // memory as well as the size of all data segments being copied in.
910        struct Memory {
911            data_size: u64,
912            min_addr: u64,
913            max_addr: u64,
914            // The `usize` here is a pointer into `self.data` which is the list
915            // of data segments corresponding to what was found in the original
916            // wasm module.
917            segments: Vec<(usize, StaticMemoryInitializer)>,
918        }
919        let mut info = PrimaryMap::with_capacity(self.module.memories.len());
920        for _ in 0..self.module.memories.len() {
921            info.push(Memory {
922                data_size: 0,
923                min_addr: u64::MAX,
924                max_addr: 0,
925                segments: Vec::new(),
926            });
927        }
928
929        struct InitMemoryAtCompileTime<'a> {
930            module: &'a Module,
931            info: &'a mut PrimaryMap<MemoryIndex, Memory>,
932            idx: usize,
933        }
934        impl InitMemory for InitMemoryAtCompileTime<'_> {
935            fn memory_size_in_bytes(
936                &mut self,
937                memory_index: MemoryIndex,
938            ) -> Result<u64, SizeOverflow> {
939                self.module.memories[memory_index].minimum_byte_size()
940            }
941
942            fn eval_offset(&mut self, memory_index: MemoryIndex, expr: &ConstExpr) -> Option<u64> {
943                match (expr.ops(), self.module.memories[memory_index].idx_type) {
944                    (&[ConstOp::I32Const(offset)], IndexType::I32) => {
945                        Some(offset.unsigned().into())
946                    }
947                    (&[ConstOp::I64Const(offset)], IndexType::I64) => Some(offset.unsigned()),
948                    _ => None,
949                }
950            }
951
952            fn write(&mut self, memory: MemoryIndex, init: &StaticMemoryInitializer) -> bool {
953                // Currently `Static` only applies to locally-defined memories,
954                // so if a data segment references an imported memory then
955                // transitioning to a `Static` memory initializer is not
956                // possible.
957                if self.module.defined_memory_index(memory).is_none() {
958                    return false;
959                };
960                let info = &mut self.info[memory];
961                let data_len = u64::from(init.data.end - init.data.start);
962                if data_len > 0 {
963                    info.data_size += data_len;
964                    info.min_addr = info.min_addr.min(init.offset);
965                    info.max_addr = info.max_addr.max(init.offset + data_len);
966                    info.segments.push((self.idx, init.clone()));
967                }
968                self.idx += 1;
969                true
970            }
971        }
972        let ok = self
973            .module
974            .memory_initialization
975            .init_memory(&mut InitMemoryAtCompileTime {
976                idx: 0,
977                module: &self.module,
978                info: &mut info,
979            });
980        if !ok {
981            return;
982        }
983
984        // Validate that the memory information collected is indeed valid for
985        // static memory initialization.
986        for (i, info) in info.iter().filter(|(_, info)| info.data_size > 0) {
987            let image_size = info.max_addr - info.min_addr;
988
989            // Simplify things for now by bailing out entirely if any memory has
990            // a page size smaller than the host's page size. This fixes a case
991            // where currently initializers are created in host-page-size units
992            // of length which means that a larger-than-the-entire-memory
993            // initializer can be created. This can be handled technically but
994            // would require some more changes to help fix the assert elsewhere
995            // that this protects against.
996            if self.module.memories[i].page_size() < page_size {
997                return;
998            }
999
1000            // If the range of memory being initialized is less than twice the
1001            // total size of the data itself then it's assumed that static
1002            // initialization is ok. This means we'll at most double memory
1003            // consumption during the memory image creation process, which is
1004            // currently assumed to "probably be ok" but this will likely need
1005            // tweaks over time.
1006            if image_size < info.data_size.saturating_mul(2) {
1007                continue;
1008            }
1009
1010            // If the memory initialization image is larger than the size of all
1011            // data, then we still allow memory initialization if the image will
1012            // be of a relatively modest size, such as 1MB here.
1013            if image_size < max_image_size_always_allowed {
1014                continue;
1015            }
1016
1017            // At this point memory initialization is concluded to be too
1018            // expensive to do at compile time so it's entirely deferred to
1019            // happen at runtime.
1020            return;
1021        }
1022
1023        // Here's where we've now committed to changing to static memory. The
1024        // memory initialization image is built here from the page data and then
1025        // it's converted to a single initializer.
1026        let data = mem::replace(&mut self.data, Vec::new());
1027        let mut map = PrimaryMap::with_capacity(info.len());
1028        let mut module_data_size = 0u32;
1029        for (memory, info) in info.iter() {
1030            // Create the in-memory `image` which is the initialized contents of
1031            // this linear memory.
1032            let extent = if info.segments.len() > 0 {
1033                (info.max_addr - info.min_addr) as usize
1034            } else {
1035                0
1036            };
1037            let mut image = Vec::with_capacity(extent);
1038            for (idx, init) in info.segments.iter() {
1039                let data = &data[*idx];
1040                assert_eq!(data.len(), init.data.len());
1041                let offset = usize::try_from(init.offset - info.min_addr).unwrap();
1042                if image.len() < offset {
1043                    image.resize(offset, 0u8);
1044                    image.extend_from_slice(data);
1045                } else {
1046                    image.splice(
1047                        offset..(offset + data.len()).min(image.len()),
1048                        data.iter().copied(),
1049                    );
1050                }
1051            }
1052            assert_eq!(image.len(), extent);
1053            assert_eq!(image.capacity(), extent);
1054            let mut offset = if info.segments.len() > 0 {
1055                info.min_addr
1056            } else {
1057                0
1058            };
1059
1060            // Chop off trailing zeros from the image as memory is already
1061            // zero-initialized. Note that `i` is the position of a nonzero
1062            // entry here, so to not lose it we truncate to `i + 1`.
1063            if let Some(i) = image.iter().rposition(|i| *i != 0) {
1064                image.truncate(i + 1);
1065            }
1066
1067            // Also chop off leading zeros, if any.
1068            if let Some(i) = image.iter().position(|i| *i != 0) {
1069                offset += i as u64;
1070                image.drain(..i);
1071            }
1072            let mut len = u64::try_from(image.len()).unwrap();
1073
1074            // The goal is to enable mapping this image directly into memory, so
1075            // the offset into linear memory must be a multiple of the page
1076            // size. If that's not already the case then the image is padded at
1077            // the front and back with extra zeros as necessary
1078            if offset % page_size != 0 {
1079                let zero_padding = offset % page_size;
1080                self.data.push(vec![0; zero_padding as usize].into());
1081                offset -= zero_padding;
1082                len += zero_padding;
1083            }
1084            self.data.push(image.into());
1085            if len % page_size != 0 {
1086                let zero_padding = page_size - (len % page_size);
1087                self.data.push(vec![0; zero_padding as usize].into());
1088                len += zero_padding;
1089            }
1090
1091            // Offset/length should now always be page-aligned.
1092            assert!(offset % page_size == 0);
1093            assert!(len % page_size == 0);
1094
1095            // Create the `StaticMemoryInitializer` which describes this image,
1096            // only needed if the image is actually present and has a nonzero
1097            // length. The `offset` has been calculates above, originally
1098            // sourced from `info.min_addr`. The `data` field is the extent
1099            // within the final data segment we'll emit to an ELF image, which
1100            // is the concatenation of `self.data`, so here it's the size of
1101            // the section-so-far plus the current segment we're appending.
1102            let len = u32::try_from(len).unwrap();
1103            let init = if len > 0 {
1104                Some(StaticMemoryInitializer {
1105                    offset,
1106                    data: module_data_size..module_data_size + len,
1107                })
1108            } else {
1109                None
1110            };
1111            let idx = map.push(init);
1112            assert_eq!(idx, memory);
1113            module_data_size += len;
1114        }
1115        self.data_align = Some(page_size);
1116        self.module.memory_initialization = MemoryInitialization::Static { map };
1117    }
1118
1119    /// Attempts to convert the module's table initializers to
1120    /// FuncTable form where possible. This enables lazy table
1121    /// initialization later by providing a one-to-one map of initial
1122    /// table values, without having to parse all segments.
1123    pub fn try_func_table_init(&mut self) {
1124        // This should be large enough to support very large Wasm
1125        // modules with huge funcref tables, but small enough to avoid
1126        // OOMs or DoS on truly sparse tables.
1127        const MAX_FUNC_TABLE_SIZE: u64 = 1024 * 1024;
1128
1129        // First convert any element-initialized tables to images of just that
1130        // single function if the minimum size of the table allows doing so.
1131        for ((_, init), (_, table)) in self
1132            .module
1133            .table_initialization
1134            .initial_values
1135            .iter_mut()
1136            .zip(
1137                self.module
1138                    .tables
1139                    .iter()
1140                    .skip(self.module.num_imported_tables),
1141            )
1142        {
1143            let table_size = table.limits.min;
1144            if table_size > MAX_FUNC_TABLE_SIZE {
1145                continue;
1146            }
1147            if let TableInitialValue::Expr(expr) = init {
1148                if let [ConstOp::RefFunc(f)] = expr.ops() {
1149                    *init = TableInitialValue::Null {
1150                        precomputed: vec![*f; table_size as usize],
1151                    };
1152                }
1153            }
1154        }
1155
1156        let mut segments = mem::take(&mut self.module.table_initialization.segments)
1157            .into_iter()
1158            .peekable();
1159
1160        // The goal of this loop is to interpret a table segment and apply it
1161        // "statically" to a local table. This will iterate over segments and
1162        // apply them one-by-one to each table.
1163        //
1164        // If any segment can't be applied, however, then this loop exits and
1165        // all remaining segments are placed back into the segment list. This is
1166        // because segments are supposed to be initialized one-at-a-time which
1167        // means that intermediate state is visible with respect to traps. If
1168        // anything isn't statically known to not trap it's pessimistically
1169        // assumed to trap meaning all further segment initializers must be
1170        // applied manually at instantiation time.
1171        while let Some(segment) = segments.peek() {
1172            let defined_index = match self.module.defined_table_index(segment.table_index) {
1173                Some(index) => index,
1174                // Skip imported tables: we can't provide a preconstructed
1175                // table for them, because their values depend on the
1176                // imported table overlaid with whatever segments we have.
1177                None => break,
1178            };
1179
1180            // If the base of this segment is dynamic, then we can't
1181            // include it in the statically-built array of initial
1182            // contents.
1183            let offset = match segment.offset.ops() {
1184                &[ConstOp::I32Const(offset)] => u64::from(offset.unsigned()),
1185                &[ConstOp::I64Const(offset)] => offset.unsigned(),
1186                _ => break,
1187            };
1188
1189            // Get the end of this segment. If out-of-bounds, or too
1190            // large for our dense table representation, then skip the
1191            // segment.
1192            let top = match offset.checked_add(segment.elements.len()) {
1193                Some(top) => top,
1194                None => break,
1195            };
1196            let table_size = self.module.tables[segment.table_index].limits.min;
1197            if top > table_size || top > MAX_FUNC_TABLE_SIZE {
1198                break;
1199            }
1200
1201            match self.module.tables[segment.table_index]
1202                .ref_type
1203                .heap_type
1204                .top()
1205            {
1206                WasmHeapTopType::Func => {}
1207                // If this is not a funcref table, then we can't support a
1208                // pre-computed table of function indices. Technically this
1209                // initializer won't trap so we could continue processing
1210                // segments, but that's left as a future optimization if
1211                // necessary.
1212                WasmHeapTopType::Any | WasmHeapTopType::Extern => break,
1213            }
1214
1215            // Function indices can be optimized here, but fully general
1216            // expressions are deferred to get evaluated at runtime.
1217            let function_elements = match &segment.elements {
1218                TableSegmentElements::Functions(indices) => indices,
1219                TableSegmentElements::Expressions(_) => break,
1220            };
1221
1222            let precomputed =
1223                match &mut self.module.table_initialization.initial_values[defined_index] {
1224                    TableInitialValue::Null { precomputed } => precomputed,
1225
1226                    // If this table is still listed as an initial value here
1227                    // then that means the initial size of the table doesn't
1228                    // support a precomputed function list, so skip this.
1229                    // Technically this won't trap so it's possible to process
1230                    // further initializers, but that's left as a future
1231                    // optimization.
1232                    TableInitialValue::Expr(_) => break,
1233                };
1234
1235            // At this point we're committing to pre-initializing the table
1236            // with the `segment` that's being iterated over. This segment is
1237            // applied to the `precomputed` list for the table by ensuring
1238            // it's large enough to hold the segment and then copying the
1239            // segment into the precomputed list.
1240            if precomputed.len() < top as usize {
1241                precomputed.resize(top as usize, FuncIndex::reserved_value());
1242            }
1243            let dst = &mut precomputed[offset as usize..top as usize];
1244            dst.copy_from_slice(&function_elements);
1245
1246            // advance the iterator to see the next segment
1247            let _ = segments.next();
1248        }
1249        self.module.table_initialization.segments = segments.collect();
1250    }
1251}