Skip to content

Commit

Permalink
Merge pull request #10 from kellypleahy/bugfix/Sync-wrong-file-handle
Browse files Browse the repository at this point in the history
Fix issue where Sync was passing the wrong file handle.
  • Loading branch information
tysonmote authored Mar 29, 2022
2 parents 078b7ad + 9f78a49 commit 237f44f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 9 additions & 0 deletions gommap_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,12 @@ func (s *S) TestLock(c *C) {
err = mmap.Unlock()
c.Assert(err, IsNil)
}

func (s *S) TestSync(c *C) {
mmap, err := Map(s.file.Fd(), PROT_READ|PROT_WRITE, MAP_PRIVATE)
c.Assert(err, IsNil)
defer mmap.UnsafeUnmap()

err = mmap.Sync(MS_SYNC)
c.Assert(err, IsNil)
}
8 changes: 6 additions & 2 deletions mmap_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
// We keep this map so that we can get back the original handle from the memory address.
var handleLock sync.Mutex
var handleMap = map[uintptr]syscall.Handle{}
var fileHandleMap = map[uintptr]syscall.Handle{}
var addrLocked = map[uintptr]bool{}

func mmap(len int64, prot, flags, hfile uintptr, off int64) ([]byte, error) {
Expand All @@ -48,7 +49,8 @@ func mmap(len int64, prot, flags, hfile uintptr, off int64) ([]byte, error) {
maxSizeHigh := uint32((off + len) >> 32)
maxSizeLow := uint32((off + len) & 0xFFFFFFFF)
// TODO: Do we need to set some security attributes? It might help portability.
h, errno := syscall.CreateFileMapping(syscall.Handle(hfile), nil, flProtect, maxSizeHigh, maxSizeLow, nil)
fileHandle := syscall.Handle(hfile)
h, errno := syscall.CreateFileMapping(fileHandle, nil, flProtect, maxSizeHigh, maxSizeLow, nil)
if h == 0 {
if errno == syscall.ERROR_ACCESS_DENIED {
return nil, syscall.EACCES
Expand All @@ -66,6 +68,7 @@ func mmap(len int64, prot, flags, hfile uintptr, off int64) ([]byte, error) {
}
handleLock.Lock()
handleMap[addr] = h
fileHandleMap[addr] = fileHandle
handleLock.Unlock()

m := MMap{}
Expand All @@ -85,7 +88,7 @@ func flush(addr, len uintptr) error {

handleLock.Lock()
defer handleLock.Unlock()
handle, ok := handleMap[addr]
handle, ok := fileHandleMap[addr]
if !ok {
// should be impossible; we would've errored above
return errors.New("unknown base address")
Expand Down Expand Up @@ -137,6 +140,7 @@ func unmap(addr, len uintptr) error {
return errors.New("unknown base address")
}
delete(handleMap, addr)
delete(fileHandleMap, addr)

e := syscall.CloseHandle(syscall.Handle(handle))
return os.NewSyscallError("CloseHandle", e)
Expand Down

0 comments on commit 237f44f

Please sign in to comment.