2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-02-05 13:40:41 +01:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-02-05 13:40:41 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
2015-12-30 12:46:10 +01:00
|
|
|
//! Evm interface.
|
2015-12-28 22:37:15 +01:00
|
|
|
|
2016-09-05 11:56:44 +02:00
|
|
|
use std::{ops, cmp, fmt};
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{U128, U256, U512};
|
2017-08-01 12:37:57 +02:00
|
|
|
use vm::{Ext, Result, ReturnData, GasLeft, Error};
|
2017-05-23 15:49:17 +02:00
|
|
|
|
|
|
|
/// Finalization result. Gas Left: either it is a known value, or it needs to be computed by processing
|
|
|
|
/// a return instruction.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FinalizationResult {
|
|
|
|
/// Final amount of gas left.
|
|
|
|
pub gas_left: U256,
|
|
|
|
/// Apply execution state changes or revert them.
|
|
|
|
pub apply_state: bool,
|
2017-06-06 17:47:12 +02:00
|
|
|
/// Return data buffer.
|
|
|
|
pub return_data: ReturnData,
|
2016-06-02 19:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Types that can be "finalized" using an EVM.
|
2016-04-06 10:07:24 +02:00
|
|
|
///
|
2016-06-02 19:04:15 +02:00
|
|
|
/// In practice, this is just used to define an inherent impl on
|
|
|
|
/// `Reult<GasLeft<'a>>`.
|
|
|
|
pub trait Finalize {
|
2017-05-23 15:49:17 +02:00
|
|
|
/// Consume the externalities, call return if necessary, and produce call result.
|
|
|
|
fn finalize<E: Ext>(self, ext: E) -> Result<FinalizationResult>;
|
2016-06-02 19:04:15 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 17:47:12 +02:00
|
|
|
impl Finalize for Result<GasLeft> {
|
2017-05-23 15:49:17 +02:00
|
|
|
fn finalize<E: Ext>(self, ext: E) -> Result<FinalizationResult> {
|
2016-06-02 19:04:15 +02:00
|
|
|
match self {
|
2017-06-06 17:47:12 +02:00
|
|
|
Ok(GasLeft::Known(gas_left)) => Ok(FinalizationResult { gas_left: gas_left, apply_state: true, return_data: ReturnData::empty() }),
|
2018-08-13 23:27:13 +02:00
|
|
|
Ok(GasLeft::NeedsReturn { gas_left, data, apply_state }) => ext.ret(&gas_left, &data, apply_state).map(|gas_left| FinalizationResult {
|
2017-05-23 15:49:17 +02:00
|
|
|
gas_left: gas_left,
|
|
|
|
apply_state: apply_state,
|
2017-06-06 17:47:12 +02:00
|
|
|
return_data: data,
|
2017-05-23 15:49:17 +02:00
|
|
|
}),
|
2016-06-02 19:04:15 +02:00
|
|
|
Err(err) => Err(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-11 02:17:29 +01:00
|
|
|
|
2018-08-13 22:06:15 +02:00
|
|
|
impl Finalize for Error {
|
|
|
|
fn finalize<E: Ext>(self, _ext: E) -> Result<FinalizationResult> {
|
|
|
|
Err(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 09:42:41 +02:00
|
|
|
/// Cost calculation type. For low-gas usage we calculate costs using usize instead of U256
|
2018-10-02 16:33:19 +02:00
|
|
|
pub trait CostType: Sized + From<usize> + Copy + Send
|
2018-10-04 14:50:18 +02:00
|
|
|
+ ops::Mul<Output=Self> + ops::Div<Output=Self> + ops::Add<Output=Self> + ops::Sub<Output=Self>
|
2016-09-19 12:16:22 +02:00
|
|
|
+ ops::Shr<usize, Output=Self> + ops::Shl<usize, Output=Self>
|
|
|
|
+ cmp::Ord + fmt::Debug {
|
2016-07-11 09:42:41 +02:00
|
|
|
/// Converts this cost into `U256`
|
2016-07-05 15:15:44 +02:00
|
|
|
fn as_u256(&self) -> U256;
|
2016-07-11 09:42:41 +02:00
|
|
|
/// Tries to fit `U256` into this `Cost` type
|
2016-07-05 15:15:44 +02:00
|
|
|
fn from_u256(val: U256) -> Result<Self>;
|
2016-07-11 09:42:41 +02:00
|
|
|
/// Convert to usize (may panic)
|
2016-07-05 15:15:44 +02:00
|
|
|
fn as_usize(&self) -> usize;
|
2016-07-11 09:42:41 +02:00
|
|
|
/// Add with overflow
|
2016-07-05 15:15:44 +02:00
|
|
|
fn overflow_add(self, other: Self) -> (Self, bool);
|
2016-07-11 09:42:41 +02:00
|
|
|
/// Multiple with overflow
|
2016-07-05 15:15:44 +02:00
|
|
|
fn overflow_mul(self, other: Self) -> (Self, bool);
|
2016-07-30 15:38:44 +02:00
|
|
|
/// Single-step full multiplication and shift: `(self*other) >> shr`
|
2016-07-12 09:49:16 +02:00
|
|
|
/// Should not overflow on intermediate steps
|
2016-07-30 15:38:44 +02:00
|
|
|
fn overflow_mul_shr(self, other: Self, shr: usize) -> (Self, bool);
|
2016-07-05 15:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CostType for U256 {
|
|
|
|
fn as_u256(&self) -> U256 {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_u256(val: U256) -> Result<Self> {
|
|
|
|
Ok(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_usize(&self) -> usize {
|
|
|
|
self.as_u64() as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
fn overflow_add(self, other: Self) -> (Self, bool) {
|
2017-05-24 12:31:33 +02:00
|
|
|
self.overflowing_add(other)
|
2016-07-05 15:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn overflow_mul(self, other: Self) -> (Self, bool) {
|
2017-05-24 12:31:33 +02:00
|
|
|
self.overflowing_mul(other)
|
2016-07-05 15:15:44 +02:00
|
|
|
}
|
2016-07-12 09:49:16 +02:00
|
|
|
|
2016-07-30 15:38:44 +02:00
|
|
|
fn overflow_mul_shr(self, other: Self, shr: usize) -> (Self, bool) {
|
2016-07-12 09:49:16 +02:00
|
|
|
let x = self.full_mul(other);
|
2016-07-30 15:38:44 +02:00
|
|
|
let U512(parts) = x;
|
2016-07-12 09:49:16 +02:00
|
|
|
let overflow = (parts[4] | parts[5] | parts[6] | parts[7]) > 0;
|
2016-07-30 15:38:44 +02:00
|
|
|
let U512(parts) = x >> shr;
|
2016-07-12 09:49:16 +02:00
|
|
|
(
|
|
|
|
U256([parts[0], parts[1], parts[2], parts[3]]),
|
2016-07-30 15:38:44 +02:00
|
|
|
overflow
|
2016-07-12 09:49:16 +02:00
|
|
|
)
|
|
|
|
}
|
2016-07-05 15:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CostType for usize {
|
|
|
|
fn as_u256(&self) -> U256 {
|
|
|
|
U256::from(*self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_u256(val: U256) -> Result<Self> {
|
2016-08-08 09:59:00 +02:00
|
|
|
let res = val.low_u64() as usize;
|
|
|
|
|
|
|
|
// validate if value fits into usize
|
|
|
|
if U256::from(res) != val {
|
2016-07-05 15:15:44 +02:00
|
|
|
return Err(Error::OutOfGas);
|
|
|
|
}
|
2016-08-08 09:59:00 +02:00
|
|
|
|
|
|
|
Ok(res)
|
2016-07-05 15:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn as_usize(&self) -> usize {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn overflow_add(self, other: Self) -> (Self, bool) {
|
|
|
|
self.overflowing_add(other)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn overflow_mul(self, other: Self) -> (Self, bool) {
|
|
|
|
self.overflowing_mul(other)
|
|
|
|
}
|
2016-07-12 09:49:16 +02:00
|
|
|
|
2016-07-30 15:38:44 +02:00
|
|
|
fn overflow_mul_shr(self, other: Self, shr: usize) -> (Self, bool) {
|
2016-07-12 09:49:16 +02:00
|
|
|
let (c, o) = U128::from(self).overflowing_mul(U128::from(other));
|
2016-07-30 15:38:44 +02:00
|
|
|
let U128(parts) = c;
|
|
|
|
let overflow = o | (parts[1] > 0);
|
|
|
|
let U128(parts) = c >> shr;
|
2016-07-12 09:49:16 +02:00
|
|
|
let result = parts[0] as usize;
|
2016-07-30 15:38:44 +02:00
|
|
|
let overflow = overflow | (parts[0] > result as u64);
|
2016-07-12 09:49:16 +02:00
|
|
|
(result, overflow)
|
|
|
|
}
|
2016-07-05 15:15:44 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 09:59:00 +02:00
|
|
|
#[cfg(test)]
|
2016-09-05 11:56:44 +02:00
|
|
|
mod tests {
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::U256;
|
2016-09-05 11:56:44 +02:00
|
|
|
use super::CostType;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_calculate_overflow_mul_shr_without_overflow() {
|
|
|
|
// given
|
|
|
|
let num = 1048576;
|
|
|
|
|
|
|
|
// when
|
|
|
|
let (res1, o1) = U256::from(num).overflow_mul_shr(U256::from(num), 20);
|
|
|
|
let (res2, o2) = num.overflow_mul_shr(num, 20);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert_eq!(res1, U256::from(num));
|
|
|
|
assert!(!o1);
|
|
|
|
assert_eq!(res2, num);
|
|
|
|
assert!(!o2);
|
|
|
|
}
|
2016-07-12 09:49:16 +02:00
|
|
|
|
2016-09-05 11:56:44 +02:00
|
|
|
#[test]
|
|
|
|
fn should_calculate_overflow_mul_shr_with_overflow() {
|
|
|
|
// given
|
|
|
|
let max = u64::max_value();
|
|
|
|
let num1 = U256([max, max, max, max]);
|
|
|
|
let num2 = usize::max_value();
|
2016-07-12 09:49:16 +02:00
|
|
|
|
2016-09-05 11:56:44 +02:00
|
|
|
// when
|
|
|
|
let (res1, o1) = num1.overflow_mul_shr(num1, 256);
|
|
|
|
let (res2, o2) = num2.overflow_mul_shr(num2, 64);
|
2016-08-08 09:59:00 +02:00
|
|
|
|
2016-09-05 11:56:44 +02:00
|
|
|
// then
|
|
|
|
assert_eq!(res2, num2 - 1);
|
|
|
|
assert!(o2);
|
|
|
|
|
|
|
|
assert_eq!(res1, !U256::zero() - U256::one());
|
|
|
|
assert!(o1);
|
|
|
|
}
|
2016-08-08 09:59:00 +02:00
|
|
|
|
2016-09-05 11:56:44 +02:00
|
|
|
#[test]
|
|
|
|
fn should_validate_u256_to_usize_conversion() {
|
|
|
|
// given
|
|
|
|
let v = U256::from(usize::max_value()) + U256::from(1);
|
|
|
|
|
|
|
|
// when
|
|
|
|
let res = usize::from_u256(v);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
2016-08-08 09:59:00 +02:00
|
|
|
}
|