unix: fix darwin pipe implementation
The raw syscall returned the two pipes whereas the libc call
takes a pointer to a location to write the two pipes.
When we switched over from raw syscalls to libc calls, this
change in behavior was missed.
Fixes golang/go#43498
Change-Id: Icee2204dcb8be8fc94be0df106e1ff061cafa446
Reviewed-on: https://go-review.googlesource.com/c/sys/+/281432
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/unix/syscall_darwin.go b/unix/syscall_darwin.go
index b625738..16f9c22 100644
--- a/unix/syscall_darwin.go
+++ b/unix/syscall_darwin.go
@@ -119,13 +119,16 @@
Forkattr uint32
}
-//sysnb pipe() (r int, w int, err error)
+//sysnb pipe(p *[2]int32) (err error)
func Pipe(p []int) (err error) {
if len(p) != 2 {
return EINVAL
}
- p[0], p[1], err = pipe()
+ var x [2]int32
+ err = pipe(&x)
+ p[0] = int(x[0])
+ p[1] = int(x[1])
return
}
diff --git a/unix/syscall_unix_test.go b/unix/syscall_unix_test.go
index 473e790..41de3b8 100644
--- a/unix/syscall_unix_test.go
+++ b/unix/syscall_unix_test.go
@@ -896,3 +896,39 @@
os.RemoveAll(d)
}
}
+
+func TestPipe(t *testing.T) {
+ const s = "hello"
+ var pipes [2]int
+ unix.Pipe(pipes[:])
+ r := pipes[0]
+ w := pipes[1]
+ go func() {
+ n, err := unix.Write(w, []byte(s))
+ if err != nil {
+ t.Fatalf("bad write: %s\n", err)
+ }
+ if n != len(s) {
+ t.Fatalf("bad write count: %d\n", n)
+ }
+ err = unix.Close(w)
+ if err != nil {
+ t.Fatalf("bad close: %s\n", err)
+ }
+ }()
+ var buf [10 + len(s)]byte
+ n, err := unix.Read(r, buf[:])
+ if err != nil {
+ t.Fatalf("bad read: %s\n", err)
+ }
+ if n != len(s) {
+ t.Fatalf("bad read count: %d\n", n)
+ }
+ if string(buf[:n]) != s {
+ t.Fatalf("bad contents: %s\n", string(buf[:n]))
+ }
+ err = unix.Close(r)
+ if err != nil {
+ t.Fatalf("bad close: %s\n", err)
+ }
+}
diff --git a/unix/zsyscall_darwin_386.go b/unix/zsyscall_darwin_386.go
index 36ab7ea..3877183 100644
--- a/unix/zsyscall_darwin_386.go
+++ b/unix/zsyscall_darwin_386.go
@@ -462,10 +462,8 @@
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
-func pipe() (r int, w int, err error) {
- r0, r1, e1 := syscall_rawSyscall(funcPC(libc_pipe_trampoline), 0, 0, 0)
- r = int(r0)
- w = int(r1)
+func pipe(p *[2]int32) (err error) {
+ _, _, e1 := syscall_rawSyscall(funcPC(libc_pipe_trampoline), uintptr(unsafe.Pointer(p)), 0, 0)
if e1 != 0 {
err = errnoErr(e1)
}
diff --git a/unix/zsyscall_darwin_amd64.go b/unix/zsyscall_darwin_amd64.go
index 7b854cc..508e563 100644
--- a/unix/zsyscall_darwin_amd64.go
+++ b/unix/zsyscall_darwin_amd64.go
@@ -462,10 +462,8 @@
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
-func pipe() (r int, w int, err error) {
- r0, r1, e1 := syscall_rawSyscall(funcPC(libc_pipe_trampoline), 0, 0, 0)
- r = int(r0)
- w = int(r1)
+func pipe(p *[2]int32) (err error) {
+ _, _, e1 := syscall_rawSyscall(funcPC(libc_pipe_trampoline), uintptr(unsafe.Pointer(p)), 0, 0)
if e1 != 0 {
err = errnoErr(e1)
}
diff --git a/unix/zsyscall_darwin_arm.go b/unix/zsyscall_darwin_arm.go
index 8e79ad3..c0c771f 100644
--- a/unix/zsyscall_darwin_arm.go
+++ b/unix/zsyscall_darwin_arm.go
@@ -462,10 +462,8 @@
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
-func pipe() (r int, w int, err error) {
- r0, r1, e1 := syscall_rawSyscall(funcPC(libc_pipe_trampoline), 0, 0, 0)
- r = int(r0)
- w = int(r1)
+func pipe(p *[2]int32) (err error) {
+ _, _, e1 := syscall_rawSyscall(funcPC(libc_pipe_trampoline), uintptr(unsafe.Pointer(p)), 0, 0)
if e1 != 0 {
err = errnoErr(e1)
}
diff --git a/unix/zsyscall_darwin_arm64.go b/unix/zsyscall_darwin_arm64.go
index 99509b1..9b01a79 100644
--- a/unix/zsyscall_darwin_arm64.go
+++ b/unix/zsyscall_darwin_arm64.go
@@ -462,10 +462,8 @@
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
-func pipe() (r int, w int, err error) {
- r0, r1, e1 := syscall_rawSyscall(funcPC(libc_pipe_trampoline), 0, 0, 0)
- r = int(r0)
- w = int(r1)
+func pipe(p *[2]int32) (err error) {
+ _, _, e1 := syscall_rawSyscall(funcPC(libc_pipe_trampoline), uintptr(unsafe.Pointer(p)), 0, 0)
if e1 != 0 {
err = errnoErr(e1)
}