instructions.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/common/math"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/params"
  24. "golang.org/x/crypto/sha3"
  25. )
  26. var (
  27. bigZero = new(big.Int)
  28. tt255 = math.BigPow(2, 255)
  29. errWriteProtection = errors.New("evm: write protection")
  30. errReturnDataOutOfBounds = errors.New("evm: return data out of bounds")
  31. errExecutionReverted = errors.New("evm: execution reverted")
  32. errMaxCodeSizeExceeded = errors.New("evm: max code size exceeded")
  33. errInvalidJump = errors.New("evm: invalid jump destination")
  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.GetPtr(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(interpreter.intPool.get().SetBytes(contract.Address().Bytes()))
  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.intPool.get().SetBytes(interpreter.evm.Origin.Bytes()))
  359. return nil, nil
  360. }
  361. func opCaller(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  362. stack.push(interpreter.intPool.get().SetBytes(contract.Caller().Bytes()))
  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.IsUint64() || 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.intPool.get().SetBytes(interpreter.evm.Coinbase.Bytes()))
  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. v := stack.peek()
  515. offset := v.Int64()
  516. v.SetBytes(memory.GetPtr(offset, 32))
  517. return nil, nil
  518. }
  519. func opMstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  520. // pop value of the stack
  521. mStart, val := stack.pop(), stack.pop()
  522. memory.Set32(mStart.Uint64(), val)
  523. interpreter.intPool.put(mStart, val)
  524. return nil, nil
  525. }
  526. func opMstore8(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  527. off, val := stack.pop().Int64(), stack.pop().Int64()
  528. memory.store[off] = byte(val & 0xff)
  529. return nil, nil
  530. }
  531. func opSload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  532. loc := stack.peek()
  533. val := interpreter.evm.StateDB.GetState(contract.Address(), common.BigToHash(loc))
  534. loc.SetBytes(val.Bytes())
  535. return nil, nil
  536. }
  537. func opSstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  538. loc := common.BigToHash(stack.pop())
  539. val := stack.pop()
  540. interpreter.evm.StateDB.SetState(contract.Address(), loc, common.BigToHash(val))
  541. interpreter.intPool.put(val)
  542. return nil, nil
  543. }
  544. func opJump(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  545. pos := stack.pop()
  546. if !contract.validJumpdest(pos) {
  547. return nil, errInvalidJump
  548. }
  549. *pc = pos.Uint64()
  550. interpreter.intPool.put(pos)
  551. return nil, nil
  552. }
  553. func opJumpi(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  554. pos, cond := stack.pop(), stack.pop()
  555. if cond.Sign() != 0 {
  556. if !contract.validJumpdest(pos) {
  557. return nil, errInvalidJump
  558. }
  559. *pc = pos.Uint64()
  560. } else {
  561. *pc++
  562. }
  563. interpreter.intPool.put(pos, cond)
  564. return nil, nil
  565. }
  566. func opJumpdest(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  567. return nil, nil
  568. }
  569. func opPc(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  570. stack.push(interpreter.intPool.get().SetUint64(*pc))
  571. return nil, nil
  572. }
  573. func opMsize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  574. stack.push(interpreter.intPool.get().SetInt64(int64(memory.Len())))
  575. return nil, nil
  576. }
  577. func opGas(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  578. stack.push(interpreter.intPool.get().SetUint64(contract.Gas))
  579. return nil, nil
  580. }
  581. func opCreate(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  582. var (
  583. value = stack.pop()
  584. offset, size = stack.pop(), stack.pop()
  585. input = memory.GetCopy(offset.Int64(), size.Int64())
  586. gas = contract.Gas
  587. )
  588. if interpreter.evm.chainRules.IsEIP150 {
  589. gas -= gas / 64
  590. }
  591. contract.UseGas(gas)
  592. res, addr, returnGas, suberr := interpreter.evm.Create(contract, input, gas, value)
  593. // Push item on the stack based on the returned error. If the ruleset is
  594. // homestead we must check for CodeStoreOutOfGasError (homestead only
  595. // rule) and treat as an error, if the ruleset is frontier we must
  596. // ignore this error and pretend the operation was successful.
  597. if interpreter.evm.chainRules.IsHomestead && suberr == ErrCodeStoreOutOfGas {
  598. stack.push(interpreter.intPool.getZero())
  599. } else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
  600. stack.push(interpreter.intPool.getZero())
  601. } else {
  602. stack.push(interpreter.intPool.get().SetBytes(addr.Bytes()))
  603. }
  604. contract.Gas += returnGas
  605. interpreter.intPool.put(value, offset, size)
  606. if suberr == errExecutionReverted {
  607. return res, nil
  608. }
  609. return nil, nil
  610. }
  611. func opCreate2(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  612. var (
  613. endowment = stack.pop()
  614. offset, size = stack.pop(), stack.pop()
  615. salt = stack.pop()
  616. input = memory.GetCopy(offset.Int64(), size.Int64())
  617. gas = contract.Gas
  618. )
  619. // Apply EIP150
  620. gas -= gas / 64
  621. contract.UseGas(gas)
  622. res, addr, returnGas, suberr := interpreter.evm.Create2(contract, input, gas, endowment, salt)
  623. // Push item on the stack based on the returned error.
  624. if suberr != nil {
  625. stack.push(interpreter.intPool.getZero())
  626. } else {
  627. stack.push(interpreter.intPool.get().SetBytes(addr.Bytes()))
  628. }
  629. contract.Gas += returnGas
  630. interpreter.intPool.put(endowment, offset, size, salt)
  631. if suberr == errExecutionReverted {
  632. return res, nil
  633. }
  634. return nil, nil
  635. }
  636. func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  637. // Pop gas. The actual gas in interpreter.evm.callGasTemp.
  638. interpreter.intPool.put(stack.pop())
  639. gas := interpreter.evm.callGasTemp
  640. // Pop other call parameters.
  641. addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  642. toAddr := common.BigToAddress(addr)
  643. value = math.U256(value)
  644. // Get the arguments from the memory.
  645. args := memory.GetPtr(inOffset.Int64(), inSize.Int64())
  646. if value.Sign() != 0 {
  647. gas += params.CallStipend
  648. }
  649. ret, returnGas, err := interpreter.evm.Call(contract, toAddr, args, gas, value)
  650. if err != nil {
  651. stack.push(interpreter.intPool.getZero())
  652. } else {
  653. stack.push(interpreter.intPool.get().SetUint64(1))
  654. }
  655. if err == nil || err == errExecutionReverted {
  656. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  657. }
  658. contract.Gas += returnGas
  659. interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
  660. return ret, nil
  661. }
  662. func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  663. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  664. interpreter.intPool.put(stack.pop())
  665. gas := interpreter.evm.callGasTemp
  666. // Pop other call parameters.
  667. addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  668. toAddr := common.BigToAddress(addr)
  669. value = math.U256(value)
  670. // Get arguments from the memory.
  671. args := memory.GetPtr(inOffset.Int64(), inSize.Int64())
  672. if value.Sign() != 0 {
  673. gas += params.CallStipend
  674. }
  675. ret, returnGas, err := interpreter.evm.CallCode(contract, toAddr, args, gas, value)
  676. if err != nil {
  677. stack.push(interpreter.intPool.getZero())
  678. } else {
  679. stack.push(interpreter.intPool.get().SetUint64(1))
  680. }
  681. if err == nil || err == errExecutionReverted {
  682. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  683. }
  684. contract.Gas += returnGas
  685. interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
  686. return ret, nil
  687. }
  688. func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  689. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  690. interpreter.intPool.put(stack.pop())
  691. gas := interpreter.evm.callGasTemp
  692. // Pop other call parameters.
  693. addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  694. toAddr := common.BigToAddress(addr)
  695. // Get arguments from the memory.
  696. args := memory.GetPtr(inOffset.Int64(), inSize.Int64())
  697. ret, returnGas, err := interpreter.evm.DelegateCall(contract, toAddr, args, gas)
  698. if err != nil {
  699. stack.push(interpreter.intPool.getZero())
  700. } else {
  701. stack.push(interpreter.intPool.get().SetUint64(1))
  702. }
  703. if err == nil || err == errExecutionReverted {
  704. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  705. }
  706. contract.Gas += returnGas
  707. interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
  708. return ret, nil
  709. }
  710. func opStaticCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  711. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  712. interpreter.intPool.put(stack.pop())
  713. gas := interpreter.evm.callGasTemp
  714. // Pop other call parameters.
  715. addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  716. toAddr := common.BigToAddress(addr)
  717. // Get arguments from the memory.
  718. args := memory.GetPtr(inOffset.Int64(), inSize.Int64())
  719. ret, returnGas, err := interpreter.evm.StaticCall(contract, toAddr, args, gas)
  720. if err != nil {
  721. stack.push(interpreter.intPool.getZero())
  722. } else {
  723. stack.push(interpreter.intPool.get().SetUint64(1))
  724. }
  725. if err == nil || err == errExecutionReverted {
  726. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  727. }
  728. contract.Gas += returnGas
  729. interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
  730. return ret, nil
  731. }
  732. func opReturn(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  733. offset, size := stack.pop(), stack.pop()
  734. ret := memory.GetPtr(offset.Int64(), size.Int64())
  735. interpreter.intPool.put(offset, size)
  736. return ret, nil
  737. }
  738. func opRevert(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  739. offset, size := stack.pop(), stack.pop()
  740. ret := memory.GetPtr(offset.Int64(), size.Int64())
  741. interpreter.intPool.put(offset, size)
  742. return ret, nil
  743. }
  744. func opStop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  745. return nil, nil
  746. }
  747. func opSuicide(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  748. balance := interpreter.evm.StateDB.GetBalance(contract.Address())
  749. interpreter.evm.StateDB.AddBalance(common.BigToAddress(stack.pop()), balance)
  750. interpreter.evm.StateDB.Suicide(contract.Address())
  751. return nil, nil
  752. }
  753. // following functions are used by the instruction jump table
  754. // make log instruction function
  755. func makeLog(size int) executionFunc {
  756. return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  757. topics := make([]common.Hash, size)
  758. mStart, mSize := stack.pop(), stack.pop()
  759. for i := 0; i < size; i++ {
  760. topics[i] = common.BigToHash(stack.pop())
  761. }
  762. d := memory.GetCopy(mStart.Int64(), mSize.Int64())
  763. interpreter.evm.StateDB.AddLog(&types.Log{
  764. Address: contract.Address(),
  765. Topics: topics,
  766. Data: d,
  767. // This is a non-consensus field, but assigned here because
  768. // core/state doesn't know the current block number.
  769. BlockNumber: interpreter.evm.BlockNumber.Uint64(),
  770. })
  771. interpreter.intPool.put(mStart, mSize)
  772. return nil, nil
  773. }
  774. }
  775. // opPush1 is a specialized version of pushN
  776. func opPush1(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  777. var (
  778. codeLen = uint64(len(contract.Code))
  779. integer = interpreter.intPool.get()
  780. )
  781. *pc += 1
  782. if *pc < codeLen {
  783. stack.push(integer.SetUint64(uint64(contract.Code[*pc])))
  784. } else {
  785. stack.push(integer.SetUint64(0))
  786. }
  787. return nil, nil
  788. }
  789. // make push instruction function
  790. func makePush(size uint64, pushByteSize int) executionFunc {
  791. return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  792. codeLen := len(contract.Code)
  793. startMin := codeLen
  794. if int(*pc+1) < startMin {
  795. startMin = int(*pc + 1)
  796. }
  797. endMin := codeLen
  798. if startMin+pushByteSize < endMin {
  799. endMin = startMin + pushByteSize
  800. }
  801. integer := interpreter.intPool.get()
  802. stack.push(integer.SetBytes(common.RightPadBytes(contract.Code[startMin:endMin], pushByteSize)))
  803. *pc += size
  804. return nil, nil
  805. }
  806. }
  807. // make dup instruction function
  808. func makeDup(size int64) executionFunc {
  809. return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  810. stack.dup(interpreter.intPool, int(size))
  811. return nil, nil
  812. }
  813. }
  814. // make swap instruction function
  815. func makeSwap(size int64) executionFunc {
  816. // switch n + 1 otherwise n would be swapped with n
  817. size++
  818. return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  819. stack.swap(int(size))
  820. return nil, nil
  821. }
  822. }