Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vm: handle negative arguments in SHL/SHR #827

Merged
merged 1 commit into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pkg/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,8 +806,17 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
a := v.estack.Pop().BigInt()
v.checkBigIntSize(a)

newOp := op
if b < 0 {
b = -b
if op == opcode.SHR {
newOp = opcode.SHL
} else {
newOp = opcode.SHR
}
}
var item big.Int
if op == opcode.SHL {
if newOp == opcode.SHL {
item.Lsh(a, uint(b))
} else {
item.Rsh(a, uint(b))
Expand Down
15 changes: 14 additions & 1 deletion pkg/vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ func TestMULBigResult(t *testing.T) {
checkVMFailed(t, vm)
}

func TestDivMod(t *testing.T) {
func TestArithNegativeArguments(t *testing.T) {
runCase := func(op opcode.Opcode, p, q, result int64) func(t *testing.T) {
return func(t *testing.T) {
vm := load(makeProgram(op))
Expand All @@ -869,6 +869,19 @@ func TestDivMod(t *testing.T) {
t.Run("negative/negative", runCase(opcode.MOD, -5, -2, -1))
})

t.Run("SHR", func(t *testing.T) {
fyrchik marked this conversation as resolved.
Show resolved Hide resolved
t.Run("positive/positive", runCase(opcode.SHR, 5, 2, 1))
t.Run("positive/negative", runCase(opcode.SHR, 5, -2, 20))
t.Run("negative/positive", runCase(opcode.SHR, -5, 2, -2))
t.Run("negative/negative", runCase(opcode.SHR, -5, -2, -20))
})

t.Run("SHL", func(t *testing.T) {
t.Run("positive/positive", runCase(opcode.SHL, 5, 2, 20))
t.Run("positive/negative", runCase(opcode.SHL, 5, -2, 1))
t.Run("negative/positive", runCase(opcode.SHL, -5, 2, -20))
t.Run("negative/negative", runCase(opcode.SHL, -5, -2, -2))
})
}

func TestSub(t *testing.T) {
Expand Down