runner.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "bytes"
  19. "fmt"
  20. "io/ioutil"
  21. "os"
  22. "runtime/pprof"
  23. "time"
  24. goruntime "runtime"
  25. "github.com/ethereum/go-ethereum/cmd/evm/internal/compiler"
  26. "github.com/ethereum/go-ethereum/cmd/utils"
  27. "github.com/ethereum/go-ethereum/common"
  28. "github.com/ethereum/go-ethereum/core/state"
  29. "github.com/ethereum/go-ethereum/core/vm"
  30. "github.com/ethereum/go-ethereum/core/vm/runtime"
  31. "github.com/ethereum/go-ethereum/ethdb"
  32. "github.com/ethereum/go-ethereum/log"
  33. cli "gopkg.in/urfave/cli.v1"
  34. )
  35. var runCommand = cli.Command{
  36. Action: runCmd,
  37. Name: "run",
  38. Usage: "run arbitrary evm binary",
  39. ArgsUsage: "<code>",
  40. Description: `The run command runs arbitrary EVM code.`,
  41. }
  42. func runCmd(ctx *cli.Context) error {
  43. glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
  44. glogger.Verbosity(log.Lvl(ctx.GlobalInt(VerbosityFlag.Name)))
  45. log.Root().SetHandler(glogger)
  46. var (
  47. db, _ = ethdb.NewMemDatabase()
  48. statedb, _ = state.New(common.Hash{}, db)
  49. sender = common.StringToAddress("sender")
  50. logger = vm.NewStructLogger(nil)
  51. )
  52. statedb.CreateAccount(sender)
  53. var (
  54. code []byte
  55. ret []byte
  56. err error
  57. )
  58. if fn := ctx.Args().First(); len(fn) > 0 {
  59. src, err := ioutil.ReadFile(fn)
  60. if err != nil {
  61. return err
  62. }
  63. bin, err := compiler.Compile(fn, src, false)
  64. if err != nil {
  65. return err
  66. }
  67. code = common.Hex2Bytes(bin)
  68. } else if ctx.GlobalString(CodeFlag.Name) != "" {
  69. code = common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name))
  70. } else {
  71. var hexcode []byte
  72. if ctx.GlobalString(CodeFileFlag.Name) != "" {
  73. var err error
  74. hexcode, err = ioutil.ReadFile(ctx.GlobalString(CodeFileFlag.Name))
  75. if err != nil {
  76. fmt.Printf("Could not load code from file: %v\n", err)
  77. os.Exit(1)
  78. }
  79. } else {
  80. var err error
  81. hexcode, err = ioutil.ReadAll(os.Stdin)
  82. if err != nil {
  83. fmt.Printf("Could not load code from stdin: %v\n", err)
  84. os.Exit(1)
  85. }
  86. }
  87. code = common.Hex2Bytes(string(bytes.TrimRight(hexcode, "\n")))
  88. }
  89. runtimeConfig := runtime.Config{
  90. Origin: sender,
  91. State: statedb,
  92. GasLimit: ctx.GlobalUint64(GasFlag.Name),
  93. GasPrice: utils.GlobalBig(ctx, PriceFlag.Name),
  94. Value: utils.GlobalBig(ctx, ValueFlag.Name),
  95. EVMConfig: vm.Config{
  96. Tracer: logger,
  97. Debug: ctx.GlobalBool(DebugFlag.Name),
  98. DisableGasMetering: ctx.GlobalBool(DisableGasMeteringFlag.Name),
  99. },
  100. }
  101. if cpuProfilePath := ctx.GlobalString(CPUProfileFlag.Name); cpuProfilePath != "" {
  102. f, err := os.Create(cpuProfilePath)
  103. if err != nil {
  104. fmt.Println("could not create CPU profile: ", err)
  105. os.Exit(1)
  106. }
  107. if err := pprof.StartCPUProfile(f); err != nil {
  108. fmt.Println("could not start CPU profile: ", err)
  109. os.Exit(1)
  110. }
  111. defer pprof.StopCPUProfile()
  112. }
  113. tstart := time.Now()
  114. if ctx.GlobalBool(CreateFlag.Name) {
  115. input := append(code, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name))...)
  116. ret, _, err = runtime.Create(input, &runtimeConfig)
  117. } else {
  118. receiver := common.StringToAddress("receiver")
  119. statedb.SetCode(receiver, code)
  120. ret, err = runtime.Call(receiver, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name)), &runtimeConfig)
  121. }
  122. execTime := time.Since(tstart)
  123. if ctx.GlobalBool(DumpFlag.Name) {
  124. statedb.Commit(true)
  125. fmt.Println(string(statedb.Dump()))
  126. }
  127. if memProfilePath := ctx.GlobalString(MemProfileFlag.Name); memProfilePath != "" {
  128. f, err := os.Create(memProfilePath)
  129. if err != nil {
  130. fmt.Println("could not create memory profile: ", err)
  131. os.Exit(1)
  132. }
  133. if err := pprof.WriteHeapProfile(f); err != nil {
  134. fmt.Println("could not write memory profile: ", err)
  135. os.Exit(1)
  136. }
  137. f.Close()
  138. }
  139. if ctx.GlobalBool(DebugFlag.Name) {
  140. fmt.Fprintln(os.Stderr, "#### TRACE ####")
  141. vm.WriteTrace(os.Stderr, logger.StructLogs())
  142. fmt.Fprintln(os.Stderr, "#### LOGS ####")
  143. vm.WriteLogs(os.Stderr, statedb.Logs())
  144. }
  145. if ctx.GlobalBool(StatDumpFlag.Name) {
  146. var mem goruntime.MemStats
  147. goruntime.ReadMemStats(&mem)
  148. fmt.Fprintf(os.Stderr, `evm execution time: %v
  149. heap objects: %d
  150. allocations: %d
  151. total allocations: %d
  152. GC calls: %d
  153. `, execTime, mem.HeapObjects, mem.Alloc, mem.TotalAlloc, mem.NumGC)
  154. }
  155. fmt.Printf("0x%x", ret)
  156. if err != nil {
  157. fmt.Printf(" error: %v", err)
  158. }
  159. fmt.Println()
  160. return nil
  161. }