instructions.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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 opBeginSub(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  564. return nil, ErrInvalidSubroutineEntry
  565. }
  566. func opJumpSub(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  567. if len(callContext.rstack.data) >= 1023 {
  568. return nil, ErrReturnStackExceeded
  569. }
  570. pos := callContext.stack.pop()
  571. if !pos.IsUint64() {
  572. return nil, ErrInvalidJump
  573. }
  574. posU64 := pos.Uint64()
  575. if !callContext.contract.validJumpSubdest(posU64) {
  576. return nil, ErrInvalidJump
  577. }
  578. callContext.rstack.push(*pc)
  579. *pc = posU64 + 1
  580. interpreter.intPool.put(pos)
  581. return nil, nil
  582. }
  583. func opReturnSub(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  584. if len(callContext.rstack.data) == 0 {
  585. return nil, ErrInvalidRetsub
  586. }
  587. // Other than the check that the return stack is not empty, there is no
  588. // need to validate the pc from 'returns', since we only ever push valid
  589. //values onto it via jumpsub.
  590. *pc = callContext.rstack.pop() + 1
  591. return nil, nil
  592. }
  593. func opPc(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  594. callContext.stack.push(interpreter.intPool.get().SetUint64(*pc))
  595. return nil, nil
  596. }
  597. func opMsize(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  598. callContext.stack.push(interpreter.intPool.get().SetInt64(int64(callContext.memory.Len())))
  599. return nil, nil
  600. }
  601. func opGas(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  602. callContext.stack.push(interpreter.intPool.get().SetUint64(callContext.contract.Gas))
  603. return nil, nil
  604. }
  605. func opCreate(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  606. var (
  607. value = callContext.stack.pop()
  608. offset, size = callContext.stack.pop(), callContext.stack.pop()
  609. input = callContext.memory.GetCopy(offset.Int64(), size.Int64())
  610. gas = callContext.contract.Gas
  611. )
  612. if interpreter.evm.chainRules.IsEIP150 {
  613. gas -= gas / 64
  614. }
  615. callContext.contract.UseGas(gas)
  616. res, addr, returnGas, suberr := interpreter.evm.Create(callContext.contract, input, gas, value)
  617. // Push item on the stack based on the returned error. If the ruleset is
  618. // homestead we must check for CodeStoreOutOfGasError (homestead only
  619. // rule) and treat as an error, if the ruleset is frontier we must
  620. // ignore this error and pretend the operation was successful.
  621. if interpreter.evm.chainRules.IsHomestead && suberr == ErrCodeStoreOutOfGas {
  622. callContext.stack.push(interpreter.intPool.getZero())
  623. } else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
  624. callContext.stack.push(interpreter.intPool.getZero())
  625. } else {
  626. callContext.stack.push(interpreter.intPool.get().SetBytes(addr.Bytes()))
  627. }
  628. callContext.contract.Gas += returnGas
  629. interpreter.intPool.put(value, offset, size)
  630. if suberr == ErrExecutionReverted {
  631. return res, nil
  632. }
  633. return nil, nil
  634. }
  635. func opCreate2(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  636. var (
  637. endowment = callContext.stack.pop()
  638. offset, size = callContext.stack.pop(), callContext.stack.pop()
  639. salt = callContext.stack.pop()
  640. input = callContext.memory.GetCopy(offset.Int64(), size.Int64())
  641. gas = callContext.contract.Gas
  642. )
  643. // Apply EIP150
  644. gas -= gas / 64
  645. callContext.contract.UseGas(gas)
  646. res, addr, returnGas, suberr := interpreter.evm.Create2(callContext.contract, input, gas, endowment, salt)
  647. // Push item on the stack based on the returned error.
  648. if suberr != nil {
  649. callContext.stack.push(interpreter.intPool.getZero())
  650. } else {
  651. callContext.stack.push(interpreter.intPool.get().SetBytes(addr.Bytes()))
  652. }
  653. callContext.contract.Gas += returnGas
  654. interpreter.intPool.put(endowment, offset, size, salt)
  655. if suberr == ErrExecutionReverted {
  656. return res, nil
  657. }
  658. return nil, nil
  659. }
  660. func opCall(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  661. // Pop gas. The actual gas in interpreter.evm.callGasTemp.
  662. interpreter.intPool.putOne(callContext.stack.pop())
  663. gas := interpreter.evm.callGasTemp
  664. // Pop other call parameters.
  665. addr, value, inOffset, inSize, retOffset, retSize := callContext.stack.pop(), callContext.stack.pop(), callContext.stack.pop(), callContext.stack.pop(), callContext.stack.pop(), callContext.stack.pop()
  666. toAddr := common.BigToAddress(addr)
  667. value = math.U256(value)
  668. // Get the arguments from the memory.
  669. args := callContext.memory.GetPtr(inOffset.Int64(), inSize.Int64())
  670. if value.Sign() != 0 {
  671. gas += params.CallStipend
  672. }
  673. ret, returnGas, err := interpreter.evm.Call(callContext.contract, toAddr, args, gas, value)
  674. if err != nil {
  675. callContext.stack.push(interpreter.intPool.getZero())
  676. } else {
  677. callContext.stack.push(interpreter.intPool.get().SetUint64(1))
  678. }
  679. if err == nil || err == ErrExecutionReverted {
  680. callContext.memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  681. }
  682. callContext.contract.Gas += returnGas
  683. interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
  684. return ret, nil
  685. }
  686. func opCallCode(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  687. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  688. interpreter.intPool.putOne(callContext.stack.pop())
  689. gas := interpreter.evm.callGasTemp
  690. // Pop other call parameters.
  691. addr, value, inOffset, inSize, retOffset, retSize := callContext.stack.pop(), callContext.stack.pop(), callContext.stack.pop(), callContext.stack.pop(), callContext.stack.pop(), callContext.stack.pop()
  692. toAddr := common.BigToAddress(addr)
  693. value = math.U256(value)
  694. // Get arguments from the memory.
  695. args := callContext.memory.GetPtr(inOffset.Int64(), inSize.Int64())
  696. if value.Sign() != 0 {
  697. gas += params.CallStipend
  698. }
  699. ret, returnGas, err := interpreter.evm.CallCode(callContext.contract, toAddr, args, gas, value)
  700. if err != nil {
  701. callContext.stack.push(interpreter.intPool.getZero())
  702. } else {
  703. callContext.stack.push(interpreter.intPool.get().SetUint64(1))
  704. }
  705. if err == nil || err == ErrExecutionReverted {
  706. callContext.memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  707. }
  708. callContext.contract.Gas += returnGas
  709. interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
  710. return ret, nil
  711. }
  712. func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  713. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  714. interpreter.intPool.putOne(callContext.stack.pop())
  715. gas := interpreter.evm.callGasTemp
  716. // Pop other call parameters.
  717. addr, inOffset, inSize, retOffset, retSize := callContext.stack.pop(), callContext.stack.pop(), callContext.stack.pop(), callContext.stack.pop(), callContext.stack.pop()
  718. toAddr := common.BigToAddress(addr)
  719. // Get arguments from the memory.
  720. args := callContext.memory.GetPtr(inOffset.Int64(), inSize.Int64())
  721. ret, returnGas, err := interpreter.evm.DelegateCall(callContext.contract, toAddr, args, gas)
  722. if err != nil {
  723. callContext.stack.push(interpreter.intPool.getZero())
  724. } else {
  725. callContext.stack.push(interpreter.intPool.get().SetUint64(1))
  726. }
  727. if err == nil || err == ErrExecutionReverted {
  728. callContext.memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  729. }
  730. callContext.contract.Gas += returnGas
  731. interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
  732. return ret, nil
  733. }
  734. func opStaticCall(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  735. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  736. interpreter.intPool.putOne(callContext.stack.pop())
  737. gas := interpreter.evm.callGasTemp
  738. // Pop other call parameters.
  739. addr, inOffset, inSize, retOffset, retSize := callContext.stack.pop(), callContext.stack.pop(), callContext.stack.pop(), callContext.stack.pop(), callContext.stack.pop()
  740. toAddr := common.BigToAddress(addr)
  741. // Get arguments from the memory.
  742. args := callContext.memory.GetPtr(inOffset.Int64(), inSize.Int64())
  743. ret, returnGas, err := interpreter.evm.StaticCall(callContext.contract, toAddr, args, gas)
  744. if err != nil {
  745. callContext.stack.push(interpreter.intPool.getZero())
  746. } else {
  747. callContext.stack.push(interpreter.intPool.get().SetUint64(1))
  748. }
  749. if err == nil || err == ErrExecutionReverted {
  750. callContext.memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  751. }
  752. callContext.contract.Gas += returnGas
  753. interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
  754. return ret, nil
  755. }
  756. func opReturn(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  757. offset, size := callContext.stack.pop(), callContext.stack.pop()
  758. ret := callContext.memory.GetPtr(offset.Int64(), size.Int64())
  759. interpreter.intPool.put(offset, size)
  760. return ret, nil
  761. }
  762. func opRevert(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  763. offset, size := callContext.stack.pop(), callContext.stack.pop()
  764. ret := callContext.memory.GetPtr(offset.Int64(), size.Int64())
  765. interpreter.intPool.put(offset, size)
  766. return ret, nil
  767. }
  768. func opStop(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  769. return nil, nil
  770. }
  771. func opSuicide(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  772. balance := interpreter.evm.StateDB.GetBalance(callContext.contract.Address())
  773. interpreter.evm.StateDB.AddBalance(common.BigToAddress(callContext.stack.pop()), balance)
  774. interpreter.evm.StateDB.Suicide(callContext.contract.Address())
  775. return nil, nil
  776. }
  777. // following functions are used by the instruction jump table
  778. // make log instruction function
  779. func makeLog(size int) executionFunc {
  780. return func(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  781. topics := make([]common.Hash, size)
  782. mStart, mSize := callContext.stack.pop(), callContext.stack.pop()
  783. for i := 0; i < size; i++ {
  784. topics[i] = common.BigToHash(callContext.stack.pop())
  785. }
  786. d := callContext.memory.GetCopy(mStart.Int64(), mSize.Int64())
  787. interpreter.evm.StateDB.AddLog(&types.Log{
  788. Address: callContext.contract.Address(),
  789. Topics: topics,
  790. Data: d,
  791. // This is a non-consensus field, but assigned here because
  792. // core/state doesn't know the current block number.
  793. BlockNumber: interpreter.evm.BlockNumber.Uint64(),
  794. })
  795. interpreter.intPool.put(mStart, mSize)
  796. return nil, nil
  797. }
  798. }
  799. // opPush1 is a specialized version of pushN
  800. func opPush1(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  801. var (
  802. codeLen = uint64(len(callContext.contract.Code))
  803. integer = interpreter.intPool.get()
  804. )
  805. *pc += 1
  806. if *pc < codeLen {
  807. callContext.stack.push(integer.SetUint64(uint64(callContext.contract.Code[*pc])))
  808. } else {
  809. callContext.stack.push(integer.SetUint64(0))
  810. }
  811. return nil, nil
  812. }
  813. // make push instruction function
  814. func makePush(size uint64, pushByteSize int) executionFunc {
  815. return func(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  816. codeLen := len(callContext.contract.Code)
  817. startMin := codeLen
  818. if int(*pc+1) < startMin {
  819. startMin = int(*pc + 1)
  820. }
  821. endMin := codeLen
  822. if startMin+pushByteSize < endMin {
  823. endMin = startMin + pushByteSize
  824. }
  825. integer := interpreter.intPool.get()
  826. callContext.stack.push(integer.SetBytes(common.RightPadBytes(callContext.contract.Code[startMin:endMin], pushByteSize)))
  827. *pc += size
  828. return nil, nil
  829. }
  830. }
  831. // make dup instruction function
  832. func makeDup(size int64) executionFunc {
  833. return func(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  834. callContext.stack.dup(interpreter.intPool, int(size))
  835. return nil, nil
  836. }
  837. }
  838. // make swap instruction function
  839. func makeSwap(size int64) executionFunc {
  840. // switch n + 1 otherwise n would be swapped with n
  841. size++
  842. return func(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  843. callContext.stack.swap(int(size))
  844. return nil, nil
  845. }
  846. }