|
| 1 | +/* |
| 2 | +# Copyright 2022 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +################################################################################ |
| 17 | +*/ |
| 18 | +#![no_main] |
| 19 | + |
| 20 | +use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target}; |
| 21 | +use std::sync::Arc; |
| 22 | +use wasm_smith::{Config, ConfiguredModule}; |
| 23 | +use wasmer::wasmparser::Operator; |
| 24 | +use wasmer::{imports, CompilerConfig, Instance, Module, Store}; |
| 25 | +use wasmer_compiler_cranelift::Cranelift; |
| 26 | +use wasmer_middlewares::Metering; |
| 27 | + |
| 28 | +#[derive(Arbitrary, Debug, Default, Copy, Clone)] |
| 29 | +struct NoImportsConfig; |
| 30 | +impl Config for NoImportsConfig { |
| 31 | + fn max_imports(&self) -> usize { |
| 32 | + 0 |
| 33 | + } |
| 34 | + fn max_memory_pages(&self) -> u32 { |
| 35 | + // https://github.com/wasmerio/wasmer/issues/2187 |
| 36 | + 65535 |
| 37 | + } |
| 38 | + fn allow_start_export(&self) -> bool { |
| 39 | + false |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Arbitrary)] |
| 44 | +struct WasmSmithModule(ConfiguredModule<NoImportsConfig>); |
| 45 | +impl std::fmt::Debug for WasmSmithModule { |
| 46 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 47 | + f.write_str(&wasmprinter::print_bytes(self.0.to_bytes()).unwrap()) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +fn cost(operator: &Operator) -> u64 { |
| 52 | + match operator { |
| 53 | + Operator::LocalGet { .. } | Operator::I32Const { .. } => 1, |
| 54 | + Operator::I32Add { .. } => 2, |
| 55 | + _ => 0, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +fuzz_target!(|module: WasmSmithModule| { |
| 60 | + let wasm_bytes = module.0.to_bytes(); |
| 61 | + |
| 62 | + if let Ok(path) = std::env::var("DUMP_TESTCASE") { |
| 63 | + use std::fs::File; |
| 64 | + use std::io::Write; |
| 65 | + let mut file = File::create(path).unwrap(); |
| 66 | + file.write_all(&wasm_bytes).unwrap(); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + let mut compiler = Cranelift::default(); |
| 71 | + compiler.canonicalize_nans(true); |
| 72 | + compiler.enable_verifier(); |
| 73 | + let metering = Arc::new(Metering::new(10, cost)); |
| 74 | + compiler.push_middleware(metering); |
| 75 | + let mut store = Store::new(compiler); |
| 76 | + let module = Module::new(&store, &wasm_bytes).unwrap(); |
| 77 | + match Instance::new(&mut store, &module, &imports! {}) { |
| 78 | + Ok(_) => {} |
| 79 | + Err(e) => { |
| 80 | + let error_message = format!("{}", e); |
| 81 | + if error_message.starts_with("RuntimeError: ") |
| 82 | + && error_message.contains("out of bounds") |
| 83 | + { |
| 84 | + return; |
| 85 | + } |
| 86 | + panic!("{}", e); |
| 87 | + } |
| 88 | + } |
| 89 | +}); |
0 commit comments