From 2a6b0bfc296b30f2970dbf6aab68124908cd6882 Mon Sep 17 00:00:00 2001 From: database64128 Date: Mon, 20 Jan 2025 14:52:08 +0800 Subject: [PATCH] Modernize Windows mmap code (again) Closes #162. --- LICENSE | 29 --------------- README.md | 3 +- mmap_windows.go | 94 +++++++++++-------------------------------------- 3 files changed, 21 insertions(+), 105 deletions(-) diff --git a/LICENSE b/LICENSE index 63c80cc..2969677 100644 --- a/LICENSE +++ b/LICENSE @@ -13,32 +13,3 @@ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - --------------------------------------------------------------------- - -The file mmap_windows.go is governed by the following license: - -Copyright (c) 2011, Evan Shaw -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - * Neither the name of the copyright holder nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY -DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md index 0f35fce..84ca876 100644 --- a/README.md +++ b/README.md @@ -33,5 +33,4 @@ with your changes. ## License ## -This is free software, primarily licensed under the ISC License. The file -`mmap_windows.go` is licensed under a BSD-style license by Evan Shaw. +This is free software, primarily licensed under the ISC License. diff --git a/mmap_windows.go b/mmap_windows.go index 62f5fde..9e5a8e0 100644 --- a/mmap_windows.go +++ b/mmap_windows.go @@ -3,48 +3,14 @@ package maxminddb -// Windows support largely borrowed from mmap-go. -// -// Copyright (c) 2011, Evan Shaw -// All rights reserved. - -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in the -// documentation and/or other materials provided with the distribution. -// * Neither the name of the copyright holder nor the -// names of its contributors may be used to endorse or promote products -// derived from this software without specific prior written permission. - -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY -// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - import ( "errors" "os" - "sync" "unsafe" "golang.org/x/sys/windows" ) -// Windows -var ( - handleLock sync.Mutex - handleMap = map[uintptr]windows.Handle{} -) - // mmap maps a file into memory and returns a byte slice. func mmap(fd int, length int) ([]byte, error) { // Create a file mapping @@ -53,42 +19,43 @@ func mmap(fd int, length int) ([]byte, error) { nil, windows.PAGE_READONLY, 0, - uint32(length), + 0, nil, ) if err != nil { return nil, os.NewSyscallError("CreateFileMapping", err) } + defer windows.CloseHandle(handle) // Map the file into memory - addr, err := windows.MapViewOfFile( + addrUintptr, err := windows.MapViewOfFile( handle, windows.FILE_MAP_READ, 0, 0, - uintptr(length), + 0, ) if err != nil { - windows.CloseHandle(handle) return nil, os.NewSyscallError("MapViewOfFile", err) } - // Store the handle in the map - handleLock.Lock() - handleMap[addr] = handle - handleLock.Unlock() - - data := unsafe.Slice((*byte)(unsafe.Pointer(addr)), length) - return data, nil -} - -// flush ensures changes to a memory-mapped region are written to disk. -func flush(addr, length uintptr) error { - err := windows.FlushViewOfFile(addr, length) - if err != nil { - return os.NewSyscallError("FlushViewOfFile", err) + // When there's not enough address space for the whole file (e.g. large + // files on 32-bit systems), MapViewOfFile may return a partial mapping. + // Query the region size and fail on partial mappings. + var info windows.MemoryBasicInformation + if err := windows.VirtualQuery(addrUintptr, &info, unsafe.Sizeof(info)); err != nil { + _ = windows.UnmapViewOfFile(addrUintptr) + return nil, os.NewSyscallError("VirtualQuery", err) } - return nil + if info.RegionSize < uintptr(length) { + _ = windows.UnmapViewOfFile(addrUintptr) + return nil, errors.New("file too large") + } + + // Workaround for unsafeptr check in go vet, see + // https://github.com/golang/go/issues/58625 + addr := *(*unsafe.Pointer)(unsafe.Pointer(&addrUintptr)) + return unsafe.Slice((*byte)(addr), length), nil } // munmap unmaps a memory-mapped file and releases associated resources. @@ -96,31 +63,10 @@ func munmap(b []byte) error { // Convert slice to base address and length data := unsafe.SliceData(b) addr := uintptr(unsafe.Pointer(data)) - length := uintptr(len(b)) - - // Flush the memory region - if err := flush(addr, length); err != nil { - return err - } // Unmap the memory if err := windows.UnmapViewOfFile(addr); err != nil { return os.NewSyscallError("UnmapViewOfFile", err) } - - // Remove the handle from the map and close it - 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") - } - delete(handleMap, addr) - - if err := windows.CloseHandle(handle); err != nil { - return os.NewSyscallError("CloseHandle", err) - } return nil }