github.com/klaytn/klaytn@v1.12.1/blockchain/vm/memory.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2015 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from core/vm/memory.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package vm 22 23 import ( 24 "fmt" 25 26 "github.com/holiman/uint256" 27 ) 28 29 // Memory implements a simple memory model for the Klaytn virtual machine. 30 type Memory struct { 31 store []byte 32 lastGasCost uint64 33 } 34 35 // NewMemory returns a new memory model. 36 func NewMemory() *Memory { 37 return &Memory{} 38 } 39 40 // Set sets offset + size to value 41 func (m *Memory) Set(offset, size uint64, value []byte) { 42 // It's possible the offset is greater than 0 and size equals 0. This is because 43 // the calcMemSize (common.go) could potentially return 0 when size is zero (NO-OP) 44 if size > 0 { 45 // length of store may never be less than offset + size. 46 // The store should be resized PRIOR to setting the memory 47 if offset+size > uint64(len(m.store)) { 48 panic("invalid memory: store empty") 49 } 50 copy(m.store[offset:offset+size], value) 51 } 52 } 53 54 // Set32 sets the 32 bytes starting at offset to the value of val, left-padded with zeroes to 55 // 32 bytes. 56 func (m *Memory) Set32(offset uint64, val *uint256.Int) { 57 // length of store may never be less than offset + size. 58 // The store should be resized PRIOR to setting the memory 59 if offset+32 > uint64(len(m.store)) { 60 panic("invalid memory: store empty") 61 } 62 // Fill in relevant bits 63 b32 := val.Bytes32() 64 copy(m.store[offset:], b32[:]) 65 } 66 67 // Increase increases the memory with size bytes 68 func (m *Memory) Increase(size uint64) { 69 m.store = append(m.store, make([]byte, size)...) 70 } 71 72 // Resize resizes the memory to size 73 func (m *Memory) Resize(size uint64) { 74 if uint64(m.Len()) < size { 75 m.store = append(m.store, make([]byte, size-uint64(m.Len()))...) 76 } 77 } 78 79 // Get returns offset + size as a new slice 80 func (m *Memory) GetCopy(offset, size int64) (cpy []byte) { 81 if size == 0 { 82 return nil 83 } 84 85 if len(m.store) > int(offset) { 86 cpy = make([]byte, size) 87 copy(cpy, m.store[offset:offset+size]) 88 89 return 90 } 91 92 return 93 } 94 95 // GetPtr returns the offset + size 96 func (m *Memory) GetPtr(offset, size int64) []byte { 97 if size == 0 { 98 return nil 99 } 100 101 if len(m.store) > int(offset) { 102 return m.store[offset : offset+size] 103 } 104 105 return nil 106 } 107 108 // Len returns the length of the backing slice 109 func (m *Memory) Len() int { 110 return len(m.store) 111 } 112 113 // Data returns the backing slice 114 func (m *Memory) Data() []byte { 115 return m.store 116 } 117 118 // Copy copies data from the src position slice into the dst position. 119 // The source and destination may overlap. 120 // OBS: This operation assumes that any necessary memory expansion has already been performed, 121 // and this method may panic otherwise. 122 func (m *Memory) Copy(dst, src, len uint64) { 123 if len == 0 { 124 return 125 } 126 copy(m.store[dst:], m.store[src:src+len]) 127 } 128 129 // Print dumps the content of the memory. 130 func (m *Memory) Print() { 131 fmt.Printf("### mem %d bytes ###\n", len(m.store)) 132 if len(m.store) > 0 { 133 addr := 0 134 for i := 0; i+32 <= len(m.store); i += 32 { 135 fmt.Printf("%03d: % x\n", addr, m.store[i:i+32]) 136 addr++ 137 } 138 } else { 139 fmt.Println("-- empty --") 140 } 141 fmt.Println("####################") 142 } 143 144 func (m *Memory) Slice(begin, end int64) []byte { 145 if end == begin { 146 return []byte{} 147 } 148 if end < begin || begin < 0 { 149 logger.Warn("Tracer accessed out of bound memory", "offset", begin, "end", end) 150 return nil 151 } 152 if m.Len() < int(end) { 153 // TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go 154 // runtime goes belly up https://github.com/golang/go/issues/15639. 155 logger.Warn("Tracer accessed out of bound memory", "available", m.Len(), "offset", begin, "size", end-begin) 156 return nil 157 } 158 return m.GetCopy(begin, end-begin) 159 }