From a0f816e4bfb12b433deba2fe930fcbde36ca6ff7 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Tue, 27 Jun 2023 16:54:44 +0200 Subject: [PATCH] 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 TryBot-Result: Gopher Robot Run-TryBot: Quim Muntal Reviewed-by: Alex Brainman Reviewed-by: Bryan Mills --- src/internal/fuzz/sys_windows.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/internal/fuzz/sys_windows.go b/src/internal/fuzz/sys_windows.go index aa85be7e1c6..82c97034c7e 100644 --- a/src/internal/fuzz/sys_windows.go +++ b/src/internal/fuzz/sys_windows.go @@ -85,12 +85,13 @@ func (m *sharedMem) Close() error { // run a worker process. func setWorkerComm(cmd *exec.Cmd, comm workerComm) { mem := <-comm.memMu - memName := mem.f.Name() + memFD := mem.f.Fd() comm.memMu <- mem syscall.SetHandleInformation(syscall.Handle(comm.fuzzIn.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)) - cmd.SysProcAttr = &syscall.SysProcAttr{AdditionalInheritedHandles: []syscall.Handle{syscall.Handle(comm.fuzzIn.Fd()), syscall.Handle(comm.fuzzOut.Fd())}} + syscall.SetHandleInformation(syscall.Handle(memFD), syscall.HANDLE_FLAG_INHERIT, 1) + 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. @@ -99,19 +100,15 @@ func getWorkerComm() (comm workerComm, err error) { if v == "" { return workerComm{}, fmt.Errorf("GO_TEST_FUZZ_WORKER_HANDLES not set") } - var fuzzInFD, fuzzOutFD uintptr - var memName string - if _, err := fmt.Sscanf(v, "%x,%x,%q", &fuzzInFD, &fuzzOutFD, &memName); err != nil { + var fuzzInFD, fuzzOutFD, memFileFD uintptr + if _, err := fmt.Sscanf(v, "%x,%x,%x", &fuzzInFD, &fuzzOutFD, &memFileFD); err != nil { return workerComm{}, fmt.Errorf("parsing GO_TEST_FUZZ_WORKER_HANDLES=%s: %v", v, err) } fuzzIn := os.NewFile(fuzzInFD, "fuzz_in") fuzzOut := os.NewFile(fuzzOutFD, "fuzz_out") - tmpFile, err := os.OpenFile(memName, os.O_RDWR, 0) - if err != nil { - return workerComm{}, fmt.Errorf("worker opening temp file: %w", err) - } - fi, err := tmpFile.Stat() + memFile := os.NewFile(memFileFD, "fuzz_mem") + fi, err := memFile.Stat() if err != nil { 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") } removeOnClose := false - mem, err := sharedMemMapFile(tmpFile, size, removeOnClose) + mem, err := sharedMemMapFile(memFile, size, removeOnClose) if err != nil { return workerComm{}, err }