2018-05-19 12:49:46 +00:00
|
|
|
// Copyright 2011 Evan Shaw. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package mmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"sync"
|
2019-09-12 19:58:09 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2018-05-19 12:49:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// mmap on Windows is a two-step process.
|
|
|
|
// First, we call CreateFileMapping to get a handle.
|
|
|
|
// Then, we call MapviewToFile to get an actual pointer into memory.
|
|
|
|
// Because we want to emulate a POSIX-style mmap, we don't want to expose
|
|
|
|
// the handle -- only the pointer. We also want to return only a byte slice,
|
|
|
|
// not a struct, so it's convenient to manipulate.
|
|
|
|
|
|
|
|
// We keep this map so that we can get back the original handle from the memory address.
|
2019-09-12 19:58:09 +00:00
|
|
|
|
|
|
|
type addrinfo struct {
|
|
|
|
file windows.Handle
|
|
|
|
mapview windows.Handle
|
|
|
|
}
|
|
|
|
|
2018-05-19 12:49:46 +00:00
|
|
|
var handleLock sync.Mutex
|
2019-09-12 19:58:09 +00:00
|
|
|
var handleMap = map[uintptr]*addrinfo{}
|
2018-05-19 12:49:46 +00:00
|
|
|
|
|
|
|
func mmap(len int, prot, flags, hfile uintptr, off int64) ([]byte, error) {
|
2019-09-12 19:58:09 +00:00
|
|
|
flProtect := uint32(windows.PAGE_READONLY)
|
|
|
|
dwDesiredAccess := uint32(windows.FILE_MAP_READ)
|
2018-05-19 12:49:46 +00:00
|
|
|
switch {
|
|
|
|
case prot© != 0:
|
2019-09-12 19:58:09 +00:00
|
|
|
flProtect = windows.PAGE_WRITECOPY
|
|
|
|
dwDesiredAccess = windows.FILE_MAP_COPY
|
2018-05-19 12:49:46 +00:00
|
|
|
case prot&RDWR != 0:
|
2019-09-12 19:58:09 +00:00
|
|
|
flProtect = windows.PAGE_READWRITE
|
|
|
|
dwDesiredAccess = windows.FILE_MAP_WRITE
|
2018-05-19 12:49:46 +00:00
|
|
|
}
|
|
|
|
if prot&EXEC != 0 {
|
|
|
|
flProtect <<= 4
|
2019-09-12 19:58:09 +00:00
|
|
|
dwDesiredAccess |= windows.FILE_MAP_EXECUTE
|
2018-05-19 12:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The maximum size is the area of the file, starting from 0,
|
|
|
|
// that we wish to allow to be mappable. It is the sum of
|
|
|
|
// the length the user requested, plus the offset where that length
|
|
|
|
// is starting from. This does not map the data into memory.
|
|
|
|
maxSizeHigh := uint32((off + int64(len)) >> 32)
|
|
|
|
maxSizeLow := uint32((off + int64(len)) & 0xFFFFFFFF)
|
|
|
|
// TODO: Do we need to set some security attributes? It might help portability.
|
2019-09-12 19:58:09 +00:00
|
|
|
h, errno := windows.CreateFileMapping(windows.Handle(hfile), nil, flProtect, maxSizeHigh, maxSizeLow, nil)
|
2018-05-19 12:49:46 +00:00
|
|
|
if h == 0 {
|
|
|
|
return nil, os.NewSyscallError("CreateFileMapping", errno)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actually map a view of the data into memory. The view's size
|
|
|
|
// is the length the user requested.
|
|
|
|
fileOffsetHigh := uint32(off >> 32)
|
|
|
|
fileOffsetLow := uint32(off & 0xFFFFFFFF)
|
2019-09-12 19:58:09 +00:00
|
|
|
addr, errno := windows.MapViewOfFile(h, dwDesiredAccess, fileOffsetHigh, fileOffsetLow, uintptr(len))
|
2018-05-19 12:49:46 +00:00
|
|
|
if addr == 0 {
|
|
|
|
return nil, os.NewSyscallError("MapViewOfFile", errno)
|
|
|
|
}
|
|
|
|
handleLock.Lock()
|
2019-09-12 19:58:09 +00:00
|
|
|
handleMap[addr] = &addrinfo{
|
|
|
|
file: windows.Handle(hfile),
|
|
|
|
mapview: h,
|
|
|
|
}
|
2018-05-19 12:49:46 +00:00
|
|
|
handleLock.Unlock()
|
|
|
|
|
|
|
|
m := MMap{}
|
|
|
|
dh := m.header()
|
|
|
|
dh.Data = addr
|
|
|
|
dh.Len = len
|
|
|
|
dh.Cap = dh.Len
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2019-09-12 19:58:09 +00:00
|
|
|
func (m MMap) flush() error {
|
|
|
|
addr, len := m.addrLen()
|
|
|
|
errno := windows.FlushViewOfFile(addr, len)
|
2018-05-19 12:49:46 +00:00
|
|
|
if errno != nil {
|
|
|
|
return os.NewSyscallError("FlushViewOfFile", errno)
|
|
|
|
}
|
|
|
|
|
|
|
|
handleLock.Lock()
|
|
|
|
defer handleLock.Unlock()
|
|
|
|
handle, ok := handleMap[addr]
|
|
|
|
if !ok {
|
|
|
|
// should be impossible; we would've errored above
|
|
|
|
return errors.New("unknown base address")
|
|
|
|
}
|
|
|
|
|
2019-09-12 19:58:09 +00:00
|
|
|
errno = windows.FlushFileBuffers(handle.file)
|
2018-05-19 12:49:46 +00:00
|
|
|
return os.NewSyscallError("FlushFileBuffers", errno)
|
|
|
|
}
|
|
|
|
|
2019-09-12 19:58:09 +00:00
|
|
|
func (m MMap) lock() error {
|
|
|
|
addr, len := m.addrLen()
|
|
|
|
errno := windows.VirtualLock(addr, len)
|
2018-05-19 12:49:46 +00:00
|
|
|
return os.NewSyscallError("VirtualLock", errno)
|
|
|
|
}
|
|
|
|
|
2019-09-12 19:58:09 +00:00
|
|
|
func (m MMap) unlock() error {
|
|
|
|
addr, len := m.addrLen()
|
|
|
|
errno := windows.VirtualUnlock(addr, len)
|
2018-05-19 12:49:46 +00:00
|
|
|
return os.NewSyscallError("VirtualUnlock", errno)
|
|
|
|
}
|
|
|
|
|
2019-09-12 19:58:09 +00:00
|
|
|
func (m MMap) unmap() error {
|
|
|
|
err := m.flush()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := m.header().Data
|
2018-05-19 12:49:46 +00:00
|
|
|
// Lock the UnmapViewOfFile along with the handleMap deletion.
|
|
|
|
// As soon as we unmap the view, the OS is free to give the
|
|
|
|
// same addr to another new map. We don't want another goroutine
|
|
|
|
// to insert and remove the same addr into handleMap while
|
|
|
|
// we're trying to remove our old addr/handle pair.
|
|
|
|
handleLock.Lock()
|
|
|
|
defer handleLock.Unlock()
|
2019-09-12 19:58:09 +00:00
|
|
|
err = windows.UnmapViewOfFile(addr)
|
2018-05-19 12:49:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
handle, ok := handleMap[addr]
|
|
|
|
if !ok {
|
|
|
|
// should be impossible; we would've errored above
|
|
|
|
return errors.New("unknown base address")
|
|
|
|
}
|
|
|
|
delete(handleMap, addr)
|
|
|
|
|
2019-09-12 19:58:09 +00:00
|
|
|
e := windows.CloseHandle(windows.Handle(handle.mapview))
|
2018-05-19 12:49:46 +00:00
|
|
|
return os.NewSyscallError("CloseHandle", e)
|
|
|
|
}
|