github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/core/vm/memory.go (about)

     1  // Copyright 2015 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  	"fmt"
    21  	"github.com/holiman/uint256"
    22  )
    23  
    24  // Memory implements a simple memory model for the ethereum virtual machine.
    25  type Memory struct {
    26  	store       []byte
    27  	lastGasCost uint64
    28  }
    29  
    30  func NewMemory() *Memory {
    31  	return &Memory{}
    32  }
    33  
    34  // Set sets offset + size to value
    35  func (m *Memory) Set(offset, size uint64, value []byte) {
    36  	// length of store may never be less than offset + size.
    37  	// The store should be resized PRIOR to setting the memory
    38  	if size > uint64(len(m.store)) {
    39  		panic("INVALID memory: store empty")
    40  	}
    41  
    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  		copy(m.store[offset:offset+size], value)
    46  	}
    47  }
    48  
    49  // Set32 sets the 32 bytes starting at offset to the value of val, left-padded with zeroes to
    50  // 32 bytes.
    51  func (m *Memory) Set32(offset uint64, val *uint256.Int) {
    52  	// length of store may never be less than offset + size.
    53  	// The store should be resized PRIOR to setting the memory
    54  	if offset+32 > uint64(len(m.store)) {
    55  		panic("invalid memory: store empty")
    56  	}
    57  	// Zero the memory area
    58  	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})
    59  	// Fill in relevant bits
    60  	val.WriteToSlice(m.store[offset:])
    61  }
    62  
    63  // Resize resizes the memory to size
    64  func (m *Memory) Resize(size uint64) {
    65  	if uint64(m.Len()) < size {
    66  		m.store = append(m.store, make([]byte, size-uint64(m.Len()))...)
    67  	}
    68  }
    69  
    70  // Get returns offset + size as a new slice
    71  func (self *Memory) Get(offset, size int64) (cpy []byte) {
    72  	if size == 0 {
    73  		return nil
    74  	}
    75  
    76  	if len(self.store) > int(offset) {
    77  		cpy = make([]byte, size)
    78  		copy(cpy, self.store[offset:offset+size])
    79  
    80  		return
    81  	}
    82  
    83  	return
    84  }
    85  
    86  // Get returns offset + size as a new slice
    87  func (m *Memory) GetCopy(offset, size int64) (cpy []byte) {
    88  	if size == 0 {
    89  		return nil
    90  	}
    91  
    92  	if len(m.store) > int(offset) {
    93  		cpy = make([]byte, size)
    94  		copy(cpy, m.store[offset:offset+size])
    95  
    96  		return
    97  	}
    98  
    99  	return
   100  }
   101  
   102  // GetPtr returns the offset + size
   103  func (self *Memory) GetPtr(offset, size int64) []byte {
   104  	if size == 0 {
   105  		return nil
   106  	}
   107  
   108  	if len(self.store) > int(offset) {
   109  		return self.store[offset : offset+size]
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  // Len returns the length of the backing slice
   116  func (m *Memory) Len() int {
   117  	return len(m.store)
   118  }
   119  
   120  // Data returns the backing slice
   121  func (m *Memory) Data() []byte {
   122  	return m.store
   123  }
   124  
   125  func (m *Memory) Print() {
   126  	fmt.Printf("### mem %d bytes ###\n", len(m.store))
   127  	if len(m.store) > 0 {
   128  		addr := 0
   129  		for i := 0; i+32 <= len(m.store); i += 32 {
   130  			fmt.Printf("%03d: % x\n", addr, m.store[i:i+32])
   131  			addr++
   132  		}
   133  	} else {
   134  		fmt.Println("-- empty --")
   135  	}
   136  	fmt.Println("####################")
   137  }