instructions.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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, scope *ScopeContext) ([]byte, error) {
  25. x, y := scope.Stack.pop(), scope.Stack.peek()
  26. y.Add(&x, y)
  27. return nil, nil
  28. }
  29. func opSub(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  30. x, y := scope.Stack.pop(), scope.Stack.peek()
  31. y.Sub(&x, y)
  32. return nil, nil
  33. }
  34. func opMul(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  35. x, y := scope.Stack.pop(), scope.Stack.peek()
  36. y.Mul(&x, y)
  37. return nil, nil
  38. }
  39. func opDiv(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  40. x, y := scope.Stack.pop(), scope.Stack.peek()
  41. y.Div(&x, y)
  42. return nil, nil
  43. }
  44. func opSdiv(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  45. x, y := scope.Stack.pop(), scope.Stack.peek()
  46. y.SDiv(&x, y)
  47. return nil, nil
  48. }
  49. func opMod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  50. x, y := scope.Stack.pop(), scope.Stack.peek()
  51. y.Mod(&x, y)
  52. return nil, nil
  53. }
  54. func opSmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  55. x, y := scope.Stack.pop(), scope.Stack.peek()
  56. y.SMod(&x, y)
  57. return nil, nil
  58. }
  59. func opExp(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  60. base, exponent := scope.Stack.pop(), scope.Stack.peek()
  61. exponent.Exp(&base, exponent)
  62. return nil, nil
  63. }
  64. func opSignExtend(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  65. back, num := scope.Stack.pop(), scope.Stack.peek()
  66. num.ExtendSign(num, &back)
  67. return nil, nil
  68. }
  69. func opNot(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  70. x := scope.Stack.peek()
  71. x.Not(x)
  72. return nil, nil
  73. }
  74. func opLt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  75. x, y := scope.Stack.pop(), scope.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, scope *ScopeContext) ([]byte, error) {
  84. x, y := scope.Stack.pop(), scope.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, scope *ScopeContext) ([]byte, error) {
  93. x, y := scope.Stack.pop(), scope.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, scope *ScopeContext) ([]byte, error) {
  102. x, y := scope.Stack.pop(), scope.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, scope *ScopeContext) ([]byte, error) {
  111. x, y := scope.Stack.pop(), scope.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, scope *ScopeContext) ([]byte, error) {
  120. x := scope.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, scope *ScopeContext) ([]byte, error) {
  129. x, y := scope.Stack.pop(), scope.Stack.peek()
  130. y.And(&x, y)
  131. return nil, nil
  132. }
  133. func opOr(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  134. x, y := scope.Stack.pop(), scope.Stack.peek()
  135. y.Or(&x, y)
  136. return nil, nil
  137. }
  138. func opXor(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  139. x, y := scope.Stack.pop(), scope.Stack.peek()
  140. y.Xor(&x, y)
  141. return nil, nil
  142. }
  143. func opByte(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  144. th, val := scope.Stack.pop(), scope.Stack.peek()
  145. val.Byte(&th)
  146. return nil, nil
  147. }
  148. func opAddmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  149. x, y, z := scope.Stack.pop(), scope.Stack.pop(), scope.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, scope *ScopeContext) ([]byte, error) {
  158. x, y, z := scope.Stack.pop(), scope.Stack.pop(), scope.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, scope *ScopeContext) ([]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 := scope.Stack.pop(), scope.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, scope *ScopeContext) ([]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 := scope.Stack.pop(), scope.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, scope *ScopeContext) ([]byte, error) {
  192. shift, value := scope.Stack.pop(), scope.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, scope *ScopeContext) ([]byte, error) {
  207. offset, size := scope.Stack.pop(), scope.Stack.peek()
  208. data := scope.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, scope *ScopeContext) ([]byte, error) {
  224. scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Address().Bytes()))
  225. return nil, nil
  226. }
  227. func opBalance(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  228. slot := scope.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, scope *ScopeContext) ([]byte, error) {
  234. scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.Origin.Bytes()))
  235. return nil, nil
  236. }
  237. func opCaller(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  238. scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Caller().Bytes()))
  239. return nil, nil
  240. }
  241. func opCallValue(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  242. v, _ := uint256.FromBig(scope.Contract.value)
  243. scope.Stack.push(v)
  244. return nil, nil
  245. }
  246. func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  247. x := scope.Stack.peek()
  248. if offset, overflow := x.Uint64WithOverflow(); !overflow {
  249. data := getData(scope.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, scope *ScopeContext) ([]byte, error) {
  257. scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(scope.Contract.Input))))
  258. return nil, nil
  259. }
  260. func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  261. var (
  262. memOffset = scope.Stack.pop()
  263. dataOffset = scope.Stack.pop()
  264. length = scope.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. scope.Memory.Set(memOffset64, length64, getData(scope.Contract.Input, dataOffset64, length64))
  274. return nil, nil
  275. }
  276. func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  277. scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(interpreter.returnData))))
  278. return nil, nil
  279. }
  280. func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  281. var (
  282. memOffset = scope.Stack.pop()
  283. dataOffset = scope.Stack.pop()
  284. length = scope.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. scope.Memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[offset64:end64])
  298. return nil, nil
  299. }
  300. func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  301. slot := scope.Stack.peek()
  302. slot.SetUint64(uint64(interpreter.evm.StateDB.GetCodeSize(slot.Bytes20())))
  303. return nil, nil
  304. }
  305. func opCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  306. l := new(uint256.Int)
  307. l.SetUint64(uint64(len(scope.Contract.Code)))
  308. scope.Stack.push(l)
  309. return nil, nil
  310. }
  311. func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  312. var (
  313. memOffset = scope.Stack.pop()
  314. codeOffset = scope.Stack.pop()
  315. length = scope.Stack.pop()
  316. )
  317. uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
  318. if overflow {
  319. uint64CodeOffset = 0xffffffffffffffff
  320. }
  321. codeCopy := getData(scope.Contract.Code, uint64CodeOffset, length.Uint64())
  322. scope.Memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
  323. return nil, nil
  324. }
  325. func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  326. var (
  327. stack = scope.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. scope.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, scope *ScopeContext) ([]byte, error) {
  369. slot := scope.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, scope *ScopeContext) ([]byte, error) {
  379. v, _ := uint256.FromBig(interpreter.evm.GasPrice)
  380. scope.Stack.push(v)
  381. return nil, nil
  382. }
  383. func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  384. num := scope.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, scope *ScopeContext) ([]byte, error) {
  405. scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.Context.Coinbase.Bytes()))
  406. return nil, nil
  407. }
  408. func opTimestamp(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  409. v, _ := uint256.FromBig(interpreter.evm.Context.Time)
  410. scope.Stack.push(v)
  411. return nil, nil
  412. }
  413. func opNumber(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  414. v, _ := uint256.FromBig(interpreter.evm.Context.BlockNumber)
  415. scope.Stack.push(v)
  416. return nil, nil
  417. }
  418. func opDifficulty(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  419. v, _ := uint256.FromBig(interpreter.evm.Context.Difficulty)
  420. scope.Stack.push(v)
  421. return nil, nil
  422. }
  423. func opGasLimit(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  424. scope.Stack.push(new(uint256.Int).SetUint64(interpreter.evm.Context.GasLimit))
  425. return nil, nil
  426. }
  427. func opPop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  428. scope.Stack.pop()
  429. return nil, nil
  430. }
  431. func opMload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  432. v := scope.Stack.peek()
  433. offset := int64(v.Uint64())
  434. v.SetBytes(scope.Memory.GetPtr(offset, 32))
  435. return nil, nil
  436. }
  437. func opMstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  438. // pop value of the stack
  439. mStart, val := scope.Stack.pop(), scope.Stack.pop()
  440. scope.Memory.Set32(mStart.Uint64(), &val)
  441. return nil, nil
  442. }
  443. func opMstore8(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  444. off, val := scope.Stack.pop(), scope.Stack.pop()
  445. scope.Memory.store[off.Uint64()] = byte(val.Uint64())
  446. return nil, nil
  447. }
  448. func opSload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  449. loc := scope.Stack.peek()
  450. hash := common.Hash(loc.Bytes32())
  451. val := interpreter.evm.StateDB.GetState(scope.Contract.Address(), hash)
  452. loc.SetBytes(val.Bytes())
  453. return nil, nil
  454. }
  455. func opSstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  456. loc := scope.Stack.pop()
  457. val := scope.Stack.pop()
  458. interpreter.evm.StateDB.SetState(scope.Contract.Address(),
  459. loc.Bytes32(), val.Bytes32())
  460. return nil, nil
  461. }
  462. func opJump(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  463. pos := scope.Stack.pop()
  464. if !scope.Contract.validJumpdest(&pos) {
  465. return nil, ErrInvalidJump
  466. }
  467. *pc = pos.Uint64()
  468. return nil, nil
  469. }
  470. func opJumpi(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  471. pos, cond := scope.Stack.pop(), scope.Stack.pop()
  472. if !cond.IsZero() {
  473. if !scope.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, scope *ScopeContext) ([]byte, error) {
  483. return nil, nil
  484. }
  485. func opPc(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  486. scope.Stack.push(new(uint256.Int).SetUint64(*pc))
  487. return nil, nil
  488. }
  489. func opMsize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  490. scope.Stack.push(new(uint256.Int).SetUint64(uint64(scope.Memory.Len())))
  491. return nil, nil
  492. }
  493. func opGas(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  494. scope.Stack.push(new(uint256.Int).SetUint64(scope.Contract.Gas))
  495. return nil, nil
  496. }
  497. func opCreate(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  498. var (
  499. value = scope.Stack.pop()
  500. offset, size = scope.Stack.pop(), scope.Stack.pop()
  501. input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
  502. gas = scope.Contract.Gas
  503. )
  504. if interpreter.evm.chainRules.IsEIP150 {
  505. gas -= gas / 64
  506. }
  507. // reuse size int for stackvalue
  508. stackvalue := size
  509. scope.Contract.UseGas(gas)
  510. //TODO: use uint256.Int instead of converting with toBig()
  511. var bigVal = big0
  512. if !value.IsZero() {
  513. bigVal = value.ToBig()
  514. }
  515. res, addr, returnGas, suberr := interpreter.evm.Create(scope.Contract, input, gas, bigVal)
  516. // Push item on the stack based on the returned error. If the ruleset is
  517. // homestead we must check for CodeStoreOutOfGasError (homestead only
  518. // rule) and treat as an error, if the ruleset is frontier we must
  519. // ignore this error and pretend the operation was successful.
  520. if interpreter.evm.chainRules.IsHomestead && suberr == ErrCodeStoreOutOfGas {
  521. stackvalue.Clear()
  522. } else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
  523. stackvalue.Clear()
  524. } else {
  525. stackvalue.SetBytes(addr.Bytes())
  526. }
  527. scope.Stack.push(&stackvalue)
  528. scope.Contract.Gas += returnGas
  529. if suberr == ErrExecutionReverted {
  530. return res, nil
  531. }
  532. return nil, nil
  533. }
  534. func opCreate2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  535. var (
  536. endowment = scope.Stack.pop()
  537. offset, size = scope.Stack.pop(), scope.Stack.pop()
  538. salt = scope.Stack.pop()
  539. input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
  540. gas = scope.Contract.Gas
  541. )
  542. // Apply EIP150
  543. gas -= gas / 64
  544. scope.Contract.UseGas(gas)
  545. // reuse size int for stackvalue
  546. stackvalue := size
  547. //TODO: use uint256.Int instead of converting with toBig()
  548. bigEndowment := big0
  549. if !endowment.IsZero() {
  550. bigEndowment = endowment.ToBig()
  551. }
  552. res, addr, returnGas, suberr := interpreter.evm.Create2(scope.Contract, input, gas,
  553. bigEndowment, &salt)
  554. // Push item on the stack based on the returned error.
  555. if suberr != nil {
  556. stackvalue.Clear()
  557. } else {
  558. stackvalue.SetBytes(addr.Bytes())
  559. }
  560. scope.Stack.push(&stackvalue)
  561. scope.Contract.Gas += returnGas
  562. if suberr == ErrExecutionReverted {
  563. return res, nil
  564. }
  565. return nil, nil
  566. }
  567. func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  568. stack := scope.Stack
  569. // Pop gas. The actual gas in interpreter.evm.callGasTemp.
  570. // We can use this as a temporary value
  571. temp := stack.pop()
  572. gas := interpreter.evm.callGasTemp
  573. // Pop other call parameters.
  574. addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  575. toAddr := common.Address(addr.Bytes20())
  576. // Get the arguments from the memory.
  577. args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
  578. var bigVal = big0
  579. //TODO: use uint256.Int instead of converting with toBig()
  580. // By using big0 here, we save an alloc for the most common case (non-ether-transferring contract calls),
  581. // but it would make more sense to extend the usage of uint256.Int
  582. if !value.IsZero() {
  583. gas += params.CallStipend
  584. bigVal = value.ToBig()
  585. }
  586. ret, returnGas, err := interpreter.evm.Call(scope.Contract, toAddr, args, gas, bigVal)
  587. if err != nil {
  588. temp.Clear()
  589. } else {
  590. temp.SetOne()
  591. }
  592. stack.push(&temp)
  593. if err == nil || err == ErrExecutionReverted {
  594. scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  595. }
  596. scope.Contract.Gas += returnGas
  597. return ret, nil
  598. }
  599. func opCallCode(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  600. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  601. stack := scope.Stack
  602. // We use it as a temporary value
  603. temp := stack.pop()
  604. gas := interpreter.evm.callGasTemp
  605. // Pop other call parameters.
  606. addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  607. toAddr := common.Address(addr.Bytes20())
  608. // Get arguments from the memory.
  609. args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
  610. //TODO: use uint256.Int instead of converting with toBig()
  611. var bigVal = big0
  612. if !value.IsZero() {
  613. gas += params.CallStipend
  614. bigVal = value.ToBig()
  615. }
  616. ret, returnGas, err := interpreter.evm.CallCode(scope.Contract, toAddr, args, gas, bigVal)
  617. if err != nil {
  618. temp.Clear()
  619. } else {
  620. temp.SetOne()
  621. }
  622. stack.push(&temp)
  623. if err == nil || err == ErrExecutionReverted {
  624. scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  625. }
  626. scope.Contract.Gas += returnGas
  627. return ret, nil
  628. }
  629. func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  630. stack := scope.Stack
  631. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  632. // We use it as a temporary value
  633. temp := stack.pop()
  634. gas := interpreter.evm.callGasTemp
  635. // Pop other call parameters.
  636. addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  637. toAddr := common.Address(addr.Bytes20())
  638. // Get arguments from the memory.
  639. args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
  640. ret, returnGas, err := interpreter.evm.DelegateCall(scope.Contract, toAddr, args, gas)
  641. if err != nil {
  642. temp.Clear()
  643. } else {
  644. temp.SetOne()
  645. }
  646. stack.push(&temp)
  647. if err == nil || err == ErrExecutionReverted {
  648. scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  649. }
  650. scope.Contract.Gas += returnGas
  651. return ret, nil
  652. }
  653. func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  654. // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
  655. stack := scope.Stack
  656. // We use it as a temporary value
  657. temp := stack.pop()
  658. gas := interpreter.evm.callGasTemp
  659. // Pop other call parameters.
  660. addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  661. toAddr := common.Address(addr.Bytes20())
  662. // Get arguments from the memory.
  663. args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
  664. ret, returnGas, err := interpreter.evm.StaticCall(scope.Contract, toAddr, args, gas)
  665. if err != nil {
  666. temp.Clear()
  667. } else {
  668. temp.SetOne()
  669. }
  670. stack.push(&temp)
  671. if err == nil || err == ErrExecutionReverted {
  672. scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  673. }
  674. scope.Contract.Gas += returnGas
  675. return ret, nil
  676. }
  677. func opReturn(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  678. offset, size := scope.Stack.pop(), scope.Stack.pop()
  679. ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
  680. return ret, nil
  681. }
  682. func opRevert(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  683. offset, size := scope.Stack.pop(), scope.Stack.pop()
  684. ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
  685. return ret, nil
  686. }
  687. func opStop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  688. return nil, nil
  689. }
  690. func opSuicide(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  691. beneficiary := scope.Stack.pop()
  692. balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address())
  693. interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance)
  694. interpreter.evm.StateDB.Suicide(scope.Contract.Address())
  695. return nil, nil
  696. }
  697. // following functions are used by the instruction jump table
  698. // make log instruction function
  699. func makeLog(size int) executionFunc {
  700. return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  701. topics := make([]common.Hash, size)
  702. stack := scope.Stack
  703. mStart, mSize := stack.pop(), stack.pop()
  704. for i := 0; i < size; i++ {
  705. addr := stack.pop()
  706. topics[i] = addr.Bytes32()
  707. }
  708. d := scope.Memory.GetCopy(int64(mStart.Uint64()), int64(mSize.Uint64()))
  709. interpreter.evm.StateDB.AddLog(&types.Log{
  710. Address: scope.Contract.Address(),
  711. Topics: topics,
  712. Data: d,
  713. // This is a non-consensus field, but assigned here because
  714. // core/state doesn't know the current block number.
  715. BlockNumber: interpreter.evm.Context.BlockNumber.Uint64(),
  716. })
  717. return nil, nil
  718. }
  719. }
  720. // opPush1 is a specialized version of pushN
  721. func opPush1(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  722. var (
  723. codeLen = uint64(len(scope.Contract.Code))
  724. integer = new(uint256.Int)
  725. )
  726. *pc += 1
  727. if *pc < codeLen {
  728. scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc])))
  729. } else {
  730. scope.Stack.push(integer.Clear())
  731. }
  732. return nil, nil
  733. }
  734. // make push instruction function
  735. func makePush(size uint64, pushByteSize int) executionFunc {
  736. return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  737. codeLen := len(scope.Contract.Code)
  738. startMin := codeLen
  739. if int(*pc+1) < startMin {
  740. startMin = int(*pc + 1)
  741. }
  742. endMin := codeLen
  743. if startMin+pushByteSize < endMin {
  744. endMin = startMin + pushByteSize
  745. }
  746. integer := new(uint256.Int)
  747. scope.Stack.push(integer.SetBytes(common.RightPadBytes(
  748. scope.Contract.Code[startMin:endMin], pushByteSize)))
  749. *pc += size
  750. return nil, nil
  751. }
  752. }
  753. // make dup instruction function
  754. func makeDup(size int64) executionFunc {
  755. return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  756. scope.Stack.dup(int(size))
  757. return nil, nil
  758. }
  759. }
  760. // make swap instruction function
  761. func makeSwap(size int64) executionFunc {
  762. // switch n + 1 otherwise n would be swapped with n
  763. size++
  764. return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  765. scope.Stack.swap(int(size))
  766. return nil, nil
  767. }
  768. }