Skip to content

Commit

Permalink
Fix native build params circular reference (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanZiWen authored and laizy committed Aug 31, 2018
1 parent f9f038d commit 229c10c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
14 changes: 11 additions & 3 deletions smartcontract/service/neovm/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ package neovm

import (
"bytes"
"errors"
"fmt"
"math/big"
"reflect"

"github.com/ontio/ontology/common"
Expand All @@ -29,7 +31,6 @@ import (
"github.com/ontio/ontology/smartcontract/states"
vm "github.com/ontio/ontology/vm/neovm"
"github.com/ontio/ontology/vm/neovm/types"
"math/big"
)

func NativeInvoke(service *NeoVmService, engine *vm.ExecutionEngine) error {
Expand Down Expand Up @@ -92,6 +93,13 @@ func NativeInvoke(service *NeoVmService, engine *vm.ExecutionEngine) error {
}

func BuildParamToNative(bf *bytes.Buffer, item types.StackItems) error {
if CircularRefAndDepthDetection(item) {
return errors.New("invoke native circular reference!")
}
return buildParamToNative(bf, item)
}

func buildParamToNative(bf *bytes.Buffer, item types.StackItems) error {
switch item.(type) {
case *types.ByteArray:
a, _ := item.GetByteArray()
Expand All @@ -114,14 +122,14 @@ func BuildParamToNative(bf *bytes.Buffer, item types.StackItems) error {
return err
}
for _, v := range arr {
if err := BuildParamToNative(bf, v); err != nil {
if err := buildParamToNative(bf, v); err != nil {
return err
}
}
case *types.Struct:
st, _ := item.GetStruct()
for _, v := range st {
if err := BuildParamToNative(bf, v); err != nil {
if err := buildParamToNative(bf, v); err != nil {
return err
}
}
Expand Down
52 changes: 52 additions & 0 deletions smartcontract/test/native_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/

package test

import (
"github.com/ontio/ontology/common"
"github.com/ontio/ontology/smartcontract"
"github.com/stretchr/testify/assert"
"testing"
)

func TestBuildParamToNative(t *testing.T) {
code := `00c57676c84c0500000000004c1400000000000000000000000000000000000000060068164f6e746f6c6f67792e4e61746976652e496e766f6b65`

hex, err := common.HexToBytes(code)

if err != nil {
t.Fatal("hex to byte error:", err)
}

config := &smartcontract.Config{
Time: 10,
Height: 10,
Tx: nil,
}
//cache := storage.NewCloneCache(testBatch)
sc := smartcontract.SmartContract{
Config: config,
Gas: 100000,
}
engine, err := sc.NewExecuteEngine(hex)

_, err = engine.Invoke()

assert.Error(t, err, "invoke smart contract err: [NeoVmService] service system call error!: [SystemCall] service execute error!: invoke native circular reference!")
}

0 comments on commit 229c10c

Please sign in to comment.