instructions.go 24 KB

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