From bdf6a5660ebe3e9e22560956ffe48f8d54e2b7ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Mon, 8 Aug 2016 09:59:00 +0200 Subject: [PATCH 1/2] Fixing gas conversion --- ethcore/src/evm/evm.rs | 22 ++++++++++++++++++++-- ethcore/src/evm/interpreter/mod.rs | 6 +++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/ethcore/src/evm/evm.rs b/ethcore/src/evm/evm.rs index a40c03d1b..f19541d43 100644 --- a/ethcore/src/evm/evm.rs +++ b/ethcore/src/evm/evm.rs @@ -151,10 +151,14 @@ impl CostType for usize { } fn from_u256(val: U256) -> Result { - if U256::from(val.low_u64()) != val { + let res = val.low_u64() as usize; + + // validate if value fits into usize + if U256::from(res) != val { return Err(Error::OutOfGas); } - Ok(val.low_u64() as usize) + + Ok(res) } fn as_usize(&self) -> usize { @@ -191,6 +195,7 @@ pub trait Evm { #[test] +#[cfg(test)] fn should_calculate_overflow_mul_shr_without_overflow() { // given let num = 1048576; @@ -207,6 +212,7 @@ fn should_calculate_overflow_mul_shr_without_overflow() { } #[test] +#[cfg(test)] fn should_calculate_overflow_mul_shr_with_overflow() { // given let max = ::std::u64::MAX; @@ -225,3 +231,15 @@ fn should_calculate_overflow_mul_shr_with_overflow() { assert!(o1); } +#[test] +#[cfg(test)] +fn should_validate_u256_to_usize_conversion() { + // given + let v = U256::from(::std::usize::MAX) + U256::from(1); + + // when + let res = Gas::::from_u256(v); + + // then + assert!(res.is_err()); +} diff --git a/ethcore/src/evm/interpreter/mod.rs b/ethcore/src/evm/interpreter/mod.rs index 8a3eae5b3..07aa0060b 100644 --- a/ethcore/src/evm/interpreter/mod.rs +++ b/ethcore/src/evm/interpreter/mod.rs @@ -515,11 +515,11 @@ impl Interpreter { Ok(InstructionResult::Ok) } - fn copy_data_to_memory(&mut self, stack: &mut Stack, data: &[u8]) { + fn copy_data_to_memory(&mut self, stack: &mut Stack, source: &[u8]) { let dest_offset = stack.pop_back(); let source_offset = stack.pop_back(); let size = stack.pop_back(); - let source_size = U256::from(data.len()); + let source_size = U256::from(source.len()); let output_end = match source_offset > source_size || size > source_size || source_offset + size > source_size { true => { @@ -531,7 +531,7 @@ impl Interpreter { for i in zero_slice.iter_mut() { *i = 0; } - data.len() + source.len() }, false => (size.low_u64() + source_offset.low_u64()) as usize }; From 10b18db833dca057d839840af02e927ee294c120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Mon, 8 Aug 2016 10:26:28 +0200 Subject: [PATCH 2/2] Validating u256->usize conversion --- Cargo.lock | 9 +++------ ethcore/src/evm/evm.rs | 2 +- ethcore/src/evm/interpreter/mod.rs | 2 +- json/src/hash.rs | 4 ++-- json/src/uint.rs | 4 ++-- 5 files changed, 9 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 46a2fd71c..baa88f12e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1549,16 +1549,13 @@ dependencies = [ "checksum aster 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "07d344974f0a155f091948aa389fb1b912d3a58414fbdb9c8d446d193ee3496a" "checksum bit-set 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9bf6104718e80d7b26a68fdbacff3481cfc05df670821affc7e9cbc1884400c" "checksum bit-vec 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5b97c2c8e8bbb4251754f559df8af22fb264853c7d009084a576cdf12565089d" -"checksum bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "32866f4d103c4e438b1db1158aa1b1a80ee078e5d77a59a2f906fd62a577389c" "checksum bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dead7461c1127cf637931a1e50934eb6eee8bff2f74433ac7909e9afcee04a3" "checksum bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4f67931368edf3a9a51d29886d245f1c3db2f1ef0dcc9e35ff70341b78c10d23" -"checksum bitflags 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "72cd7314bd4ee024071241147222c706e80385a1605ac7d4cd2fcc339da2ae46" "checksum blastfig 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "09640e0509d97d5cdff03a9f5daf087a8e04c735c3b113a75139634a19cfc7b2" "checksum bloomchain 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3f421095d2a76fc24cd3fb3f912b90df06be7689912b1bdb423caefae59c258d" "checksum byteorder 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3e68d0b3b234a583993a53d5b0063fb5fe8713590fe733d41b98a2cee6a9c26e" "checksum bytes 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c129aff112dcc562970abb69e2508b40850dd24c274761bb50fb8a0067ba6c27" "checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c" -"checksum chrono 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)" = "a714b6792cb4bb07643c35d2a051d92988d4e296322a60825549dd0764bcd396" "checksum clippy 0.0.80 (registry+https://github.com/rust-lang/crates.io-index)" = "e96469b413984c78285727f94f9c626a1f2006cecdcf813b5d6893c0c85df42f" "checksum clippy_lints 0.0.80 (registry+https://github.com/rust-lang/crates.io-index)" = "f11938c4b10c556903bb1c1e717eb038658324bf7197e4cfc159a16417327345" "checksum cookie 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "90266f45846f14a1e986c77d1e9c2626b8c342ed806fe60241ec38cc8697b245" @@ -1577,9 +1574,10 @@ dependencies = [ "checksum hpack 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d2da7d3a34cf6406d9d700111b8eafafe9a251de41ae71d8052748259343b58" "checksum httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "46534074dbb80b070d60a5cb8ecadd8963a00a438ae1a95268850a7ef73b67ae" "checksum hyper 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bb0f4d00bb781e559b6e66ae4b5479df0fdf9ab15949f52fa2f1f5de16d4cc07" +"checksum hyper 0.9.10 (registry+https://github.com/rust-lang/crates.io-index)" = "eb27e8a3e8f17ac43ffa41bbda9cf5ad3f9f13ef66fa4873409d4902310275f7" "checksum hyper 0.9.4 (git+https://github.com/ethcore/hyper)" = "" "checksum idna 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1053236e00ce4f668aeca4a769a09b3bf5a682d802abd6f3cb39374f6b162c11" -"checksum igd 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8f5b93df68d6152576e9bc9f371e33e00b40738d528b3566ff41ea11d04401dc" +"checksum igd 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c8c12b1795b8b168f577c45fa10379b3814dcb11b7ab702406001f0d63f40484" "checksum isatty 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7408a548dc0e406b7912d9f84c261cc533c1866e047644a811c133c56041ac0c" "checksum itertools 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)" = "086e1fa5fe48840b1cfdef3a20c7e3115599f8d5c4c87ef32a794a7cdd184d76" "checksum json-ipc-server 0.2.4 (git+https://github.com/ethcore/json-ipc-server.git)" = "" @@ -1683,6 +1681,5 @@ dependencies = [ "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" "checksum ws 0.5.0 (git+https://github.com/ethcore/ws-rs.git?branch=stable)" = "" "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" -"checksum xml-rs 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)" = "4bac8fd82b24db2dd3b54aa7b29f336d8b5ca1830065ce3aada71bce6f661519" -"checksum xml-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f11ef7864e55d06a38755beaf03ab70139a04e619acfe94ef800b11bd79eb52c" +"checksum xml-rs 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "65e74b96bd3179209dc70a980da6df843dff09e46eee103a0376c0949257e3ef" "checksum xmltree 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "472a9d37c7c53ab2391161df5b89b1f3bf76dab6ab150d7941ecbdd832282082" diff --git a/ethcore/src/evm/evm.rs b/ethcore/src/evm/evm.rs index f19541d43..2bda9c488 100644 --- a/ethcore/src/evm/evm.rs +++ b/ethcore/src/evm/evm.rs @@ -238,7 +238,7 @@ fn should_validate_u256_to_usize_conversion() { let v = U256::from(::std::usize::MAX) + U256::from(1); // when - let res = Gas::::from_u256(v); + let res = usize::from_u256(v); // then assert!(res.is_err()); diff --git a/ethcore/src/evm/interpreter/mod.rs b/ethcore/src/evm/interpreter/mod.rs index 07aa0060b..df3ca2b4a 100644 --- a/ethcore/src/evm/interpreter/mod.rs +++ b/ethcore/src/evm/interpreter/mod.rs @@ -538,7 +538,7 @@ impl Interpreter { if source_offset < source_size { let output_begin = source_offset.low_u64() as usize; - self.mem.write_slice(dest_offset, &data[output_begin..output_end]); + self.mem.write_slice(dest_offset, &source[output_begin..output_end]); } } diff --git a/json/src/hash.rs b/json/src/hash.rs index ad546fcab..16d8ee9c0 100644 --- a/json/src/hash.rs +++ b/json/src/hash.rs @@ -48,10 +48,10 @@ macro_rules! impl_hash { 0 => $inner::from(0), 2 if value == "0x" => $inner::from(0), _ if value.starts_with("0x") => try!($inner::from_str(&value[2..]).map_err(|_| { - Error::custom(format!("Invalid hex value {}.", value).as_ref()) + Error::custom(format!("Invalid hex value {}.", value).as_str()) })), _ => try!($inner::from_str(value).map_err(|_| { - Error::custom(format!("Invalid hex value {}.", value).as_ref()) + Error::custom(format!("Invalid hex value {}.", value).as_str()) })) }; diff --git a/json/src/uint.rs b/json/src/uint.rs index e77001461..bcab142f6 100644 --- a/json/src/uint.rs +++ b/json/src/uint.rs @@ -70,10 +70,10 @@ impl Visitor for UintVisitor { 0 => U256::from(0), 2 if value.starts_with("0x") => U256::from(0), _ if value.starts_with("0x") => try!(U256::from_str(&value[2..]).map_err(|_| { - Error::custom(format!("Invalid hex value {}.", value).as_ref()) + Error::custom(format!("Invalid hex value {}.", value).as_str()) })), _ => try!(U256::from_dec_str(value).map_err(|_| { - Error::custom(format!("Invalid decimal value {}.", value).as_ref()) + Error::custom(format!("Invalid decimal value {}.", value).as_str()) })) };