github.com/amazechain/amc@v0.1.3/internal/vm/common.go (about) 1 // Copyright 2023 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 package vm 17 18 import ( 19 "github.com/amazechain/amc/common/types" 20 "github.com/holiman/uint256" 21 22 "github.com/amazechain/amc/common/math" 23 ) 24 25 // calcMemSize64 calculates the required memory size, and returns 26 // the size and whether the result overflowed uint64 27 func calcMemSize64(off, l *uint256.Int) (uint64, bool) { 28 if !l.IsUint64() { 29 return 0, true 30 } 31 return calcMemSize64WithUint(off, l.Uint64()) 32 } 33 34 // calcMemSize64WithUint calculates the required memory size, and returns 35 // the size and whether the result overflowed uint64 36 // Identical to calcMemSize64, but length is a uint64 37 func calcMemSize64WithUint(off *uint256.Int, length64 uint64) (uint64, bool) { 38 // if length is zero, memsize is always zero, regardless of offset 39 if length64 == 0 { 40 return 0, false 41 } 42 // Check that offset doesn't overflow 43 offset64, overflow := off.Uint64WithOverflow() 44 if overflow { 45 return 0, true 46 } 47 val := offset64 + length64 48 // if value < either of it's parts, then it overflowed 49 return val, val < offset64 50 } 51 52 // getData returns a slice from the data based on the start and size and pads 53 // up to size with zero's. This function is overflow safe. 54 func getData(data []byte, start uint64, size uint64) []byte { 55 length := uint64(len(data)) 56 if start > length { 57 start = length 58 } 59 end := start + size 60 if end > length { 61 end = length 62 } 63 return types.RightPadBytes(data[start:end], int(size)) 64 } 65 66 // getDataBig returns a slice from the data based on the start and size and pads 67 // up to size with zero's. This function is overflow safe. 68 func getDataBig(data []byte, start *uint256.Int, size uint64) []byte { 69 start64, overflow := start.Uint64WithOverflow() 70 if overflow { 71 start64 = ^uint64(0) 72 } 73 return getData(data, start64, size) 74 } 75 76 // ToWordSize returns the ceiled word size required for memory expansion. 77 func ToWordSize(size uint64) uint64 { 78 if size > math.MaxUint64-31 { 79 return math.MaxUint64/32 + 1 80 } 81 82 return (size + 31) / 32 83 } 84 85 func allZero(b []byte) bool { 86 for _, byte := range b { 87 if byte != 0 { 88 return false 89 } 90 } 91 return true 92 }