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
// TODO: Uncomment
// #![warn(missing_docs)]

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
#[macro_use]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std as alloc;

pub mod stack;

/// Index of default linear memory.
pub const DEFAULT_MEMORY_INDEX: u32 = 0;
/// Index of default table.
pub const DEFAULT_TABLE_INDEX: u32 = 0;

/// Maximal number of pages that a wasm instance supports.
pub const LINEAR_MEMORY_MAX_PAGES: u32 = 65536;

use alloc::{string::String, vec::Vec};
use core::fmt;
#[cfg(feature = "std")]
use std::error;

use self::context::ModuleContextBuilder;
use parity_wasm::elements::{
    BlockType, ExportEntry, External, FuncBody, GlobalEntry, GlobalType, InitExpr, Instruction,
    Internal, MemoryType, Module, ResizableLimits, TableType, Type, ValueType,
};

pub mod context;
pub mod func;
pub mod util;

#[cfg(test)]
mod tests;

// TODO: Consider using a type other than String, because
// of formatting machinary is not welcomed in substrate runtimes.
#[derive(Debug)]
pub struct Error(pub String);

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[cfg(feature = "std")]
impl error::Error for Error {
    fn description(&self) -> &str {
        &self.0
    }
}

impl From<stack::Error> for Error {
    fn from(e: stack::Error) -> Error {
        Error(format!("Stack: {}", e))
    }
}

pub trait Validator {
    type Output;
    type FuncValidator: FuncValidator;
    fn new(module: &Module) -> Self;
    fn on_function_validated(
        &mut self,
        index: u32,
        output: <<Self as Validator>::FuncValidator as FuncValidator>::Output,
    );
    fn finish(self) -> Self::Output;
}

pub trait FuncValidator {
    type Output;
    fn new(ctx: &func::FunctionValidationContext, body: &FuncBody) -> Self;
    fn next_instruction(
        &mut self,
        ctx: &mut func::FunctionValidationContext,
        instruction: &Instruction,
    ) -> Result<(), Error>;
    fn finish(self) -> Self::Output;
}

/// A module validator that just validates modules and produces no result.
pub struct PlainValidator;

impl Validator for PlainValidator {
    type Output = ();
    type FuncValidator = PlainFuncValidator;
    fn new(_module: &Module) -> PlainValidator {
        PlainValidator
    }
    fn on_function_validated(
        &mut self,
        _index: u32,
        _output: <<Self as Validator>::FuncValidator as FuncValidator>::Output,
    ) {
    }
    fn finish(self) {}
}

/// A function validator that just validates modules and produces no result.
pub struct PlainFuncValidator;

impl FuncValidator for PlainFuncValidator {
    type Output = ();

    fn new(_ctx: &func::FunctionValidationContext, _body: &FuncBody) -> PlainFuncValidator {
        PlainFuncValidator
    }

    fn next_instruction(
        &mut self,
        ctx: &mut func::FunctionValidationContext,
        instruction: &Instruction,
    ) -> Result<(), Error> {
        ctx.step(instruction)
    }

    fn finish(self) {}
}

pub fn validate_module<V: Validator>(module: &Module) -> Result<V::Output, Error> {
    let mut context_builder = ModuleContextBuilder::new();
    let mut imported_globals = Vec::new();
    let mut validation = V::new(module);

    // Copy types from module as is.
    context_builder.set_types(
        module
            .type_section()
            .map(|ts| {
                ts.types()
                    .iter()
                    .map(|&Type::Function(ref ty)| ty)
                    .cloned()
                    .collect()
            })
            .unwrap_or_default(),
    );

    // Fill elements with imported values.
    for import_entry in module
        .import_section()
        .map(|i| i.entries())
        .unwrap_or_default()
    {
        match *import_entry.external() {
            External::Function(idx) => context_builder.push_func_type_index(idx),
            External::Table(ref table) => context_builder.push_table(*table),
            External::Memory(ref memory) => context_builder.push_memory(*memory),
            External::Global(ref global) => {
                context_builder.push_global(*global);
                imported_globals.push(*global);
            }
        }
    }

    // Concatenate elements with defined in the module.
    if let Some(function_section) = module.function_section() {
        for func_entry in function_section.entries() {
            context_builder.push_func_type_index(func_entry.type_ref())
        }
    }
    if let Some(table_section) = module.table_section() {
        for table_entry in table_section.entries() {
            validate_table_type(table_entry)?;
            context_builder.push_table(*table_entry);
        }
    }
    if let Some(mem_section) = module.memory_section() {
        for mem_entry in mem_section.entries() {
            validate_memory_type(mem_entry)?;
            context_builder.push_memory(*mem_entry);
        }
    }
    if let Some(global_section) = module.global_section() {
        for global_entry in global_section.entries() {
            validate_global_entry(global_entry, &imported_globals)?;
            context_builder.push_global(*global_entry.global_type());
        }
    }

    let context = context_builder.build();

    let function_section_len = module
        .function_section()
        .map(|s| s.entries().len())
        .unwrap_or(0);
    let code_section_len = module.code_section().map(|s| s.bodies().len()).unwrap_or(0);
    if function_section_len != code_section_len {
        return Err(Error(format!(
            "length of function section is {}, while len of code section is {}",
            function_section_len, code_section_len
        )));
    }

    // validate every function body in user modules
    if function_section_len != 0 {
        // tests use invalid code
        let function_section = module
            .function_section()
            .expect("function_section_len != 0; qed");
        let code_section = module
            .code_section()
            .expect("function_section_len != 0; function_section_len == code_section_len; qed");
        // check every function body
        for (index, function) in function_section.entries().iter().enumerate() {
            let function_body = code_section
                .bodies()
                .get(index as usize)
                .ok_or_else(|| Error(format!("Missing body for function {}", index)))?;

            let output = func::drive::<V::FuncValidator>(&context, function, function_body)
                .map_err(|Error(ref msg)| {
                    Error(format!(
                        "Function #{} reading/validation error: {}",
                        index, msg
                    ))
                })?;
            validation.on_function_validated(index as u32, output);
        }
    }

    // validate start section
    if let Some(start_fn_idx) = module.start_section() {
        let (params, return_ty) = context.require_function(start_fn_idx)?;
        if return_ty != BlockType::NoResult || !params.is_empty() {
            return Err(Error(
                "start function expected to have type [] -> []".into(),
            ));
        }
    }

    // validate export section
    if let Some(export_section) = module.export_section() {
        let mut export_names = export_section
            .entries()
            .iter()
            .map(ExportEntry::field)
            .collect::<Vec<_>>();

        export_names.sort_unstable();

        for (fst, snd) in export_names.iter().zip(export_names.iter().skip(1)) {
            if fst == snd {
                return Err(Error(format!("duplicate export {}", fst)));
            }
        }

        for export in export_section.entries() {
            match *export.internal() {
                Internal::Function(function_index) => {
                    context.require_function(function_index)?;
                }
                Internal::Global(global_index) => {
                    context.require_global(global_index, None)?;
                }
                Internal::Memory(memory_index) => {
                    context.require_memory(memory_index)?;
                }
                Internal::Table(table_index) => {
                    context.require_table(table_index)?;
                }
            }
        }
    }

    // validate import section
    if let Some(import_section) = module.import_section() {
        for import in import_section.entries() {
            match *import.external() {
                External::Function(function_type_index) => {
                    context.require_function_type(function_type_index)?;
                }
                External::Global(_) => {}
                External::Memory(ref memory_type) => {
                    validate_memory_type(memory_type)?;
                }
                External::Table(ref table_type) => {
                    validate_table_type(table_type)?;
                }
            }
        }
    }

    // there must be no greater than 1 table in tables index space
    if context.tables().len() > 1 {
        return Err(Error(format!(
            "too many tables in index space: {}",
            context.tables().len()
        )));
    }

    // there must be no greater than 1 linear memory in memory index space
    if context.memories().len() > 1 {
        return Err(Error(format!(
            "too many memory regions in index space: {}",
            context.memories().len()
        )));
    }

    // use data section to initialize linear memory regions
    if let Some(data_section) = module.data_section() {
        for data_segment in data_section.entries() {
            context.require_memory(data_segment.index())?;
            let offset = data_segment
                .offset()
                .as_ref()
                .ok_or_else(|| Error("passive memory segments are not supported".into()))?;
            let init_ty = expr_const_type(offset, context.globals())?;
            if init_ty != ValueType::I32 {
                return Err(Error("segment offset should return I32".into()));
            }
        }
    }

    // use element section to fill tables
    if let Some(element_section) = module.elements_section() {
        for element_segment in element_section.entries() {
            context.require_table(element_segment.index())?;
            let offset = element_segment
                .offset()
                .as_ref()
                .ok_or_else(|| Error("passive element segments are not supported".into()))?;
            let init_ty = expr_const_type(offset, context.globals())?;
            if init_ty != ValueType::I32 {
                return Err(Error("segment offset should return I32".into()));
            }

            for function_index in element_segment.members() {
                context.require_function(*function_index)?;
            }
        }
    }

    Ok(validation.finish())
}

fn validate_limits(limits: &ResizableLimits) -> Result<(), Error> {
    if let Some(maximum) = limits.maximum() {
        if limits.initial() > maximum {
            return Err(Error(format!(
                "maximum limit {} is less than minimum {}",
                maximum,
                limits.initial()
            )));
        }
    }
    Ok(())
}

fn validate_memory_type(memory_type: &MemoryType) -> Result<(), Error> {
    let initial = memory_type.limits().initial();
    let maximum: Option<u32> = memory_type.limits().maximum();
    validate_memory(initial, maximum).map_err(Error)
}

pub fn validate_memory(initial: u32, maximum: Option<u32>) -> Result<(), String> {
    if initial > LINEAR_MEMORY_MAX_PAGES {
        return Err(format!(
            "initial memory size must be at most {} pages",
            LINEAR_MEMORY_MAX_PAGES
        ));
    }
    if let Some(maximum) = maximum {
        if initial > maximum {
            return Err(format!(
                "maximum limit {} is less than minimum {}",
                maximum, initial,
            ));
        }

        if maximum > LINEAR_MEMORY_MAX_PAGES {
            return Err(format!(
                "maximum memory size must be at most {} pages",
                LINEAR_MEMORY_MAX_PAGES
            ));
        }
    }
    Ok(())
}

fn validate_table_type(table_type: &TableType) -> Result<(), Error> {
    validate_limits(table_type.limits())
}

fn validate_global_entry(global_entry: &GlobalEntry, globals: &[GlobalType]) -> Result<(), Error> {
    let init = global_entry.init_expr();
    let init_expr_ty = expr_const_type(init, globals)?;
    if init_expr_ty != global_entry.global_type().content_type() {
        return Err(Error(format!(
            "Trying to initialize variable of type {:?} with value of type {:?}",
            global_entry.global_type().content_type(),
            init_expr_ty
        )));
    }
    Ok(())
}

/// Returns type of this constant expression.
fn expr_const_type(init_expr: &InitExpr, globals: &[GlobalType]) -> Result<ValueType, Error> {
    let code = init_expr.code();
    if code.len() != 2 {
        return Err(Error(
            "Init expression should always be with length 2".into(),
        ));
    }
    let expr_ty: ValueType = match code[0] {
        Instruction::I32Const(_) => ValueType::I32,
        Instruction::I64Const(_) => ValueType::I64,
        Instruction::F32Const(_) => ValueType::F32,
        Instruction::F64Const(_) => ValueType::F64,
        Instruction::GetGlobal(idx) => match globals.get(idx as usize) {
            Some(target_global) => {
                if target_global.is_mutable() {
                    return Err(Error(format!("Global {} is mutable", idx)));
                }
                target_global.content_type()
            }
            None => {
                return Err(Error(format!(
                    "Global {} doesn't exists or not yet defined",
                    idx
                )));
            }
        },
        _ => return Err(Error("Non constant opcode in init expr".into())),
    };
    if code[1] != Instruction::End {
        return Err(Error("Expression doesn't ends with `end` opcode".into()));
    }
    Ok(expr_ty)
}