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

fix opcode xtuck panic #932

Merged
merged 2 commits into from Jun 3, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion vm/neovm/func_flowcontrol.go
Expand Up @@ -78,7 +78,7 @@ func opDCALL(e *ExecutionEngine) (VMState, error) {
return FAULT, errors.ERR_DCALL_OFFSET_ERROR
}

if dest.Sign() < 0 || dest.Cmp(big.NewInt(int64(len(e.Context.Code)))) > 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why it need to add equal zero?

if dest.Sign() < 0 || dest.Cmp(big.NewInt(int64(len(e.Context.Code)))) >= 0 {
return FAULT, errors.ERR_DCALL_OFFSET_ERROR
}

Expand Down
6 changes: 3 additions & 3 deletions vm/neovm/func_stack_test.go
Expand Up @@ -141,10 +141,10 @@ func TestOpXTuck(t *testing.T) {
stack.Push(types.NewInteger(big.NewInt(8888)))
stack.Push(types.NewInteger(big.NewInt(7777)))

stack.Push(NewStackItem(types.NewInteger(big.NewInt(2))))
stack.Push(NewStackItem(types.NewInteger(big.NewInt(0))))
e.EvaluationStack = stack

opXSwap(&e)
opXTuck(&e)
v1, err := stack.Peek(0).GetBigInteger()
if err != nil {
t.Fatal("NeoVM OpXTuck test failed.")
Expand All @@ -156,7 +156,7 @@ func TestOpXTuck(t *testing.T) {
e1 := v1.Int64()
e2 := v2.Int64()

if stack.Count() != 3 || e1 != 9999 || e2 != 7777 {
if stack.Count() != 4 || e1 != 7777 || e2 != 8888 {
t.Fatal("NeoVM OpXTuck test failed.")
}
}
Expand Down
15 changes: 14 additions & 1 deletion vm/neovm/func_validate.go
Expand Up @@ -157,7 +157,20 @@ func validateXSwap(e *ExecutionEngine) error {
}

func validateXTuck(e *ExecutionEngine) error {
return validateOpStack(e, "[validateXTuck]")
total := EvaluationStackCount(e)
if total < 1 {
return errors.ERR_UNDER_STACK_LEN
}
index, err := PeekBigInteger(e)
if err != nil {
return err
}
count := big.NewInt(0)
if index.Sign() < 0 || count.Add(index, big.NewInt(1)).Cmp(big.NewInt(int64(total))) > 0 {
return errors.ERR_BAD_VALUE
}

return nil
}

func validatePick(e *ExecutionEngine) error {
Expand Down
4 changes: 2 additions & 2 deletions vm/neovm/stack.go
Expand Up @@ -47,8 +47,8 @@ func (r *RandomAccessStack) Insert(index int, t types.StackItems) {
return
}
index = l - index
r.e = append(r.e, r.e[l-1])
copy(r.e[index+1:l], r.e[index:])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why it need to be updated?

r.e = append(r.e, t)
copy(r.e[index+1:], r.e[index:])
r.e[index] = t
}

Expand Down