-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For golang/go#60409 Change-Id: I75a9732ee996f0aeb91599d80803f96ada468c27 GitHub-Last-Rev: c348b61 GitHub-Pull-Request: #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>
- Loading branch information
1 parent
ca096e4
commit e0c3b6e
Showing
6 changed files
with
114 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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 | ||
|
||
import "unsafe" | ||
|
||
type mremapMmapper struct { | ||
mmapper | ||
mremap func(oldaddr uintptr, oldlength uintptr, newlength uintptr, flags int, newaddr uintptr) (xaddr uintptr, err error) | ||
} | ||
|
||
func (m *mremapMmapper) Mremap(oldData []byte, newLength int, flags int) (data []byte, err error) { | ||
if newLength <= 0 || len(oldData) == 0 || len(oldData) != cap(oldData) || flags&MREMAP_FIXED != 0 { | ||
return nil, EINVAL | ||
} | ||
|
||
pOld := &oldData[cap(oldData)-1] | ||
m.Lock() | ||
defer m.Unlock() | ||
bOld := m.active[pOld] | ||
if bOld == nil || &bOld[0] != &oldData[0] { | ||
return nil, EINVAL | ||
} | ||
newAddr, errno := m.mremap(uintptr(unsafe.Pointer(&bOld[0])), uintptr(len(bOld)), uintptr(newLength), flags, 0) | ||
if errno != nil { | ||
return nil, errno | ||
} | ||
bNew := unsafe.Slice((*byte)(unsafe.Pointer(newAddr)), newLength) | ||
pNew := &bNew[cap(bNew)-1] | ||
if flags&MREMAP_DONTUNMAP == 0 { | ||
delete(m.active, pOld) | ||
} | ||
m.active[pNew] = bNew | ||
return bNew, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.