9 "github.com/vapor/errors"
12 type virtualMachine struct {
15 program []byte // the program currently executing
20 expansionReserved bool
22 // Stores the data parsed out of an opcode. Used as input to
23 // data-pushing opcodes.
26 // CHECKPREDICATE spawns a child vm with depth+1
29 // In each of these stacks, stack[len(stack)-1] is the top element.
34 // TraceOut - if non-nil - will receive trace output during
36 var TraceOut io.Writer
38 // Verify program by running VM
39 func Verify(context *Context, gasLimit int64) (gasLeft int64, err error) {
41 if r := recover(); r != nil {
42 if rErr, ok := r.(error); ok {
43 err = errors.Sub(ErrUnexpected, rErr)
45 err = errors.Wrap(ErrUnexpected, r)
50 if context.VMVersion != 1 {
51 return gasLimit, ErrUnsupportedVM
54 vm := &virtualMachine{
55 expansionReserved: context.TxVersion != nil && *context.TxVersion == 1,
56 program: context.Code,
61 args := context.Arguments
62 for i, arg := range args {
63 err = vm.push(arg, false)
65 return vm.runLimit, errors.Wrapf(err, "pushing initial argument %d", i)
70 if err == nil && vm.falseResult() {
71 err = ErrFalseVMResult
74 return vm.runLimit, wrapErr(err, vm, args)
77 // falseResult returns true iff the stack is empty or the top
79 func (vm *virtualMachine) falseResult() bool {
80 return len(vm.dataStack) == 0 || !AsBool(vm.dataStack[len(vm.dataStack)-1])
83 func (vm *virtualMachine) run() error {
84 for vm.pc = 0; vm.pc < uint32(len(vm.program)); { // handle vm.pc updates in step
93 func (vm *virtualMachine) step() error {
94 inst, err := ParseOp(vm.program, vm.pc)
99 vm.nextPC = vm.pc + inst.Len
102 opname := inst.Op.String()
103 fmt.Fprintf(TraceOut, "vm %d pc %d limit %d %s", vm.depth, vm.pc, vm.runLimit, opname)
104 if len(inst.Data) > 0 {
105 fmt.Fprintf(TraceOut, " %x", inst.Data)
107 fmt.Fprint(TraceOut, "\n")
110 if isExpansion[inst.Op] {
111 if vm.expansionReserved {
112 return ErrDisallowedOpcode
115 return vm.applyCost(1)
120 err = ops[inst.Op].fn(vm)
124 err = vm.applyCost(vm.deferredCost)
131 for i := len(vm.dataStack) - 1; i >= 0; i-- {
132 fmt.Fprintf(TraceOut, " stack %d: %x\n", len(vm.dataStack)-1-i, vm.dataStack[i])
138 func (vm *virtualMachine) push(data []byte, deferred bool) error {
139 cost := 8 + int64(len(data))
143 err := vm.applyCost(cost)
148 vm.dataStack = append(vm.dataStack, data)
152 func (vm *virtualMachine) pushBool(b bool, deferred bool) error {
153 return vm.push(BoolBytes(b), deferred)
156 func (vm *virtualMachine) pushInt64(n int64, deferred bool) error {
157 return vm.push(Int64Bytes(n), deferred)
160 func (vm *virtualMachine) pop(deferred bool) ([]byte, error) {
161 if len(vm.dataStack) == 0 {
162 return nil, ErrDataStackUnderflow
164 res := vm.dataStack[len(vm.dataStack)-1]
165 vm.dataStack = vm.dataStack[:len(vm.dataStack)-1]
167 cost := 8 + int64(len(res))
177 func (vm *virtualMachine) popInt64(deferred bool) (int64, error) {
178 bytes, err := vm.pop(deferred)
182 n, err := AsInt64(bytes)
186 func (vm *virtualMachine) top() ([]byte, error) {
187 if len(vm.dataStack) == 0 {
188 return nil, ErrDataStackUnderflow
190 return vm.dataStack[len(vm.dataStack)-1], nil
193 // positive cost decreases runlimit, negative cost increases it
194 func (vm *virtualMachine) applyCost(n int64) error {
197 return ErrRunLimitExceeded
203 func (vm *virtualMachine) deferCost(n int64) {
207 func stackCost(stack [][]byte) int64 {
208 result := int64(8 * len(stack))
209 for _, item := range stack {
210 result += int64(len(item))
215 func wrapErr(err error, vm *virtualMachine, args [][]byte) error {
220 dis, errDis := Disassemble(vm.program)
225 dataArgs := make([]string, 0, len(args))
226 for _, a := range args {
227 dataArgs = append(dataArgs, hex.EncodeToString(a))
230 return errors.Wrap(err, fmt.Sprintf("%s [prog %x = %s; args %s]", err.Error(), vm.program, dis, strings.Join(dataArgs, " ")))