Skip to content

Commit a84d905

Browse files
authored
Fix go:linkname for mmap (#107)
1 parent aa7edb1 commit a84d905

File tree

3 files changed

+9
-17
lines changed

3 files changed

+9
-17
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/tetratelabs/wazero v1.7.3
1111
golang.org/x/crypto v0.24.0
1212
golang.org/x/sync v0.7.0
13-
golang.org/x/sys v0.21.0
13+
golang.org/x/sys v0.21.1-0.20240625003756-daa239428c2d
1414
golang.org/x/text v0.16.0
1515
lukechampine.com/adiantum v1.1.1
1616
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
1212
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
1313
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
1414
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
15-
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
16-
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
15+
golang.org/x/sys v0.21.1-0.20240625003756-daa239428c2d h1:+oefiaQuWDtgsRTCSGwaYzgW+OElRO3uOsTt564+/N8=
16+
golang.org/x/sys v0.21.1-0.20240625003756-daa239428c2d/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
1717
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
1818
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
1919
lukechampine.com/adiantum v1.1.1 h1:4fp6gTxWCqpEbLy40ExiYDDED3oUNWx5cTqBCtPdZqA=

internal/util/mmap.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (s *mmapState) new(ctx context.Context, mod api.Module, size int32) *Mapped
4646
// Save the newly allocated region.
4747
ptr := uint32(stack[0])
4848
buf := View(mod, ptr, uint64(size))
49-
addr := uintptr(unsafe.Pointer(&buf[0]))
49+
addr := unsafe.Pointer(&buf[0])
5050
s.regions = append(s.regions, &MappedRegion{
5151
Ptr: ptr,
5252
addr: addr,
@@ -56,7 +56,7 @@ func (s *mmapState) new(ctx context.Context, mod api.Module, size int32) *Mapped
5656
}
5757

5858
type MappedRegion struct {
59-
addr uintptr
59+
addr unsafe.Pointer
6060
Ptr uint32
6161
size int32
6262
used bool
@@ -76,23 +76,15 @@ func (r *MappedRegion) Unmap() error {
7676
// We can't munmap the region, otherwise it could be remaped.
7777
// Instead, convert it to a protected, private, anonymous mapping.
7878
// If successful, it can be reused for a subsequent mmap.
79-
_, err := mmap(r.addr, uintptr(r.size),
80-
unix.PROT_NONE, unix.MAP_PRIVATE|unix.MAP_ANON|unix.MAP_FIXED,
81-
-1, 0)
79+
_, err := unix.MmapPtr(-1, 0, r.addr, uintptr(r.size),
80+
unix.PROT_NONE, unix.MAP_PRIVATE|unix.MAP_FIXED|unix.MAP_ANON)
8281
r.used = err != nil
8382
return err
8483
}
8584

8685
func (r *MappedRegion) mmap(f *os.File, offset int64, prot int) error {
87-
_, err := mmap(r.addr, uintptr(r.size),
88-
prot, unix.MAP_SHARED|unix.MAP_FIXED,
89-
int(f.Fd()), offset)
86+
_, err := unix.MmapPtr(int(f.Fd()), offset, r.addr, uintptr(r.size),
87+
prot, unix.MAP_SHARED|unix.MAP_FIXED)
9088
r.used = err == nil
9189
return err
9290
}
93-
94-
// We need the low level mmap for MAP_FIXED to work.
95-
// Bind the syscall version hoping that it is more stable.
96-
97-
//go:linkname mmap syscall.mmap
98-
func mmap(addr, length uintptr, prot, flag, fd int, pos int64) (*byte, error)

0 commit comments

Comments
 (0)