2018-03-30 16:15:06 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2018-04-04 19:41:19 +00:00
|
|
|
"bytes"
|
2021-08-12 13:39:46 +00:00
|
|
|
"crypto/elliptic"
|
2020-06-25 14:25:05 +00:00
|
|
|
"encoding/base64"
|
2018-04-04 19:41:19 +00:00
|
|
|
"encoding/hex"
|
2020-12-21 11:27:07 +00:00
|
|
|
"encoding/json"
|
2018-04-10 09:45:31 +00:00
|
|
|
"errors"
|
2018-03-30 16:15:06 +00:00
|
|
|
"fmt"
|
2020-12-21 11:27:07 +00:00
|
|
|
"io/ioutil"
|
2020-04-16 12:28:34 +00:00
|
|
|
"math/big"
|
2018-03-30 16:15:06 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-07-23 11:09:21 +00:00
|
|
|
"text/tabwriter"
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2021-07-21 20:20:33 +00:00
|
|
|
"github.com/abiosoft/ishell/v2"
|
2020-12-01 15:27:38 +00:00
|
|
|
"github.com/abiosoft/readline"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/compiler"
|
2021-08-12 13:39:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-07-23 11:09:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2021-05-28 09:07:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2020-12-21 11:27:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-07-23 11:09:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-07-18 12:55:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2018-03-30 16:15:06 +00:00
|
|
|
)
|
|
|
|
|
2019-09-10 16:41:11 +00:00
|
|
|
const (
|
2020-12-21 11:27:07 +00:00
|
|
|
vmKey = "vm"
|
|
|
|
manifestKey = "manifest"
|
|
|
|
boolType = "bool"
|
|
|
|
boolFalse = "false"
|
|
|
|
boolTrue = "true"
|
|
|
|
intType = "int"
|
|
|
|
stringType = "string"
|
|
|
|
exitFunc = "exitFunc"
|
2019-09-10 16:41:11 +00:00
|
|
|
)
|
2019-09-10 16:11:48 +00:00
|
|
|
|
|
|
|
var commands = []*ishell.Cmd{
|
|
|
|
{
|
|
|
|
Name: "exit",
|
|
|
|
Help: "Exit the VM prompt",
|
|
|
|
LongHelp: "Exit the VM prompt",
|
|
|
|
Func: handleExit,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "ip",
|
|
|
|
Help: "Show current instruction",
|
|
|
|
LongHelp: "Show current instruction",
|
|
|
|
Func: handleIP,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "break",
|
|
|
|
Help: "Place a breakpoint",
|
|
|
|
LongHelp: `Usage: break <ip>
|
|
|
|
<ip> is mandatory parameter, example:
|
|
|
|
> break 12`,
|
|
|
|
Func: handleBreak,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "estack",
|
|
|
|
Help: "Show evaluation stack contents",
|
|
|
|
LongHelp: "Show evaluation stack contents",
|
|
|
|
Func: handleXStack,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "istack",
|
|
|
|
Help: "Show invocation stack contents",
|
|
|
|
LongHelp: "Show invocation stack contents",
|
|
|
|
Func: handleXStack,
|
|
|
|
},
|
2021-09-08 14:27:11 +00:00
|
|
|
{
|
|
|
|
Name: "sslot",
|
|
|
|
Help: "Show static slot contents",
|
|
|
|
LongHelp: "Show static slot contents",
|
|
|
|
Func: handleSlots,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "lslot",
|
|
|
|
Help: "Show local slot contents",
|
|
|
|
LongHelp: "Show local slot contents",
|
|
|
|
Func: handleSlots,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "aslot",
|
|
|
|
Help: "Show arguments slot contents",
|
|
|
|
LongHelp: "Show arguments slot contents",
|
|
|
|
Func: handleSlots,
|
|
|
|
},
|
2019-09-10 16:11:48 +00:00
|
|
|
{
|
2020-06-25 07:36:21 +00:00
|
|
|
Name: "loadnef",
|
|
|
|
Help: "Load a NEF-consistent script into the VM",
|
2020-12-21 11:27:07 +00:00
|
|
|
LongHelp: `Usage: loadnef <file> <manifest>
|
|
|
|
both parameters are mandatory, example:
|
|
|
|
> loadnef /path/to/script.nef /path/to/manifest.json`,
|
2020-06-25 07:36:21 +00:00
|
|
|
Func: handleLoadNEF,
|
2019-09-10 16:11:48 +00:00
|
|
|
},
|
2020-06-25 14:25:05 +00:00
|
|
|
{
|
|
|
|
Name: "loadbase64",
|
|
|
|
Help: "Load a base64-encoded script string into the VM",
|
|
|
|
LongHelp: `Usage: loadbase64 <string>
|
|
|
|
<string> is mandatory parameter, example:
|
|
|
|
> loadbase64 AwAQpdToAAAADBQV9ehtQR1OrVZVhtHtoUHRfoE+agwUzmFvf3Rhfg/EuAVYOvJgKiON9j8TwAwIdHJhbnNmZXIMFDt9NxHG8Mz5sdypA9G/odiW8SOMQWJ9W1I4`,
|
|
|
|
Func: handleLoadBase64,
|
|
|
|
},
|
2019-09-10 16:11:48 +00:00
|
|
|
{
|
|
|
|
Name: "loadhex",
|
|
|
|
Help: "Load a hex-encoded script string into the VM",
|
|
|
|
LongHelp: `Usage: loadhex <string>
|
|
|
|
<string> is mandatory parameter, example:
|
2020-06-25 07:38:53 +00:00
|
|
|
> loadhex 0c0c48656c6c6f20776f726c6421`,
|
2019-09-10 16:11:48 +00:00
|
|
|
Func: handleLoadHex,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "loadgo",
|
2020-12-21 11:27:07 +00:00
|
|
|
Help: "Compile and load a Go file with the manifest into the VM",
|
2020-06-25 07:38:53 +00:00
|
|
|
LongHelp: `Usage: loadgo <file>
|
2019-09-10 16:11:48 +00:00
|
|
|
<file> is mandatory parameter, example:
|
2020-06-25 07:38:53 +00:00
|
|
|
> loadgo /path/to/file.go`,
|
2019-09-10 16:11:48 +00:00
|
|
|
Func: handleLoadGo,
|
|
|
|
},
|
2020-07-23 11:09:21 +00:00
|
|
|
{
|
|
|
|
Name: "parse",
|
|
|
|
Help: "Parse provided argument and convert it into other possible formats",
|
|
|
|
LongHelp: `Usage: parse <arg>
|
|
|
|
|
|
|
|
<arg> is an argument which is tried to be interpreted as an item of different types
|
|
|
|
and converted to other formats. Strings are escaped and output in quotes.`,
|
|
|
|
Func: handleParse,
|
|
|
|
},
|
2019-09-10 16:11:48 +00:00
|
|
|
{
|
|
|
|
Name: "run",
|
|
|
|
Help: "Execute the current loaded script",
|
2020-12-21 11:27:07 +00:00
|
|
|
LongHelp: `Usage: run [<method> [<parameter>...]]
|
2019-09-10 16:11:48 +00:00
|
|
|
|
2020-12-21 11:27:07 +00:00
|
|
|
<method> is a contract method, specified in manifest (and it
|
|
|
|
can't be 'help' at the moment). It can be '_' which will push parameters
|
|
|
|
onto the stack and execute from the current offset.
|
2019-09-10 16:41:11 +00:00
|
|
|
<parameter> is a parameter (can be repeated multiple times) that can be specified
|
2019-09-10 16:30:32 +00:00
|
|
|
as <type>:<value>, where type can be:
|
2019-09-10 16:41:11 +00:00
|
|
|
'` + boolType + `': supports '` + boolFalse + `' and '` + boolTrue + `' values
|
|
|
|
'` + intType + `': supports integers as values
|
|
|
|
'` + stringType + `': supports strings as values (that are pushed as a byte array
|
|
|
|
values to the stack)
|
|
|
|
or can be just <value>, for which the type will be detected automatically
|
|
|
|
following these rules: '` + boolTrue + `' and '` + boolFalse + `' are treated as respective
|
|
|
|
boolean values, everything that can be converted to integer is treated as
|
|
|
|
integer and everything else is treated like a string.
|
2019-09-10 16:30:32 +00:00
|
|
|
|
2019-09-10 16:11:48 +00:00
|
|
|
Example:
|
2019-09-10 16:41:11 +00:00
|
|
|
> run put ` + stringType + `:"Something to put"`,
|
2019-09-10 16:11:48 +00:00
|
|
|
Func: handleRun,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "cont",
|
|
|
|
Help: "Continue execution of the current loaded script",
|
|
|
|
LongHelp: "Continue execution of the current loaded script",
|
|
|
|
Func: handleCont,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "step",
|
|
|
|
Help: "Step (n) instruction in the program",
|
|
|
|
LongHelp: `Usage: step [<n>]
|
|
|
|
<n> is optional parameter to specify number of instructions to run, example:
|
|
|
|
> step 10`,
|
|
|
|
Func: handleStep,
|
|
|
|
},
|
2019-10-14 15:37:11 +00:00
|
|
|
{
|
|
|
|
Name: "stepinto",
|
|
|
|
Help: "Stepinto instruction to take in the debugger",
|
|
|
|
LongHelp: `Usage: stepInto
|
|
|
|
example:
|
|
|
|
> stepinto`,
|
|
|
|
Func: handleStepInto,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "stepout",
|
|
|
|
Help: "Stepout instruction to take in the debugger",
|
|
|
|
LongHelp: `Usage: stepOut
|
|
|
|
example:
|
|
|
|
> stepout`,
|
|
|
|
Func: handleStepOut,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "stepover",
|
|
|
|
Help: "Stepover instruction to take in the debugger",
|
|
|
|
LongHelp: `Usage: stepOver
|
|
|
|
example:
|
|
|
|
> stepover`,
|
|
|
|
Func: handleStepOver,
|
|
|
|
},
|
2019-09-10 16:11:48 +00:00
|
|
|
{
|
|
|
|
Name: "ops",
|
|
|
|
Help: "Dump opcodes of the current loaded program",
|
|
|
|
LongHelp: "Dump opcodes of the current loaded program",
|
|
|
|
Func: handleOps,
|
|
|
|
},
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 14:15:01 +00:00
|
|
|
// Various errors.
|
|
|
|
var (
|
|
|
|
ErrMissingParameter = errors.New("missing argument")
|
|
|
|
ErrInvalidParameter = errors.New("can't parse argument")
|
|
|
|
)
|
|
|
|
|
2019-10-14 15:38:05 +00:00
|
|
|
// VMCLI object for interacting with the VM.
|
2018-03-30 16:15:06 +00:00
|
|
|
type VMCLI struct {
|
2019-09-10 16:11:48 +00:00
|
|
|
vm *vm.VM
|
|
|
|
shell *ishell.Shell
|
2020-12-01 15:27:38 +00:00
|
|
|
// printLogo specifies if logo is printed.
|
|
|
|
printLogo bool
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new VMCLI object.
|
|
|
|
func New() *VMCLI {
|
2020-12-01 15:27:38 +00:00
|
|
|
return NewWithConfig(true, os.Exit, &readline.Config{
|
|
|
|
Prompt: ">>>",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWithConfig returns new VMCLI instance using provided config.
|
|
|
|
func NewWithConfig(printLogo bool, onExit func(int), c *readline.Config) *VMCLI {
|
2019-09-10 16:11:48 +00:00
|
|
|
vmcli := VMCLI{
|
2020-12-01 15:27:38 +00:00
|
|
|
vm: vm.New(),
|
|
|
|
shell: ishell.NewWithConfig(c),
|
|
|
|
printLogo: printLogo,
|
2019-09-10 16:11:48 +00:00
|
|
|
}
|
|
|
|
vmcli.shell.Set(vmKey, vmcli.vm)
|
2020-12-21 11:27:07 +00:00
|
|
|
vmcli.shell.Set(manifestKey, new(manifest.Manifest))
|
2020-12-01 15:27:38 +00:00
|
|
|
vmcli.shell.Set(exitFunc, onExit)
|
2019-09-10 16:11:48 +00:00
|
|
|
for _, c := range commands {
|
|
|
|
vmcli.shell.AddCmd(c)
|
|
|
|
}
|
|
|
|
changePrompt(vmcli.shell, vmcli.vm)
|
|
|
|
return &vmcli
|
|
|
|
}
|
|
|
|
|
|
|
|
func getVMFromContext(c *ishell.Context) *vm.VM {
|
|
|
|
return c.Get(vmKey).(*vm.VM)
|
|
|
|
}
|
|
|
|
|
2020-12-21 11:27:07 +00:00
|
|
|
func getManifestFromContext(c *ishell.Context) *manifest.Manifest {
|
|
|
|
return c.Get(manifestKey).(*manifest.Manifest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setManifestInContext(c *ishell.Context, m *manifest.Manifest) {
|
|
|
|
old := getManifestFromContext(c)
|
|
|
|
*old = *m
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:11:48 +00:00
|
|
|
func checkVMIsReady(c *ishell.Context) bool {
|
|
|
|
v := getVMFromContext(c)
|
|
|
|
if v == nil || !v.Ready() {
|
|
|
|
c.Err(errors.New("VM is not ready: no program loaded"))
|
|
|
|
return false
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
2019-09-10 16:11:48 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleExit(c *ishell.Context) {
|
|
|
|
c.Println("Bye!")
|
2020-12-01 15:27:38 +00:00
|
|
|
c.Get(exitFunc).(func(int))(0)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 16:11:48 +00:00
|
|
|
func handleIP(c *ishell.Context) {
|
|
|
|
if !checkVMIsReady(c) {
|
2018-03-30 16:15:06 +00:00
|
|
|
return
|
|
|
|
}
|
2019-09-10 16:11:48 +00:00
|
|
|
v := getVMFromContext(c)
|
2020-12-01 13:53:38 +00:00
|
|
|
ctx := v.Context()
|
|
|
|
if ctx.NextIP() < ctx.LenInstr() {
|
|
|
|
ip, opcode := v.Context().NextInstr()
|
|
|
|
c.Printf("instruction pointer at %d (%s)\n", ip, opcode)
|
|
|
|
} else {
|
|
|
|
c.Println("execution has finished")
|
|
|
|
}
|
2019-09-10 16:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleBreak(c *ishell.Context) {
|
|
|
|
if !checkVMIsReady(c) {
|
2018-03-30 16:15:06 +00:00
|
|
|
return
|
|
|
|
}
|
2019-09-10 16:11:48 +00:00
|
|
|
v := getVMFromContext(c)
|
|
|
|
if len(c.Args) != 1 {
|
2020-12-01 14:15:01 +00:00
|
|
|
c.Err(fmt.Errorf("%w: <ip>", ErrMissingParameter))
|
2020-12-01 15:30:19 +00:00
|
|
|
return
|
2019-09-10 16:11:48 +00:00
|
|
|
}
|
|
|
|
n, err := strconv.Atoi(c.Args[0])
|
|
|
|
if err != nil {
|
2020-12-01 14:15:01 +00:00
|
|
|
c.Err(fmt.Errorf("%w: %v", ErrInvalidParameter, err))
|
2018-03-30 16:15:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:11:48 +00:00
|
|
|
v.AddBreakPoint(n)
|
|
|
|
c.Printf("breakpoint added at instruction %d\n", n)
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-09-10 16:11:48 +00:00
|
|
|
func handleXStack(c *ishell.Context) {
|
|
|
|
v := getVMFromContext(c)
|
2021-09-08 15:51:34 +00:00
|
|
|
var stackDump string
|
|
|
|
switch c.Cmd.Name {
|
|
|
|
case "estack":
|
|
|
|
stackDump = v.DumpEStack()
|
|
|
|
case "istack":
|
|
|
|
stackDump = v.DumpIStack()
|
|
|
|
default:
|
|
|
|
c.Err(errors.New("unknown stack"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Println(stackDump)
|
2019-09-10 16:11:48 +00:00
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2021-09-08 14:27:11 +00:00
|
|
|
func handleSlots(c *ishell.Context) {
|
|
|
|
v := getVMFromContext(c)
|
|
|
|
vmCtx := v.Context()
|
|
|
|
if vmCtx == nil {
|
|
|
|
c.Err(errors.New("no program loaded"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var rawSlot string
|
|
|
|
switch c.Cmd.Name {
|
|
|
|
case "sslot":
|
|
|
|
rawSlot = vmCtx.DumpStaticSlot()
|
|
|
|
case "lslot":
|
|
|
|
rawSlot = vmCtx.DumpLocalSlot()
|
|
|
|
case "aslot":
|
|
|
|
rawSlot = vmCtx.DumpArgumentsSlot()
|
|
|
|
default:
|
|
|
|
c.Err(errors.New("unknown slot"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Println(rawSlot)
|
|
|
|
}
|
|
|
|
|
2020-06-25 07:36:21 +00:00
|
|
|
func handleLoadNEF(c *ishell.Context) {
|
2019-09-10 16:11:48 +00:00
|
|
|
v := getVMFromContext(c)
|
2020-12-21 11:27:07 +00:00
|
|
|
if len(c.Args) < 2 {
|
|
|
|
c.Err(fmt.Errorf("%w: <file> <manifest>", ErrMissingParameter))
|
2020-06-25 14:32:58 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-28 09:07:03 +00:00
|
|
|
if err := v.LoadFileWithFlags(c.Args[0], callflag.All); err != nil {
|
2019-09-10 16:11:48 +00:00
|
|
|
c.Err(err)
|
2020-12-21 11:27:07 +00:00
|
|
|
return
|
2019-09-10 16:11:48 +00:00
|
|
|
}
|
2020-12-21 11:27:07 +00:00
|
|
|
m, err := getManifestFromFile(c.Args[1])
|
|
|
|
if err != nil {
|
|
|
|
c.Err(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Printf("READY: loaded %d instructions\n", v.Context().LenInstr())
|
|
|
|
setManifestInContext(c, m)
|
2019-09-10 16:11:48 +00:00
|
|
|
changePrompt(c, v)
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2020-06-25 14:25:05 +00:00
|
|
|
func handleLoadBase64(c *ishell.Context) {
|
|
|
|
v := getVMFromContext(c)
|
2020-06-25 14:32:58 +00:00
|
|
|
if len(c.Args) < 1 {
|
2020-12-01 14:15:01 +00:00
|
|
|
c.Err(fmt.Errorf("%w: <string>", ErrMissingParameter))
|
2020-06-25 14:32:58 +00:00
|
|
|
return
|
|
|
|
}
|
2020-06-25 14:25:05 +00:00
|
|
|
b, err := base64.StdEncoding.DecodeString(c.Args[0])
|
|
|
|
if err != nil {
|
2020-12-01 14:15:01 +00:00
|
|
|
c.Err(fmt.Errorf("%w: %v", ErrInvalidParameter, err))
|
2020-06-25 14:25:05 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-28 09:07:03 +00:00
|
|
|
v.LoadWithFlags(b, callflag.All)
|
2020-06-25 14:25:05 +00:00
|
|
|
c.Printf("READY: loaded %d instructions\n", v.Context().LenInstr())
|
|
|
|
changePrompt(c, v)
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:11:48 +00:00
|
|
|
func handleLoadHex(c *ishell.Context) {
|
|
|
|
v := getVMFromContext(c)
|
2020-06-25 14:32:58 +00:00
|
|
|
if len(c.Args) < 1 {
|
2020-12-01 14:15:01 +00:00
|
|
|
c.Err(fmt.Errorf("%w: <string>", ErrMissingParameter))
|
2020-06-25 14:32:58 +00:00
|
|
|
return
|
|
|
|
}
|
2019-09-10 16:11:48 +00:00
|
|
|
b, err := hex.DecodeString(c.Args[0])
|
|
|
|
if err != nil {
|
2020-12-01 14:15:01 +00:00
|
|
|
c.Err(fmt.Errorf("%w: %v", ErrInvalidParameter, err))
|
2019-09-10 16:11:48 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-28 09:07:03 +00:00
|
|
|
v.LoadWithFlags(b, callflag.All)
|
2019-09-10 16:11:48 +00:00
|
|
|
c.Printf("READY: loaded %d instructions\n", v.Context().LenInstr())
|
|
|
|
changePrompt(c, v)
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-09-10 16:11:48 +00:00
|
|
|
func handleLoadGo(c *ishell.Context) {
|
|
|
|
v := getVMFromContext(c)
|
2020-06-25 14:32:58 +00:00
|
|
|
if len(c.Args) < 1 {
|
2020-12-01 14:15:01 +00:00
|
|
|
c.Err(fmt.Errorf("%w: <file>", ErrMissingParameter))
|
2020-06-25 14:32:58 +00:00
|
|
|
return
|
|
|
|
}
|
2021-12-02 13:36:29 +00:00
|
|
|
|
|
|
|
name := strings.TrimSuffix(c.Args[0], ".go")
|
|
|
|
b, di, err := compiler.CompileWithOptions(c.Args[0], nil, &compiler.Options{Name: name})
|
2019-09-10 16:11:48 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Err(err)
|
|
|
|
return
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2020-12-21 11:27:07 +00:00
|
|
|
// Don't perform checks, just load.
|
|
|
|
m, err := di.ConvertToManifest(&compiler.Options{})
|
|
|
|
if err != nil {
|
|
|
|
c.Err(fmt.Errorf("can't create manifest: %w", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setManifestInContext(c, m)
|
|
|
|
|
2021-12-08 19:33:03 +00:00
|
|
|
v.LoadWithFlags(b.Script, callflag.All)
|
2019-09-10 16:11:48 +00:00
|
|
|
c.Printf("READY: loaded %d instructions\n", v.Context().LenInstr())
|
|
|
|
changePrompt(c, v)
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2020-12-21 11:27:07 +00:00
|
|
|
func getManifestFromFile(name string) (*manifest.Manifest, error) {
|
|
|
|
bs, err := ioutil.ReadFile(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: can't read manifest", ErrInvalidParameter)
|
|
|
|
}
|
|
|
|
|
|
|
|
var m manifest.Manifest
|
|
|
|
if err := json.Unmarshal(bs, &m); err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: can't unmarshal manifest", ErrInvalidParameter)
|
|
|
|
}
|
|
|
|
return &m, nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:11:48 +00:00
|
|
|
func handleRun(c *ishell.Context) {
|
|
|
|
v := getVMFromContext(c)
|
2020-12-21 11:27:07 +00:00
|
|
|
m := getManifestFromContext(c)
|
2019-09-10 16:11:48 +00:00
|
|
|
if len(c.Args) != 0 {
|
|
|
|
var (
|
2020-12-21 11:27:07 +00:00
|
|
|
params []stackitem.Item
|
|
|
|
offset int
|
|
|
|
err error
|
|
|
|
runCurrent = c.Args[0] != "_"
|
2019-09-10 16:11:48 +00:00
|
|
|
)
|
2020-12-21 11:27:07 +00:00
|
|
|
|
2021-01-26 14:37:34 +00:00
|
|
|
params, err = parseArgs(c.Args[1:])
|
|
|
|
if err != nil {
|
|
|
|
c.Err(err)
|
|
|
|
return
|
|
|
|
}
|
2020-12-21 11:27:07 +00:00
|
|
|
if runCurrent {
|
2021-01-26 14:37:34 +00:00
|
|
|
md := m.ABI.GetMethod(c.Args[0], len(params))
|
2020-12-21 11:27:07 +00:00
|
|
|
if md == nil {
|
|
|
|
c.Err(fmt.Errorf("%w: method not found", ErrInvalidParameter))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
offset = md.Offset
|
|
|
|
}
|
|
|
|
for i := len(params) - 1; i >= 0; i-- {
|
|
|
|
v.Estack().PushVal(params[i])
|
|
|
|
}
|
|
|
|
if runCurrent {
|
vm CLI: check whether VM is ready before jumping to the instruction
It allows to avoid panic:
```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0xdab469]
goroutine 1 [running]:
github.com/nspcc-dev/neo-go/pkg/vm.(*VM).Jump(...)
github.com/nspcc-dev/neo-go/pkg/vm/vm.go:1506
github.com/nspcc-dev/neo-go/pkg/vm/cli.handleRun(0xc0005988f0)
github.com/nspcc-dev/neo-go/pkg/vm/cli/cli.go:413 +0x2e9
github.com/abiosoft/ishell/v2.(*Shell).handleCommand(0xc0004320f0, {0xc00032c7c0, 0xc0002a3920, 0x0})
github.com/abiosoft/ishell/v2@v2.0.2/ishell.go:279 +0x143
github.com/abiosoft/ishell/v2.handleInput(0xc0004320f0, {0xc00032c7c0, 0x3, 0x4})
github.com/abiosoft/ishell/v2@v2.0.2/ishell.go:233 +0x31
github.com/abiosoft/ishell/v2.(*Shell).run(0xc0004320f0)
github.com/abiosoft/ishell/v2@v2.0.2/ishell.go:212 +0x30f
github.com/abiosoft/ishell/v2.(*Shell).Run(0xc0004320f0)
github.com/abiosoft/ishell/v2@v2.0.2/ishell.go:112 +0x28
github.com/nspcc-dev/neo-go/pkg/vm/cli.(*VMCLI).Run(0xc000224030)
github.com/nspcc-dev/neo-go/pkg/vm/cli/cli.go:538 +0x39
github.com/nspcc-dev/neo-go/cli/vm.startVMPrompt(0xc0001f46e0)
github.com/nspcc-dev/neo-go/cli/vm/vm.go:28 +0xb4
github.com/urfave/cli.HandleAction({0xe65fa0, 0x1161c68}, 0x2)
github.com/urfave/cli@v1.22.5/app.go:524 +0xa8
github.com/urfave/cli.Command.Run({{0xfed435, 0x2}, {0x0, 0x0}, {0x0, 0x0, 0x0}, {0x100576d, 0x19}, {0x0, ...}, ...}, ...)
github.com/urfave/cli@v1.22.5/command.go:173 +0x652
github.com/urfave/cli.(*App).Run(0xc0001016c0, {0xc0000c6000, 0x2, 0x2})
github.com/urfave/cli@v1.22.5/app.go:277 +0x705
main.main()
./main.go:19 +0x33
```
2021-09-08 09:38:53 +00:00
|
|
|
if !v.Ready() {
|
|
|
|
c.Err(fmt.Errorf("no program loaded"))
|
|
|
|
return
|
|
|
|
}
|
2021-11-19 13:46:29 +00:00
|
|
|
v.Context().Jump(offset)
|
2021-01-26 14:37:34 +00:00
|
|
|
if initMD := m.ABI.GetMethod(manifest.MethodInit, 0); initMD != nil {
|
2021-11-19 14:15:30 +00:00
|
|
|
v.Call(initMD.Offset)
|
2020-12-21 11:27:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-10 16:11:48 +00:00
|
|
|
}
|
2019-10-22 10:44:14 +00:00
|
|
|
runVMWithHandling(c, v)
|
2019-09-10 16:11:48 +00:00
|
|
|
changePrompt(c, v)
|
|
|
|
}
|
2018-04-04 19:41:19 +00:00
|
|
|
|
2019-10-22 10:44:14 +00:00
|
|
|
// runVMWithHandling runs VM with handling errors and additional state messages.
|
|
|
|
func runVMWithHandling(c *ishell.Context, v *vm.VM) {
|
|
|
|
err := v.Run()
|
|
|
|
if err != nil {
|
|
|
|
c.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var message string
|
|
|
|
switch {
|
|
|
|
case v.HasFailed():
|
2020-12-01 13:53:38 +00:00
|
|
|
message = "" // the error will be printed on return
|
2019-10-22 10:44:14 +00:00
|
|
|
case v.HasHalted():
|
2021-09-08 15:51:34 +00:00
|
|
|
message = v.DumpEStack()
|
2019-10-22 10:44:14 +00:00
|
|
|
case v.AtBreakpoint():
|
|
|
|
ctx := v.Context()
|
2020-12-01 13:53:38 +00:00
|
|
|
if ctx.NextIP() < ctx.LenInstr() {
|
|
|
|
i, op := ctx.NextInstr()
|
|
|
|
message = fmt.Sprintf("at breakpoint %d (%s)", i, op)
|
|
|
|
} else {
|
|
|
|
message = "execution has finished"
|
|
|
|
}
|
2019-10-22 10:44:14 +00:00
|
|
|
}
|
|
|
|
if message != "" {
|
2020-12-01 13:53:38 +00:00
|
|
|
c.Println(message)
|
2019-10-22 10:44:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:11:48 +00:00
|
|
|
func handleCont(c *ishell.Context) {
|
|
|
|
if !checkVMIsReady(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v := getVMFromContext(c)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVMWithHandling(c, v)
|
2019-09-10 16:11:48 +00:00
|
|
|
changePrompt(c, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleStep(c *ishell.Context) {
|
|
|
|
var (
|
|
|
|
n = 1
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if !checkVMIsReady(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v := getVMFromContext(c)
|
|
|
|
if len(c.Args) > 0 {
|
|
|
|
n, err = strconv.Atoi(c.Args[0])
|
2018-04-04 19:41:19 +00:00
|
|
|
if err != nil {
|
2020-12-01 14:15:01 +00:00
|
|
|
c.Err(fmt.Errorf("%w: %v", ErrInvalidParameter, err))
|
2018-04-04 19:41:19 +00:00
|
|
|
return
|
|
|
|
}
|
2019-09-10 16:11:48 +00:00
|
|
|
}
|
|
|
|
v.AddBreakPointRel(n)
|
2019-10-22 10:44:14 +00:00
|
|
|
runVMWithHandling(c, v)
|
2019-09-10 16:11:48 +00:00
|
|
|
changePrompt(c, v)
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-10-14 15:37:11 +00:00
|
|
|
func handleStepInto(c *ishell.Context) {
|
|
|
|
handleStepType(c, "into")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleStepOut(c *ishell.Context) {
|
|
|
|
handleStepType(c, "out")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleStepOver(c *ishell.Context) {
|
|
|
|
handleStepType(c, "over")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleStepType(c *ishell.Context, stepType string) {
|
|
|
|
if !checkVMIsReady(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v := getVMFromContext(c)
|
2019-10-22 10:44:14 +00:00
|
|
|
var err error
|
2019-10-14 15:37:11 +00:00
|
|
|
switch stepType {
|
|
|
|
case "into":
|
2019-10-22 10:44:14 +00:00
|
|
|
err = v.StepInto()
|
2019-10-14 15:37:11 +00:00
|
|
|
case "out":
|
2019-10-22 10:44:14 +00:00
|
|
|
err = v.StepOut()
|
2019-10-14 15:37:11 +00:00
|
|
|
case "over":
|
2019-10-22 10:44:14 +00:00
|
|
|
err = v.StepOver()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.Err(err)
|
2020-12-01 13:53:38 +00:00
|
|
|
} else {
|
|
|
|
handleIP(c)
|
2019-10-14 15:37:11 +00:00
|
|
|
}
|
|
|
|
changePrompt(c, v)
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:11:48 +00:00
|
|
|
func handleOps(c *ishell.Context) {
|
|
|
|
if !checkVMIsReady(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v := getVMFromContext(c)
|
2020-12-01 13:52:23 +00:00
|
|
|
out := bytes.NewBuffer(nil)
|
|
|
|
v.PrintOps(out)
|
|
|
|
c.Println(out.String())
|
2019-09-10 16:11:48 +00:00
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-09-10 16:11:48 +00:00
|
|
|
func changePrompt(c ishell.Actions, v *vm.VM) {
|
2020-12-01 13:53:38 +00:00
|
|
|
if v.Ready() && v.Context().NextIP() >= 0 && v.Context().NextIP() < v.Context().LenInstr() {
|
|
|
|
c.SetPrompt(fmt.Sprintf("NEO-GO-VM %d > ", v.Context().NextIP()))
|
2019-09-10 16:11:48 +00:00
|
|
|
} else {
|
|
|
|
c.SetPrompt("NEO-GO-VM > ")
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run waits for user input from Stdin and executes the passed command.
|
|
|
|
func (c *VMCLI) Run() error {
|
2020-12-01 15:27:38 +00:00
|
|
|
if c.printLogo {
|
|
|
|
printLogo(c.shell)
|
|
|
|
}
|
2019-09-10 16:11:48 +00:00
|
|
|
c.shell.Run()
|
|
|
|
return nil
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 11:09:21 +00:00
|
|
|
func handleParse(c *ishell.Context) {
|
2020-08-04 06:40:06 +00:00
|
|
|
res, err := Parse(c.Args)
|
|
|
|
if err != nil {
|
|
|
|
c.Err(err)
|
2020-07-23 11:09:21 +00:00
|
|
|
return
|
|
|
|
}
|
2020-08-04 06:40:06 +00:00
|
|
|
c.Print(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse converts it's argument to other formats.
|
|
|
|
func Parse(args []string) (string, error) {
|
|
|
|
if len(args) < 1 {
|
2020-12-01 14:15:01 +00:00
|
|
|
return "", ErrMissingParameter
|
2020-08-04 06:40:06 +00:00
|
|
|
}
|
|
|
|
arg := args[0]
|
2020-07-23 11:09:21 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
if val, err := strconv.ParseInt(arg, 10, 64); err == nil {
|
|
|
|
bs := bigint.ToBytes(big.NewInt(val))
|
|
|
|
buf.WriteString(fmt.Sprintf("Integer to Hex\t%s\n", hex.EncodeToString(bs)))
|
|
|
|
buf.WriteString(fmt.Sprintf("Integer to Base64\t%s\n", base64.StdEncoding.EncodeToString(bs)))
|
|
|
|
}
|
|
|
|
noX := strings.TrimPrefix(arg, "0x")
|
|
|
|
if rawStr, err := hex.DecodeString(noX); err == nil {
|
|
|
|
if val, err := util.Uint160DecodeBytesBE(rawStr); err == nil {
|
|
|
|
buf.WriteString(fmt.Sprintf("BE ScriptHash to Address\t%s\n", address.Uint160ToString(val)))
|
|
|
|
buf.WriteString(fmt.Sprintf("LE ScriptHash to Address\t%s\n", address.Uint160ToString(val.Reverse())))
|
|
|
|
}
|
2021-08-12 13:39:46 +00:00
|
|
|
if pub, err := keys.NewPublicKeyFromBytes(rawStr, elliptic.P256()); err == nil {
|
|
|
|
sh := pub.GetScriptHash()
|
|
|
|
buf.WriteString(fmt.Sprintf("Public key to BE ScriptHash\t%s\n", sh))
|
|
|
|
buf.WriteString(fmt.Sprintf("Public key to LE ScriptHash\t%s\n", sh.Reverse()))
|
|
|
|
buf.WriteString(fmt.Sprintf("Public key to Address\t%s\n", address.Uint160ToString(sh)))
|
|
|
|
}
|
2020-07-23 11:09:21 +00:00
|
|
|
buf.WriteString(fmt.Sprintf("Hex to String\t%s\n", fmt.Sprintf("%q", string(rawStr))))
|
|
|
|
buf.WriteString(fmt.Sprintf("Hex to Integer\t%s\n", bigint.FromBytes(rawStr)))
|
2021-07-18 12:55:37 +00:00
|
|
|
buf.WriteString(fmt.Sprintf("Swap Endianness\t%s\n", hex.EncodeToString(slice.CopyReverse(rawStr))))
|
2020-07-23 11:09:21 +00:00
|
|
|
}
|
|
|
|
if addr, err := address.StringToUint160(arg); err == nil {
|
|
|
|
buf.WriteString(fmt.Sprintf("Address to BE ScriptHash\t%s\n", addr))
|
|
|
|
buf.WriteString(fmt.Sprintf("Address to LE ScriptHash\t%s\n", addr.Reverse()))
|
|
|
|
buf.WriteString(fmt.Sprintf("Address to Base64 (BE)\t%s\n", base64.StdEncoding.EncodeToString(addr.BytesBE())))
|
|
|
|
buf.WriteString(fmt.Sprintf("Address to Base64 (LE)\t%s\n", base64.StdEncoding.EncodeToString(addr.BytesLE())))
|
|
|
|
}
|
|
|
|
if rawStr, err := base64.StdEncoding.DecodeString(arg); err == nil {
|
|
|
|
buf.WriteString(fmt.Sprintf("Base64 to String\t%s\n", fmt.Sprintf("%q", string(rawStr))))
|
|
|
|
buf.WriteString(fmt.Sprintf("Base64 to BigInteger\t%s\n", bigint.FromBytes(rawStr)))
|
2021-10-29 11:55:08 +00:00
|
|
|
if u, err := util.Uint160DecodeBytesBE(rawStr); err == nil {
|
|
|
|
buf.WriteString(fmt.Sprintf("Base64 to BE ScriptHash\t%s\n", u.StringBE()))
|
|
|
|
buf.WriteString(fmt.Sprintf("Base64 to LE ScriptHash\t%s\n", u.StringLE()))
|
|
|
|
buf.WriteString(fmt.Sprintf("Base64 to Address (BE)\t%s\n", address.Uint160ToString(u)))
|
|
|
|
buf.WriteString(fmt.Sprintf("Base64 to Address (LE)\t%s\n", address.Uint160ToString(u.Reverse())))
|
|
|
|
}
|
2020-07-23 11:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf("String to Hex\t%s\n", hex.EncodeToString([]byte(arg))))
|
|
|
|
buf.WriteString(fmt.Sprintf("String to Base64\t%s\n", base64.StdEncoding.EncodeToString([]byte(arg))))
|
|
|
|
|
|
|
|
out := buf.Bytes()
|
|
|
|
buf = bytes.NewBuffer(nil)
|
2020-08-04 06:40:27 +00:00
|
|
|
w := tabwriter.NewWriter(buf, 0, 4, 4, '\t', 0)
|
2020-07-23 11:09:21 +00:00
|
|
|
if _, err := w.Write(out); err != nil {
|
2020-08-04 06:40:06 +00:00
|
|
|
return "", err
|
2020-07-23 11:09:21 +00:00
|
|
|
}
|
|
|
|
if err := w.Flush(); err != nil {
|
2020-08-04 06:40:06 +00:00
|
|
|
return "", err
|
2020-07-23 11:09:21 +00:00
|
|
|
}
|
2021-05-12 15:42:50 +00:00
|
|
|
return buf.String(), nil
|
2020-07-23 11:09:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func parseArgs(args []string) ([]stackitem.Item, error) {
|
|
|
|
items := make([]stackitem.Item, len(args))
|
2018-04-10 09:45:31 +00:00
|
|
|
for i, arg := range args {
|
2019-09-10 16:41:11 +00:00
|
|
|
var typ, value string
|
2018-04-10 09:45:31 +00:00
|
|
|
typeAndVal := strings.Split(arg, ":")
|
|
|
|
if len(typeAndVal) < 2 {
|
2019-09-10 16:41:11 +00:00
|
|
|
if typeAndVal[0] == boolFalse || typeAndVal[0] == boolTrue {
|
|
|
|
typ = boolType
|
|
|
|
} else if _, err := strconv.Atoi(typeAndVal[0]); err == nil {
|
|
|
|
typ = intType
|
|
|
|
} else {
|
|
|
|
typ = stringType
|
|
|
|
}
|
|
|
|
value = typeAndVal[0]
|
|
|
|
} else {
|
|
|
|
typ = typeAndVal[0]
|
|
|
|
value = typeAndVal[1]
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch typ {
|
2019-09-10 16:41:11 +00:00
|
|
|
case boolType:
|
|
|
|
if value == boolFalse {
|
2020-06-03 12:55:06 +00:00
|
|
|
items[i] = stackitem.NewBool(false)
|
2019-09-10 16:41:11 +00:00
|
|
|
} else if value == boolTrue {
|
2020-06-03 12:55:06 +00:00
|
|
|
items[i] = stackitem.NewBool(true)
|
2019-09-10 16:30:32 +00:00
|
|
|
} else {
|
2020-12-01 14:15:01 +00:00
|
|
|
return nil, fmt.Errorf("%w: invalid bool value", ErrInvalidParameter)
|
2019-09-10 16:30:32 +00:00
|
|
|
}
|
2019-09-10 16:41:11 +00:00
|
|
|
case intType:
|
2020-03-24 08:06:26 +00:00
|
|
|
val, err := strconv.ParseInt(value, 10, 64)
|
2018-04-10 09:45:31 +00:00
|
|
|
if err != nil {
|
2020-12-01 14:15:01 +00:00
|
|
|
return nil, fmt.Errorf("%w: invalid integer value", ErrInvalidParameter)
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
items[i] = stackitem.NewBigInteger(big.NewInt(val))
|
2019-09-10 16:41:11 +00:00
|
|
|
case stringType:
|
2020-06-03 12:55:06 +00:00
|
|
|
items[i] = stackitem.NewByteArray([]byte(value))
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:27:38 +00:00
|
|
|
const logo = `
|
2018-03-30 16:15:06 +00:00
|
|
|
_ ____________ __________ _ ____ ___
|
|
|
|
/ | / / ____/ __ \ / ____/ __ \ | | / / |/ /
|
|
|
|
/ |/ / __/ / / / /_____/ / __/ / / /____| | / / /|_/ /
|
|
|
|
/ /| / /___/ /_/ /_____/ /_/ / /_/ /_____/ |/ / / / /
|
|
|
|
/_/ |_/_____/\____/ \____/\____/ |___/_/ /_/
|
|
|
|
`
|
2020-12-01 15:27:38 +00:00
|
|
|
|
|
|
|
func printLogo(c *ishell.Shell) {
|
2019-10-16 10:47:42 +00:00
|
|
|
c.Print(logo)
|
|
|
|
c.Println()
|
|
|
|
c.Println()
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|