Skip to content

Commit 1ebdfab

Browse files
committed
add test for calls.rs and vm.rs
1 parent b9963b2 commit 1ebdfab

File tree

4 files changed

+172
-7
lines changed

4 files changed

+172
-7
lines changed

packages/vm/src/calls.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ mod test {
6666

6767
impl vm::Env for MockEnv {
6868
fn get_span_size(&self) -> i64 {
69-
30000
69+
300
7070
}
7171
fn get_calldata(&self) -> Result<Vec<u8>, Error> {
7272
Ok(vec![1])
@@ -143,7 +143,7 @@ mod test {
143143
let code = compile(&wasm).unwrap();
144144
let mut cache = Cache::new(CacheOptions { cache_size: 10000 });
145145
let env = MockEnv {};
146-
let gas_used = run(&mut cache, &code, 4294967290, true, env).unwrap();
146+
let gas_used = run(&mut cache, &code, u64::MAX, true, env).unwrap();
147147
assert_eq!(gas_used, 800013 as u64);
148148
}
149149

@@ -181,7 +181,37 @@ mod test {
181181
let code = compile(&wasm).unwrap();
182182
let mut cache = Cache::new(CacheOptions { cache_size: 10000 });
183183
let env = MockEnv {};
184-
let gas_used = run(&mut cache, &code, 4294967290, true, env).unwrap();
185-
assert_eq!(gas_used, 830018 as u64);
184+
let gas_used = run(&mut cache, &code, u64::MAX, true, env).unwrap();
185+
assert_eq!(gas_used, 800318 as u64);
186+
}
187+
188+
#[test]
189+
fn test_out_of_gas() {
190+
let wasm = wat2wasm(
191+
r#"(module
192+
(type (func (param i64 i64 i64 i64) (result)))
193+
(func
194+
(local $idx i32)
195+
(local.set $idx (i32.const 0))
196+
(block
197+
(loop
198+
(local.set $idx (local.get $idx) (i32.const 1) (i32.add) )
199+
(br_if 0 (i32.lt_u (local.get $idx) (i32.const 100000)))
200+
)
201+
)
202+
)
203+
(func (;"execute": Resolves with result "beeb";)
204+
)
205+
(memory 17)
206+
(data (i32.const 1048576) "beeb") (;str = "beeb";)
207+
(export "prepare" (func 0))
208+
(export "execute" (func 1)))
209+
"#,
210+
);
211+
let code = compile(&wasm).unwrap();
212+
let mut cache = Cache::new(CacheOptions { cache_size: 10000 });
213+
let env = MockEnv {};
214+
let out_of_gas_err = run(&mut cache, &code, 0, true, env).unwrap_err();
215+
assert_eq!(out_of_gas_err, Error::OutOfGasError);
186216
}
187217
}

packages/vm/src/compile.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,14 @@ mod test {
287287
fn test_check_wasm_imports() {
288288
let wasm = wat2wasm(
289289
r#"(module
290-
(type (func (param i64 i64 i32 i64) (result i64)))
290+
(type (func (param i64 i64 i64 i64) (result i64)))
291291
(import "env" "beeb" (func (type 0))))"#,
292292
);
293293
let module = get_module_from_wasm(&wasm);
294294
assert_eq!(check_wasm_imports(&module), Err(Error::InvalidImportsError));
295295
let wasm = wat2wasm(
296296
r#"(module
297-
(type (func (param i64 i64 i32 i64) (result i64)))
297+
(type (func (param i64 i64 i64 i64) (result i64)))
298298
(import "env" "ask_external_data" (func (type 0))))"#,
299299
);
300300
let module = get_module_from_wasm(&wasm);
@@ -309,6 +309,12 @@ mod test {
309309
);
310310
let module = get_module_from_wasm(&wasm);
311311
assert_eq!(check_wasm_exports(&module), Err(Error::InvalidExportsError));
312+
let wasm = wat2wasm(
313+
r#"(module
314+
(func $prepare (export "prepare")))"#,
315+
);
316+
let module = get_module_from_wasm(&wasm);
317+
assert_eq!(check_wasm_exports(&module), Err(Error::InvalidExportsError));
312318
let wasm = wat2wasm(
313319
r#"(module
314320
(func $execute (export "execute"))

packages/vm/src/store.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::sync::Arc;
22

3-
use wasmer::{Singlepass, Store, Universal, wasmparser::Operator, CompilerConfig};
3+
use wasmer::wasmparser::Operator;
4+
use wasmer::{CompilerConfig, Singlepass, Store, Universal};
45
use wasmer_middlewares::Metering;
56

67
fn cost(_operator: &Operator) -> u64 {

packages/vm/src/vm.rs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,131 @@ where
175175
}
176176
}
177177
}
178+
179+
#[cfg(test)]
180+
mod test {
181+
use std::{
182+
io::{Read, Write},
183+
process::Command,
184+
};
185+
186+
use tempfile::NamedTempFile;
187+
use wasmer::{imports, Singlepass, Store, Universal};
188+
189+
use crate::{
190+
cache::{Cache, CacheOptions},
191+
store::make_store,
192+
};
193+
194+
use super::*;
195+
196+
pub struct MockEnv {}
197+
198+
impl Env for MockEnv {
199+
fn get_span_size(&self) -> i64 {
200+
300
201+
}
202+
fn get_calldata(&self) -> Result<Vec<u8>, Error> {
203+
Ok(vec![1])
204+
}
205+
fn set_return_data(&self, _: &[u8]) -> Result<(), Error> {
206+
Ok(())
207+
}
208+
fn get_ask_count(&self) -> i64 {
209+
10
210+
}
211+
fn get_min_count(&self) -> i64 {
212+
8
213+
}
214+
fn get_prepare_time(&self) -> i64 {
215+
100_000
216+
}
217+
fn get_execute_time(&self) -> Result<i64, Error> {
218+
Ok(100_000)
219+
}
220+
fn get_ans_count(&self) -> Result<i64, Error> {
221+
Ok(8)
222+
}
223+
fn ask_external_data(&self, _: i64, _: i64, _: &[u8]) -> Result<(), Error> {
224+
Ok(())
225+
}
226+
fn get_external_data_status(&self, _: i64, _: i64) -> Result<i64, Error> {
227+
Ok(1)
228+
}
229+
fn get_external_data(&self, _: i64, _: i64) -> Result<Vec<u8>, Error> {
230+
Ok(vec![1])
231+
}
232+
}
233+
234+
fn wat2wasm(wat: impl AsRef<[u8]>) -> Vec<u8> {
235+
let mut input_file = NamedTempFile::new().unwrap();
236+
let mut output_file = NamedTempFile::new().unwrap();
237+
input_file.write_all(wat.as_ref()).unwrap();
238+
Command::new("wat2wasm")
239+
.args(&[
240+
input_file.path().to_str().unwrap(),
241+
"-o",
242+
output_file.path().to_str().unwrap(),
243+
])
244+
.output()
245+
.unwrap();
246+
let mut wasm = Vec::new();
247+
output_file.read_to_end(&mut wasm).unwrap();
248+
wasm
249+
}
250+
251+
#[test]
252+
fn test_env_vm() {
253+
let env = Environment::new(MockEnv {});
254+
assert_eq!(300, env.with_vm(|vm| vm.env.get_span_size()));
255+
assert_eq!(300, env.with_mut_vm(|vm| vm.env.get_span_size()));
256+
}
257+
258+
#[test]
259+
fn test_env_wasmer_instance() {
260+
let env = Environment::new(MockEnv {});
261+
assert_eq!(
262+
Error::UninitializedContextData,
263+
env.with_wasmer_instance(|_| { Ok(()) }).unwrap_err()
264+
);
265+
266+
let wasm = wat2wasm(
267+
r#"(module
268+
(func $execute (export "execute"))
269+
(func $prepare (export "prepare"))
270+
)"#,
271+
);
272+
let compiler = Singlepass::new();
273+
let store = Store::new(&Universal::new(compiler).engine());
274+
let import_object = imports! {};
275+
let mut cache = Cache::new(CacheOptions { cache_size: 10000 });
276+
let instance = cache.get_instance(&wasm, &store, &import_object).unwrap();
277+
env.set_wasmer_instance(Some(NonNull::from(&instance)));
278+
assert_eq!(Ok(()), env.with_wasmer_instance(|_| { Ok(()) }));
279+
}
280+
281+
#[test]
282+
fn test_env_gas() {
283+
let env = Environment::new(MockEnv {});
284+
let wasm = wat2wasm(
285+
r#"(module
286+
(func $execute (export "execute"))
287+
(func $prepare (export "prepare"))
288+
)"#,
289+
);
290+
let store = make_store();
291+
let import_object = imports! {};
292+
let mut cache = Cache::new(CacheOptions { cache_size: 10000 });
293+
let instance = cache.get_instance(&wasm, &store, &import_object).unwrap();
294+
env.set_wasmer_instance(Some(NonNull::from(&instance)));
295+
296+
assert_eq!(0, env.get_gas_left());
297+
298+
env.set_gas_left(10);
299+
assert_eq!(10, env.get_gas_left());
300+
301+
assert_eq!(Error::OutOfGasError, env.decrease_gas_left(11).unwrap_err());
302+
assert_eq!(Ok(()), env.decrease_gas_left(3));
303+
assert_eq!(7, env.get_gas_left());
304+
}
305+
}

0 commit comments

Comments
 (0)