vm_debug.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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: false}
  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. gas.Set(GasLog)
  144. addStepGasUsage(new(big.Int).Mul(big.NewInt(int64(n)), GasLog))
  145. mSize, mStart := stack.Peekn()
  146. addStepGasUsage(mSize)
  147. newMemSize = calcMemSize(mStart, mSize)
  148. case EXP:
  149. require(2)
  150. exp := new(big.Int).Set(stack.data[stack.Len()-2])
  151. nbytes := 0
  152. for exp.Cmp(ethutil.Big0) > 0 {
  153. nbytes += 1
  154. exp.Rsh(exp, 8)
  155. }
  156. gas.Set(big.NewInt(int64(nbytes + 1)))
  157. // Gas only
  158. case STOP:
  159. reqGas.Set(ethutil.Big0)
  160. case SUICIDE:
  161. require(1)
  162. reqGas.Set(ethutil.Big0)
  163. case SLOAD:
  164. require(1)
  165. reqGas.Set(GasSLoad)
  166. // Memory resize & Gas
  167. case SSTORE:
  168. require(2)
  169. var mult *big.Int
  170. y, x := stack.Peekn()
  171. val := ethutil.BigD(self.env.GetState(x.Bytes())) //closure.GetStorage(x)
  172. if val.BigInt().Cmp(ethutil.Big0) == 0 && len(y.Bytes()) > 0 {
  173. // 0 => non 0
  174. mult = ethutil.Big3
  175. } else if val.BigInt().Cmp(ethutil.Big0) != 0 && len(y.Bytes()) == 0 {
  176. //statedb.Refund(closure.caller.Address(), GasSStoreRefund, closure.Price)
  177. self.env.Refund(call.Caller, GasSStoreRefund, call.Price)
  178. mult = ethutil.Big0
  179. } else {
  180. // non 0 => non 0
  181. mult = ethutil.Big1
  182. }
  183. reqGas.Set(new(big.Int).Mul(mult, GasSStore))
  184. case BALANCE:
  185. require(1)
  186. reqGas.Set(GasBalance)
  187. case MSTORE:
  188. require(2)
  189. newMemSize = calcMemSize(stack.Peek(), u256(32))
  190. case MLOAD:
  191. require(1)
  192. newMemSize = calcMemSize(stack.Peek(), u256(32))
  193. case MSTORE8:
  194. require(2)
  195. newMemSize = calcMemSize(stack.Peek(), u256(1))
  196. case RETURN:
  197. require(2)
  198. newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-2])
  199. case SHA3:
  200. require(2)
  201. reqGas.Set(GasSha)
  202. newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-2])
  203. case CALLDATACOPY:
  204. require(2)
  205. newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-3])
  206. case CODECOPY:
  207. require(3)
  208. newMemSize = calcMemSize(stack.Peek(), stack.data[stack.Len()-3])
  209. case EXTCODECOPY:
  210. require(4)
  211. newMemSize = calcMemSize(stack.data[stack.Len()-2], stack.data[stack.Len()-4])
  212. case CALL, CALLCODE:
  213. require(7)
  214. reqGas.Set(GasCall)
  215. addStepGasUsage(stack.data[stack.Len()-1])
  216. x := calcMemSize(stack.data[stack.Len()-6], stack.data[stack.Len()-7])
  217. y := calcMemSize(stack.data[stack.Len()-4], stack.data[stack.Len()-5])
  218. newMemSize = ethutil.BigMax(x, y)
  219. case CREATE:
  220. require(3)
  221. reqGas.Set(GasCreate)
  222. newMemSize = calcMemSize(stack.data[stack.Len()-2], stack.data[stack.Len()-3])
  223. }
  224. if newMemSize.Cmp(ethutil.Big0) > 0 {
  225. newMemSize.Add(newMemSize, u256(31))
  226. newMemSize.Div(newMemSize, u256(32))
  227. newMemSize.Mul(newMemSize, u256(32))
  228. switch op {
  229. // Additional gas usage on *CODPY
  230. case CALLDATACOPY, CODECOPY, EXTCODECOPY:
  231. addStepGasUsage(new(big.Int).Div(newMemSize, u256(32)))
  232. }
  233. if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 {
  234. memGasUsage := new(big.Int).Sub(newMemSize, u256(int64(mem.Len())))
  235. memGasUsage.Mul(GasMemory, memGasUsage)
  236. memGasUsage.Div(memGasUsage, u256(32))
  237. addStepGasUsage(memGasUsage)
  238. }
  239. }
  240. self.Printf("(pc) %-3d -o- %-14s", pc, op.String())
  241. self.Printf(" (m) %-4d (s) %-4d (g) %-3v (%v)", mem.Len(), stack.Len(), reqGas, gas)
  242. if !useGas(regGas) {
  243. self.Endl()
  244. return nil, new(big.Int), OOG(reqGas, gas)
  245. }
  246. mem.Resize(newMemSize.Uint64())
  247. switch op {
  248. // 0x20 range
  249. case ADD:
  250. x, y := stack.Popn()
  251. self.Printf(" %v + %v", y, x)
  252. base.Add(y, x)
  253. U256(base)
  254. self.Printf(" = %v", base)
  255. // Pop result back on the stack
  256. stack.Push(base)
  257. case SUB:
  258. x, y := stack.Popn()
  259. self.Printf(" %v - %v", y, x)
  260. base.Sub(y, x)
  261. U256(base)
  262. self.Printf(" = %v", base)
  263. // Pop result back on the stack
  264. stack.Push(base)
  265. case MUL:
  266. x, y := stack.Popn()
  267. self.Printf(" %v * %v", y, x)
  268. base.Mul(y, x)
  269. U256(base)
  270. self.Printf(" = %v", base)
  271. // Pop result back on the stack
  272. stack.Push(base)
  273. case DIV:
  274. x, y := stack.Pop(), stack.Pop()
  275. self.Printf(" %v / %v", x, y)
  276. if y.Cmp(ethutil.Big0) != 0 {
  277. base.Div(x, y)
  278. }
  279. U256(base)
  280. self.Printf(" = %v", base)
  281. // Pop result back on the stack
  282. stack.Push(base)
  283. case SDIV:
  284. x, y := S256(stack.Pop()), S256(stack.Pop())
  285. self.Printf(" %v / %v", x, y)
  286. if y.Cmp(ethutil.Big0) == 0 {
  287. base.Set(ethutil.Big0)
  288. } else {
  289. n := new(big.Int)
  290. if new(big.Int).Mul(x, y).Cmp(ethutil.Big0) < 0 {
  291. n.SetInt64(-1)
  292. } else {
  293. n.SetInt64(1)
  294. }
  295. base.Div(x.Abs(x), y.Abs(y)).Mul(base, n)
  296. U256(base)
  297. }
  298. self.Printf(" = %v", base)
  299. stack.Push(base)
  300. case MOD:
  301. x, y := stack.Pop(), stack.Pop()
  302. self.Printf(" %v %% %v", x, y)
  303. if y.Cmp(ethutil.Big0) == 0 {
  304. base.Set(ethutil.Big0)
  305. } else {
  306. base.Mod(x, y)
  307. }
  308. U256(base)
  309. self.Printf(" = %v", base)
  310. stack.Push(base)
  311. case SMOD:
  312. x, y := S256(stack.Pop()), S256(stack.Pop())
  313. self.Printf(" %v %% %v", x, y)
  314. if y.Cmp(ethutil.Big0) == 0 {
  315. base.Set(ethutil.Big0)
  316. } else {
  317. n := new(big.Int)
  318. if x.Cmp(ethutil.Big0) < 0 {
  319. n.SetInt64(-1)
  320. } else {
  321. n.SetInt64(1)
  322. }
  323. base.Mod(x.Abs(x), y.Abs(y)).Mul(base, n)
  324. U256(base)
  325. }
  326. self.Printf(" = %v", base)
  327. stack.Push(base)
  328. case EXP:
  329. x, y := stack.Popn()
  330. self.Printf(" %v ** %v", y, x)
  331. base.Exp(y, x, Pow256)
  332. U256(base)
  333. self.Printf(" = %v", base)
  334. stack.Push(base)
  335. case SIGNEXTEND:
  336. back := stack.Pop().Uint64()
  337. if back < 31 {
  338. bit := uint(back*8 + 7)
  339. num := stack.Pop()
  340. mask := new(big.Int).Lsh(ethutil.Big1, bit)
  341. mask.Sub(mask, ethutil.Big1)
  342. if ethutil.BitTest(num, int(bit)) {
  343. num.Or(num, mask.Not(mask))
  344. } else {
  345. num.And(num, mask)
  346. }
  347. num = U256(num)
  348. self.Printf(" = %v", num)
  349. stack.Push(num)
  350. }
  351. case NOT:
  352. base.Sub(Pow256, stack.Pop()).Sub(base, ethutil.Big1)
  353. // Not needed
  354. //base = U256(base)
  355. stack.Push(base)
  356. case LT:
  357. x, y := stack.Popn()
  358. self.Printf(" %v < %v", y, x)
  359. // x < y
  360. if y.Cmp(x) < 0 {
  361. stack.Push(ethutil.BigTrue)
  362. } else {
  363. stack.Push(ethutil.BigFalse)
  364. }
  365. case GT:
  366. x, y := stack.Popn()
  367. self.Printf(" %v > %v", y, x)
  368. // x > y
  369. if y.Cmp(x) > 0 {
  370. stack.Push(ethutil.BigTrue)
  371. } else {
  372. stack.Push(ethutil.BigFalse)
  373. }
  374. case SLT:
  375. y, x := S256(stack.Pop()), S256(stack.Pop())
  376. self.Printf(" %v < %v", y, x)
  377. // x < y
  378. if y.Cmp(S256(x)) < 0 {
  379. stack.Push(ethutil.BigTrue)
  380. } else {
  381. stack.Push(ethutil.BigFalse)
  382. }
  383. case SGT:
  384. y, x := S256(stack.Pop()), S256(stack.Pop())
  385. self.Printf(" %v > %v", y, x)
  386. // x > y
  387. if y.Cmp(x) > 0 {
  388. stack.Push(ethutil.BigTrue)
  389. } else {
  390. stack.Push(ethutil.BigFalse)
  391. }
  392. case EQ:
  393. x, y := stack.Popn()
  394. self.Printf(" %v == %v", y, x)
  395. // x == y
  396. if x.Cmp(y) == 0 {
  397. stack.Push(ethutil.BigTrue)
  398. } else {
  399. stack.Push(ethutil.BigFalse)
  400. }
  401. case ISZERO:
  402. x := stack.Pop()
  403. if x.Cmp(ethutil.BigFalse) > 0 {
  404. stack.Push(ethutil.BigFalse)
  405. } else {
  406. stack.Push(ethutil.BigTrue)
  407. }
  408. // 0x10 range
  409. case AND:
  410. x, y := stack.Popn()
  411. self.Printf(" %v & %v", y, x)
  412. stack.Push(base.And(y, x))
  413. case OR:
  414. x, y := stack.Popn()
  415. self.Printf(" %v | %v", y, x)
  416. stack.Push(base.Or(y, x))
  417. case XOR:
  418. x, y := stack.Popn()
  419. self.Printf(" %v ^ %v", y, x)
  420. stack.Push(base.Xor(y, x))
  421. case BYTE:
  422. val, th := stack.Popn()
  423. if th.Cmp(big.NewInt(32)) < 0 {
  424. byt := big.NewInt(int64(ethutil.LeftPadBytes(val.Bytes(), 32)[th.Int64()]))
  425. base.Set(byt)
  426. } else {
  427. base.Set(ethutil.BigFalse)
  428. }
  429. self.Printf(" => 0x%x", base.Bytes())
  430. stack.Push(base)
  431. case ADDMOD:
  432. x := stack.Pop()
  433. y := stack.Pop()
  434. z := stack.Pop()
  435. base.Add(x, y)
  436. base.Mod(base, z)
  437. U256(base)
  438. self.Printf(" = %v", base)
  439. stack.Push(base)
  440. case MULMOD:
  441. x := stack.Pop()
  442. y := stack.Pop()
  443. z := stack.Pop()
  444. base.Mul(x, y)
  445. base.Mod(base, z)
  446. U256(base)
  447. self.Printf(" = %v", base)
  448. stack.Push(base)
  449. // 0x20 range
  450. case SHA3:
  451. size, offset := stack.Popn()
  452. data := crypto.Sha3(mem.Get(offset.Int64(), size.Int64()))
  453. stack.Push(ethutil.BigD(data))
  454. self.Printf(" => %x", data)
  455. // 0x30 range
  456. case ADDRESS:
  457. //stack.Push(ethutil.BigD(closure.Address()))
  458. stack.Push(ethutil.BigD(call.Address))
  459. self.Printf(" => %x", call.Address)
  460. case BALANCE:
  461. addr := stack.Pop().Bytes()
  462. //balance := statedb.GetBalance(addr)
  463. balance := self.env.GetBalance(addr)
  464. stack.Push(balance)
  465. self.Printf(" => %v (%x)", balance, addr)
  466. case ORIGIN:
  467. origin := self.env.Origin()
  468. stack.Push(ethutil.BigD(origin))
  469. self.Printf(" => %x", origin)
  470. case CALLER:
  471. //caller := closure.caller.Address()
  472. //stack.Push(ethutil.BigD(caller))
  473. stack.Push(call.Caller)
  474. self.Printf(" => %x", call.Caller)
  475. case CALLVALUE:
  476. //value := closure.exe.value
  477. stack.Push(call.Value)
  478. self.Printf(" => %v", call.Value)
  479. case CALLDATALOAD:
  480. var (
  481. offset = stack.Pop()
  482. data = make([]byte, 32)
  483. lenData = big.NewInt(int64(len(call.Data)))
  484. )
  485. if lenData.Cmp(offset) >= 0 {
  486. length := new(big.Int).Add(offset, ethutil.Big32)
  487. length = ethutil.BigMin(length, lenData)
  488. copy(data, call.Data[offset.Int64():length.Int64()])
  489. }
  490. self.Printf(" => 0x%x", data)
  491. stack.Push(ethutil.BigD(data))
  492. case CALLDATASIZE:
  493. l := int64(len(call.Data))
  494. stack.Push(big.NewInt(l))
  495. self.Printf(" => %d", l)
  496. case CALLDATACOPY:
  497. var (
  498. size = int64(len(call.Data))
  499. mOff = stack.Pop().Int64()
  500. cOff = stack.Pop().Int64()
  501. l = stack.Pop().Int64()
  502. )
  503. if cOff > size {
  504. cOff = 0
  505. l = 0
  506. } else if cOff+l > size {
  507. l = 0
  508. }
  509. code := call.Data[cOff : cOff+l]
  510. mem.Set(mOff, l, code)
  511. self.Printf(" => [%v, %v, %v] %x", mOff, cOff, l, code[cOff:cOff+l])
  512. case CODESIZE, EXTCODESIZE:
  513. var code []byte
  514. if op == EXTCODESIZE {
  515. addr := stack.Pop().Bytes()
  516. self.env.GetCode(addr)
  517. } else {
  518. code = call.Code
  519. }
  520. l := big.NewInt(int64(len(code)))
  521. stack.Push(l)
  522. self.Printf(" => %d", l)
  523. case CODECOPY, EXTCODECOPY:
  524. var code []byte
  525. if op == EXTCODECOPY {
  526. addr := stack.Pop().Bytes()
  527. code = self.env.GetCode(addr)
  528. } else {
  529. code = call.Code
  530. }
  531. var (
  532. size = int64(len(code))
  533. mOff = stack.Pop().Int64()
  534. cOff = stack.Pop().Int64()
  535. l = stack.Pop().Int64()
  536. )
  537. if cOff > size {
  538. cOff = 0
  539. l = 0
  540. } else if cOff+l > size {
  541. l = 0
  542. }
  543. codeCopy := code[cOff : cOff+l]
  544. mem.Set(mOff, l, codeCopy)
  545. self.Printf(" => [%v, %v, %v] %x", mOff, cOff, l, code[cOff:cOff+l])
  546. case GASPRICE:
  547. stack.Push(call.Price)
  548. self.Printf(" => %v", call.Price)
  549. // 0x40 range
  550. case PREVHASH:
  551. prevHash := self.env.PrevHash()
  552. stack.Push(ethutil.BigD(prevHash))
  553. self.Printf(" => 0x%x", prevHash)
  554. case COINBASE:
  555. coinbase := self.env.Coinbase()
  556. stack.Push(ethutil.BigD(coinbase))
  557. self.Printf(" => 0x%x", coinbase)
  558. case TIMESTAMP:
  559. time := self.env.Time()
  560. stack.Push(big.NewInt(time))
  561. self.Printf(" => 0x%x", time)
  562. case NUMBER:
  563. number := self.env.BlockNumber()
  564. stack.Push(number)
  565. self.Printf(" => 0x%x", number.Bytes())
  566. case DIFFICULTY:
  567. difficulty := self.env.Difficulty()
  568. stack.Push(difficulty)
  569. self.Printf(" => 0x%x", difficulty.Bytes())
  570. case GASLIMIT:
  571. stack.Push(self.env.GasLimit())
  572. // 0x50 range
  573. 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:
  574. a := uint64(op) - uint64(PUSH1) + 1
  575. pc.Add(pc, ethutil.Big1)
  576. data := call.Get(pc.Uint64(), a) //closure.Gets(pc, a)
  577. val := ethutil.BigD(data)
  578. // Push value to stack
  579. stack.Push(val)
  580. pc.Add(pc, big.NewInt(int64(a)-1))
  581. step += uint64(op) - uint64(PUSH1) + 1
  582. self.Printf(" => 0x%x", data)
  583. case POP:
  584. stack.Pop()
  585. case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16:
  586. n := int(op - DUP1 + 1)
  587. v := stack.Dupn(n)
  588. self.Printf(" => [%d] 0x%x", n, stack.Peek().Bytes())
  589. case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16:
  590. n := int(op - SWAP1 + 2)
  591. x, y := stack.Swapn(n)
  592. self.Printf(" => [%d] %x [0] %x", n, x.Bytes(), y.Bytes())
  593. case LOG0, LOG1, LOG2, LOG3, LOG4:
  594. n := int(op - LOG0)
  595. topics := make([][]byte, n)
  596. mSize, mStart := stack.Pop().Int64(), stack.Pop().Int64()
  597. data := mem.Geti(mStart, mSize)
  598. for i := 0; i < n; i++ {
  599. topics[i] = ethutil.LeftPadBytes(stack.Pop().Bytes(), 32)
  600. }
  601. //log := &state.Log{closure.Address(), topics, data}
  602. self.env.AddLog(call.Address, topics, data)
  603. self.Printf(" => %v", log)
  604. case MLOAD:
  605. offset := stack.Pop()
  606. val := ethutil.BigD(mem.Get(offset.Int64(), 32))
  607. stack.Push(val)
  608. self.Printf(" => 0x%x", val.Bytes())
  609. case MSTORE: // Store the value at stack top-1 in to memory at location stack top
  610. // Pop value of the stack
  611. val, mStart := stack.Popn()
  612. mem.Set(mStart.Int64(), 32, ethutil.BigToBytes(val, 256))
  613. self.Printf(" => 0x%x", val)
  614. case MSTORE8:
  615. off := stack.Pop()
  616. val := stack.Pop()
  617. mem.store[off.Int64()] = byte(val.Int64() & 0xff)
  618. self.Printf(" => [%v] 0x%x", off, val)
  619. case SLOAD:
  620. loc := stack.Pop()
  621. val := ethutil.BigD(self.env.GetState(call.Address, loc.Bytes()))
  622. stack.Push(val)
  623. self.Printf(" {0x%x : 0x%x}", loc.Bytes(), val.Bytes())
  624. case SSTORE:
  625. val, loc := stack.Popn()
  626. self.env.SetState(call.Address, loc.Bytes(), val.Bytes())
  627. //statedb.SetState(closure.Address(), loc.Bytes(), val)
  628. //closure.message.AddStorageChange(loc.Bytes())
  629. self.Printf(" {0x%x : 0x%x}", loc.Bytes(), val.Bytes())
  630. case JUMP:
  631. jump(pc, stack.Pop())
  632. continue
  633. case JUMPI:
  634. cond, pos := stack.Popn()
  635. if cond.Cmp(ethutil.BigTrue) >= 0 {
  636. jump(pc, pos)
  637. continue
  638. }
  639. case JUMPDEST:
  640. case PC:
  641. stack.Push(pc)
  642. case MSIZE:
  643. stack.Push(big.NewInt(int64(mem.Len())))
  644. case GAS:
  645. stack.Push(call.Gas)
  646. // 0x60 range
  647. case CREATE:
  648. var (
  649. err error
  650. value = stack.Pop()
  651. size, offset = stack.Popn()
  652. input = mem.Get(offset.Int64(), size.Int64())
  653. gas = new(big.Int).Set(call.Gas)
  654. // Snapshot the current stack so we are able to
  655. // revert back to it later.
  656. //snapshot = self.env.State().Copy()
  657. )
  658. // Generate a new address
  659. //n := statedb.GetNonce(closure.Address())
  660. //addr := crypto.CreateAddress(closure.Address(), n)
  661. //statedb.SetNonce(closure.Address(), n+1)
  662. n := self.env.GetNonce(call.Address)
  663. addr := crypto.CreateAddress(call.Address, n)
  664. self.env.SetNonce(call.Address, n+1)
  665. self.Printf(" (*) %x", addr).Endl()
  666. //closure.UseGas(closure.Gas)
  667. msg := NewExecution(self, addr, input, gas, call.Price, value)
  668. ret, lgas, err := msg.Create(call.Address)
  669. if err != nil {
  670. stack.Push(ethutil.BigFalse)
  671. // Revert the state as it was before.
  672. //self.env.State().Set(snapshot)
  673. self.Printf("CREATE err %v", err)
  674. } else {
  675. msg.object.Code = ret
  676. stack.Push(ethutil.BigD(addr))
  677. }
  678. gas = lgas
  679. self.Endl()
  680. case CALL, CALLCODE:
  681. self.Endl()
  682. gas := stack.Pop()
  683. // Pop gas and value of the stack.
  684. value, addr := stack.Popn()
  685. // Pop input size and offset
  686. inSize, inOffset := stack.Popn()
  687. // Pop return size and offset
  688. retSize, retOffset := stack.Popn()
  689. // Get the arguments from the memory
  690. args := mem.Get(inOffset.Int64(), inSize.Int64())
  691. var executeAddr []byte
  692. if op == CALLCODE {
  693. executeAddr = call.Address //closure.Address()
  694. } else {
  695. executeAddr = addr.Bytes()
  696. }
  697. msg := NewExecution(self, executeAddr, args, gas, call.Price, value)
  698. ret, err := msg.Exec(addr.Bytes(), closure)
  699. if err != nil {
  700. stack.Push(ethutil.BigFalse)
  701. vmlogger.Debugln(err)
  702. } else {
  703. stack.Push(ethutil.BigTrue)
  704. mem.Set(retOffset.Int64(), retSize.Int64(), ret)
  705. }
  706. self.Printf("resume %x", closure.Address())
  707. case RETURN:
  708. size, offset := stack.Popn()
  709. ret := mem.Get(offset.Int64(), size.Int64())
  710. self.Printf(" => (%d) 0x%x", len(ret), ret).Endl()
  711. return ret, gas, nil
  712. //return closure.Return(ret), gas, nil
  713. case SUICIDE:
  714. //receiver := statedb.GetOrNewStateObject(stack.Pop().Bytes())
  715. //receiver.AddAmount(statedb.GetBalance(closure.Address()))
  716. //statedb.Delete(closure.Address())
  717. self.env.AddBalance(stack.Pop().Bytes(), self.env.Balance(call.Address))
  718. self.env.DeleteAccount(call.Address)
  719. fallthrough
  720. case STOP: // Stop the closure
  721. self.Endl()
  722. return closure.Return(nil), nil
  723. default:
  724. vmlogger.Debugf("(pc) %-3v Invalid opcode %x\n", pc, op)
  725. //panic(fmt.Sprintf("Invalid opcode %x", op))
  726. closure.ReturnGas(big.NewInt(1), nil)
  727. return closure.Return(nil), fmt.Errorf("Invalid opcode %x", op)
  728. }
  729. pc.Add(pc, ethutil.Big1)
  730. self.Endl()
  731. }
  732. }
  733. func (self *DebugVm) Printf(format string, v ...interface{}) VirtualMachine {
  734. if self.logTy == LogTyPretty {
  735. self.logStr += fmt.Sprintf(format, v...)
  736. }
  737. return self
  738. }
  739. func (self *DebugVm) Endl() VirtualMachine {
  740. if self.logTy == LogTyPretty {
  741. vmlogger.Debugln(self.logStr)
  742. self.logStr = ""
  743. }
  744. return self
  745. }
  746. func (self *DebugVm) Env() Environment {
  747. return self.env
  748. }
  749. func (self *DebugVm) Depth() int {
  750. return self.depth
  751. }