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

test(fuzz): fix broken OSS-Fuz build #21940

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
81 changes: 81 additions & 0 deletions fuzz/fuzz.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
diff --git a/types/address_test.go b/types/address_test.go
index 014a48b73..99f9a1a86 100644
--- a/types/address_test.go
+++ b/types/address_test.go
@@ -26,10 +26,6 @@ type addressTestSuite struct {
suite.Suite
}

-func TestAddressTestSuite(t *testing.T) {
- suite.Run(t, new(addressTestSuite))
-}
Comment on lines +9 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Potential Test Coverage Reduction Due to Removal of Test Suite Runner

The TestAddressTestSuite function has been removed, and no alternative test suite runners were found for address-related tests. While individual tests like TestBech32ifyAddressBytes still exist in types/address_test.go, their execution may be compromised without a suite runner, potentially reducing overall test coverage.

  • Ensure that individual tests are invoked through other test runners or frameworks.
  • Consider reintroducing a suite runner or alternative mechanism to execute these tests.
  • Add comments to clarify the rationale behind removing the suite runner and how test coverage is maintained.
🔗 Analysis chain

Verify the impact of removing TestAddressTestSuite

The removal of TestAddressTestSuite function, which was responsible for running the entire address test suite, could significantly impact the test coverage. While this might be necessary to fix the OSS-Fuzz build as per the PR objectives, we need to ensure that critical tests are still being run elsewhere.

Let's verify if there are other test suite runners or if these tests are covered elsewhere:

Consider adding a comment explaining why these tests were removed and how the functionality is being tested now, if applicable. This will help maintain clarity for future developers working on this code.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other test suite runners and coverage of address tests

# Search for other potential test suite runners
echo "Searching for other test suite runners:"
rg --type go 'func Test.*Suite\(' -g '!*_test.go'

# Check for other files that might be testing address functionality
echo "Checking for other address test files:"
fd -e go | rg 'address.*test'

# Look for any TODO or FIXME comments related to testing
echo "Searching for TODO/FIXME comments related to testing:"
rg -i 'todo|fixme' -g '*test*.go'

Length of output: 7093

-
func (s *addressTestSuite) SetupSuite() {
s.T().Parallel()
}
@@ -403,65 +399,6 @@ func (s *addressTestSuite) TestAddressInterface() {
}
}

-func (s *addressTestSuite) TestBech32ifyAddressBytes() {
- type args struct {
- prefix string
- bs []byte
- }
- tests := []struct {
- name string
- args args
- want string
- wantErr bool
- }{
- {"empty address", args{"prefixa", []byte{}}, "", false},
- {"empty prefix", args{"", addr20byte}, "", true},
- {"10-byte address", args{"prefixa", addr10byte}, "prefixa1qqqsyqcyq5rqwzqf3953cc", false},
- {"10-byte address", args{"prefixb", addr10byte}, "prefixb1qqqsyqcyq5rqwzqf20xxpc", false},
- {"20-byte address", args{"prefixa", addr20byte}, "prefixa1qqqsyqcyq5rqwzqfpg9scrgwpugpzysn7hzdtn", false},
- {"20-byte address", args{"prefixb", addr20byte}, "prefixb1qqqsyqcyq5rqwzqfpg9scrgwpugpzysnrujsuw", false},
- }
- for _, tt := range tests {
- s.T().Run(tt.name, func(t *testing.T) {
- got, err := types.Bech32ifyAddressBytes(tt.args.prefix, tt.args.bs)
- if (err != nil) != tt.wantErr {
- t.Errorf("Bech32ifyBytes() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- require.Equal(t, tt.want, got)
- })
- }
-}
-
-func (s *addressTestSuite) TestMustBech32ifyAddressBytes() {
- type args struct {
- prefix string
- bs []byte
- }
- tests := []struct {
- name string
- args args
- want string
- wantPanic bool
- }{
- {"empty address", args{"prefixa", []byte{}}, "", false},
- {"empty prefix", args{"", addr20byte}, "", true},
- {"10-byte address", args{"prefixa", addr10byte}, "prefixa1qqqsyqcyq5rqwzqf3953cc", false},
- {"10-byte address", args{"prefixb", addr10byte}, "prefixb1qqqsyqcyq5rqwzqf20xxpc", false},
- {"20-byte address", args{"prefixa", addr20byte}, "prefixa1qqqsyqcyq5rqwzqfpg9scrgwpugpzysn7hzdtn", false},
- {"20-byte address", args{"prefixb", addr20byte}, "prefixb1qqqsyqcyq5rqwzqfpg9scrgwpugpzysnrujsuw", false},
- }
- for _, tt := range tests {
- s.T().Run(tt.name, func(t *testing.T) {
- if tt.wantPanic {
- require.Panics(t, func() { types.MustBech32ifyAddressBytes(tt.args.prefix, tt.args.bs) })
- return
- }
- require.Equal(t, tt.want, types.MustBech32ifyAddressBytes(tt.args.prefix, tt.args.bs))
- })
- }
-}
Comment on lines +50 to +77
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Additional tests for MustBech32ifyAddressBytes are needed

The removal of TestMustBech32ifyAddressBytes reduces the test coverage for the MustBech32ifyAddressBytes function, particularly its panic conditions. While there is usage in client/keys/show_test.go, it may not comprehensively cover all test cases previously handled.

  • Ensure that all critical scenarios, including different prefixes, byte lengths, and panic conditions, are adequately tested.
  • If necessary, reintroduce or create new test functions to maintain full coverage of MustBech32ifyAddressBytes.
🔗 Analysis chain

Verify coverage for MustBech32ifyAddressBytes functionality

The removal of TestMustBech32ifyAddressBytes function eliminates tests for the MustBech32ifyAddressBytes functionality, including its panic behavior. This could potentially leave this critical function untested, risking unexpected runtime errors if the function's panic conditions are not properly maintained.

Let's verify if this functionality is tested elsewhere:

If no other tests are found, consider adding a minimal test for MustBech32ifyAddressBytes that doesn't interfere with the OSS-Fuzz build, especially focusing on its panic behavior. Alternatively, document why this test was removed and how the functionality, particularly the panic conditions, is ensured to work correctly without these specific tests.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other tests covering MustBech32ifyAddressBytes functionality

# Search for other tests of MustBech32ifyAddressBytes
echo "Searching for other tests of MustBech32ifyAddressBytes:"
rg --type go 'func Test.*MustBech32ifyAddressBytes'

# Look for usage of MustBech32ifyAddressBytes in the codebase
echo "Checking usage of MustBech32ifyAddressBytes:"
rg --type go 'MustBech32ifyAddressBytes'

Length of output: 943

-
func (s *addressTestSuite) TestAddressTypesEquals() {
addr1 := secp256k1.GenPrivKey().PubKey().Address()
accAddr1 := types.AccAddress(addr1)
40 changes: 33 additions & 7 deletions fuzz/oss-fuzz-build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
#!/bin/bash

set -o nounset
set -o pipefail
set -o errexit
set -x
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Remove set -x to prevent verbose logging in production

The set -x command enables shell debugging by printing each command before execution. This can clutter logs and potentially expose sensitive information. It's recommended to remove set -x or conditionally enable it only during debugging sessions.


cd $SRC
wget https://go.dev/dl/go1.23.1.linux-amd64.tar.gz
mkdir $SRC/new-go
rm -rf /root/.go && tar -C $SRC/new-go/ -xzf go1.23.1.linux-amd64.tar.gz
mv $SRC/new-go/go /root/.go
Comment on lines +11 to +12
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Avoid using rm -rf /root/.go; consider safer installation methods

Using rm -rf /root/.go is risky and can lead to accidental deletion if misused. Additionally, modifying files in /root requires elevated privileges and may affect other processes. Consider installing Go in a local directory within your workspace or using the existing Go installation available in the environment.

ls /root/.go

cd $SRC/go-118-fuzz-build
go build .
mv go-118-fuzz-build /root/go/bin/
cd $SRC/cosmos-sdk
git apply ./fuzz/fuzz.patch

mkdir $SRC/cosmos-sdk/types/fuzzing
mv $SRC/cosmos-sdk/types/address*_test.go $SRC/cosmos-sdk/types/fuzzing/
sed 's/package types_test/package fuzzing/g' -i "$SRC"/cosmos-sdk/types/fuzzing/*

rm $SRC/cosmos-sdk/math/dec_internal_test.go
rm $SRC/cosmos-sdk/math/int_internal_test.go
rm $SRC/cosmos-sdk/math/uint_internal_test.go
mv $SRC/cosmos-sdk/types/fuzz_test.go $SRC/cosmos-sdk/types/fuzz.go
rm $SRC/cosmos-sdk/types/*_test.go
mv $SRC/cosmos-sdk/types/fuzz.go $SRC/cosmos-sdk/types/fuzz_test.go
Comment on lines +25 to +30
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Assess the impact of removing test files on code coverage

Removing test files like dec_internal_test.go, int_internal_test.go, uint_internal_test.go, and all *_test.go files in the types directory could significantly reduce test coverage. This might lead to undetected bugs and decreased code reliability. Please evaluate the necessity of deleting these tests and consider retaining critical ones.


set -euo pipefail

export FUZZ_ROOT="github.com/cosmos/cosmos-sdk"
Expand All @@ -18,25 +47,22 @@ build_go_fuzzer() {
compile_native_go_fuzzer cosmossdk.io/math FuzzLegacyNewDecFromStr fuzz_math_legacy_new_dec_from_str
)

go get github.com/AdamKorcz/go-118-fuzz-build/testing
printf "package types \nimport _ \"github.com/AdamKorcz/go-118-fuzz-build/testing\"\n" > ./types/fuzz-register.go
go mod edit -replace github.com/AdamKorcz/go-118-fuzz-build=$SRC/go-118-fuzz-build
go mod tidy

# TODO: fails to build with
# main.413864645.go:12:2: found packages query (collections_pagination.go) and query_test (fuzz_test.go_fuzz.go) in /src/cosmos-sdk/types/query
# because of the separate query_test package.
# compile_native_go_fuzzer "$FUZZ_ROOT"/types/query FuzzPagination fuzz_types_query_pagination
compile_native_go_fuzzer "$FUZZ_ROOT"/types FuzzCoinUnmarshalJSON fuzz_types_coin_unmarshal_json
compile_native_go_fuzzer "$FUZZ_ROOT"/types FuzzBech32AccAddrConsistencyYAML fuzz_types_bech32_acc_addr_consistency_yaml

compile_native_go_fuzzer "$FUZZ_ROOT"/types/fuzzing FuzzBech32AccAddrConsistencyYAML fuzz_types_bech32_acc_addr_consistency_yaml
build_go_fuzzer FuzzCryptoHDDerivePrivateKeyForPath fuzz_crypto_hd_deriveprivatekeyforpath
build_go_fuzzer FuzzCryptoHDNewParamsFromPath fuzz_crypto_hd_newparamsfrompath

build_go_fuzzer FuzzCryptoTypesCompactbitarrayMarshalUnmarshal fuzz_crypto_types_compactbitarray_marshalunmarshal

build_go_fuzzer FuzzTendermintAminoDecodeTime fuzz_tendermint_amino_decodetime

build_go_fuzzer FuzzTypesParseCoin fuzz_types_parsecoin
build_go_fuzzer FuzzTypesParseDecCoin fuzz_types_parsedeccoin
build_go_fuzzer FuzzTypesParseTimeBytes fuzz_types_parsetimebytes
build_go_fuzzer FuzzTypesDecSetString fuzz_types_dec_setstring

build_go_fuzzer FuzzUnknownProto fuzz_unknownproto
Loading