github.com/klaytn/klaytn@v1.10.2/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  	"math/big"
    26  
    27  	"github.com/klaytn/klaytn/common/math"
    28  )
    29  
    30  // Memory implements a simple memory model for the Klaytn virtual machine.
    31  type Memory struct {
    32  	store       []byte
    33  	lastGasCost uint64
    34  }
    35  
    36  // NewMemory returns a new memory model.
    37  func NewMemory() *Memory {
    38  	return &Memory{}
    39  }
    40  
    41  // Set sets offset + size to value
    42  func (m *Memory) Set(offset, size uint64, value []byte) {
    43  	// It's possible the offset is greater than 0 and size equals 0. This is because
    44  	// the calcMemSize (common.go) could potentially return 0 when size is zero (NO-OP)
    45  	if size > 0 {
    46  		// length of store may never be less than offset + size.
    47  		// The store should be resized PRIOR to setting the memory
    48  		if offset+size > uint64(len(m.store)) {
    49  			panic("invalid memory: store empty")
    50  		}
    51  		copy(m.store[offset:offset+size], value)
    52  	}
    53  }
    54  
    55  // Set32 sets the 32 bytes starting at offset to the value of val, left-padded with zeroes to
    56  // 32 bytes.
    57  func (m *Memory) Set32(offset uint64, val *big.Int) {
    58  	// length of store may never be less than offset + size.
    59  	// The store should be resized PRIOR to setting the memory
    60  	if offset+32 > uint64(len(m.store)) {
    61  		panic("invalid memory: store empty")
    62  	}
    63  	// Zero the memory area
    64  	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})
    65  	// Fill in relevant bits
    66  	math.ReadBits(val, m.store[offset:offset+32])
    67  }
    68  
    69  // Increase increases the memory with size bytes
    70  func (m *Memory) Increase(size uint64) {
    71  	m.store = append(m.store, make([]byte, size)...)
    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  }
   134  
   135  func (m *Memory) Slice(begin, end int64) []byte {
   136  	if end == begin {
   137  		return []byte{}
   138  	}
   139  	if end < begin || begin < 0 {
   140  		logger.Warn("Tracer accessed out of bound memory", "offset", begin, "end", end)
   141  		return nil
   142  	}
   143  	if m.Len() < int(end) {
   144  		// TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go
   145  		// runtime goes belly up https://github.com/golang/go/issues/15639.
   146  		logger.Warn("Tracer accessed out of bound memory", "available", m.Len(), "offset", begin, "size", end-begin)
   147  		return nil
   148  	}
   149  	return m.GetCopy(begin, end-begin)
   150  }