instructions.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. // Copyright 2014 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. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core/state"
  21. "github.com/ethereum/go-ethereum/crypto"
  22. "github.com/ethereum/go-ethereum/params"
  23. )
  24. type instrFn func(instr instruction, env Environment, context *Context, memory *Memory, stack *stack)
  25. type instrExFn func(instr instruction, ret *big.Int, env Environment, context *Context, memory *Memory, stack *stack)
  26. type instruction struct {
  27. op OpCode
  28. pc uint64
  29. fn instrFn
  30. specFn instrExFn
  31. data *big.Int
  32. gas *big.Int
  33. spop int
  34. spush int
  35. }
  36. func opStaticJump(instr instruction, ret *big.Int, env Environment, context *Context, memory *Memory, stack *stack) {
  37. ret.Set(instr.data)
  38. }
  39. func opAdd(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  40. x, y := stack.pop(), stack.pop()
  41. stack.push(U256(x.Add(x, y)))
  42. }
  43. func opSub(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  44. x, y := stack.pop(), stack.pop()
  45. stack.push(U256(x.Sub(x, y)))
  46. }
  47. func opMul(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  48. x, y := stack.pop(), stack.pop()
  49. stack.push(U256(x.Mul(x, y)))
  50. }
  51. func opDiv(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  52. x, y := stack.pop(), stack.pop()
  53. if y.Cmp(common.Big0) != 0 {
  54. stack.push(U256(x.Div(x, y)))
  55. } else {
  56. stack.push(new(big.Int))
  57. }
  58. }
  59. func opSdiv(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  60. x, y := S256(stack.pop()), S256(stack.pop())
  61. if y.Cmp(common.Big0) == 0 {
  62. stack.push(new(big.Int))
  63. return
  64. } else {
  65. n := new(big.Int)
  66. if new(big.Int).Mul(x, y).Cmp(common.Big0) < 0 {
  67. n.SetInt64(-1)
  68. } else {
  69. n.SetInt64(1)
  70. }
  71. res := x.Div(x.Abs(x), y.Abs(y))
  72. res.Mul(res, n)
  73. stack.push(U256(res))
  74. }
  75. }
  76. func opMod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  77. x, y := stack.pop(), stack.pop()
  78. if y.Cmp(common.Big0) == 0 {
  79. stack.push(new(big.Int))
  80. } else {
  81. stack.push(U256(x.Mod(x, y)))
  82. }
  83. }
  84. func opSmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  85. x, y := S256(stack.pop()), S256(stack.pop())
  86. if y.Cmp(common.Big0) == 0 {
  87. stack.push(new(big.Int))
  88. } else {
  89. n := new(big.Int)
  90. if x.Cmp(common.Big0) < 0 {
  91. n.SetInt64(-1)
  92. } else {
  93. n.SetInt64(1)
  94. }
  95. res := x.Mod(x.Abs(x), y.Abs(y))
  96. res.Mul(res, n)
  97. stack.push(U256(res))
  98. }
  99. }
  100. func opExp(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  101. x, y := stack.pop(), stack.pop()
  102. stack.push(U256(x.Exp(x, y, Pow256)))
  103. }
  104. func opSignExtend(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  105. back := stack.pop()
  106. if back.Cmp(big.NewInt(31)) < 0 {
  107. bit := uint(back.Uint64()*8 + 7)
  108. num := stack.pop()
  109. mask := back.Lsh(common.Big1, bit)
  110. mask.Sub(mask, common.Big1)
  111. if common.BitTest(num, int(bit)) {
  112. num.Or(num, mask.Not(mask))
  113. } else {
  114. num.And(num, mask)
  115. }
  116. stack.push(U256(num))
  117. }
  118. }
  119. func opNot(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  120. x := stack.pop()
  121. stack.push(U256(x.Not(x)))
  122. }
  123. func opLt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  124. x, y := stack.pop(), stack.pop()
  125. if x.Cmp(y) < 0 {
  126. stack.push(big.NewInt(1))
  127. } else {
  128. stack.push(new(big.Int))
  129. }
  130. }
  131. func opGt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  132. x, y := stack.pop(), stack.pop()
  133. if x.Cmp(y) > 0 {
  134. stack.push(big.NewInt(1))
  135. } else {
  136. stack.push(new(big.Int))
  137. }
  138. }
  139. func opSlt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  140. x, y := S256(stack.pop()), S256(stack.pop())
  141. if x.Cmp(S256(y)) < 0 {
  142. stack.push(big.NewInt(1))
  143. } else {
  144. stack.push(new(big.Int))
  145. }
  146. }
  147. func opSgt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  148. x, y := S256(stack.pop()), S256(stack.pop())
  149. if x.Cmp(y) > 0 {
  150. stack.push(big.NewInt(1))
  151. } else {
  152. stack.push(new(big.Int))
  153. }
  154. }
  155. func opEq(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  156. x, y := stack.pop(), stack.pop()
  157. if x.Cmp(y) == 0 {
  158. stack.push(big.NewInt(1))
  159. } else {
  160. stack.push(new(big.Int))
  161. }
  162. }
  163. func opIszero(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  164. x := stack.pop()
  165. if x.Cmp(common.Big0) > 0 {
  166. stack.push(new(big.Int))
  167. } else {
  168. stack.push(big.NewInt(1))
  169. }
  170. }
  171. func opAnd(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  172. x, y := stack.pop(), stack.pop()
  173. stack.push(x.And(x, y))
  174. }
  175. func opOr(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  176. x, y := stack.pop(), stack.pop()
  177. stack.push(x.Or(x, y))
  178. }
  179. func opXor(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  180. x, y := stack.pop(), stack.pop()
  181. stack.push(x.Xor(x, y))
  182. }
  183. func opByte(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  184. th, val := stack.pop(), stack.pop()
  185. if th.Cmp(big.NewInt(32)) < 0 {
  186. byte := big.NewInt(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()]))
  187. stack.push(byte)
  188. } else {
  189. stack.push(new(big.Int))
  190. }
  191. }
  192. func opAddmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  193. x, y, z := stack.pop(), stack.pop(), stack.pop()
  194. if z.Cmp(Zero) > 0 {
  195. add := x.Add(x, y)
  196. add.Mod(add, z)
  197. stack.push(U256(add))
  198. } else {
  199. stack.push(new(big.Int))
  200. }
  201. }
  202. func opMulmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  203. x, y, z := stack.pop(), stack.pop(), stack.pop()
  204. if z.Cmp(Zero) > 0 {
  205. mul := x.Mul(x, y)
  206. mul.Mod(mul, z)
  207. stack.push(U256(mul))
  208. } else {
  209. stack.push(new(big.Int))
  210. }
  211. }
  212. func opSha3(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  213. offset, size := stack.pop(), stack.pop()
  214. hash := crypto.Sha3(memory.Get(offset.Int64(), size.Int64()))
  215. stack.push(common.BytesToBig(hash))
  216. }
  217. func opAddress(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  218. stack.push(common.Bytes2Big(context.Address().Bytes()))
  219. }
  220. func opBalance(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  221. addr := common.BigToAddress(stack.pop())
  222. balance := env.State().GetBalance(addr)
  223. stack.push(new(big.Int).Set(balance))
  224. }
  225. func opOrigin(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  226. stack.push(env.Origin().Big())
  227. }
  228. func opCaller(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  229. stack.push(common.Bytes2Big(context.caller.Address().Bytes()))
  230. }
  231. func opCallValue(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  232. stack.push(new(big.Int).Set(context.value))
  233. }
  234. func opCalldataLoad(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  235. stack.push(common.Bytes2Big(getData(context.Input, stack.pop(), common.Big32)))
  236. }
  237. func opCalldataSize(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  238. stack.push(big.NewInt(int64(len(context.Input))))
  239. }
  240. func opCalldataCopy(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  241. var (
  242. mOff = stack.pop()
  243. cOff = stack.pop()
  244. l = stack.pop()
  245. )
  246. memory.Set(mOff.Uint64(), l.Uint64(), getData(context.Input, cOff, l))
  247. }
  248. func opExtCodeSize(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  249. addr := common.BigToAddress(stack.pop())
  250. l := big.NewInt(int64(len(env.State().GetCode(addr))))
  251. stack.push(l)
  252. }
  253. func opCodeSize(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  254. l := big.NewInt(int64(len(context.Code)))
  255. stack.push(l)
  256. }
  257. func opCodeCopy(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  258. var (
  259. mOff = stack.pop()
  260. cOff = stack.pop()
  261. l = stack.pop()
  262. )
  263. codeCopy := getData(context.Code, cOff, l)
  264. memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
  265. }
  266. func opExtCodeCopy(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  267. var (
  268. addr = common.BigToAddress(stack.pop())
  269. mOff = stack.pop()
  270. cOff = stack.pop()
  271. l = stack.pop()
  272. )
  273. codeCopy := getData(env.State().GetCode(addr), cOff, l)
  274. memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
  275. }
  276. func opGasprice(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  277. stack.push(new(big.Int).Set(context.Price))
  278. }
  279. func opBlockhash(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  280. num := stack.pop()
  281. n := new(big.Int).Sub(env.BlockNumber(), common.Big257)
  282. if num.Cmp(n) > 0 && num.Cmp(env.BlockNumber()) < 0 {
  283. stack.push(env.GetHash(num.Uint64()).Big())
  284. } else {
  285. stack.push(new(big.Int))
  286. }
  287. }
  288. func opCoinbase(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  289. stack.push(env.Coinbase().Big())
  290. }
  291. func opTimestamp(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  292. stack.push(U256(new(big.Int).Set(env.Time())))
  293. }
  294. func opNumber(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  295. stack.push(U256(new(big.Int).Set(env.BlockNumber())))
  296. }
  297. func opDifficulty(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  298. stack.push(U256(new(big.Int).Set(env.Difficulty())))
  299. }
  300. func opGasLimit(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  301. stack.push(U256(new(big.Int).Set(env.GasLimit())))
  302. }
  303. func opPop(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  304. stack.pop()
  305. }
  306. func opPush(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  307. stack.push(new(big.Int).Set(instr.data))
  308. }
  309. func opDup(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  310. stack.dup(int(instr.data.Int64()))
  311. }
  312. func opSwap(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  313. stack.swap(int(instr.data.Int64()))
  314. }
  315. func opLog(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  316. n := int(instr.data.Int64())
  317. topics := make([]common.Hash, n)
  318. mStart, mSize := stack.pop(), stack.pop()
  319. for i := 0; i < n; i++ {
  320. topics[i] = common.BigToHash(stack.pop())
  321. }
  322. d := memory.Get(mStart.Int64(), mSize.Int64())
  323. log := state.NewLog(context.Address(), topics, d, env.BlockNumber().Uint64())
  324. env.AddLog(log)
  325. }
  326. func opMload(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  327. offset := stack.pop()
  328. val := common.BigD(memory.Get(offset.Int64(), 32))
  329. stack.push(val)
  330. }
  331. func opMstore(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  332. // pop value of the stack
  333. mStart, val := stack.pop(), stack.pop()
  334. memory.Set(mStart.Uint64(), 32, common.BigToBytes(val, 256))
  335. }
  336. func opMstore8(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  337. off, val := stack.pop().Int64(), stack.pop().Int64()
  338. memory.store[off] = byte(val & 0xff)
  339. }
  340. func opSload(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  341. loc := common.BigToHash(stack.pop())
  342. val := env.State().GetState(context.Address(), loc).Big()
  343. stack.push(val)
  344. }
  345. func opSstore(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  346. loc := common.BigToHash(stack.pop())
  347. val := stack.pop()
  348. env.State().SetState(context.Address(), loc, common.BigToHash(val))
  349. }
  350. func opJump(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {}
  351. func opJumpi(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {}
  352. func opJumpdest(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {}
  353. func opPc(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  354. stack.push(new(big.Int).Set(instr.data))
  355. }
  356. func opMsize(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  357. stack.push(big.NewInt(int64(memory.Len())))
  358. }
  359. func opGas(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  360. stack.push(new(big.Int).Set(context.Gas))
  361. }
  362. func opCreate(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  363. var (
  364. value = stack.pop()
  365. offset, size = stack.pop(), stack.pop()
  366. input = memory.Get(offset.Int64(), size.Int64())
  367. gas = new(big.Int).Set(context.Gas)
  368. addr common.Address
  369. )
  370. context.UseGas(context.Gas)
  371. ret, suberr, ref := env.Create(context, input, gas, context.Price, value)
  372. if suberr != nil {
  373. stack.push(new(big.Int))
  374. } else {
  375. // gas < len(ret) * Createinstr.dataGas == NO_CODE
  376. dataGas := big.NewInt(int64(len(ret)))
  377. dataGas.Mul(dataGas, params.CreateDataGas)
  378. if context.UseGas(dataGas) {
  379. ref.SetCode(ret)
  380. }
  381. addr = ref.Address()
  382. stack.push(addr.Big())
  383. }
  384. }
  385. func opCall(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  386. gas := stack.pop()
  387. // pop gas and value of the stack.
  388. addr, value := stack.pop(), stack.pop()
  389. value = U256(value)
  390. // pop input size and offset
  391. inOffset, inSize := stack.pop(), stack.pop()
  392. // pop return size and offset
  393. retOffset, retSize := stack.pop(), stack.pop()
  394. address := common.BigToAddress(addr)
  395. // Get the arguments from the memory
  396. args := memory.Get(inOffset.Int64(), inSize.Int64())
  397. if len(value.Bytes()) > 0 {
  398. gas.Add(gas, params.CallStipend)
  399. }
  400. ret, err := env.Call(context, address, args, gas, context.Price, value)
  401. if err != nil {
  402. stack.push(new(big.Int))
  403. } else {
  404. stack.push(big.NewInt(1))
  405. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  406. }
  407. }
  408. func opCallCode(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  409. gas := stack.pop()
  410. // pop gas and value of the stack.
  411. addr, value := stack.pop(), stack.pop()
  412. value = U256(value)
  413. // pop input size and offset
  414. inOffset, inSize := stack.pop(), stack.pop()
  415. // pop return size and offset
  416. retOffset, retSize := stack.pop(), stack.pop()
  417. address := common.BigToAddress(addr)
  418. // Get the arguments from the memory
  419. args := memory.Get(inOffset.Int64(), inSize.Int64())
  420. if len(value.Bytes()) > 0 {
  421. gas.Add(gas, params.CallStipend)
  422. }
  423. ret, err := env.CallCode(context, address, args, gas, context.Price, value)
  424. if err != nil {
  425. stack.push(new(big.Int))
  426. } else {
  427. stack.push(big.NewInt(1))
  428. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  429. }
  430. }
  431. func opReturn(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {}
  432. func opStop(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {}
  433. func opSuicide(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
  434. receiver := env.State().GetOrNewStateObject(common.BigToAddress(stack.pop()))
  435. balance := env.State().GetBalance(context.Address())
  436. receiver.AddBalance(balance)
  437. env.State().Delete(context.Address())
  438. }