Skip to content

Commit b0e97b8

Browse files
committed
Readonly shared memory.
1 parent 1c03f8f commit b0e97b8

File tree

5 files changed

+59
-57
lines changed

5 files changed

+59
-57
lines changed

internal/testcfg/testcfg.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ func init() {
1919
path := filepath.Join(os.TempDir(), "wazero")
2020
if err := os.MkdirAll(path, 0777); err == nil {
2121
if cache, err := wazero.NewCompilationCacheWithDir(path); err == nil {
22-
sqlite3.RuntimeConfig.WithCompilationCache(cache)
22+
sqlite3.RuntimeConfig = sqlite3.RuntimeConfig.
23+
WithCompilationCache(cache)
2324
}
2425
}
2526
}

vfs/shm.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,5 @@ func NewSharedMemory(path string, flags OpenFlag) SharedMemory {
2222
if flags&OPEN_MAIN_DB == 0 || flags&(OPEN_DELETEONCLOSE|OPEN_MEMORY) != 0 {
2323
return nil
2424
}
25-
return &vfsShm{
26-
path: path,
27-
readOnly: flags&OPEN_READONLY != 0,
28-
}
25+
return &vfsShm{path: path}
2926
}

vfs/shm_bsd.go

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ type vfsShmFile struct {
1818
*os.File
1919
info os.FileInfo
2020

21-
// +checklocks:vfsShmFilesMtx
22-
refs int
21+
refs int // +checklocks:vfsShmFilesMtx
2322

24-
// +checklocks:Mutex
25-
lock [_SHM_NLOCK]int16
23+
lock [_SHM_NLOCK]int16 // +checklocks:Mutex
2624
sync.Mutex
2725
}
2826

@@ -34,10 +32,9 @@ var (
3432

3533
type vfsShm struct {
3634
*vfsShmFile
37-
path string
38-
lock [_SHM_NLOCK]bool
39-
regions []*util.MappedRegion
40-
readOnly bool
35+
path string
36+
lock [_SHM_NLOCK]bool
37+
regions []*util.MappedRegion
4138
}
4239

4340
func (s *vfsShm) Close() error {
@@ -69,7 +66,7 @@ func (s *vfsShm) Close() error {
6966
panic(util.AssertErr())
7067
}
7168

72-
func (s *vfsShm) shmOpen() (rc _ErrorCode) {
69+
func (s *vfsShm) shmOpen() _ErrorCode {
7370
if s.vfsShmFile != nil {
7471
return _OK
7572
}
@@ -100,17 +97,13 @@ func (s *vfsShm) shmOpen() (rc _ErrorCode) {
10097
}
10198
}
10299

103-
// Lock and truncate the file, if not readonly.
100+
// Lock and truncate the file.
104101
// The lock is only released by closing the file.
105-
if s.readOnly {
106-
rc = _READONLY_CANTINIT
107-
} else {
108-
if rc := osLock(f, unix.LOCK_EX|unix.LOCK_NB, _IOERR_LOCK); rc != _OK {
109-
return rc
110-
}
111-
if err := f.Truncate(0); err != nil {
112-
return _IOERR_SHMOPEN
113-
}
102+
if rc := osLock(f, unix.LOCK_EX|unix.LOCK_NB, _IOERR_LOCK); rc != _OK {
103+
return rc
104+
}
105+
if err := f.Truncate(0); err != nil {
106+
return _IOERR_SHMOPEN
114107
}
115108

116109
// Add the new shared file.
@@ -122,11 +115,11 @@ func (s *vfsShm) shmOpen() (rc _ErrorCode) {
122115
for i, g := range vfsShmFiles {
123116
if g == nil {
124117
vfsShmFiles[i] = s.vfsShmFile
125-
return rc
118+
return _OK
126119
}
127120
}
128121
vfsShmFiles = append(vfsShmFiles, s.vfsShmFile)
129-
return rc
122+
return _OK
130123
}
131124

132125
func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, extend bool) (uint32, _ErrorCode) {
@@ -148,25 +141,17 @@ func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, ext
148141
if !extend {
149142
return 0, _OK
150143
}
151-
if s.readOnly || osAllocate(s.File, n) != nil {
144+
if osAllocate(s.File, n) != nil {
152145
return 0, _IOERR_SHMSIZE
153146
}
154147
}
155148

156-
var prot int
157-
if s.readOnly {
158-
prot = unix.PROT_READ
159-
} else {
160-
prot = unix.PROT_READ | unix.PROT_WRITE
161-
}
162-
r, err := util.MapRegion(ctx, mod, s.File, int64(id)*int64(size), size, prot)
149+
r, err := util.MapRegion(ctx, mod, s.File, int64(id)*int64(size), size,
150+
unix.PROT_READ|unix.PROT_WRITE)
163151
if err != nil {
164152
return 0, _IOERR_SHMMAP
165153
}
166154
s.regions = append(s.regions, r)
167-
if s.readOnly {
168-
return r.Ptr, _READONLY
169-
}
170155
return r.Ptr, _OK
171156
}
172157

vfs/shm_copy.go renamed to vfs/shm_dotlk.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ package vfs
44

55
import (
66
"context"
7+
"errors"
8+
"io/fs"
9+
"os"
710
"sync"
811
"unsafe"
912

@@ -29,15 +32,14 @@ var (
2932

3033
type vfsShm struct {
3134
*vfsShmBuffer
32-
mod api.Module
33-
alloc api.Function
34-
free api.Function
35-
path string
36-
shadow []byte
37-
ptrs []uint32
38-
stack [1]uint64
39-
lock [_SHM_NLOCK]bool
40-
readOnly bool
35+
mod api.Module
36+
alloc api.Function
37+
free api.Function
38+
path string
39+
shadow []byte
40+
ptrs []uint32
41+
stack [1]uint64
42+
lock [_SHM_NLOCK]bool
4143
}
4244

4345
func (s *vfsShm) Close() error {
@@ -58,13 +60,18 @@ func (s *vfsShm) Close() error {
5860
return nil
5961
}
6062

63+
err := os.Remove(s.path)
64+
if err != nil && !errors.Is(err, fs.ErrNotExist) {
65+
return _IOERR_UNLOCK
66+
}
6167
delete(vfsShmBuffers, s.path)
68+
s.vfsShmBuffer = nil
6269
return nil
6370
}
6471

65-
func (s *vfsShm) shmOpen() {
72+
func (s *vfsShm) shmOpen() _ErrorCode {
6673
if s.vfsShmBuffer != nil {
67-
return
74+
return _OK
6875
}
6976

7077
vfsShmBuffersMtx.Lock()
@@ -74,12 +81,23 @@ func (s *vfsShm) shmOpen() {
7481
if g, ok := vfsShmBuffers[s.path]; ok {
7582
s.vfsShmBuffer = g
7683
g.refs++
77-
return
84+
return _OK
85+
}
86+
87+
// Create a directory on disk to ensure only this process
88+
// uses this path to register a shared memory.
89+
err := os.Mkdir(s.path, 0777)
90+
if errors.Is(err, fs.ErrExist) {
91+
return _BUSY
92+
}
93+
if err != nil {
94+
return _IOERR_LOCK
7895
}
7996

8097
// Add the new shared buffer.
8198
s.vfsShmBuffer = &vfsShmBuffer{}
8299
vfsShmBuffers[s.path] = s.vfsShmBuffer
100+
return _OK
83101
}
84102

85103
func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, extend bool) (uint32, _ErrorCode) {
@@ -91,8 +109,10 @@ func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, ext
91109
s.free = mod.ExportedFunction("sqlite3_free")
92110
s.alloc = mod.ExportedFunction("sqlite3_malloc64")
93111
}
112+
if rc := s.shmOpen(); rc != _OK {
113+
return 0, rc
114+
}
94115

95-
s.shmOpen()
96116
s.Lock()
97117
defer s.Unlock()
98118
defer s.shmAcquire()

vfs/shm_ofd.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ var _ blockingSharedMemory = &vfsShm{}
2828

2929
func (s *vfsShm) shmOpen() _ErrorCode {
3030
if s.File == nil {
31-
var flag int
32-
if s.readOnly {
33-
flag = unix.O_RDONLY
34-
} else {
35-
flag = unix.O_RDWR
36-
}
3731
f, err := os.OpenFile(s.path,
38-
flag|unix.O_CREAT|unix.O_NOFOLLOW, 0666)
32+
unix.O_RDWR|unix.O_CREAT|unix.O_NOFOLLOW, 0666)
33+
if err != nil {
34+
f, err = os.OpenFile(s.path,
35+
unix.O_CREAT|unix.O_NOFOLLOW, 0666)
36+
s.readOnly = true
37+
}
3938
if err != nil {
4039
return _CANTOPEN
4140
}

0 commit comments

Comments
 (0)