instructions.go 23 KB

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