diff --git a/unix/shm_test.go b/unix/shm_test.go new file mode 100644 index 00000000..b21bb439 --- /dev/null +++ b/unix/shm_test.go @@ -0,0 +1,64 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build darwin +// +build darwin + +package unix_test + +import ( + "testing" + + "golang.org/x/sys/unix" +) + +func TestSysvSharedMemory(t *testing.T) { + // create ipc + id, err := unix.SysvShmGet(unix.IPC_PRIVATE, 1024, unix.IPC_CREAT|unix.IPC_EXCL|0o600) + if err != nil { + t.Fatalf("SysvShmGet: %v", err) + } + defer func() { + _, err := unix.SysvShmCtl(id, unix.IPC_RMID, nil) + if err != nil { + t.Errorf("Remove failed: %v", err) + } + }() + + // attach + b1, err := unix.SysvShmAttach(id, 0, 0) + if err != nil { + t.Fatalf("Attach: %v", err) + } + + if len(b1) != 1024 { + t.Fatalf("b1 len = %v, want 1024", len(b1)) + } + + b1[42] = 'x' + + // attach again + b2, err := unix.SysvShmAttach(id, 0, 0) + if err != nil { + t.Fatalf("Attach: %v", err) + } + + if len(b2) != 1024 { + t.Fatalf("b2 len = %v, want 1024", len(b1)) + } + + b2[43] = 'y' + if b2[42] != 'x' || b1[43] != 'y' { + t.Fatalf("shared memory isn't shared") + } + + // detach + if err = unix.SysvShmDetach(b2); err != nil { + t.Fatalf("Detach: %v", err) + } + + if b1[42] != 'x' || b1[43] != 'y' { + t.Fatalf("shared memory was invalidated") + } +} diff --git a/unix/syscall_unix.go b/unix/syscall_unix.go index 9d3d4615..a0bcf4f9 100644 --- a/unix/syscall_unix.go +++ b/unix/syscall_unix.go @@ -447,8 +447,9 @@ func SysvShmAttach(id int, addr uintptr, flag int) ([]byte, error) { _, err := SysvShmCtl(id, IPC_STAT, &info) if err != nil { - // release the shared memory if we can't find the size; ignoring error - // here since there's nothing sensible we can return here + // release the shared memory if we can't find the size + + // ignoring error from shmdt as there's nothing sensible to return here shmdt(addr) return nil, err } diff --git a/unix/syscall_unix_test.go b/unix/syscall_unix_test.go index ea3a71be..2f672832 100644 --- a/unix/syscall_unix_test.go +++ b/unix/syscall_unix_test.go @@ -852,56 +852,6 @@ func TestRenameat(t *testing.T) { } } -func TestSysvSharedMemory(t *testing.T) { - // create ipc - id, err := unix.SysvShmGet(unix.IPC_PRIVATE, 1024, unix.IPC_CREAT|unix.IPC_EXCL|0o600) - if err != nil { - t.Fatalf("SysvShmGet: %v", err) - } - defer func() { - _, err := unix.SysvShmCtl(id, unix.IPC_RMID, nil) - if err != nil { - t.Errorf("Remove failed: %v", err) - } - }() - - // attach - b1, err := unix.SysvShmAttach(id, 0, 0) - if err != nil { - t.Fatalf("Attach: %v", err) - } - - if len(b1) != 1024 { - t.Fatalf("b1 len = %v, want 1024", len(b1)) - } - - b1[42] = 'x' - - // attach again - b2, err := unix.SysvShmAttach(id, 0, 0) - if err != nil { - t.Fatalf("Attach: %v", err) - } - - if len(b2) != 1024 { - t.Fatalf("b2 len = %v, want 1024", len(b1)) - } - - b2[43] = 'y' - if b2[42] != 'x' || b1[43] != 'y' { - t.Fatalf("shared memory isn't shared") - } - - // detach - if err = unix.SysvShmDetach(b2); err != nil { - t.Fatalf("Detach: %v", err) - } - - if b1[42] != 'x' || b1[43] != 'y' { - t.Fatalf("shared memory was invalidated") - } -} - func TestUtimesNanoAt(t *testing.T) { defer chtmpdir(t)()