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

A test that triggers null pointer exception on 3rd level of external query #212

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 9 additions & 0 deletions x/wasm/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package keeper
import (
"bytes"
"encoding/binary"
"fmt"
"path/filepath"

xdebug "runtime/debug"

"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/pkg/errors"

Expand Down Expand Up @@ -336,7 +339,13 @@ func (k Keeper) QuerySmart(ctx sdk.Context, contractAddr sdk.AccAddress, req []b
if err != nil {
return nil, err
}

// prepare querier
if nil == k.queryPlugins.Wasm {
fmt.Println("k.queryPlugins.Wasm is nil: \n" + string(xdebug.Stack()))
}

fmt.Printf("keeper.go/QuerySmart/k.queryPlugins: %v\n", k.queryPlugins)
querier := QueryHandler{
Ctx: ctx,
Plugins: k.queryPlugins,
Expand Down
28 changes: 28 additions & 0 deletions x/wasm/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"
Expand Down Expand Up @@ -869,3 +870,30 @@ func keyPubAddr() (crypto.PrivKey, crypto.PubKey, sdk.AccAddress) {
addr := sdk.AccAddress(pub.Address())
return key, pub, addr
}

func TestOOMKillsInfiniteQueryLoopEithan(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
accKeeper, keeper := keepers.AccountKeeper, keepers.WasmKeeper

deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
walletA := createFakeFundedAccount(ctx, accKeeper, deposit.Add(deposit...))

wasmCode, err := ioutil.ReadFile("./testdata/test-contract/contract.wasm.gz")
require.NoError(t, err)

codeID, err := keeper.Create(ctx, walletA, wasmCode, "", "")
require.NoError(t, err)
defer os.RemoveAll(tempDir)

// init

contractAddress, _ := keeper.Instantiate(ctx, codeID, walletA, nil, []byte(`{"nop":{}}`), "some label", sdk.NewCoins(sdk.NewInt64Coin("denom", 0)))

// infinite external query loop

_, err = keeper.QuerySmart(ctx, contractAddress, []byte(fmt.Sprintf(`{"send_external_query_infinite_loop":{"to":"%s"}}`, contractAddress.String())))

require.Error(t, err)
}
12 changes: 12 additions & 0 deletions x/wasm/internal/keeper/testdata/test-contract/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Build results
/target

# Text file backups
**/*.rs.bk

# macOS
.DS_Store

# IDEs
*.iml
.idea
245 changes: 245 additions & 0 deletions x/wasm/internal/keeper/testdata/test-contract/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions x/wasm/internal/keeper/testdata/test-contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
name = "test-contract"
version = "0.0.1"
authors = ["Enigma <info@enigma.co>"]
edition = "2018"
description = "A Test contract intended to use in system tests for the Secret Netowrk"
license = "MIT"
exclude = [
# Those files are cosmwasm-opt artifacts. You might want to commit them for convenience but they should not be part of the source code publication.
"contract.wasm",
"hash.txt",
]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib", "rlib"]

[profile.release]
opt-level = 3
debug = false
rpath = false
lto = true
debug-assertions = false
codegen-units = 1
panic = 'abort'
incremental = false
overflow-checks = true

[features]
default = ["backtraces"]
backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
cosmwasm-std = { git = "https://github.com/CosmWasm/cosmwasm", tag = "v0.9.1" }
schemars = "0.7"
serde = { version = "1.0.114", default-features = false, features = [
"derive",
"alloc"
] }
# serde-json-wasm = "0.2.1"
8 changes: 8 additions & 0 deletions x/wasm/internal/keeper/testdata/test-contract/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
all:
RUSTFLAGS='-C link-arg=-s' cargo build --release --target wasm32-unknown-unknown --locked
wasm-opt -Os ./target/wasm32-unknown-unknown/release/test_contract.wasm -o ./contract.wasm
cat contract.wasm | gzip -9 > ./contract.wasm.gz

clean:
cargo clean
-rm -f ./contract.wasm ./contract.wasm.gz
1 change: 1 addition & 0 deletions x/wasm/internal/keeper/testdata/test-contract/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Just `make`. :rainbow:
Binary file not shown.
Binary file not shown.
Loading