diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index c63499bd1..b0f27b006 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -1535,12 +1535,7 @@ func (c *codegen) writeJumps(b []byte) error { if int(index) > len(c.l) { return fmt.Errorf("unexpected label number: %d (max %d)", index, len(c.l)) } - var offset int - if op == opcode.PUSHA { - offset = c.l[index] - } else { - offset = c.l[index] - nextIP + 5 - } + offset := c.l[index] - nextIP + 5 if offset > math.MaxInt32 || offset < math.MinInt32 { return fmt.Errorf("label offset is too big at the instruction %d: %d (max %d, min %d)", nextIP-5, offset, math.MaxInt32, math.MinInt32) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index bd5ab19fc..1d780ad89 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -192,16 +192,14 @@ func (v *VM) PrintOps() { opcode.JMPGT, opcode.JMPGE, opcode.JMPLE, opcode.JMPLT, opcode.JMPL, opcode.JMPIFL, opcode.JMPIFNOTL, opcode.CALLL, opcode.JMPEQL, opcode.JMPNEL, - opcode.JMPGTL, opcode.JMPGEL, opcode.JMPLEL, opcode.JMPLTL: + opcode.JMPGTL, opcode.JMPGEL, opcode.JMPLEL, opcode.JMPLTL, + opcode.PUSHA: offset, rOffset, err := v.calcJumpOffset(ctx, parameter) if err != nil { desc = fmt.Sprintf("ERROR: %v", err) } else { desc = fmt.Sprintf("%d (%d/%x)", offset, rOffset, parameter) } - case opcode.PUSHA: - offset := int32(binary.LittleEndian.Uint32(parameter)) - desc = fmt.Sprintf("%d (%x)", offset, parameter) case opcode.INITSSLOT: desc = fmt.Sprint(parameter[0]) case opcode.INITSLOT: @@ -535,11 +533,8 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro v.estack.PushVal(parameter) case opcode.PUSHA: - n := int32(binary.LittleEndian.Uint32(parameter)) - if n < 0 || int(n) > len(ctx.prog) { - panic(fmt.Sprintf("invalid pointer offset (%d)", n)) - } - ptr := stackitem.NewPointer(int(n), ctx.prog) + n := v.getJumpOffset(ctx, parameter) + ptr := stackitem.NewPointer(n, ctx.prog) v.estack.PushVal(ptr) case opcode.PUSHNULL: diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 359d26488..de76e513f 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -980,8 +980,8 @@ func TestPUSHA(t *testing.T) { t.Run("Negative", getTestFuncForVM(makeProgram(opcode.PUSHA, 0xFF, 0xFF, 0xFF, 0xFF), nil)) t.Run("TooBig", getTestFuncForVM(makeProgram(opcode.PUSHA, 10, 0, 0, 0), nil)) t.Run("Good", func(t *testing.T) { - prog := makeProgram(opcode.PUSHA, 2, 0, 0, 0) - runWithArgs(t, prog, stackitem.NewPointer(2, prog)) + prog := makeProgram(opcode.NOP, opcode.PUSHA, 2, 0, 0, 0) + runWithArgs(t, prog, stackitem.NewPointer(3, prog)) }) }