instructions.go 26 KB

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