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
// This file contains code from external sources.
// Attributions: https://github.com/wasmerio/wasmer/blob/master/ATTRIBUTIONS.md

//! Translation skeleton that traverses the whole WebAssembly module and call helper functions
//! to deal with each part of it.
use super::environ::ModuleEnvironment;
use super::sections::{
    parse_data_section, parse_element_section, parse_export_section, parse_function_section,
    parse_global_section, parse_import_section, parse_memory_section, parse_name_section,
    parse_start_section, parse_table_section, parse_type_section,
};
use super::state::ModuleTranslationState;
use crate::WasmResult;
use wasmparser::{NameSectionReader, Parser, Payload};

/// Translate a sequence of bytes forming a valid Wasm binary into a
/// parsed ModuleInfo `ModuleTranslationState`.
#[tracing::instrument(target = "unc_vm", level = "trace", skip_all)]
pub fn translate_module<'data>(
    data: &'data [u8],
    environ: &mut ModuleEnvironment<'data>,
) -> WasmResult<ModuleTranslationState> {
    let mut module_translation_state = ModuleTranslationState::new();

    for payload in Parser::new(0).parse_all(data) {
        match payload? {
            Payload::Version { .. } | Payload::End(_) => {}

            Payload::TypeSection(types) => {
                parse_type_section(types, &mut module_translation_state, environ)?;
            }

            Payload::ImportSection(imports) => {
                parse_import_section(imports, environ)?;
            }

            Payload::FunctionSection(functions) => {
                parse_function_section(functions, environ)?;
            }

            Payload::TableSection(tables) => {
                parse_table_section(tables, environ)?;
            }

            Payload::MemorySection(memories) => {
                parse_memory_section(memories, environ)?;
            }

            Payload::GlobalSection(globals) => {
                parse_global_section(globals, environ)?;
            }

            Payload::ExportSection(exports) => {
                parse_export_section(exports, environ)?;
            }

            Payload::StartSection { func, .. } => {
                parse_start_section(func, environ)?;
            }

            Payload::ElementSection(elements) => {
                parse_element_section(elements, environ)?;
            }

            Payload::CodeSectionStart { .. } => {}
            Payload::CodeSectionEntry(code) => {
                let mut code = code.get_binary_reader();
                let size = code.bytes_remaining();
                let offset = code.original_position();
                environ.define_function_body(
                    &module_translation_state,
                    code.read_bytes(size)?,
                    offset,
                )?;
            }

            Payload::DataSection(data) => {
                parse_data_section(data, environ)?;
            }

            Payload::DataCountSection { count, .. } => {
                environ.reserve_passive_data(count)?;
            }

            Payload::InstanceSection(_) => {
                unimplemented!("module linking not implemented yet")
            }

            Payload::TagSection(_) => {
                unimplemented!("exception handling proposal is not implemented yet")
            }

            Payload::CustomSection(reader) => {
                if reader.name() == "name" {
                    parse_name_section(
                        NameSectionReader::new(reader.data(), reader.data_offset()),
                        environ,
                    )?;
                } else {
                    environ.custom_section(reader.name(), reader.data())?;
                }
            }

            Payload::ModuleSection { .. } => unimplemented!("module sections not supported yet"), // which proposal is this coming from?
            Payload::CoreTypeSection { .. } => {
                unimplemented!("component proposal is not implemented yet")
            }
            Payload::ComponentSection { .. } => {
                unimplemented!("component proposal is not implemented yet")
            }
            Payload::ComponentInstanceSection { .. } => {
                unimplemented!("component proposal is not implemented yet")
            }
            Payload::ComponentAliasSection { .. } => {
                unimplemented!("component proposal is not implemented yet")
            }
            Payload::ComponentTypeSection { .. } => {
                unimplemented!("component proposal is not implemented yet")
            }
            Payload::ComponentCanonicalSection { .. } => {
                unimplemented!("component proposal is not implemented yet")
            }
            Payload::ComponentStartSection { .. } => {
                unimplemented!("component proposal is not implemented yet")
            }
            Payload::ComponentImportSection { .. } => {
                unimplemented!("component proposal is not implemented yet")
            }
            Payload::ComponentExportSection { .. } => {
                unimplemented!("component proposal is not implemented yet")
            }

            Payload::UnknownSection { .. } => unreachable!(),
        }
    }

    module_translation_state.build_import_map(&environ.module);

    Ok(module_translation_state)
}