59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
package writeBytesFuzz
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.thisco.de/vbatts/fuzz-walker/walker"
|
|
"github.com/hashicorp/errwrap"
|
|
)
|
|
|
|
func init() {
|
|
walker.RegisterFuzzFunc(Name, FuzzFunc)
|
|
// malloc early
|
|
for _, size := range sizes {
|
|
bufs[size] = make([]byte, size)
|
|
}
|
|
}
|
|
|
|
var bufs = map[int][]byte{}
|
|
var sizes = []int{1, 9, 33, 65, 129, 257} // going with common int sizes + 1
|
|
var randomized = false
|
|
|
|
// this need only be run once
|
|
func randomize(b map[int][]byte) error {
|
|
for size := range b {
|
|
_, err := rand.Read(b[size])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
const Name = "writeBytesFuzz"
|
|
|
|
func FuzzFunc(path string, info os.FileInfo) error {
|
|
if !info.Mode().IsRegular() {
|
|
return nil
|
|
}
|
|
if !randomized {
|
|
if err := randomize(bufs); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
fd, err := os.OpenFile(path, os.O_WRONLY, os.ModePerm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer fd.Close()
|
|
|
|
for size, buf := range bufs {
|
|
_, err := fd.Write(buf)
|
|
if err != nil {
|
|
return errwrap.Wrapf(fmt.Sprintf("failed writing %d random bytes to %q: {{err}}", size, path), err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|