Skip to content

Commit 5e73c5d

Browse files
committed
Issue #330.
1 parent 6d92aa1 commit 5e73c5d

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

internal/util/mmap_windows.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package util
22

33
import (
4-
"context"
54
"os"
65
"reflect"
76
"unsafe"
87

9-
"github.com/tetratelabs/wazero/api"
108
"golang.org/x/sys/windows"
119
)
1210

@@ -16,14 +14,21 @@ type MappedRegion struct {
1614
addr uintptr
1715
}
1816

19-
func MapRegion(ctx context.Context, mod api.Module, f *os.File, offset int64, size int32) (*MappedRegion, error) {
20-
h, err := windows.CreateFileMapping(windows.Handle(f.Fd()), nil, windows.PAGE_READWRITE, 0, 0, nil)
17+
func MapRegion(f *os.File, offset int64, size int32) (*MappedRegion, error) {
18+
maxSize := offset + int64(size)
19+
h, err := windows.CreateFileMapping(
20+
windows.Handle(f.Fd()), nil, windows.PAGE_READWRITE,
21+
uint32(maxSize>>32), uint32(maxSize), nil)
2122
if h == 0 {
2223
return nil, err
2324
}
2425

26+
const allocationGranularity = 64 * 1024
27+
align := offset % allocationGranularity
28+
offset -= align
29+
2530
a, err := windows.MapViewOfFile(h, windows.FILE_MAP_WRITE,
26-
uint32(offset>>32), uint32(offset), uintptr(size))
31+
uint32(offset>>32), uint32(offset), uintptr(size)+uintptr(align))
2732
if a == 0 {
2833
windows.CloseHandle(h)
2934
return nil, err
@@ -32,9 +37,9 @@ func MapRegion(ctx context.Context, mod api.Module, f *os.File, offset int64, si
3237
ret := &MappedRegion{Handle: h, addr: a}
3338
// SliceHeader, although deprecated, avoids a go vet warning.
3439
sh := (*reflect.SliceHeader)(unsafe.Pointer(&ret.Data))
40+
sh.Data = a + uintptr(align)
3541
sh.Len = int(size)
3642
sh.Cap = int(size)
37-
sh.Data = a
3843
return ret, nil
3944
}
4045

vfs/shm_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, ext
9898

9999
// Maps regions into memory.
100100
for int(id) >= len(s.shared) {
101-
r, err := util.MapRegion(ctx, mod, s.File, int64(id)*int64(size), size)
101+
r, err := util.MapRegion(s.File, int64(id)*int64(size), size)
102102
if err != nil {
103103
return 0, err
104104
}

vfs/vfs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ func vfsErrorCode(ctx context.Context, err error, code _ErrorCode) _ErrorCode {
470470
switch v := reflect.ValueOf(err); v.Kind() {
471471
case reflect.Uint8, reflect.Uint16:
472472
code = _ErrorCode(v.Uint())
473+
default:
474+
sys = err
473475
}
474476
}
475477

0 commit comments

Comments
 (0)