Skip to content

Commit a0f816e

Browse files
committed
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 <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Quim Muntal <[email protected]> Reviewed-by: Alex Brainman <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent d3e3e11 commit a0f816e

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

src/internal/fuzz/sys_windows.go

+9-12
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,13 @@ func (m *sharedMem) Close() error {
8585
// run a worker process.
8686
func setWorkerComm(cmd *exec.Cmd, comm workerComm) {
8787
mem := <-comm.memMu
88-
memName := mem.f.Name()
88+
memFD := mem.f.Fd()
8989
comm.memMu <- mem
9090
syscall.SetHandleInformation(syscall.Handle(comm.fuzzIn.Fd()), syscall.HANDLE_FLAG_INHERIT, 1)
9191
syscall.SetHandleInformation(syscall.Handle(comm.fuzzOut.Fd()), syscall.HANDLE_FLAG_INHERIT, 1)
92-
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_TEST_FUZZ_WORKER_HANDLES=%x,%x,%q", comm.fuzzIn.Fd(), comm.fuzzOut.Fd(), memName))
93-
cmd.SysProcAttr = &syscall.SysProcAttr{AdditionalInheritedHandles: []syscall.Handle{syscall.Handle(comm.fuzzIn.Fd()), syscall.Handle(comm.fuzzOut.Fd())}}
92+
syscall.SetHandleInformation(syscall.Handle(memFD), syscall.HANDLE_FLAG_INHERIT, 1)
93+
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_TEST_FUZZ_WORKER_HANDLES=%x,%x,%x", comm.fuzzIn.Fd(), comm.fuzzOut.Fd(), memFD))
94+
cmd.SysProcAttr = &syscall.SysProcAttr{AdditionalInheritedHandles: []syscall.Handle{syscall.Handle(comm.fuzzIn.Fd()), syscall.Handle(comm.fuzzOut.Fd()), syscall.Handle(memFD)}}
9495
}
9596

9697
// getWorkerComm returns communication channels in the worker process.
@@ -99,19 +100,15 @@ func getWorkerComm() (comm workerComm, err error) {
99100
if v == "" {
100101
return workerComm{}, fmt.Errorf("GO_TEST_FUZZ_WORKER_HANDLES not set")
101102
}
102-
var fuzzInFD, fuzzOutFD uintptr
103-
var memName string
104-
if _, err := fmt.Sscanf(v, "%x,%x,%q", &fuzzInFD, &fuzzOutFD, &memName); err != nil {
103+
var fuzzInFD, fuzzOutFD, memFileFD uintptr
104+
if _, err := fmt.Sscanf(v, "%x,%x,%x", &fuzzInFD, &fuzzOutFD, &memFileFD); err != nil {
105105
return workerComm{}, fmt.Errorf("parsing GO_TEST_FUZZ_WORKER_HANDLES=%s: %v", v, err)
106106
}
107107

108108
fuzzIn := os.NewFile(fuzzInFD, "fuzz_in")
109109
fuzzOut := os.NewFile(fuzzOutFD, "fuzz_out")
110-
tmpFile, err := os.OpenFile(memName, os.O_RDWR, 0)
111-
if err != nil {
112-
return workerComm{}, fmt.Errorf("worker opening temp file: %w", err)
113-
}
114-
fi, err := tmpFile.Stat()
110+
memFile := os.NewFile(memFileFD, "fuzz_mem")
111+
fi, err := memFile.Stat()
115112
if err != nil {
116113
return workerComm{}, fmt.Errorf("worker checking temp file size: %w", err)
117114
}
@@ -120,7 +117,7 @@ func getWorkerComm() (comm workerComm, err error) {
120117
return workerComm{}, fmt.Errorf("fuzz temp file exceeds maximum size")
121118
}
122119
removeOnClose := false
123-
mem, err := sharedMemMapFile(tmpFile, size, removeOnClose)
120+
mem, err := sharedMemMapFile(memFile, size, removeOnClose)
124121
if err != nil {
125122
return workerComm{}, err
126123
}

0 commit comments

Comments
 (0)