1
0
mirror of https://github.com/golang/go synced 2024-11-06 14:26:22 -07:00

internal/fuzz: pass handle to temporary file instead of the path

This CL partially reverts CL 297034. Inheritable handles are not
inherited by all workers thanks to using AdditionalInheritedHandles,
which explicitly specifies which handles to inherit by each worker.

This CL doesn't fix any bug, it's more of a cleanup, but also makes
the code more robust and more similar to its Unix counterpart.

Change-Id: I24c2d7f69dfb839a1aeb5858088d8f38b022f702
Reviewed-on: https://go-review.googlesource.com/c/go/+/506535
Reviewed-by: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Quim Muntal <quimmuntal@gmail.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
This commit is contained in:
qmuntal 2023-06-27 16:54:44 +02:00 committed by Quim Muntal
parent d3e3e11298
commit a0f816e4bf

View File

@ -85,12 +85,13 @@ func (m *sharedMem) Close() error {
// run a worker process. // run a worker process.
func setWorkerComm(cmd *exec.Cmd, comm workerComm) { func setWorkerComm(cmd *exec.Cmd, comm workerComm) {
mem := <-comm.memMu mem := <-comm.memMu
memName := mem.f.Name() memFD := mem.f.Fd()
comm.memMu <- mem comm.memMu <- mem
syscall.SetHandleInformation(syscall.Handle(comm.fuzzIn.Fd()), syscall.HANDLE_FLAG_INHERIT, 1) syscall.SetHandleInformation(syscall.Handle(comm.fuzzIn.Fd()), syscall.HANDLE_FLAG_INHERIT, 1)
syscall.SetHandleInformation(syscall.Handle(comm.fuzzOut.Fd()), syscall.HANDLE_FLAG_INHERIT, 1) syscall.SetHandleInformation(syscall.Handle(comm.fuzzOut.Fd()), syscall.HANDLE_FLAG_INHERIT, 1)
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_TEST_FUZZ_WORKER_HANDLES=%x,%x,%q", comm.fuzzIn.Fd(), comm.fuzzOut.Fd(), memName)) syscall.SetHandleInformation(syscall.Handle(memFD), syscall.HANDLE_FLAG_INHERIT, 1)
cmd.SysProcAttr = &syscall.SysProcAttr{AdditionalInheritedHandles: []syscall.Handle{syscall.Handle(comm.fuzzIn.Fd()), syscall.Handle(comm.fuzzOut.Fd())}} cmd.Env = append(cmd.Env, fmt.Sprintf("GO_TEST_FUZZ_WORKER_HANDLES=%x,%x,%x", comm.fuzzIn.Fd(), comm.fuzzOut.Fd(), memFD))
cmd.SysProcAttr = &syscall.SysProcAttr{AdditionalInheritedHandles: []syscall.Handle{syscall.Handle(comm.fuzzIn.Fd()), syscall.Handle(comm.fuzzOut.Fd()), syscall.Handle(memFD)}}
} }
// getWorkerComm returns communication channels in the worker process. // getWorkerComm returns communication channels in the worker process.
@ -99,19 +100,15 @@ func getWorkerComm() (comm workerComm, err error) {
if v == "" { if v == "" {
return workerComm{}, fmt.Errorf("GO_TEST_FUZZ_WORKER_HANDLES not set") return workerComm{}, fmt.Errorf("GO_TEST_FUZZ_WORKER_HANDLES not set")
} }
var fuzzInFD, fuzzOutFD uintptr var fuzzInFD, fuzzOutFD, memFileFD uintptr
var memName string if _, err := fmt.Sscanf(v, "%x,%x,%x", &fuzzInFD, &fuzzOutFD, &memFileFD); err != nil {
if _, err := fmt.Sscanf(v, "%x,%x,%q", &fuzzInFD, &fuzzOutFD, &memName); err != nil {
return workerComm{}, fmt.Errorf("parsing GO_TEST_FUZZ_WORKER_HANDLES=%s: %v", v, err) return workerComm{}, fmt.Errorf("parsing GO_TEST_FUZZ_WORKER_HANDLES=%s: %v", v, err)
} }
fuzzIn := os.NewFile(fuzzInFD, "fuzz_in") fuzzIn := os.NewFile(fuzzInFD, "fuzz_in")
fuzzOut := os.NewFile(fuzzOutFD, "fuzz_out") fuzzOut := os.NewFile(fuzzOutFD, "fuzz_out")
tmpFile, err := os.OpenFile(memName, os.O_RDWR, 0) memFile := os.NewFile(memFileFD, "fuzz_mem")
if err != nil { fi, err := memFile.Stat()
return workerComm{}, fmt.Errorf("worker opening temp file: %w", err)
}
fi, err := tmpFile.Stat()
if err != nil { if err != nil {
return workerComm{}, fmt.Errorf("worker checking temp file size: %w", err) return workerComm{}, fmt.Errorf("worker checking temp file size: %w", err)
} }
@ -120,7 +117,7 @@ func getWorkerComm() (comm workerComm, err error) {
return workerComm{}, fmt.Errorf("fuzz temp file exceeds maximum size") return workerComm{}, fmt.Errorf("fuzz temp file exceeds maximum size")
} }
removeOnClose := false removeOnClose := false
mem, err := sharedMemMapFile(tmpFile, size, removeOnClose) mem, err := sharedMemMapFile(memFile, size, removeOnClose)
if err != nil { if err != nil {
return workerComm{}, err return workerComm{}, err
} }