1use crate::error::{OutOfMemory, Result, bail};
2use crate::module::{
3 FuncRefIndex, Initializer, MemoryInitialization, MemoryInitializer, Module, TableSegment,
4 TableSegmentElements,
5};
6use crate::prelude::*;
7use crate::{
8 ConstExpr, ConstOp, DataIndex, DefinedFuncIndex, ElemIndex, EngineOrModuleTypeIndex,
9 EntityIndex, EntityType, FuncIndex, FuncKey, GlobalIndex, IndexType, InitMemory, MemoryIndex,
10 ModuleInternedTypeIndex, ModuleTypesBuilder, PanicOnOom as _, PrimaryMap, SizeOverflow,
11 StaticMemoryInitializer, StaticModuleIndex, TableIndex, TableInitialValue, Tag, TagIndex,
12 Tunables, TypeConvert, TypeIndex, WasmError, WasmHeapTopType, WasmHeapType, WasmResult,
13 WasmValType, WasmparserTypeConverter,
14};
15use cranelift_entity::SecondaryMap;
16use cranelift_entity::packed_option::ReservedValue;
17use std::borrow::Cow;
18use std::collections::HashMap;
19use std::mem;
20use std::path::PathBuf;
21use std::sync::Arc;
22use wasmparser::{
23 CustomSectionReader, DataKind, ElementItems, ElementKind, Encoding, ExternalKind,
24 FuncToValidate, FunctionBody, KnownCustom, NameSectionReader, Naming, Parser, Payload, TypeRef,
25 Validator, ValidatorResources, types::Types,
26};
27
28pub struct ModuleEnvironment<'a, 'data> {
30 result: ModuleTranslation<'data>,
32
33 types: &'a mut ModuleTypesBuilder,
35
36 validator: &'a mut Validator,
38 tunables: &'a Tunables,
39}
40
41pub struct ModuleTranslation<'data> {
46 pub module: Module,
48
49 pub wasm: &'data [u8],
55
56 pub wasm_module_offset: u64,
61
62 pub function_body_inputs: PrimaryMap<DefinedFuncIndex, FunctionBodyData<'data>>,
64
65 pub known_imported_functions: SecondaryMap<FuncIndex, Option<FuncKey>>,
74
75 pub exported_signatures: Vec<ModuleInternedTypeIndex>,
79
80 pub debuginfo: DebugInfoData<'data>,
82
83 pub has_unparsed_debuginfo: bool,
86
87 pub data: Vec<Cow<'data, [u8]>>,
93
94 pub data_align: Option<u64>,
101
102 total_data: u32,
104
105 pub passive_data: Vec<&'data [u8]>,
108
109 total_passive_data: u32,
111
112 code_index: u32,
115
116 types: Option<Types>,
119}
120
121impl<'data> ModuleTranslation<'data> {
122 pub fn new(module_index: StaticModuleIndex) -> Self {
124 Self {
125 module: Module::new(module_index),
126 wasm: &[],
127 wasm_module_offset: 0,
128 function_body_inputs: PrimaryMap::default(),
129 known_imported_functions: SecondaryMap::default(),
130 exported_signatures: Vec::default(),
131 debuginfo: DebugInfoData::default(),
132 has_unparsed_debuginfo: false,
133 data: Vec::default(),
134 data_align: None,
135 total_data: 0,
136 passive_data: Vec::default(),
137 total_passive_data: 0,
138 code_index: 0,
139 types: None,
140 }
141 }
142
143 pub fn get_types(&self) -> &Types {
145 self.types
146 .as_ref()
147 .expect("module type information to be available")
148 }
149
150 pub fn module_index(&self) -> StaticModuleIndex {
152 self.module.module_index
153 }
154}
155
156pub struct FunctionBodyData<'a> {
158 pub body: FunctionBody<'a>,
160 pub validator: FuncToValidate<ValidatorResources>,
162}
163
164#[derive(Debug, Default)]
165#[expect(missing_docs, reason = "self-describing fields")]
166pub struct DebugInfoData<'a> {
167 pub dwarf: Dwarf<'a>,
168 pub name_section: NameSection<'a>,
169 pub wasm_file: WasmFileInfo,
170 pub debug_loc: gimli::DebugLoc<Reader<'a>>,
171 pub debug_loclists: gimli::DebugLocLists<Reader<'a>>,
172 pub debug_ranges: gimli::DebugRanges<Reader<'a>>,
173 pub debug_rnglists: gimli::DebugRngLists<Reader<'a>>,
174 pub debug_cu_index: gimli::DebugCuIndex<Reader<'a>>,
175 pub debug_tu_index: gimli::DebugTuIndex<Reader<'a>>,
176}
177
178#[expect(missing_docs, reason = "self-describing")]
179pub type Dwarf<'input> = gimli::Dwarf<Reader<'input>>;
180
181type Reader<'input> = gimli::EndianSlice<'input, gimli::LittleEndian>;
182
183#[derive(Debug, Default)]
184#[expect(missing_docs, reason = "self-describing fields")]
185pub struct NameSection<'a> {
186 pub module_name: Option<&'a str>,
187 pub func_names: HashMap<FuncIndex, &'a str>,
188 pub locals_names: HashMap<FuncIndex, HashMap<u32, &'a str>>,
189}
190
191#[derive(Debug, Default)]
192#[expect(missing_docs, reason = "self-describing fields")]
193pub struct WasmFileInfo {
194 pub path: Option<PathBuf>,
195 pub code_section_offset: u64,
196 pub imported_func_count: u32,
197 pub funcs: Vec<FunctionMetadata>,
198}
199
200#[derive(Debug)]
201#[expect(missing_docs, reason = "self-describing fields")]
202pub struct FunctionMetadata {
203 pub params: Box<[WasmValType]>,
204 pub locals: Box<[(u32, WasmValType)]>,
205}
206
207impl<'a, 'data> ModuleEnvironment<'a, 'data> {
208 pub fn new(
210 tunables: &'a Tunables,
211 validator: &'a mut Validator,
212 types: &'a mut ModuleTypesBuilder,
213 module_index: StaticModuleIndex,
214 ) -> Self {
215 Self {
216 result: ModuleTranslation::new(module_index),
217 types,
218 tunables,
219 validator,
220 }
221 }
222
223 pub fn translate(
232 mut self,
233 parser: Parser,
234 data: &'data [u8],
235 ) -> Result<ModuleTranslation<'data>> {
236 self.result.wasm = data;
237
238 for payload in parser.parse_all(data) {
239 self.translate_payload(payload?)?;
240 }
241
242 Ok(self.result)
243 }
244
245 fn translate_payload(&mut self, payload: Payload<'data>) -> Result<()> {
246 match payload {
247 Payload::Version {
248 num,
249 encoding,
250 range,
251 } => {
252 self.validator.version(num, encoding, &range)?;
253 match encoding {
254 Encoding::Module => {}
255 Encoding::Component => {
256 bail!("expected a WebAssembly module but was given a WebAssembly component")
257 }
258 }
259 }
260
261 Payload::End(offset) => {
262 self.result.types = Some(self.validator.end(offset)?);
263
264 self.result.exported_signatures = self
268 .result
269 .module
270 .functions
271 .iter()
272 .filter_map(|(_, func)| {
273 if func.is_escaping() {
274 Some(func.signature.unwrap_module_type_index())
275 } else {
276 None
277 }
278 })
279 .collect();
280 self.result.exported_signatures.sort_unstable();
281 self.result.exported_signatures.dedup();
282 }
283
284 Payload::TypeSection(types) => {
285 self.validator.type_section(&types)?;
286
287 let count = self.validator.types(0).unwrap().core_type_count_in_module();
288 log::trace!("interning {count} Wasm types");
289
290 let capacity = usize::try_from(count).unwrap();
291 self.result.module.types.reserve(capacity)?;
292 self.types.reserve_wasm_signatures(capacity);
293
294 let mut type_index = 0;
304 while type_index < count {
305 let validator_types = self.validator.types(0).unwrap();
306
307 log::trace!("looking up wasmparser type for index {type_index}");
310 let core_type_id = validator_types.core_type_at_in_module(type_index);
311 log::trace!(
312 " --> {core_type_id:?} = {:?}",
313 validator_types[core_type_id],
314 );
315 let rec_group_id = validator_types.rec_group_id_of(core_type_id);
316 debug_assert_eq!(
317 validator_types
318 .rec_group_elements(rec_group_id)
319 .position(|id| id == core_type_id),
320 Some(0)
321 );
322
323 let interned = self.types.intern_rec_group(validator_types, rec_group_id)?;
326 let elems = self.types.rec_group_elements(interned);
327 let len = elems.len();
328 self.result.module.types.reserve(len)?;
329 for ty in elems {
330 self.result.module.types.push(ty.into())?;
331 }
332
333 type_index += u32::try_from(len).unwrap();
335 }
336 }
337
338 Payload::ImportSection(imports) => {
339 self.validator.import_section(&imports)?;
340
341 let cnt = usize::try_from(imports.count()).unwrap();
342 self.result.module.initializers.reserve(cnt)?;
343
344 for entry in imports.into_imports() {
345 let import = entry?;
346 let ty = match import.ty {
347 TypeRef::Func(index) => {
348 let index = TypeIndex::from_u32(index);
349 let interned_index = self.result.module.types[index];
350 self.result.module.num_imported_funcs += 1;
351 self.result.debuginfo.wasm_file.imported_func_count += 1;
352 EntityType::Function(interned_index)
353 }
354 TypeRef::Memory(ty) => {
355 self.result.module.num_imported_memories += 1;
356 EntityType::Memory(ty.into())
357 }
358 TypeRef::Global(ty) => {
359 self.result.module.num_imported_globals += 1;
360 EntityType::Global(self.convert_global_type(&ty)?)
361 }
362 TypeRef::Table(ty) => {
363 self.result.module.num_imported_tables += 1;
364 EntityType::Table(self.convert_table_type(&ty)?)
365 }
366 TypeRef::Tag(ty) => {
367 let index = TypeIndex::from_u32(ty.func_type_idx);
368 let signature = self.result.module.types[index];
369 let exception = self.types.define_exception_type_for_tag(
370 signature.unwrap_module_type_index(),
371 );
372 let tag = Tag {
373 signature,
374 exception: EngineOrModuleTypeIndex::Module(exception),
375 };
376 self.result.module.num_imported_tags += 1;
377 EntityType::Tag(tag)
378 }
379 TypeRef::FuncExact(_) => {
380 bail!("custom-descriptors proposal not implemented yet");
381 }
382 };
383 self.declare_import(import.module, import.name, ty)?;
384 }
385 }
386
387 Payload::FunctionSection(functions) => {
388 self.validator.function_section(&functions)?;
389
390 let cnt = usize::try_from(functions.count()).unwrap();
391 self.result.module.functions.reserve_exact(cnt)?;
392
393 for entry in functions {
394 let sigindex = entry?;
395 let ty = TypeIndex::from_u32(sigindex);
396 let interned_index = self.result.module.types[ty];
397 self.result.module.push_function(interned_index);
398 }
399 }
400
401 Payload::TableSection(tables) => {
402 self.validator.table_section(&tables)?;
403 let cnt = usize::try_from(tables.count()).unwrap();
404 self.result.module.tables.reserve_exact(cnt)?;
405
406 for entry in tables {
407 let wasmparser::Table { ty, init } = entry?;
408 let table = self.convert_table_type(&ty)?;
409 self.result.module.needs_gc_heap |= table.ref_type.is_vmgcref_type();
410 self.result.module.tables.push(table)?;
411 let init = match init {
412 wasmparser::TableInit::RefNull => TableInitialValue::Null {
413 precomputed: TryVec::new(),
414 },
415 wasmparser::TableInit::Expr(expr) => {
416 let (init, escaped) = ConstExpr::from_wasmparser(self, expr)?;
417 for f in escaped {
418 self.flag_func_escaped(f);
419 }
420 TableInitialValue::Expr(init)
421 }
422 };
423 self.result
424 .module
425 .table_initialization
426 .initial_values
427 .push(init)?;
428 }
429 }
430
431 Payload::MemorySection(memories) => {
432 self.validator.memory_section(&memories)?;
433
434 let cnt = usize::try_from(memories.count()).unwrap();
435 self.result.module.memories.reserve_exact(cnt)?;
436
437 for entry in memories {
438 let memory = entry?;
439 self.result.module.memories.push(memory.into())?;
440 }
441 }
442
443 Payload::TagSection(tags) => {
444 self.validator.tag_section(&tags)?;
445
446 for entry in tags {
447 let sigindex = entry?.func_type_idx;
448 let ty = TypeIndex::from_u32(sigindex);
449 let interned_index = self.result.module.types[ty];
450 let exception = self
451 .types
452 .define_exception_type_for_tag(interned_index.unwrap_module_type_index());
453 self.result.module.push_tag(interned_index, exception);
454 }
455 }
456
457 Payload::GlobalSection(globals) => {
458 self.validator.global_section(&globals)?;
459
460 let cnt = usize::try_from(globals.count()).unwrap();
461 self.result.module.globals.reserve_exact(cnt)?;
462
463 for entry in globals {
464 let wasmparser::Global { ty, init_expr } = entry?;
465 let (initializer, escaped) = ConstExpr::from_wasmparser(self, init_expr)?;
466 for f in escaped {
467 self.flag_func_escaped(f);
468 }
469 let ty = self.convert_global_type(&ty)?;
470 self.result.module.globals.push(ty)?;
471 self.result.module.global_initializers.push(initializer)?;
472 }
473 }
474
475 Payload::ExportSection(exports) => {
476 self.validator.export_section(&exports)?;
477
478 let cnt = usize::try_from(exports.count()).unwrap();
479 self.result.module.exports.reserve(cnt)?;
480
481 for entry in exports {
482 let wasmparser::Export { name, kind, index } = entry?;
483 let entity = match kind {
484 ExternalKind::Func | ExternalKind::FuncExact => {
485 let index = FuncIndex::from_u32(index);
486 self.flag_func_escaped(index);
487 EntityIndex::Function(index)
488 }
489 ExternalKind::Table => EntityIndex::Table(TableIndex::from_u32(index)),
490 ExternalKind::Memory => EntityIndex::Memory(MemoryIndex::from_u32(index)),
491 ExternalKind::Global => EntityIndex::Global(GlobalIndex::from_u32(index)),
492 ExternalKind::Tag => EntityIndex::Tag(TagIndex::from_u32(index)),
493 };
494 let name = self.result.module.strings.insert(name)?;
495 self.result.module.exports.insert(name, entity)?;
496 }
497 }
498
499 Payload::StartSection { func, range } => {
500 self.validator.start_section(func, &range)?;
501
502 let func_index = FuncIndex::from_u32(func);
503 self.flag_func_escaped(func_index);
504 debug_assert!(self.result.module.start_func.is_none());
505 self.result.module.start_func = Some(func_index);
506 }
507
508 Payload::ElementSection(elements) => {
509 self.validator.element_section(&elements)?;
510
511 for (index, entry) in elements.into_iter().enumerate() {
512 let wasmparser::Element {
513 kind,
514 items,
515 range: _,
516 } = entry?;
517
518 let elements = match items {
524 ElementItems::Functions(funcs) => {
525 let mut elems =
526 Vec::with_capacity(usize::try_from(funcs.count()).unwrap());
527 for func in funcs {
528 let func = FuncIndex::from_u32(func?);
529 self.flag_func_escaped(func);
530 elems.push(func);
531 }
532 TableSegmentElements::Functions(elems.into())
533 }
534 ElementItems::Expressions(ty, items) => {
535 let ty = self.convert_ref_type(ty)?;
536 let mut exprs =
537 Vec::with_capacity(usize::try_from(items.count()).unwrap());
538 for expr in items {
539 let (expr, escaped) = ConstExpr::from_wasmparser(self, expr?)?;
540 exprs.push(expr);
541 for func in escaped {
542 self.flag_func_escaped(func);
543 }
544 }
545 TableSegmentElements::Expressions {
546 ty,
547 exprs: exprs.into(),
548 }
549 }
550 };
551
552 match kind {
553 ElementKind::Active {
554 table_index,
555 offset_expr,
556 } => {
557 let table_index = TableIndex::from_u32(table_index.unwrap_or(0));
558 let (offset, escaped) = ConstExpr::from_wasmparser(self, offset_expr)?;
559 debug_assert!(escaped.is_empty());
560
561 self.result.module.table_initialization.segments.push(
562 TableSegment {
563 table_index,
564 offset,
565 elements,
566 },
567 )?;
568 }
569
570 ElementKind::Passive => {
571 let elem_index = ElemIndex::from_u32(index as u32);
572 let passive_index =
573 self.result.module.passive_elements.push(elements)?;
574 self.result
575 .module
576 .passive_elements_map
577 .insert(elem_index, passive_index);
578 }
579
580 ElementKind::Declared => {}
581 }
582 }
583 }
584
585 Payload::CodeSectionStart { count, range, .. } => {
586 self.validator.code_section_start(&range)?;
587 let cnt = usize::try_from(count).unwrap();
588 self.result.function_body_inputs.reserve_exact(cnt);
589 self.result.debuginfo.wasm_file.code_section_offset = range.start as u64;
590 }
591
592 Payload::CodeSectionEntry(body) => {
593 let validator = self.validator.code_section_entry(&body)?;
594 let func_index =
595 self.result.code_index + self.result.module.num_imported_funcs as u32;
596 let func_index = FuncIndex::from_u32(func_index);
597
598 if self.tunables.debug_native {
599 let sig_index = self.result.module.functions[func_index]
600 .signature
601 .unwrap_module_type_index();
602 let sig = self.types[sig_index].unwrap_func();
603 let mut locals = Vec::new();
604 for pair in body.get_locals_reader()? {
605 let (cnt, ty) = pair?;
606 let ty = self.convert_valtype(ty)?;
607 locals.push((cnt, ty));
608 }
609 self.result
610 .debuginfo
611 .wasm_file
612 .funcs
613 .push(FunctionMetadata {
614 locals: locals.into_boxed_slice(),
615 params: sig.params().into(),
616 });
617 }
618 if self.tunables.debug_guest {
619 self.flag_func_escaped(func_index);
623 }
624 self.result
625 .function_body_inputs
626 .push(FunctionBodyData { validator, body });
627 self.result.code_index += 1;
628 }
629
630 Payload::DataSection(data) => {
631 self.validator.data_section(&data)?;
632
633 let initializers = match &mut self.result.module.memory_initialization {
634 MemoryInitialization::Segmented(i) => i,
635 _ => unreachable!(),
636 };
637
638 let cnt = usize::try_from(data.count()).unwrap();
639 initializers.reserve_exact(cnt)?;
640 self.result.data.reserve_exact(cnt);
641
642 for (index, entry) in data.into_iter().enumerate() {
643 let wasmparser::Data {
644 kind,
645 data,
646 range: _,
647 } = entry?;
648 let mk_range = |total: &mut u32| -> Result<_, WasmError> {
649 let range = u32::try_from(data.len())
650 .ok()
651 .and_then(|size| {
652 let start = *total;
653 let end = start.checked_add(size)?;
654 Some(start..end)
655 })
656 .ok_or_else(|| {
657 WasmError::Unsupported(format!(
658 "more than 4 gigabytes of data in wasm module",
659 ))
660 })?;
661 *total += range.end - range.start;
662 Ok(range)
663 };
664 match kind {
665 DataKind::Active {
666 memory_index,
667 offset_expr,
668 } => {
669 let range = mk_range(&mut self.result.total_data)?;
670 let memory_index = MemoryIndex::from_u32(memory_index);
671 let (offset, escaped) = ConstExpr::from_wasmparser(self, offset_expr)?;
672 debug_assert!(escaped.is_empty());
673
674 let initializers = match &mut self.result.module.memory_initialization {
675 MemoryInitialization::Segmented(i) => i,
676 _ => unreachable!(),
677 };
678 initializers.push(MemoryInitializer {
679 memory_index,
680 offset,
681 data: range,
682 })?;
683 self.result.data.push(data.into());
684 }
685 DataKind::Passive => {
686 let data_index = DataIndex::from_u32(index as u32);
687 let range = mk_range(&mut self.result.total_passive_data)?;
688 self.result.passive_data.push(data);
689 self.result
690 .module
691 .passive_data_map
692 .insert(data_index, range);
693 }
694 }
695 }
696 }
697
698 Payload::DataCountSection { count, range } => {
699 self.validator.data_count_section(count, &range)?;
700
701 }
707
708 Payload::CustomSection(s)
709 if s.name() == "webidl-bindings" || s.name() == "wasm-interface-types" =>
710 {
711 bail!(
712 "\
713Support for interface types has temporarily been removed from `wasmtime`.
714
715For more information about this temporary change you can read on the issue online:
716
717 https://github.com/bytecodealliance/wasmtime/issues/1271
718
719and for re-adding support for interface types you can see this issue:
720
721 https://github.com/bytecodealliance/wasmtime/issues/677
722"
723 )
724 }
725
726 Payload::CustomSection(s) => {
727 self.register_custom_section(&s);
728 }
729
730 other => {
735 self.validator.payload(&other)?;
736 panic!("unimplemented section in wasm file {other:?}");
737 }
738 }
739 Ok(())
740 }
741
742 fn register_custom_section(&mut self, section: &CustomSectionReader<'data>) {
743 match section.as_known() {
744 KnownCustom::Name(name) => {
745 let result = self.name_section(name);
746 if let Err(e) = result {
747 log::warn!("failed to parse name section {e:?}");
748 }
749 }
750 _ => {
751 let name = section.name().trim_end_matches(".dwo");
752 if name.starts_with(".debug_") {
753 self.dwarf_section(name, section);
754 }
755 }
756 }
757 }
758
759 fn dwarf_section(&mut self, name: &str, section: &CustomSectionReader<'data>) {
760 if !self.tunables.debug_native && !self.tunables.parse_wasm_debuginfo {
761 self.result.has_unparsed_debuginfo = true;
762 return;
763 }
764 let info = &mut self.result.debuginfo;
765 let dwarf = &mut info.dwarf;
766 let endian = gimli::LittleEndian;
767 let data = section.data();
768 let slice = gimli::EndianSlice::new(data, endian);
769
770 match name {
771 ".debug_abbrev" => dwarf.debug_abbrev = gimli::DebugAbbrev::new(data, endian),
773 ".debug_addr" => dwarf.debug_addr = gimli::DebugAddr::from(slice),
774 ".debug_info" => {
775 dwarf.debug_info = gimli::DebugInfo::new(data, endian);
776 }
777 ".debug_line" => dwarf.debug_line = gimli::DebugLine::new(data, endian),
778 ".debug_line_str" => dwarf.debug_line_str = gimli::DebugLineStr::from(slice),
779 ".debug_str" => dwarf.debug_str = gimli::DebugStr::new(data, endian),
780 ".debug_str_offsets" => dwarf.debug_str_offsets = gimli::DebugStrOffsets::from(slice),
781 ".debug_str_sup" => {
782 let mut dwarf_sup: Dwarf<'data> = Default::default();
783 dwarf_sup.debug_str = gimli::DebugStr::from(slice);
784 dwarf.sup = Some(Arc::new(dwarf_sup));
785 }
786 ".debug_types" => dwarf.debug_types = gimli::DebugTypes::from(slice),
787
788 ".debug_loc" => info.debug_loc = gimli::DebugLoc::from(slice),
790 ".debug_loclists" => info.debug_loclists = gimli::DebugLocLists::from(slice),
791 ".debug_ranges" => info.debug_ranges = gimli::DebugRanges::new(data, endian),
792 ".debug_rnglists" => info.debug_rnglists = gimli::DebugRngLists::new(data, endian),
793
794 ".debug_cu_index" => info.debug_cu_index = gimli::DebugCuIndex::new(data, endian),
796 ".debug_tu_index" => info.debug_tu_index = gimli::DebugTuIndex::new(data, endian),
797
798 ".debug_aranges" | ".debug_pubnames" | ".debug_pubtypes" => return,
800 other => {
801 log::warn!("unknown debug section `{other}`");
802 return;
803 }
804 }
805
806 dwarf.ranges = gimli::RangeLists::new(info.debug_ranges, info.debug_rnglists);
807 dwarf.locations = gimli::LocationLists::new(info.debug_loc, info.debug_loclists);
808 }
809
810 fn declare_import(
823 &mut self,
824 module: &'data str,
825 field: &'data str,
826 ty: EntityType,
827 ) -> Result<(), OutOfMemory> {
828 let index = self.push_type(ty);
829 self.result.module.initializers.push(Initializer::Import {
830 name: self.result.module.strings.insert(module)?,
831 field: self.result.module.strings.insert(field)?,
832 index,
833 })?;
834 Ok(())
835 }
836
837 fn push_type(&mut self, ty: EntityType) -> EntityIndex {
838 match ty {
839 EntityType::Function(ty) => EntityIndex::Function({
840 let func_index = self
841 .result
842 .module
843 .push_function(ty.unwrap_module_type_index());
844 self.flag_func_escaped(func_index);
847 func_index
848 }),
849 EntityType::Table(ty) => {
850 EntityIndex::Table(self.result.module.tables.push(ty).panic_on_oom())
851 }
852 EntityType::Memory(ty) => {
853 EntityIndex::Memory(self.result.module.memories.push(ty).panic_on_oom())
854 }
855 EntityType::Global(ty) => {
856 EntityIndex::Global(self.result.module.globals.push(ty).panic_on_oom())
857 }
858 EntityType::Tag(ty) => {
859 EntityIndex::Tag(self.result.module.tags.push(ty).panic_on_oom())
860 }
861 }
862 }
863
864 fn flag_func_escaped(&mut self, func: FuncIndex) {
865 let ty = &mut self.result.module.functions[func];
866 if ty.is_escaping() {
868 return;
869 }
870 let index = self.result.module.num_escaped_funcs as u32;
871 ty.func_ref = FuncRefIndex::from_u32(index);
872 self.result.module.num_escaped_funcs += 1;
873 }
874
875 fn name_section(&mut self, names: NameSectionReader<'data>) -> WasmResult<()> {
877 for subsection in names {
878 match subsection? {
879 wasmparser::Name::Function(names) => {
880 for name in names {
881 let Naming { index, name } = name?;
882 if (index as usize) >= self.result.module.functions.len() {
885 continue;
886 }
887
888 let index = FuncIndex::from_u32(index);
893 self.result
894 .debuginfo
895 .name_section
896 .func_names
897 .insert(index, name);
898 }
899 }
900 wasmparser::Name::Module { name, .. } => {
901 self.result.module.name =
902 Some(self.result.module.strings.insert(name).panic_on_oom());
903 if self.tunables.debug_native {
904 self.result.debuginfo.name_section.module_name = Some(name);
905 }
906 }
907 wasmparser::Name::Local(reader) => {
908 if !self.tunables.debug_native {
909 continue;
910 }
911 for f in reader {
912 let f = f?;
913 if (f.index as usize) >= self.result.module.functions.len() {
916 continue;
917 }
918 for name in f.names {
919 let Naming { index, name } = name?;
920
921 self.result
922 .debuginfo
923 .name_section
924 .locals_names
925 .entry(FuncIndex::from_u32(f.index))
926 .or_insert(HashMap::new())
927 .insert(index, name);
928 }
929 }
930 }
931 wasmparser::Name::Label(_)
932 | wasmparser::Name::Type(_)
933 | wasmparser::Name::Table(_)
934 | wasmparser::Name::Global(_)
935 | wasmparser::Name::Memory(_)
936 | wasmparser::Name::Element(_)
937 | wasmparser::Name::Data(_)
938 | wasmparser::Name::Tag(_)
939 | wasmparser::Name::Field(_)
940 | wasmparser::Name::Unknown { .. } => {}
941 }
942 }
943 Ok(())
944 }
945}
946
947impl TypeConvert for ModuleEnvironment<'_, '_> {
948 fn lookup_heap_type(&self, index: wasmparser::UnpackedIndex) -> WasmHeapType {
949 WasmparserTypeConverter::new(&self.types, |idx| {
950 self.result.module.types[idx].unwrap_module_type_index()
951 })
952 .lookup_heap_type(index)
953 }
954
955 fn lookup_type_index(&self, index: wasmparser::UnpackedIndex) -> EngineOrModuleTypeIndex {
956 WasmparserTypeConverter::new(&self.types, |idx| {
957 self.result.module.types[idx].unwrap_module_type_index()
958 })
959 .lookup_type_index(index)
960 }
961}
962
963impl ModuleTranslation<'_> {
964 pub fn try_static_init(&mut self, page_size: u64, max_image_size_always_allowed: u64) {
995 if !self.module.memory_initialization.is_segmented() {
998 return;
999 }
1000
1001 struct Memory {
1005 data_size: u64,
1006 min_addr: u64,
1007 max_addr: u64,
1008 segments: Vec<(usize, StaticMemoryInitializer)>,
1012 }
1013 let mut info = PrimaryMap::with_capacity(self.module.memories.len());
1014 for _ in 0..self.module.memories.len() {
1015 info.push(Memory {
1016 data_size: 0,
1017 min_addr: u64::MAX,
1018 max_addr: 0,
1019 segments: Vec::new(),
1020 });
1021 }
1022
1023 struct InitMemoryAtCompileTime<'a> {
1024 module: &'a Module,
1025 info: &'a mut PrimaryMap<MemoryIndex, Memory>,
1026 idx: usize,
1027 }
1028 impl InitMemory for InitMemoryAtCompileTime<'_> {
1029 fn memory_size_in_bytes(
1030 &mut self,
1031 memory_index: MemoryIndex,
1032 ) -> Result<u64, SizeOverflow> {
1033 self.module.memories[memory_index].minimum_byte_size()
1034 }
1035
1036 fn eval_offset(&mut self, memory_index: MemoryIndex, expr: &ConstExpr) -> Option<u64> {
1037 match (expr.ops(), self.module.memories[memory_index].idx_type) {
1038 (&[ConstOp::I32Const(offset)], IndexType::I32) => {
1039 Some(offset.cast_unsigned().into())
1040 }
1041 (&[ConstOp::I64Const(offset)], IndexType::I64) => Some(offset.cast_unsigned()),
1042 _ => None,
1043 }
1044 }
1045
1046 fn write(&mut self, memory: MemoryIndex, init: &StaticMemoryInitializer) -> bool {
1047 if self.module.defined_memory_index(memory).is_none() {
1052 return false;
1053 };
1054 let info = &mut self.info[memory];
1055 let data_len = u64::from(init.data.end - init.data.start);
1056 if data_len > 0 {
1057 info.data_size += data_len;
1058 info.min_addr = info.min_addr.min(init.offset);
1059 info.max_addr = info.max_addr.max(init.offset + data_len);
1060 info.segments.push((self.idx, init.clone()));
1061 }
1062 self.idx += 1;
1063 true
1064 }
1065 }
1066 let ok = self
1067 .module
1068 .memory_initialization
1069 .init_memory(&mut InitMemoryAtCompileTime {
1070 idx: 0,
1071 module: &self.module,
1072 info: &mut info,
1073 });
1074 if !ok {
1075 return;
1076 }
1077
1078 for (i, info) in info.iter().filter(|(_, info)| info.data_size > 0) {
1081 let image_size = info.max_addr - info.min_addr;
1082
1083 if self.module.memories[i].page_size() < page_size {
1091 return;
1092 }
1093
1094 if image_size < info.data_size.saturating_mul(2) {
1101 continue;
1102 }
1103
1104 if image_size < max_image_size_always_allowed {
1108 continue;
1109 }
1110
1111 return;
1115 }
1116
1117 let data = mem::replace(&mut self.data, Vec::new());
1121 let mut map = TryPrimaryMap::with_capacity(info.len()).panic_on_oom();
1122 let mut module_data_size = 0u32;
1123 for (memory, info) in info.iter() {
1124 let extent = if info.segments.len() > 0 {
1127 (info.max_addr - info.min_addr) as usize
1128 } else {
1129 0
1130 };
1131 let mut image = Vec::with_capacity(extent);
1132 for (idx, init) in info.segments.iter() {
1133 let data = &data[*idx];
1134 assert_eq!(data.len(), init.data.len());
1135 let offset = usize::try_from(init.offset - info.min_addr).unwrap();
1136 if image.len() < offset {
1137 image.resize(offset, 0u8);
1138 image.extend_from_slice(data);
1139 } else {
1140 image.splice(
1141 offset..(offset + data.len()).min(image.len()),
1142 data.iter().copied(),
1143 );
1144 }
1145 }
1146 assert_eq!(image.len(), extent);
1147 assert_eq!(image.capacity(), extent);
1148 let mut offset = if info.segments.len() > 0 {
1149 info.min_addr
1150 } else {
1151 0
1152 };
1153
1154 if let Some(i) = image.iter().rposition(|i| *i != 0) {
1158 image.truncate(i + 1);
1159 }
1160
1161 if let Some(i) = image.iter().position(|i| *i != 0) {
1163 offset += i as u64;
1164 image.drain(..i);
1165 }
1166 let mut len = u64::try_from(image.len()).unwrap();
1167
1168 if offset % page_size != 0 {
1173 let zero_padding = offset % page_size;
1174 self.data.push(vec![0; zero_padding as usize].into());
1175 offset -= zero_padding;
1176 len += zero_padding;
1177 }
1178 self.data.push(image.into());
1179 if len % page_size != 0 {
1180 let zero_padding = page_size - (len % page_size);
1181 self.data.push(vec![0; zero_padding as usize].into());
1182 len += zero_padding;
1183 }
1184
1185 assert!(offset % page_size == 0);
1187 assert!(len % page_size == 0);
1188
1189 let len = u32::try_from(len).unwrap();
1197 let init = if len > 0 {
1198 Some(StaticMemoryInitializer {
1199 offset,
1200 data: module_data_size..module_data_size + len,
1201 })
1202 } else {
1203 None
1204 };
1205 let idx = map.push(init).panic_on_oom();
1206 assert_eq!(idx, memory);
1207 module_data_size += len;
1208 }
1209 self.data_align = Some(page_size);
1210 self.module.memory_initialization = MemoryInitialization::Static { map };
1211 }
1212
1213 pub fn try_func_table_init(&mut self) {
1218 const MAX_FUNC_TABLE_SIZE: u64 = 1024 * 1024;
1222
1223 for ((_, init), (_, table)) in self
1226 .module
1227 .table_initialization
1228 .initial_values
1229 .iter_mut()
1230 .zip(
1231 self.module
1232 .tables
1233 .iter()
1234 .skip(self.module.num_imported_tables),
1235 )
1236 {
1237 let table_size = table.limits.min;
1238 if table_size > MAX_FUNC_TABLE_SIZE {
1239 continue;
1240 }
1241 if let TableInitialValue::Expr(expr) = init {
1242 if let [ConstOp::RefFunc(f)] = expr.ops() {
1243 *init = TableInitialValue::Null {
1244 precomputed: try_vec![*f; table_size as usize].panic_on_oom(),
1245 };
1246 }
1247 }
1248 }
1249
1250 let mut segments = mem::take(&mut self.module.table_initialization.segments)
1251 .into_iter()
1252 .peekable();
1253
1254 while let Some(segment) = segments.peek() {
1266 let defined_index = match self.module.defined_table_index(segment.table_index) {
1267 Some(index) => index,
1268 None => break,
1272 };
1273
1274 let offset = match segment.offset.ops() {
1278 &[ConstOp::I32Const(offset)] => u64::from(offset.cast_unsigned()),
1279 &[ConstOp::I64Const(offset)] => offset.cast_unsigned(),
1280 _ => break,
1281 };
1282
1283 let top = match offset.checked_add(segment.elements.len()) {
1287 Some(top) => top,
1288 None => break,
1289 };
1290 let table_size = self.module.tables[segment.table_index].limits.min;
1291 if top > table_size || top > MAX_FUNC_TABLE_SIZE {
1292 break;
1293 }
1294
1295 match self.module.tables[segment.table_index]
1296 .ref_type
1297 .heap_type
1298 .top()
1299 {
1300 WasmHeapTopType::Func => {}
1301 WasmHeapTopType::Any
1307 | WasmHeapTopType::Extern
1308 | WasmHeapTopType::Cont
1309 | WasmHeapTopType::Exn => break,
1310 }
1311
1312 let function_elements = match &segment.elements {
1315 TableSegmentElements::Functions(indices) => indices,
1316 TableSegmentElements::Expressions { .. } => break,
1317 };
1318
1319 let precomputed =
1320 match &mut self.module.table_initialization.initial_values[defined_index] {
1321 TableInitialValue::Null { precomputed } => precomputed,
1322
1323 TableInitialValue::Expr(_) => break,
1330 };
1331
1332 if precomputed.len() < top as usize {
1338 precomputed
1339 .resize(top as usize, FuncIndex::reserved_value())
1340 .panic_on_oom();
1341 }
1342 let dst = &mut precomputed[offset as usize..top as usize];
1343 dst.copy_from_slice(&function_elements);
1344
1345 let _ = segments.next();
1347 }
1348 self.module.table_initialization.segments = segments.try_collect().panic_on_oom();
1349 }
1350}