instructions.go 31 KB

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