2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2014-10-18 11:31:20 +00:00
|
|
|
package vm
|
2014-07-22 09:54:48 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-18 07:10:59 +00:00
|
|
|
"github.com/holiman/uint256"
|
2014-10-14 09:48:52 +00:00
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/common/math"
|
2014-10-14 09:48:52 +00:00
|
|
|
)
|
|
|
|
|
2019-03-12 09:40:05 +00:00
|
|
|
// calcMemSize64 calculates the required memory size, and returns
|
|
|
|
// the size and whether the result overflowed uint64
|
2020-05-18 07:10:59 +00:00
|
|
|
func calcMemSize64(off, l *uint256.Int) (uint64, bool) {
|
2019-03-12 09:40:05 +00:00
|
|
|
if !l.IsUint64() {
|
|
|
|
return 0, true
|
2014-10-14 09:48:52 +00:00
|
|
|
}
|
2019-03-12 09:40:05 +00:00
|
|
|
return calcMemSize64WithUint(off, l.Uint64())
|
|
|
|
}
|
2014-10-14 09:48:52 +00:00
|
|
|
|
2019-03-12 09:40:05 +00:00
|
|
|
// calcMemSize64WithUint calculates the required memory size, and returns
|
|
|
|
// the size and whether the result overflowed uint64
|
|
|
|
// Identical to calcMemSize64, but length is a uint64
|
2020-05-18 07:10:59 +00:00
|
|
|
func calcMemSize64WithUint(off *uint256.Int, length64 uint64) (uint64, bool) {
|
2019-03-12 09:40:05 +00:00
|
|
|
// if length is zero, memsize is always zero, regardless of offset
|
|
|
|
if length64 == 0 {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
// Check that offset doesn't overflow
|
2020-05-18 07:10:59 +00:00
|
|
|
offset64, overflow := off.Uint64WithOverflow()
|
|
|
|
if overflow {
|
2019-03-12 09:40:05 +00:00
|
|
|
return 0, true
|
|
|
|
}
|
|
|
|
val := offset64 + length64
|
|
|
|
// if value < either of it's parts, then it overflowed
|
|
|
|
return val, val < offset64
|
2014-10-14 09:48:52 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
// getData returns a slice from the data based on the start and size and pads
|
|
|
|
// up to size with zero's. This function is overflow safe.
|
2017-08-14 14:08:49 +00:00
|
|
|
func getData(data []byte, start uint64, size uint64) []byte {
|
|
|
|
length := uint64(len(data))
|
|
|
|
if start > length {
|
|
|
|
start = length
|
|
|
|
}
|
|
|
|
end := start + size
|
|
|
|
if end > length {
|
|
|
|
end = length
|
|
|
|
}
|
|
|
|
return common.RightPadBytes(data[start:end], int(size))
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDataBig returns a slice from the data based on the start and size and pads
|
|
|
|
// up to size with zero's. This function is overflow safe.
|
2020-05-18 07:10:59 +00:00
|
|
|
func getDataBig(data []byte, start *uint256.Int, size uint64) []byte {
|
|
|
|
start64, overflow := start.Uint64WithOverflow()
|
|
|
|
if overflow {
|
|
|
|
start64 = ^uint64(0)
|
|
|
|
}
|
|
|
|
return getData(data, start64, size)
|
2017-01-04 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-31 12:40:41 +00:00
|
|
|
// ToWordSize returns the ceiled word size required for memory expansion.
|
|
|
|
func ToWordSize(size uint64) uint64 {
|
2017-01-04 19:17:24 +00:00
|
|
|
if size > math.MaxUint64-31 {
|
|
|
|
return math.MaxUint64/32 + 1
|
2015-03-28 19:03:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 19:17:24 +00:00
|
|
|
return (size + 31) / 32
|
2015-03-28 19:03:25 +00:00
|
|
|
}
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 21:21:51 +00:00
|
|
|
|
|
|
|
func allZero(b []byte) bool {
|
|
|
|
for _, byte := range b {
|
|
|
|
if byte != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|