instructions.go 32 KB

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