switching to plugins

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2018-06-06 10:42:39 -04:00
parent 339b6145f9
commit 0f0ce2af37
Signed by: vbatts
GPG key ID: 10937E57733F1362
5 changed files with 70 additions and 42 deletions

View file

@ -13,21 +13,24 @@ func LoadPlugin(path string) (Fuzzer, error) {
if err != nil {
return nil, err
}
sym, err := p.Lookup("MyFuzzerFunc")
sym, err := p.Lookup("NewFuzzer")
if err != nil {
return nil, err
return nil, fmt.Errorf("LoadPlugin: %q", err)
}
mff, ok := sym.(Fuzzer)
nf, ok := sym.(func() (Fuzzer, error))
if !ok {
return nil, fmt.Errorf("plugin at %q did not provide a FuzzFunc named MyFuzzerFunc", path)
return nil, fmt.Errorf("LoadPlugin: plugin at %q did not provide a NewFuzzer function", path)
}
return mff, nil
return nf()
}
// NewFuzzer initializes a new Fuzzer
type NewFuzzer func() (Fuzzer, error)
// Fuzzer is a struct that produces a FuzzFunc
type Fuzzer interface {
Name() string
Func() FuzzFunc
Func(path string, info os.FileInfo, timeout time.Duration) error
}
// FuzzFunc is like filepath.WalkerFunc, but for poking at `path`

View file

@ -0,0 +1,42 @@
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"time"
"git.thisco.de/vbatts/fuzz-walker/walker"
)
func NewFuzzer() (walker.Fuzzer, error) {
return &myFuzzer{}, nil
}
type myFuzzer struct{}
func (mf myFuzzer) Name() string {
return "readFuzz"
}
func (mf myFuzzer) Func(path string, info os.FileInfo, timeout time.Duration) error {
c1 := make(chan error, 1)
go func() {
fd, err := os.Open(path)
if err != nil {
c1 <- err
}
defer fd.Close()
_, err = io.Copy(ioutil.Discard, fd)
c1 <- err
}()
select {
case err := <-c1:
return err
case <-time.After(timeout):
return fmt.Errorf("timeout reached")
}
return nil
}