instructions.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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. func opSha3(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  251. offset, size := stack.pop(), stack.pop()
  252. data := memory.Get(offset.Int64(), size.Int64())
  253. hash := crypto.Keccak256(data)
  254. if evm.vmConfig.EnablePreimageRecording {
  255. evm.StateDB.AddPreimage(common.BytesToHash(hash), data)
  256. }
  257. stack.push(new(big.Int).SetBytes(hash))
  258. evm.interpreter.intPool.put(offset, size)
  259. return nil, nil
  260. }
  261. func opAddress(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  262. stack.push(contract.Address().Big())
  263. return nil, nil
  264. }
  265. func opBalance(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  266. addr := common.BigToAddress(stack.pop())
  267. balance := evm.StateDB.GetBalance(addr)
  268. stack.push(new(big.Int).Set(balance))
  269. return nil, nil
  270. }
  271. func opOrigin(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  272. stack.push(evm.Origin.Big())
  273. return nil, nil
  274. }
  275. func opCaller(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  276. stack.push(contract.Caller().Big())
  277. return nil, nil
  278. }
  279. func opCallValue(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  280. stack.push(evm.interpreter.intPool.get().Set(contract.value))
  281. return nil, nil
  282. }
  283. func opCallDataLoad(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  284. stack.push(new(big.Int).SetBytes(getDataBig(contract.Input, stack.pop(), big32)))
  285. return nil, nil
  286. }
  287. func opCallDataSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  288. stack.push(evm.interpreter.intPool.get().SetInt64(int64(len(contract.Input))))
  289. return nil, nil
  290. }
  291. func opCallDataCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  292. var (
  293. memOffset = stack.pop()
  294. dataOffset = stack.pop()
  295. length = stack.pop()
  296. )
  297. memory.Set(memOffset.Uint64(), length.Uint64(), getDataBig(contract.Input, dataOffset, length))
  298. evm.interpreter.intPool.put(memOffset, dataOffset, length)
  299. return nil, nil
  300. }
  301. func opReturnDataSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  302. stack.push(evm.interpreter.intPool.get().SetUint64(uint64(len(evm.interpreter.returnData))))
  303. return nil, nil
  304. }
  305. func opReturnDataCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  306. var (
  307. memOffset = stack.pop()
  308. dataOffset = stack.pop()
  309. length = stack.pop()
  310. )
  311. defer evm.interpreter.intPool.put(memOffset, dataOffset, length)
  312. end := new(big.Int).Add(dataOffset, length)
  313. if end.BitLen() > 64 || uint64(len(evm.interpreter.returnData)) < end.Uint64() {
  314. return nil, errReturnDataOutOfBounds
  315. }
  316. memory.Set(memOffset.Uint64(), length.Uint64(), evm.interpreter.returnData[dataOffset.Uint64():end.Uint64()])
  317. return nil, nil
  318. }
  319. func opExtCodeSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  320. a := stack.pop()
  321. addr := common.BigToAddress(a)
  322. a.SetInt64(int64(evm.StateDB.GetCodeSize(addr)))
  323. stack.push(a)
  324. return nil, nil
  325. }
  326. func opCodeSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  327. l := evm.interpreter.intPool.get().SetInt64(int64(len(contract.Code)))
  328. stack.push(l)
  329. return nil, nil
  330. }
  331. func opCodeCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  332. var (
  333. memOffset = stack.pop()
  334. codeOffset = stack.pop()
  335. length = stack.pop()
  336. )
  337. codeCopy := getDataBig(contract.Code, codeOffset, length)
  338. memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
  339. evm.interpreter.intPool.put(memOffset, codeOffset, length)
  340. return nil, nil
  341. }
  342. func opExtCodeCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  343. var (
  344. addr = common.BigToAddress(stack.pop())
  345. memOffset = stack.pop()
  346. codeOffset = stack.pop()
  347. length = stack.pop()
  348. )
  349. codeCopy := getDataBig(evm.StateDB.GetCode(addr), codeOffset, length)
  350. memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
  351. evm.interpreter.intPool.put(memOffset, codeOffset, length)
  352. return nil, nil
  353. }
  354. func opGasprice(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  355. stack.push(evm.interpreter.intPool.get().Set(evm.GasPrice))
  356. return nil, nil
  357. }
  358. func opBlockhash(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  359. num := stack.pop()
  360. n := evm.interpreter.intPool.get().Sub(evm.BlockNumber, common.Big257)
  361. if num.Cmp(n) > 0 && num.Cmp(evm.BlockNumber) < 0 {
  362. stack.push(evm.GetHash(num.Uint64()).Big())
  363. } else {
  364. stack.push(new(big.Int))
  365. }
  366. evm.interpreter.intPool.put(num, n)
  367. return nil, nil
  368. }
  369. func opCoinbase(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  370. stack.push(evm.Coinbase.Big())
  371. return nil, nil
  372. }
  373. func opTimestamp(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  374. stack.push(math.U256(new(big.Int).Set(evm.Time)))
  375. return nil, nil
  376. }
  377. func opNumber(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  378. stack.push(math.U256(new(big.Int).Set(evm.BlockNumber)))
  379. return nil, nil
  380. }
  381. func opDifficulty(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  382. stack.push(math.U256(new(big.Int).Set(evm.Difficulty)))
  383. return nil, nil
  384. }
  385. func opGasLimit(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  386. stack.push(math.U256(new(big.Int).SetUint64(evm.GasLimit)))
  387. return nil, nil
  388. }
  389. func opPop(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  390. evm.interpreter.intPool.put(stack.pop())
  391. return nil, nil
  392. }
  393. func opMload(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  394. offset := stack.pop()
  395. val := new(big.Int).SetBytes(memory.Get(offset.Int64(), 32))
  396. stack.push(val)
  397. evm.interpreter.intPool.put(offset)
  398. return nil, nil
  399. }
  400. func opMstore(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  401. // pop value of the stack
  402. mStart, val := stack.pop(), stack.pop()
  403. memory.Set(mStart.Uint64(), 32, math.PaddedBigBytes(val, 32))
  404. evm.interpreter.intPool.put(mStart, val)
  405. return nil, nil
  406. }
  407. func opMstore8(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  408. off, val := stack.pop().Int64(), stack.pop().Int64()
  409. memory.store[off] = byte(val & 0xff)
  410. return nil, nil
  411. }
  412. func opSload(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  413. loc := common.BigToHash(stack.pop())
  414. val := evm.StateDB.GetState(contract.Address(), loc).Big()
  415. stack.push(val)
  416. return nil, nil
  417. }
  418. func opSstore(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  419. loc := common.BigToHash(stack.pop())
  420. val := stack.pop()
  421. evm.StateDB.SetState(contract.Address(), loc, common.BigToHash(val))
  422. evm.interpreter.intPool.put(val)
  423. return nil, nil
  424. }
  425. func opJump(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  426. pos := stack.pop()
  427. if !contract.jumpdests.has(contract.CodeHash, contract.Code, pos) {
  428. nop := contract.GetOp(pos.Uint64())
  429. return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
  430. }
  431. *pc = pos.Uint64()
  432. evm.interpreter.intPool.put(pos)
  433. return nil, nil
  434. }
  435. func opJumpi(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  436. pos, cond := stack.pop(), stack.pop()
  437. if cond.Sign() != 0 {
  438. if !contract.jumpdests.has(contract.CodeHash, contract.Code, pos) {
  439. nop := contract.GetOp(pos.Uint64())
  440. return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
  441. }
  442. *pc = pos.Uint64()
  443. } else {
  444. *pc++
  445. }
  446. evm.interpreter.intPool.put(pos, cond)
  447. return nil, nil
  448. }
  449. func opJumpdest(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  450. return nil, nil
  451. }
  452. func opPc(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  453. stack.push(evm.interpreter.intPool.get().SetUint64(*pc))
  454. return nil, nil
  455. }
  456. func opMsize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  457. stack.push(evm.interpreter.intPool.get().SetInt64(int64(memory.Len())))
  458. return nil, nil
  459. }
  460. func opGas(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  461. stack.push(evm.interpreter.intPool.get().SetUint64(contract.Gas))
  462. return nil, nil
  463. }
  464. func opCreate(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  465. var (
  466. value = stack.pop()
  467. offset, size = stack.pop(), stack.pop()
  468. input = memory.Get(offset.Int64(), size.Int64())
  469. gas = contract.Gas
  470. )
  471. if evm.ChainConfig().IsEIP150(evm.BlockNumber) {
  472. gas -= gas / 64
  473. }
  474. contract.UseGas(gas)
  475. res, addr, returnGas, suberr := evm.Create(contract, input, gas, value)
  476. // Push item on the stack based on the returned error. If the ruleset is
  477. // homestead we must check for CodeStoreOutOfGasError (homestead only
  478. // rule) and treat as an error, if the ruleset is frontier we must
  479. // ignore this error and pretend the operation was successful.
  480. if evm.ChainConfig().IsHomestead(evm.BlockNumber) && suberr == ErrCodeStoreOutOfGas {
  481. stack.push(new(big.Int))
  482. } else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
  483. stack.push(new(big.Int))
  484. } else {
  485. stack.push(addr.Big())
  486. }
  487. contract.Gas += returnGas
  488. evm.interpreter.intPool.put(value, offset, size)
  489. if suberr == errExecutionReverted {
  490. return res, nil
  491. }
  492. return nil, nil
  493. }
  494. func opCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  495. // Pop gas. The actual gas in in evm.callGasTemp.
  496. evm.interpreter.intPool.put(stack.pop())
  497. gas := evm.callGasTemp
  498. // Pop other call parameters.
  499. addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  500. toAddr := common.BigToAddress(addr)
  501. value = math.U256(value)
  502. // Get the arguments from the memory.
  503. args := memory.Get(inOffset.Int64(), inSize.Int64())
  504. if value.Sign() != 0 {
  505. gas += params.CallStipend
  506. }
  507. ret, returnGas, err := evm.Call(contract, toAddr, args, gas, value)
  508. if err != nil {
  509. stack.push(new(big.Int))
  510. } else {
  511. stack.push(big.NewInt(1))
  512. }
  513. if err == nil || err == errExecutionReverted {
  514. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  515. }
  516. contract.Gas += returnGas
  517. evm.interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
  518. return ret, nil
  519. }
  520. func opCallCode(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  521. // Pop gas. The actual gas is in evm.callGasTemp.
  522. evm.interpreter.intPool.put(stack.pop())
  523. gas := evm.callGasTemp
  524. // Pop other call parameters.
  525. addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  526. toAddr := common.BigToAddress(addr)
  527. value = math.U256(value)
  528. // Get arguments from the memory.
  529. args := memory.Get(inOffset.Int64(), inSize.Int64())
  530. if value.Sign() != 0 {
  531. gas += params.CallStipend
  532. }
  533. ret, returnGas, err := evm.CallCode(contract, toAddr, args, gas, value)
  534. if err != nil {
  535. stack.push(new(big.Int))
  536. } else {
  537. stack.push(big.NewInt(1))
  538. }
  539. if err == nil || err == errExecutionReverted {
  540. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  541. }
  542. contract.Gas += returnGas
  543. evm.interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
  544. return ret, nil
  545. }
  546. func opDelegateCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  547. // Pop gas. The actual gas is in evm.callGasTemp.
  548. evm.interpreter.intPool.put(stack.pop())
  549. gas := evm.callGasTemp
  550. // Pop other call parameters.
  551. addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  552. toAddr := common.BigToAddress(addr)
  553. // Get arguments from the memory.
  554. args := memory.Get(inOffset.Int64(), inSize.Int64())
  555. ret, returnGas, err := evm.DelegateCall(contract, toAddr, args, gas)
  556. if err != nil {
  557. stack.push(new(big.Int))
  558. } else {
  559. stack.push(big.NewInt(1))
  560. }
  561. if err == nil || err == errExecutionReverted {
  562. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  563. }
  564. contract.Gas += returnGas
  565. evm.interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
  566. return ret, nil
  567. }
  568. func opStaticCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  569. // Pop gas. The actual gas is in evm.callGasTemp.
  570. evm.interpreter.intPool.put(stack.pop())
  571. gas := evm.callGasTemp
  572. // Pop other call parameters.
  573. addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  574. toAddr := common.BigToAddress(addr)
  575. // Get arguments from the memory.
  576. args := memory.Get(inOffset.Int64(), inSize.Int64())
  577. ret, returnGas, err := evm.StaticCall(contract, toAddr, args, gas)
  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, inOffset, inSize, retOffset, retSize)
  588. return ret, nil
  589. }
  590. func opReturn(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  591. offset, size := stack.pop(), stack.pop()
  592. ret := memory.GetPtr(offset.Int64(), size.Int64())
  593. evm.interpreter.intPool.put(offset, size)
  594. return ret, nil
  595. }
  596. func opRevert(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  597. offset, size := stack.pop(), stack.pop()
  598. ret := memory.GetPtr(offset.Int64(), size.Int64())
  599. evm.interpreter.intPool.put(offset, size)
  600. return ret, nil
  601. }
  602. func opStop(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  603. return nil, nil
  604. }
  605. func opSuicide(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  606. balance := evm.StateDB.GetBalance(contract.Address())
  607. evm.StateDB.AddBalance(common.BigToAddress(stack.pop()), balance)
  608. evm.StateDB.Suicide(contract.Address())
  609. return nil, nil
  610. }
  611. // following functions are used by the instruction jump table
  612. // make log instruction function
  613. func makeLog(size int) executionFunc {
  614. return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  615. topics := make([]common.Hash, size)
  616. mStart, mSize := stack.pop(), stack.pop()
  617. for i := 0; i < size; i++ {
  618. topics[i] = common.BigToHash(stack.pop())
  619. }
  620. d := memory.Get(mStart.Int64(), mSize.Int64())
  621. evm.StateDB.AddLog(&types.Log{
  622. Address: contract.Address(),
  623. Topics: topics,
  624. Data: d,
  625. // This is a non-consensus field, but assigned here because
  626. // core/state doesn't know the current block number.
  627. BlockNumber: evm.BlockNumber.Uint64(),
  628. })
  629. evm.interpreter.intPool.put(mStart, mSize)
  630. return nil, nil
  631. }
  632. }
  633. // make push instruction function
  634. func makePush(size uint64, pushByteSize int) executionFunc {
  635. return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  636. codeLen := len(contract.Code)
  637. startMin := codeLen
  638. if int(*pc+1) < startMin {
  639. startMin = int(*pc + 1)
  640. }
  641. endMin := codeLen
  642. if startMin+pushByteSize < endMin {
  643. endMin = startMin + pushByteSize
  644. }
  645. integer := evm.interpreter.intPool.get()
  646. stack.push(integer.SetBytes(common.RightPadBytes(contract.Code[startMin:endMin], pushByteSize)))
  647. *pc += size
  648. return nil, nil
  649. }
  650. }
  651. // make push instruction function
  652. func makeDup(size int64) executionFunc {
  653. return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  654. stack.dup(evm.interpreter.intPool, int(size))
  655. return nil, nil
  656. }
  657. }
  658. // make swap instruction function
  659. func makeSwap(size int64) executionFunc {
  660. // switch n + 1 otherwise n would be swapped with n
  661. size += 1
  662. return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  663. stack.swap(int(size))
  664. return nil, nil
  665. }
  666. }