rwasm 0.4.3

ZK-friendly WebAssembly runtime optimized for blockchain and zero-knowledge applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
use crate::{
    compiler::{
        block_fuel::compile_block_params,
        compiled_expr::CompiledExpr,
        func_builder::FuncBuilder,
        snippets::Snippet,
        translator::{InstructionTranslator, ReusableAllocations},
    },
    CompilationConfig, CompilationError, ConstructorParams, DataSegmentIdx, ElementSegmentIdx,
    FuncIdx, FuncRef, GlobalIdx, GlobalVariable, ImportName, Opcode, RwasmModule, RwasmModuleInner,
    TableIdx, DEFAULT_MEMORY_INDEX, SNIPPET_FUNC_IDX_UNRESOLVED,
};
use alloc::{boxed::Box, vec::Vec};
use core::{
    mem::{replace, take},
    ops::Range,
};
use hashbrown::HashMap;
use wasmparser::{
    CustomSectionReader, DataKind, DataSectionReader, ElementItems, ElementKind,
    ElementSectionReader, Encoding, ExportSectionReader, ExternalKind, FuncType, FunctionBody,
    FunctionSectionReader, GlobalSectionReader, ImportSectionReader, MemorySectionReader, Parser,
    Payload, TableSectionReader, Type, TypeRef, TypeSectionReader, ValType, Validator,
};

/// Single-pass Wasm front-end that validates, translates, and assembles rwasm bytecode.
/// It streams the Wasm module with wasmparser, builds the instruction set and sections,
/// and applies configuration (entry routing, snippets) before finalizing the module.
pub struct ModuleParser {
    /// The Wasm validator used throughout stream parsing.
    validator: Validator,
    /// The number of compiled or processed functions.
    compiled_funcs: u32,
    /// Reusable allocations for validating and translation functions.
    allocations: ReusableAllocations,
    /// A compilation config
    config: CompilationConfig,
}

impl ModuleParser {
    pub fn new(config: CompilationConfig) -> Self {
        Self {
            validator: Validator::new_with_features(config.wasm_features()),
            compiled_funcs: 0,
            allocations: ReusableAllocations::default(),
            config,
        }
    }

    pub fn parse(&mut self, wasm_binary: &[u8]) -> Result<(), CompilationError> {
        let parser = Parser::new(0);
        let payloads = parser.parse_all(wasm_binary).collect::<Vec<_>>();
        let mut func_bodies = Vec::new();
        for payload in payloads {
            match payload? {
                Payload::CodeSectionEntry(func_body) => {
                    func_bodies.push(func_body);
                }
                Payload::End(offset) => {
                    for func_body in take(&mut func_bodies) {
                        self.process_code_entry(func_body)?;
                    }
                    self.process_end(offset)?;
                }
                payload => {
                    self.process_payload(payload)?;
                }
            }
        }
        Ok(())
    }

    pub fn parse_function_exports(
        config: CompilationConfig,
        wasm_binary: &[u8],
    ) -> Result<Vec<(Box<str>, FuncIdx, FuncType)>, CompilationError> {
        let mut result = Vec::default();
        let mut parser = ModuleParser::new(config);
        parser.parse(wasm_binary)?;
        for (k, v) in &parser.allocations.translation.exported_funcs {
            let func_type_idx = parser.allocations.translation.resolve_func_type_index(*v);
            let func_type = parser
                .allocations
                .translation
                .func_type_registry
                .resolve_original_func_type(func_type_idx)
                .clone();
            result.push((k.clone(), *v, func_type));
            #[cfg(feature = "debug-print")]
            println!("{}: func_idx={}, func_type_idx={}", k, v, func_type_idx);
        }
        Ok(result)
    }

    pub fn finalize(
        mut self,
        wasm_binary: &[u8],
    ) -> Result<(RwasmModule, ConstructorParams), CompilationError> {
        if let Some(start_func) = self.allocations.translation.start_func {
            if !self.config.allow_start_section {
                return Err(CompilationError::StartSectionsAreNotAllowed);
            }
            self.allocations
                .translation
                .emit_function_call(start_func, true, false);
        }
        self.allocations
            .translation
            .segment_builder
            .entrypoint_bytecode
            .op_return();

        // A pointer to the instruction set (post-init section)
        let source_pc = self
            .allocations
            .translation
            .segment_builder
            .entrypoint_bytecode
            .len() as u32;

        if let Some(entrypoint_name) = self.config.entrypoint_name.as_ref() {
            let func_idx = self
                .allocations
                .translation
                .exported_funcs
                .get(entrypoint_name)
                .copied()
                .ok_or(CompilationError::MissingEntrypoint)?;
            self.allocations
                .translation
                .emit_function_call(func_idx, true, true);
        } else if self.config.state_router.is_none() {
            // if there is no state router, then such an application can't be executed; then why do
            // we need to compile it?
            return Err(CompilationError::MissingEntrypoint);
        }
        self.emit_snippets();
        // we can emit state router only at the end of a translation process
        self.emit_state_router()?;
        // the entrypoint always ends with an empty return
        self.allocations
            .translation
            .segment_builder
            .entrypoint_bytecode
            .finalize(true);

        // merge the entrypoint with our code section
        let mut code_section = self
            .allocations
            .translation
            .segment_builder
            .entrypoint_bytecode;
        let entrypoint_length = code_section.len() as u32;
        code_section.extend(self.allocations.translation.instruction_set.iter());

        // TODO(dmitry123): "optimize it"
        for instr in code_section.iter_mut() {
            match instr {
                Opcode::CallInternal(compiled_func)
                | Opcode::ReturnCallInternal(compiled_func)
                | Opcode::RefFunc(compiled_func) => {
                    if *compiled_func > 0 {
                        *compiled_func = self.allocations.translation.func_offsets
                            [*compiled_func as usize - 1]
                            + entrypoint_length;
                    }
                }
                _ => continue,
            }
        }

        let mut element_section = self
            .allocations
            .translation
            .segment_builder
            .global_element_section;
        for elem in element_section.iter_mut() {
            if *elem > 0 {
                *elem = self.allocations.translation.func_offsets[*elem as usize - 1]
                    + entrypoint_length;
            }
        }

        let module = RwasmModuleInner {
            code_section,
            data_section: self
                .allocations
                .translation
                .segment_builder
                .global_memory_section,
            elem_section: element_section,
            hint_section: wasm_binary.to_vec(),
            source_pc,
        };
        let constructor_params = self.allocations.translation.constructor_params;

        Ok((RwasmModule::from(module), constructor_params))
    }

    pub fn emit_state_router(&mut self) -> Result<(), CompilationError> {
        // if we have a state router, then translate state router
        let allow_malformed_entrypoint_func_type = self.config.allow_malformed_entrypoint_func_type;
        let Some(state_router) = &self.config.state_router else {
            return Ok(());
        };
        // push state on the stack
        if let Some(opcode) = &state_router.opcode {
            self.allocations
                .translation
                .segment_builder
                .entrypoint_bytecode
                .push(*opcode);
        }
        // translate state router
        for (entrypoint_name, state_value) in state_router.states.iter() {
            let Some(func_idx) = self
                .allocations
                .translation
                .exported_funcs
                .get(entrypoint_name)
                .copied()
            else {
                continue;
            };
            let func_type_idx = self
                .allocations
                .translation
                .resolve_func_type_index(func_idx);
            // make sure the func type is empty
            let is_empty_func_type = self
                .allocations
                .translation
                .func_type_registry
                .resolve_func_type_ref(func_type_idx, |func_type| {
                    func_type.params().is_empty() && func_type.results().is_empty()
                });
            if !is_empty_func_type && !allow_malformed_entrypoint_func_type {
                return Err(CompilationError::MalformedFuncType);
            }
            let entrypoint_bytecode = &mut self
                .allocations
                .translation
                .segment_builder
                .entrypoint_bytecode;
            entrypoint_bytecode.op_local_get(1u32);
            entrypoint_bytecode.op_i32_const(*state_value);
            entrypoint_bytecode.op_i32_eq();
            entrypoint_bytecode.op_br_if_eqz(3);
            // it's super important to drop the original state from the stack
            // because input params might be passed though the stack
            entrypoint_bytecode.op_drop();
            self.allocations
                .translation
                .emit_function_call(func_idx, true, true);
        }
        // drop input state from the stack
        self.allocations
            .translation
            .segment_builder
            .entrypoint_bytecode
            .op_drop();
        Ok(())
    }

    pub fn emit_snippets(&mut self) {
        if !self.config.code_snippets {
            return;
        }
        let mut emitted_snippets: HashMap<Snippet, FuncIdx> = HashMap::new();

        let snippet_calls = self.allocations.translation.snippet_calls.clone();
        for snippet_call in snippet_calls {
            let snippet = snippet_call.snippet;

            let snippet_func_idx = *emitted_snippets.entry(snippet).or_insert_with(|| {
                let new_func_idx = self.next_func();
                let alloc = &mut self.allocations.translation;
                let func_offset = alloc.instruction_set.len() as u32;
                alloc.func_offsets.push(func_offset);
                alloc
                    .instruction_set
                    .op_stack_check(snippet.max_stack_height());
                snippet.emit(&mut alloc.instruction_set);
                alloc.instruction_set.op_return();
                new_func_idx
            });

            let loc = snippet_call.loc;
            let alloc = &mut self.allocations.translation;
            let opcode = alloc.instruction_set.get_nth_mut(loc as usize)
                .unwrap_or_else(|| panic!("expected snippet call at index {loc}, but instruction set length is smaller"));

            match opcode {
                Opcode::CallInternal(func_idx) => {
                    assert_eq!(*func_idx, SNIPPET_FUNC_IDX_UNRESOLVED);
                    *func_idx = snippet_func_idx + 1;
                }
                other => {
                    panic!("expected Opcode::CallInternal at index {loc}, but found {other:?}")
                }
            }
        }
    }

    /// Processes the `wasmparser` payload.
    ///
    /// # Errors
    ///
    /// - If Wasm validation of the payload fails.
    /// - If some unsupported Wasm proposal definition is encountered.
    /// - If `rwasm` limits are exceeded.
    fn process_payload(&mut self, payload: Payload) -> Result<bool, CompilationError> {
        match payload {
            Payload::Version {
                num,
                encoding,
                range,
            } => self.process_version(num, encoding, range),
            Payload::TypeSection(section) => self.process_types(section),
            Payload::ImportSection(section) => self.process_imports(section),
            Payload::InstanceSection(section) => self.process_instances(section),
            Payload::FunctionSection(section) => self.process_functions(section),
            Payload::TableSection(section) => self.process_tables(section),
            Payload::MemorySection(section) => self.process_memories(section),
            Payload::TagSection(section) => self.process_tags(section),
            Payload::GlobalSection(section) => self.process_globals(section),
            Payload::ExportSection(section) => self.process_exports(section),
            Payload::StartSection { func, range } => self.process_start(func, range),
            Payload::ElementSection(section) => self.process_element(section),
            Payload::DataCountSection { count, range } => self.process_data_count(count, range),
            Payload::DataSection(section) => self.process_data(section),
            Payload::CustomSection(section) => self.process_custom_section(section),
            Payload::CodeSectionStart { count, range, .. } => self.process_code_start(count, range),
            Payload::CodeSectionEntry(func_body) => self.process_code_entry(func_body),
            Payload::UnknownSection { id, range, .. } => self.process_unknown(id, range),
            Payload::ModuleSection { parser: _, range } => {
                self.process_unsupported_component_model(range)
            }
            Payload::CoreTypeSection(section) => {
                self.process_unsupported_component_model(section.range())
            }
            Payload::ComponentSection { parser: _, range } => {
                self.process_unsupported_component_model(range)
            }
            Payload::ComponentInstanceSection(section) => {
                self.process_unsupported_component_model(section.range())
            }
            Payload::ComponentAliasSection(section) => {
                self.process_unsupported_component_model(section.range())
            }
            Payload::ComponentTypeSection(section) => {
                self.process_unsupported_component_model(section.range())
            }
            Payload::ComponentCanonicalSection(section) => {
                self.process_unsupported_component_model(section.range())
            }
            Payload::ComponentStartSection { start: _, range } => {
                self.process_unsupported_component_model(range)
            }
            Payload::ComponentImportSection(section) => {
                self.process_unsupported_component_model(section.range())
            }
            Payload::ComponentExportSection(section) => {
                self.process_unsupported_component_model(section.range())
            }
            Payload::End(offset) => {
                self.process_end(offset)?;
                return Ok(true);
            }
        }?;
        Ok(false)
    }

    /// Validates the Wasm version section.
    fn process_version(
        &mut self,
        num: u16,
        encoding: Encoding,
        range: Range<usize>,
    ) -> Result<(), CompilationError> {
        self.validator
            .version(num, encoding, &range)
            .map_err(Into::into)
    }

    /// Processes the Wasm type section.
    ///
    /// # Note
    ///
    /// This extracts all function types into the [`Module`] under construction.
    ///
    /// # Errors
    ///
    /// If an unsupported function type is encountered.
    fn process_types(&mut self, section: TypeSectionReader) -> Result<(), CompilationError> {
        self.validator.type_section(&section)?;
        for func_type in section.into_iter() {
            let Type::Func(func_type) = func_type?;
            self.allocations
                .translation
                .func_type_registry
                .alloc_func_type(func_type)?;
        }
        Ok(())
    }

    /// Processes the Wasm import section.
    ///
    /// # Note
    ///
    /// This extracts all imports into the [`Module`] under construction.
    ///
    /// # Errors
    ///
    /// - If an import fails to validate.
    /// - If an unsupported import declaration is encountered.
    fn process_imports(&mut self, section: ImportSectionReader) -> Result<(), CompilationError> {
        self.validator.import_section(&section)?;
        for import in section.into_iter() {
            let import = import?;
            let func_type_index = match import.ty {
                TypeRef::Func(func_type_index) => func_type_index,
                TypeRef::Global(global_type) => {
                    let Some(default_value) = self.config.default_imported_global_value else {
                        return Err(CompilationError::NotSupportedImportType);
                    };
                    let global_index = self.allocations.translation.globals.len() as u32;
                    let global_variable = GlobalVariable::new(global_type, default_value);
                    self.allocations
                        .translation
                        .segment_builder
                        .add_global_variable(global_index, &global_variable)?;
                    self.allocations.translation.globals.push(global_variable);
                    continue;
                }
                _ => return Err(CompilationError::NotSupportedImportType),
            };
            let import_name = ImportName::new(import.module, import.name);
            let Some(import_linker) = self.config.import_linker.as_ref() else {
                // Do we need to process imports if there is no import linker?
                return Err(CompilationError::UnresolvedImportFunction);
            };
            let import_linker_entity = import_linker
                .resolve_by_import_name(&import_name)
                .cloned()
                .ok_or(CompilationError::UnresolvedImportFunction)?;
            // verify an imported function type
            let func_type = self
                .allocations
                .translation
                .func_type_registry
                .resolve_original_func_type(func_type_index);
            if !import_linker_entity.matches_func_type(func_type) {
                return Err(CompilationError::MalformedImportFunctionType);
            }
            // don't allow funcref/externref in imported functions
            if !self.config.allow_func_ref_function_types {
                for x in func_type.params().iter().chain(func_type.results()) {
                    if x == &ValType::FuncRef || x == &ValType::ExternRef {
                        return Err(CompilationError::MalformedImportFunctionType);
                    }
                }
            }
            // inject an import function trampoline to support reffunc
            let func_idx = self.next_func();
            self.allocations
                .translation
                .compiled_funcs
                .push(func_type_index);

            if let Some(intrinsic) = import_linker_entity.intrinsic {
                self.allocations
                    .translation
                    .intrinsic_handler
                    .intrinsics
                    .push((func_idx, intrinsic));
            }

            let allocations = take(&mut self.allocations);
            let mut translator = InstructionTranslator::new(
                allocations.translation,
                self.config.consume_fuel,
                self.config.code_snippets,
                self.config.consume_fuel_for_params_and_locals,
                self.config.max_allowed_memory_pages,
            );
            translator.prepare(func_idx)?;
            let signature_index = translator
                .alloc
                .func_type_registry
                .resolve_func_type_signature(func_type_index);
            translator.alloc.instruction_set.op_stack_check(u32::MAX);

            if self.config.builtins_consume_fuel {
                compile_block_params(
                    &mut translator.alloc.instruction_set,
                    import_linker_entity.syscall_fuel_param,
                )
            }

            translator
                .alloc
                .instruction_set
                .op_call(import_linker_entity.sys_func_idx);
            translator.alloc.instruction_set.op_return();
            translator.finish()?;
            let _ = replace(
                &mut self.allocations,
                ReusableAllocations {
                    translation: take(&mut translator.alloc),
                    validation: allocations.validation,
                },
            );
        }
        Ok(())
    }

    /// Process module instances.
    ///
    /// # Note
    ///
    /// This is part of the module linking a Wasm proposal and not yet supported
    /// by `rwasm`.
    fn process_instances(
        &mut self,
        section: wasmparser::InstanceSectionReader,
    ) -> Result<(), CompilationError> {
        self.validator
            .instance_section(&section)
            .map_err(Into::into)
    }

    /// Process module function declarations.
    ///
    /// # Note
    ///
    /// This extracts all function declarations into the [`Module`] under construction.
    ///
    /// # Errors
    ///
    /// If a function declaration fails to validate.
    fn process_functions(
        &mut self,
        section: FunctionSectionReader,
    ) -> Result<(), CompilationError> {
        self.validator.function_section(&section)?;
        for func_type_index in section.into_iter() {
            let func_type_index = func_type_index?;
            self.allocations
                .translation
                .compiled_funcs
                .push(func_type_index);
        }
        Ok(())
    }

    /// Process module table declarations.
    ///
    /// # Note
    ///
    /// This extracts all table declarations into the [`Module`] under construction.
    ///
    /// # Errors
    ///
    /// If a table declaration fails to validate.
    fn process_tables(&mut self, section: TableSectionReader) -> Result<(), CompilationError> {
        self.validator.table_section(&section)?;
        for (table_idx, table_type) in section.into_iter().enumerate() {
            let table_type = table_type?;
            let table_idx = TableIdx::try_from(table_idx).unwrap();
            self.allocations
                .translation
                .segment_builder
                .emit_table_segment(table_idx, &table_type)?;
            self.allocations.translation.tables.push(table_type);
        }
        Ok(())
    }

    /// Process module linear memory declarations.
    ///
    /// # Note
    ///
    /// This extracts all linear memory declarations into the [`Module`] under construction.
    ///
    /// # Errors
    ///
    /// If a linear memory declaration fails to validate.
    fn process_memories(&mut self, section: MemorySectionReader) -> Result<(), CompilationError> {
        self.validator.memory_section(&section)?;
        for memory_type in section.into_iter() {
            let memory_type = memory_type?;
            self.allocations.translation.memories.push(memory_type);
            let initial_memory =
                u32::try_from(memory_type.initial).expect("memory initial size too large");
            self.allocations
                .translation
                .segment_builder
                .add_memory_pages(initial_memory, self.config.max_allowed_memory_pages)?;
        }
        Ok(())
    }

    /// Process module tags.
    ///
    /// # Note
    ///
    /// This is part of the module linking a Wasm proposal and not yet supported
    /// by `rwasm`.
    fn process_tags(
        &mut self,
        section: wasmparser::TagSectionReader,
    ) -> Result<(), CompilationError> {
        self.validator.tag_section(&section).map_err(Into::into)
    }

    /// Process module global variable declarations.
    ///
    /// # Note
    ///
    /// This extracts all global variable declarations into the [`Module`] under construction.
    ///
    /// # Errors
    ///
    /// If a global variable declaration fails to validate.
    fn process_globals(&mut self, section: GlobalSectionReader) -> Result<(), CompilationError> {
        self.validator.global_section(&section)?;
        for global in section.into_iter() {
            let global = global?;
            let init_expr = CompiledExpr::new(global.init_expr);
            let default_value = self.eval_const(init_expr)?;
            let global_variable = GlobalVariable::new(global.ty, default_value);
            let global_idx = GlobalIdx::from(self.allocations.translation.globals.len() as u32);
            self.allocations
                .translation
                .segment_builder
                .add_global_variable(global_idx, &global_variable)?;
            self.allocations.translation.globals.push(global_variable);
        }
        Ok(())
    }

    /// Process module export declarations.
    ///
    /// # Note
    ///
    /// This extracts all export declarations into the [`Module`] under construction.
    ///
    /// # Errors
    ///
    /// If an export declaration fails to validate.
    fn process_exports(&mut self, section: ExportSectionReader) -> Result<(), CompilationError> {
        self.validator.export_section(&section)?;
        for export in section.into_iter() {
            let export = export?;
            // #[cfg(feature = "debug-print")]
            // println!("export: func_idx={} {}", export.index, export.name);
            if export.kind == ExternalKind::Func {
                let function_name: Box<str> = export.name.into();
                self.allocations
                    .translation
                    .exported_funcs
                    .insert(function_name, FuncIdx::from(export.index));
            }
        }
        Ok(())
    }

    /// Process module start section.
    ///
    /// # Note
    ///
    /// This sets the start function for the [`Module`] under construction.
    ///
    /// # Errors
    ///
    /// If the start function declaration fails to validate.
    fn process_start(&mut self, func: u32, range: Range<usize>) -> Result<(), CompilationError> {
        self.validator.start_section(func, &range)?;
        self.allocations.translation.start_func = Some(FuncIdx::from(func));
        Ok(())
    }

    /// Process module table element segments.
    ///
    /// # Note
    ///
    /// This extracts all table element segments into the [`Module`] under construction.
    ///
    /// # Errors
    ///
    /// If any of the table element segments fail to validate.
    fn process_element(&mut self, section: ElementSectionReader) -> Result<(), CompilationError> {
        self.validator.element_section(&section)?;
        for (element_segment_idx, element) in section.into_iter().enumerate() {
            let element = element?;
            let element_segment_idx = ElementSegmentIdx::from(element_segment_idx as u32);

            let element_items_vec = match element.items {
                ElementItems::Expressions(section) => section
                    .into_iter()
                    .map(|v| {
                        let compiled_expr = CompiledExpr::new(v?);
                        compiled_expr
                            .funcref()
                            .map(|v| v + 1)
                            .or_else(|| compiled_expr.eval_const().map(|v| v as i32 as u32))
                            .ok_or(CompilationError::ConstEvaluationFailed)
                    })
                    .collect::<Result<Vec<_>, _>>()?,
                ElementItems::Functions(section) => section
                    .into_iter()
                    .map(|v| v.map(|v| v + 1).map_err(CompilationError::from))
                    .collect::<Result<Vec<_>, _>>()?,
            };

            match element.kind {
                ElementKind::Active {
                    table_index,
                    offset_expr,
                } => {
                    let compiled_expr = CompiledExpr::new(offset_expr);
                    // We can fail-fast here because we already that know that there an overflow
                    let element_offset = u32::try_from(self.eval_const(compiled_expr)?)
                        .map_err(|_| CompilationError::TableOutOfBounds)?;
                    let table_idx = TableIdx::try_from(table_index).unwrap();
                    self.allocations
                        .translation
                        .segment_builder
                        .add_active_elements(
                            element_segment_idx,
                            element_offset,
                            table_idx,
                            element_items_vec,
                        );
                }
                ElementKind::Passive => self
                    .allocations
                    .translation
                    .segment_builder
                    .add_passive_elements(element_segment_idx, element_items_vec),
                ElementKind::Declared => self
                    .allocations
                    .translation
                    .segment_builder
                    .add_passive_elements(element_segment_idx, []),
            };
        }
        Ok(())
    }

    /// Process module data count section.
    ///
    /// # Note
    ///
    /// This is part of the bulk memory operations Wasm proposal and not yet supported
    /// by `rwasm`.
    fn process_data_count(
        &mut self,
        count: u32,
        range: Range<usize>,
    ) -> Result<(), CompilationError> {
        self.validator
            .data_count_section(count, &range)
            .map_err(Into::into)
    }

    /// Process module linear memory data segments.
    ///
    /// # Note
    ///
    /// This extracts all table elements into the [`Module`] under construction.
    ///
    /// # Errors
    ///
    /// If any of the table elements fail to validate.
    fn process_data(&mut self, section: DataSectionReader) -> Result<(), CompilationError> {
        self.validator.data_section(&section)?;
        for (data_segment_idx, data) in section.into_iter().enumerate() {
            let data = data?;
            let data_segment_idx = DataSegmentIdx::from(data_segment_idx as u32);
            match data.kind {
                DataKind::Active {
                    memory_index,
                    offset_expr,
                } => {
                    if memory_index != DEFAULT_MEMORY_INDEX {
                        return Err(CompilationError::NonDefaultMemoryIndex);
                    }
                    let compiled_expr = CompiledExpr::new(offset_expr);
                    // We can fail-fast here because we already that know that there an overflow
                    let data_offset = u32::try_from(self.eval_const(compiled_expr)?)
                        .map_err(|_| CompilationError::MemoryOutOfBounds)?;
                    self.allocations
                        .translation
                        .segment_builder
                        .add_active_memory(data_segment_idx, data_offset, data.data);
                }
                DataKind::Passive => self
                    .allocations
                    .translation
                    .segment_builder
                    .add_passive_memory(data_segment_idx, data.data),
            };
        }
        Ok(())
    }

    fn eval_const(&self, compiled_expr: CompiledExpr) -> Result<i64, CompilationError> {
        compiled_expr
            .eval_with_context(
                |global_index| {
                    self.allocations
                        .translation
                        .globals
                        .get(global_index as usize)
                        .and_then(GlobalVariable::value)
                },
                |function_index| Some(FuncRef::new(function_index + 1)),
            )
            .ok_or(CompilationError::ConstEvaluationFailed)
    }

    fn process_custom_section(
        &mut self,
        reader: CustomSectionReader,
    ) -> Result<(), CompilationError> {
        self.allocations
            .translation
            .constructor_params
            .try_parse(reader);
        Ok(())
    }

    /// Process module code section start.
    ///
    /// # Note
    ///
    /// This currently does not do a lot, but it might become important in the
    /// future if we add parallel translation of function bodies to prepare for
    /// the translation.
    ///
    /// # Errors
    ///
    /// If the code start section fails to validate.
    fn process_code_start(
        &mut self,
        count: u32,
        range: Range<usize>,
    ) -> Result<(), CompilationError> {
        self.validator.code_section_start(count, &range)?;
        Ok(())
    }

    /// Returns the next `FuncIdx` for processing of its function body.
    fn next_func(&mut self) -> FuncIdx {
        let compiled_func = self.compiled_funcs;
        self.compiled_funcs += 1;
        FuncIdx::from(compiled_func)
    }

    /// Process a single module code section entry.
    ///
    /// # Note
    ///
    /// This contains the local variables and Wasm instructions of
    /// a single function body.
    /// This procedure is translating the Wasm bytecode into `rwasm` bytecode.
    ///
    /// # Errors
    ///
    /// If the function body fails to validate.
    fn process_code_entry(&mut self, func_body: FunctionBody) -> Result<(), CompilationError> {
        let func_idx = self.next_func();
        // #[cfg(feature = "debug-print")]
        // println!("\nfunc_idx={}", func_idx);
        let allocations = take(&mut self.allocations);
        let validator = self.validator.code_section_entry(&func_body)?;
        let func_validator = validator.into_validator(allocations.validation);
        let allocations = FuncBuilder::new(
            func_body,
            func_validator,
            func_idx,
            allocations.translation,
            self.config.consume_fuel,
            self.config.code_snippets,
            self.config.consume_fuel_for_params_and_locals,
            self.config.max_allowed_memory_pages,
        )
        .translate()?;
        let _ = replace(&mut self.allocations, allocations);
        Ok(())
    }

    /// Process an unknown Wasm module section.
    ///
    /// # Note
    ///
    /// This generally will be treated as an error for now.
    fn process_unknown(&mut self, id: u8, range: Range<usize>) -> Result<(), CompilationError> {
        self.validator
            .unknown_section(id, &range)
            .map_err(Into::into)
    }

    /// Process the entries for the Wasm component model proposal.
    fn process_unsupported_component_model(
        &mut self,
        range: Range<usize>,
    ) -> Result<(), CompilationError> {
        panic!(
            "rwasm does not support the `component-model` Wasm proposal: bytes[{}..{}]",
            range.start, range.end
        )
    }

    /// Processes the end of the Wasm binary.
    fn process_end(&mut self, offset: usize) -> Result<(), CompilationError> {
        self.validator.end(offset)?;
        Ok(())
    }
}