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