contract.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. "math/big"
  19. lru "github.com/hashicorp/golang-lru"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/metrics"
  22. "github.com/holiman/uint256"
  23. )
  24. const codeBitmapCacheSize = 2000
  25. var (
  26. codeBitmapCache, _ = lru.New(codeBitmapCacheSize)
  27. contractCodeBitmapHitMeter = metrics.NewRegisteredMeter("vm/contract/code/bitmap/hit", nil)
  28. contractCodeBitmapMissMeter = metrics.NewRegisteredMeter("vm/contract/code/bitmap/miss", nil)
  29. )
  30. // ContractRef is a reference to the contract's backing object
  31. type ContractRef interface {
  32. Address() common.Address
  33. }
  34. // AccountRef implements ContractRef.
  35. //
  36. // Account references are used during EVM initialisation and
  37. // it's primary use is to fetch addresses. Removing this object
  38. // proves difficult because of the cached jump destinations which
  39. // are fetched from the parent contract (i.e. the caller), which
  40. // is a ContractRef.
  41. type AccountRef common.Address
  42. // Address casts AccountRef to a Address
  43. func (ar AccountRef) Address() common.Address { return (common.Address)(ar) }
  44. // Contract represents an ethereum contract in the state database. It contains
  45. // the contract code, calling arguments. Contract implements ContractRef
  46. type Contract struct {
  47. // CallerAddress is the result of the caller which initialised this
  48. // contract. However when the "call method" is delegated this value
  49. // needs to be initialised to that of the caller's caller.
  50. CallerAddress common.Address
  51. caller ContractRef
  52. self ContractRef
  53. jumpdests map[common.Hash]bitvec // Aggregated result of JUMPDEST analysis.
  54. analysis bitvec // Locally cached result of JUMPDEST analysis
  55. Code []byte
  56. CodeHash common.Hash
  57. CodeAddr *common.Address
  58. Input []byte
  59. Gas uint64
  60. value *big.Int
  61. }
  62. // NewContract returns a new contract environment for the execution of EVM.
  63. func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract {
  64. c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object}
  65. if parent, ok := caller.(*Contract); ok {
  66. // Reuse JUMPDEST analysis from parent context if available.
  67. c.jumpdests = parent.jumpdests
  68. } else {
  69. c.jumpdests = make(map[common.Hash]bitvec)
  70. }
  71. // Gas should be a pointer so it can safely be reduced through the run
  72. // This pointer will be off the state transition
  73. c.Gas = gas
  74. // ensures a value is set
  75. c.value = value
  76. return c
  77. }
  78. func (c *Contract) validJumpdest(dest *uint256.Int) bool {
  79. udest, overflow := dest.Uint64WithOverflow()
  80. // PC cannot go beyond len(code) and certainly can't be bigger than 63bits.
  81. // Don't bother checking for JUMPDEST in that case.
  82. if overflow || udest >= uint64(len(c.Code)) {
  83. return false
  84. }
  85. // Only JUMPDESTs allowed for destinations
  86. if OpCode(c.Code[udest]) != JUMPDEST {
  87. return false
  88. }
  89. return c.isCode(udest)
  90. }
  91. // isCode returns true if the provided PC location is an actual opcode, as
  92. // opposed to a data-segment following a PUSHN operation.
  93. func (c *Contract) isCode(udest uint64) bool {
  94. // Do we already have an analysis laying around?
  95. if c.analysis != nil {
  96. return c.analysis.codeSegment(udest)
  97. }
  98. // Do we have a contract hash already?
  99. // If we do have a hash, that means it's a 'regular' contract. For regular
  100. // contracts ( not temporary initcode), we store the analysis in a map
  101. if c.CodeHash != (common.Hash{}) {
  102. // Does parent context have the analysis?
  103. analysis, exist := c.jumpdests[c.CodeHash]
  104. if !exist {
  105. if cached, ok := codeBitmapCache.Get(c.CodeHash); ok {
  106. contractCodeBitmapHitMeter.Mark(1)
  107. analysis = cached.(bitvec)
  108. } else {
  109. // Do the analysis and save in parent context
  110. // We do not need to store it in c.analysis
  111. analysis = codeBitmap(c.Code)
  112. c.jumpdests[c.CodeHash] = analysis
  113. contractCodeBitmapMissMeter.Mark(1)
  114. codeBitmapCache.Add(c.CodeHash, analysis)
  115. }
  116. }
  117. // Also stash it in current contract for faster access
  118. c.analysis = analysis
  119. return analysis.codeSegment(udest)
  120. }
  121. // We don't have the code hash, most likely a piece of initcode not already
  122. // in state trie. In that case, we do an analysis, and save it locally, so
  123. // we don't have to recalculate it for every JUMP instruction in the execution
  124. // However, we don't save it within the parent context
  125. if c.analysis == nil {
  126. c.analysis = codeBitmap(c.Code)
  127. }
  128. return c.analysis.codeSegment(udest)
  129. }
  130. // AsDelegate sets the contract to be a delegate call and returns the current
  131. // contract (for chaining calls)
  132. func (c *Contract) AsDelegate() *Contract {
  133. // NOTE: caller must, at all times be a contract. It should never happen
  134. // that caller is something other than a Contract.
  135. parent := c.caller.(*Contract)
  136. c.CallerAddress = parent.CallerAddress
  137. c.value = parent.value
  138. return c
  139. }
  140. // GetOp returns the n'th element in the contract's byte array
  141. func (c *Contract) GetOp(n uint64) OpCode {
  142. return OpCode(c.GetByte(n))
  143. }
  144. // GetByte returns the n'th byte in the contract's byte array
  145. func (c *Contract) GetByte(n uint64) byte {
  146. if n < uint64(len(c.Code)) {
  147. return c.Code[n]
  148. }
  149. return 0
  150. }
  151. // Caller returns the caller of the contract.
  152. //
  153. // Caller will recursively call caller when the contract is a delegate
  154. // call, including that of caller's caller.
  155. func (c *Contract) Caller() common.Address {
  156. return c.CallerAddress
  157. }
  158. // UseGas attempts the use gas and subtracts it and returns true on success
  159. func (c *Contract) UseGas(gas uint64) (ok bool) {
  160. if c.Gas < gas {
  161. return false
  162. }
  163. c.Gas -= gas
  164. return true
  165. }
  166. // Address returns the contracts address
  167. func (c *Contract) Address() common.Address {
  168. return c.self.Address()
  169. }
  170. // Value returns the contract's value (sent to it from it's caller)
  171. func (c *Contract) Value() *big.Int {
  172. return c.value
  173. }
  174. // SetCallCode sets the code of the contract and address of the backing data
  175. // object
  176. func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
  177. c.Code = code
  178. c.CodeHash = hash
  179. c.CodeAddr = addr
  180. }
  181. // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
  182. // In case hash is not provided, the jumpdest analysis will not be saved to the parent context
  183. func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) {
  184. c.Code = codeAndHash.code
  185. c.CodeHash = codeAndHash.hash
  186. c.CodeAddr = addr
  187. }