instructions.go 29 KB

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