multiversx_sc_meta_lib/tools/
wasm_extractor.rs

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
use colored::Colorize;
use std::{
    collections::{HashMap, HashSet},
    fs,
};
use wasmparser::{
    BinaryReaderError, DataSectionReader, ExportSectionReader, FunctionBody, ImportSectionReader,
    Operator, Parser, Payload,
};

use crate::ei::EIVersion;

use super::report_creator::ReportCreator;

type CallGraph = HashMap<usize, HashSet<usize>>;

const ERROR_FAIL_ALLOCATOR: &[u8; 27] = b"memory allocation forbidden";
const WRITE_OP: &[&str] = &[
    "mBufferStorageStore",
    "storageStore",
    "int64storageStore",
    "bigIntStorageStoreUnsigned",
    "smallIntStorageStoreUnsigned",
    "smallIntStorageStoreSigned",
];

#[derive(Default)]
pub struct WasmInfo {
    pub imports: Vec<String>,
    pub ei_check: bool,
    pub memory_grow_flag: bool,
    pub report: ReportCreator,
    pub call_graph: CallGraph,
    pub write_index_functions: HashSet<usize>,
    pub view_endpoints: HashMap<String, usize>,
}

impl WasmInfo {
    pub fn extract_wasm_info(
        output_wasm_path: &str,
        extract_imports_enabled: bool,
        check_ei: &Option<EIVersion>,
        view_endpoints: Vec<&str>,
    ) -> WasmInfo {
        let wasm_data = fs::read(output_wasm_path)
            .expect("error occured while extracting information from .wasm: file not found");

        let wasm_info = populate_wasm_info(
            output_wasm_path.to_string(),
            wasm_data,
            extract_imports_enabled,
            check_ei,
            view_endpoints,
        );

        wasm_info.expect("error occured while extracting information from .wasm file")
    }

    fn create_call_graph(&mut self, body: FunctionBody) {
        let mut instructions_reader = body
            .get_operators_reader()
            .expect("Failed to get operators reader");

        let mut call_functions = HashSet::new();
        while let Ok(op) = instructions_reader.read() {
            if let Operator::Call { function_index } = op {
                let function_usize: usize = function_index.try_into().unwrap();
                call_functions.insert(function_usize);
            }
        }

        self.call_graph
            .insert(self.call_graph.len(), call_functions);
    }

    pub fn process_imports(
        &mut self,
        import_section: ImportSectionReader,
        import_extraction_enabled: bool,
    ) {
        for (index, import) in import_section.into_iter().flatten().enumerate() {
            if import_extraction_enabled {
                self.imports.push(import.name.to_string());
            }
            self.call_graph.insert(index, HashSet::new());
            if WRITE_OP.contains(&import.name) {
                self.write_index_functions.insert(index);
            }
        }

        self.imports.sort();
    }

    pub fn detect_write_operations_in_views(&mut self) {
        let mut visited: HashSet<usize> = HashSet::new();
        for index in self.view_endpoints.values() {
            mark_write(
                *index,
                &self.call_graph,
                &mut self.write_index_functions,
                &mut visited,
            );
        }

        for (name, index) in &self.view_endpoints {
            if self.write_index_functions.contains(index) {
                println!(
                    "{} {}",
                    "Write storage operation in VIEW endpoint:"
                        .to_string()
                        .red()
                        .bold(),
                    name.red().bold()
                );
            }
        }
    }

    fn parse_export_section(
        &mut self,
        export_section: ExportSectionReader,
        view_endpoints: &[&str],
    ) {
        for export in export_section {
            let export = export.expect("Failed to read export section");
            if let wasmparser::ExternalKind::Func = export.kind {
                if view_endpoints.contains(&export.name) {
                    self.view_endpoints
                        .insert(export.name.to_owned(), export.index.try_into().unwrap());
                }
            }
        }
    }
}

pub(crate) fn populate_wasm_info(
    path: String,
    wasm_data: Vec<u8>,
    import_extraction_enabled: bool,
    check_ei: &Option<EIVersion>,
    view_endpoints: Vec<&str>,
) -> Result<WasmInfo, BinaryReaderError> {
    let mut wasm_info = WasmInfo::default();

    let parser = Parser::new(0);
    for payload in parser.parse_all(&wasm_data) {
        match payload? {
            Payload::ImportSection(import_section) => {
                wasm_info.process_imports(import_section, import_extraction_enabled);
                wasm_info.ei_check |= is_ei_valid(&wasm_info.imports, check_ei);
            },
            Payload::DataSection(data_section) => {
                wasm_info.report.has_allocator |= is_fail_allocator_triggered(data_section.clone());
                wasm_info.report.has_panic.max_severity(data_section);
            },
            Payload::CodeSectionEntry(code_section) => {
                wasm_info.memory_grow_flag |= is_mem_grow(&code_section);
                wasm_info.create_call_graph(code_section);
            },
            Payload::ExportSection(export_section) => {
                wasm_info.parse_export_section(export_section, &view_endpoints);
            },
            _ => (),
        }
    }

    wasm_info.detect_write_operations_in_views();

    let report = ReportCreator {
        path,
        has_allocator: wasm_info.report.has_allocator,
        has_panic: wasm_info.report.has_panic,
    };

    Ok(WasmInfo {
        imports: wasm_info.imports,
        ei_check: wasm_info.ei_check,
        memory_grow_flag: wasm_info.memory_grow_flag,
        call_graph: wasm_info.call_graph,
        report,
        write_index_functions: wasm_info.write_index_functions,
        view_endpoints: wasm_info.view_endpoints,
    })
}

fn is_fail_allocator_triggered(data_section: DataSectionReader) -> bool {
    for data_fragment in data_section.into_iter().flatten() {
        if data_fragment
            .data
            .windows(ERROR_FAIL_ALLOCATOR.len())
            .any(|data| data == ERROR_FAIL_ALLOCATOR)
        {
            println!(
                "{}",
                "FailAllocator used while memory allocation is accessible in code. Contract may fail unexpectedly when memory allocation is attempted"
                    .to_string()
                    .red()
                    .bold()
            );
            return true;
        }
    }

    false
}

fn mark_write(
    func: usize,
    call_graph: &CallGraph,
    write_functions: &mut HashSet<usize>,
    visited: &mut HashSet<usize>,
) {
    // Return early to prevent cycles.
    if visited.contains(&func) {
        return;
    }

    visited.insert(func);

    if let Some(callees) = call_graph.get(&func) {
        for &callee in callees {
            if write_functions.contains(&callee) {
                write_functions.insert(func);
            } else {
                mark_write(callee, call_graph, write_functions, visited);
                if write_functions.contains(&callee) {
                    write_functions.insert(func);
                }
            }
        }
    }
}

fn is_ei_valid(imports: &[String], check_ei: &Option<EIVersion>) -> bool {
    if let Some(ei) = check_ei {
        let mut num_errors = 0;
        for import in imports {
            if !ei.contains_vm_hook(import.as_str()) {
                num_errors += 1;
            }
        }

        if num_errors == 0 {
            return true;
        }
    }

    false
}

fn is_mem_grow(code_section: &FunctionBody) -> bool {
    let mut instructions_reader = code_section
        .get_operators_reader()
        .expect("Failed to get operators reader");

    while let Ok(op) = instructions_reader.read() {
        if let Operator::MemoryGrow { mem: _ } = op {
            return true;
        }
    }

    false
}