Skip to content

Commit 21cb316

Browse files
committed
add fuzz test
1 parent a53ea7e commit 21cb316

File tree

4 files changed

+509
-0
lines changed

4 files changed

+509
-0
lines changed

packages/vm/fuzz/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target
2+
corpus
3+
artifacts

packages/vm/fuzz/Cargo.toml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[package]
2+
name = "owasm-vm-fuzz"
3+
version = "0.0.0"
4+
authors = ["Automatically generated"]
5+
publish = false
6+
edition = "2018"
7+
8+
[package.metadata]
9+
cargo-fuzz = true
10+
11+
[dependencies]
12+
libfuzzer-sys = "0.4"
13+
tempfile = "3.1.0"
14+
15+
[dependencies.owasm-vm]
16+
path = ".."
17+
18+
# Prevent this from interfering with workspaces
19+
[workspace]
20+
members = ["."]
21+
22+
[[bin]]
23+
name = "fuzz_target_1"
24+
path = "fuzz_targets/fuzz_target_1.rs"
25+
test = false
26+
doc = false
27+
28+
[[bin]]
29+
name = "../../src/compile2"
30+
path = "fuzz_targets/../../src/compile2.rs"
31+
test = false
32+
doc = false
33+
34+
[[bin]]
35+
name = "newtry"
36+
path = "fuzz_targets/newtry.rs"
37+
test = false
38+
doc = false
39+
40+
[[bin]]
41+
name = "fuzz_imported_wat"
42+
path = "fuzz_targets/fuzz_imported_wat.rs"
43+
test = false
44+
doc = false
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
#![no_main]
2+
use libfuzzer_sys::fuzz_target;
3+
extern crate owasm_vm;
4+
use crate::owasm_vm::cache::*;
5+
use crate::owasm_vm::error::Error;
6+
use owasm_vm::vm::Querier;
7+
use std::collections::HashMap;
8+
use std::io::{Read, Write};
9+
use std::process::Command;
10+
use tempfile::NamedTempFile;
11+
12+
pub struct MockQuerier {}
13+
14+
impl Querier for MockQuerier {
15+
fn get_span_size(&self) -> i64 {
16+
300
17+
}
18+
fn get_calldata(&self) -> Result<Vec<u8>, Error> {
19+
Ok(vec![1])
20+
}
21+
fn set_return_data(&self, _: &[u8]) -> Result<(), Error> {
22+
Ok(())
23+
}
24+
fn get_ask_count(&self) -> i64 {
25+
10
26+
}
27+
fn get_min_count(&self) -> i64 {
28+
8
29+
}
30+
fn get_prepare_time(&self) -> i64 {
31+
100_000
32+
}
33+
fn get_execute_time(&self) -> Result<i64, Error> {
34+
Ok(100_000)
35+
}
36+
fn get_ans_count(&self) -> Result<i64, Error> {
37+
Ok(8)
38+
}
39+
fn ask_external_data(&self, _: i64, _: i64, _: &[u8]) -> Result<(), Error> {
40+
Ok(())
41+
}
42+
fn get_external_data_status(&self, _: i64, _: i64) -> Result<i64, Error> {
43+
Ok(1)
44+
}
45+
fn get_external_data(&self, _: i64, _: i64) -> Result<Vec<u8>, Error> {
46+
Ok(vec![1])
47+
}
48+
}
49+
50+
fn wat2wasm(wat: impl AsRef<[u8]>) -> Vec<u8> {
51+
let mut input_file = NamedTempFile::new().unwrap();
52+
let mut output_file = NamedTempFile::new().unwrap();
53+
input_file.write_all(wat.as_ref()).unwrap();
54+
Command::new("wat2wasm")
55+
.args(&[
56+
input_file.path().to_str().unwrap(),
57+
"-o",
58+
output_file.path().to_str().unwrap(),
59+
"--no-check",
60+
])
61+
.output()
62+
.unwrap();
63+
let mut wasm = Vec::new();
64+
output_file.read_to_end(&mut wasm).unwrap();
65+
wasm
66+
}
67+
68+
fn generate_wat(imported_function: String) -> String {
69+
let s = format!(
70+
r#"(module
71+
{}
72+
(func (;"execute": Resolves with result "beeb";))
73+
(memory (export "memory") 512)
74+
(data (i32.const 1048576) "beeb")
75+
(export "prepare" (func 1))
76+
(export "execute" (func 2)))
77+
"#,
78+
imported_function
79+
);
80+
return s;
81+
}
82+
83+
fuzz_target!(|data: [u64; 30]| {
84+
let mut imported_wat = vec![];
85+
imported_wat.push((
86+
"get_span_size",
87+
format!(
88+
r#"(type (func (param) (result i64)))
89+
(import "env" "get_span_size" (func (type 0)))
90+
(func
91+
call 0
92+
drop
93+
)"#
94+
),
95+
));
96+
imported_wat.push((
97+
"read_calldata",
98+
format!(
99+
r#"(type (func (param i64) (result i64)))
100+
(import "env" "read_calldata" (func (type 0)))
101+
(func
102+
(i64.const {})
103+
call 0
104+
drop
105+
)"#,
106+
data[29],
107+
),
108+
));
109+
imported_wat.push((
110+
"set_return_data",
111+
format!(
112+
r#"(type (func (param i64 i64) (result)))
113+
(import "env" "set_return_data" (func (type 0)))
114+
(func
115+
(i64.const {})
116+
(i64.const {})
117+
call 0
118+
)"#,
119+
data[28], data[29],
120+
),
121+
));
122+
imported_wat.push((
123+
"get_ask_count",
124+
format!(
125+
r#"(type (func (param) (result i64)))
126+
(import "env" "get_ask_count" (func (type 0)))
127+
(func
128+
call 0
129+
drop
130+
)"#,
131+
),
132+
));
133+
imported_wat.push((
134+
"get_min_count",
135+
format!(
136+
r#"(type (func (param) (result i64)))
137+
(import "env" "get_min_count" (func (type 0)))
138+
(func
139+
call 0
140+
drop
141+
)"#,
142+
),
143+
));
144+
imported_wat.push((
145+
"get_prepare_time",
146+
format!(
147+
r#"(type (func (param) (result i64)))
148+
(import "env" "get_prepare_time" (func (type 0)))
149+
(func
150+
call 0
151+
drop
152+
)"#,
153+
),
154+
));
155+
imported_wat.push((
156+
"get_execute_time",
157+
format!(
158+
r#"(type (func (param) (result i64)))
159+
(import "env" "get_execute_time" (func (type 0)))
160+
(func
161+
call 0
162+
drop
163+
)"#,
164+
),
165+
));
166+
imported_wat.push((
167+
"get_ans_count",
168+
format!(
169+
r#"(type (func (param) (result i64)))
170+
(import "env" "get_ans_count" (func (type 0)))
171+
(func
172+
call 0
173+
drop
174+
)"#,
175+
),
176+
));
177+
imported_wat.push((
178+
"ask_external_data",
179+
format!(
180+
r#"(type (func (param i64 i64 i64 i64) (result)))
181+
(import "env" "ask_external_data" (func (type 0)))
182+
(func
183+
(i64.const {})
184+
(i64.const {})
185+
(i64.const {})
186+
(i64.const {})
187+
call 0
188+
)"#,
189+
data[26], data[27], data[28], data[29]
190+
),
191+
));
192+
imported_wat.push((
193+
"get_external_data_status",
194+
format!(
195+
r#"(type (func (param i64 i64) (result i64)))
196+
(import "env" "get_external_data_status" (func (type 0)))
197+
(func
198+
(i64.const {})
199+
(i64.const {})
200+
call 0
201+
drop
202+
)"#,
203+
data[28], data[29]
204+
),
205+
));
206+
imported_wat.push((
207+
"read_external_data",
208+
format!(
209+
r#"(type (func (param i64 i64 i64) (result i64)))
210+
(import "env" "read_external_data" (func (type 0)))
211+
(func
212+
(i64.const {})
213+
(i64.const {})
214+
(i64.const {})
215+
call 0
216+
drop
217+
)"#,
218+
data[27], data[28], data[29]
219+
),
220+
));
221+
for (func, wat) in &imported_wat {
222+
println!("======================");
223+
println!("{:?}", func);
224+
let s = generate_wat(wat.clone());
225+
let wasm = wat2wasm(s);
226+
let code = owasm_vm::compile(&wasm).unwrap();
227+
let mut cache = Cache::new(CacheOptions { cache_size: 10000 });
228+
let gas = owasm_vm::run(&mut cache, &code, u64::MAX, true, MockQuerier {});
229+
println!("{:?}", gas);
230+
}
231+
});

0 commit comments

Comments
 (0)