instructions.go 26 KB

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