-
Notifications
You must be signed in to change notification settings - Fork 164
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
fuzzing: add raft fuzzer from cncf-fuzzing #55
Open
AdamKorcz
wants to merge
8
commits into
etcd-io:main
Choose a base branch
from
AdamKorcz:fuzz1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
216c276
fuzzing: add raft fuzzer from cncf-fuzzing
AdamKorcz a9d227d
Add workflow for PRs
AdamKorcz 07e5518
catch known panic
AdamKorcz 048a4ce
use go-fuzz-headers with reduced dependencies
AdamKorcz af6cf7a
remove zap dependencies
AdamKorcz ffda7e3
remove check in recover()
AdamKorcz 78b72ce
use upstream go-fuzz-headers
AdamKorcz 763e6ea
Fix static lint issues
AdamKorcz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Fuzzing | ||
on: pull_request | ||
permissions: read-all | ||
jobs: | ||
fuzzing: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
env: | ||
TARGET_PATH: "." | ||
steps: | ||
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2 | ||
- uses: actions/setup-go@4d34df0c2316fe8122ab82dc22947d607c0c91f9 # v4.0.0 | ||
with: | ||
go-version: "1.19.9" | ||
- run: | | ||
set -euo pipefail | ||
|
||
GOARCH=amd64 CPU=4 make fuzz | ||
- uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2 | ||
if: failure() | ||
with: | ||
path: "${{env.TARGET_PATH}}/testdata/fuzz/**/*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// Copyright 2022 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package raft | ||
|
||
import ( | ||
"runtime" | ||
"strings" | ||
"testing" | ||
|
||
fuzz "github.com/AdaLogics/go-fuzz-headers" | ||
|
||
pb "go.etcd.io/raft/v3/raftpb" | ||
) | ||
|
||
func getMsgType(i int) pb.MessageType { | ||
allTypes := map[int]pb.MessageType{0: pb.MsgHup, | ||
1: pb.MsgBeat, | ||
2: pb.MsgProp, | ||
3: pb.MsgApp, | ||
4: pb.MsgAppResp, | ||
5: pb.MsgVote, | ||
6: pb.MsgVoteResp, | ||
7: pb.MsgSnap, | ||
8: pb.MsgHeartbeat, | ||
9: pb.MsgHeartbeatResp, | ||
10: pb.MsgUnreachable, | ||
11: pb.MsgSnapStatus, | ||
12: pb.MsgCheckQuorum, | ||
13: pb.MsgTransferLeader, | ||
14: pb.MsgTimeoutNow, | ||
15: pb.MsgReadIndex, | ||
16: pb.MsgReadIndexResp, | ||
17: pb.MsgPreVote, | ||
18: pb.MsgPreVoteResp} | ||
return allTypes[i%len(allTypes)] | ||
} | ||
|
||
// All cases in shouldReport represent known errors in etcd | ||
// as these are reported via manually added panics. | ||
func shouldReport(err string) bool { | ||
if strings.Contains(err, "stepped empty MsgProp") { | ||
return false | ||
} | ||
if strings.Contains(err, "Was the raft log corrupted, truncated, or lost?") { | ||
return false | ||
} | ||
if strings.Contains(err, "ConfStates not equivalent after sorting:") { | ||
return false | ||
} | ||
if strings.Contains(err, "term should be set when sending ") { | ||
return false | ||
} | ||
if (strings.Contains(err, "unable to restore config")) && (strings.Contains(err, "removed all voters")) { | ||
return false | ||
} | ||
if strings.Contains(err, "ENCOUNTERED A PANIC OR FATAL") { | ||
return false | ||
} | ||
if strings.Contains(err, "need non-empty snapshot") { | ||
return false | ||
} | ||
if strings.Contains(err, "index, ") && strings.Contains(err, ", is out of range [") { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func catchPanics() { | ||
if r := recover(); r != nil { | ||
var errMsg string | ||
switch r.(type) { | ||
case string: | ||
errMsg = r.(string) | ||
case runtime.Error: | ||
errMsg = r.(runtime.Error).Error() | ||
} | ||
if shouldReport(errMsg) { | ||
// Getting to this point means that the fuzzer | ||
// did not stop because of a manually added panic. | ||
panic(errMsg) | ||
} | ||
} | ||
} | ||
|
||
func FuzzStep(f *testing.F) { | ||
f.Fuzz(func(t *testing.T, data []byte) { | ||
defer SetLogger(getLogger()) | ||
SetLogger(discardLogger) | ||
|
||
defer catchPanics() | ||
f := fuzz.NewConsumer(data) | ||
msg := pb.Message{} | ||
err := f.GenerateStruct(&msg) | ||
if err != nil { | ||
return | ||
} | ||
|
||
msgTypeIndex, err := f.GetInt() | ||
if err != nil { | ||
return | ||
} | ||
msg.Type = getMsgType(msgTypeIndex) | ||
|
||
cfg := newTestConfig(1, 5, 1, newTestMemoryStorage(withPeers(1, 2))) | ||
cfg.Logger = &ZapRaftLogger{} | ||
r := newRaft(cfg) | ||
r.becomeCandidate() | ||
r.becomeLeader() | ||
r.prs.Progress[2].BecomeReplicate() | ||
_ = r.Step(msg) | ||
_ = r.readMessages() | ||
}) | ||
} | ||
|
||
type ZapRaftLogger struct { | ||
} | ||
|
||
func (zl *ZapRaftLogger) Debug(_ ...interface{}) {} | ||
|
||
func (zl *ZapRaftLogger) Debugf(_ string, _ ...interface{}) {} | ||
|
||
func (zl *ZapRaftLogger) Error(_ ...interface{}) {} | ||
|
||
func (zl *ZapRaftLogger) Errorf(_ string, _ ...interface{}) {} | ||
|
||
func (zl *ZapRaftLogger) Info(_ ...interface{}) {} | ||
|
||
func (zl *ZapRaftLogger) Infof(_ string, _ ...interface{}) {} | ||
|
||
func (zl *ZapRaftLogger) Warning(_ ...interface{}) {} | ||
|
||
func (zl *ZapRaftLogger) Warningf(_ string, _ ...interface{}) {} | ||
|
||
func (zl *ZapRaftLogger) Fatal(_ ...interface{}) { | ||
panic("ENCOUNTERED A PANIC OR FATAL") | ||
} | ||
|
||
func (zl *ZapRaftLogger) Fatalf(_ string, _ ...interface{}) { | ||
panic("ENCOUNTERED A PANIC OR FATAL") | ||
} | ||
|
||
func (zl *ZapRaftLogger) Panic(_ ...interface{}) { | ||
panic("ENCOUNTERED A PANIC OR FATAL") | ||
} | ||
|
||
func (zl *ZapRaftLogger) Panicf(_ string, _ ...interface{}) { | ||
panic("ENCOUNTERED A PANIC OR FATAL") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
source ./scripts/test_lib.sh | ||
|
||
GO_CMD="go" | ||
fuzz_time=${FUZZ_TIME:-"300s"} | ||
target_path=${TARGET_PATH:-"."} | ||
TARGETS="FuzzStep" | ||
|
||
|
||
for target in ${TARGETS}; do | ||
log_callout -e "\\nExecuting fuzzing with target ${target} in $target_path with a timeout of $fuzz_time\\n" | ||
run pushd "${target_path}" | ||
$GO_CMD test -fuzz "${target}" -fuzztime "${fuzz_time}" | ||
run popd | ||
log_success -e "\\COMPLETED: fuzzing with target $target in $target_path \\n" | ||
done |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried running this in IDE. Test failed and it took ~3mins. Is this expected? Runtime seems too long.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That stacktrace looks normal. The fuzzer runs into this panic:
raft/raft.go
Line 608 in 0c22de0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to make sure that this code works at least in CI. Please provide the tuning required for fuzzer to run on workflows.