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

[Bugfix] fix to use single wasmvm during recursive external query #588

Merged
merged 1 commit into from
Oct 22, 2021
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [v0.5.9]

### Bug Fixes
- [\#588](https://github.com/terra-money/core/pull/588) - fix wasm external querier to use single wasmvm with context value

## [v0.5.8]

### Bug Fixes
Expand Down
10 changes: 6 additions & 4 deletions x/wasm/keeper/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func (k Keeper) queryToStore(ctx sdk.Context, contractAddress sdk.AccAddress, ke
return prefixStore.Get(key)
}

func (k Keeper) queryToContract(ctx sdk.Context, contractAddress sdk.AccAddress, queryMsg []byte, wasmVMs ...types.WasmerEngine) ([]byte, error) {
func (k Keeper) queryToContract(ctx sdk.Context, contractAddress sdk.AccAddress, queryMsg []byte) ([]byte, error) {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "query-smart")
ctx.GasMeter().ConsumeGas(types.InstantiateContractCosts(len(queryMsg)), "Loading CosmWasm module: query")

Expand All @@ -432,9 +432,11 @@ func (k Keeper) queryToContract(ctx sdk.Context, contractAddress sdk.AccAddress,
env := types.NewEnv(ctx, contractAddress)

// when the vm is given, use that given vm
wasmVM := k.wasmVM
if len(wasmVMs) != 0 {
wasmVM = wasmVMs[0]
var wasmVM types.WasmerEngine
if vm := ctx.Context().Value(types.QueryWasmVMContextKey); vm != nil {
wasmVM = vm.(types.WasmerEngine)
} else {
wasmVM = k.wasmVM
}

// assert max depth to prevent stack overflow
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Keeper struct {
queryRouter types.GRPCQueryRouter

wasmVM types.WasmerEngine
wasmReadVMPool []types.WasmerEngine
wasmReadVMPool *[]types.WasmerEngine
wasmReadVMSemaphore *semaphore.Weighted
wasmReadVMMutex *sync.Mutex

Expand Down Expand Up @@ -114,7 +114,7 @@ func NewKeeper(
cdc: cdc,
paramSpace: paramspace,
wasmVM: writeWasmVM,
wasmReadVMPool: wasmReadVMPool,
wasmReadVMPool: &wasmReadVMPool,
wasmReadVMSemaphore: semaphore.NewWeighted(int64(numReadVms)),
wasmReadVMMutex: &sync.Mutex{},
accountKeeper: accountKeeper,
Expand Down
5 changes: 4 additions & 1 deletion x/wasm/keeper/legacy_querier.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"context"
"fmt"
"runtime/debug"

Expand Down Expand Up @@ -147,7 +148,9 @@ func queryContractStore(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacy
}
}()

bz, err = k.queryToContract(ctx, params.ContractAddress, params.Msg, wasmVM)
// store query wasmvm in the context
ctx = ctx.WithContext(context.WithValue(ctx.Context(), types.QueryWasmVMContextKey, wasmVM))
bz, err = k.queryToContract(ctx, params.ContractAddress, params.Msg)

return
}
Expand Down
8 changes: 3 additions & 5 deletions x/wasm/keeper/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,23 @@ import (
"github.com/terra-money/core/x/wasm/types"
)

var n = 0

func (k Keeper) acquireWasmVM(ctx context.Context) (types.WasmerEngine, error) {
err := k.wasmReadVMSemaphore.Acquire(ctx, 1)
if err != nil {
return nil, err
}

k.wasmReadVMMutex.Lock()
wasmVM := k.wasmReadVMPool[0]
k.wasmReadVMPool = k.wasmReadVMPool[1:]
wasmVM := (*k.wasmReadVMPool)[0]
*k.wasmReadVMPool = (*k.wasmReadVMPool)[1:]
k.wasmReadVMMutex.Unlock()

return wasmVM, nil
}

func (k Keeper) releaseWasmVM(wasmVM types.WasmerEngine) {
k.wasmReadVMMutex.Lock()
k.wasmReadVMPool = append(k.wasmReadVMPool, wasmVM)
*k.wasmReadVMPool = append(*k.wasmReadVMPool, wasmVM)
k.wasmReadVMMutex.Unlock()

k.wasmReadVMSemaphore.Release(1)
Expand Down
5 changes: 3 additions & 2 deletions x/wasm/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ func (q querier) ContractStore(c context.Context, req *types.QueryContractStoreR
}
}()

var bz []byte
bz, err = q.queryToContract(ctx, contractAddr, req.QueryMsg, wasmVM)
// store query wasmvm in the context
ctx = ctx.WithContext(context.WithValue(ctx.Context(), types.QueryWasmVMContextKey, wasmVM))
bz, err := q.queryToContract(ctx, contractAddr, req.QueryMsg)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand Down
20 changes: 14 additions & 6 deletions x/wasm/keeper/recursive_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"context"
"encoding/json"
"io/ioutil"
"testing"
Expand Down Expand Up @@ -344,18 +345,25 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
}

func TestLimitRecursiveQueryDepth(t *testing.T) {

contractAddr, _, ctx, keeper, _ := initRecurseContract(t)
// pick query wasmvm
wasmvm, err := keeper.acquireWasmVM(ctx.Context())
require.NoError(t, err)
defer keeper.releaseWasmVM(wasmvm)

ctx = ctx.WithContext(context.WithValue(ctx.Context(), types.QueryWasmVMContextKey, wasmvm))

// exceed max query depth
msg := buildQuery(t, Recurse{
Depth: types.ContractMaxQueryDepth - 1, // need to include first query
Depth: types.ContractMaxQueryDepth,
})

_, err := keeper.queryToContract(ctx, contractAddr, msg)
require.NoError(t, err)
_, err = keeper.queryToContract(ctx, contractAddr, msg)
require.Error(t, err)

msg = buildQuery(t, Recurse{
Depth: types.ContractMaxQueryDepth,
Depth: types.ContractMaxQueryDepth - 1, // need to include first query
})
_, err = keeper.queryToContract(ctx, contractAddr, msg)
require.Error(t, err)
require.NoError(t, err)
}
3 changes: 3 additions & 0 deletions x/wasm/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const (

// RouterKey is the msg router key for the wasm module
RouterKey = ModuleName

// QueryWasmVMContextKey is the context key to store wasmvm in query context
QueryWasmVMContextKey = "wasmvm"
)

// Keys for wasm store
Expand Down