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])
139 func (vm *virtualMachine) push(data []byte, deferred bool) error {
140 cost := 8 + int64(len(data))
144 err := vm.applyCost(cost)
149 vm.dataStack = append(vm.dataStack, data)
153 func (vm *virtualMachine) pushBool(b bool, deferred bool) error {
154 return vm.push(BoolBytes(b), deferred)
157 func (vm *virtualMachine) pushInt64(n int64, deferred bool) error {
158 return vm.push(Int64Bytes(n), deferred)
161 func (vm *virtualMachine) pop(deferred bool) ([]byte, error) {
162 if len(vm.dataStack) == 0 {
163 return nil, ErrDataStackUnderflow
165 res := vm.dataStack[len(vm.dataStack)-1]
166 vm.dataStack = vm.dataStack[:len(vm.dataStack)-1]
168 cost := 8 + int64(len(res))
178 func (vm *virtualMachine) popInt64(deferred bool) (int64, error) {
179 bytes, err := vm.pop(deferred)
183 n, err := AsInt64(bytes)
187 func (vm *virtualMachine) top() ([]byte, error) {
188 if len(vm.dataStack) == 0 {
189 return nil, ErrDataStackUnderflow
191 return vm.dataStack[len(vm.dataStack)-1], nil
194 // positive cost decreases runlimit, negative cost increases it
195 func (vm *virtualMachine) applyCost(n int64) error {
198 return ErrRunLimitExceeded
204 func (vm *virtualMachine) deferCost(n int64) {
208 func stackCost(stack [][]byte) int64 {
209 result := int64(8 * len(stack))
210 for _, item := range stack {
211 result += int64(len(item))
216 func wrapErr(err error, vm *virtualMachine, args [][]byte) error {
221 dis, errDis := Disassemble(vm.program)
226 dataArgs := make([]string, 0, len(args))
227 for _, a := range args {
228 dataArgs = append(dataArgs, hex.EncodeToString(a))
231 return errors.Wrap(err, fmt.Sprintf("%s [prog %x = %s; args %s]", err.Error(), vm.program, dis, strings.Join(dataArgs, " ")))