gitlab.com/flarenetwork/coreth@v0.1.1/core/vm/memory.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2015 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package vm 28 29 import ( 30 "fmt" 31 32 "github.com/holiman/uint256" 33 ) 34 35 // Memory implements a simple memory model for the ethereum virtual machine. 36 type Memory struct { 37 store []byte 38 lastGasCost uint64 39 } 40 41 // NewMemory returns a new memory model. 42 func NewMemory() *Memory { 43 return &Memory{} 44 } 45 46 // Set sets offset + size to value 47 func (m *Memory) Set(offset, size uint64, value []byte) { 48 // It's possible the offset is greater than 0 and size equals 0. This is because 49 // the calcMemSize (common.go) could potentially return 0 when size is zero (NO-OP) 50 if size > 0 { 51 // length of store may never be less than offset + size. 52 // The store should be resized PRIOR to setting the memory 53 if offset+size > uint64(len(m.store)) { 54 panic("invalid memory: store empty") 55 } 56 copy(m.store[offset:offset+size], value) 57 } 58 } 59 60 // Set32 sets the 32 bytes starting at offset to the value of val, left-padded with zeroes to 61 // 32 bytes. 62 func (m *Memory) Set32(offset uint64, val *uint256.Int) { 63 // length of store may never be less than offset + size. 64 // The store should be resized PRIOR to setting the memory 65 if offset+32 > uint64(len(m.store)) { 66 panic("invalid memory: store empty") 67 } 68 // Zero the memory area 69 copy(m.store[offset:offset+32], []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) 70 // Fill in relevant bits 71 val.WriteToSlice(m.store[offset:]) 72 } 73 74 // Resize resizes the memory to size 75 func (m *Memory) Resize(size uint64) { 76 if uint64(m.Len()) < size { 77 m.store = append(m.store, make([]byte, size-uint64(m.Len()))...) 78 } 79 } 80 81 // Get returns offset + size as a new slice 82 func (m *Memory) GetCopy(offset, size int64) (cpy []byte) { 83 if size == 0 { 84 return nil 85 } 86 87 if len(m.store) > int(offset) { 88 cpy = make([]byte, size) 89 copy(cpy, m.store[offset:offset+size]) 90 91 return 92 } 93 94 return 95 } 96 97 // GetPtr returns the offset + size 98 func (m *Memory) GetPtr(offset, size int64) []byte { 99 if size == 0 { 100 return nil 101 } 102 103 if len(m.store) > int(offset) { 104 return m.store[offset : offset+size] 105 } 106 107 return nil 108 } 109 110 // Len returns the length of the backing slice 111 func (m *Memory) Len() int { 112 return len(m.store) 113 } 114 115 // Data returns the backing slice 116 func (m *Memory) Data() []byte { 117 return m.store 118 } 119 120 // Print dumps the content of the memory. 121 func (m *Memory) Print() { 122 fmt.Printf("### mem %d bytes ###\n", len(m.store)) 123 if len(m.store) > 0 { 124 addr := 0 125 for i := 0; i+32 <= len(m.store); i += 32 { 126 fmt.Printf("%03d: % x\n", addr, m.store[i:i+32]) 127 addr++ 128 } 129 } else { 130 fmt.Println("-- empty --") 131 } 132 fmt.Println("####################") 133 }