Skip to content

Commit b40a321

Browse files
authored
Use x/sys for better OS coverage (#111)
1 parent 5d6fbc0 commit b40a321

File tree

10 files changed

+67
-41
lines changed

10 files changed

+67
-41
lines changed

.github/workflows/ci.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,29 @@ jobs:
8585
# Windows runner seems to not have enough CPU performance to keep up without a memory limit
8686
GOMEMLIMIT: ${{ github.event_name != 'pull_request' && '1GiB' || '' }}
8787

88+
test-bsd:
89+
runs-on: ubuntu-latest
90+
91+
steps:
92+
- uses: actions/checkout@v4
93+
- uses: actions/setup-go@v5
94+
with:
95+
go-version-file: go.work
96+
97+
- name: Build
98+
run: go test -c .
99+
env:
100+
GOOS: freebsd
101+
102+
- name: Test
103+
uses: cross-platform-actions/[email protected]
104+
with:
105+
operating_system: freebsd
106+
version: '14.0'
107+
shell: bash
108+
run: ./go-re2.test -test.v -test.short
109+
sync_files: runner-to-vm
110+
88111
# Runs tests using wazero inside a minimal golang docker image. This makes sure the code builds
89112
# even when there is no C toolchain available. It is possible for code to work fine with CGO_ENABLED=0
90113
# but not build without a C toolchain available, e.g. if C source files are checked into the repo

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.20
55
require (
66
github.com/tetratelabs/wazero v1.7.2
77
github.com/wasilibs/nottinygc v0.4.0
8+
golang.org/x/sys v0.21.0
89
)
910

1011
require github.com/magefile/mage v1.15.1-0.20230912152418-9f54e0f83e2a // indirect

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
github.com/magefile/mage v1.15.1-0.20230912152418-9f54e0f83e2a h1:tdPcGgyiH0K+SbsJBBm2oPyEIOTAvLBwD9TuUwVtZho=
2+
github.com/magefile/mage v1.15.1-0.20230912152418-9f54e0f83e2a/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
23
github.com/tetratelabs/wazero v1.7.2 h1:1+z5nXJNwMLPAWaTePFi49SSTL0IMx/i3Fg8Yc25GDc=
34
github.com/tetratelabs/wazero v1.7.2/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
45
github.com/wasilibs/nottinygc v0.4.0 h1:h1TJMihMC4neN6Zq+WKpLxgd9xCFMw7O9ETLwY2exJQ=
56
github.com/wasilibs/nottinygc v0.4.0/go.mod h1:oDcIotskuYNMpqMF23l7Z8uzD4TC0WXHK8jetlB3HIo=
7+
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
8+
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

internal/alloc/alloc_unix.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ package alloc
2828

2929
import (
3030
"math"
31-
"syscall"
3231

3332
"github.com/tetratelabs/wazero/experimental"
33+
"golang.org/x/sys/unix"
3434
)
3535

3636
func Allocator() experimental.MemoryAllocator {
@@ -39,25 +39,25 @@ func Allocator() experimental.MemoryAllocator {
3939

4040
func mmappedAllocator(cap, max uint64) experimental.LinearMemory {
4141
// Round up to the page size.
42-
rnd := uint64(syscall.Getpagesize() - 1)
42+
rnd := uint64(unix.Getpagesize() - 1)
4343
max = (max + rnd) &^ rnd
4444
cap = (cap + rnd) &^ rnd
4545

4646
if max > math.MaxInt {
4747
// This ensures int(max) overflows to a negative value,
48-
// and syscall.Mmap returns EINVAL.
48+
// and unix.Mmap returns EINVAL.
4949
max = math.MaxUint64
5050
}
5151
// Reserve max bytes of address space, to ensure we won't need to move it.
5252
// A protected, private, anonymous mapping should not commit memory.
53-
b, err := syscall.Mmap(-1, 0, int(max), syscall.PROT_NONE, syscall.MAP_PRIVATE|syscall.MAP_ANON)
53+
b, err := unix.Mmap(-1, 0, int(max), unix.PROT_NONE, unix.MAP_PRIVATE|unix.MAP_ANON)
5454
if err != nil {
5555
panic(err)
5656
}
5757
// Commit the initial cap bytes of memory.
58-
err = syscall.Mprotect(b[:cap], syscall.PROT_READ|syscall.PROT_WRITE)
58+
err = unix.Mprotect(b[:cap], unix.PROT_READ|unix.PROT_WRITE)
5959
if err != nil {
60-
_ = syscall.Munmap(b)
60+
_ = unix.Munmap(b)
6161
panic(err)
6262
}
6363
return &mmappedMemory{buf: b[:cap]}
@@ -75,11 +75,11 @@ func (m *mmappedMemory) Reallocate(size uint64) []byte {
7575
res := uint64(cap(m.buf))
7676
if com < size && size < res {
7777
// Round up to the page size.
78-
rnd := uint64(syscall.Getpagesize() - 1)
78+
rnd := uint64(unix.Getpagesize() - 1)
7979
new := (size + rnd) &^ rnd
8080

8181
// Commit additional memory up to new bytes.
82-
err := syscall.Mprotect(m.buf[com:new], syscall.PROT_READ|syscall.PROT_WRITE)
82+
err := unix.Mprotect(m.buf[com:new], unix.PROT_READ|unix.PROT_WRITE)
8383
if err != nil {
8484
panic(err)
8585
}
@@ -91,7 +91,7 @@ func (m *mmappedMemory) Reallocate(size uint64) []byte {
9191
}
9292

9393
func (m *mmappedMemory) Free() {
94-
err := syscall.Munmap(m.buf[:cap(m.buf)])
94+
err := unix.Munmap(m.buf[:cap(m.buf)])
9595
if err != nil {
9696
panic(err)
9797
}

internal/alloc/alloc_windows.go

+12-28
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,10 @@ package alloc
55
import (
66
"fmt"
77
"math"
8-
"syscall"
98
"unsafe"
109

1110
"github.com/tetratelabs/wazero/experimental"
12-
)
13-
14-
var (
15-
kernel32 = syscall.NewLazyDLL("kernel32.dll")
16-
procVirtualAlloc = kernel32.NewProc("VirtualAlloc")
17-
procVirtualFree = kernel32.NewProc("VirtualFree")
18-
)
19-
20-
const (
21-
windows_MEM_COMMIT uintptr = 0x00001000
22-
windows_MEM_RESERVE uintptr = 0x00002000
23-
windows_MEM_RELEASE uintptr = 0x00008000
24-
windows_PAGE_READWRITE uintptr = 0x00000004
25-
26-
// https://cs.opensource.google/go/x/sys/+/refs/tags/v0.20.0:windows/syscall_windows.go;l=131
27-
windows_PAGE_SIZE uint64 = 4096
11+
"golang.org/x/sys/windows"
2812
)
2913

3014
func Allocator() experimental.MemoryAllocator {
@@ -33,7 +17,7 @@ func Allocator() experimental.MemoryAllocator {
3317

3418
func virtualAllocator(cap, max uint64) experimental.LinearMemory {
3519
// Round up to the page size.
36-
rnd := windows_PAGE_SIZE - 1
20+
rnd := uint64(windows.Getpagesize() - 1)
3721
max = (max + rnd) &^ rnd
3822
cap = (cap + rnd) &^ rnd
3923

@@ -45,15 +29,15 @@ func virtualAllocator(cap, max uint64) experimental.LinearMemory {
4529

4630
// Reserve, but don't commit, max bytes of address space, to ensure we won't need to move it.
4731
// This does not commit memory.
48-
r, _, err := procVirtualAlloc.Call(0, uintptr(max), windows_MEM_RESERVE, windows_PAGE_READWRITE)
49-
if r == 0 {
32+
r, err := windows.VirtualAlloc(0, uintptr(max), windows.MEM_RESERVE, windows.PAGE_READWRITE)
33+
if err != nil {
5034
panic(fmt.Errorf("alloc_windows: failed to reserve memory: %w", err))
5135
}
5236

5337
// Commit the initial cap bytes of memory.
54-
r, _, err = procVirtualAlloc.Call(r, uintptr(cap), windows_MEM_COMMIT, windows_PAGE_READWRITE)
55-
if r == 0 {
56-
_, _, _ = procVirtualFree.Call(r, 0, windows_MEM_RELEASE)
38+
r, err = windows.VirtualAlloc(r, uintptr(cap), windows.MEM_COMMIT, windows.PAGE_READWRITE)
39+
if err != nil {
40+
_ = windows.VirtualFree(r, 0, windows.MEM_RELEASE)
5741
panic(fmt.Errorf("alloc_windows: failed to commit initial memory: %w", err))
5842
}
5943

@@ -74,12 +58,12 @@ func (m *virtualMemory) Reallocate(size uint64) []byte {
7458
res := uint64(cap(m.buf))
7559
if com < size && size < res {
7660
// Round up to the page size.
77-
rnd := windows_PAGE_SIZE - 1
61+
rnd := uint64(windows.Getpagesize() - 1)
7862
new := (size + rnd) &^ rnd
7963

8064
// Commit additional memory up to new bytes.
81-
r, _, err := procVirtualAlloc.Call(m.addr, uintptr(new), windows_MEM_COMMIT, windows_PAGE_READWRITE)
82-
if r == 0 {
65+
_, err := windows.VirtualAlloc(m.addr, uintptr(new), windows.MEM_COMMIT, windows.PAGE_READWRITE)
66+
if err != nil {
8367
panic(fmt.Errorf("alloc_windows: failed to commit memory: %w", err))
8468
}
8569

@@ -91,8 +75,8 @@ func (m *virtualMemory) Reallocate(size uint64) []byte {
9175
}
9276

9377
func (m *virtualMemory) Free() {
94-
r, _, err := procVirtualFree.Call(m.addr, 0, windows_MEM_RELEASE)
95-
if r == 0 {
78+
err := windows.VirtualFree(m.addr, 0, windows.MEM_RELEASE)
79+
if err != nil {
9680
panic(fmt.Errorf("alloc_windows: failed to release memory: %w", err))
9781
}
9882
m.addr = 0

internal/e2e/go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ go 1.20
44

55
require github.com/wasilibs/go-re2 v1.5.2
66

7-
require github.com/tetratelabs/wazero v1.7.2 // indirect
7+
require (
8+
github.com/tetratelabs/wazero v1.7.2 // indirect
9+
golang.org/x/sys v0.21.0 // indirect
10+
)
811

912
replace github.com/wasilibs/go-re2 => ../..

internal/e2e/go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
github.com/magefile/mage v1.15.1-0.20230912152418-9f54e0f83e2a h1:tdPcGgyiH0K+SbsJBBm2oPyEIOTAvLBwD9TuUwVtZho=
22
github.com/tetratelabs/wazero v1.7.2 h1:1+z5nXJNwMLPAWaTePFi49SSTL0IMx/i3Fg8Yc25GDc=
33
github.com/wasilibs/nottinygc v0.4.0 h1:h1TJMihMC4neN6Zq+WKpLxgd9xCFMw7O9ETLwY2exJQ=
4+
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=

stress_test.go

+12
Large diffs are not rendered by default.

wafbench/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ require (
3838
github.com/yargevad/filepathx v1.0.0 // indirect
3939
golang.org/x/crypto v0.1.0 // indirect
4040
golang.org/x/net v0.1.0 // indirect
41-
golang.org/x/sys v0.2.0 // indirect
41+
golang.org/x/sys v0.21.0 // indirect
4242
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
4343
gopkg.in/yaml.v3 v3.0.1 // indirect
4444
)

wafbench/go.sum

+1-2
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,7 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
390390
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
391391
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
392392
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
393-
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
394-
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
393+
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
395394
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
396395
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
397396
golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=

0 commit comments

Comments
 (0)