mirror of
https://github.com/rwinkhart/sys.git
synced 2026-08-29 13:26:45 -04:00
For golang/go#60409 Change-Id: I75a9732ee996f0aeb91599d80803f96ada468c27 GitHub-Last-Rev: c348b6194d9fbe92f4f912c66709b0af75810e99 GitHub-Pull-Request: golang/sys#164 Reviewed-on: https://go-review.googlesource.com/c/sys/+/502715 Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
// Copyright 2023 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 linux
|
|
// +build linux
|
|
|
|
package unix_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func TestMremap(t *testing.T) {
|
|
b, err := unix.Mmap(-1, 0, unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
|
|
if err != nil {
|
|
t.Fatalf("Mmap: %v", err)
|
|
}
|
|
if err := unix.Mprotect(b, unix.PROT_READ|unix.PROT_WRITE); err != nil {
|
|
t.Fatalf("Mprotect: %v", err)
|
|
}
|
|
|
|
b[0] = 42
|
|
|
|
bNew, err := unix.Mremap(b, unix.Getpagesize()*2, unix.MREMAP_MAYMOVE)
|
|
if err != nil {
|
|
t.Fatalf("Mremap2: %v", err)
|
|
}
|
|
bNew[unix.Getpagesize()+1] = 84 // checks
|
|
|
|
if bNew[0] != 42 {
|
|
t.Fatal("first element value was changed")
|
|
}
|
|
if len(bNew) != unix.Getpagesize()*2 {
|
|
t.Fatal("new memory len not equal to specified len")
|
|
}
|
|
if cap(bNew) != unix.Getpagesize()*2 {
|
|
t.Fatal("new memory cap not equal to specified len")
|
|
}
|
|
|
|
_, err = unix.Mremap(b, unix.Getpagesize(), unix.MREMAP_FIXED)
|
|
if err != unix.EINVAL {
|
|
t.Fatalf("unix.MREMAP_FIXED should be forbidden")
|
|
}
|
|
}
|