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
26pub struct ModuleEnvironment<'a, 'data> {
28 result: ModuleTranslation<'data>,
30
31 types: &'a mut ModuleTypesBuilder,
33
34 validator: &'a mut Validator,
36 tunables: &'a Tunables,
37}
38
39#[derive(Default)]
43pub struct ModuleTranslation<'data> {
44 pub module: Module,
46
47 pub wasm: &'data [u8],
53
54 pub function_body_inputs: PrimaryMap<DefinedFuncIndex, FunctionBodyData<'data>>,
56
57 pub exported_signatures: Vec<ModuleInternedTypeIndex>,
61
62 pub debuginfo: DebugInfoData<'data>,
64
65 pub has_unparsed_debuginfo: bool,
68
69 pub data: Vec<Cow<'data, [u8]>>,
75
76 pub data_align: Option<u64>,
83
84 total_data: u32,
86
87 pub passive_data: Vec<&'data [u8]>,
90
91 total_passive_data: u32,
93
94 code_index: u32,
97
98 types: Option<Types>,
101}
102
103impl<'data> ModuleTranslation<'data> {
104 pub fn get_types(&self) -> &Types {
106 self.types
107 .as_ref()
108 .expect("module type information to be available")
109 }
110}
111
112pub struct FunctionBodyData<'a> {
114 pub body: FunctionBody<'a>,
116 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 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 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 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 let mut type_index = 0;
259 while type_index < count {
260 let validator_types = self.validator.types(0).unwrap();
261
262 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 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 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 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 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 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 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 }
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 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 ".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 ".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 ".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 ".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 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 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 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 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 if (index as usize) >= self.result.module.functions.len() {
796 continue;
797 }
798
799 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 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 pub fn try_static_init(&mut self, page_size: u64, max_image_size_always_allowed: u64) {
901 if !self.module.memory_initialization.is_segmented() {
904 return;
905 }
906
907 struct Memory {
911 data_size: u64,
912 min_addr: u64,
913 max_addr: u64,
914 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 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 for (i, info) in info.iter().filter(|(_, info)| info.data_size > 0) {
987 let image_size = info.max_addr - info.min_addr;
988
989 if self.module.memories[i].page_size() < page_size {
997 return;
998 }
999
1000 if image_size < info.data_size.saturating_mul(2) {
1007 continue;
1008 }
1009
1010 if image_size < max_image_size_always_allowed {
1014 continue;
1015 }
1016
1017 return;
1021 }
1022
1023 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 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 if let Some(i) = image.iter().rposition(|i| *i != 0) {
1064 image.truncate(i + 1);
1065 }
1066
1067 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 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 assert!(offset % page_size == 0);
1093 assert!(len % page_size == 0);
1094
1095 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 pub fn try_func_table_init(&mut self) {
1124 const MAX_FUNC_TABLE_SIZE: u64 = 1024 * 1024;
1128
1129 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 while let Some(segment) = segments.peek() {
1172 let defined_index = match self.module.defined_table_index(segment.table_index) {
1173 Some(index) => index,
1174 None => break,
1178 };
1179
1180 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 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 WasmHeapTopType::Any | WasmHeapTopType::Extern => break,
1213 }
1214
1215 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 TableInitialValue::Expr(_) => break,
1233 };
1234
1235 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 let _ = segments.next();
1248 }
1249 self.module.table_initialization.segments = segments.collect();
1250 }
1251}