-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- enabled by default on 64-bit macOS and Linux (`amd64`/`arm64`) - depends on merged but unreleased wazero - may cause small performance regression - users may need WithMemoryLimitPages if not enough address space available - needs docs
- Loading branch information
Showing
40 changed files
with
709 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
free | ||
malloc | ||
malloc_destructor | ||
aligned_alloc | ||
sqlite3_anycollseq_init | ||
sqlite3_backup_finish | ||
sqlite3_backup_init | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= | ||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= | ||
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= | ||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= | ||
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= | ||
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q= | ||
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk= | ||
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= | ||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//go:build unix | ||
|
||
package util | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/tetratelabs/wazero/experimental" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func mmappedAllocator(min, cap, max uint64) experimental.MemoryBuffer { | ||
// Round up to the page size. | ||
rnd := uint64(unix.Getpagesize() - 1) | ||
max = (max + rnd) &^ rnd | ||
cap = (cap + rnd) &^ rnd | ||
|
||
if max > math.MaxInt { | ||
// This ensures int(max) overflows to a negative value, | ||
// and unix.Mmap returns EINVAL. | ||
max = math.MaxUint64 | ||
} | ||
// Reserve max bytes of address space, to ensure we won't need to move it. | ||
// A protected, private, anonymous mapping should not commit memory. | ||
b, err := unix.Mmap(-1, 0, int(max), unix.PROT_NONE, unix.MAP_PRIVATE|unix.MAP_ANON) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// Commit the initial cap bytes of memory. | ||
err = unix.Mprotect(b[:cap], unix.PROT_READ|unix.PROT_WRITE) | ||
if err != nil { | ||
unix.Munmap(b) | ||
panic(err) | ||
} | ||
return &mmappedBuffer{ | ||
buf: b[:cap], | ||
cur: min, | ||
} | ||
} | ||
|
||
// The slice covers the entire mmapped memory: | ||
// - len(buf) is the already committed memory, | ||
// - cap(buf) is the reserved address space, | ||
// - cur is the already requested size. | ||
type mmappedBuffer struct { | ||
buf []byte | ||
cur uint64 | ||
} | ||
|
||
func (m *mmappedBuffer) Buffer() []byte { | ||
// Limit capacity because bytes beyond len(m.buf) | ||
// have not yet been committed. | ||
return m.buf[:m.cur:len(m.buf)] | ||
} | ||
|
||
func (m *mmappedBuffer) Grow(size uint64) []byte { | ||
if com := uint64(len(m.buf)); com < size { | ||
// Round up to the page size. | ||
rnd := uint64(unix.Getpagesize() - 1) | ||
new := (size + rnd) &^ rnd | ||
|
||
// Commit additional memory up to new bytes. | ||
err := unix.Mprotect(m.buf[com:new], unix.PROT_READ|unix.PROT_WRITE) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Update commited memory. | ||
m.buf = m.buf[:new] | ||
} | ||
m.cur = size | ||
return m.Buffer() | ||
} | ||
|
||
func (m *mmappedBuffer) Free() { | ||
err := unix.Munmap(m.buf[:cap(m.buf)]) | ||
if err != nil { | ||
panic(err) | ||
} | ||
m.buf = nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
//go:build (linux || darwin) && (amd64 || arm64) && !sqlite3_flock && !sqlite3_noshm && !sqlite3_nosys | ||
|
||
package util | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"unsafe" | ||
|
||
"github.com/tetratelabs/wazero/api" | ||
"github.com/tetratelabs/wazero/experimental" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
type mmapState struct { | ||
regions []*MappedRegion | ||
enabled bool | ||
} | ||
|
||
func (s *mmapState) init(ctx context.Context, enabled bool) context.Context { | ||
if s.enabled = enabled; enabled { | ||
return experimental.WithMemoryAllocator(ctx, mmappedAllocator) | ||
} | ||
return ctx | ||
} | ||
|
||
func CanMap(ctx context.Context) bool { | ||
s := ctx.Value(moduleKey{}).(*moduleState) | ||
return s.mmapState.enabled | ||
} | ||
|
||
func (s *mmapState) new(ctx context.Context, mod api.Module, size uint32) *MappedRegion { | ||
// Find unused region. | ||
for _, r := range s.regions { | ||
if !r.used && r.size == size { | ||
return r | ||
} | ||
} | ||
|
||
// Allocate page aligned memmory. | ||
alloc := mod.ExportedFunction("aligned_alloc") | ||
stack := [2]uint64{ | ||
uint64(unix.Getpagesize()), | ||
uint64(size), | ||
} | ||
if err := alloc.CallWithStack(ctx, stack[:]); err != nil { | ||
panic(err) | ||
} | ||
if stack[0] == 0 { | ||
panic(OOMErr) | ||
} | ||
|
||
// Save the newly allocated region. | ||
ptr := uint32(stack[0]) | ||
buf := View(mod, ptr, uint64(size)) | ||
addr := uintptr(unsafe.Pointer(&buf[0])) | ||
s.regions = append(s.regions, &MappedRegion{ | ||
Ptr: ptr, | ||
addr: addr, | ||
size: size, | ||
}) | ||
return s.regions[len(s.regions)-1] | ||
} | ||
|
||
type MappedRegion struct { | ||
addr uintptr | ||
Ptr uint32 | ||
size uint32 | ||
used bool | ||
} | ||
|
||
func MapRegion(ctx context.Context, mod api.Module, f *os.File, offset int64, size uint32) (*MappedRegion, error) { | ||
s := ctx.Value(moduleKey{}).(*moduleState) | ||
r := s.new(ctx, mod, size) | ||
err := r.mmap(f, offset) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return r, nil | ||
} | ||
|
||
func (r *MappedRegion) Unmap() error { | ||
// We can't munmap the region, otherwise it could be remaped. | ||
// Instead, convert it to a protected, private, anonymous mapping. | ||
// If successful, it can be reused for a subsequent mmap. | ||
_, err := mmap(r.addr, uintptr(r.size), | ||
unix.PROT_NONE, unix.MAP_PRIVATE|unix.MAP_ANON|unix.MAP_FIXED, | ||
-1, 0) | ||
r.used = err != nil | ||
return err | ||
} | ||
|
||
func (r *MappedRegion) mmap(f *os.File, offset int64) error { | ||
_, err := mmap(r.addr, uintptr(r.size), | ||
unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED|unix.MAP_FIXED, | ||
int(f.Fd()), offset) | ||
r.used = err == nil | ||
return err | ||
} | ||
|
||
//go:linkname mmap syscall.mmap | ||
func mmap(addr, length uintptr, prot, flag, fd int, pos int64) (*byte, error) | ||
|
||
//go:linkname munmap syscall.munmap | ||
func munmap(addr, length uintptr) error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//go:build !(linux || darwin) || !(amd64 || arm64) || sqlite3_flock || sqlite3_noshm || sqlite3_nosys | ||
|
||
package util | ||
|
||
import "context" | ||
|
||
type mmapState struct{} | ||
|
||
func (s *mmapState) init(ctx context.Context, _ bool) context.Context { | ||
return ctx | ||
} | ||
|
||
func CanMap(ctx context.Context) bool { | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/tetratelabs/wazero/experimental" | ||
) | ||
|
||
type moduleKey struct{} | ||
type moduleState struct { | ||
handleState | ||
mmapState | ||
} | ||
|
||
func NewContext(ctx context.Context, mappableMemory bool) context.Context { | ||
state := new(moduleState) | ||
ctx = context.WithValue(ctx, moduleKey{}, state) | ||
ctx = experimental.WithCloseNotifier(ctx, state) | ||
ctx = state.mmapState.init(ctx, mappableMemory) | ||
return ctx | ||
} |
Oops, something went wrong.