Skip to content

Commit

Permalink
[ctr/lua] limit contract max call depth to 5 and increase instruction…
Browse files Browse the repository at this point in the history
… count of contract call
  • Loading branch information
eve2adam committed Apr 22, 2019
1 parent edc3cf0 commit 261b871
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion contract/lstate_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var getCh chan *LState
var freeCh chan *LState
var once sync.Once

const MAX_LSTATE_SIZE = 100
const MAX_LSTATE_SIZE = 150

func StartLStateFactory() {
once.Do(func() {
Expand Down
14 changes: 14 additions & 0 deletions contract/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
callMaxInstLimit = C.int(5000000)
queryMaxInstLimit = callMaxInstLimit * C.int(10)
dbUpdateMaxLimit = fee.StateDbMaxUpdateSize
maxCallDepth = 5
)

var (
Expand Down Expand Up @@ -88,6 +89,7 @@ type StateSet struct {
seed *rand.Rand
events []*types.Event
eventCount int32
callDepth int32
}

type recoveryEntry struct {
Expand Down Expand Up @@ -198,6 +200,15 @@ func (L *LState) Close() {
}

func newExecutor(contract []byte, contractId []byte, stateSet *StateSet, ci *types.CallInfo, amount *big.Int, isCreate bool) *Executor {
if stateSet.callDepth > maxCallDepth {
ce := &Executor{
code: contract,
stateSet: stateSet,
}
ce.err = errors.New(fmt.Sprintf("exceeded the maximum call depth(%d)", maxCallDepth))
return ce
}
stateSet.callDepth++
ce := &Executor{
code: contract,
L: GetLState(),
Expand Down Expand Up @@ -503,6 +514,9 @@ func (ce *Executor) rollbackToSavepoint() error {

func (ce *Executor) close() {
if ce != nil {
if ce.stateSet != nil {
ce.stateSet.callDepth--
}
FreeLState(ce.L)
}
}
Expand Down
17 changes: 13 additions & 4 deletions contract/vm_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var mulAergo, mulGaer, zeroBig *big.Int
const maxEventCnt = 50
const maxEventNameSize = 64
const maxEventArgSize = 4096
const luaCallCountDeduc = 1000

func init() {
mulAergo, _ = new(big.Int).SetString("1000000000000000000", 10)
Expand Down Expand Up @@ -139,6 +140,14 @@ func setInstCount(parent *LState, child *LState) {
C.luaL_setinstcount(parent, C.luaL_instcount(child))
}

func minusCallCount(curCount C.int) C.int {
remain := curCount - luaCallCountDeduc
if remain <= 0 {
remain = 1
}
return remain
}

//export LuaCallContract
func LuaCallContract(L *LState, service *C.int, contractId *C.char, fname *C.char, args *C.char,
amount *C.char, gas uint64) (C.int, *C.char) {
Expand Down Expand Up @@ -205,7 +214,7 @@ func LuaCallContract(L *LState, service *C.int, contractId *C.char, fname *C.cha
stateSet.curContract = newContractInfo(callState, prevContractInfo.contractId, cid,
callState.curState.SqlRecoveryPoint, amountBig)

ce.setCountHook(C.luaL_instcount(L))
ce.setCountHook(minusCallCount(C.luaL_instcount(L)))
defer setInstCount(L, ce.L)

ret := ce.call(L)
Expand Down Expand Up @@ -273,7 +282,7 @@ func LuaDelegateCallContract(L *LState, service *C.int, contractId *C.char,
}
}

ce.setCountHook(C.luaL_instcount(L))
ce.setCountHook(minusCallCount(C.luaL_instcount(L)))
defer setInstCount(L, ce.L)

ret := ce.call(L)
Expand Down Expand Up @@ -355,7 +364,7 @@ func LuaSendAmount(L *LState, service *C.int, contractId *C.char, amount *C.char
stateSet.curContract = newContractInfo(callState, prevContractInfo.contractId, cid,
callState.curState.SqlRecoveryPoint, amountBig)

ce.setCountHook(C.luaL_instcount(L))
ce.setCountHook(minusCallCount(C.luaL_instcount(L)))
defer setInstCount(L, ce.L)

ce.call(L)
Expand Down Expand Up @@ -874,7 +883,7 @@ func LuaDeployContract(
addr := C.CString(types.EncodeAddress(newContract.ID()))
ret := C.int(1)
if ce != nil {
ce.setCountHook(C.luaL_instcount(L))
ce.setCountHook(minusCallCount(C.luaL_instcount(L)))
defer setInstCount(L, ce.L)

ret += ce.call(L)
Expand Down

0 comments on commit 261b871

Please sign in to comment.