Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

WASM: added math overflow test #6474

Merged
merged 1 commit into from
Sep 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions ethcore/wasm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,16 +436,12 @@ macro_rules! reqrep_test {
fake_ext.info = $info;
fake_ext.blockhashes = $block_hashes;

let (gas_left, result) = {
let mut interpreter = wasm_interpreter();
let result = interpreter.exec(params, &mut fake_ext).expect("Interpreter to execute without any errors");
match result {
GasLeft::Known(_) => { panic!("Test is expected to return payload to check"); },
GasLeft::NeedsReturn { gas_left: gas, data: result, apply_state: _apply } => (gas, result.to_vec()),
}
};

(gas_left, result)
let mut interpreter = wasm_interpreter();
interpreter.exec(params, &mut fake_ext)
.map(|result| match result {
GasLeft::Known(_) => { panic!("Test is expected to return payload to check"); },
GasLeft::NeedsReturn { gas_left: gas, data: result, apply_state: _apply } => (gas, result.to_vec()),
})
}
};
}
Expand All @@ -470,7 +466,7 @@ fn math_add() {
arg_b.to_big_endian(&mut args[33..65]);
args.to_vec()
}
);
).expect("Interpreter to execute without any errors");

assert_eq!(gas_left, U256::from(98177));
assert_eq!(
Expand All @@ -492,7 +488,7 @@ fn math_mul() {
arg_b.to_big_endian(&mut args[33..65]);
args.to_vec()
}
);
).expect("Interpreter to execute without any errors");

assert_eq!(gas_left, U256::from(97326));
assert_eq!(
Expand All @@ -501,7 +497,7 @@ fn math_mul() {
);
}

// substraction
// subtraction
#[test]
fn math_sub() {
let (gas_left, result) = reqrep_test!(
Expand All @@ -514,7 +510,7 @@ fn math_sub() {
arg_b.to_big_endian(&mut args[33..65]);
args.to_vec()
}
);
).expect("Interpreter to execute without any errors");

assert_eq!(gas_left, U256::from(98221));
assert_eq!(
Expand All @@ -523,6 +519,24 @@ fn math_sub() {
);
}

// subtraction with overflow
#[test]
fn math_sub_with_overflow() {
let result = reqrep_test!(
"math.wasm",
{
let mut args = [2u8; 65];
let arg_a = U256::from_dec_str("888888888888888888888888888888").unwrap();
let arg_b = U256::from_dec_str("999999999999999999999999999999").unwrap();
arg_a.to_big_endian(&mut args[1..33]);
arg_b.to_big_endian(&mut args[33..65]);
args.to_vec()
}
);

assert_eq!(result, Err(vm::Error::Wasm("Wasm runtime error: User(Panic(\"arithmetic operation overflow\"))".into())));
}

#[test]
fn math_div() {
let (gas_left, result) = reqrep_test!(
Expand All @@ -535,7 +549,7 @@ fn math_div() {
arg_b.to_big_endian(&mut args[33..65]);
args.to_vec()
}
);
).expect("Interpreter to execute without any errors");

assert_eq!(gas_left, U256::from(91510));
assert_eq!(
Expand Down Expand Up @@ -572,7 +586,7 @@ fn externs() {
);
hashes
}
);
).expect("Interpreter to execute without any errors");

assert_eq!(
&result[0..64].to_vec(),
Expand Down