Skip to content

Commit 67d1929

Browse files
committed
go-fuzz: add reuseable buffers for testee communication
These are small, but they get used a lot. Also, binary.Read and binary.Write go through reflect, which allocates. Not a lot, but this is very high volume.
1 parent 90da512 commit 67d1929

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

go-fuzz/testee.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package main
66
import (
77
"encoding/binary"
88
"fmt"
9+
"io"
910
"io/ioutil"
1011
"log"
1112
"os"
@@ -28,6 +29,8 @@ type Testee struct {
2829
inPipe *os.File
2930
outPipe *os.File
3031
stdoutPipe *os.File
32+
datalen [8]byte // reusable encoded data length
33+
resbuf [24]byte // reusable results buffer
3134
execs int
3235
startTime int64
3336
outputC chan []byte
@@ -287,7 +290,8 @@ func (t *Testee) test(data []byte) (res int, ns uint64, cover, sonar []byte, cra
287290

288291
copy(t.inputRegion[:], data)
289292
atomic.StoreInt64(&t.startTime, time.Now().UnixNano())
290-
if err := binary.Write(t.outPipe, binary.LittleEndian, uint64(len(data))); err != nil {
293+
binary.LittleEndian.PutUint64(t.datalen[:], uint64(len(data)))
294+
if _, err := t.outPipe.Write(t.datalen[:]); err != nil {
291295
if *flagV >= 1 {
292296
log.Printf("write to testee failed: %v", err)
293297
}
@@ -301,8 +305,12 @@ func (t *Testee) test(data []byte) (res int, ns uint64, cover, sonar []byte, cra
301305
Ns uint64
302306
Sonar uint64
303307
}
304-
var r Reply
305-
err := binary.Read(t.inPipe, binary.LittleEndian, &r)
308+
_, err := io.ReadFull(t.inPipe, t.resbuf[:])
309+
r := Reply{
310+
Res: binary.LittleEndian.Uint64(t.resbuf[:]),
311+
Ns: binary.LittleEndian.Uint64(t.resbuf[8:]),
312+
Sonar: binary.LittleEndian.Uint64(t.resbuf[16:]),
313+
}
306314
hanged = atomic.LoadInt64(&t.startTime) == -1
307315
atomic.StoreInt64(&t.startTime, 0)
308316
if err != nil || hanged {

0 commit comments

Comments
 (0)