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

Add -dict option (like AFL's -x) to replace low-signal string literal list #315

Open
wants to merge 6 commits into
base: master
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
19 changes: 19 additions & 0 deletions go-fuzz/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package main
import (
"fmt"
"log"
"io/ioutil"
"strings"
"net/rpc"
"path/filepath"
"sync"
Expand Down Expand Up @@ -116,6 +118,23 @@ func newHub(metadata MetaData) *Hub {
ro.intLits = append(ro.intLits, []byte(lit.Val))
}
}
if *flagDict != "" {
ro.strLits = nil // Discard existing tokens
fileName := *flagDict
Copy link
Owner

Choose a reason for hiding this comment

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

This renaming looks excessive, especially if we remove the log.Printf. Just use *flagDict.

dictData, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatalf("could not read tokens from %q: %v", fileName, err)
}
textData := string(dictData)
splits := strings.Split(textData,"\n")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not that it really matters, but let's not convert to string and then convert back to []byte. Use bytes.Split instead. Or use a bufio.Scanner, which'll be simpler yet.

Copy link
Owner

Choose a reason for hiding this comment

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

+1

for S := range splits {
Copy link
Owner

Choose a reason for hiding this comment

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

Will be shorter with for _, token := range splits.
Or even shorter with for _, token := range strings.Split(textData,"\n").

token := splits[S]
if token == "\n" || token == "" {
Copy link
Owner

Choose a reason for hiding this comment

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

Can it contain "\n" is we split by "\n"?

continue // skip blanks
}
ro.strLits = append(ro.strLits, []byte(token))
}
}
hub.ro.Store(ro)

go hub.loop()
Expand Down
11 changes: 11 additions & 0 deletions go-fuzz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
flagSonar = flag.Bool("sonar", true, "use sonar hints")
flagV = flag.Int("v", 0, "verbosity level")
flagHTTP = flag.String("http", "", "HTTP server listen address (coordinator mode only)")
flagDict = flag.String("dict", "", "dictionary file containing string tokens (one per line, not quoted)")

shutdown uint32
shutdownC = make(chan struct{})
Expand All @@ -57,6 +58,16 @@ func main() {
log.Fatalf("both -http and -worker are specified")
}

if *flagDict != "" {
dictStat, err := os.Stat(*flagDict)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add a comment explaining why we stat here.

Copy link
Owner

Choose a reason for hiding this comment

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

Could we pass *flagDict to workerMain? Or read here and pass contents to workerMain?
It looks like this split stat/size check + reading separately doubles amount of code.

if err != nil {
log.Fatalf("cannot read dictionary file %q: %v", *flagDict, err)
}
if dictStat.Size() == 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any reason not to allow an empty dictionary? E.g. I can imagine someone using this to disable dictionaries. (Maybe it'd break something in the mutator code? If so, we could add an early exit when strLits is empty.)

Copy link
Author

Choose a reason for hiding this comment

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

I feel if the user is manually specifying a dictionary, it would be strange if it was empty -- therefore probably a mistake. Happy to remove the Fatalf call, but I still think it should be logged. Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sure, we could log.

log.Fatalf("dictionary file %q is empty", *flagDict)
}
}

go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT)
Expand Down