instructions.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. "github.com/ethereum/go-ethereum/common"
  19. "github.com/ethereum/go-ethereum/core/types"
  20. "github.com/ethereum/go-ethereum/params"
  21. "github.com/holiman/uint256"
  22. "golang.org/x/crypto/sha3"
  23. )
  24. func opAdd(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  25. x, y := callContext.stack.pop(), callContext.stack.peek()
  26. y.Add(&x, y)
  27. return nil, nil
  28. }
  29. func opSub(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  30. x, y := callContext.stack.pop(), callContext.stack.peek()
  31. y.Sub(&x, y)
  32. return nil, nil
  33. }
  34. func opMul(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  35. x, y := callContext.stack.pop(), callContext.stack.peek()
  36. y.Mul(&x, y)
  37. return nil, nil
  38. }
  39. func opDiv(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  40. x, y := callContext.stack.pop(), callContext.stack.peek()
  41. y.Div(&x, y)
  42. return nil, nil
  43. }
  44. func opSdiv(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  45. x, y := callContext.stack.pop(), callContext.stack.peek()
  46. y.SDiv(&x, y)
  47. return nil, nil
  48. }
  49. func opMod(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  50. x, y := callContext.stack.pop(), callContext.stack.peek()
  51. y.Mod(&x, y)
  52. return nil, nil
  53. }
  54. func opSmod(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  55. x, y := callContext.stack.pop(), callContext.stack.peek()
  56. y.SMod(&x, y)
  57. return nil, nil
  58. }
  59. func opExp(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  60. base, exponent := callContext.stack.pop(), callContext.stack.peek()
  61. exponent.Exp(&base, exponent)
  62. return nil, nil
  63. }
  64. func opSignExtend(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  65. back, num := callContext.stack.pop(), callContext.stack.peek()
  66. num.ExtendSign(num, &back)
  67. return nil, nil
  68. }
  69. func opNot(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  70. x := callContext.stack.peek()
  71. x.Not(x)
  72. return nil, nil
  73. }
  74. func opLt(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  75. x, y := callContext.stack.pop(), callContext.stack.peek()
  76. if x.Lt(y) {
  77. y.SetOne()
  78. } else {
  79. y.Clear()
  80. }
  81. return nil, nil
  82. }
  83. func opGt(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  84. x, y := callContext.stack.pop(), callContext.stack.peek()
  85. if x.Gt(y) {
  86. y.SetOne()
  87. } else {
  88. y.Clear()
  89. }
  90. return nil, nil
  91. }
  92. func opSlt(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  93. x, y := callContext.stack.pop(), callContext.stack.peek()
  94. if x.Slt(y) {
  95. y.SetOne()
  96. } else {
  97. y.Clear()
  98. }
  99. return nil, nil
  100. }
  101. func opSgt(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  102. x, y := callContext.stack.pop(), callContext.stack.peek()
  103. if x.Sgt(y) {
  104. y.SetOne()
  105. } else {
  106. y.Clear()
  107. }
  108. return nil, nil
  109. }
  110. func opEq(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  111. x, y := callContext.stack.pop(), callContext.stack.peek()
  112. if x.Eq(y) {
  113. y.SetOne()
  114. } else {
  115. y.Clear()
  116. }
  117. return nil, nil
  118. }
  119. func opIszero(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  120. x := callContext.stack.peek()
  121. if x.IsZero() {
  122. x.SetOne()
  123. } else {
  124. x.Clear()
  125. }
  126. return nil, nil
  127. }
  128. func opAnd(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  129. x, y := callContext.stack.pop(), callContext.stack.peek()
  130. y.And(&x, y)
  131. return nil, nil
  132. }
  133. func opOr(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  134. x, y := callContext.stack.pop(), callContext.stack.peek()
  135. y.Or(&x, y)
  136. return nil, nil
  137. }
  138. func opXor(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  139. x, y := callContext.stack.pop(), callContext.stack.peek()
  140. y.Xor(&x, y)
  141. return nil, nil
  142. }
  143. func opByte(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  144. th, val := callContext.stack.pop(), callContext.stack.peek()
  145. val.Byte(&th)
  146. return nil, nil
  147. }
  148. func opAddmod(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  149. x, y, z := callContext.stack.pop(), callContext.stack.pop(), callContext.stack.peek()
  150. if z.IsZero() {
  151. z.Clear()
  152. } else {
  153. z.AddMod(&x, &y, z)
  154. }
  155. return nil, nil
  156. }
  157. func opMulmod(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  158. x, y, z := callContext.stack.pop(), callContext.stack.pop(), callContext.stack.peek()
  159. z.MulMod(&x, &y, z)
  160. return nil, nil
  161. }
  162. // opSHL implements Shift Left
  163. // The SHL instruction (shift left) pops 2 values from the stack, first arg1 and then arg2,
  164. // and pushes on the stack arg2 shifted to the left by arg1 number of bits.
  165. func opSHL(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  166. // Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
  167. shift, value := callContext.stack.pop(), callContext.stack.peek()
  168. if shift.LtUint64(256) {
  169. value.Lsh(value, uint(shift.Uint64()))
  170. } else {
  171. value.Clear()
  172. }
  173. return nil, nil
  174. }
  175. // opSHR implements Logical Shift Right
  176. // The SHR instruction (logical shift right) pops 2 values from the stack, first arg1 and then arg2,
  177. // and pushes on the stack arg2 shifted to the right by arg1 number of bits with zero fill.
  178. func opSHR(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  179. // Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
  180. shift, value := callContext.stack.pop(), callContext.stack.peek()
  181. if shift.LtUint64(256) {
  182. value.Rsh(value, uint(shift.Uint64()))
  183. } else {
  184. value.Clear()
  185. }
  186. return nil, nil
  187. }
  188. // opSAR implements Arithmetic Shift Right
  189. // The SAR instruction (arithmetic shift right) pops 2 values from the stack, first arg1 and then arg2,
  190. // and pushes on the stack arg2 shifted to the right by arg1 number of bits with sign extension.
  191. func opSAR(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  192. shift, value := callContext.stack.pop(), callContext.stack.peek()
  193. if shift.GtUint64(256) {
  194. if value.Sign() >= 0 {
  195. value.Clear()
  196. } else {
  197. // Max negative shift: all bits set
  198. value.SetAllOne()
  199. }
  200. return nil, nil
  201. }
  202. n := uint(shift.Uint64())
  203. value.SRsh(value, n)
  204. return nil, nil
  205. }
  206. func opSha3(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  207. offset, size := callContext.stack.pop(), callContext.stack.peek()
  208. data := callContext.memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
  209. if interpreter.hasher == nil {
  210. interpreter.hasher = sha3.NewLegacyKeccak256().(keccakState)
  211. } else {
  212. interpreter.hasher.Reset()
  213. }
  214. interpreter.hasher.Write(data)
  215. interpreter.hasher.Read(interpreter.hasherBuf[:])
  216. evm := interpreter.evm
  217. if evm.vmConfig.EnablePreimageRecording {
  218. evm.StateDB.AddPreimage(interpreter.hasherBuf, data)
  219. }
  220. size.SetBytes(interpreter.hasherBuf[:])
  221. return nil, nil
  222. }
  223. func opAddress(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  224. callContext.stack.push(new(uint256.Int).SetBytes(callContext.contract.Address().Bytes()))
  225. return nil, nil
  226. }
  227. func opBalance(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  228. slot := callContext.stack.peek()
  229. address := common.Address(slot.Bytes20())
  230. slot.SetFromBig(interpreter.evm.StateDB.GetBalance(address))
  231. return nil, nil
  232. }
  233. func opOrigin(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  234. callContext.stack.push(new(uint256.Int).SetBytes(interpreter.evm.Origin.Bytes()))
  235. return nil, nil
  236. }
  237. func opCaller(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  238. callContext.stack.push(new(uint256.Int).SetBytes(callContext.contract.Caller().Bytes()))
  239. return nil, nil
  240. }
  241. func opCallValue(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  242. v, _ := uint256.FromBig(callContext.contract.value)
  243. callContext.stack.push(v)
  244. return nil, nil
  245. }
  246. func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  247. x := callContext.stack.peek()
  248. if offset, overflow := x.Uint64WithOverflow(); !overflow {
  249. data := getData(callContext.contract.Input, offset, 32)
  250. x.SetBytes(data)
  251. } else {
  252. x.Clear()
  253. }
  254. return nil, nil
  255. }
  256. func opCallDataSize(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  257. callContext.stack.push(new(uint256.Int).SetUint64(uint64(len(callContext.contract.Input))))
  258. return nil, nil
  259. }
  260. func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  261. var (
  262. memOffset = callContext.stack.pop()
  263. dataOffset = callContext.stack.pop()
  264. length = callContext.stack.pop()
  265. )
  266. dataOffset64, overflow := dataOffset.Uint64WithOverflow()
  267. if overflow {
  268. dataOffset64 = 0xffffffffffffffff
  269. }
  270. // These values are checked for overflow during gas cost calculation
  271. memOffset64 := memOffset.Uint64()
  272. length64 := length.Uint64()
  273. callContext.memory.Set(memOffset64, length64, getData(callContext.contract.Input, dataOffset64, length64))
  274. return nil, nil
  275. }
  276. func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  277. callContext.stack.push(new(uint256.Int).SetUint64(uint64(len(interpreter.returnData))))
  278. return nil, nil
  279. }
  280. func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  281. var (
  282. memOffset = callContext.stack.pop()
  283. dataOffset = callContext.stack.pop()
  284. length = callContext.stack.pop()
  285. )
  286. offset64, overflow := dataOffset.Uint64WithOverflow()
  287. if overflow {
  288. return nil, ErrReturnDataOutOfBounds
  289. }
  290. // we can reuse dataOffset now (aliasing it for clarity)
  291. var end = dataOffset
  292. end.Add(&dataOffset, &length)
  293. end64, overflow := end.Uint64WithOverflow()
  294. if overflow || uint64(len(interpreter.returnData)) < end64 {
  295. return nil, ErrReturnDataOutOfBounds
  296. }
  297. callContext.memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[offset64:end64])
  298. return nil, nil
  299. }
  300. func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  301. slot := callContext.stack.peek()
  302. slot.SetUint64(uint64(interpreter.evm.StateDB.GetCodeSize(slot.Bytes20())))
  303. return nil, nil
  304. }
  305. func opCodeSize(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  306. l := new(uint256.Int)
  307. l.SetUint64(uint64(len(callContext.contract.Code)))
  308. callContext.stack.push(l)
  309. return nil, nil
  310. }
  311. func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  312. var (
  313. memOffset = callContext.stack.pop()
  314. codeOffset = callContext.stack.pop()
  315. length = callContext.stack.pop()
  316. )
  317. uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
  318. if overflow {
  319. uint64CodeOffset = 0xffffffffffffffff
  320. }
  321. codeCopy := getData(callContext.contract.Code, uint64CodeOffset, length.Uint64())
  322. callContext.memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
  323. return nil, nil
  324. }
  325. func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  326. var (
  327. stack = callContext.stack
  328. a = stack.pop()
  329. memOffset = stack.pop()
  330. codeOffset = stack.pop()
  331. length = stack.pop()
  332. )
  333. uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
  334. if overflow {
  335. uint64CodeOffset = 0xffffffffffffffff
  336. }
  337. addr := common.Address(a.Bytes20())
  338. codeCopy := getData(interpreter.evm.StateDB.GetCode(addr), uint64CodeOffset, length.Uint64())
  339. callContext.memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
  340. return nil, nil
  341. }
  342. // opExtCodeHash returns the code hash of a specified account.
  343. // There are several cases when the function is called, while we can relay everything
  344. // to `state.GetCodeHash` function to ensure the correctness.
  345. // (1) Caller tries to get the code hash of a normal contract account, state
  346. // should return the relative code hash and set it as the result.
  347. //
  348. // (2) Caller tries to get the code hash of a non-existent account, state should
  349. // return common.Hash{} and zero will be set as the result.
  350. //
  351. // (3) Caller tries to get the code hash for an account without contract code,
  352. // state should return emptyCodeHash(0xc5d246...) as the result.
  353. //
  354. // (4) Caller tries to get the code hash of a precompiled account, the result
  355. // should be zero or emptyCodeHash.
  356. //
  357. // It is worth noting that in order to avoid unnecessary create and clean,
  358. // all precompile accounts on mainnet have been transferred 1 wei, so the return
  359. // here should be emptyCodeHash.
  360. // If the precompile account is not transferred any amount on a private or
  361. // customized chain, the return value will be zero.
  362. //
  363. // (5) Caller tries to get the code hash for an account which is marked as suicided
  364. // in the current transaction, the code hash of this account should be returned.
  365. //
  366. // (6) Caller tries to get the code hash for an account which is marked as deleted,
  367. // this account should be regarded as a non-existent account and zero should be returned.
  368. func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  369. slot := callContext.stack.peek()
  370. address := common.Address(slot.Bytes20())
  371. if interpreter.evm.StateDB.Empty(address) {
  372. slot.Clear()
  373. } else {
  374. slot.SetBytes(interpreter.evm.StateDB.GetCodeHash(address).Bytes())
  375. }
  376. return nil, nil
  377. }
  378. func opGasprice(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  379. v, _ := uint256.FromBig(interpreter.evm.GasPrice)
  380. callContext.stack.push(v)
  381. return nil, nil
  382. }
  383. func opBlockhash(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  384. num := callContext.stack.peek()
  385. num64, overflow := num.Uint64WithOverflow()
  386. if overflow {
  387. num.Clear()
  388. return nil, nil
  389. }
  390. var upper, lower uint64
  391. upper = interpreter.evm.Context.BlockNumber.Uint64()
  392. if upper < 257 {
  393. lower = 0
  394. } else {
  395. lower = upper - 256
  396. }
  397. if num64 >= lower && num64 < upper {
  398. num.SetBytes(interpreter.evm.Context.GetHash(num64).Bytes())
  399. } else {
  400. num.Clear()
  401. }
  402. return nil, nil
  403. }
  404. func opCoinbase(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  405. callContext.stack.push(new(uint256.Int).SetBytes(interpreter.evm.Context.Coinbase.Bytes()))
  406. return nil, nil
  407. }
  408. func opTimestamp(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  409. v, _ := uint256.FromBig(interpreter.evm.Context.Time)
  410. callContext.stack.push(v)
  411. return nil, nil
  412. }
  413. func opNumber(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  414. v, _ := uint256.FromBig(interpreter.evm.Context.BlockNumber)
  415. callContext.stack.push(v)
  416. return nil, nil
  417. }
  418. func opDifficulty(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  419. v, _ := uint256.FromBig(interpreter.evm.Context.Difficulty)
  420. callContext.stack.push(v)
  421. return nil, nil
  422. }
  423. func opGasLimit(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  424. callContext.stack.push(new(uint256.Int).SetUint64(interpreter.evm.Context.GasLimit))
  425. return nil, nil
  426. }
  427. func opPop(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  428. callContext.stack.pop()
  429. return nil, nil
  430. }
  431. func opMload(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  432. v := callContext.stack.peek()
  433. offset := int64(v.Uint64())
  434. v.SetBytes(callContext.memory.GetPtr(offset, 32))
  435. return nil, nil
  436. }
  437. func opMstore(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  438. // pop value of the stack
  439. mStart, val := callContext.stack.pop(), callContext.stack.pop()
  440. callContext.memory.Set32(mStart.Uint64(), &val)
  441. return nil, nil
  442. }
  443. func opMstore8(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  444. off, val := callContext.stack.pop(), callContext.stack.pop()
  445. callContext.memory.store[off.Uint64()] = byte(val.Uint64())
  446. return nil, nil
  447. }
  448. func opSload(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  449. loc := callContext.stack.peek()
  450. hash := common.Hash(loc.Bytes32())
  451. val := interpreter.evm.StateDB.GetState(callContext.contract.Address(), hash)
  452. loc.SetBytes(val.Bytes())
  453. return nil, nil
  454. }
  455. func opSstore(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  456. loc := callContext.stack.pop()
  457. val := callContext.stack.pop()
  458. interpreter.evm.StateDB.SetState(callContext.contract.Address(),
  459. loc.Bytes32(), val.Bytes32())
  460. return nil, nil
  461. }
  462. func opJump(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  463. pos := callContext.stack.pop()
  464. if !callContext.contract.validJumpdest(&pos) {
  465. return nil, ErrInvalidJump
  466. }
  467. *pc = pos.Uint64()
  468. return nil, nil
  469. }
  470. func opJumpi(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  471. pos, cond := callContext.stack.pop(), callContext.stack.pop()
  472. if !cond.IsZero() {
  473. if !callContext.contract.validJumpdest(&pos) {
  474. return nil, ErrInvalidJump
  475. }
  476. *pc = pos.Uint64()
  477. } else {
  478. *pc++
  479. }
  480. return nil, nil
  481. }
  482. func opJumpdest(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  483. return nil, nil
  484. }
  485. func opBeginSub(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  486. return nil, ErrInvalidSubroutineEntry
  487. }
  488. func opJumpSub(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  489. if len(callContext.rstack.data) >= 1023 {
  490. return nil, ErrReturnStackExceeded
  491. }
  492. pos := callContext.stack.pop()
  493. if !pos.IsUint64() {
  494. return nil, ErrInvalidJump
  495. }
  496. posU64 := pos.Uint64()
  497. if !callContext.contract.validJumpSubdest(posU64) {
  498. return nil, ErrInvalidJump
  499. }
  500. callContext.rstack.push(uint32(*pc))
  501. *pc = posU64 + 1
  502. return nil, nil
  503. }
  504. func opReturnSub(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  505. if len(callContext.rstack.data) == 0 {
  506. return nil, ErrInvalidRetsub
  507. }
  508. // Other than the check that the return stack is not empty, there is no
  509. // need to validate the pc from 'returns', since we only ever push valid
  510. //values onto it via jumpsub.
  511. *pc = uint64(callContext.rstack.pop()) + 1
  512. return nil, nil
  513. }
  514. func opPc(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  515. callContext.stack.push(new(uint256.Int).SetUint64(*pc))
  516. return nil, nil
  517. }
  518. func opMsize(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  519. callContext.stack.push(new(uint256.Int).SetUint64(uint64(callContext.memory.Len())))
  520. return nil, nil
  521. }
  522. func opGas(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  523. callContext.stack.push(new(uint256.Int).SetUint64(callContext.contract.Gas))
  524. return nil, nil
  525. }
  526. func opCreate(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  527. var (
  528. value = callContext.stack.pop()
  529. offset, size = callContext.stack.pop(), callContext.stack.pop()
  530. input = callContext.memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
  531. gas = callContext.contract.Gas
  532. )
  533. if interpreter.evm.chainRules.IsEIP150 {
  534. gas -= gas / 64
  535. }
  536. // reuse size int for stackvalue
  537. stackvalue := size
  538. callContext.contract.UseGas(gas)
  539. //TODO: use uint256.Int instead of converting with toBig()
  540. var bigVal = big0
  541. if !value.IsZero() {
  542. bigVal = value.ToBig()
  543. }
  544. res, addr, returnGas, suberr := interpreter.evm.Create(callContext.contract, input, gas, bigVal)
  545. // Push item on the stack based on the returned error. If the ruleset is
  546. // homestead we must check for CodeStoreOutOfGasError (homestead only
  547. // rule) and treat as an error, if the ruleset is frontier we must
  548. // ignore this error and pretend the operation was successful.
  549. if interpreter.evm.chainRules.IsHomestead && suberr == ErrCodeStoreOutOfGas {
  550. stackvalue.Clear()
  551. } else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
  552. stackvalue.Clear()
  553. } else {
  554. stackvalue.SetBytes(addr.Bytes())
  555. }
  556. callContext.stack.push(&stackvalue)
  557. callContext.contract.Gas += returnGas
  558. if suberr == ErrExecutionReverted {
  559. return res, nil
  560. }
  561. return nil, nil
  562. }
  563. func opCreate2(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  564. var (
  565. endowment = callContext.stack.pop()
  566. offset, size = callContext.stack.pop(), callContext.stack.pop()
  567. salt = callContext.stack.pop()
  568. input = callContext.memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
  569. gas = callContext.contract.Gas
  570. )
  571. // Apply EIP150
  572. gas -= gas / 64
  573. callContext.contract.UseGas(gas)
  574. // reuse size int for stackvalue
  575. stackvalue := size
  576. //TODO: use uint256.Int instead of converting with toBig()
  577. bigEndowment := big0
  578. if !endowment.IsZero() {
  579. bigEndowment = endowment.ToBig()
  580. }
  581. res, addr, returnGas, suberr := interpreter.evm.Create2(callContext.contract, input, gas,
  582. bigEndowment, &salt)
  583. // Push item on the stack based on the returned error.
  584. if suberr != nil {
  585. stackvalue.Clear()
  586. } else {
  587. stackvalue.SetBytes(addr.Bytes())
  588. }
  589. callContext.stack.push(&stackvalue)
  590. callContext.contract.Gas += returnGas
  591. if suberr == ErrExecutionReverted {
  592. return res, nil
  593. }
  594. return nil, nil
  595. }
  596. func opCall(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  597. stack := callContext.stack
  598. // Pop gas. The actual gas in interpreter.evm.callGasTemp.
  599. // We can use this as a temporary value
  600. temp := stack.pop()
  601. gas := interpreter.evm.callGasTemp
  602. // Pop other call parameters.
  603. addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  604. toAddr := common.Address(addr.Bytes20())
  605. // Get the arguments from the memory.
  606. args := callContext.memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
  607. var bigVal = big0
  608. //TODO: use uint256.Int instead of converting with toBig()
  609. // By using big0 here, we save an alloc for the most common case (non-ether-transferring contract calls),
  610. // but it would make more sense to extend the usage of uint256.Int
  611. if !value.IsZero() {
  612. gas += params.CallStipend
  613. bigVal = value.ToBig()
  614. }
  615. ret, returnGas, err := interpreter.evm.Call(callContext.contract, toAddr, args, gas, bigVal)
  616. if err != nil {
  617. temp.Clear()
  618. } else {
  619. temp.SetOne()
  620. }
  621. stack.push(&temp)
  622. if err == nil || err == ErrExecutionReverted {
  623. callContext.memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  624. }
  625. callContext.contract.Gas += returnGas
  626. return ret, nil
  627. }
  628. func opCallCode(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  629. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  630. stack := callContext.stack
  631. // We use it as a temporary value
  632. temp := stack.pop()
  633. gas := interpreter.evm.callGasTemp
  634. // Pop other call parameters.
  635. addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  636. toAddr := common.Address(addr.Bytes20())
  637. // Get arguments from the memory.
  638. args := callContext.memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
  639. //TODO: use uint256.Int instead of converting with toBig()
  640. var bigVal = big0
  641. if !value.IsZero() {
  642. gas += params.CallStipend
  643. bigVal = value.ToBig()
  644. }
  645. ret, returnGas, err := interpreter.evm.CallCode(callContext.contract, toAddr, args, gas, bigVal)
  646. if err != nil {
  647. temp.Clear()
  648. } else {
  649. temp.SetOne()
  650. }
  651. stack.push(&temp)
  652. if err == nil || err == ErrExecutionReverted {
  653. callContext.memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  654. }
  655. callContext.contract.Gas += returnGas
  656. return ret, nil
  657. }
  658. func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  659. stack := callContext.stack
  660. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  661. // We use it as a temporary value
  662. temp := stack.pop()
  663. gas := interpreter.evm.callGasTemp
  664. // Pop other call parameters.
  665. addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  666. toAddr := common.Address(addr.Bytes20())
  667. // Get arguments from the memory.
  668. args := callContext.memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
  669. ret, returnGas, err := interpreter.evm.DelegateCall(callContext.contract, toAddr, args, gas)
  670. if err != nil {
  671. temp.Clear()
  672. } else {
  673. temp.SetOne()
  674. }
  675. stack.push(&temp)
  676. if err == nil || err == ErrExecutionReverted {
  677. callContext.memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  678. }
  679. callContext.contract.Gas += returnGas
  680. return ret, nil
  681. }
  682. func opStaticCall(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  683. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  684. stack := callContext.stack
  685. // We use it as a temporary value
  686. temp := stack.pop()
  687. gas := interpreter.evm.callGasTemp
  688. // Pop other call parameters.
  689. addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  690. toAddr := common.Address(addr.Bytes20())
  691. // Get arguments from the memory.
  692. args := callContext.memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
  693. ret, returnGas, err := interpreter.evm.StaticCall(callContext.contract, toAddr, args, gas)
  694. if err != nil {
  695. temp.Clear()
  696. } else {
  697. temp.SetOne()
  698. }
  699. stack.push(&temp)
  700. if err == nil || err == ErrExecutionReverted {
  701. callContext.memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  702. }
  703. callContext.contract.Gas += returnGas
  704. return ret, nil
  705. }
  706. func opReturn(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  707. offset, size := callContext.stack.pop(), callContext.stack.pop()
  708. ret := callContext.memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
  709. return ret, nil
  710. }
  711. func opRevert(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  712. offset, size := callContext.stack.pop(), callContext.stack.pop()
  713. ret := callContext.memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
  714. return ret, nil
  715. }
  716. func opStop(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  717. return nil, nil
  718. }
  719. func opSuicide(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  720. beneficiary := callContext.stack.pop()
  721. balance := interpreter.evm.StateDB.GetBalance(callContext.contract.Address())
  722. interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance)
  723. interpreter.evm.StateDB.Suicide(callContext.contract.Address())
  724. return nil, nil
  725. }
  726. // following functions are used by the instruction jump table
  727. // make log instruction function
  728. func makeLog(size int) executionFunc {
  729. return func(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  730. topics := make([]common.Hash, size)
  731. stack := callContext.stack
  732. mStart, mSize := stack.pop(), stack.pop()
  733. for i := 0; i < size; i++ {
  734. addr := stack.pop()
  735. topics[i] = addr.Bytes32()
  736. }
  737. d := callContext.memory.GetCopy(int64(mStart.Uint64()), int64(mSize.Uint64()))
  738. interpreter.evm.StateDB.AddLog(&types.Log{
  739. Address: callContext.contract.Address(),
  740. Topics: topics,
  741. Data: d,
  742. // This is a non-consensus field, but assigned here because
  743. // core/state doesn't know the current block number.
  744. BlockNumber: interpreter.evm.Context.BlockNumber.Uint64(),
  745. })
  746. return nil, nil
  747. }
  748. }
  749. // opPush1 is a specialized version of pushN
  750. func opPush1(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  751. var (
  752. codeLen = uint64(len(callContext.contract.Code))
  753. integer = new(uint256.Int)
  754. )
  755. *pc += 1
  756. if *pc < codeLen {
  757. callContext.stack.push(integer.SetUint64(uint64(callContext.contract.Code[*pc])))
  758. } else {
  759. callContext.stack.push(integer.Clear())
  760. }
  761. return nil, nil
  762. }
  763. // make push instruction function
  764. func makePush(size uint64, pushByteSize int) executionFunc {
  765. return func(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  766. codeLen := len(callContext.contract.Code)
  767. startMin := codeLen
  768. if int(*pc+1) < startMin {
  769. startMin = int(*pc + 1)
  770. }
  771. endMin := codeLen
  772. if startMin+pushByteSize < endMin {
  773. endMin = startMin + pushByteSize
  774. }
  775. integer := new(uint256.Int)
  776. callContext.stack.push(integer.SetBytes(common.RightPadBytes(
  777. callContext.contract.Code[startMin:endMin], pushByteSize)))
  778. *pc += size
  779. return nil, nil
  780. }
  781. }
  782. // make dup instruction function
  783. func makeDup(size int64) executionFunc {
  784. return func(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  785. callContext.stack.dup(int(size))
  786. return nil, nil
  787. }
  788. }
  789. // make swap instruction function
  790. func makeSwap(size int64) executionFunc {
  791. // switch n + 1 otherwise n would be swapped with n
  792. size++
  793. return func(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  794. callContext.stack.swap(int(size))
  795. return nil, nil
  796. }
  797. }