memory.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package vm
  17. import (
  18. "fmt"
  19. "github.com/holiman/uint256"
  20. )
  21. // Memory implements a simple memory model for the ethereum virtual machine.
  22. type Memory struct {
  23. store []byte
  24. lastGasCost uint64
  25. }
  26. // NewMemory returns a new memory model.
  27. func NewMemory() *Memory {
  28. return &Memory{}
  29. }
  30. // Set sets offset + size to value
  31. func (m *Memory) Set(offset, size uint64, value []byte) {
  32. // It's possible the offset is greater than 0 and size equals 0. This is because
  33. // the calcMemSize (common.go) could potentially return 0 when size is zero (NO-OP)
  34. if size > 0 {
  35. // length of store may never be less than offset + size.
  36. // The store should be resized PRIOR to setting the memory
  37. if offset+size > uint64(len(m.store)) {
  38. panic("invalid memory: store empty")
  39. }
  40. copy(m.store[offset:offset+size], value)
  41. }
  42. }
  43. // Set32 sets the 32 bytes starting at offset to the value of val, left-padded with zeroes to
  44. // 32 bytes.
  45. func (m *Memory) Set32(offset uint64, val *uint256.Int) {
  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+32 > uint64(len(m.store)) {
  49. panic("invalid memory: store empty")
  50. }
  51. // Zero the memory area
  52. 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})
  53. // Fill in relevant bits
  54. val.WriteToSlice(m.store[offset:])
  55. }
  56. // Resize resizes the memory to size
  57. func (m *Memory) Resize(size uint64) {
  58. if uint64(m.Len()) < size {
  59. m.store = append(m.store, make([]byte, size-uint64(m.Len()))...)
  60. }
  61. }
  62. // Get returns offset + size as a new slice
  63. func (m *Memory) GetCopy(offset, size int64) (cpy []byte) {
  64. if size == 0 {
  65. return nil
  66. }
  67. if len(m.store) > int(offset) {
  68. cpy = make([]byte, size)
  69. copy(cpy, m.store[offset:offset+size])
  70. return
  71. }
  72. return
  73. }
  74. // GetPtr returns the offset + size
  75. func (m *Memory) GetPtr(offset, size int64) []byte {
  76. if size == 0 {
  77. return nil
  78. }
  79. if len(m.store) > int(offset) {
  80. return m.store[offset : offset+size]
  81. }
  82. return nil
  83. }
  84. // Len returns the length of the backing slice
  85. func (m *Memory) Len() int {
  86. return len(m.store)
  87. }
  88. // Data returns the backing slice
  89. func (m *Memory) Data() []byte {
  90. return m.store
  91. }
  92. // Print dumps the content of the memory.
  93. func (m *Memory) Print() {
  94. fmt.Printf("### mem %d bytes ###\n", len(m.store))
  95. if len(m.store) > 0 {
  96. addr := 0
  97. for i := 0; i+32 <= len(m.store); i += 32 {
  98. fmt.Printf("%03d: % x\n", addr, m.store[i:i+32])
  99. addr++
  100. }
  101. } else {
  102. fmt.Println("-- empty --")
  103. }
  104. fmt.Println("####################")
  105. }