2018-06-05 20:53:08 +00:00
|
|
|
package walker
|
|
|
|
|
|
|
|
import (
|
2018-06-05 21:34:58 +00:00
|
|
|
"fmt"
|
2018-06-05 20:53:08 +00:00
|
|
|
"os"
|
2018-06-05 21:34:58 +00:00
|
|
|
"plugin"
|
2018-06-06 16:29:33 +00:00
|
|
|
"sync"
|
2018-06-05 20:53:08 +00:00
|
|
|
)
|
|
|
|
|
2018-06-06 16:29:33 +00:00
|
|
|
// RegisterFuzzFunc is for static linking (not plugin) model
|
|
|
|
func RegisterFuzzFunc(name string, fnc FuzzFunc) error {
|
|
|
|
ffLock.Lock()
|
|
|
|
defer ffLock.Unlock()
|
|
|
|
if _, ok := fuzzfuncs[name]; ok {
|
|
|
|
// maybe just output a warning and not bail here?
|
|
|
|
return fmt.Errorf("RegisterFuzzFunc: %q is already registered", name)
|
|
|
|
}
|
|
|
|
fuzzfuncs[name] = fnc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func FuncMap() map[string]FuzzFunc {
|
|
|
|
return fuzzfuncs
|
|
|
|
}
|
|
|
|
|
|
|
|
var fuzzfuncs = map[string]FuzzFunc{}
|
|
|
|
var ffLock sync.Mutex
|
|
|
|
|
|
|
|
// LoadPlugin loads the file path and returns a Fuzzer.
|
|
|
|
//
|
|
|
|
// This is only for making a plugin.
|
2018-06-05 21:34:58 +00:00
|
|
|
func LoadPlugin(path string) (Fuzzer, error) {
|
|
|
|
p, err := plugin.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-06 14:42:39 +00:00
|
|
|
sym, err := p.Lookup("NewFuzzer")
|
2018-06-05 21:34:58 +00:00
|
|
|
if err != nil {
|
2018-06-06 14:42:39 +00:00
|
|
|
return nil, fmt.Errorf("LoadPlugin: %q", err)
|
2018-06-05 21:34:58 +00:00
|
|
|
}
|
2018-06-06 14:42:39 +00:00
|
|
|
nf, ok := sym.(func() (Fuzzer, error))
|
2018-06-05 21:34:58 +00:00
|
|
|
if !ok {
|
2018-06-06 14:42:39 +00:00
|
|
|
return nil, fmt.Errorf("LoadPlugin: plugin at %q did not provide a NewFuzzer function", path)
|
2018-06-05 21:34:58 +00:00
|
|
|
}
|
2018-06-06 14:42:39 +00:00
|
|
|
return nf()
|
2018-06-05 21:34:58 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 16:29:33 +00:00
|
|
|
// NewFuzzer initializes a new Fuzzer.
|
|
|
|
//
|
|
|
|
// This is only for making a plugin.
|
2018-06-06 14:42:39 +00:00
|
|
|
type NewFuzzer func() (Fuzzer, error)
|
|
|
|
|
2018-06-06 16:29:33 +00:00
|
|
|
// Fuzzer is a struct that produces a FuzzFunc.
|
|
|
|
//
|
|
|
|
// This is only for making a plugin.
|
2018-06-05 21:34:58 +00:00
|
|
|
type Fuzzer interface {
|
|
|
|
Name() string
|
2018-06-06 16:29:33 +00:00
|
|
|
Func(path string, info os.FileInfo) error
|
2018-06-05 21:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FuzzFunc is like filepath.WalkerFunc, but for poking at `path`
|
2018-06-06 16:29:33 +00:00
|
|
|
type FuzzFunc func(path string, info os.FileInfo) error
|