Skip to content

Commit

Permalink
Fix crashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Apr 4, 2024
1 parent a031df4 commit 761b3b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
6 changes: 1 addition & 5 deletions internal/util/mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ func MapRegion(ctx context.Context, mod api.Module, f *os.File, offset int64, si
}

func (r *MappedRegion) Close() error {
if addr := r.addr; addr != 0 {
r.addr = 0
return munmap(addr, uintptr(r.size))
}
return nil
return munmap(r.addr, uintptr(r.size))
}

func (r *MappedRegion) Unmap() error {
Expand Down
12 changes: 12 additions & 0 deletions tests/wal_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
package tests

import (
"os"
"path/filepath"
"testing"

"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/vfs"
"github.com/tetratelabs/wazero"
)

func TestMain(m *testing.M) {
if vfs.SupportsSharedMemory {
sqlite3.RuntimeConfig = wazero.NewRuntimeConfig().
WithMemoryCapacityFromMax(true).
WithMemoryLimitPages(1024)
}
os.Exit(m.Run())
}

func TestWAL_enter_exit(t *testing.T) {
t.Parallel()

Expand Down
14 changes: 5 additions & 9 deletions vfs/shm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"context"
"io"
"os"
"unsafe"
"reflect"

"github.com/ncruces/go-sqlite3/internal/util"
"github.com/tetratelabs/wazero/api"
Expand All @@ -24,20 +24,16 @@ import (
const SupportsSharedMemory = true

func vfsVersion(mod api.Module) uint32 {
pagesize := unix.Getpagesize()

// 32KB pages must be a multiple of the system's page size.
if (32*1024)%pagesize != 0 {
if (32*1024)%unix.Getpagesize() != 0 {
return 0
}

// The module's memory must be page aligned.
b, ok := mod.Memory().Read(0, 1)
if ok && uintptr(unsafe.Pointer(&b[0]))%uintptr(pagesize) != 0 {
// Memory must have been mmaped.
if reflect.ValueOf(mod.Memory()).Elem().FieldByName("mmappedBuffer").IsNil() {
return 0
}

return 1 // TODO: feeling lucky?
return 1
}

type vfsShm struct {
Expand Down

0 comments on commit 761b3b5

Please sign in to comment.