Skip to content

Commit fa01c8a

Browse files
[release-branch.go1.15] internal/poll: adjust ignoringEINTR to avoid slice escape
The 1.15 compiler is not quite smart enough to see that the byte slice passed to ignoringEINTR does not escape. This ripples back up to user code which would see a byte slice passed to os.(*File).Write escape, which did not happen in 1.14. Rather than backport some moderately complex compiler fixes, rewrite the code slightly so that the 1.15 compiler is able to see that the slice does not escape. This is not a backport from tip, where the code is already different. The test for this will be on tip, where we will most likely change the compiler to understand this kind of code. Fixes #41543 For #41474 Change-Id: I6c78164229fea7794e7edba512bfd7034a0b91c3 Reviewed-on: https://go-review.googlesource.com/c/go/+/256418 Trust: Ian Lance Taylor <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent b1be142 commit fa01c8a

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/internal/poll/fd_unix.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (fd *FD) Read(p []byte) (int, error) {
152152
p = p[:maxRW]
153153
}
154154
for {
155-
n, err := ignoringEINTR(syscall.Read, fd.Sysfd, p)
155+
n, err := ignoringEINTR(func() (int, error) { return syscall.Read(fd.Sysfd, p) })
156156
if err != nil {
157157
n = 0
158158
if err == syscall.EAGAIN && fd.pd.pollable() {
@@ -264,7 +264,7 @@ func (fd *FD) Write(p []byte) (int, error) {
264264
if fd.IsStream && max-nn > maxRW {
265265
max = nn + maxRW
266266
}
267-
n, err := ignoringEINTR(syscall.Write, fd.Sysfd, p[nn:max])
267+
n, err := ignoringEINTR(func() (int, error) { return syscall.Write(fd.Sysfd, p[nn:max]) })
268268
if n > 0 {
269269
nn += n
270270
}
@@ -423,7 +423,7 @@ func (fd *FD) ReadDirent(buf []byte) (int, error) {
423423
}
424424
defer fd.decref()
425425
for {
426-
n, err := ignoringEINTR(syscall.ReadDirent, fd.Sysfd, buf)
426+
n, err := ignoringEINTR(func() (int, error) { return syscall.ReadDirent(fd.Sysfd, buf) })
427427
if err != nil {
428428
n = 0
429429
if err == syscall.EAGAIN && fd.pd.pollable() {
@@ -514,7 +514,7 @@ func (fd *FD) WriteOnce(p []byte) (int, error) {
514514
return 0, err
515515
}
516516
defer fd.writeUnlock()
517-
return ignoringEINTR(syscall.Write, fd.Sysfd, p)
517+
return ignoringEINTR(func() (int, error) { return syscall.Write(fd.Sysfd, p) })
518518
}
519519

520520
// RawRead invokes the user-defined function f for a read operation.
@@ -562,9 +562,9 @@ func (fd *FD) RawWrite(f func(uintptr) bool) error {
562562
// installed without setting SA_RESTART. None of these are the common case,
563563
// but there are enough of them that it seems that we can't avoid
564564
// an EINTR loop.
565-
func ignoringEINTR(fn func(fd int, p []byte) (int, error), fd int, p []byte) (int, error) {
565+
func ignoringEINTR(fn func() (int, error)) (int, error) {
566566
for {
567-
n, err := fn(fd, p)
567+
n, err := fn()
568568
if err != syscall.EINTR {
569569
return n, err
570570
}

src/runtime/trace/trace_stack_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ func TestTraceSymbolize(t *testing.T) {
252252
{trace.EvGoSysCall, []frame{
253253
{"syscall.read", 0},
254254
{"syscall.Read", 0},
255+
{"internal/poll.(*FD).Read.func1", 0},
255256
{"internal/poll.ignoringEINTR", 0},
256257
{"internal/poll.(*FD).Read", 0},
257258
{"os.(*File).read", 0},

0 commit comments

Comments
 (0)