forked from TrueCloudLab/neoneo-go
vm: restrict SHL/SHR arguments to -256..256
Also unify SHL/SHR implementation.
This commit is contained in:
parent
4a8be486f0
commit
3c53beca82
2 changed files with 32 additions and 11 deletions
25
pkg/vm/vm.go
25
pkg/vm/vm.go
|
@ -22,6 +22,11 @@ var (
|
||||||
ModeMute Mode = 1 << 0
|
ModeMute Mode = 1 << 0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxSHLArg = 256
|
||||||
|
minSHLArg = -256
|
||||||
|
)
|
||||||
|
|
||||||
// VM represents the virtual machine.
|
// VM represents the virtual machine.
|
||||||
type VM struct {
|
type VM struct {
|
||||||
state State
|
state State
|
||||||
|
@ -508,21 +513,19 @@ func (v *VM) execute(ctx *Context, op Instruction) {
|
||||||
a := v.estack.Pop().BigInt()
|
a := v.estack.Pop().BigInt()
|
||||||
v.estack.PushVal(new(big.Int).Mod(a, b))
|
v.estack.PushVal(new(big.Int).Mod(a, b))
|
||||||
|
|
||||||
case SHL:
|
case SHL, SHR:
|
||||||
b := v.estack.Pop().BigInt()
|
b := v.estack.Pop().BigInt().Int64()
|
||||||
if b.Int64() == 0 {
|
if b == 0 {
|
||||||
return
|
return
|
||||||
|
} else if b < minSHLArg || b > maxSHLArg {
|
||||||
|
panic(fmt.Sprintf("operand must be between %d and %d", minSHLArg, maxSHLArg))
|
||||||
}
|
}
|
||||||
a := v.estack.Pop().BigInt()
|
a := v.estack.Pop().BigInt()
|
||||||
v.estack.PushVal(new(big.Int).Lsh(a, uint(b.Int64())))
|
if op == SHL {
|
||||||
|
v.estack.PushVal(new(big.Int).Lsh(a, uint(b)))
|
||||||
case SHR:
|
} else {
|
||||||
b := v.estack.Pop().BigInt()
|
v.estack.PushVal(new(big.Int).Rsh(a, uint(b)))
|
||||||
if b.Int64() == 0 {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
a := v.estack.Pop().BigInt()
|
|
||||||
v.estack.PushVal(new(big.Int).Rsh(a, uint(b.Int64())))
|
|
||||||
|
|
||||||
case BOOLAND:
|
case BOOLAND:
|
||||||
b := v.estack.Pop().Bool()
|
b := v.estack.Pop().Bool()
|
||||||
|
|
|
@ -305,6 +305,15 @@ func TestSHRZero(t *testing.T) {
|
||||||
assert.Equal(t, makeStackItem([]byte{0, 1}), vm.estack.Pop().value)
|
assert.Equal(t, makeStackItem([]byte{0, 1}), vm.estack.Pop().value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSHRSmallValue(t *testing.T) {
|
||||||
|
prog := makeProgram(SHR)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(5)
|
||||||
|
vm.estack.PushVal(-257)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
func TestSHLGood(t *testing.T) {
|
func TestSHLGood(t *testing.T) {
|
||||||
prog := makeProgram(SHL)
|
prog := makeProgram(SHL)
|
||||||
vm := load(prog)
|
vm := load(prog)
|
||||||
|
@ -327,6 +336,15 @@ func TestSHLZero(t *testing.T) {
|
||||||
assert.Equal(t, makeStackItem([]byte{0, 1}), vm.estack.Pop().value)
|
assert.Equal(t, makeStackItem([]byte{0, 1}), vm.estack.Pop().value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSHLBigValue(t *testing.T) {
|
||||||
|
prog := makeProgram(SHL)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal(5)
|
||||||
|
vm.estack.PushVal(257)
|
||||||
|
vm.Run()
|
||||||
|
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
||||||
|
}
|
||||||
|
|
||||||
func TestLT(t *testing.T) {
|
func TestLT(t *testing.T) {
|
||||||
prog := makeProgram(LT)
|
prog := makeProgram(LT)
|
||||||
vm := load(prog)
|
vm := load(prog)
|
||||||
|
|
Loading…
Reference in a new issue