-
Notifications
You must be signed in to change notification settings - Fork 281
/
main.go
99 lines (91 loc) · 2.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2015 go-fuzz project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// +build gofuzz
// +build !gofuzz_libfuzzer
package gofuzzdep
import (
"runtime"
"sync/atomic"
"syscall"
"time"
"unsafe"
. "github.com/dvyukov/go-fuzz/go-fuzz-defs"
)
func Main(fns []func([]byte) int) {
mem, inFD, outFD := setupCommFile()
CoverTab = (*[CoverSize]byte)(unsafe.Pointer(&mem[0]))
input := mem[CoverSize : CoverSize+MaxInputSize]
sonarRegion = mem[CoverSize+MaxInputSize:]
runtime.GOMAXPROCS(1) // makes coverage more deterministic, we parallelize on higher level
for {
fnidx, n := read(inFD)
if n > uint64(len(input)) {
println("invalid input length")
syscall.Exit(1)
}
for i := range CoverTab {
CoverTab[i] = 0
}
atomic.StoreUint32(&sonarPos, 0)
t0 := time.Now()
res := fns[fnidx](input[:n:n])
ns := time.Since(t0)
write(outFD, uint64(res), uint64(ns), uint64(atomic.LoadUint32(&sonarPos)))
}
}
// read reads little-endian-encoded uint8+uint64 from fd.
func read(fd FD) (uint8, uint64) {
rd := 0
var buf [9]byte
for rd != len(buf) {
n, err := fd.read(buf[rd:])
if err == syscall.EINTR {
continue
}
if n == 0 {
syscall.Exit(1)
}
if err != nil {
println("failed to read fd =", fd, "errno =", err.(syscall.Errno))
syscall.Exit(1)
}
rd += n
}
return buf[0], deserialize64(buf[1:])
}
// write writes little-endian-encoded vals... to fd.
func write(fd FD, vals ...uint64) {
var tmp [3 * 8]byte
buf := tmp[:len(vals)*8]
for i, v := range vals {
serialize64(buf[i*8:], v)
}
wr := 0
for wr != len(buf) {
n, err := fd.write(buf[wr:])
if err == syscall.EINTR {
continue
}
if err != nil {
println("failed to read fd =", fd, "errno =", err.(syscall.Errno))
syscall.Exit(1)
}
wr += n
}
}
// writeStr writes strings s to fd.
func writeStr(fd FD, s string) {
buf := []byte(s)
wr := 0
for wr != len(buf) {
n, err := fd.write(buf[wr:])
if err == syscall.EINTR {
continue
}
if err != nil {
println("failed to read fd =", fd, "errno =", err.(syscall.Errno))
syscall.Exit(1)
}
wr += n
}
}