wasmer_compiler/translator/
module.rs

1// This file contains code from external sources.
2// Attributions: https://github.com/wasmerio/wasmer/blob/main/docs/ATTRIBUTIONS.md
3
4//! Translation skeleton that traverses the whole WebAssembly module and call helper functions
5//! to deal with each part of it.
6use super::environ::ModuleEnvironment;
7use super::error::from_binaryreadererror_wasmerror;
8use super::sections::{
9    parse_data_section, parse_element_section, parse_export_section, parse_function_section,
10    parse_global_section, parse_import_section, parse_memory_section, parse_name_section,
11    parse_start_section, parse_table_section, parse_tag_section, parse_type_section,
12};
13use super::state::ModuleTranslationState;
14use wasmer_types::{WasmError, WasmResult};
15use wasmparser::{BinaryReader, NameSectionReader, Parser, Payload};
16
17/// Translate a sequence of bytes forming a valid Wasm binary into a
18/// parsed ModuleInfo `ModuleTranslationState`.
19pub fn translate_module<'data>(
20    data: &'data [u8],
21    environ: &mut ModuleEnvironment<'data>,
22) -> WasmResult<ModuleTranslationState> {
23    let mut module_translation_state = ModuleTranslationState::new();
24
25    for payload in Parser::new(0).parse_all(data) {
26        match payload.map_err(from_binaryreadererror_wasmerror)? {
27            Payload::Version { .. } | Payload::End { .. } => {}
28
29            Payload::TypeSection(types) => {
30                parse_type_section(types, &mut module_translation_state, environ)?;
31            }
32
33            Payload::ImportSection(imports) => {
34                parse_import_section(imports, environ)?;
35            }
36
37            Payload::FunctionSection(functions) => {
38                parse_function_section(functions, environ)?;
39            }
40
41            Payload::TableSection(tables) => {
42                parse_table_section(tables, environ)?;
43            }
44
45            Payload::MemorySection(memories) => {
46                parse_memory_section(memories, environ)?;
47            }
48
49            Payload::GlobalSection(globals) => {
50                parse_global_section(globals, environ)?;
51            }
52
53            Payload::ExportSection(exports) => {
54                parse_export_section(exports, environ)?;
55            }
56
57            Payload::StartSection { func, .. } => {
58                parse_start_section(func, environ)?;
59            }
60
61            Payload::ElementSection(elements) => {
62                parse_element_section(elements, environ)?;
63            }
64
65            Payload::CodeSectionStart { .. } => {}
66            Payload::CodeSectionEntry(code) => {
67                let mut code = code.get_binary_reader();
68                let size = code.bytes_remaining();
69                let offset = code.original_position();
70                environ.define_function_body(
71                    &module_translation_state,
72                    code.read_bytes(size)
73                        .map_err(from_binaryreadererror_wasmerror)?,
74                    offset,
75                )?;
76            }
77
78            Payload::DataSection(data) => {
79                parse_data_section(data, environ)?;
80            }
81
82            Payload::DataCountSection { count, .. } => {
83                environ.reserve_passive_data(count)?;
84            }
85
86            Payload::TagSection(t) => parse_tag_section(t, environ)?,
87
88            Payload::CustomSection(sectionreader) => {
89                // We still add the custom section data, but also read it as name section reader
90                let name = sectionreader.name();
91                environ.custom_section(name, sectionreader.data())?;
92                if name == "name" {
93                    parse_name_section(
94                        NameSectionReader::new(BinaryReader::new(
95                            sectionreader.data(),
96                            sectionreader.data_offset(),
97                        )),
98                        environ,
99                    )?;
100                }
101            }
102
103            Payload::UnknownSection { .. } => unreachable!(),
104            k => {
105                return Err(WasmError::Unsupported(format!(
106                    "Unsupported paylod kind: {k:?}"
107                )));
108            }
109        }
110    }
111
112    Ok(module_translation_state)
113}