instructions.go 26 KB

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