instructions.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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. "errors"
  19. "fmt"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/common/math"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. var (
  28. bigZero = new(big.Int)
  29. tt255 = math.BigPow(2, 255)
  30. errWriteProtection = errors.New("evm: write protection")
  31. errReturnDataOutOfBounds = errors.New("evm: return data out of bounds")
  32. errExecutionReverted = errors.New("evm: execution reverted")
  33. errMaxCodeSizeExceeded = errors.New("evm: max code size exceeded")
  34. )
  35. func opAdd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  36. x, y := stack.pop(), stack.peek()
  37. math.U256(y.Add(x, y))
  38. interpreter.intPool.put(x)
  39. return nil, nil
  40. }
  41. func opSub(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  42. x, y := stack.pop(), stack.peek()
  43. math.U256(y.Sub(x, y))
  44. interpreter.intPool.put(x)
  45. return nil, nil
  46. }
  47. func opMul(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  48. x, y := stack.pop(), stack.pop()
  49. stack.push(math.U256(x.Mul(x, y)))
  50. interpreter.intPool.put(y)
  51. return nil, nil
  52. }
  53. func opDiv(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  54. x, y := stack.pop(), stack.peek()
  55. if y.Sign() != 0 {
  56. math.U256(y.Div(x, y))
  57. } else {
  58. y.SetUint64(0)
  59. }
  60. interpreter.intPool.put(x)
  61. return nil, nil
  62. }
  63. func opSdiv(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  64. x, y := math.S256(stack.pop()), math.S256(stack.pop())
  65. res := interpreter.intPool.getZero()
  66. if y.Sign() == 0 || x.Sign() == 0 {
  67. stack.push(res)
  68. } else {
  69. if x.Sign() != y.Sign() {
  70. res.Div(x.Abs(x), y.Abs(y))
  71. res.Neg(res)
  72. } else {
  73. res.Div(x.Abs(x), y.Abs(y))
  74. }
  75. stack.push(math.U256(res))
  76. }
  77. interpreter.intPool.put(x, y)
  78. return nil, nil
  79. }
  80. func opMod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  81. x, y := stack.pop(), stack.pop()
  82. if y.Sign() == 0 {
  83. stack.push(x.SetUint64(0))
  84. } else {
  85. stack.push(math.U256(x.Mod(x, y)))
  86. }
  87. interpreter.intPool.put(y)
  88. return nil, nil
  89. }
  90. func opSmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  91. x, y := math.S256(stack.pop()), math.S256(stack.pop())
  92. res := interpreter.intPool.getZero()
  93. if y.Sign() == 0 {
  94. stack.push(res)
  95. } else {
  96. if x.Sign() < 0 {
  97. res.Mod(x.Abs(x), y.Abs(y))
  98. res.Neg(res)
  99. } else {
  100. res.Mod(x.Abs(x), y.Abs(y))
  101. }
  102. stack.push(math.U256(res))
  103. }
  104. interpreter.intPool.put(x, y)
  105. return nil, nil
  106. }
  107. func opExp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  108. base, exponent := stack.pop(), stack.pop()
  109. stack.push(math.Exp(base, exponent))
  110. interpreter.intPool.put(base, exponent)
  111. return nil, nil
  112. }
  113. func opSignExtend(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  114. back := stack.pop()
  115. if back.Cmp(big.NewInt(31)) < 0 {
  116. bit := uint(back.Uint64()*8 + 7)
  117. num := stack.pop()
  118. mask := back.Lsh(common.Big1, bit)
  119. mask.Sub(mask, common.Big1)
  120. if num.Bit(int(bit)) > 0 {
  121. num.Or(num, mask.Not(mask))
  122. } else {
  123. num.And(num, mask)
  124. }
  125. stack.push(math.U256(num))
  126. }
  127. interpreter.intPool.put(back)
  128. return nil, nil
  129. }
  130. func opNot(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  131. x := stack.peek()
  132. math.U256(x.Not(x))
  133. return nil, nil
  134. }
  135. func opLt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  136. x, y := stack.pop(), stack.peek()
  137. if x.Cmp(y) < 0 {
  138. y.SetUint64(1)
  139. } else {
  140. y.SetUint64(0)
  141. }
  142. interpreter.intPool.put(x)
  143. return nil, nil
  144. }
  145. func opGt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  146. x, y := stack.pop(), stack.peek()
  147. if x.Cmp(y) > 0 {
  148. y.SetUint64(1)
  149. } else {
  150. y.SetUint64(0)
  151. }
  152. interpreter.intPool.put(x)
  153. return nil, nil
  154. }
  155. func opSlt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  156. x, y := stack.pop(), stack.peek()
  157. xSign := x.Cmp(tt255)
  158. ySign := y.Cmp(tt255)
  159. switch {
  160. case xSign >= 0 && ySign < 0:
  161. y.SetUint64(1)
  162. case xSign < 0 && ySign >= 0:
  163. y.SetUint64(0)
  164. default:
  165. if x.Cmp(y) < 0 {
  166. y.SetUint64(1)
  167. } else {
  168. y.SetUint64(0)
  169. }
  170. }
  171. interpreter.intPool.put(x)
  172. return nil, nil
  173. }
  174. func opSgt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  175. x, y := stack.pop(), stack.peek()
  176. xSign := x.Cmp(tt255)
  177. ySign := y.Cmp(tt255)
  178. switch {
  179. case xSign >= 0 && ySign < 0:
  180. y.SetUint64(0)
  181. case xSign < 0 && ySign >= 0:
  182. y.SetUint64(1)
  183. default:
  184. if x.Cmp(y) > 0 {
  185. y.SetUint64(1)
  186. } else {
  187. y.SetUint64(0)
  188. }
  189. }
  190. interpreter.intPool.put(x)
  191. return nil, nil
  192. }
  193. func opEq(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  194. x, y := stack.pop(), stack.peek()
  195. if x.Cmp(y) == 0 {
  196. y.SetUint64(1)
  197. } else {
  198. y.SetUint64(0)
  199. }
  200. interpreter.intPool.put(x)
  201. return nil, nil
  202. }
  203. func opIszero(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  204. x := stack.peek()
  205. if x.Sign() > 0 {
  206. x.SetUint64(0)
  207. } else {
  208. x.SetUint64(1)
  209. }
  210. return nil, nil
  211. }
  212. func opAnd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  213. x, y := stack.pop(), stack.pop()
  214. stack.push(x.And(x, y))
  215. interpreter.intPool.put(y)
  216. return nil, nil
  217. }
  218. func opOr(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  219. x, y := stack.pop(), stack.peek()
  220. y.Or(x, y)
  221. interpreter.intPool.put(x)
  222. return nil, nil
  223. }
  224. func opXor(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  225. x, y := stack.pop(), stack.peek()
  226. y.Xor(x, y)
  227. interpreter.intPool.put(x)
  228. return nil, nil
  229. }
  230. func opByte(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  231. th, val := stack.pop(), stack.peek()
  232. if th.Cmp(common.Big32) < 0 {
  233. b := math.Byte(val, 32, int(th.Int64()))
  234. val.SetUint64(uint64(b))
  235. } else {
  236. val.SetUint64(0)
  237. }
  238. interpreter.intPool.put(th)
  239. return nil, nil
  240. }
  241. func opAddmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  242. x, y, z := stack.pop(), stack.pop(), stack.pop()
  243. if z.Cmp(bigZero) > 0 {
  244. x.Add(x, y)
  245. x.Mod(x, z)
  246. stack.push(math.U256(x))
  247. } else {
  248. stack.push(x.SetUint64(0))
  249. }
  250. interpreter.intPool.put(y, z)
  251. return nil, nil
  252. }
  253. func opMulmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  254. x, y, z := stack.pop(), stack.pop(), stack.pop()
  255. if z.Cmp(bigZero) > 0 {
  256. x.Mul(x, y)
  257. x.Mod(x, z)
  258. stack.push(math.U256(x))
  259. } else {
  260. stack.push(x.SetUint64(0))
  261. }
  262. interpreter.intPool.put(y, z)
  263. return nil, nil
  264. }
  265. // opSHL implements Shift Left
  266. // The SHL instruction (shift left) pops 2 values from the stack, first arg1 and then arg2,
  267. // and pushes on the stack arg2 shifted to the left by arg1 number of bits.
  268. func opSHL(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  269. // Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
  270. shift, value := math.U256(stack.pop()), math.U256(stack.peek())
  271. defer interpreter.intPool.put(shift) // First operand back into the pool
  272. if shift.Cmp(common.Big256) >= 0 {
  273. value.SetUint64(0)
  274. return nil, nil
  275. }
  276. n := uint(shift.Uint64())
  277. math.U256(value.Lsh(value, n))
  278. return nil, nil
  279. }
  280. // opSHR implements Logical Shift Right
  281. // The SHR instruction (logical shift right) pops 2 values from the stack, first arg1 and then arg2,
  282. // and pushes on the stack arg2 shifted to the right by arg1 number of bits with zero fill.
  283. func opSHR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  284. // Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
  285. shift, value := math.U256(stack.pop()), math.U256(stack.peek())
  286. defer interpreter.intPool.put(shift) // First operand back into the pool
  287. if shift.Cmp(common.Big256) >= 0 {
  288. value.SetUint64(0)
  289. return nil, nil
  290. }
  291. n := uint(shift.Uint64())
  292. math.U256(value.Rsh(value, n))
  293. return nil, nil
  294. }
  295. // opSAR implements Arithmetic Shift Right
  296. // The SAR instruction (arithmetic shift right) pops 2 values from the stack, first arg1 and then arg2,
  297. // and pushes on the stack arg2 shifted to the right by arg1 number of bits with sign extension.
  298. func opSAR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  299. // Note, S256 returns (potentially) a new bigint, so we're popping, not peeking this one
  300. shift, value := math.U256(stack.pop()), math.S256(stack.pop())
  301. defer interpreter.intPool.put(shift) // First operand back into the pool
  302. if shift.Cmp(common.Big256) >= 0 {
  303. if value.Sign() >= 0 {
  304. value.SetUint64(0)
  305. } else {
  306. value.SetInt64(-1)
  307. }
  308. stack.push(math.U256(value))
  309. return nil, nil
  310. }
  311. n := uint(shift.Uint64())
  312. value.Rsh(value, n)
  313. stack.push(math.U256(value))
  314. return nil, nil
  315. }
  316. func opSha3(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  317. offset, size := stack.pop(), stack.pop()
  318. data := memory.Get(offset.Int64(), size.Int64())
  319. hash := crypto.Keccak256(data)
  320. evm := interpreter.evm
  321. if evm.vmConfig.EnablePreimageRecording {
  322. evm.StateDB.AddPreimage(common.BytesToHash(hash), data)
  323. }
  324. stack.push(interpreter.intPool.get().SetBytes(hash))
  325. interpreter.intPool.put(offset, size)
  326. return nil, nil
  327. }
  328. func opAddress(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  329. stack.push(contract.Address().Big())
  330. return nil, nil
  331. }
  332. func opBalance(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  333. slot := stack.peek()
  334. slot.Set(interpreter.evm.StateDB.GetBalance(common.BigToAddress(slot)))
  335. return nil, nil
  336. }
  337. func opOrigin(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  338. stack.push(interpreter.evm.Origin.Big())
  339. return nil, nil
  340. }
  341. func opCaller(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  342. stack.push(contract.Caller().Big())
  343. return nil, nil
  344. }
  345. func opCallValue(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  346. stack.push(interpreter.intPool.get().Set(contract.value))
  347. return nil, nil
  348. }
  349. func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  350. stack.push(interpreter.intPool.get().SetBytes(getDataBig(contract.Input, stack.pop(), big32)))
  351. return nil, nil
  352. }
  353. func opCallDataSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  354. stack.push(interpreter.intPool.get().SetInt64(int64(len(contract.Input))))
  355. return nil, nil
  356. }
  357. func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  358. var (
  359. memOffset = stack.pop()
  360. dataOffset = stack.pop()
  361. length = stack.pop()
  362. )
  363. memory.Set(memOffset.Uint64(), length.Uint64(), getDataBig(contract.Input, dataOffset, length))
  364. interpreter.intPool.put(memOffset, dataOffset, length)
  365. return nil, nil
  366. }
  367. func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  368. stack.push(interpreter.intPool.get().SetUint64(uint64(len(interpreter.returnData))))
  369. return nil, nil
  370. }
  371. func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  372. var (
  373. memOffset = stack.pop()
  374. dataOffset = stack.pop()
  375. length = stack.pop()
  376. end = interpreter.intPool.get().Add(dataOffset, length)
  377. )
  378. defer interpreter.intPool.put(memOffset, dataOffset, length, end)
  379. if end.BitLen() > 64 || uint64(len(interpreter.returnData)) < end.Uint64() {
  380. return nil, errReturnDataOutOfBounds
  381. }
  382. memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[dataOffset.Uint64():end.Uint64()])
  383. return nil, nil
  384. }
  385. func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  386. slot := stack.peek()
  387. slot.SetUint64(uint64(interpreter.evm.StateDB.GetCodeSize(common.BigToAddress(slot))))
  388. return nil, nil
  389. }
  390. func opCodeSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  391. l := interpreter.intPool.get().SetInt64(int64(len(contract.Code)))
  392. stack.push(l)
  393. return nil, nil
  394. }
  395. func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  396. var (
  397. memOffset = stack.pop()
  398. codeOffset = stack.pop()
  399. length = stack.pop()
  400. )
  401. codeCopy := getDataBig(contract.Code, codeOffset, length)
  402. memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
  403. interpreter.intPool.put(memOffset, codeOffset, length)
  404. return nil, nil
  405. }
  406. func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  407. var (
  408. addr = common.BigToAddress(stack.pop())
  409. memOffset = stack.pop()
  410. codeOffset = stack.pop()
  411. length = stack.pop()
  412. )
  413. codeCopy := getDataBig(interpreter.evm.StateDB.GetCode(addr), codeOffset, length)
  414. memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
  415. interpreter.intPool.put(memOffset, codeOffset, length)
  416. return nil, nil
  417. }
  418. // opExtCodeHash returns the code hash of a specified account.
  419. // There are several cases when the function is called, while we can relay everything
  420. // to `state.GetCodeHash` function to ensure the correctness.
  421. // (1) Caller tries to get the code hash of a normal contract account, state
  422. // should return the relative code hash and set it as the result.
  423. //
  424. // (2) Caller tries to get the code hash of a non-existent account, state should
  425. // return common.Hash{} and zero will be set as the result.
  426. //
  427. // (3) Caller tries to get the code hash for an account without contract code,
  428. // state should return emptyCodeHash(0xc5d246...) as the result.
  429. //
  430. // (4) Caller tries to get the code hash of a precompiled account, the result
  431. // should be zero or emptyCodeHash.
  432. //
  433. // It is worth noting that in order to avoid unnecessary create and clean,
  434. // all precompile accounts on mainnet have been transferred 1 wei, so the return
  435. // here should be emptyCodeHash.
  436. // If the precompile account is not transferred any amount on a private or
  437. // customized chain, the return value will be zero.
  438. //
  439. // (5) Caller tries to get the code hash for an account which is marked as suicided
  440. // in the current transaction, the code hash of this account should be returned.
  441. //
  442. // (6) Caller tries to get the code hash for an account which is marked as deleted,
  443. // this account should be regarded as a non-existent account and zero should be returned.
  444. func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  445. slot := stack.peek()
  446. slot.SetBytes(interpreter.evm.StateDB.GetCodeHash(common.BigToAddress(slot)).Bytes())
  447. return nil, nil
  448. }
  449. func opGasprice(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  450. stack.push(interpreter.intPool.get().Set(interpreter.evm.GasPrice))
  451. return nil, nil
  452. }
  453. func opBlockhash(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  454. num := stack.pop()
  455. n := interpreter.intPool.get().Sub(interpreter.evm.BlockNumber, common.Big257)
  456. if num.Cmp(n) > 0 && num.Cmp(interpreter.evm.BlockNumber) < 0 {
  457. stack.push(interpreter.evm.GetHash(num.Uint64()).Big())
  458. } else {
  459. stack.push(interpreter.intPool.getZero())
  460. }
  461. interpreter.intPool.put(num, n)
  462. return nil, nil
  463. }
  464. func opCoinbase(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  465. stack.push(interpreter.evm.Coinbase.Big())
  466. return nil, nil
  467. }
  468. func opTimestamp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  469. stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.Time)))
  470. return nil, nil
  471. }
  472. func opNumber(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  473. stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.BlockNumber)))
  474. return nil, nil
  475. }
  476. func opDifficulty(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  477. stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.Difficulty)))
  478. return nil, nil
  479. }
  480. func opGasLimit(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  481. stack.push(math.U256(interpreter.intPool.get().SetUint64(interpreter.evm.GasLimit)))
  482. return nil, nil
  483. }
  484. func opPop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  485. interpreter.intPool.put(stack.pop())
  486. return nil, nil
  487. }
  488. func opMload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  489. offset := stack.pop()
  490. val := interpreter.intPool.get().SetBytes(memory.Get(offset.Int64(), 32))
  491. stack.push(val)
  492. interpreter.intPool.put(offset)
  493. return nil, nil
  494. }
  495. func opMstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  496. // pop value of the stack
  497. mStart, val := stack.pop(), stack.pop()
  498. memory.Set32(mStart.Uint64(), val)
  499. interpreter.intPool.put(mStart, val)
  500. return nil, nil
  501. }
  502. func opMstore8(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  503. off, val := stack.pop().Int64(), stack.pop().Int64()
  504. memory.store[off] = byte(val & 0xff)
  505. return nil, nil
  506. }
  507. func opSload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  508. loc := stack.peek()
  509. val := interpreter.evm.StateDB.GetState(contract.Address(), common.BigToHash(loc))
  510. loc.SetBytes(val.Bytes())
  511. return nil, nil
  512. }
  513. func opSstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  514. loc := common.BigToHash(stack.pop())
  515. val := stack.pop()
  516. interpreter.evm.StateDB.SetState(contract.Address(), loc, common.BigToHash(val))
  517. interpreter.intPool.put(val)
  518. return nil, nil
  519. }
  520. func opJump(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  521. pos := stack.pop()
  522. if !contract.validJumpdest(pos) {
  523. nop := contract.GetOp(pos.Uint64())
  524. return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
  525. }
  526. *pc = pos.Uint64()
  527. interpreter.intPool.put(pos)
  528. return nil, nil
  529. }
  530. func opJumpi(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  531. pos, cond := stack.pop(), stack.pop()
  532. if cond.Sign() != 0 {
  533. if !contract.validJumpdest(pos) {
  534. nop := contract.GetOp(pos.Uint64())
  535. return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
  536. }
  537. *pc = pos.Uint64()
  538. } else {
  539. *pc++
  540. }
  541. interpreter.intPool.put(pos, cond)
  542. return nil, nil
  543. }
  544. func opJumpdest(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  545. return nil, nil
  546. }
  547. func opPc(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  548. stack.push(interpreter.intPool.get().SetUint64(*pc))
  549. return nil, nil
  550. }
  551. func opMsize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  552. stack.push(interpreter.intPool.get().SetInt64(int64(memory.Len())))
  553. return nil, nil
  554. }
  555. func opGas(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  556. stack.push(interpreter.intPool.get().SetUint64(contract.Gas))
  557. return nil, nil
  558. }
  559. func opCreate(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  560. var (
  561. value = stack.pop()
  562. offset, size = stack.pop(), stack.pop()
  563. input = memory.Get(offset.Int64(), size.Int64())
  564. gas = contract.Gas
  565. )
  566. if interpreter.evm.ChainConfig().IsEIP150(interpreter.evm.BlockNumber) {
  567. gas -= gas / 64
  568. }
  569. contract.UseGas(gas)
  570. res, addr, returnGas, suberr := interpreter.evm.Create(contract, input, gas, value)
  571. // Push item on the stack based on the returned error. If the ruleset is
  572. // homestead we must check for CodeStoreOutOfGasError (homestead only
  573. // rule) and treat as an error, if the ruleset is frontier we must
  574. // ignore this error and pretend the operation was successful.
  575. if interpreter.evm.ChainConfig().IsHomestead(interpreter.evm.BlockNumber) && suberr == ErrCodeStoreOutOfGas {
  576. stack.push(interpreter.intPool.getZero())
  577. } else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
  578. stack.push(interpreter.intPool.getZero())
  579. } else {
  580. stack.push(addr.Big())
  581. }
  582. contract.Gas += returnGas
  583. interpreter.intPool.put(value, offset, size)
  584. if suberr == errExecutionReverted {
  585. return res, nil
  586. }
  587. return nil, nil
  588. }
  589. func opCreate2(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  590. var (
  591. endowment = stack.pop()
  592. offset, size = stack.pop(), stack.pop()
  593. salt = stack.pop()
  594. input = memory.Get(offset.Int64(), size.Int64())
  595. gas = contract.Gas
  596. )
  597. // Apply EIP150
  598. gas -= gas / 64
  599. contract.UseGas(gas)
  600. res, addr, returnGas, suberr := interpreter.evm.Create2(contract, input, gas, endowment, salt)
  601. // Push item on the stack based on the returned error.
  602. if suberr != nil {
  603. stack.push(interpreter.intPool.getZero())
  604. } else {
  605. stack.push(addr.Big())
  606. }
  607. contract.Gas += returnGas
  608. interpreter.intPool.put(endowment, offset, size, salt)
  609. if suberr == errExecutionReverted {
  610. return res, nil
  611. }
  612. return nil, nil
  613. }
  614. func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  615. // Pop gas. The actual gas in interpreter.evm.callGasTemp.
  616. interpreter.intPool.put(stack.pop())
  617. gas := interpreter.evm.callGasTemp
  618. // Pop other call parameters.
  619. addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  620. toAddr := common.BigToAddress(addr)
  621. value = math.U256(value)
  622. // Get the arguments from the memory.
  623. args := memory.Get(inOffset.Int64(), inSize.Int64())
  624. if value.Sign() != 0 {
  625. gas += params.CallStipend
  626. }
  627. ret, returnGas, err := interpreter.evm.Call(contract, toAddr, args, gas, value)
  628. if err != nil {
  629. stack.push(interpreter.intPool.getZero())
  630. } else {
  631. stack.push(interpreter.intPool.get().SetUint64(1))
  632. }
  633. if err == nil || err == errExecutionReverted {
  634. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  635. }
  636. contract.Gas += returnGas
  637. interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
  638. return ret, nil
  639. }
  640. func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  641. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  642. interpreter.intPool.put(stack.pop())
  643. gas := interpreter.evm.callGasTemp
  644. // Pop other call parameters.
  645. addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  646. toAddr := common.BigToAddress(addr)
  647. value = math.U256(value)
  648. // Get arguments from the memory.
  649. args := memory.Get(inOffset.Int64(), inSize.Int64())
  650. if value.Sign() != 0 {
  651. gas += params.CallStipend
  652. }
  653. ret, returnGas, err := interpreter.evm.CallCode(contract, toAddr, args, gas, value)
  654. if err != nil {
  655. stack.push(interpreter.intPool.getZero())
  656. } else {
  657. stack.push(interpreter.intPool.get().SetUint64(1))
  658. }
  659. if err == nil || err == errExecutionReverted {
  660. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  661. }
  662. contract.Gas += returnGas
  663. interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
  664. return ret, nil
  665. }
  666. func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  667. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  668. interpreter.intPool.put(stack.pop())
  669. gas := interpreter.evm.callGasTemp
  670. // Pop other call parameters.
  671. addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  672. toAddr := common.BigToAddress(addr)
  673. // Get arguments from the memory.
  674. args := memory.Get(inOffset.Int64(), inSize.Int64())
  675. ret, returnGas, err := interpreter.evm.DelegateCall(contract, toAddr, args, gas)
  676. if err != nil {
  677. stack.push(interpreter.intPool.getZero())
  678. } else {
  679. stack.push(interpreter.intPool.get().SetUint64(1))
  680. }
  681. if err == nil || err == errExecutionReverted {
  682. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  683. }
  684. contract.Gas += returnGas
  685. interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
  686. return ret, nil
  687. }
  688. func opStaticCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  689. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  690. interpreter.intPool.put(stack.pop())
  691. gas := interpreter.evm.callGasTemp
  692. // Pop other call parameters.
  693. addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  694. toAddr := common.BigToAddress(addr)
  695. // Get arguments from the memory.
  696. args := memory.Get(inOffset.Int64(), inSize.Int64())
  697. ret, returnGas, err := interpreter.evm.StaticCall(contract, toAddr, args, gas)
  698. if err != nil {
  699. stack.push(interpreter.intPool.getZero())
  700. } else {
  701. stack.push(interpreter.intPool.get().SetUint64(1))
  702. }
  703. if err == nil || err == errExecutionReverted {
  704. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  705. }
  706. contract.Gas += returnGas
  707. interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
  708. return ret, nil
  709. }
  710. func opReturn(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  711. offset, size := stack.pop(), stack.pop()
  712. ret := memory.GetPtr(offset.Int64(), size.Int64())
  713. interpreter.intPool.put(offset, size)
  714. return ret, nil
  715. }
  716. func opRevert(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  717. offset, size := stack.pop(), stack.pop()
  718. ret := memory.GetPtr(offset.Int64(), size.Int64())
  719. interpreter.intPool.put(offset, size)
  720. return ret, nil
  721. }
  722. func opStop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  723. return nil, nil
  724. }
  725. func opSuicide(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  726. balance := interpreter.evm.StateDB.GetBalance(contract.Address())
  727. interpreter.evm.StateDB.AddBalance(common.BigToAddress(stack.pop()), balance)
  728. interpreter.evm.StateDB.Suicide(contract.Address())
  729. return nil, nil
  730. }
  731. // following functions are used by the instruction jump table
  732. // make log instruction function
  733. func makeLog(size int) executionFunc {
  734. return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  735. topics := make([]common.Hash, size)
  736. mStart, mSize := stack.pop(), stack.pop()
  737. for i := 0; i < size; i++ {
  738. topics[i] = common.BigToHash(stack.pop())
  739. }
  740. d := memory.Get(mStart.Int64(), mSize.Int64())
  741. interpreter.evm.StateDB.AddLog(&types.Log{
  742. Address: contract.Address(),
  743. Topics: topics,
  744. Data: d,
  745. // This is a non-consensus field, but assigned here because
  746. // core/state doesn't know the current block number.
  747. BlockNumber: interpreter.evm.BlockNumber.Uint64(),
  748. })
  749. interpreter.intPool.put(mStart, mSize)
  750. return nil, nil
  751. }
  752. }
  753. // make push instruction function
  754. func makePush(size uint64, pushByteSize int) executionFunc {
  755. return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  756. codeLen := len(contract.Code)
  757. startMin := codeLen
  758. if int(*pc+1) < startMin {
  759. startMin = int(*pc + 1)
  760. }
  761. endMin := codeLen
  762. if startMin+pushByteSize < endMin {
  763. endMin = startMin + pushByteSize
  764. }
  765. integer := interpreter.intPool.get()
  766. stack.push(integer.SetBytes(common.RightPadBytes(contract.Code[startMin:endMin], pushByteSize)))
  767. *pc += size
  768. return nil, nil
  769. }
  770. }
  771. // make dup instruction function
  772. func makeDup(size int64) executionFunc {
  773. return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  774. stack.dup(interpreter.intPool, int(size))
  775. return nil, nil
  776. }
  777. }
  778. // make swap instruction function
  779. func makeSwap(size int64) executionFunc {
  780. // switch n + 1 otherwise n would be swapped with n
  781. size++
  782. return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  783. stack.swap(int(size))
  784. return nil, nil
  785. }
  786. }