github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/core/vm/common.go (about) 1 // Copyright 2014 The Spectrum Authors 2 // This file is part of the Spectrum library. 3 // 4 // The Spectrum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The Spectrum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the Spectrum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "github.com/holiman/uint256" 21 "math/big" 22 23 "github.com/SmartMeshFoundation/Spectrum/common" 24 "github.com/SmartMeshFoundation/Spectrum/common/math" 25 ) 26 27 // calcMemSize64 calculates the required memory size, and returns 28 // the size and whether the result overflowed uint64 29 func calcMemSize64(off, l *uint256.Int) (uint64, bool) { 30 if !l.IsUint64() { 31 return 0, true 32 } 33 return calcMemSize64WithUint(off, l.Uint64()) 34 } 35 36 // calcMemSize64WithUint calculates the required memory size, and returns 37 // the size and whether the result overflowed uint64 38 // Identical to calcMemSize64, but length is a uint64 39 func calcMemSize64WithUint(off *uint256.Int, length64 uint64) (uint64, bool) { 40 // if length is zero, memsize is always zero, regardless of offset 41 if length64 == 0 { 42 return 0, false 43 } 44 // Check that offset doesn't overflow 45 offset64, overflow := off.Uint64WithOverflow() 46 if overflow { 47 return 0, true 48 } 49 val := offset64 + length64 50 // if value < either of it's parts, then it overflowed 51 return val, val < offset64 52 } 53 54 // calculates the memory size required for a step 55 func calcMemSize(off, l *big.Int) *big.Int { 56 if l.Sign() == 0 { 57 return common.Big0 58 } 59 60 return new(big.Int).Add(off, l) 61 } 62 63 // getData returns a slice from the data based on the start and size and pads 64 // up to size with zero's. This function is overflow safe. 65 func getData(data []byte, start uint64, size uint64) []byte { 66 length := uint64(len(data)) 67 if start > length { 68 start = length 69 } 70 end := start + size 71 if end > length { 72 end = length 73 } 74 return common.RightPadBytes(data[start:end], int(size)) 75 } 76 77 // getDataBig returns a slice from the data based on the start and size and pads 78 // up to size with zero's. This function is overflow safe. 79 func getDataBig(data []byte, start *big.Int, size *big.Int) []byte { 80 dlen := big.NewInt(int64(len(data))) 81 82 s := math.BigMin(start, dlen) 83 e := math.BigMin(new(big.Int).Add(s, size), dlen) 84 return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64())) 85 } 86 87 // bigUint64 returns the integer casted to a uint64 and returns whether it 88 // overflowed in the process. 89 func bigUint64(v *big.Int) (uint64, bool) { 90 return v.Uint64(), v.BitLen() > 64 91 } 92 93 // toWordSize returns the ceiled word size required for memory expansion. 94 func toWordSize(size uint64) uint64 { 95 if size > math.MaxUint64-31 { 96 return math.MaxUint64/32 + 1 97 } 98 99 return (size + 31) / 32 100 } 101 102 func allZero(b []byte) bool { 103 for _, byte := range b { 104 if byte != 0 { 105 return false 106 } 107 } 108 return true 109 }