2018-03-04 13:56:49 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-02-09 15:53:58 +00:00
|
|
|
"io"
|
2018-03-04 13:56:49 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2019-12-03 14:05:06 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
2018-03-04 13:56:49 +00:00
|
|
|
)
|
|
|
|
|
2019-08-14 12:40:31 +00:00
|
|
|
// Emit a VM Instruction with data to the given buffer.
|
2019-12-03 14:05:06 +00:00
|
|
|
func Emit(w *bytes.Buffer, op opcode.Opcode, b []byte) error {
|
2018-03-04 13:56:49 +00:00
|
|
|
if err := w.WriteByte(byte(op)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := w.Write(b)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-14 12:40:31 +00:00
|
|
|
// EmitOpcode emits a single VM Instruction the given buffer.
|
2019-12-03 14:05:06 +00:00
|
|
|
func EmitOpcode(w io.ByteWriter, op opcode.Opcode) error {
|
2018-03-04 13:56:49 +00:00
|
|
|
return w.WriteByte(byte(op))
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmitBool emits a bool type the given buffer.
|
2019-02-09 15:53:58 +00:00
|
|
|
func EmitBool(w io.ByteWriter, ok bool) error {
|
2018-03-04 13:56:49 +00:00
|
|
|
if ok {
|
2019-12-03 14:05:06 +00:00
|
|
|
return EmitOpcode(w, opcode.PUSHT)
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
2019-12-03 14:05:06 +00:00
|
|
|
return EmitOpcode(w, opcode.PUSHF)
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EmitInt emits a int type to the given buffer.
|
|
|
|
func EmitInt(w *bytes.Buffer, i int64) error {
|
|
|
|
if i == -1 {
|
2019-12-03 14:05:06 +00:00
|
|
|
return EmitOpcode(w, opcode.PUSHM1)
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
if i == 0 {
|
2019-12-03 14:05:06 +00:00
|
|
|
return EmitOpcode(w, opcode.PUSHF)
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
if i > 0 && i < 16 {
|
2019-12-03 14:05:06 +00:00
|
|
|
val := opcode.Opcode(int(opcode.PUSH1) - 1 + int(i))
|
2018-03-04 13:56:49 +00:00
|
|
|
return EmitOpcode(w, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
bInt := big.NewInt(i)
|
|
|
|
val := util.ArrayReverse(bInt.Bytes())
|
|
|
|
return EmitBytes(w, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmitString emits a string to the given buffer.
|
|
|
|
func EmitString(w *bytes.Buffer, s string) error {
|
|
|
|
return EmitBytes(w, []byte(s))
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// EmitBytes emits a byte array to the given buffer.
|
2018-03-04 13:56:49 +00:00
|
|
|
func EmitBytes(w *bytes.Buffer, b []byte) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
n = len(b)
|
|
|
|
)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
if n <= int(opcode.PUSHBYTES75) {
|
|
|
|
return Emit(w, opcode.Opcode(n), b)
|
2018-03-04 13:56:49 +00:00
|
|
|
} else if n < 0x100 {
|
2019-12-03 14:05:06 +00:00
|
|
|
err = Emit(w, opcode.PUSHDATA1, []byte{byte(n)})
|
2018-03-04 13:56:49 +00:00
|
|
|
} else if n < 0x10000 {
|
|
|
|
buf := make([]byte, 2)
|
|
|
|
binary.LittleEndian.PutUint16(buf, uint16(n))
|
2019-12-03 14:05:06 +00:00
|
|
|
err = Emit(w, opcode.PUSHDATA2, buf)
|
2018-03-04 13:56:49 +00:00
|
|
|
} else {
|
|
|
|
buf := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(buf, uint32(n))
|
2019-12-03 14:05:06 +00:00
|
|
|
err = Emit(w, opcode.PUSHDATA4, buf)
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(b)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmitSyscall emits the syscall API to the given buffer.
|
|
|
|
// Syscall API string cannot be 0.
|
|
|
|
func EmitSyscall(w *bytes.Buffer, api string) error {
|
|
|
|
if len(api) == 0 {
|
|
|
|
return errors.New("syscall api cannot be of length 0")
|
|
|
|
}
|
|
|
|
buf := make([]byte, len(api)+1)
|
|
|
|
buf[0] = byte(len(api))
|
2019-10-18 15:39:31 +00:00
|
|
|
copy(buf[1:], api)
|
2019-12-03 14:05:06 +00:00
|
|
|
return Emit(w, opcode.SYSCALL, buf)
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 12:40:31 +00:00
|
|
|
// EmitCall emits a call Instruction with label to the given buffer.
|
2019-12-03 14:05:06 +00:00
|
|
|
func EmitCall(w *bytes.Buffer, op opcode.Opcode, label int16) error {
|
2018-03-04 13:56:49 +00:00
|
|
|
return EmitJmp(w, op, label)
|
|
|
|
}
|
|
|
|
|
2019-08-14 12:40:31 +00:00
|
|
|
// EmitJmp emits a jump Instruction along with label to the given buffer.
|
2019-12-03 14:05:06 +00:00
|
|
|
func EmitJmp(w *bytes.Buffer, op opcode.Opcode, label int16) error {
|
2019-08-14 12:40:31 +00:00
|
|
|
if !isInstructionJmp(op) {
|
2019-02-09 15:53:58 +00:00
|
|
|
return fmt.Errorf("opcode %s is not a jump or call type", op.String())
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
buf := make([]byte, 2)
|
|
|
|
binary.LittleEndian.PutUint16(buf, uint16(label))
|
|
|
|
return Emit(w, op, buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmitAppCall emits an appcall, if tailCall is true, tailCall opcode will be
|
|
|
|
// emitted instead.
|
|
|
|
func EmitAppCall(w *bytes.Buffer, scriptHash util.Uint160, tailCall bool) error {
|
2019-12-03 14:05:06 +00:00
|
|
|
op := opcode.APPCALL
|
2018-03-04 13:56:49 +00:00
|
|
|
if tailCall {
|
2019-12-03 14:05:06 +00:00
|
|
|
op = opcode.TAILCALL
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
return Emit(w, op, scriptHash.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmitAppCallWithOperationAndData emits an appcall with the given operation and data.
|
|
|
|
func EmitAppCallWithOperationAndData(w *bytes.Buffer, scriptHash util.Uint160, operation string, data []byte) error {
|
|
|
|
if err := EmitBytes(w, data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := EmitString(w, operation); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return EmitAppCall(w, scriptHash, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmitAppCallWithOperation emits an appcall with the given operation.
|
|
|
|
func EmitAppCallWithOperation(w *bytes.Buffer, scriptHash util.Uint160, operation string) error {
|
|
|
|
if err := EmitBool(w, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := EmitString(w, operation); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return EmitAppCall(w, scriptHash, false)
|
|
|
|
}
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
func isInstructionJmp(op opcode.Opcode) bool {
|
|
|
|
if op == opcode.JMP || op == opcode.JMPIFNOT || op == opcode.JMPIF || op == opcode.CALL {
|
2018-03-04 13:56:49 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|