vm_debug.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. package vm
  2. import (
  3. "fmt"
  4. "math/big"
  5. "github.com/ethereum/go-ethereum/crypto"
  6. "github.com/ethereum/go-ethereum/ethutil"
  7. )
  8. type DebugVm struct {
  9. env Environment
  10. logTy byte
  11. logStr string
  12. err error
  13. // Debugging
  14. Dbg Debugger
  15. BreakPoints []int64
  16. Stepping bool
  17. Fn string
  18. Recoverable bool
  19. depth int
  20. }
  21. type Options struct {
  22. Address, Caller []byte
  23. Data []byte
  24. Code []byte
  25. Value, Gas, Price *big.Int
  26. }
  27. func NewDebugVm(env Environment) *DebugVm {
  28. lt := LogTyPretty
  29. if ethutil.Config.Diff {
  30. lt = LogTyDiff
  31. }
  32. return &DebugVm{env: env, logTy: lt, Recoverable: true}
  33. }
  34. //func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) {
  35. func (self *DebugVm) Run(call Options) (ret []byte, gas *big.Int, err error) {
  36. // Don't bother with the execution if there's no code.
  37. if len(call.Code) == 0 {
  38. return nil, new(big.Int), nil
  39. }
  40. self.depth++
  41. if self.Recoverable {
  42. // Recover from any require exception
  43. defer func() {
  44. if r := recover(); r != nil {
  45. self.Endl()
  46. gas = new(big.Int)
  47. err = fmt.Errorf("%v", r)
  48. }
  49. }()
  50. }
  51. gas = new(big.Int).Set(opt.Gas)
  52. var (
  53. op OpCode
  54. destinations = analyseJumpDests(call.Code)
  55. mem = NewMemory()
  56. stack = NewStack()
  57. pc = big.NewInt(0)
  58. step = 0
  59. prevStep = 0
  60. //statedb = self.env.State()
  61. require = func(m int) {
  62. if stack.Len() < m {
  63. panic(fmt.Sprintf("%04v (%v) stack err size = %d, required = %d", pc, op, stack.Len(), m))
  64. }
  65. }
  66. useGas = func(amount *big.Int) bool {
  67. if amount.Cmp(gas) > 0 {
  68. return false
  69. }
  70. gas.Sub(gas, amount)
  71. return true
  72. }
  73. jump = func(from, to *big.Int) {
  74. p := to.Uint64()
  75. self.Printf(" ~> %v", to)
  76. // Return to start
  77. if p == 0 {
  78. pc = big.NewInt(0)
  79. } else {
  80. nop := OpCode(call.GetOp(p))
  81. if !(nop == JUMPDEST || destinations[from.Int64()] != nil) {
  82. panic(fmt.Sprintf("JUMP missed JUMPDEST (%v) %v", nop, p))
  83. } else if nop == JUMP || nop == JUMPI {
  84. panic(fmt.Sprintf("not allowed to JUMP(I) in to JUMP"))
  85. }
  86. pc = to
  87. }
  88. self.Endl()
  89. }
  90. )
  91. vmlogger.Debugf("(%d) %x gas: %v (d) %x\n", self.depth, call.Address, gas, call.Data)
  92. for {
  93. prevStep = step
  94. // The base for all big integer arithmetic
  95. base := new(big.Int)
  96. step++
  97. // Get the memory location of pc
  98. op = call.GetOp(pc.Uint64())
  99. // XXX Leave this Println intact. Don't change this to the log system.
  100. // Used for creating diffs between implementations
  101. /*
  102. if self.logTy == LogTyDiff {
  103. switch op {
  104. case STOP, RETURN, SUICIDE:
  105. statedb.GetStateObject(closure.Address()).EachStorage(func(key string, value *ethutil.Value) {
  106. value.Decode()
  107. fmt.Printf("%x %x\n", new(big.Int).SetBytes([]byte(key)).Bytes(), value.Bytes())
  108. })
  109. }
  110. b := pc.Bytes()
  111. if len(b) == 0 {
  112. b = []byte{0}
  113. }
  114. fmt.Printf("%x %x %x %x\n", closure.Address(), b, []byte{byte(op)}, closure.Gas.Bytes())
  115. }
  116. */
  117. reqGas := new(big.Int)
  118. addStepGasUsage := func(amount *big.Int) {
  119. if amount.Cmp(ethutil.Big0) >= 0 {
  120. reqGas.Add(reqGas, amount)
  121. }
  122. }
  123. addStepGasUsage(GasStep)
  124. var newMemSize *big.Int = ethutil.Big0
  125. // Stack Check, memory resize & gas phase
  126. switch op {
  127. // Stack checks only
  128. case ISZERO, CALLDATALOAD, POP, JUMP, NOT: // 1
  129. require(1)
  130. case ADD, SUB, DIV, SDIV, MOD, SMOD, LT, GT, SLT, SGT, EQ, AND, OR, XOR, BYTE: // 2
  131. require(2)
  132. case ADDMOD, MULMOD: // 3
  133. require(3)
  134. case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16:
  135. n := int(op - SWAP1 + 2)
  136. require(n)
  137. case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16:
  138. n := int(op - DUP1 + 1)
  139. require(n)
  140. case LOG0, LOG1, LOG2, LOG3, LOG4:
  141. n := int(op - LOG0)
  142. require(n + 2)
  143. mSize, mStart := stack.Peekn()
  144. reqGs.Set(GasLog)
  145. addStepGasUsage(new(big.Int).Mul(big.NewInt(int64(n)), GasLog))
  146. addStepGasUsage(new(big.Int).Add(mSize, mStart))
  147. case EXP:
  148. require(2)
  149. exp := new(big.Int).Set(stack.data[stack.Len()-2])
  150. nbytes := 0
  151. for exp.Cmp(ethutil.Big0) > 0 {
  152. nbytes += 1
  153. exp.Rsh(exp, 8)
  154. }
  155. gas.Set(big.NewInt(int64(nbytes + 1)))
  156. // Gas only
  157. case STOP:
  158. reqGas.Set(ethutil.Big0)
  159. case SUICIDE:
  160. require(1)
  161. reqGas.Set(ethutil.Big0)
  162. case SLOAD:
  163. require(1)
  164. reqGas.Set(GasSLoad)
  165. // Memory resize & Gas
  166. case SSTORE:
  167. require(2)
  168. var mult *big.Int
  169. y, x := stack.Peekn()
  170. val := ethutil.BigD(self.env.GetState(x.Bytes())) //closure.GetStorage(x)
  171. if val.BigInt().Cmp(ethutil.Big0) == 0 && len(y.Bytes()) > 0 {
  172. // 0 => non 0
  173. mult = ethutil.Big3
  174. } else if val.BigInt().Cmp(ethutil.Big0) != 0 && len(y.Bytes()) == 0 {
  175. //statedb.Refund(closure.caller.Address(), GasSStoreRefund, closure.Price)
  176. self.env.Refund(call.Caller, GasSStoreRefund, call.Price)
  177. mult = ethutil.Big0
  178. } else {
  179. // non 0 => non 0
  180. mult = ethutil.Big1
  181. }
  182. reqGas.Set(new(big.Int).Mul(mult, GasSStore))
  183. case BALANCE:
  184. require(1)
  185. reqGas.Set(GasBalance)
  186. case MSTORE:
  187. require(2)
  188. newMemSize = calcMemSize(stack.Peek(), u256(32))
  189. case MLOAD:
  190. require(1)
  191. newMemSize = calcMemSize(stack.Peek(), u256(32))
  192. case MSTORE8:
  193. require(2)
  194. newMemSize = calcMemSize(stack.Peek(), u256(1))
  195. case RETURN:
  196. require(2)
  197. newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-2])
  198. case SHA3:
  199. require(2)
  200. reqGas.Set(GasSha)
  201. newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-2])
  202. case CALLDATACOPY:
  203. require(2)
  204. newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-3])
  205. case CODECOPY:
  206. require(3)
  207. newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-3])
  208. case EXTCODECOPY:
  209. require(4)
  210. newMemSize = calcMemSize(stack.data[stack.Len()-2], stack.data[stack.Len()-4])
  211. case CALL, CALLCODE:
  212. require(7)
  213. reqGas.Set(GasCall)
  214. addStepGasUsage(stack.data[stack.Len()-1])
  215. x := calcMemSize(stack.data[stack.Len()-6], stack.data[stack.Len()-7])
  216. y := calcMemSize(stack.data[stack.Len()-4], stack.data[stack.Len()-5])
  217. newMemSize = ethutil.BigMax(x, y)
  218. case CREATE:
  219. require(3)
  220. reqGas.Set(GasCreate)
  221. newMemSize = calcMemSize(stack.data[stack.Len()-2], stack.data[stack.Len()-3])
  222. }
  223. if newMemSize.Cmp(ethutil.Big0) > 0 {
  224. newMemSize.Add(newMemSize, u256(31))
  225. newMemSize.Div(newMemSize, u256(32))
  226. newMemSize.Mul(newMemSize, u256(32))
  227. switch op {
  228. // Additional gas usage on *CODPY
  229. case CALLDATACOPY, CODECOPY, EXTCODECOPY:
  230. addStepGasUsage(new(big.Int).Div(newMemSize, u256(32)))
  231. }
  232. if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 {
  233. memGasUsage := new(big.Int).Sub(newMemSize, u256(int64(mem.Len())))
  234. memGasUsage.Mul(GasMemory, memGasUsage)
  235. memGasUsage.Div(memGasUsage, u256(32))
  236. addStepGasUsage(memGasUsage)
  237. }
  238. }
  239. self.Printf("(pc) %-3d -o- %-14s", pc, op.String())
  240. self.Printf(" (m) %-4d (s) %-4d (g) %-3v (%v)", mem.Len(), stack.Len(), reqGas, gas)
  241. if !useGas(regGas) {
  242. self.Endl()
  243. return nil, new(big.Int), OOG(reqGas, gas)
  244. }
  245. mem.Resize(newMemSize.Uint64())
  246. switch op {
  247. // 0x20 range
  248. case ADD:
  249. x, y := stack.Popn()
  250. self.Printf(" %v + %v", y, x)
  251. base.Add(y, x)
  252. U256(base)
  253. self.Printf(" = %v", base)
  254. // Pop result back on the stack
  255. stack.Push(base)
  256. case SUB:
  257. x, y := stack.Popn()
  258. self.Printf(" %v - %v", y, x)
  259. base.Sub(y, x)
  260. U256(base)
  261. self.Printf(" = %v", base)
  262. // Pop result back on the stack
  263. stack.Push(base)
  264. case MUL:
  265. x, y := stack.Popn()
  266. self.Printf(" %v * %v", y, x)
  267. base.Mul(y, x)
  268. U256(base)
  269. self.Printf(" = %v", base)
  270. // Pop result back on the stack
  271. stack.Push(base)
  272. case DIV:
  273. x, y := stack.Pop(), stack.Pop()
  274. self.Printf(" %v / %v", x, y)
  275. if y.Cmp(ethutil.Big0) != 0 {
  276. base.Div(x, y)
  277. }
  278. U256(base)
  279. self.Printf(" = %v", base)
  280. // Pop result back on the stack
  281. stack.Push(base)
  282. case SDIV:
  283. x, y := S256(stack.Pop()), S256(stack.Pop())
  284. self.Printf(" %v / %v", x, y)
  285. if y.Cmp(ethutil.Big0) == 0 {
  286. base.Set(ethutil.Big0)
  287. } else {
  288. n := new(big.Int)
  289. if new(big.Int).Mul(x, y).Cmp(ethutil.Big0) < 0 {
  290. n.SetInt64(-1)
  291. } else {
  292. n.SetInt64(1)
  293. }
  294. base.Div(x.Abs(x), y.Abs(y)).Mul(base, n)
  295. U256(base)
  296. }
  297. self.Printf(" = %v", base)
  298. stack.Push(base)
  299. case MOD:
  300. x, y := stack.Pop(), stack.Pop()
  301. self.Printf(" %v %% %v", x, y)
  302. if y.Cmp(ethutil.Big0) == 0 {
  303. base.Set(ethutil.Big0)
  304. } else {
  305. base.Mod(x, y)
  306. }
  307. U256(base)
  308. self.Printf(" = %v", base)
  309. stack.Push(base)
  310. case SMOD:
  311. x, y := S256(stack.Pop()), S256(stack.Pop())
  312. self.Printf(" %v %% %v", x, y)
  313. if y.Cmp(ethutil.Big0) == 0 {
  314. base.Set(ethutil.Big0)
  315. } else {
  316. n := new(big.Int)
  317. if x.Cmp(ethutil.Big0) < 0 {
  318. n.SetInt64(-1)
  319. } else {
  320. n.SetInt64(1)
  321. }
  322. base.Mod(x.Abs(x), y.Abs(y)).Mul(base, n)
  323. U256(base)
  324. }
  325. self.Printf(" = %v", base)
  326. stack.Push(base)
  327. case EXP:
  328. x, y := stack.Popn()
  329. self.Printf(" %v ** %v", y, x)
  330. base.Exp(y, x, Pow256)
  331. U256(base)
  332. self.Printf(" = %v", base)
  333. stack.Push(base)
  334. case SIGNEXTEND:
  335. back := stack.Pop().Uint64()
  336. if back < 31 {
  337. bit := uint(back*8 + 7)
  338. num := stack.Pop()
  339. mask := new(big.Int).Lsh(ethutil.Big1, bit)
  340. mask.Sub(mask, ethutil.Big1)
  341. if ethutil.BitTest(num, int(bit)) {
  342. num.Or(num, mask.Not(mask))
  343. } else {
  344. num.And(num, mask)
  345. }
  346. num = U256(num)
  347. self.Printf(" = %v", num)
  348. stack.Push(num)
  349. }
  350. case NOT:
  351. base.Sub(Pow256, stack.Pop()).Sub(base, ethutil.Big1)
  352. // Not needed
  353. //base = U256(base)
  354. stack.Push(base)
  355. case LT:
  356. x, y := stack.Popn()
  357. self.Printf(" %v < %v", y, x)
  358. // x < y
  359. if y.Cmp(x) < 0 {
  360. stack.Push(ethutil.BigTrue)
  361. } else {
  362. stack.Push(ethutil.BigFalse)
  363. }
  364. case GT:
  365. x, y := stack.Popn()
  366. self.Printf(" %v > %v", y, x)
  367. // x > y
  368. if y.Cmp(x) > 0 {
  369. stack.Push(ethutil.BigTrue)
  370. } else {
  371. stack.Push(ethutil.BigFalse)
  372. }
  373. case SLT:
  374. y, x := S256(stack.Pop()), S256(stack.Pop())
  375. self.Printf(" %v < %v", y, x)
  376. // x < y
  377. if y.Cmp(S256(x)) < 0 {
  378. stack.Push(ethutil.BigTrue)
  379. } else {
  380. stack.Push(ethutil.BigFalse)
  381. }
  382. case SGT:
  383. y, x := S256(stack.Pop()), S256(stack.Pop())
  384. self.Printf(" %v > %v", y, x)
  385. // x > y
  386. if y.Cmp(x) > 0 {
  387. stack.Push(ethutil.BigTrue)
  388. } else {
  389. stack.Push(ethutil.BigFalse)
  390. }
  391. case EQ:
  392. x, y := stack.Popn()
  393. self.Printf(" %v == %v", y, x)
  394. // x == y
  395. if x.Cmp(y) == 0 {
  396. stack.Push(ethutil.BigTrue)
  397. } else {
  398. stack.Push(ethutil.BigFalse)
  399. }
  400. case ISZERO:
  401. x := stack.Pop()
  402. if x.Cmp(ethutil.BigFalse) > 0 {
  403. stack.Push(ethutil.BigFalse)
  404. } else {
  405. stack.Push(ethutil.BigTrue)
  406. }
  407. // 0x10 range
  408. case AND:
  409. x, y := stack.Popn()
  410. self.Printf(" %v & %v", y, x)
  411. stack.Push(base.And(y, x))
  412. case OR:
  413. x, y := stack.Popn()
  414. self.Printf(" %v | %v", y, x)
  415. stack.Push(base.Or(y, x))
  416. case XOR:
  417. x, y := stack.Popn()
  418. self.Printf(" %v ^ %v", y, x)
  419. stack.Push(base.Xor(y, x))
  420. case BYTE:
  421. val, th := stack.Popn()
  422. if th.Cmp(big.NewInt(32)) < 0 {
  423. byt := big.NewInt(int64(ethutil.LeftPadBytes(val.Bytes(), 32)[th.Int64()]))
  424. base.Set(byt)
  425. } else {
  426. base.Set(ethutil.BigFalse)
  427. }
  428. self.Printf(" => 0x%x", base.Bytes())
  429. stack.Push(base)
  430. case ADDMOD:
  431. x := stack.Pop()
  432. y := stack.Pop()
  433. z := stack.Pop()
  434. base.Add(x, y)
  435. base.Mod(base, z)
  436. U256(base)
  437. self.Printf(" = %v", base)
  438. stack.Push(base)
  439. case MULMOD:
  440. x := stack.Pop()
  441. y := stack.Pop()
  442. z := stack.Pop()
  443. base.Mul(x, y)
  444. base.Mod(base, z)
  445. U256(base)
  446. self.Printf(" = %v", base)
  447. stack.Push(base)
  448. // 0x20 range
  449. case SHA3:
  450. size, offset := stack.Popn()
  451. data := crypto.Sha3(mem.Get(offset.Int64(), size.Int64()))
  452. stack.Push(ethutil.BigD(data))
  453. self.Printf(" => %x", data)
  454. // 0x30 range
  455. case ADDRESS:
  456. //stack.Push(ethutil.BigD(closure.Address()))
  457. stack.Push(ethutil.BigD(call.Address))
  458. self.Printf(" => %x", call.Address)
  459. case BALANCE:
  460. addr := stack.Pop().Bytes()
  461. //balance := statedb.GetBalance(addr)
  462. balance := self.env.GetBalance(addr)
  463. stack.Push(balance)
  464. self.Printf(" => %v (%x)", balance, addr)
  465. case ORIGIN:
  466. origin := self.env.Origin()
  467. stack.Push(ethutil.BigD(origin))
  468. self.Printf(" => %x", origin)
  469. case CALLER:
  470. //caller := closure.caller.Address()
  471. //stack.Push(ethutil.BigD(caller))
  472. stack.Push(call.Caller)
  473. self.Printf(" => %x", call.Caller)
  474. case CALLVALUE:
  475. //value := closure.exe.value
  476. stack.Push(call.Value)
  477. self.Printf(" => %v", call.Value)
  478. case CALLDATALOAD:
  479. var (
  480. offset = stack.Pop()
  481. data = make([]byte, 32)
  482. lenData = big.NewInt(int64(len(call.Data)))
  483. )
  484. if lenData.Cmp(offset) >= 0 {
  485. length := new(big.Int).Add(offset, ethutil.Big32)
  486. length = ethutil.BigMin(length, lenData)
  487. copy(data, call.Data[offset.Int64():length.Int64()])
  488. }
  489. self.Printf(" => 0x%x", data)
  490. stack.Push(ethutil.BigD(data))
  491. case CALLDATASIZE:
  492. l := int64(len(call.Data))
  493. stack.Push(big.NewInt(l))
  494. self.Printf(" => %d", l)
  495. case CALLDATACOPY:
  496. var (
  497. size = int64(len(call.Data))
  498. mOff = stack.Pop().Int64()
  499. cOff = stack.Pop().Int64()
  500. l = stack.Pop().Int64()
  501. )
  502. if cOff > size {
  503. cOff = 0
  504. l = 0
  505. } else if cOff+l > size {
  506. l = 0
  507. }
  508. code := call.Data[cOff : cOff+l]
  509. mem.Set(mOff, l, code)
  510. self.Printf(" => [%v, %v, %v] %x", mOff, cOff, l, code[cOff:cOff+l])
  511. case CODESIZE, EXTCODESIZE:
  512. var code []byte
  513. if op == EXTCODESIZE {
  514. addr := stack.Pop().Bytes()
  515. self.env.GetCode(addr)
  516. } else {
  517. code = call.Code
  518. }
  519. l := big.NewInt(int64(len(code)))
  520. stack.Push(l)
  521. self.Printf(" => %d", l)
  522. case CODECOPY, EXTCODECOPY:
  523. var code []byte
  524. if op == EXTCODECOPY {
  525. addr := stack.Pop().Bytes()
  526. code = self.env.GetCode(addr)
  527. } else {
  528. code = call.Code
  529. }
  530. var (
  531. size = int64(len(code))
  532. mOff = stack.Pop().Int64()
  533. cOff = stack.Pop().Int64()
  534. l = stack.Pop().Int64()
  535. )
  536. if cOff > size {
  537. cOff = 0
  538. l = 0
  539. } else if cOff+l > size {
  540. l = 0
  541. }
  542. codeCopy := code[cOff : cOff+l]
  543. mem.Set(mOff, l, codeCopy)
  544. self.Printf(" => [%v, %v, %v] %x", mOff, cOff, l, code[cOff:cOff+l])
  545. case GASPRICE:
  546. stack.Push(call.Price)
  547. self.Printf(" => %v", call.Price)
  548. // 0x40 range
  549. case PREVHASH:
  550. prevHash := self.env.PrevHash()
  551. stack.Push(ethutil.BigD(prevHash))
  552. self.Printf(" => 0x%x", prevHash)
  553. case COINBASE:
  554. coinbase := self.env.Coinbase()
  555. stack.Push(ethutil.BigD(coinbase))
  556. self.Printf(" => 0x%x", coinbase)
  557. case TIMESTAMP:
  558. time := self.env.Time()
  559. stack.Push(big.NewInt(time))
  560. self.Printf(" => 0x%x", time)
  561. case NUMBER:
  562. number := self.env.BlockNumber()
  563. stack.Push(number)
  564. self.Printf(" => 0x%x", number.Bytes())
  565. case DIFFICULTY:
  566. difficulty := self.env.Difficulty()
  567. stack.Push(difficulty)
  568. self.Printf(" => 0x%x", difficulty.Bytes())
  569. case GASLIMIT:
  570. stack.Push(self.env.GasLimit())
  571. // 0x50 range
  572. case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32:
  573. a := uint64(op) - uint64(PUSH1) + 1
  574. pc.Add(pc, ethutil.Big1)
  575. data := call.Get(pc.Uint64(), a) //closure.Gets(pc, a)
  576. val := ethutil.BigD(data)
  577. // Push value to stack
  578. stack.Push(val)
  579. pc.Add(pc, big.NewInt(int64(a)-1))
  580. step += uint64(op) - uint64(PUSH1) + 1
  581. self.Printf(" => 0x%x", data)
  582. case POP:
  583. stack.Pop()
  584. case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16:
  585. n := int(op - DUP1 + 1)
  586. v := stack.Dupn(n)
  587. self.Printf(" => [%d] 0x%x", n, stack.Peek().Bytes())
  588. case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16:
  589. n := int(op - SWAP1 + 2)
  590. x, y := stack.Swapn(n)
  591. self.Printf(" => [%d] %x [0] %x", n, x.Bytes(), y.Bytes())
  592. case LOG0, LOG1, LOG2, LOG3, LOG4:
  593. n := int(op - LOG0)
  594. topics := make([][]byte, n)
  595. mStart, mSize := stack.Pop().Int64(), stack.Pop().Int64()
  596. data := mem.Geti(mStart, mSize)
  597. for i := 0; i < n; i++ {
  598. topics[i] = stack.Pop().Bytes()
  599. }
  600. //log := &state.Log{closure.Address(), topics, data}
  601. self.env.AddLog(call.Address, topics, data)
  602. self.Printf(" => %v", log)
  603. case MLOAD:
  604. offset := stack.Pop()
  605. val := ethutil.BigD(mem.Get(offset.Int64(), 32))
  606. stack.Push(val)
  607. self.Printf(" => 0x%x", val.Bytes())
  608. case MSTORE: // Store the value at stack top-1 in to memory at location stack top
  609. // Pop value of the stack
  610. val, mStart := stack.Popn()
  611. mem.Set(mStart.Int64(), 32, ethutil.BigToBytes(val, 256))
  612. self.Printf(" => 0x%x", val)
  613. case MSTORE8:
  614. off := stack.Pop()
  615. val := stack.Pop()
  616. mem.store[off.Int64()] = byte(val.Int64() & 0xff)
  617. self.Printf(" => [%v] 0x%x", off, val)
  618. case SLOAD:
  619. loc := stack.Pop()
  620. val := ethutil.BigD(self.env.GetState(call.Address, loc.Bytes()))
  621. stack.Push(val)
  622. self.Printf(" {0x%x : 0x%x}", loc.Bytes(), val.Bytes())
  623. case SSTORE:
  624. val, loc := stack.Popn()
  625. self.env.SetState(call.Address, loc.Bytes(), val.Bytes())
  626. //statedb.SetState(closure.Address(), loc.Bytes(), val)
  627. //closure.message.AddStorageChange(loc.Bytes())
  628. self.Printf(" {0x%x : 0x%x}", loc.Bytes(), val.Bytes())
  629. case JUMP:
  630. jump(pc, stack.Pop())
  631. continue
  632. case JUMPI:
  633. cond, pos := stack.Popn()
  634. if cond.Cmp(ethutil.BigTrue) >= 0 {
  635. jump(pc, pos)
  636. continue
  637. }
  638. case JUMPDEST:
  639. case PC:
  640. stack.Push(pc)
  641. case MSIZE:
  642. stack.Push(big.NewInt(int64(mem.Len())))
  643. case GAS:
  644. stack.Push(call.Gas)
  645. // 0x60 range
  646. case CREATE:
  647. var (
  648. err error
  649. value = stack.Pop()
  650. size, offset = stack.Popn()
  651. input = mem.Get(offset.Int64(), size.Int64())
  652. gas = new(big.Int).Set(call.Gas)
  653. // Snapshot the current stack so we are able to
  654. // revert back to it later.
  655. //snapshot = self.env.State().Copy()
  656. )
  657. // Generate a new address
  658. //n := statedb.GetNonce(closure.Address())
  659. //addr := crypto.CreateAddress(closure.Address(), n)
  660. //statedb.SetNonce(closure.Address(), n+1)
  661. n := self.env.GetNonce(call.Address)
  662. addr := crypto.CreateAddress(call.Address, n)
  663. self.env.SetNonce(call.Address, n+1)
  664. self.Printf(" (*) %x", addr).Endl()
  665. //closure.UseGas(closure.Gas)
  666. msg := NewExecution(self, addr, input, gas, call.Price, value)
  667. ret, lgas, err := msg.Create(call.Address)
  668. if err != nil {
  669. stack.Push(ethutil.BigFalse)
  670. // Revert the state as it was before.
  671. //self.env.State().Set(snapshot)
  672. self.Printf("CREATE err %v", err)
  673. } else {
  674. msg.object.Code = ret
  675. stack.Push(ethutil.BigD(addr))
  676. }
  677. gas = lgas
  678. self.Endl()
  679. case CALL, CALLCODE:
  680. self.Endl()
  681. gas := stack.Pop()
  682. // Pop gas and value of the stack.
  683. value, addr := stack.Popn()
  684. // Pop input size and offset
  685. inSize, inOffset := stack.Popn()
  686. // Pop return size and offset
  687. retSize, retOffset := stack.Popn()
  688. // Get the arguments from the memory
  689. args := mem.Get(inOffset.Int64(), inSize.Int64())
  690. var executeAddr []byte
  691. if op == CALLCODE {
  692. executeAddr = call.Address //closure.Address()
  693. } else {
  694. executeAddr = addr.Bytes()
  695. }
  696. msg := NewExecution(self, executeAddr, args, gas, call.Price, value)
  697. ret, err := msg.Exec(addr.Bytes(), closure)
  698. if err != nil {
  699. stack.Push(ethutil.BigFalse)
  700. vmlogger.Debugln(err)
  701. } else {
  702. stack.Push(ethutil.BigTrue)
  703. mem.Set(retOffset.Int64(), retSize.Int64(), ret)
  704. }
  705. self.Printf("resume %x", closure.Address())
  706. case RETURN:
  707. size, offset := stack.Popn()
  708. ret := mem.Get(offset.Int64(), size.Int64())
  709. self.Printf(" => (%d) 0x%x", len(ret), ret).Endl()
  710. return ret, gas, nil
  711. //return closure.Return(ret), gas, nil
  712. case SUICIDE:
  713. //receiver := statedb.GetOrNewStateObject(stack.Pop().Bytes())
  714. //receiver.AddAmount(statedb.GetBalance(closure.Address()))
  715. //statedb.Delete(closure.Address())
  716. self.env.AddBalance(stack.Pop().Bytes(), self.env.Balance(call.Address))
  717. self.env.DeleteAccount(call.Address)
  718. fallthrough
  719. case STOP: // Stop the closure
  720. self.Endl()
  721. return closure.Return(nil), nil
  722. default:
  723. vmlogger.Debugf("(pc) %-3v Invalid opcode %x\n", pc, op)
  724. //panic(fmt.Sprintf("Invalid opcode %x", op))
  725. closure.ReturnGas(big.NewInt(1), nil)
  726. return closure.Return(nil), fmt.Errorf("Invalid opcode %x", op)
  727. }
  728. pc.Add(pc, ethutil.Big1)
  729. self.Endl()
  730. }
  731. }
  732. func (self *DebugVm) Printf(format string, v ...interface{}) VirtualMachine {
  733. if self.logTy == LogTyPretty {
  734. self.logStr += fmt.Sprintf(format, v...)
  735. }
  736. return self
  737. }
  738. func (self *DebugVm) Endl() VirtualMachine {
  739. if self.logTy == LogTyPretty {
  740. vmlogger.Debugln(self.logStr)
  741. self.logStr = ""
  742. }
  743. return self
  744. }
  745. func (self *DebugVm) Env() Environment {
  746. return self.env
  747. }
  748. func (self *DebugVm) Depth() int {
  749. return self.depth
  750. }