| 1 |
package wasi_snapshot_preview1_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
_ "embed" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"io/fs" |
| 9 |
"math" |
| 10 |
"os" |
| 11 |
"path" |
| 12 |
"testing" |
| 13 |
gofstest "testing/fstest" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/tetratelabs/wazero" |
| 17 |
"github.com/tetratelabs/wazero/api" |
| 18 |
experimentalsys "github.com/tetratelabs/wazero/experimental/sys" |
| 19 |
"github.com/tetratelabs/wazero/internal/fstest" |
| 20 |
"github.com/tetratelabs/wazero/internal/platform" |
| 21 |
"github.com/tetratelabs/wazero/internal/sys" |
| 22 |
"github.com/tetratelabs/wazero/internal/sysfs" |
| 23 |
"github.com/tetratelabs/wazero/internal/testing/require" |
| 24 |
"github.com/tetratelabs/wazero/internal/u64" |
| 25 |
"github.com/tetratelabs/wazero/internal/wasip1" |
| 26 |
"github.com/tetratelabs/wazero/internal/wasm" |
| 27 |
sysapi "github.com/tetratelabs/wazero/sys" |
| 28 |
) |
| 29 |
|
| 30 |
func Test_fdAdvise(t *testing.T) { |
| 31 |
mod, r, _ := requireProxyModule(t, wazero.NewModuleConfig().WithFS(fstest.FS)) |
| 32 |
defer r.Close(testCtx) |
| 33 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdAdviseName, uint64(3), 0, 0, uint64(wasip1.FdAdviceNormal)) |
| 34 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdAdviseName, uint64(3), 0, 0, uint64(wasip1.FdAdviceSequential)) |
| 35 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdAdviseName, uint64(3), 0, 0, uint64(wasip1.FdAdviceRandom)) |
| 36 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdAdviseName, uint64(3), 0, 0, uint64(wasip1.FdAdviceWillNeed)) |
| 37 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdAdviseName, uint64(3), 0, 0, uint64(wasip1.FdAdviceDontNeed)) |
| 38 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdAdviseName, uint64(3), 0, 0, uint64(wasip1.FdAdviceNoReuse)) |
| 39 |
requireErrnoResult(t, wasip1.ErrnoInval, mod, wasip1.FdAdviseName, uint64(3), 0, 0, uint64(wasip1.FdAdviceNoReuse+1)) |
| 40 |
requireErrnoResult(t, wasip1.ErrnoBadf, mod, wasip1.FdAdviseName, uint64(1111111), 0, 0, uint64(wasip1.FdAdviceNoReuse+1)) |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
func Test_fdAllocate(t *testing.T) { |
| 45 |
tmpDir := t.TempDir() |
| 46 |
const fileName = "file.txt" |
| 47 |
|
| 48 |
|
| 49 |
realPath := joinPath(tmpDir, fileName) |
| 50 |
require.NoError(t, os.WriteFile(realPath, []byte("0123456789"), 0o600)) |
| 51 |
|
| 52 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFSConfig( |
| 53 |
wazero.NewFSConfig().WithDirMount(tmpDir, "/"), |
| 54 |
)) |
| 55 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 56 |
preopen := getPreopen(t, fsc) |
| 57 |
defer r.Close(testCtx) |
| 58 |
|
| 59 |
fd, errno := fsc.OpenFile(preopen, fileName, experimentalsys.O_RDWR, 0) |
| 60 |
require.EqualErrno(t, 0, errno) |
| 61 |
|
| 62 |
f, ok := fsc.LookupFile(fd) |
| 63 |
require.True(t, ok) |
| 64 |
|
| 65 |
requireSizeEqual := func(exp int64) { |
| 66 |
st, errno := f.File.Stat() |
| 67 |
require.EqualErrno(t, 0, errno) |
| 68 |
require.Equal(t, exp, st.Size) |
| 69 |
} |
| 70 |
|
| 71 |
t.Run("errors", func(t *testing.T) { |
| 72 |
requireErrnoResult(t, wasip1.ErrnoBadf, mod, wasip1.FdAllocateName, uint64(12345), 0, 0) |
| 73 |
minusOne := int64(-1) |
| 74 |
requireErrnoResult(t, wasip1.ErrnoInval, mod, wasip1.FdAllocateName, uint64(fd), uint64(minusOne), uint64(minusOne)) |
| 75 |
requireErrnoResult(t, wasip1.ErrnoInval, mod, wasip1.FdAllocateName, uint64(fd), 0, uint64(minusOne)) |
| 76 |
requireErrnoResult(t, wasip1.ErrnoInval, mod, wasip1.FdAllocateName, uint64(fd), uint64(minusOne), 0) |
| 77 |
}) |
| 78 |
|
| 79 |
t.Run("do not change size", func(t *testing.T) { |
| 80 |
for _, tc := range []struct{ offset, length uint64 }{ |
| 81 |
{offset: 0, length: 10}, |
| 82 |
{offset: 5, length: 5}, |
| 83 |
{offset: 4, length: 0}, |
| 84 |
{offset: 10, length: 0}, |
| 85 |
} { |
| 86 |
|
| 87 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdAllocateName, |
| 88 |
uint64(fd), tc.offset, tc.length) |
| 89 |
requireSizeEqual(10) |
| 90 |
} |
| 91 |
}) |
| 92 |
|
| 93 |
t.Run("increase", func(t *testing.T) { |
| 94 |
|
| 95 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdAllocateName, |
| 96 |
uint64(fd), 10, 10) |
| 97 |
requireSizeEqual(20) |
| 98 |
|
| 99 |
|
| 100 |
buf, err := os.ReadFile(realPath) |
| 101 |
require.NoError(t, err) |
| 102 |
require.Equal(t, "0123456789", string(buf[:10])) |
| 103 |
}) |
| 104 |
|
| 105 |
require.Equal(t, ` |
| 106 |
==> wasi_snapshot_preview1.fd_allocate(fd=12345,offset=0,len=0) |
| 107 |
<== errno=EBADF |
| 108 |
==> wasi_snapshot_preview1.fd_allocate(fd=4,offset=-1,len=-1) |
| 109 |
<== errno=EINVAL |
| 110 |
==> wasi_snapshot_preview1.fd_allocate(fd=4,offset=0,len=-1) |
| 111 |
<== errno=EINVAL |
| 112 |
==> wasi_snapshot_preview1.fd_allocate(fd=4,offset=-1,len=0) |
| 113 |
<== errno=EINVAL |
| 114 |
==> wasi_snapshot_preview1.fd_allocate(fd=4,offset=0,len=10) |
| 115 |
<== errno=ESUCCESS |
| 116 |
==> wasi_snapshot_preview1.fd_allocate(fd=4,offset=5,len=5) |
| 117 |
<== errno=ESUCCESS |
| 118 |
==> wasi_snapshot_preview1.fd_allocate(fd=4,offset=4,len=0) |
| 119 |
<== errno=ESUCCESS |
| 120 |
==> wasi_snapshot_preview1.fd_allocate(fd=4,offset=10,len=0) |
| 121 |
<== errno=ESUCCESS |
| 122 |
==> wasi_snapshot_preview1.fd_allocate(fd=4,offset=10,len=10) |
| 123 |
<== errno=ESUCCESS |
| 124 |
`, "\n"+log.String()) |
| 125 |
} |
| 126 |
|
| 127 |
func getPreopen(t *testing.T, fsc *sys.FSContext) experimentalsys.FS { |
| 128 |
preopen, ok := fsc.LookupFile(sys.FdPreopen) |
| 129 |
require.True(t, ok) |
| 130 |
return preopen.FS |
| 131 |
} |
| 132 |
|
| 133 |
func Test_fdClose(t *testing.T) { |
| 134 |
|
| 135 |
path1, path2 := "dir/-", "dir/a-" |
| 136 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(fstest.FS)) |
| 137 |
defer r.Close(testCtx) |
| 138 |
|
| 139 |
|
| 140 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 141 |
preopen := getPreopen(t, fsc) |
| 142 |
|
| 143 |
fdToClose, errno := fsc.OpenFile(preopen, path1, experimentalsys.O_RDONLY, 0) |
| 144 |
require.EqualErrno(t, 0, errno) |
| 145 |
|
| 146 |
fdToKeep, errno := fsc.OpenFile(preopen, path2, experimentalsys.O_RDONLY, 0) |
| 147 |
require.EqualErrno(t, 0, errno) |
| 148 |
|
| 149 |
|
| 150 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdCloseName, uint64(fdToClose)) |
| 151 |
require.Equal(t, ` |
| 152 |
==> wasi_snapshot_preview1.fd_close(fd=4) |
| 153 |
<== errno=ESUCCESS |
| 154 |
`, "\n"+log.String()) |
| 155 |
|
| 156 |
|
| 157 |
_, ok := fsc.LookupFile(fdToClose) |
| 158 |
require.False(t, ok) |
| 159 |
|
| 160 |
|
| 161 |
_, ok = fsc.LookupFile(fdToKeep) |
| 162 |
require.True(t, ok) |
| 163 |
|
| 164 |
log.Reset() |
| 165 |
t.Run("ErrnoBadF for an invalid FD", func(t *testing.T) { |
| 166 |
requireErrnoResult(t, wasip1.ErrnoBadf, mod, wasip1.FdCloseName, uint64(42)) |
| 167 |
require.Equal(t, ` |
| 168 |
==> wasi_snapshot_preview1.fd_close(fd=42) |
| 169 |
<== errno=EBADF |
| 170 |
`, "\n"+log.String()) |
| 171 |
}) |
| 172 |
log.Reset() |
| 173 |
t.Run("Can close a pre-open", func(t *testing.T) { |
| 174 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdCloseName, uint64(sys.FdPreopen)) |
| 175 |
require.Equal(t, ` |
| 176 |
==> wasi_snapshot_preview1.fd_close(fd=3) |
| 177 |
<== errno=ESUCCESS |
| 178 |
`, "\n"+log.String()) |
| 179 |
}) |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
func Test_fdDatasync(t *testing.T) { |
| 184 |
tmpDir := t.TempDir() |
| 185 |
pathName := "test_path" |
| 186 |
mod, fd, log, r := requireOpenFile(t, tmpDir, pathName, []byte{}, false) |
| 187 |
defer r.Close(testCtx) |
| 188 |
|
| 189 |
tests := []struct { |
| 190 |
name string |
| 191 |
fd int32 |
| 192 |
expectedErrno wasip1.Errno |
| 193 |
expectedLog string |
| 194 |
}{ |
| 195 |
{ |
| 196 |
name: "invalid FD", |
| 197 |
fd: 42, |
| 198 |
expectedErrno: wasip1.ErrnoBadf, |
| 199 |
expectedLog: ` |
| 200 |
==> wasi_snapshot_preview1.fd_datasync(fd=42) |
| 201 |
<== errno=EBADF |
| 202 |
`, |
| 203 |
}, |
| 204 |
{ |
| 205 |
name: "valid FD", |
| 206 |
fd: fd, |
| 207 |
expectedErrno: wasip1.ErrnoSuccess, |
| 208 |
expectedLog: ` |
| 209 |
==> wasi_snapshot_preview1.fd_datasync(fd=4) |
| 210 |
<== errno=ESUCCESS |
| 211 |
`, |
| 212 |
}, |
| 213 |
} |
| 214 |
|
| 215 |
for _, tt := range tests { |
| 216 |
tc := tt |
| 217 |
t.Run(tc.name, func(t *testing.T) { |
| 218 |
defer log.Reset() |
| 219 |
|
| 220 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdDatasyncName, uint64(tc.fd)) |
| 221 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 222 |
}) |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
func openPipe(t *testing.T) (*os.File, *os.File) { |
| 227 |
r, w, err := os.Pipe() |
| 228 |
require.NoError(t, err) |
| 229 |
return r, w |
| 230 |
} |
| 231 |
|
| 232 |
func closePipe(r, w *os.File) { |
| 233 |
r.Close() |
| 234 |
w.Close() |
| 235 |
} |
| 236 |
|
| 237 |
func Test_fdFdstatGet(t *testing.T) { |
| 238 |
file, dir := "animals.txt", "sub" |
| 239 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(fstest.FS)) |
| 240 |
defer r.Close(testCtx) |
| 241 |
memorySize := mod.Memory().Size() |
| 242 |
|
| 243 |
|
| 244 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 245 |
preopen := getPreopen(t, fsc) |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
stdin, _ := fsc.LookupFile(sys.FdStdin) |
| 250 |
stdinFile, errno := (&sysfs.AdaptFS{FS: &gofstest.MapFS{"stdin": &gofstest.MapFile{ |
| 251 |
Mode: fs.ModeDevice | fs.ModeCharDevice | 0o600, |
| 252 |
}}}).OpenFile("stdin", 0, 0) |
| 253 |
require.EqualErrno(t, 0, errno) |
| 254 |
|
| 255 |
stdin.File = stdinFile |
| 256 |
|
| 257 |
|
| 258 |
fileFD, errno := fsc.OpenFile(preopen, file, experimentalsys.O_RDWR, 0) |
| 259 |
require.EqualErrno(t, 0, errno) |
| 260 |
|
| 261 |
dirFD, errno := fsc.OpenFile(preopen, dir, experimentalsys.O_RDONLY, 0) |
| 262 |
require.EqualErrno(t, 0, errno) |
| 263 |
|
| 264 |
tests := []struct { |
| 265 |
name string |
| 266 |
fd int32 |
| 267 |
resultFdstat uint32 |
| 268 |
expectedMemory []byte |
| 269 |
expectedErrno wasip1.Errno |
| 270 |
expectedLog string |
| 271 |
}{ |
| 272 |
{ |
| 273 |
name: "stdin is a tty", |
| 274 |
fd: sys.FdStdin, |
| 275 |
expectedMemory: []byte{ |
| 276 |
2, 0, |
| 277 |
0, 0, 0, 0, 0, 0, |
| 278 |
0xdb, 0x1, 0xe0, 0x8, 0x0, 0x0, 0x0, 0x0, |
| 279 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 280 |
}, |
| 281 |
expectedLog: ` |
| 282 |
==> wasi_snapshot_preview1.fd_fdstat_get(fd=0) |
| 283 |
<== (stat={filetype=CHARACTER_DEVICE,fdflags=,fs_rights_base=FD_DATASYNC|FD_READ|FDSTAT_SET_FLAGS|FD_SYNC|FD_WRITE|FD_ADVISE|FD_ALLOCATE,fs_rights_inheriting=},errno=ESUCCESS) |
| 284 |
`, |
| 285 |
}, |
| 286 |
{ |
| 287 |
name: "stdout", |
| 288 |
fd: sys.FdStdout, |
| 289 |
expectedMemory: []byte{ |
| 290 |
1, 0, |
| 291 |
0, 0, 0, 0, 0, 0, |
| 292 |
0xff, 0x1, 0xe0, 0x8, 0x0, 0x0, 0x0, 0x0, |
| 293 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 294 |
}, |
| 295 |
expectedLog: ` |
| 296 |
==> wasi_snapshot_preview1.fd_fdstat_get(fd=1) |
| 297 |
<== (stat={filetype=BLOCK_DEVICE,fdflags=,fs_rights_base=FD_DATASYNC|FD_READ|FD_SEEK|FDSTAT_SET_FLAGS|FD_SYNC|FD_TELL|FD_WRITE|FD_ADVISE|FD_ALLOCATE,fs_rights_inheriting=},errno=ESUCCESS) |
| 298 |
`, |
| 299 |
}, |
| 300 |
{ |
| 301 |
name: "stderr", |
| 302 |
fd: sys.FdStderr, |
| 303 |
expectedMemory: []byte{ |
| 304 |
1, 0, |
| 305 |
0, 0, 0, 0, 0, 0, |
| 306 |
0xff, 0x1, 0xe0, 0x8, 0x0, 0x0, 0x0, 0x0, |
| 307 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 308 |
}, |
| 309 |
expectedLog: ` |
| 310 |
==> wasi_snapshot_preview1.fd_fdstat_get(fd=2) |
| 311 |
<== (stat={filetype=BLOCK_DEVICE,fdflags=,fs_rights_base=FD_DATASYNC|FD_READ|FD_SEEK|FDSTAT_SET_FLAGS|FD_SYNC|FD_TELL|FD_WRITE|FD_ADVISE|FD_ALLOCATE,fs_rights_inheriting=},errno=ESUCCESS) |
| 312 |
`, |
| 313 |
}, |
| 314 |
{ |
| 315 |
name: "root", |
| 316 |
fd: sys.FdPreopen, |
| 317 |
expectedMemory: []byte{ |
| 318 |
3, 0, |
| 319 |
0, 0, 0, 0, 0, 0, |
| 320 |
0x19, 0xfe, 0xbf, 0x7, 0x0, 0x0, 0x0, 0x0, |
| 321 |
0xff, 0xff, 0xff, 0xf, 0x0, 0x0, 0x0, 0x0, |
| 322 |
}, |
| 323 |
expectedLog: ` |
| 324 |
==> wasi_snapshot_preview1.fd_fdstat_get(fd=3) |
| 325 |
<== (stat={filetype=DIRECTORY,fdflags=,fs_rights_base=FD_DATASYNC|FDSTAT_SET_FLAGS|FD_SYNC|PATH_CREATE_DIRECTORY|PATH_CREATE_FILE|PATH_LINK_SOURCE|PATH_LINK_TARGET|PATH_OPEN|FD_READDIR|PATH_READLINK,fs_rights_inheriting=FD_DATASYNC|FD_READ|FD_SEEK|FDSTAT_SET_FLAGS|FD_SYNC|FD_TELL|FD_WRITE|FD_ADVISE|FD_ALLOCATE|PATH_CREATE_DIRECTORY|PATH_CREATE_FILE|PATH_LINK_SOURCE|PATH_LINK_TARGET|PATH_OPEN|FD_READDIR|PATH_READLINK},errno=ESUCCESS) |
| 326 |
`, |
| 327 |
}, |
| 328 |
{ |
| 329 |
name: "file", |
| 330 |
fd: fileFD, |
| 331 |
expectedMemory: []byte{ |
| 332 |
4, 0, |
| 333 |
0, 0, 0, 0, 0, 0, |
| 334 |
0xff, 0x1, 0xe0, 0x8, 0x0, 0x0, 0x0, 0x0, |
| 335 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 336 |
}, |
| 337 |
expectedLog: ` |
| 338 |
==> wasi_snapshot_preview1.fd_fdstat_get(fd=4) |
| 339 |
<== (stat={filetype=REGULAR_FILE,fdflags=,fs_rights_base=FD_DATASYNC|FD_READ|FD_SEEK|FDSTAT_SET_FLAGS|FD_SYNC|FD_TELL|FD_WRITE|FD_ADVISE|FD_ALLOCATE,fs_rights_inheriting=},errno=ESUCCESS) |
| 340 |
`, |
| 341 |
}, |
| 342 |
{ |
| 343 |
name: "dir", |
| 344 |
fd: dirFD, |
| 345 |
expectedMemory: []byte{ |
| 346 |
3, 0, |
| 347 |
0, 0, 0, 0, 0, 0, |
| 348 |
0x19, 0xfe, 0xbf, 0x7, 0x0, 0x0, 0x0, 0x0, |
| 349 |
0xff, 0xff, 0xff, 0xf, 0x0, 0x0, 0x0, 0x0, |
| 350 |
}, |
| 351 |
expectedLog: ` |
| 352 |
==> wasi_snapshot_preview1.fd_fdstat_get(fd=5) |
| 353 |
<== (stat={filetype=DIRECTORY,fdflags=,fs_rights_base=FD_DATASYNC|FDSTAT_SET_FLAGS|FD_SYNC|PATH_CREATE_DIRECTORY|PATH_CREATE_FILE|PATH_LINK_SOURCE|PATH_LINK_TARGET|PATH_OPEN|FD_READDIR|PATH_READLINK,fs_rights_inheriting=FD_DATASYNC|FD_READ|FD_SEEK|FDSTAT_SET_FLAGS|FD_SYNC|FD_TELL|FD_WRITE|FD_ADVISE|FD_ALLOCATE|PATH_CREATE_DIRECTORY|PATH_CREATE_FILE|PATH_LINK_SOURCE|PATH_LINK_TARGET|PATH_OPEN|FD_READDIR|PATH_READLINK},errno=ESUCCESS) |
| 354 |
`, |
| 355 |
}, |
| 356 |
{ |
| 357 |
name: "bad FD", |
| 358 |
fd: -1, |
| 359 |
expectedErrno: wasip1.ErrnoBadf, |
| 360 |
expectedLog: ` |
| 361 |
==> wasi_snapshot_preview1.fd_fdstat_get(fd=-1) |
| 362 |
<== (stat=,errno=EBADF) |
| 363 |
`, |
| 364 |
}, |
| 365 |
{ |
| 366 |
name: "resultFdstat exceeds the maximum valid address by 1", |
| 367 |
fd: dirFD, |
| 368 |
resultFdstat: memorySize - 24 + 1, |
| 369 |
expectedErrno: wasip1.ErrnoFault, |
| 370 |
expectedLog: ` |
| 371 |
==> wasi_snapshot_preview1.fd_fdstat_get(fd=5) |
| 372 |
<== (stat=,errno=EFAULT) |
| 373 |
`, |
| 374 |
}, |
| 375 |
} |
| 376 |
|
| 377 |
for _, tt := range tests { |
| 378 |
tc := tt |
| 379 |
|
| 380 |
t.Run(tc.name, func(t *testing.T) { |
| 381 |
defer log.Reset() |
| 382 |
|
| 383 |
maskMemory(t, mod, len(tc.expectedMemory)) |
| 384 |
|
| 385 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdFdstatGetName, uint64(tc.fd), uint64(tc.resultFdstat)) |
| 386 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 387 |
|
| 388 |
actual, ok := mod.Memory().Read(0, uint32(len(tc.expectedMemory))) |
| 389 |
require.True(t, ok) |
| 390 |
require.Equal(t, tc.expectedMemory, actual) |
| 391 |
}) |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
func Test_fdFdstatGet_StdioNonblock(t *testing.T) { |
| 396 |
stdinR, stdinW := openPipe(t) |
| 397 |
defer closePipe(stdinR, stdinW) |
| 398 |
|
| 399 |
stdoutR, stdoutW := openPipe(t) |
| 400 |
defer closePipe(stdoutR, stdoutW) |
| 401 |
|
| 402 |
stderrR, stderrW := openPipe(t) |
| 403 |
defer closePipe(stderrR, stderrW) |
| 404 |
|
| 405 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig(). |
| 406 |
WithStdin(stdinR). |
| 407 |
WithStdout(stdoutW). |
| 408 |
WithStderr(stderrW)) |
| 409 |
defer r.Close(testCtx) |
| 410 |
|
| 411 |
stdin, stdout, stderr := uint64(0), uint64(1), uint64(2) |
| 412 |
requireErrnoResult(t, 0, mod, wasip1.FdFdstatSetFlagsName, stdin, uint64(wasip1.FD_NONBLOCK)) |
| 413 |
requireErrnoResult(t, 0, mod, wasip1.FdFdstatSetFlagsName, stdout, uint64(wasip1.FD_NONBLOCK)) |
| 414 |
requireErrnoResult(t, 0, mod, wasip1.FdFdstatSetFlagsName, stderr, uint64(wasip1.FD_NONBLOCK)) |
| 415 |
log.Reset() |
| 416 |
|
| 417 |
tests := []struct { |
| 418 |
name string |
| 419 |
fd int32 |
| 420 |
resultFdstat uint32 |
| 421 |
expectedMemory []byte |
| 422 |
expectedErrno wasip1.Errno |
| 423 |
expectedLog string |
| 424 |
}{ |
| 425 |
{ |
| 426 |
name: "stdin", |
| 427 |
fd: sys.FdStdin, |
| 428 |
expectedMemory: []byte{ |
| 429 |
0, 0, |
| 430 |
5, 0, 0, 0, 0, 0, |
| 431 |
0xff, 0x1, 0xe0, 0x8, 0x0, 0x0, 0x0, 0x0, |
| 432 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 433 |
}, |
| 434 |
expectedLog: ` |
| 435 |
==> wasi_snapshot_preview1.fd_fdstat_get(fd=0) |
| 436 |
<== (stat={filetype=UNKNOWN,fdflags=APPEND|NONBLOCK,fs_rights_base=FD_DATASYNC|FD_READ|FD_SEEK|FDSTAT_SET_FLAGS|FD_SYNC|FD_TELL|FD_WRITE|FD_ADVISE|FD_ALLOCATE,fs_rights_inheriting=},errno=ESUCCESS) |
| 437 |
`, |
| 438 |
}, |
| 439 |
{ |
| 440 |
name: "stdout", |
| 441 |
fd: sys.FdStdout, |
| 442 |
expectedMemory: []byte{ |
| 443 |
0, 0, |
| 444 |
5, 0, 0, 0, 0, 0, |
| 445 |
0xff, 0x1, 0xe0, 0x8, 0x0, 0x0, 0x0, 0x0, |
| 446 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 447 |
}, |
| 448 |
expectedLog: ` |
| 449 |
==> wasi_snapshot_preview1.fd_fdstat_get(fd=1) |
| 450 |
<== (stat={filetype=UNKNOWN,fdflags=APPEND|NONBLOCK,fs_rights_base=FD_DATASYNC|FD_READ|FD_SEEK|FDSTAT_SET_FLAGS|FD_SYNC|FD_TELL|FD_WRITE|FD_ADVISE|FD_ALLOCATE,fs_rights_inheriting=},errno=ESUCCESS) |
| 451 |
`, |
| 452 |
}, |
| 453 |
{ |
| 454 |
name: "stderr", |
| 455 |
fd: sys.FdStderr, |
| 456 |
expectedMemory: []byte{ |
| 457 |
0, 0, |
| 458 |
5, 0, 0, 0, 0, 0, |
| 459 |
0xff, 0x1, 0xe0, 0x8, 0x0, 0x0, 0x0, 0x0, |
| 460 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 461 |
}, |
| 462 |
expectedLog: ` |
| 463 |
==> wasi_snapshot_preview1.fd_fdstat_get(fd=2) |
| 464 |
<== (stat={filetype=UNKNOWN,fdflags=APPEND|NONBLOCK,fs_rights_base=FD_DATASYNC|FD_READ|FD_SEEK|FDSTAT_SET_FLAGS|FD_SYNC|FD_TELL|FD_WRITE|FD_ADVISE|FD_ALLOCATE,fs_rights_inheriting=},errno=ESUCCESS) |
| 465 |
`, |
| 466 |
}, |
| 467 |
} |
| 468 |
|
| 469 |
for _, tt := range tests { |
| 470 |
tc := tt |
| 471 |
|
| 472 |
t.Run(tc.name, func(t *testing.T) { |
| 473 |
defer log.Reset() |
| 474 |
|
| 475 |
maskMemory(t, mod, len(tc.expectedMemory)) |
| 476 |
|
| 477 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdFdstatGetName, uint64(tc.fd), uint64(tc.resultFdstat)) |
| 478 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 479 |
|
| 480 |
actual, ok := mod.Memory().Read(0, uint32(len(tc.expectedMemory))) |
| 481 |
require.True(t, ok) |
| 482 |
require.Equal(t, tc.expectedMemory, actual) |
| 483 |
}) |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
func Test_fdFdstatSetFlagsWithTrunc(t *testing.T) { |
| 488 |
tmpDir := t.TempDir() |
| 489 |
fileName := "test" |
| 490 |
|
| 491 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig(). |
| 492 |
WithFSConfig(wazero.NewFSConfig().WithDirMount(tmpDir, "/"))) |
| 493 |
defer r.Close(testCtx) |
| 494 |
|
| 495 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 496 |
preopen := getPreopen(t, fsc) |
| 497 |
|
| 498 |
fd, errno := fsc.OpenFile(preopen, fileName, experimentalsys.O_RDWR|experimentalsys.O_CREAT|experimentalsys.O_EXCL|experimentalsys.O_TRUNC, 0o600) |
| 499 |
require.EqualErrno(t, 0, errno) |
| 500 |
|
| 501 |
|
| 502 |
f, ok := fsc.LookupFile(fd) |
| 503 |
require.True(t, ok) |
| 504 |
n, _ := f.File.Write([]byte("abc")) |
| 505 |
require.Equal(t, n, 3) |
| 506 |
|
| 507 |
buf, err := os.ReadFile(joinPath(tmpDir, fileName)) |
| 508 |
require.NoError(t, err) |
| 509 |
require.Equal(t, "abc", string(buf)) |
| 510 |
|
| 511 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdFdstatSetFlagsName, uint64(fd), uint64(0)) |
| 512 |
require.Equal(t, ` |
| 513 |
==> wasi_snapshot_preview1.fd_fdstat_set_flags(fd=4,flags=) |
| 514 |
<== errno=ESUCCESS |
| 515 |
`, "\n"+log.String()) |
| 516 |
log.Reset() |
| 517 |
|
| 518 |
buf, err = os.ReadFile(joinPath(tmpDir, fileName)) |
| 519 |
require.NoError(t, err) |
| 520 |
require.Equal(t, "abc", string(buf)) |
| 521 |
} |
| 522 |
|
| 523 |
func Test_fdFdstatSetFlags(t *testing.T) { |
| 524 |
tmpDir := t.TempDir() |
| 525 |
|
| 526 |
stdinR, stdinW := openPipe(t) |
| 527 |
defer closePipe(stdinR, stdinW) |
| 528 |
|
| 529 |
stdoutR, stdoutW := openPipe(t) |
| 530 |
defer closePipe(stdoutR, stdoutW) |
| 531 |
|
| 532 |
stderrR, stderrW := openPipe(t) |
| 533 |
defer closePipe(stderrR, stderrW) |
| 534 |
|
| 535 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig(). |
| 536 |
WithStdin(stdinR). |
| 537 |
WithStdout(stdoutW). |
| 538 |
WithStderr(stderrW). |
| 539 |
WithFSConfig(wazero.NewFSConfig().WithDirMount(tmpDir, "/"))) |
| 540 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 541 |
preopen := getPreopen(t, fsc) |
| 542 |
defer r.Close(testCtx) |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
const fileName = "file.txt" |
| 548 |
|
| 549 |
fd, errno := fsc.OpenFile(preopen, fileName, experimentalsys.O_RDWR|experimentalsys.O_APPEND|experimentalsys.O_CREAT|experimentalsys.O_EXCL, 0o600) |
| 550 |
require.EqualErrno(t, 0, errno) |
| 551 |
|
| 552 |
|
| 553 |
f, ok := fsc.LookupFile(fd) |
| 554 |
require.True(t, ok) |
| 555 |
_, errno = f.File.Write([]byte("0123456789")) |
| 556 |
require.EqualErrno(t, 0, errno) |
| 557 |
|
| 558 |
writeWazero := func() { |
| 559 |
iovs := uint32(1) |
| 560 |
initialMemory := []byte{ |
| 561 |
'?', |
| 562 |
18, 0, 0, 0, |
| 563 |
4, 0, 0, 0, |
| 564 |
23, 0, 0, 0, |
| 565 |
2, 0, 0, 0, |
| 566 |
'?', |
| 567 |
'w', 'a', 'z', 'e', |
| 568 |
'?', |
| 569 |
'r', 'o', |
| 570 |
'?', |
| 571 |
} |
| 572 |
iovsCount := uint32(2) |
| 573 |
resultNwritten := uint32(26) |
| 574 |
|
| 575 |
ok := mod.Memory().Write(0, initialMemory) |
| 576 |
require.True(t, ok) |
| 577 |
|
| 578 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdWriteName, uint64(fd), uint64(iovs), uint64(iovsCount), uint64(resultNwritten)) |
| 579 |
require.Equal(t, ` |
| 580 |
==> wasi_snapshot_preview1.fd_write(fd=4,iovs=1,iovs_len=2) |
| 581 |
<== (nwritten=6,errno=ESUCCESS) |
| 582 |
`, "\n"+log.String()) |
| 583 |
log.Reset() |
| 584 |
} |
| 585 |
|
| 586 |
requireFileContent := func(exp string) { |
| 587 |
buf, err := os.ReadFile(joinPath(tmpDir, fileName)) |
| 588 |
require.NoError(t, err) |
| 589 |
require.Equal(t, exp, string(buf)) |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
writeWazero() |
| 594 |
requireFileContent("0123456789" + "wazero") |
| 595 |
|
| 596 |
|
| 597 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdFdstatSetFlagsName, uint64(fd), uint64(0)) |
| 598 |
require.Equal(t, ` |
| 599 |
==> wasi_snapshot_preview1.fd_fdstat_set_flags(fd=4,flags=) |
| 600 |
<== errno=ESUCCESS |
| 601 |
`, "\n"+log.String()) |
| 602 |
log.Reset() |
| 603 |
|
| 604 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdSeekName, uint64(fd), uint64(0), uint64(0), uint64(1024)) |
| 605 |
require.Equal(t, ` |
| 606 |
==> wasi_snapshot_preview1.fd_seek(fd=4,offset=0,whence=0) |
| 607 |
<== (newoffset=0,errno=ESUCCESS) |
| 608 |
`, "\n"+log.String()) |
| 609 |
log.Reset() |
| 610 |
|
| 611 |
|
| 612 |
writeWazero() |
| 613 |
requireFileContent("wazero6789" + "wazero") |
| 614 |
|
| 615 |
|
| 616 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdFdstatSetFlagsName, uint64(fd), uint64(wasip1.FD_APPEND)) |
| 617 |
require.Equal(t, ` |
| 618 |
==> wasi_snapshot_preview1.fd_fdstat_set_flags(fd=4,flags=APPEND) |
| 619 |
<== errno=ESUCCESS |
| 620 |
`, "\n"+log.String()) |
| 621 |
log.Reset() |
| 622 |
|
| 623 |
|
| 624 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdTellName, uint64(fd), uint64(1024)) |
| 625 |
require.Equal(t, ` |
| 626 |
==> wasi_snapshot_preview1.fd_tell(fd=4,result.offset=1024) |
| 627 |
<== errno=ESUCCESS |
| 628 |
`, "\n"+log.String()) |
| 629 |
log.Reset() |
| 630 |
offset, _ := mod.Memory().Read(1024, 4) |
| 631 |
require.Equal(t, offset, []byte{6, 0, 0, 0}) |
| 632 |
|
| 633 |
|
| 634 |
writeWazero() |
| 635 |
requireFileContent("wazero6789" + "wazero" + "wazero") |
| 636 |
|
| 637 |
t.Run("nonblock", func(t *testing.T) { |
| 638 |
stdin, stdout, stderr := uint64(0), uint64(1), uint64(2) |
| 639 |
requireErrnoResult(t, 0, mod, wasip1.FdFdstatSetFlagsName, stdin, uint64(wasip1.FD_NONBLOCK)) |
| 640 |
requireErrnoResult(t, 0, mod, wasip1.FdFdstatSetFlagsName, stdout, uint64(wasip1.FD_NONBLOCK)) |
| 641 |
requireErrnoResult(t, 0, mod, wasip1.FdFdstatSetFlagsName, stderr, uint64(wasip1.FD_NONBLOCK)) |
| 642 |
}) |
| 643 |
|
| 644 |
t.Run("errors", func(t *testing.T) { |
| 645 |
requireErrnoResult(t, wasip1.ErrnoInval, mod, wasip1.FdFdstatSetFlagsName, uint64(fd), uint64(wasip1.FD_DSYNC)) |
| 646 |
requireErrnoResult(t, wasip1.ErrnoInval, mod, wasip1.FdFdstatSetFlagsName, uint64(fd), uint64(wasip1.FD_RSYNC)) |
| 647 |
requireErrnoResult(t, wasip1.ErrnoInval, mod, wasip1.FdFdstatSetFlagsName, uint64(fd), uint64(wasip1.FD_SYNC)) |
| 648 |
requireErrnoResult(t, wasip1.ErrnoBadf, mod, wasip1.FdFdstatSetFlagsName, uint64(12345), uint64(wasip1.FD_APPEND)) |
| 649 |
requireErrnoResult(t, wasip1.ErrnoIsdir, mod, wasip1.FdFdstatSetFlagsName, uint64(3) , uint64(wasip1.FD_APPEND)) |
| 650 |
requireErrnoResult(t, wasip1.ErrnoIsdir, mod, wasip1.FdFdstatSetFlagsName, uint64(3), uint64(wasip1.FD_NONBLOCK)) |
| 651 |
}) |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
func Test_fdFdstatSetRights(t *testing.T) { |
| 656 |
log := requireErrnoNosys(t, wasip1.FdFdstatSetRightsName, 0, 0, 0) |
| 657 |
require.Equal(t, ` |
| 658 |
==> wasi_snapshot_preview1.fd_fdstat_set_rights(fd=0,fs_rights_base=,fs_rights_inheriting=) |
| 659 |
<== errno=ENOSYS |
| 660 |
`, log) |
| 661 |
} |
| 662 |
|
| 663 |
func Test_fdFilestatGet(t *testing.T) { |
| 664 |
file, dir := "animals.txt", "sub" |
| 665 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(fstest.FS)) |
| 666 |
defer r.Close(testCtx) |
| 667 |
memorySize := mod.Memory().Size() |
| 668 |
|
| 669 |
|
| 670 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 671 |
preopen := getPreopen(t, fsc) |
| 672 |
|
| 673 |
fileFD, errno := fsc.OpenFile(preopen, file, experimentalsys.O_RDONLY, 0) |
| 674 |
require.EqualErrno(t, 0, errno) |
| 675 |
|
| 676 |
dirFD, errno := fsc.OpenFile(preopen, dir, experimentalsys.O_RDONLY, 0) |
| 677 |
require.EqualErrno(t, 0, errno) |
| 678 |
|
| 679 |
tests := []struct { |
| 680 |
name string |
| 681 |
fd int32 |
| 682 |
resultFilestat uint32 |
| 683 |
expectedMemory []byte |
| 684 |
expectedErrno wasip1.Errno |
| 685 |
expectedLog string |
| 686 |
}{ |
| 687 |
{ |
| 688 |
name: "stdin", |
| 689 |
fd: sys.FdStdin, |
| 690 |
expectedMemory: []byte{ |
| 691 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 692 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 693 |
|
| 694 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 695 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 696 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 697 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 698 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 699 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 700 |
}, |
| 701 |
expectedLog: ` |
| 702 |
==> wasi_snapshot_preview1.fd_filestat_get(fd=0) |
| 703 |
<== (filestat={filetype=BLOCK_DEVICE,size=0,mtim=0},errno=ESUCCESS) |
| 704 |
`, |
| 705 |
}, |
| 706 |
{ |
| 707 |
name: "stdout", |
| 708 |
fd: sys.FdStdout, |
| 709 |
expectedMemory: []byte{ |
| 710 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 711 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 712 |
|
| 713 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 714 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 715 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 716 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 717 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 718 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 719 |
}, |
| 720 |
expectedLog: ` |
| 721 |
==> wasi_snapshot_preview1.fd_filestat_get(fd=1) |
| 722 |
<== (filestat={filetype=BLOCK_DEVICE,size=0,mtim=0},errno=ESUCCESS) |
| 723 |
`, |
| 724 |
}, |
| 725 |
{ |
| 726 |
name: "stderr", |
| 727 |
fd: sys.FdStderr, |
| 728 |
expectedMemory: []byte{ |
| 729 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 730 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 731 |
|
| 732 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 733 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 734 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 735 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 736 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 737 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 738 |
}, |
| 739 |
expectedLog: ` |
| 740 |
==> wasi_snapshot_preview1.fd_filestat_get(fd=2) |
| 741 |
<== (filestat={filetype=BLOCK_DEVICE,size=0,mtim=0},errno=ESUCCESS) |
| 742 |
`, |
| 743 |
}, |
| 744 |
{ |
| 745 |
name: "root", |
| 746 |
fd: sys.FdPreopen, |
| 747 |
expectedMemory: []byte{ |
| 748 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 749 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 750 |
3, 0, 0, 0, 0, 0, 0, 0, |
| 751 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 752 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 753 |
0x0, 0x0, 0x7c, 0x78, 0x9d, 0xf2, 0x55, 0x16, |
| 754 |
0x0, 0x0, 0x7c, 0x78, 0x9d, 0xf2, 0x55, 0x16, |
| 755 |
0x0, 0x0, 0x7c, 0x78, 0x9d, 0xf2, 0x55, 0x16, |
| 756 |
}, |
| 757 |
expectedLog: ` |
| 758 |
==> wasi_snapshot_preview1.fd_filestat_get(fd=3) |
| 759 |
<== (filestat={filetype=DIRECTORY,size=0,mtim=1609459200000000000},errno=ESUCCESS) |
| 760 |
`, |
| 761 |
}, |
| 762 |
{ |
| 763 |
name: "file", |
| 764 |
fd: fileFD, |
| 765 |
expectedMemory: []byte{ |
| 766 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 767 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 768 |
4, 0, 0, 0, 0, 0, 0, 0, |
| 769 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 770 |
30, 0, 0, 0, 0, 0, 0, 0, |
| 771 |
0x0, 0x82, 0x13, 0x80, 0x6b, 0x16, 0x24, 0x17, |
| 772 |
0x0, 0x82, 0x13, 0x80, 0x6b, 0x16, 0x24, 0x17, |
| 773 |
0x0, 0x82, 0x13, 0x80, 0x6b, 0x16, 0x24, 0x17, |
| 774 |
}, |
| 775 |
expectedLog: ` |
| 776 |
==> wasi_snapshot_preview1.fd_filestat_get(fd=4) |
| 777 |
<== (filestat={filetype=REGULAR_FILE,size=30,mtim=1667482413000000000},errno=ESUCCESS) |
| 778 |
`, |
| 779 |
}, |
| 780 |
{ |
| 781 |
name: "dir", |
| 782 |
fd: dirFD, |
| 783 |
expectedMemory: []byte{ |
| 784 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 785 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 786 |
3, 0, 0, 0, 0, 0, 0, 0, |
| 787 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 788 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 789 |
0x0, 0x0, 0x1f, 0xa6, 0x70, 0xfc, 0xc5, 0x16, |
| 790 |
0x0, 0x0, 0x1f, 0xa6, 0x70, 0xfc, 0xc5, 0x16, |
| 791 |
0x0, 0x0, 0x1f, 0xa6, 0x70, 0xfc, 0xc5, 0x16, |
| 792 |
}, |
| 793 |
expectedLog: ` |
| 794 |
==> wasi_snapshot_preview1.fd_filestat_get(fd=5) |
| 795 |
<== (filestat={filetype=DIRECTORY,size=0,mtim=1640995200000000000},errno=ESUCCESS) |
| 796 |
`, |
| 797 |
}, |
| 798 |
{ |
| 799 |
name: "bad FD", |
| 800 |
fd: -1, |
| 801 |
expectedErrno: wasip1.ErrnoBadf, |
| 802 |
expectedLog: ` |
| 803 |
==> wasi_snapshot_preview1.fd_filestat_get(fd=-1) |
| 804 |
<== (filestat=,errno=EBADF) |
| 805 |
`, |
| 806 |
}, |
| 807 |
{ |
| 808 |
name: "resultFilestat exceeds the maximum valid address by 1", |
| 809 |
fd: dirFD, |
| 810 |
resultFilestat: memorySize - 64 + 1, |
| 811 |
expectedErrno: wasip1.ErrnoFault, |
| 812 |
expectedLog: ` |
| 813 |
==> wasi_snapshot_preview1.fd_filestat_get(fd=5) |
| 814 |
<== (filestat=,errno=EFAULT) |
| 815 |
`, |
| 816 |
}, |
| 817 |
} |
| 818 |
|
| 819 |
for _, tt := range tests { |
| 820 |
tc := tt |
| 821 |
|
| 822 |
t.Run(tc.name, func(t *testing.T) { |
| 823 |
defer log.Reset() |
| 824 |
|
| 825 |
maskMemory(t, mod, len(tc.expectedMemory)) |
| 826 |
|
| 827 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdFilestatGetName, uint64(tc.fd), uint64(tc.resultFilestat)) |
| 828 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 829 |
|
| 830 |
actual, ok := mod.Memory().Read(0, uint32(len(tc.expectedMemory))) |
| 831 |
require.True(t, ok) |
| 832 |
require.Equal(t, tc.expectedMemory, actual) |
| 833 |
}) |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
func Test_fdFilestatSetSize(t *testing.T) { |
| 838 |
tmpDir := t.TempDir() |
| 839 |
|
| 840 |
tests := []struct { |
| 841 |
name string |
| 842 |
size uint64 |
| 843 |
content, expectedContent []byte |
| 844 |
expectedLog string |
| 845 |
expectedErrno wasip1.Errno |
| 846 |
}{ |
| 847 |
{ |
| 848 |
name: "badf", |
| 849 |
content: []byte("badf"), |
| 850 |
expectedContent: []byte("badf"), |
| 851 |
expectedErrno: wasip1.ErrnoBadf, |
| 852 |
expectedLog: ` |
| 853 |
==> wasi_snapshot_preview1.fd_filestat_set_size(fd=5,size=0) |
| 854 |
<== errno=EBADF |
| 855 |
`, |
| 856 |
}, |
| 857 |
{ |
| 858 |
name: "truncate", |
| 859 |
content: []byte("123456"), |
| 860 |
expectedContent: []byte("12345"), |
| 861 |
size: 5, |
| 862 |
expectedErrno: wasip1.ErrnoSuccess, |
| 863 |
expectedLog: ` |
| 864 |
==> wasi_snapshot_preview1.fd_filestat_set_size(fd=4,size=5) |
| 865 |
<== errno=ESUCCESS |
| 866 |
`, |
| 867 |
}, |
| 868 |
{ |
| 869 |
name: "truncate to zero", |
| 870 |
content: []byte("123456"), |
| 871 |
expectedContent: []byte(""), |
| 872 |
size: 0, |
| 873 |
expectedErrno: wasip1.ErrnoSuccess, |
| 874 |
expectedLog: ` |
| 875 |
==> wasi_snapshot_preview1.fd_filestat_set_size(fd=4,size=0) |
| 876 |
<== errno=ESUCCESS |
| 877 |
`, |
| 878 |
}, |
| 879 |
{ |
| 880 |
name: "truncate to expand", |
| 881 |
content: []byte("123456"), |
| 882 |
expectedContent: append([]byte("123456"), make([]byte, 100)...), |
| 883 |
size: 106, |
| 884 |
expectedErrno: wasip1.ErrnoSuccess, |
| 885 |
expectedLog: ` |
| 886 |
==> wasi_snapshot_preview1.fd_filestat_set_size(fd=4,size=106) |
| 887 |
<== errno=ESUCCESS |
| 888 |
`, |
| 889 |
}, |
| 890 |
{ |
| 891 |
name: "large size", |
| 892 |
content: []byte(""), |
| 893 |
expectedContent: []byte(""), |
| 894 |
size: math.MaxUint64, |
| 895 |
expectedErrno: wasip1.ErrnoInval, |
| 896 |
expectedLog: ` |
| 897 |
==> wasi_snapshot_preview1.fd_filestat_set_size(fd=4,size=-1) |
| 898 |
<== errno=EINVAL |
| 899 |
`, |
| 900 |
}, |
| 901 |
} |
| 902 |
|
| 903 |
for _, tt := range tests { |
| 904 |
tc := tt |
| 905 |
t.Run(tc.name, func(t *testing.T) { |
| 906 |
filepath := path.Base(t.Name()) |
| 907 |
mod, fd, log, r := requireOpenFile(t, tmpDir, filepath, tc.content, false) |
| 908 |
defer r.Close(testCtx) |
| 909 |
|
| 910 |
if filepath == "badf" { |
| 911 |
fd++ |
| 912 |
} |
| 913 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdFilestatSetSizeName, uint64(fd), uint64(tc.size)) |
| 914 |
|
| 915 |
actual, err := os.ReadFile(joinPath(tmpDir, filepath)) |
| 916 |
require.NoError(t, err) |
| 917 |
require.Equal(t, tc.expectedContent, actual) |
| 918 |
|
| 919 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 920 |
}) |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
func Test_fdFilestatSetTimes(t *testing.T) { |
| 925 |
tmpDir := t.TempDir() |
| 926 |
|
| 927 |
tests := []struct { |
| 928 |
name string |
| 929 |
mtime, atime int64 |
| 930 |
flags uint16 |
| 931 |
expectedLog string |
| 932 |
expectedErrno wasip1.Errno |
| 933 |
}{ |
| 934 |
{ |
| 935 |
name: "badf", |
| 936 |
expectedErrno: wasip1.ErrnoBadf, |
| 937 |
expectedLog: ` |
| 938 |
==> wasi_snapshot_preview1.fd_filestat_set_times(fd=-1,atim=0,mtim=0,fst_flags=) |
| 939 |
<== errno=EBADF |
| 940 |
`, |
| 941 |
}, |
| 942 |
{ |
| 943 |
name: "a=omit,m=omit", |
| 944 |
mtime: 1234, |
| 945 |
atime: 123451, |
| 946 |
expectedErrno: wasip1.ErrnoSuccess, |
| 947 |
expectedLog: ` |
| 948 |
==> wasi_snapshot_preview1.fd_filestat_set_times(fd=4,atim=123451,mtim=1234,fst_flags=) |
| 949 |
<== errno=ESUCCESS |
| 950 |
`, |
| 951 |
}, |
| 952 |
{ |
| 953 |
name: "a=now,m=omit", |
| 954 |
expectedErrno: wasip1.ErrnoSuccess, |
| 955 |
mtime: 1234, |
| 956 |
atime: 123451, |
| 957 |
flags: wasip1.FstflagsAtimNow, |
| 958 |
expectedLog: ` |
| 959 |
==> wasi_snapshot_preview1.fd_filestat_set_times(fd=4,atim=123451,mtim=1234,fst_flags=ATIM_NOW) |
| 960 |
<== errno=ESUCCESS |
| 961 |
`, |
| 962 |
}, |
| 963 |
{ |
| 964 |
name: "a=omit,m=now", |
| 965 |
expectedErrno: wasip1.ErrnoSuccess, |
| 966 |
mtime: 1234, |
| 967 |
atime: 123451, |
| 968 |
flags: wasip1.FstflagsMtimNow, |
| 969 |
expectedLog: ` |
| 970 |
==> wasi_snapshot_preview1.fd_filestat_set_times(fd=4,atim=123451,mtim=1234,fst_flags=MTIM_NOW) |
| 971 |
<== errno=ESUCCESS |
| 972 |
`, |
| 973 |
}, |
| 974 |
{ |
| 975 |
name: "a=now,m=now", |
| 976 |
expectedErrno: wasip1.ErrnoSuccess, |
| 977 |
mtime: 1234, |
| 978 |
atime: 123451, |
| 979 |
flags: wasip1.FstflagsAtimNow | wasip1.FstflagsMtimNow, |
| 980 |
expectedLog: ` |
| 981 |
==> wasi_snapshot_preview1.fd_filestat_set_times(fd=4,atim=123451,mtim=1234,fst_flags=ATIM_NOW|MTIM_NOW) |
| 982 |
<== errno=ESUCCESS |
| 983 |
`, |
| 984 |
}, |
| 985 |
{ |
| 986 |
name: "a=now,m=set", |
| 987 |
expectedErrno: wasip1.ErrnoSuccess, |
| 988 |
mtime: 55555500, |
| 989 |
atime: 1234, |
| 990 |
flags: wasip1.FstflagsAtimNow | wasip1.FstflagsMtim, |
| 991 |
expectedLog: ` |
| 992 |
==> wasi_snapshot_preview1.fd_filestat_set_times(fd=4,atim=1234,mtim=55555500,fst_flags=ATIM_NOW|MTIM) |
| 993 |
<== errno=ESUCCESS |
| 994 |
`, |
| 995 |
}, |
| 996 |
{ |
| 997 |
name: "a=set,m=now", |
| 998 |
expectedErrno: wasip1.ErrnoSuccess, |
| 999 |
mtime: 1234, |
| 1000 |
atime: 55555500, |
| 1001 |
flags: wasip1.FstflagsAtim | wasip1.FstflagsMtimNow, |
| 1002 |
expectedLog: ` |
| 1003 |
==> wasi_snapshot_preview1.fd_filestat_set_times(fd=4,atim=55555500,mtim=1234,fst_flags=ATIM|MTIM_NOW) |
| 1004 |
<== errno=ESUCCESS |
| 1005 |
`, |
| 1006 |
}, |
| 1007 |
{ |
| 1008 |
name: "a=set,m=omit", |
| 1009 |
expectedErrno: wasip1.ErrnoSuccess, |
| 1010 |
mtime: 1234, |
| 1011 |
atime: 55555500, |
| 1012 |
flags: wasip1.FstflagsAtim, |
| 1013 |
expectedLog: ` |
| 1014 |
==> wasi_snapshot_preview1.fd_filestat_set_times(fd=4,atim=55555500,mtim=1234,fst_flags=ATIM) |
| 1015 |
<== errno=ESUCCESS |
| 1016 |
`, |
| 1017 |
}, |
| 1018 |
|
| 1019 |
{ |
| 1020 |
name: "a=omit,m=set", |
| 1021 |
expectedErrno: wasip1.ErrnoSuccess, |
| 1022 |
mtime: 55555500, |
| 1023 |
atime: 1234, |
| 1024 |
flags: wasip1.FstflagsMtim, |
| 1025 |
expectedLog: ` |
| 1026 |
==> wasi_snapshot_preview1.fd_filestat_set_times(fd=4,atim=1234,mtim=55555500,fst_flags=MTIM) |
| 1027 |
<== errno=ESUCCESS |
| 1028 |
`, |
| 1029 |
}, |
| 1030 |
|
| 1031 |
{ |
| 1032 |
name: "a=set,m=set", |
| 1033 |
expectedErrno: wasip1.ErrnoSuccess, |
| 1034 |
mtime: 55555500, |
| 1035 |
atime: 6666666600, |
| 1036 |
flags: wasip1.FstflagsAtim | wasip1.FstflagsMtim, |
| 1037 |
expectedLog: ` |
| 1038 |
==> wasi_snapshot_preview1.fd_filestat_set_times(fd=4,atim=6666666600,mtim=55555500,fst_flags=ATIM|MTIM) |
| 1039 |
<== errno=ESUCCESS |
| 1040 |
`, |
| 1041 |
}, |
| 1042 |
} |
| 1043 |
|
| 1044 |
for _, tt := range tests { |
| 1045 |
tc := tt |
| 1046 |
t.Run(tc.name, func(t *testing.T) { |
| 1047 |
filepath := path.Base(t.Name()) |
| 1048 |
mod, fd, log, r := requireOpenFile(t, tmpDir, filepath, []byte("anything"), false) |
| 1049 |
defer r.Close(testCtx) |
| 1050 |
|
| 1051 |
sys := mod.(*wasm.ModuleInstance).Sys |
| 1052 |
fsc := sys.FS() |
| 1053 |
|
| 1054 |
paramFd := fd |
| 1055 |
if filepath == "badf" { |
| 1056 |
paramFd = -1 |
| 1057 |
} |
| 1058 |
|
| 1059 |
f, ok := fsc.LookupFile(fd) |
| 1060 |
require.True(t, ok) |
| 1061 |
|
| 1062 |
st, errno := f.File.Stat() |
| 1063 |
require.EqualErrno(t, 0, errno) |
| 1064 |
prevAtime, prevMtime := st.Atim, st.Mtim |
| 1065 |
|
| 1066 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdFilestatSetTimesName, |
| 1067 |
uint64(paramFd), uint64(tc.atime), uint64(tc.mtime), |
| 1068 |
uint64(tc.flags), |
| 1069 |
) |
| 1070 |
|
| 1071 |
if tc.expectedErrno == wasip1.ErrnoSuccess { |
| 1072 |
f, ok := fsc.LookupFile(fd) |
| 1073 |
require.True(t, ok) |
| 1074 |
|
| 1075 |
st, errno = f.File.Stat() |
| 1076 |
require.EqualErrno(t, 0, errno) |
| 1077 |
if tc.flags&wasip1.FstflagsAtim != 0 { |
| 1078 |
require.Equal(t, tc.atime, st.Atim) |
| 1079 |
} else if tc.flags&wasip1.FstflagsAtimNow != 0 { |
| 1080 |
require.True(t, (sys.WalltimeNanos()-st.Atim) < time.Second.Nanoseconds()) |
| 1081 |
} else { |
| 1082 |
require.Equal(t, prevAtime, st.Atim) |
| 1083 |
} |
| 1084 |
if tc.flags&wasip1.FstflagsMtim != 0 { |
| 1085 |
require.Equal(t, tc.mtime, st.Mtim) |
| 1086 |
} else if tc.flags&wasip1.FstflagsMtimNow != 0 { |
| 1087 |
require.True(t, (sys.WalltimeNanos()-st.Mtim) < time.Second.Nanoseconds()) |
| 1088 |
} else { |
| 1089 |
require.Equal(t, prevMtime, st.Mtim) |
| 1090 |
} |
| 1091 |
} |
| 1092 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 1093 |
}) |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
func Test_fdPread(t *testing.T) { |
| 1098 |
tmpDir := t.TempDir() |
| 1099 |
mod, fd, log, r := requireOpenFile(t, tmpDir, "test_path", []byte("wazero"), true) |
| 1100 |
defer r.Close(testCtx) |
| 1101 |
|
| 1102 |
iovs := uint32(1) |
| 1103 |
initialMemory := []byte{ |
| 1104 |
'?', |
| 1105 |
18, 0, 0, 0, |
| 1106 |
4, 0, 0, 0, |
| 1107 |
23, 0, 0, 0, |
| 1108 |
2, 0, 0, 0, |
| 1109 |
'?', |
| 1110 |
} |
| 1111 |
|
| 1112 |
iovsCount := uint32(2) |
| 1113 |
resultNread := uint32(26) |
| 1114 |
|
| 1115 |
tests := []struct { |
| 1116 |
name string |
| 1117 |
offset int64 |
| 1118 |
expectedMemory []byte |
| 1119 |
expectedLog string |
| 1120 |
}{ |
| 1121 |
{ |
| 1122 |
name: "offset zero", |
| 1123 |
offset: 0, |
| 1124 |
expectedMemory: append( |
| 1125 |
initialMemory, |
| 1126 |
'w', 'a', 'z', 'e', |
| 1127 |
'?', |
| 1128 |
'r', 'o', |
| 1129 |
'?', |
| 1130 |
6, 0, 0, 0, |
| 1131 |
'?', |
| 1132 |
), |
| 1133 |
expectedLog: ` |
| 1134 |
==> wasi_snapshot_preview1.fd_pread(fd=4,iovs=1,iovs_len=2,offset=0) |
| 1135 |
<== (nread=6,errno=ESUCCESS) |
| 1136 |
`, |
| 1137 |
}, |
| 1138 |
{ |
| 1139 |
name: "offset 2", |
| 1140 |
offset: 2, |
| 1141 |
expectedMemory: append( |
| 1142 |
initialMemory, |
| 1143 |
'z', 'e', 'r', 'o', |
| 1144 |
'?', '?', '?', '?', |
| 1145 |
4, 0, 0, 0, |
| 1146 |
'?', |
| 1147 |
), |
| 1148 |
expectedLog: ` |
| 1149 |
==> wasi_snapshot_preview1.fd_pread(fd=4,iovs=1,iovs_len=2,offset=2) |
| 1150 |
<== (nread=4,errno=ESUCCESS) |
| 1151 |
`, |
| 1152 |
}, |
| 1153 |
} |
| 1154 |
|
| 1155 |
for _, tt := range tests { |
| 1156 |
tc := tt |
| 1157 |
t.Run(tc.name, func(t *testing.T) { |
| 1158 |
defer log.Reset() |
| 1159 |
|
| 1160 |
maskMemory(t, mod, len(tc.expectedMemory)) |
| 1161 |
|
| 1162 |
ok := mod.Memory().Write(0, initialMemory) |
| 1163 |
require.True(t, ok) |
| 1164 |
|
| 1165 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdPreadName, uint64(fd), uint64(iovs), uint64(iovsCount), uint64(tc.offset), uint64(resultNread)) |
| 1166 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 1167 |
|
| 1168 |
actual, ok := mod.Memory().Read(0, uint32(len(tc.expectedMemory))) |
| 1169 |
require.True(t, ok) |
| 1170 |
require.Equal(t, tc.expectedMemory, actual) |
| 1171 |
}) |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
func Test_fdPread_offset(t *testing.T) { |
| 1176 |
tmpDir := t.TempDir() |
| 1177 |
mod, fd, log, r := requireOpenFile(t, tmpDir, "test_path", []byte("wazero"), true) |
| 1178 |
defer r.Close(testCtx) |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
iovs := uint32(1) |
| 1183 |
initialMemory := []byte{ |
| 1184 |
'?', |
| 1185 |
18, 0, 0, 0, |
| 1186 |
4, 0, 0, 0, |
| 1187 |
23, 0, 0, 0, |
| 1188 |
2, 0, 0, 0, |
| 1189 |
'?', |
| 1190 |
} |
| 1191 |
iovsCount := uint32(2) |
| 1192 |
resultNread := uint32(26) |
| 1193 |
|
| 1194 |
expectedMemory := append( |
| 1195 |
initialMemory, |
| 1196 |
'z', 'e', 'r', 'o', |
| 1197 |
'?', '?', '?', '?', |
| 1198 |
4, 0, 0, 0, |
| 1199 |
'?', |
| 1200 |
) |
| 1201 |
|
| 1202 |
maskMemory(t, mod, len(expectedMemory)) |
| 1203 |
|
| 1204 |
ok := mod.Memory().Write(0, initialMemory) |
| 1205 |
require.True(t, ok) |
| 1206 |
|
| 1207 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdPreadName, uint64(fd), uint64(iovs), uint64(iovsCount), 2, uint64(resultNread)) |
| 1208 |
actual, ok := mod.Memory().Read(0, uint32(len(expectedMemory))) |
| 1209 |
require.True(t, ok) |
| 1210 |
require.Equal(t, expectedMemory, actual) |
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
expectedMemory = append( |
| 1215 |
initialMemory, |
| 1216 |
'w', 'a', 'z', 'e', |
| 1217 |
'?', |
| 1218 |
'r', 'o', |
| 1219 |
'?', |
| 1220 |
6, 0, 0, 0, |
| 1221 |
'?', |
| 1222 |
) |
| 1223 |
|
| 1224 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdReadName, uint64(fd), uint64(iovs), uint64(iovsCount), uint64(resultNread)) |
| 1225 |
actual, ok = mod.Memory().Read(0, uint32(len(expectedMemory))) |
| 1226 |
require.True(t, ok) |
| 1227 |
require.Equal(t, expectedMemory, actual) |
| 1228 |
|
| 1229 |
expectedLog := ` |
| 1230 |
==> wasi_snapshot_preview1.fd_pread(fd=4,iovs=1,iovs_len=2,offset=2) |
| 1231 |
<== (nread=4,errno=ESUCCESS) |
| 1232 |
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=1,iovs_len=2) |
| 1233 |
<== (nread=6,errno=ESUCCESS) |
| 1234 |
` |
| 1235 |
require.Equal(t, expectedLog, "\n"+log.String()) |
| 1236 |
} |
| 1237 |
|
| 1238 |
func Test_fdPread_Errors(t *testing.T) { |
| 1239 |
tmpDir := t.TempDir() |
| 1240 |
contents := []byte("wazero") |
| 1241 |
mod, fd, log, r := requireOpenFile(t, tmpDir, "test_path", contents, true) |
| 1242 |
defer r.Close(testCtx) |
| 1243 |
|
| 1244 |
tests := []struct { |
| 1245 |
name string |
| 1246 |
fd int32 |
| 1247 |
iovs, iovsCount, resultNread uint32 |
| 1248 |
offset int64 |
| 1249 |
memory []byte |
| 1250 |
expectedErrno wasip1.Errno |
| 1251 |
expectedLog string |
| 1252 |
}{ |
| 1253 |
{ |
| 1254 |
name: "invalid FD", |
| 1255 |
fd: 42, |
| 1256 |
memory: []byte{'?', '?', '?', '?'}, |
| 1257 |
expectedErrno: wasip1.ErrnoBadf, |
| 1258 |
expectedLog: ` |
| 1259 |
==> wasi_snapshot_preview1.fd_pread(fd=42,iovs=65532,iovs_len=0,offset=0) |
| 1260 |
<== (nread=,errno=EBADF) |
| 1261 |
`, |
| 1262 |
}, |
| 1263 |
{ |
| 1264 |
name: "out-of-memory reading iovs[0].offset", |
| 1265 |
fd: fd, |
| 1266 |
iovs: 1, |
| 1267 |
memory: []byte{'?'}, |
| 1268 |
expectedErrno: wasip1.ErrnoFault, |
| 1269 |
expectedLog: ` |
| 1270 |
==> wasi_snapshot_preview1.fd_pread(fd=4,iovs=65536,iovs_len=0,offset=0) |
| 1271 |
<== (nread=,errno=EFAULT) |
| 1272 |
`, |
| 1273 |
}, |
| 1274 |
{ |
| 1275 |
name: "out-of-memory reading iovs[0].length", |
| 1276 |
fd: fd, |
| 1277 |
iovs: 1, iovsCount: 1, |
| 1278 |
memory: []byte{ |
| 1279 |
'?', |
| 1280 |
9, 0, 0, 0, |
| 1281 |
}, |
| 1282 |
expectedErrno: wasip1.ErrnoFault, |
| 1283 |
expectedLog: ` |
| 1284 |
==> wasi_snapshot_preview1.fd_pread(fd=4,iovs=65532,iovs_len=1,offset=0) |
| 1285 |
<== (nread=,errno=EFAULT) |
| 1286 |
`, |
| 1287 |
}, |
| 1288 |
{ |
| 1289 |
name: "iovs[0].offset is outside memory", |
| 1290 |
fd: fd, |
| 1291 |
iovs: 1, iovsCount: 1, |
| 1292 |
memory: []byte{ |
| 1293 |
'?', |
| 1294 |
0, 0, 0x1, 0, |
| 1295 |
1, 0, 0, 0, |
| 1296 |
}, |
| 1297 |
expectedErrno: wasip1.ErrnoFault, |
| 1298 |
expectedLog: ` |
| 1299 |
==> wasi_snapshot_preview1.fd_pread(fd=4,iovs=65528,iovs_len=1,offset=0) |
| 1300 |
<== (nread=,errno=EFAULT) |
| 1301 |
`, |
| 1302 |
}, |
| 1303 |
{ |
| 1304 |
name: "length to read exceeds memory by 1", |
| 1305 |
fd: fd, |
| 1306 |
iovs: 1, iovsCount: 1, |
| 1307 |
memory: []byte{ |
| 1308 |
'?', |
| 1309 |
9, 0, 0, 0, |
| 1310 |
0, 0, 0x1, 0, |
| 1311 |
'?', |
| 1312 |
}, |
| 1313 |
expectedErrno: wasip1.ErrnoFault, |
| 1314 |
expectedLog: ` |
| 1315 |
==> wasi_snapshot_preview1.fd_pread(fd=4,iovs=65527,iovs_len=1,offset=0) |
| 1316 |
<== (nread=,errno=EFAULT) |
| 1317 |
`, |
| 1318 |
}, |
| 1319 |
{ |
| 1320 |
name: "resultNread offset is outside memory", |
| 1321 |
fd: fd, |
| 1322 |
iovs: 1, iovsCount: 1, |
| 1323 |
resultNread: 10, |
| 1324 |
memory: []byte{ |
| 1325 |
'?', |
| 1326 |
9, 0, 0, 0, |
| 1327 |
1, 0, 0, 0, |
| 1328 |
'?', |
| 1329 |
}, |
| 1330 |
expectedErrno: wasip1.ErrnoFault, |
| 1331 |
expectedLog: ` |
| 1332 |
==> wasi_snapshot_preview1.fd_pread(fd=4,iovs=65527,iovs_len=1,offset=0) |
| 1333 |
<== (nread=,errno=EFAULT) |
| 1334 |
`, |
| 1335 |
}, |
| 1336 |
{ |
| 1337 |
name: "offset negative", |
| 1338 |
fd: fd, |
| 1339 |
iovs: 1, iovsCount: 1, |
| 1340 |
resultNread: 10, |
| 1341 |
memory: []byte{ |
| 1342 |
'?', |
| 1343 |
9, 0, 0, 0, |
| 1344 |
1, 0, 0, 0, |
| 1345 |
'?', |
| 1346 |
'?', '?', '?', '?', |
| 1347 |
}, |
| 1348 |
offset: int64(-1), |
| 1349 |
expectedErrno: wasip1.ErrnoIo, |
| 1350 |
expectedLog: ` |
| 1351 |
==> wasi_snapshot_preview1.fd_pread(fd=4,iovs=65523,iovs_len=1,offset=-1) |
| 1352 |
<== (nread=,errno=EIO) |
| 1353 |
`, |
| 1354 |
}, |
| 1355 |
} |
| 1356 |
|
| 1357 |
for _, tt := range tests { |
| 1358 |
tc := tt |
| 1359 |
t.Run(tc.name, func(t *testing.T) { |
| 1360 |
defer log.Reset() |
| 1361 |
|
| 1362 |
offset := uint32(wasm.MemoryPagesToBytesNum(testMemoryPageSize) - uint64(len(tc.memory))) |
| 1363 |
|
| 1364 |
memoryWriteOK := mod.Memory().Write(offset, tc.memory) |
| 1365 |
require.True(t, memoryWriteOK) |
| 1366 |
|
| 1367 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdPreadName, uint64(tc.fd), uint64(tc.iovs+offset), uint64(tc.iovsCount), uint64(tc.offset), uint64(tc.resultNread+offset)) |
| 1368 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 1369 |
}) |
| 1370 |
} |
| 1371 |
} |
| 1372 |
|
| 1373 |
func Test_fdPrestatGet(t *testing.T) { |
| 1374 |
fsConfig := wazero.NewFSConfig().WithDirMount(t.TempDir(), "/") |
| 1375 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFSConfig(fsConfig)) |
| 1376 |
defer r.Close(testCtx) |
| 1377 |
|
| 1378 |
resultPrestat := uint32(1) |
| 1379 |
expectedMemory := []byte{ |
| 1380 |
'?', |
| 1381 |
0, |
| 1382 |
0, 0, 0, |
| 1383 |
|
| 1384 |
1, 0, 0, 0, |
| 1385 |
'?', |
| 1386 |
} |
| 1387 |
|
| 1388 |
maskMemory(t, mod, len(expectedMemory)) |
| 1389 |
|
| 1390 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdPrestatGetName, uint64(sys.FdPreopen), uint64(resultPrestat)) |
| 1391 |
require.Equal(t, ` |
| 1392 |
==> wasi_snapshot_preview1.fd_prestat_get(fd=3) |
| 1393 |
<== (prestat={pr_name_len=1},errno=ESUCCESS) |
| 1394 |
`, "\n"+log.String()) |
| 1395 |
|
| 1396 |
actual, ok := mod.Memory().Read(0, uint32(len(expectedMemory))) |
| 1397 |
require.True(t, ok) |
| 1398 |
require.Equal(t, expectedMemory, actual) |
| 1399 |
} |
| 1400 |
|
| 1401 |
func Test_fdPrestatGet_Errors(t *testing.T) { |
| 1402 |
mod, dirFD, log, r := requireOpenFile(t, t.TempDir(), "tmp", nil, true) |
| 1403 |
defer r.Close(testCtx) |
| 1404 |
|
| 1405 |
memorySize := mod.Memory().Size() |
| 1406 |
tests := []struct { |
| 1407 |
name string |
| 1408 |
fd int32 |
| 1409 |
resultPrestat uint32 |
| 1410 |
expectedErrno wasip1.Errno |
| 1411 |
expectedLog string |
| 1412 |
}{ |
| 1413 |
{ |
| 1414 |
name: "unopened FD", |
| 1415 |
fd: 42, |
| 1416 |
resultPrestat: 0, |
| 1417 |
expectedErrno: wasip1.ErrnoBadf, |
| 1418 |
expectedLog: ` |
| 1419 |
==> wasi_snapshot_preview1.fd_prestat_get(fd=42) |
| 1420 |
<== (prestat=,errno=EBADF) |
| 1421 |
`, |
| 1422 |
}, |
| 1423 |
{ |
| 1424 |
name: "not pre-opened FD", |
| 1425 |
fd: dirFD, |
| 1426 |
resultPrestat: 0, |
| 1427 |
expectedErrno: wasip1.ErrnoBadf, |
| 1428 |
expectedLog: ` |
| 1429 |
==> wasi_snapshot_preview1.fd_prestat_get(fd=4) |
| 1430 |
<== (prestat=,errno=EBADF) |
| 1431 |
`, |
| 1432 |
}, |
| 1433 |
{ |
| 1434 |
name: "out-of-memory resultPrestat", |
| 1435 |
fd: sys.FdPreopen, |
| 1436 |
resultPrestat: memorySize, |
| 1437 |
expectedErrno: wasip1.ErrnoFault, |
| 1438 |
expectedLog: ` |
| 1439 |
==> wasi_snapshot_preview1.fd_prestat_get(fd=3) |
| 1440 |
<== (prestat=,errno=EFAULT) |
| 1441 |
`, |
| 1442 |
}, |
| 1443 |
} |
| 1444 |
|
| 1445 |
for _, tt := range tests { |
| 1446 |
tc := tt |
| 1447 |
|
| 1448 |
t.Run(tc.name, func(t *testing.T) { |
| 1449 |
defer log.Reset() |
| 1450 |
|
| 1451 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdPrestatGetName, uint64(tc.fd), uint64(tc.resultPrestat)) |
| 1452 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 1453 |
}) |
| 1454 |
} |
| 1455 |
} |
| 1456 |
|
| 1457 |
func Test_fdPrestatDirName(t *testing.T) { |
| 1458 |
fsConfig := wazero.NewFSConfig().WithDirMount(t.TempDir(), "/") |
| 1459 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFSConfig(fsConfig)) |
| 1460 |
defer r.Close(testCtx) |
| 1461 |
|
| 1462 |
path := uint32(1) |
| 1463 |
pathLen := uint32(0) |
| 1464 |
expectedMemory := []byte{ |
| 1465 |
'?', '?', '?', '?', |
| 1466 |
} |
| 1467 |
|
| 1468 |
maskMemory(t, mod, len(expectedMemory)) |
| 1469 |
|
| 1470 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdPrestatDirNameName, uint64(sys.FdPreopen), uint64(path), uint64(pathLen)) |
| 1471 |
require.Equal(t, ` |
| 1472 |
==> wasi_snapshot_preview1.fd_prestat_dir_name(fd=3) |
| 1473 |
<== (path=,errno=ESUCCESS) |
| 1474 |
`, "\n"+log.String()) |
| 1475 |
|
| 1476 |
actual, ok := mod.Memory().Read(0, uint32(len(expectedMemory))) |
| 1477 |
require.True(t, ok) |
| 1478 |
require.Equal(t, expectedMemory, actual) |
| 1479 |
} |
| 1480 |
|
| 1481 |
func Test_fdPrestatDirName_Errors(t *testing.T) { |
| 1482 |
mod, dirFD, log, r := requireOpenFile(t, t.TempDir(), "tmp", nil, true) |
| 1483 |
defer r.Close(testCtx) |
| 1484 |
|
| 1485 |
memorySize := mod.Memory().Size() |
| 1486 |
maskMemory(t, mod, 10) |
| 1487 |
|
| 1488 |
validAddress := uint32(0) |
| 1489 |
pathLen := uint32(len("/")) |
| 1490 |
|
| 1491 |
tests := []struct { |
| 1492 |
name string |
| 1493 |
fd int32 |
| 1494 |
path uint32 |
| 1495 |
pathLen uint32 |
| 1496 |
expectedErrno wasip1.Errno |
| 1497 |
expectedLog string |
| 1498 |
}{ |
| 1499 |
{ |
| 1500 |
name: "out-of-memory path", |
| 1501 |
fd: sys.FdPreopen, |
| 1502 |
path: memorySize, |
| 1503 |
pathLen: pathLen, |
| 1504 |
expectedErrno: wasip1.ErrnoFault, |
| 1505 |
expectedLog: ` |
| 1506 |
==> wasi_snapshot_preview1.fd_prestat_dir_name(fd=3) |
| 1507 |
<== (path=,errno=EFAULT) |
| 1508 |
`, |
| 1509 |
}, |
| 1510 |
{ |
| 1511 |
name: "path exceeds the maximum valid address by 1", |
| 1512 |
fd: sys.FdPreopen, |
| 1513 |
path: memorySize - pathLen + 1, |
| 1514 |
pathLen: pathLen, |
| 1515 |
expectedErrno: wasip1.ErrnoFault, |
| 1516 |
expectedLog: ` |
| 1517 |
==> wasi_snapshot_preview1.fd_prestat_dir_name(fd=3) |
| 1518 |
<== (path=,errno=EFAULT) |
| 1519 |
`, |
| 1520 |
}, |
| 1521 |
{ |
| 1522 |
name: "pathLen exceeds the length of the dir name", |
| 1523 |
fd: sys.FdPreopen, |
| 1524 |
path: validAddress, |
| 1525 |
pathLen: pathLen + 1, |
| 1526 |
expectedErrno: wasip1.ErrnoNametoolong, |
| 1527 |
expectedLog: ` |
| 1528 |
==> wasi_snapshot_preview1.fd_prestat_dir_name(fd=3) |
| 1529 |
<== (path=,errno=ENAMETOOLONG) |
| 1530 |
`, |
| 1531 |
}, |
| 1532 |
{ |
| 1533 |
name: "unopened FD", |
| 1534 |
fd: 42, |
| 1535 |
path: validAddress, |
| 1536 |
pathLen: pathLen, |
| 1537 |
expectedErrno: wasip1.ErrnoBadf, |
| 1538 |
expectedLog: ` |
| 1539 |
==> wasi_snapshot_preview1.fd_prestat_dir_name(fd=42) |
| 1540 |
<== (path=,errno=EBADF) |
| 1541 |
`, |
| 1542 |
}, |
| 1543 |
{ |
| 1544 |
name: "not pre-opened FD", |
| 1545 |
fd: dirFD, |
| 1546 |
path: validAddress, |
| 1547 |
pathLen: pathLen, |
| 1548 |
expectedErrno: wasip1.ErrnoBadf, |
| 1549 |
expectedLog: ` |
| 1550 |
==> wasi_snapshot_preview1.fd_prestat_dir_name(fd=4) |
| 1551 |
<== (path=,errno=EBADF) |
| 1552 |
`, |
| 1553 |
}, |
| 1554 |
} |
| 1555 |
|
| 1556 |
for _, tt := range tests { |
| 1557 |
tc := tt |
| 1558 |
|
| 1559 |
t.Run(tc.name, func(t *testing.T) { |
| 1560 |
defer log.Reset() |
| 1561 |
|
| 1562 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdPrestatDirNameName, uint64(tc.fd), uint64(tc.path), uint64(tc.pathLen)) |
| 1563 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 1564 |
}) |
| 1565 |
} |
| 1566 |
} |
| 1567 |
|
| 1568 |
func Test_fdPwrite(t *testing.T) { |
| 1569 |
tmpDir := t.TempDir() |
| 1570 |
pathName := "test_path" |
| 1571 |
mod, fd, log, r := requireOpenFile(t, tmpDir, pathName, []byte{}, false) |
| 1572 |
defer r.Close(testCtx) |
| 1573 |
|
| 1574 |
iovs := uint32(1) |
| 1575 |
initialMemory := []byte{ |
| 1576 |
'?', |
| 1577 |
18, 0, 0, 0, |
| 1578 |
4, 0, 0, 0, |
| 1579 |
23, 0, 0, 0, |
| 1580 |
2, 0, 0, 0, |
| 1581 |
'?', |
| 1582 |
'w', 'a', 'z', 'e', |
| 1583 |
'?', |
| 1584 |
'r', 'o', |
| 1585 |
} |
| 1586 |
|
| 1587 |
iovsCount := uint32(2) |
| 1588 |
resultNwritten := len(initialMemory) + 1 |
| 1589 |
|
| 1590 |
tests := []struct { |
| 1591 |
name string |
| 1592 |
offset int64 |
| 1593 |
expectedMemory []byte |
| 1594 |
expectedContents string |
| 1595 |
expectedLog string |
| 1596 |
}{ |
| 1597 |
{ |
| 1598 |
name: "offset zero", |
| 1599 |
offset: 0, |
| 1600 |
expectedMemory: append( |
| 1601 |
initialMemory, |
| 1602 |
'?', |
| 1603 |
6, 0, 0, 0, |
| 1604 |
'?', |
| 1605 |
), |
| 1606 |
expectedContents: "wazero", |
| 1607 |
expectedLog: ` |
| 1608 |
==> wasi_snapshot_preview1.fd_pwrite(fd=4,iovs=1,iovs_len=2,offset=0) |
| 1609 |
<== (nwritten=6,errno=ESUCCESS) |
| 1610 |
`, |
| 1611 |
}, |
| 1612 |
{ |
| 1613 |
name: "offset 2", |
| 1614 |
offset: 2, |
| 1615 |
expectedMemory: append( |
| 1616 |
initialMemory, |
| 1617 |
'?', |
| 1618 |
6, 0, 0, 0, |
| 1619 |
'?', |
| 1620 |
), |
| 1621 |
expectedContents: "wawazero", |
| 1622 |
expectedLog: ` |
| 1623 |
==> wasi_snapshot_preview1.fd_pwrite(fd=4,iovs=1,iovs_len=2,offset=2) |
| 1624 |
<== (nwritten=6,errno=ESUCCESS) |
| 1625 |
`, |
| 1626 |
}, |
| 1627 |
} |
| 1628 |
|
| 1629 |
for _, tt := range tests { |
| 1630 |
tc := tt |
| 1631 |
t.Run(tc.name, func(t *testing.T) { |
| 1632 |
defer log.Reset() |
| 1633 |
|
| 1634 |
maskMemory(t, mod, len(tc.expectedMemory)) |
| 1635 |
|
| 1636 |
ok := mod.Memory().Write(0, initialMemory) |
| 1637 |
require.True(t, ok) |
| 1638 |
|
| 1639 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdPwriteName, uint64(fd), uint64(iovs), uint64(iovsCount), uint64(tc.offset), uint64(resultNwritten)) |
| 1640 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 1641 |
|
| 1642 |
actual, ok := mod.Memory().Read(0, uint32(len(tc.expectedMemory))) |
| 1643 |
require.True(t, ok) |
| 1644 |
require.Equal(t, tc.expectedMemory, actual) |
| 1645 |
|
| 1646 |
|
| 1647 |
b, err := os.ReadFile(joinPath(tmpDir, pathName)) |
| 1648 |
require.NoError(t, err) |
| 1649 |
require.Equal(t, tc.expectedContents, string(b)) |
| 1650 |
}) |
| 1651 |
} |
| 1652 |
} |
| 1653 |
|
| 1654 |
func Test_fdPwrite_offset(t *testing.T) { |
| 1655 |
tmpDir := t.TempDir() |
| 1656 |
pathName := "test_path" |
| 1657 |
mod, fd, log, r := requireOpenFile(t, tmpDir, pathName, []byte{}, false) |
| 1658 |
defer r.Close(testCtx) |
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
iovs := uint32(1) |
| 1663 |
pwriteMemory := []byte{ |
| 1664 |
'?', |
| 1665 |
10, 0, 0, 0, |
| 1666 |
3, 0, 0, 0, |
| 1667 |
'?', |
| 1668 |
'e', 'r', 'o', |
| 1669 |
'?', |
| 1670 |
} |
| 1671 |
iovsCount := uint32(1) |
| 1672 |
resultNwritten := len(pwriteMemory) + 4 |
| 1673 |
|
| 1674 |
expectedMemory := append( |
| 1675 |
pwriteMemory, |
| 1676 |
'?', '?', '?', '?', |
| 1677 |
3, 0, 0, 0, |
| 1678 |
'?', |
| 1679 |
) |
| 1680 |
|
| 1681 |
maskMemory(t, mod, len(expectedMemory)) |
| 1682 |
|
| 1683 |
ok := mod.Memory().Write(0, pwriteMemory) |
| 1684 |
require.True(t, ok) |
| 1685 |
|
| 1686 |
|
| 1687 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdPwriteName, uint64(fd), uint64(iovs), uint64(iovsCount), 3, uint64(resultNwritten)) |
| 1688 |
actual, ok := mod.Memory().Read(0, uint32(len(expectedMemory))) |
| 1689 |
require.True(t, ok) |
| 1690 |
require.Equal(t, expectedMemory, actual) |
| 1691 |
|
| 1692 |
|
| 1693 |
writeMemory := []byte{ |
| 1694 |
'?', |
| 1695 |
10, 0, 0, 0, |
| 1696 |
3, 0, 0, 0, |
| 1697 |
'?', |
| 1698 |
'w', 'a', 'z', |
| 1699 |
'?', |
| 1700 |
} |
| 1701 |
expectedMemory = append( |
| 1702 |
writeMemory, |
| 1703 |
'?', '?', '?', '?', |
| 1704 |
3, 0, 0, 0, |
| 1705 |
'?', |
| 1706 |
) |
| 1707 |
|
| 1708 |
ok = mod.Memory().Write(0, writeMemory) |
| 1709 |
require.True(t, ok) |
| 1710 |
|
| 1711 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdWriteName, uint64(fd), uint64(iovs), uint64(iovsCount), uint64(resultNwritten)) |
| 1712 |
actual, ok = mod.Memory().Read(0, uint32(len(expectedMemory))) |
| 1713 |
require.True(t, ok) |
| 1714 |
require.Equal(t, expectedMemory, actual) |
| 1715 |
|
| 1716 |
expectedLog := ` |
| 1717 |
==> wasi_snapshot_preview1.fd_pwrite(fd=4,iovs=1,iovs_len=1,offset=3) |
| 1718 |
<== (nwritten=3,errno=ESUCCESS) |
| 1719 |
==> wasi_snapshot_preview1.fd_write(fd=4,iovs=1,iovs_len=1) |
| 1720 |
<== (nwritten=3,errno=ESUCCESS) |
| 1721 |
` |
| 1722 |
require.Equal(t, expectedLog, "\n"+log.String()) |
| 1723 |
|
| 1724 |
|
| 1725 |
b, err := os.ReadFile(joinPath(tmpDir, pathName)) |
| 1726 |
require.NoError(t, err) |
| 1727 |
require.Equal(t, "wazero", string(b)) |
| 1728 |
} |
| 1729 |
|
| 1730 |
func Test_fdPwrite_Errors(t *testing.T) { |
| 1731 |
tmpDir := t.TempDir() |
| 1732 |
pathName := "test_path" |
| 1733 |
mod, fd, log, r := requireOpenFile(t, tmpDir, pathName, []byte{}, false) |
| 1734 |
defer r.Close(testCtx) |
| 1735 |
|
| 1736 |
tests := []struct { |
| 1737 |
name string |
| 1738 |
fd int32 |
| 1739 |
iovs, iovsCount, resultNwritten uint32 |
| 1740 |
offset int64 |
| 1741 |
memory []byte |
| 1742 |
expectedErrno wasip1.Errno |
| 1743 |
expectedLog string |
| 1744 |
}{ |
| 1745 |
{ |
| 1746 |
name: "invalid FD", |
| 1747 |
fd: 42, |
| 1748 |
memory: []byte{'?', '?', '?', '?'}, |
| 1749 |
expectedErrno: wasip1.ErrnoBadf, |
| 1750 |
expectedLog: ` |
| 1751 |
==> wasi_snapshot_preview1.fd_pwrite(fd=42,iovs=65532,iovs_len=0,offset=0) |
| 1752 |
<== (nwritten=,errno=EBADF) |
| 1753 |
`, |
| 1754 |
}, |
| 1755 |
{ |
| 1756 |
name: "out-of-memory writing iovs[0].offset", |
| 1757 |
fd: fd, |
| 1758 |
iovs: 1, |
| 1759 |
memory: []byte{'?'}, |
| 1760 |
expectedErrno: wasip1.ErrnoFault, |
| 1761 |
expectedLog: ` |
| 1762 |
==> wasi_snapshot_preview1.fd_pwrite(fd=4,iovs=65536,iovs_len=0,offset=0) |
| 1763 |
<== (nwritten=,errno=EFAULT) |
| 1764 |
`, |
| 1765 |
}, |
| 1766 |
{ |
| 1767 |
name: "out-of-memory writing iovs[0].length", |
| 1768 |
fd: fd, |
| 1769 |
iovs: 1, iovsCount: 1, |
| 1770 |
memory: []byte{ |
| 1771 |
'?', |
| 1772 |
9, 0, 0, 0, |
| 1773 |
}, |
| 1774 |
expectedErrno: wasip1.ErrnoFault, |
| 1775 |
expectedLog: ` |
| 1776 |
==> wasi_snapshot_preview1.fd_pwrite(fd=4,iovs=65532,iovs_len=1,offset=0) |
| 1777 |
<== (nwritten=,errno=EFAULT) |
| 1778 |
`, |
| 1779 |
}, |
| 1780 |
{ |
| 1781 |
name: "iovs[0].offset is outside memory", |
| 1782 |
fd: fd, |
| 1783 |
iovs: 1, iovsCount: 1, |
| 1784 |
memory: []byte{ |
| 1785 |
'?', |
| 1786 |
0, 0, 0x1, 0, |
| 1787 |
1, 0, 0, 0, |
| 1788 |
}, |
| 1789 |
expectedErrno: wasip1.ErrnoFault, |
| 1790 |
expectedLog: ` |
| 1791 |
==> wasi_snapshot_preview1.fd_pwrite(fd=4,iovs=65528,iovs_len=1,offset=0) |
| 1792 |
<== (nwritten=,errno=EFAULT) |
| 1793 |
`, |
| 1794 |
}, |
| 1795 |
{ |
| 1796 |
name: "length to write exceeds memory by 1", |
| 1797 |
fd: fd, |
| 1798 |
iovs: 1, iovsCount: 1, |
| 1799 |
memory: []byte{ |
| 1800 |
'?', |
| 1801 |
9, 0, 0, 0, |
| 1802 |
0, 0, 0x1, 0, |
| 1803 |
'?', |
| 1804 |
}, |
| 1805 |
expectedErrno: wasip1.ErrnoFault, |
| 1806 |
expectedLog: ` |
| 1807 |
==> wasi_snapshot_preview1.fd_pwrite(fd=4,iovs=65527,iovs_len=1,offset=0) |
| 1808 |
<== (nwritten=,errno=EFAULT) |
| 1809 |
`, |
| 1810 |
}, |
| 1811 |
{ |
| 1812 |
name: "resultNwritten offset is outside memory", |
| 1813 |
fd: fd, |
| 1814 |
iovs: 1, iovsCount: 1, |
| 1815 |
resultNwritten: 10, |
| 1816 |
memory: []byte{ |
| 1817 |
'?', |
| 1818 |
9, 0, 0, 0, |
| 1819 |
1, 0, 0, 0, |
| 1820 |
'?', |
| 1821 |
}, |
| 1822 |
expectedErrno: wasip1.ErrnoFault, |
| 1823 |
expectedLog: ` |
| 1824 |
==> wasi_snapshot_preview1.fd_pwrite(fd=4,iovs=65527,iovs_len=1,offset=0) |
| 1825 |
<== (nwritten=,errno=EFAULT) |
| 1826 |
`, |
| 1827 |
}, |
| 1828 |
{ |
| 1829 |
name: "offset negative", |
| 1830 |
fd: fd, |
| 1831 |
iovs: 1, iovsCount: 1, |
| 1832 |
resultNwritten: 10, |
| 1833 |
memory: []byte{ |
| 1834 |
'?', |
| 1835 |
9, 0, 0, 0, |
| 1836 |
1, 0, 0, 0, |
| 1837 |
'?', |
| 1838 |
'?', '?', '?', '?', |
| 1839 |
}, |
| 1840 |
offset: int64(-1), |
| 1841 |
expectedErrno: wasip1.ErrnoIo, |
| 1842 |
expectedLog: ` |
| 1843 |
==> wasi_snapshot_preview1.fd_pwrite(fd=4,iovs=65523,iovs_len=1,offset=-1) |
| 1844 |
<== (nwritten=,errno=EIO) |
| 1845 |
`, |
| 1846 |
}, |
| 1847 |
} |
| 1848 |
|
| 1849 |
for _, tt := range tests { |
| 1850 |
tc := tt |
| 1851 |
t.Run(tc.name, func(t *testing.T) { |
| 1852 |
defer log.Reset() |
| 1853 |
|
| 1854 |
offset := uint32(wasm.MemoryPagesToBytesNum(testMemoryPageSize) - uint64(len(tc.memory))) |
| 1855 |
|
| 1856 |
memoryWriteOK := mod.Memory().Write(offset, tc.memory) |
| 1857 |
require.True(t, memoryWriteOK) |
| 1858 |
|
| 1859 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdPwriteName, uint64(tc.fd), uint64(tc.iovs+offset), uint64(tc.iovsCount), uint64(tc.offset), uint64(tc.resultNwritten+offset)) |
| 1860 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 1861 |
}) |
| 1862 |
} |
| 1863 |
} |
| 1864 |
|
| 1865 |
func Test_fdRead(t *testing.T) { |
| 1866 |
mod, fd, log, r := requireOpenFile(t, t.TempDir(), "test_path", []byte("wazero"), true) |
| 1867 |
defer r.Close(testCtx) |
| 1868 |
|
| 1869 |
iovs := uint32(1) |
| 1870 |
initialMemory := []byte{ |
| 1871 |
'?', |
| 1872 |
26, 0, 0, 0, |
| 1873 |
4, 0, 0, 0, |
| 1874 |
31, 0, 0, 0, |
| 1875 |
0, 0, 0, 0, |
| 1876 |
31, 0, 0, 0, |
| 1877 |
2, 0, 0, 0, |
| 1878 |
'?', |
| 1879 |
} |
| 1880 |
iovsCount := uint32(3) |
| 1881 |
resultNread := uint32(34) |
| 1882 |
expectedMemory := append( |
| 1883 |
initialMemory, |
| 1884 |
'w', 'a', 'z', 'e', |
| 1885 |
'?', |
| 1886 |
'r', 'o', |
| 1887 |
'?', |
| 1888 |
6, 0, 0, 0, |
| 1889 |
'?', |
| 1890 |
) |
| 1891 |
|
| 1892 |
maskMemory(t, mod, len(expectedMemory)) |
| 1893 |
|
| 1894 |
ok := mod.Memory().Write(0, initialMemory) |
| 1895 |
require.True(t, ok) |
| 1896 |
|
| 1897 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdReadName, uint64(fd), uint64(iovs), uint64(iovsCount), uint64(resultNread)) |
| 1898 |
require.Equal(t, ` |
| 1899 |
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=1,iovs_len=3) |
| 1900 |
<== (nread=6,errno=ESUCCESS) |
| 1901 |
`, "\n"+log.String()) |
| 1902 |
|
| 1903 |
actual, ok := mod.Memory().Read(0, uint32(len(expectedMemory))) |
| 1904 |
require.True(t, ok) |
| 1905 |
require.Equal(t, expectedMemory, actual) |
| 1906 |
} |
| 1907 |
|
| 1908 |
func Test_fdRead_Errors(t *testing.T) { |
| 1909 |
mod, fd, log, r := requireOpenFile(t, t.TempDir(), "test_path", []byte("wazero"), true) |
| 1910 |
defer r.Close(testCtx) |
| 1911 |
|
| 1912 |
tests := []struct { |
| 1913 |
name string |
| 1914 |
fd int32 |
| 1915 |
iovs, iovsCount, resultNread uint32 |
| 1916 |
memory []byte |
| 1917 |
expectedErrno wasip1.Errno |
| 1918 |
expectedLog string |
| 1919 |
}{ |
| 1920 |
{ |
| 1921 |
name: "invalid FD", |
| 1922 |
fd: 42, |
| 1923 |
memory: []byte{'?', '?', '?', '?'}, |
| 1924 |
expectedErrno: wasip1.ErrnoBadf, |
| 1925 |
expectedLog: ` |
| 1926 |
==> wasi_snapshot_preview1.fd_read(fd=42,iovs=65532,iovs_len=65532) |
| 1927 |
<== (nread=,errno=EBADF) |
| 1928 |
`, |
| 1929 |
}, |
| 1930 |
{ |
| 1931 |
name: "out-of-memory reading iovs[0].offset", |
| 1932 |
fd: fd, |
| 1933 |
iovs: 1, |
| 1934 |
memory: []byte{'?'}, |
| 1935 |
expectedErrno: wasip1.ErrnoFault, |
| 1936 |
expectedLog: ` |
| 1937 |
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=65536,iovs_len=65535) |
| 1938 |
<== (nread=,errno=EFAULT) |
| 1939 |
`, |
| 1940 |
}, |
| 1941 |
{ |
| 1942 |
name: "out-of-memory reading iovs[0].length", |
| 1943 |
fd: fd, |
| 1944 |
iovs: 1, iovsCount: 1, |
| 1945 |
memory: []byte{ |
| 1946 |
'?', |
| 1947 |
9, 0, 0, 0, |
| 1948 |
}, |
| 1949 |
expectedErrno: wasip1.ErrnoFault, |
| 1950 |
expectedLog: ` |
| 1951 |
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=65532,iovs_len=65532) |
| 1952 |
<== (nread=,errno=EFAULT) |
| 1953 |
`, |
| 1954 |
}, |
| 1955 |
{ |
| 1956 |
name: "iovs[0].offset is outside memory", |
| 1957 |
fd: fd, |
| 1958 |
iovs: 1, iovsCount: 1, |
| 1959 |
memory: []byte{ |
| 1960 |
'?', |
| 1961 |
0, 0, 0x1, 0, |
| 1962 |
1, 0, 0, 0, |
| 1963 |
}, |
| 1964 |
expectedErrno: wasip1.ErrnoFault, |
| 1965 |
expectedLog: ` |
| 1966 |
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=65528,iovs_len=65528) |
| 1967 |
<== (nread=,errno=EFAULT) |
| 1968 |
`, |
| 1969 |
}, |
| 1970 |
{ |
| 1971 |
name: "length to read exceeds memory by 1", |
| 1972 |
fd: fd, |
| 1973 |
iovs: 1, iovsCount: 1, |
| 1974 |
memory: []byte{ |
| 1975 |
'?', |
| 1976 |
9, 0, 0, 0, |
| 1977 |
0, 0, 0x1, 0, |
| 1978 |
'?', |
| 1979 |
}, |
| 1980 |
expectedErrno: wasip1.ErrnoFault, |
| 1981 |
expectedLog: ` |
| 1982 |
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=65527,iovs_len=65527) |
| 1983 |
<== (nread=,errno=EFAULT) |
| 1984 |
`, |
| 1985 |
}, |
| 1986 |
{ |
| 1987 |
name: "resultNread offset is outside memory", |
| 1988 |
fd: fd, |
| 1989 |
iovs: 1, iovsCount: 1, |
| 1990 |
resultNread: 10, |
| 1991 |
memory: []byte{ |
| 1992 |
'?', |
| 1993 |
9, 0, 0, 0, |
| 1994 |
1, 0, 0, 0, |
| 1995 |
'?', |
| 1996 |
}, |
| 1997 |
expectedErrno: wasip1.ErrnoFault, |
| 1998 |
expectedLog: ` |
| 1999 |
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=65527,iovs_len=65527) |
| 2000 |
<== (nread=,errno=EFAULT) |
| 2001 |
`, |
| 2002 |
}, |
| 2003 |
} |
| 2004 |
|
| 2005 |
for _, tt := range tests { |
| 2006 |
tc := tt |
| 2007 |
t.Run(tc.name, func(t *testing.T) { |
| 2008 |
defer log.Reset() |
| 2009 |
|
| 2010 |
offset := uint32(wasm.MemoryPagesToBytesNum(testMemoryPageSize) - uint64(len(tc.memory))) |
| 2011 |
|
| 2012 |
memoryWriteOK := mod.Memory().Write(offset, tc.memory) |
| 2013 |
require.True(t, memoryWriteOK) |
| 2014 |
|
| 2015 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdReadName, uint64(tc.fd), uint64(tc.iovs+offset), uint64(tc.iovsCount+offset), uint64(tc.resultNread+offset)) |
| 2016 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 2017 |
}) |
| 2018 |
} |
| 2019 |
} |
| 2020 |
|
| 2021 |
var ( |
| 2022 |
direntDot = []byte{ |
| 2023 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 2024 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 2025 |
1, 0, 0, 0, |
| 2026 |
3, 0, 0, 0, |
| 2027 |
'.', |
| 2028 |
} |
| 2029 |
direntDotDot = []byte{ |
| 2030 |
2, 0, 0, 0, 0, 0, 0, 0, |
| 2031 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 2032 |
2, 0, 0, 0, |
| 2033 |
3, 0, 0, 0, |
| 2034 |
'.', '.', |
| 2035 |
} |
| 2036 |
dirent1 = []byte{ |
| 2037 |
3, 0, 0, 0, 0, 0, 0, 0, |
| 2038 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 2039 |
1, 0, 0, 0, |
| 2040 |
4, 0, 0, 0, |
| 2041 |
'-', |
| 2042 |
} |
| 2043 |
dirent2 = []byte{ |
| 2044 |
4, 0, 0, 0, 0, 0, 0, 0, |
| 2045 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 2046 |
2, 0, 0, 0, |
| 2047 |
3, 0, 0, 0, |
| 2048 |
'a', '-', |
| 2049 |
} |
| 2050 |
dirent3 = []byte{ |
| 2051 |
5, 0, 0, 0, 0, 0, 0, 0, |
| 2052 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 2053 |
3, 0, 0, 0, |
| 2054 |
4, 0, 0, 0, |
| 2055 |
'a', 'b', '-', |
| 2056 |
} |
| 2057 |
|
| 2058 |
|
| 2059 |
|
| 2060 |
|
| 2061 |
|
| 2062 |
|
| 2063 |
|
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
|
| 2068 |
dirents = bytes.Join([][]byte{ |
| 2069 |
direntDot, |
| 2070 |
direntDotDot, |
| 2071 |
dirent1, |
| 2072 |
dirent2, |
| 2073 |
dirent3, |
| 2074 |
|
| 2075 |
}, nil) |
| 2076 |
) |
| 2077 |
|
| 2078 |
func Test_fdReaddir(t *testing.T) { |
| 2079 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(fstest.FS)) |
| 2080 |
defer r.Close(testCtx) |
| 2081 |
|
| 2082 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 2083 |
preopen := getPreopen(t, fsc) |
| 2084 |
fd := sys.FdPreopen + 1 |
| 2085 |
|
| 2086 |
tests := []struct { |
| 2087 |
name string |
| 2088 |
initialDir string |
| 2089 |
dir func() |
| 2090 |
bufLen uint32 |
| 2091 |
cookie int64 |
| 2092 |
expectedMem []byte |
| 2093 |
expectedMemSize int |
| 2094 |
expectedBufused uint32 |
| 2095 |
}{ |
| 2096 |
{ |
| 2097 |
name: "empty dir", |
| 2098 |
initialDir: "emptydir", |
| 2099 |
bufLen: wasip1.DirentSize + 1, |
| 2100 |
cookie: 0, |
| 2101 |
expectedBufused: wasip1.DirentSize + 1, |
| 2102 |
expectedMem: direntDot, |
| 2103 |
}, |
| 2104 |
{ |
| 2105 |
name: "full read", |
| 2106 |
initialDir: "dir", |
| 2107 |
bufLen: 4096, |
| 2108 |
cookie: 0, |
| 2109 |
expectedBufused: 129, |
| 2110 |
expectedMem: dirents, |
| 2111 |
}, |
| 2112 |
{ |
| 2113 |
name: "can't read name", |
| 2114 |
initialDir: "dir", |
| 2115 |
bufLen: wasip1.DirentSize, |
| 2116 |
cookie: 0, |
| 2117 |
expectedBufused: wasip1.DirentSize, |
| 2118 |
expectedMem: direntDot[:wasip1.DirentSize], |
| 2119 |
}, |
| 2120 |
{ |
| 2121 |
name: "read exactly first", |
| 2122 |
initialDir: "dir", |
| 2123 |
bufLen: 25, |
| 2124 |
cookie: 0, |
| 2125 |
expectedBufused: 25, |
| 2126 |
expectedMem: direntDot, |
| 2127 |
}, |
| 2128 |
{ |
| 2129 |
name: "read exactly second", |
| 2130 |
initialDir: "dir", |
| 2131 |
dir: func() { |
| 2132 |
f, _ := fsc.LookupFile(fd) |
| 2133 |
rdd, _ := f.DirentCache() |
| 2134 |
_, _ = rdd.Read(0, 1) |
| 2135 |
}, |
| 2136 |
bufLen: 27, |
| 2137 |
cookie: 1, |
| 2138 |
expectedBufused: 27, |
| 2139 |
expectedMem: direntDotDot, |
| 2140 |
}, |
| 2141 |
{ |
| 2142 |
name: "read second and a little more", |
| 2143 |
initialDir: "dir", |
| 2144 |
dir: func() { |
| 2145 |
f, _ := fsc.LookupFile(fd) |
| 2146 |
rdd, _ := f.DirentCache() |
| 2147 |
_, _ = rdd.Read(0, 1) |
| 2148 |
}, |
| 2149 |
bufLen: 30, |
| 2150 |
cookie: 1, |
| 2151 |
expectedBufused: 30, |
| 2152 |
expectedMem: direntDotDot, |
| 2153 |
expectedMemSize: len(direntDotDot), |
| 2154 |
}, |
| 2155 |
{ |
| 2156 |
name: "read second and header of third", |
| 2157 |
initialDir: "dir", |
| 2158 |
dir: func() { |
| 2159 |
f, _ := fsc.LookupFile(fd) |
| 2160 |
rdd, _ := f.DirentCache() |
| 2161 |
_, _ = rdd.Read(0, 1) |
| 2162 |
}, |
| 2163 |
bufLen: 50, |
| 2164 |
cookie: 1, |
| 2165 |
expectedBufused: 50, |
| 2166 |
expectedMem: append(direntDotDot, dirent1[0:24]...), |
| 2167 |
}, |
| 2168 |
{ |
| 2169 |
name: "read second and third", |
| 2170 |
initialDir: "dir", |
| 2171 |
dir: func() { |
| 2172 |
f, _ := fsc.LookupFile(fd) |
| 2173 |
rdd, _ := f.DirentCache() |
| 2174 |
_, _ = rdd.Read(0, 1) |
| 2175 |
}, |
| 2176 |
bufLen: 53, |
| 2177 |
cookie: 1, |
| 2178 |
expectedBufused: 53, |
| 2179 |
expectedMem: append(direntDotDot, dirent1...), |
| 2180 |
}, |
| 2181 |
{ |
| 2182 |
name: "read exactly third", |
| 2183 |
initialDir: "dir", |
| 2184 |
dir: func() { |
| 2185 |
f, _ := fsc.LookupFile(fd) |
| 2186 |
rdd, _ := f.DirentCache() |
| 2187 |
_, _ = rdd.Read(0, 2) |
| 2188 |
}, |
| 2189 |
bufLen: 27, |
| 2190 |
cookie: 2, |
| 2191 |
expectedBufused: 27, |
| 2192 |
expectedMem: dirent1, |
| 2193 |
}, |
| 2194 |
{ |
| 2195 |
name: "read third and beyond", |
| 2196 |
initialDir: "dir", |
| 2197 |
dir: func() { |
| 2198 |
f, _ := fsc.LookupFile(fd) |
| 2199 |
rdd, _ := f.DirentCache() |
| 2200 |
_, _ = rdd.Read(0, 2) |
| 2201 |
}, |
| 2202 |
bufLen: 300, |
| 2203 |
cookie: 2, |
| 2204 |
expectedBufused: 78, |
| 2205 |
expectedMem: append(dirent1, dirent2...), |
| 2206 |
}, |
| 2207 |
{ |
| 2208 |
name: "read exhausted directory", |
| 2209 |
initialDir: "dir", |
| 2210 |
dir: func() { |
| 2211 |
f, _ := fsc.LookupFile(fd) |
| 2212 |
rdd, _ := f.DirentCache() |
| 2213 |
_, _ = rdd.Read(0, 5) |
| 2214 |
}, |
| 2215 |
bufLen: 300, |
| 2216 |
cookie: 5, |
| 2217 |
expectedBufused: 0, |
| 2218 |
}, |
| 2219 |
} |
| 2220 |
|
| 2221 |
for _, tt := range tests { |
| 2222 |
tc := tt |
| 2223 |
t.Run(tc.name, func(t *testing.T) { |
| 2224 |
defer log.Reset() |
| 2225 |
|
| 2226 |
fd, errno := fsc.OpenFile(preopen, tc.initialDir, experimentalsys.O_RDONLY, 0) |
| 2227 |
require.EqualErrno(t, 0, errno) |
| 2228 |
defer fsc.CloseFile(fd) |
| 2229 |
|
| 2230 |
if tc.dir != nil { |
| 2231 |
tc.dir() |
| 2232 |
} |
| 2233 |
|
| 2234 |
maskMemory(t, mod, int(tc.bufLen)) |
| 2235 |
|
| 2236 |
resultBufused := uint32(0) |
| 2237 |
buf := uint32(8) |
| 2238 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdReaddirName, |
| 2239 |
uint64(fd), uint64(buf), uint64(tc.bufLen), uint64(tc.cookie), uint64(resultBufused)) |
| 2240 |
|
| 2241 |
|
| 2242 |
bufused, ok := mod.Memory().ReadUint32Le(resultBufused) |
| 2243 |
require.True(t, ok) |
| 2244 |
require.Equal(t, tc.expectedBufused, bufused) |
| 2245 |
|
| 2246 |
mem, ok := mod.Memory().Read(buf, bufused) |
| 2247 |
require.True(t, ok) |
| 2248 |
|
| 2249 |
if tc.expectedMem != nil { |
| 2250 |
if tc.expectedMemSize == 0 { |
| 2251 |
tc.expectedMemSize = len(tc.expectedMem) |
| 2252 |
} |
| 2253 |
require.Equal(t, tc.expectedMem, mem[:tc.expectedMemSize]) |
| 2254 |
} |
| 2255 |
}) |
| 2256 |
} |
| 2257 |
} |
| 2258 |
|
| 2259 |
|
| 2260 |
func Test_fdReaddir_Rewind(t *testing.T) { |
| 2261 |
tmpDir := t.TempDir() |
| 2262 |
|
| 2263 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(os.DirFS(tmpDir))) |
| 2264 |
defer r.Close(testCtx) |
| 2265 |
|
| 2266 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 2267 |
|
| 2268 |
preopen := getPreopen(t, fsc) |
| 2269 |
fd, errno := fsc.OpenFile(preopen, ".", experimentalsys.O_RDONLY, 0) |
| 2270 |
require.EqualErrno(t, 0, errno) |
| 2271 |
|
| 2272 |
mem := mod.Memory() |
| 2273 |
const resultBufused, buf = 0, 8 |
| 2274 |
fdReaddir := func(cookie uint64) uint32 { |
| 2275 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdReaddirName, |
| 2276 |
uint64(fd), buf, 256, cookie, uint64(resultBufused)) |
| 2277 |
bufused, ok := mem.ReadUint32Le(resultBufused) |
| 2278 |
require.True(t, ok) |
| 2279 |
return bufused |
| 2280 |
} |
| 2281 |
|
| 2282 |
|
| 2283 |
bufused := fdReaddir(0) |
| 2284 |
dotDirentsLen := (wasip1.DirentSize + 1) + (wasip1.DirentSize + 2) |
| 2285 |
require.Equal(t, dotDirentsLen, bufused) |
| 2286 |
|
| 2287 |
|
| 2288 |
fileName := "file" |
| 2289 |
require.NoError(t, os.WriteFile(path.Join(tmpDir, fileName), nil, 0o0666)) |
| 2290 |
fileDirentLen := wasip1.DirentSize + uint32(len(fileName)) |
| 2291 |
|
| 2292 |
|
| 2293 |
bufused = fdReaddir(0) |
| 2294 |
require.Equal(t, dotDirentsLen+fileDirentLen, bufused) |
| 2295 |
|
| 2296 |
|
| 2297 |
bufused = fdReaddir(2) |
| 2298 |
require.Equal(t, fileDirentLen, bufused) |
| 2299 |
|
| 2300 |
require.Equal(t, ` |
| 2301 |
==> wasi_snapshot_preview1.fd_readdir(fd=4,buf=8,buf_len=256,cookie=0) |
| 2302 |
<== (bufused=51,errno=ESUCCESS) |
| 2303 |
==> wasi_snapshot_preview1.fd_readdir(fd=4,buf=8,buf_len=256,cookie=0) |
| 2304 |
<== (bufused=79,errno=ESUCCESS) |
| 2305 |
==> wasi_snapshot_preview1.fd_readdir(fd=4,buf=8,buf_len=256,cookie=2) |
| 2306 |
<== (bufused=28,errno=ESUCCESS) |
| 2307 |
`, "\n"+log.String()) |
| 2308 |
} |
| 2309 |
|
| 2310 |
func Test_fdReaddir_Errors(t *testing.T) { |
| 2311 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(fstest.FS)) |
| 2312 |
defer r.Close(testCtx) |
| 2313 |
memLen := mod.Memory().Size() |
| 2314 |
|
| 2315 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 2316 |
preopen := getPreopen(t, fsc) |
| 2317 |
|
| 2318 |
fileFD, errno := fsc.OpenFile(preopen, "animals.txt", experimentalsys.O_RDONLY, 0) |
| 2319 |
require.EqualErrno(t, 0, errno) |
| 2320 |
|
| 2321 |
|
| 2322 |
dirFD := fileFD + 1 |
| 2323 |
|
| 2324 |
tests := []struct { |
| 2325 |
name string |
| 2326 |
fd int32 |
| 2327 |
buf, bufLen, resultBufused uint32 |
| 2328 |
cookie int64 |
| 2329 |
expectedErrno wasip1.Errno |
| 2330 |
expectedLog string |
| 2331 |
}{ |
| 2332 |
{ |
| 2333 |
name: "out-of-memory reading buf", |
| 2334 |
fd: dirFD, |
| 2335 |
buf: memLen, |
| 2336 |
bufLen: 1000, |
| 2337 |
expectedErrno: wasip1.ErrnoFault, |
| 2338 |
expectedLog: ` |
| 2339 |
==> wasi_snapshot_preview1.fd_readdir(fd=5,buf=65536,buf_len=1000,cookie=0) |
| 2340 |
<== (bufused=,errno=EFAULT) |
| 2341 |
`, |
| 2342 |
}, |
| 2343 |
{ |
| 2344 |
name: "invalid FD", |
| 2345 |
fd: 42, |
| 2346 |
buf: 0, bufLen: wasip1.DirentSize, |
| 2347 |
resultBufused: 1000, |
| 2348 |
expectedErrno: wasip1.ErrnoBadf, |
| 2349 |
expectedLog: ` |
| 2350 |
==> wasi_snapshot_preview1.fd_readdir(fd=42,buf=0,buf_len=24,cookie=0) |
| 2351 |
<== (bufused=,errno=EBADF) |
| 2352 |
`, |
| 2353 |
}, |
| 2354 |
{ |
| 2355 |
name: "not a dir", |
| 2356 |
fd: fileFD, |
| 2357 |
buf: 0, bufLen: wasip1.DirentSize, |
| 2358 |
resultBufused: 1000, |
| 2359 |
expectedErrno: wasip1.ErrnoBadf, |
| 2360 |
expectedLog: ` |
| 2361 |
==> wasi_snapshot_preview1.fd_readdir(fd=4,buf=0,buf_len=24,cookie=0) |
| 2362 |
<== (bufused=,errno=EBADF) |
| 2363 |
`, |
| 2364 |
}, |
| 2365 |
{ |
| 2366 |
name: "out-of-memory reading bufLen", |
| 2367 |
fd: dirFD, |
| 2368 |
buf: memLen - 1, |
| 2369 |
bufLen: 1000, |
| 2370 |
expectedErrno: wasip1.ErrnoFault, |
| 2371 |
expectedLog: ` |
| 2372 |
==> wasi_snapshot_preview1.fd_readdir(fd=5,buf=65535,buf_len=1000,cookie=0) |
| 2373 |
<== (bufused=,errno=EFAULT) |
| 2374 |
`, |
| 2375 |
}, |
| 2376 |
{ |
| 2377 |
name: "bufLen must be enough to write a struct", |
| 2378 |
fd: dirFD, |
| 2379 |
buf: 0, bufLen: 1, |
| 2380 |
resultBufused: 1000, |
| 2381 |
expectedErrno: wasip1.ErrnoInval, |
| 2382 |
expectedLog: ` |
| 2383 |
==> wasi_snapshot_preview1.fd_readdir(fd=5,buf=0,buf_len=1,cookie=0) |
| 2384 |
<== (bufused=,errno=EINVAL) |
| 2385 |
`, |
| 2386 |
}, |
| 2387 |
{ |
| 2388 |
name: "cookie invalid when no prior state", |
| 2389 |
fd: dirFD, |
| 2390 |
buf: 0, bufLen: 1000, |
| 2391 |
cookie: 1, |
| 2392 |
resultBufused: 2000, |
| 2393 |
expectedErrno: wasip1.ErrnoNoent, |
| 2394 |
expectedLog: ` |
| 2395 |
==> wasi_snapshot_preview1.fd_readdir(fd=5,buf=0,buf_len=1000,cookie=1) |
| 2396 |
<== (bufused=,errno=ENOENT) |
| 2397 |
`, |
| 2398 |
}, |
| 2399 |
{ |
| 2400 |
|
| 2401 |
|
| 2402 |
name: "negative cookie invalid", |
| 2403 |
fd: dirFD, |
| 2404 |
buf: 0, bufLen: 1000, |
| 2405 |
cookie: -1, |
| 2406 |
resultBufused: 2000, |
| 2407 |
expectedErrno: wasip1.ErrnoNoent, |
| 2408 |
expectedLog: ` |
| 2409 |
==> wasi_snapshot_preview1.fd_readdir(fd=5,buf=0,buf_len=1000,cookie=-1) |
| 2410 |
<== (bufused=,errno=ENOENT) |
| 2411 |
`, |
| 2412 |
}, |
| 2413 |
} |
| 2414 |
|
| 2415 |
for _, tt := range tests { |
| 2416 |
tc := tt |
| 2417 |
t.Run(tc.name, func(t *testing.T) { |
| 2418 |
defer log.Reset() |
| 2419 |
|
| 2420 |
|
| 2421 |
if tc.fd == dirFD { |
| 2422 |
dirFD, errno = fsc.OpenFile(preopen, "dir", experimentalsys.O_RDONLY, 0) |
| 2423 |
require.EqualErrno(t, 0, errno) |
| 2424 |
defer fsc.CloseFile(dirFD) |
| 2425 |
} |
| 2426 |
|
| 2427 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdReaddirName, |
| 2428 |
uint64(tc.fd), uint64(tc.buf), uint64(tc.bufLen), uint64(tc.cookie), uint64(tc.resultBufused)) |
| 2429 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 2430 |
}) |
| 2431 |
} |
| 2432 |
} |
| 2433 |
|
| 2434 |
func Test_fdRenumber(t *testing.T) { |
| 2435 |
const fileFD, dirFD = 4, 5 |
| 2436 |
|
| 2437 |
tests := []struct { |
| 2438 |
name string |
| 2439 |
from, to int32 |
| 2440 |
expectedErrno wasip1.Errno |
| 2441 |
expectedLog string |
| 2442 |
}{ |
| 2443 |
{ |
| 2444 |
name: "from=preopen", |
| 2445 |
from: sys.FdPreopen, |
| 2446 |
to: dirFD, |
| 2447 |
expectedErrno: wasip1.ErrnoNotsup, |
| 2448 |
expectedLog: ` |
| 2449 |
==> wasi_snapshot_preview1.fd_renumber(fd=3,to=5) |
| 2450 |
<== errno=ENOTSUP |
| 2451 |
`, |
| 2452 |
}, |
| 2453 |
{ |
| 2454 |
name: "from=badf", |
| 2455 |
from: -1, |
| 2456 |
to: sys.FdPreopen, |
| 2457 |
expectedErrno: wasip1.ErrnoBadf, |
| 2458 |
expectedLog: ` |
| 2459 |
==> wasi_snapshot_preview1.fd_renumber(fd=-1,to=3) |
| 2460 |
<== errno=EBADF |
| 2461 |
`, |
| 2462 |
}, |
| 2463 |
{ |
| 2464 |
name: "to=badf", |
| 2465 |
from: sys.FdPreopen, |
| 2466 |
to: -1, |
| 2467 |
expectedErrno: wasip1.ErrnoBadf, |
| 2468 |
expectedLog: ` |
| 2469 |
==> wasi_snapshot_preview1.fd_renumber(fd=3,to=-1) |
| 2470 |
<== errno=EBADF |
| 2471 |
`, |
| 2472 |
}, |
| 2473 |
{ |
| 2474 |
name: "to=preopen", |
| 2475 |
from: dirFD, |
| 2476 |
to: sys.FdPreopen, |
| 2477 |
expectedErrno: wasip1.ErrnoNotsup, |
| 2478 |
expectedLog: ` |
| 2479 |
==> wasi_snapshot_preview1.fd_renumber(fd=5,to=3) |
| 2480 |
<== errno=ENOTSUP |
| 2481 |
`, |
| 2482 |
}, |
| 2483 |
{ |
| 2484 |
name: "file to dir", |
| 2485 |
from: fileFD, |
| 2486 |
to: dirFD, |
| 2487 |
expectedErrno: wasip1.ErrnoSuccess, |
| 2488 |
expectedLog: ` |
| 2489 |
==> wasi_snapshot_preview1.fd_renumber(fd=4,to=5) |
| 2490 |
<== errno=ESUCCESS |
| 2491 |
`, |
| 2492 |
}, |
| 2493 |
{ |
| 2494 |
name: "dir to file", |
| 2495 |
from: dirFD, |
| 2496 |
to: fileFD, |
| 2497 |
expectedErrno: wasip1.ErrnoSuccess, |
| 2498 |
expectedLog: ` |
| 2499 |
==> wasi_snapshot_preview1.fd_renumber(fd=5,to=4) |
| 2500 |
<== errno=ESUCCESS |
| 2501 |
`, |
| 2502 |
}, |
| 2503 |
{ |
| 2504 |
name: "dir to any", |
| 2505 |
from: dirFD, |
| 2506 |
to: 12345, |
| 2507 |
expectedErrno: wasip1.ErrnoSuccess, |
| 2508 |
expectedLog: ` |
| 2509 |
==> wasi_snapshot_preview1.fd_renumber(fd=5,to=12345) |
| 2510 |
<== errno=ESUCCESS |
| 2511 |
`, |
| 2512 |
}, |
| 2513 |
{ |
| 2514 |
name: "file to any", |
| 2515 |
from: fileFD, |
| 2516 |
to: 54, |
| 2517 |
expectedErrno: wasip1.ErrnoSuccess, |
| 2518 |
expectedLog: ` |
| 2519 |
==> wasi_snapshot_preview1.fd_renumber(fd=4,to=54) |
| 2520 |
<== errno=ESUCCESS |
| 2521 |
`, |
| 2522 |
}, |
| 2523 |
} |
| 2524 |
|
| 2525 |
for _, tt := range tests { |
| 2526 |
tc := tt |
| 2527 |
t.Run(tc.name, func(t *testing.T) { |
| 2528 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(fstest.FS)) |
| 2529 |
defer r.Close(testCtx) |
| 2530 |
|
| 2531 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 2532 |
preopen := getPreopen(t, fsc) |
| 2533 |
|
| 2534 |
|
| 2535 |
fileFDAssigned, errno := fsc.OpenFile(preopen, "animals.txt", experimentalsys.O_RDONLY, 0) |
| 2536 |
require.EqualErrno(t, 0, errno) |
| 2537 |
require.Equal(t, int32(fileFD), fileFDAssigned) |
| 2538 |
|
| 2539 |
dirFDAssigned, errno := fsc.OpenFile(preopen, "dir", experimentalsys.O_RDONLY, 0) |
| 2540 |
require.EqualErrno(t, 0, errno) |
| 2541 |
require.Equal(t, int32(dirFD), dirFDAssigned) |
| 2542 |
|
| 2543 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdRenumberName, uint64(tc.from), uint64(tc.to)) |
| 2544 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 2545 |
}) |
| 2546 |
} |
| 2547 |
} |
| 2548 |
|
| 2549 |
func Test_fdSeek(t *testing.T) { |
| 2550 |
mod, fd, log, r := requireOpenFile(t, t.TempDir(), "test_path", []byte("wazero"), true) |
| 2551 |
defer r.Close(testCtx) |
| 2552 |
|
| 2553 |
resultNewoffset := uint32(1) |
| 2554 |
|
| 2555 |
tests := []struct { |
| 2556 |
name string |
| 2557 |
offset int64 |
| 2558 |
whence int |
| 2559 |
expectedOffset int64 |
| 2560 |
expectedMemory []byte |
| 2561 |
expectedLog string |
| 2562 |
}{ |
| 2563 |
{ |
| 2564 |
name: "SeekStart", |
| 2565 |
offset: 4, |
| 2566 |
whence: io.SeekStart, |
| 2567 |
expectedOffset: 4, |
| 2568 |
expectedMemory: []byte{ |
| 2569 |
'?', |
| 2570 |
4, 0, 0, 0, 0, 0, 0, 0, |
| 2571 |
'?', |
| 2572 |
}, |
| 2573 |
expectedLog: ` |
| 2574 |
==> wasi_snapshot_preview1.fd_seek(fd=4,offset=4,whence=0) |
| 2575 |
<== (newoffset=4,errno=ESUCCESS) |
| 2576 |
`, |
| 2577 |
}, |
| 2578 |
{ |
| 2579 |
name: "SeekCurrent", |
| 2580 |
offset: 1, |
| 2581 |
whence: io.SeekCurrent, |
| 2582 |
expectedOffset: 2, |
| 2583 |
expectedMemory: []byte{ |
| 2584 |
'?', |
| 2585 |
2, 0, 0, 0, 0, 0, 0, 0, |
| 2586 |
'?', |
| 2587 |
}, |
| 2588 |
expectedLog: ` |
| 2589 |
==> wasi_snapshot_preview1.fd_seek(fd=4,offset=1,whence=1) |
| 2590 |
<== (newoffset=2,errno=ESUCCESS) |
| 2591 |
`, |
| 2592 |
}, |
| 2593 |
{ |
| 2594 |
name: "SeekEnd", |
| 2595 |
offset: -1, |
| 2596 |
whence: io.SeekEnd, |
| 2597 |
expectedOffset: 5, |
| 2598 |
expectedMemory: []byte{ |
| 2599 |
'?', |
| 2600 |
5, 0, 0, 0, 0, 0, 0, 0, |
| 2601 |
'?', |
| 2602 |
}, |
| 2603 |
expectedLog: ` |
| 2604 |
==> wasi_snapshot_preview1.fd_seek(fd=4,offset=-1,whence=2) |
| 2605 |
<== (newoffset=5,errno=ESUCCESS) |
| 2606 |
`, |
| 2607 |
}, |
| 2608 |
} |
| 2609 |
|
| 2610 |
for _, tt := range tests { |
| 2611 |
tc := tt |
| 2612 |
t.Run(tc.name, func(t *testing.T) { |
| 2613 |
defer log.Reset() |
| 2614 |
|
| 2615 |
maskMemory(t, mod, len(tc.expectedMemory)) |
| 2616 |
|
| 2617 |
|
| 2618 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 2619 |
f, ok := fsc.LookupFile(fd) |
| 2620 |
require.True(t, ok) |
| 2621 |
|
| 2622 |
|
| 2623 |
offset, errno := f.File.Seek(1, io.SeekStart) |
| 2624 |
require.EqualErrno(t, 0, errno) |
| 2625 |
require.Equal(t, int64(1), offset) |
| 2626 |
|
| 2627 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdSeekName, uint64(fd), uint64(tc.offset), uint64(tc.whence), uint64(resultNewoffset)) |
| 2628 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 2629 |
|
| 2630 |
actual, ok := mod.Memory().Read(0, uint32(len(tc.expectedMemory))) |
| 2631 |
require.True(t, ok) |
| 2632 |
require.Equal(t, tc.expectedMemory, actual) |
| 2633 |
|
| 2634 |
offset, errno = f.File.Seek(0, io.SeekCurrent) |
| 2635 |
require.EqualErrno(t, 0, errno) |
| 2636 |
require.Equal(t, tc.expectedOffset, offset) |
| 2637 |
}) |
| 2638 |
} |
| 2639 |
} |
| 2640 |
|
| 2641 |
func Test_fdSeek_Errors(t *testing.T) { |
| 2642 |
mod, fileFD, log, r := requireOpenFile(t, t.TempDir(), "test_path", []byte("wazero"), false) |
| 2643 |
defer r.Close(testCtx) |
| 2644 |
|
| 2645 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 2646 |
preopen := getPreopen(t, fsc) |
| 2647 |
require.Zero(t, preopen.Mkdir("dir", 0o0777)) |
| 2648 |
dirFD := requireOpenFD(t, mod, "dir") |
| 2649 |
|
| 2650 |
memorySize := mod.Memory().Size() |
| 2651 |
|
| 2652 |
tests := []struct { |
| 2653 |
name string |
| 2654 |
fd int32 |
| 2655 |
offset uint64 |
| 2656 |
whence, resultNewoffset uint32 |
| 2657 |
expectedErrno wasip1.Errno |
| 2658 |
expectedLog string |
| 2659 |
}{ |
| 2660 |
{ |
| 2661 |
name: "invalid FD", |
| 2662 |
fd: 42, |
| 2663 |
expectedErrno: wasip1.ErrnoBadf, |
| 2664 |
expectedLog: ` |
| 2665 |
==> wasi_snapshot_preview1.fd_seek(fd=42,offset=0,whence=0) |
| 2666 |
<== (newoffset=,errno=EBADF) |
| 2667 |
`, |
| 2668 |
}, |
| 2669 |
{ |
| 2670 |
name: "invalid whence", |
| 2671 |
fd: fileFD, |
| 2672 |
whence: 3, |
| 2673 |
expectedErrno: wasip1.ErrnoInval, |
| 2674 |
expectedLog: ` |
| 2675 |
==> wasi_snapshot_preview1.fd_seek(fd=4,offset=0,whence=3) |
| 2676 |
<== (newoffset=,errno=EINVAL) |
| 2677 |
`, |
| 2678 |
}, |
| 2679 |
{ |
| 2680 |
name: "dir not file", |
| 2681 |
fd: dirFD, |
| 2682 |
expectedErrno: wasip1.ErrnoIsdir, |
| 2683 |
expectedLog: ` |
| 2684 |
==> wasi_snapshot_preview1.fd_seek(fd=5,offset=0,whence=0) |
| 2685 |
<== (newoffset=,errno=EISDIR) |
| 2686 |
`, |
| 2687 |
}, |
| 2688 |
{ |
| 2689 |
name: "out-of-memory writing resultNewoffset", |
| 2690 |
fd: fileFD, |
| 2691 |
resultNewoffset: memorySize, |
| 2692 |
expectedErrno: wasip1.ErrnoFault, |
| 2693 |
expectedLog: ` |
| 2694 |
==> wasi_snapshot_preview1.fd_seek(fd=4,offset=0,whence=0) |
| 2695 |
<== (newoffset=,errno=EFAULT) |
| 2696 |
`, |
| 2697 |
}, |
| 2698 |
} |
| 2699 |
|
| 2700 |
for _, tt := range tests { |
| 2701 |
tc := tt |
| 2702 |
t.Run(tc.name, func(t *testing.T) { |
| 2703 |
defer log.Reset() |
| 2704 |
|
| 2705 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdSeekName, uint64(tc.fd), tc.offset, uint64(tc.whence), uint64(tc.resultNewoffset)) |
| 2706 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 2707 |
}) |
| 2708 |
} |
| 2709 |
} |
| 2710 |
|
| 2711 |
|
| 2712 |
func Test_fdSync(t *testing.T) { |
| 2713 |
tmpDir := t.TempDir() |
| 2714 |
pathName := "test_path" |
| 2715 |
mod, fd, log, r := requireOpenFile(t, tmpDir, pathName, []byte{}, false) |
| 2716 |
defer r.Close(testCtx) |
| 2717 |
|
| 2718 |
tests := []struct { |
| 2719 |
name string |
| 2720 |
fd int32 |
| 2721 |
expectedErrno wasip1.Errno |
| 2722 |
expectedLog string |
| 2723 |
}{ |
| 2724 |
{ |
| 2725 |
name: "invalid FD", |
| 2726 |
fd: 42, |
| 2727 |
expectedErrno: wasip1.ErrnoBadf, |
| 2728 |
expectedLog: ` |
| 2729 |
==> wasi_snapshot_preview1.fd_sync(fd=42) |
| 2730 |
<== errno=EBADF |
| 2731 |
`, |
| 2732 |
}, |
| 2733 |
{ |
| 2734 |
name: "valid FD", |
| 2735 |
fd: fd, |
| 2736 |
expectedErrno: wasip1.ErrnoSuccess, |
| 2737 |
expectedLog: ` |
| 2738 |
==> wasi_snapshot_preview1.fd_sync(fd=4) |
| 2739 |
<== errno=ESUCCESS |
| 2740 |
`, |
| 2741 |
}, |
| 2742 |
} |
| 2743 |
|
| 2744 |
for _, tt := range tests { |
| 2745 |
tc := tt |
| 2746 |
t.Run(tc.name, func(t *testing.T) { |
| 2747 |
defer log.Reset() |
| 2748 |
|
| 2749 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdSyncName, uint64(tc.fd)) |
| 2750 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 2751 |
}) |
| 2752 |
} |
| 2753 |
} |
| 2754 |
|
| 2755 |
func Test_fdTell(t *testing.T) { |
| 2756 |
mod, fd, log, r := requireOpenFile(t, t.TempDir(), "test_path", []byte("wazero"), true) |
| 2757 |
defer r.Close(testCtx) |
| 2758 |
defer log.Reset() |
| 2759 |
|
| 2760 |
resultNewoffset := uint32(1) |
| 2761 |
|
| 2762 |
expectedOffset := int64(1) |
| 2763 |
expectedMemory := []byte{ |
| 2764 |
'?', |
| 2765 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 2766 |
'?', |
| 2767 |
} |
| 2768 |
expectedLog := ` |
| 2769 |
==> wasi_snapshot_preview1.fd_tell(fd=4,result.offset=1) |
| 2770 |
<== errno=ESUCCESS |
| 2771 |
` |
| 2772 |
|
| 2773 |
maskMemory(t, mod, len(expectedMemory)) |
| 2774 |
|
| 2775 |
|
| 2776 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 2777 |
f, ok := fsc.LookupFile(fd) |
| 2778 |
require.True(t, ok) |
| 2779 |
|
| 2780 |
|
| 2781 |
offset, errno := f.File.Seek(1, io.SeekStart) |
| 2782 |
require.EqualErrno(t, 0, errno) |
| 2783 |
require.Equal(t, int64(1), offset) |
| 2784 |
|
| 2785 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdTellName, uint64(fd), uint64(resultNewoffset)) |
| 2786 |
require.Equal(t, expectedLog, "\n"+log.String()) |
| 2787 |
|
| 2788 |
actual, ok := mod.Memory().Read(0, uint32(len(expectedMemory))) |
| 2789 |
require.True(t, ok) |
| 2790 |
require.Equal(t, expectedMemory, actual) |
| 2791 |
|
| 2792 |
offset, errno = f.File.Seek(0, io.SeekCurrent) |
| 2793 |
require.EqualErrno(t, 0, errno) |
| 2794 |
require.Equal(t, expectedOffset, offset) |
| 2795 |
} |
| 2796 |
|
| 2797 |
func Test_fdTell_Errors(t *testing.T) { |
| 2798 |
mod, fd, log, r := requireOpenFile(t, t.TempDir(), "test_path", []byte("wazero"), true) |
| 2799 |
defer r.Close(testCtx) |
| 2800 |
|
| 2801 |
memorySize := mod.Memory().Size() |
| 2802 |
|
| 2803 |
tests := []struct { |
| 2804 |
name string |
| 2805 |
fd int32 |
| 2806 |
resultNewoffset uint32 |
| 2807 |
expectedErrno wasip1.Errno |
| 2808 |
expectedLog string |
| 2809 |
}{ |
| 2810 |
{ |
| 2811 |
name: "invalid FD", |
| 2812 |
fd: 42, |
| 2813 |
expectedErrno: wasip1.ErrnoBadf, |
| 2814 |
expectedLog: ` |
| 2815 |
==> wasi_snapshot_preview1.fd_tell(fd=42,result.offset=0) |
| 2816 |
<== errno=EBADF |
| 2817 |
`, |
| 2818 |
}, |
| 2819 |
{ |
| 2820 |
name: "out-of-memory writing resultNewoffset", |
| 2821 |
fd: fd, |
| 2822 |
resultNewoffset: memorySize, |
| 2823 |
expectedErrno: wasip1.ErrnoFault, |
| 2824 |
expectedLog: ` |
| 2825 |
==> wasi_snapshot_preview1.fd_tell(fd=4,result.offset=65536) |
| 2826 |
<== errno=EFAULT |
| 2827 |
`, |
| 2828 |
}, |
| 2829 |
} |
| 2830 |
|
| 2831 |
for _, tt := range tests { |
| 2832 |
tc := tt |
| 2833 |
t.Run(tc.name, func(t *testing.T) { |
| 2834 |
defer log.Reset() |
| 2835 |
|
| 2836 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdTellName, uint64(tc.fd), uint64(tc.resultNewoffset)) |
| 2837 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 2838 |
}) |
| 2839 |
} |
| 2840 |
} |
| 2841 |
|
| 2842 |
func Test_fdWrite(t *testing.T) { |
| 2843 |
tmpDir := t.TempDir() |
| 2844 |
pathName := "test_path" |
| 2845 |
mod, fd, log, r := requireOpenFile(t, tmpDir, pathName, []byte{}, false) |
| 2846 |
defer r.Close(testCtx) |
| 2847 |
|
| 2848 |
iovs := uint32(1) |
| 2849 |
initialMemory := []byte{ |
| 2850 |
'?', |
| 2851 |
18, 0, 0, 0, |
| 2852 |
4, 0, 0, 0, |
| 2853 |
23, 0, 0, 0, |
| 2854 |
2, 0, 0, 0, |
| 2855 |
'?', |
| 2856 |
'w', 'a', 'z', 'e', |
| 2857 |
'?', |
| 2858 |
'r', 'o', |
| 2859 |
'?', |
| 2860 |
} |
| 2861 |
iovsCount := uint32(2) |
| 2862 |
resultNwritten := uint32(26) |
| 2863 |
expectedMemory := append( |
| 2864 |
initialMemory, |
| 2865 |
6, 0, 0, 0, |
| 2866 |
'?', |
| 2867 |
) |
| 2868 |
|
| 2869 |
maskMemory(t, mod, len(expectedMemory)) |
| 2870 |
ok := mod.Memory().Write(0, initialMemory) |
| 2871 |
require.True(t, ok) |
| 2872 |
|
| 2873 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdWriteName, uint64(fd), uint64(iovs), uint64(iovsCount), uint64(resultNwritten)) |
| 2874 |
require.Equal(t, ` |
| 2875 |
==> wasi_snapshot_preview1.fd_write(fd=4,iovs=1,iovs_len=2) |
| 2876 |
<== (nwritten=6,errno=ESUCCESS) |
| 2877 |
`, "\n"+log.String()) |
| 2878 |
|
| 2879 |
actual, ok := mod.Memory().Read(0, uint32(len(expectedMemory))) |
| 2880 |
require.True(t, ok) |
| 2881 |
require.Equal(t, expectedMemory, actual) |
| 2882 |
|
| 2883 |
|
| 2884 |
buf, err := os.ReadFile(joinPath(tmpDir, pathName)) |
| 2885 |
require.NoError(t, err) |
| 2886 |
|
| 2887 |
require.Equal(t, []byte("wazero"), buf) |
| 2888 |
} |
| 2889 |
|
| 2890 |
func Test_fdWrite_Errors(t *testing.T) { |
| 2891 |
tmpDir := t.TempDir() |
| 2892 |
pathName := "test_path" |
| 2893 |
mod, fd, log, r := requireOpenFile(t, tmpDir, pathName, []byte{1, 2, 3, 4}, false) |
| 2894 |
defer r.Close(testCtx) |
| 2895 |
|
| 2896 |
|
| 2897 |
iovsCount := uint32(1) |
| 2898 |
memSize := mod.Memory().Size() |
| 2899 |
|
| 2900 |
tests := []struct { |
| 2901 |
name string |
| 2902 |
fd int32 |
| 2903 |
iovs, resultNwritten uint32 |
| 2904 |
expectedErrno wasip1.Errno |
| 2905 |
expectedLog string |
| 2906 |
}{ |
| 2907 |
{ |
| 2908 |
name: "invalid FD", |
| 2909 |
fd: 42, |
| 2910 |
expectedErrno: wasip1.ErrnoBadf, |
| 2911 |
expectedLog: ` |
| 2912 |
==> wasi_snapshot_preview1.fd_write(fd=42,iovs=0,iovs_len=1) |
| 2913 |
<== (nwritten=,errno=EBADF) |
| 2914 |
`, |
| 2915 |
}, |
| 2916 |
{ |
| 2917 |
name: "not writable FD", |
| 2918 |
fd: sys.FdStdin, |
| 2919 |
expectedErrno: wasip1.ErrnoBadf, |
| 2920 |
expectedLog: "\n", |
| 2921 |
}, |
| 2922 |
{ |
| 2923 |
name: "out-of-memory reading iovs[0].offset", |
| 2924 |
fd: fd, |
| 2925 |
iovs: memSize - 2, |
| 2926 |
expectedErrno: wasip1.ErrnoFault, |
| 2927 |
expectedLog: ` |
| 2928 |
==> wasi_snapshot_preview1.fd_write(fd=4,iovs=65534,iovs_len=1) |
| 2929 |
<== (nwritten=,errno=EFAULT) |
| 2930 |
`, |
| 2931 |
}, |
| 2932 |
{ |
| 2933 |
name: "out-of-memory reading iovs[0].length", |
| 2934 |
fd: fd, |
| 2935 |
iovs: memSize - 4, |
| 2936 |
expectedErrno: wasip1.ErrnoFault, |
| 2937 |
expectedLog: ` |
| 2938 |
==> wasi_snapshot_preview1.fd_write(fd=4,iovs=65532,iovs_len=1) |
| 2939 |
<== (nwritten=,errno=EFAULT) |
| 2940 |
`, |
| 2941 |
}, |
| 2942 |
{ |
| 2943 |
name: "iovs[0].offset is outside memory", |
| 2944 |
fd: fd, |
| 2945 |
iovs: memSize - 5, |
| 2946 |
expectedErrno: wasip1.ErrnoFault, |
| 2947 |
expectedLog: ` |
| 2948 |
==> wasi_snapshot_preview1.fd_write(fd=4,iovs=65531,iovs_len=1) |
| 2949 |
<== (nwritten=,errno=EFAULT) |
| 2950 |
`, |
| 2951 |
}, |
| 2952 |
{ |
| 2953 |
name: "length to read exceeds memory by 1", |
| 2954 |
fd: fd, |
| 2955 |
iovs: memSize - 7, |
| 2956 |
expectedErrno: wasip1.ErrnoFault, |
| 2957 |
expectedLog: ` |
| 2958 |
==> wasi_snapshot_preview1.fd_write(fd=4,iovs=65529,iovs_len=1) |
| 2959 |
<== (nwritten=,errno=EFAULT) |
| 2960 |
`, |
| 2961 |
}, |
| 2962 |
{ |
| 2963 |
name: "resultNwritten offset is outside memory", |
| 2964 |
fd: fd, |
| 2965 |
resultNwritten: memSize, |
| 2966 |
expectedErrno: wasip1.ErrnoFault, |
| 2967 |
expectedLog: ` |
| 2968 |
==> wasi_snapshot_preview1.fd_write(fd=4,iovs=0,iovs_len=1) |
| 2969 |
<== (nwritten=,errno=EFAULT) |
| 2970 |
`, |
| 2971 |
}, |
| 2972 |
} |
| 2973 |
|
| 2974 |
for _, tt := range tests { |
| 2975 |
tc := tt |
| 2976 |
t.Run(tc.name, func(t *testing.T) { |
| 2977 |
defer log.Reset() |
| 2978 |
|
| 2979 |
mod.Memory().Write(tc.iovs, append( |
| 2980 |
u64.LeBytes(uint64(tc.iovs+8)), |
| 2981 |
|
| 2982 |
2, 0, 0, 0, |
| 2983 |
'h', 'i', |
| 2984 |
)) |
| 2985 |
|
| 2986 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.FdWriteName, uint64(tc.fd), uint64(tc.iovs), uint64(iovsCount), |
| 2987 |
uint64(tc.resultNwritten)) |
| 2988 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 2989 |
}) |
| 2990 |
} |
| 2991 |
} |
| 2992 |
|
| 2993 |
func Test_pathCreateDirectory(t *testing.T) { |
| 2994 |
tmpDir := t.TempDir() |
| 2995 |
fsConfig := wazero.NewFSConfig().WithDirMount(tmpDir, "/") |
| 2996 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFSConfig(fsConfig)) |
| 2997 |
defer r.Close(testCtx) |
| 2998 |
|
| 2999 |
|
| 3000 |
pathName := "wazero" |
| 3001 |
realPath := joinPath(tmpDir, pathName) |
| 3002 |
ok := mod.Memory().Write(0, append([]byte{'?'}, pathName...)) |
| 3003 |
require.True(t, ok) |
| 3004 |
|
| 3005 |
fd := sys.FdPreopen |
| 3006 |
name := 1 |
| 3007 |
nameLen := len(pathName) |
| 3008 |
|
| 3009 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.PathCreateDirectoryName, uint64(fd), uint64(name), uint64(nameLen)) |
| 3010 |
require.Equal(t, ` |
| 3011 |
==> wasi_snapshot_preview1.path_create_directory(fd=3,path=wazero) |
| 3012 |
<== errno=ESUCCESS |
| 3013 |
`, "\n"+log.String()) |
| 3014 |
|
| 3015 |
|
| 3016 |
stat, err := os.Stat(realPath) |
| 3017 |
require.NoError(t, err) |
| 3018 |
require.True(t, stat.IsDir()) |
| 3019 |
require.Equal(t, pathName, stat.Name()) |
| 3020 |
} |
| 3021 |
|
| 3022 |
func Test_pathCreateDirectory_Errors(t *testing.T) { |
| 3023 |
tmpDir := t.TempDir() |
| 3024 |
fsConfig := wazero.NewFSConfig().WithDirMount(tmpDir, "/") |
| 3025 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFSConfig(fsConfig)) |
| 3026 |
defer r.Close(testCtx) |
| 3027 |
|
| 3028 |
file := "file" |
| 3029 |
err := os.WriteFile(joinPath(tmpDir, file), []byte{}, 0o700) |
| 3030 |
require.NoError(t, err) |
| 3031 |
fileFD := requireOpenFD(t, mod, file) |
| 3032 |
|
| 3033 |
dir := "dir" |
| 3034 |
err = os.Mkdir(joinPath(tmpDir, dir), 0o700) |
| 3035 |
require.NoError(t, err) |
| 3036 |
|
| 3037 |
tests := []struct { |
| 3038 |
name, pathName string |
| 3039 |
fd int32 |
| 3040 |
path, pathLen uint32 |
| 3041 |
expectedErrno wasip1.Errno |
| 3042 |
expectedLog string |
| 3043 |
}{ |
| 3044 |
{ |
| 3045 |
name: "unopened FD", |
| 3046 |
fd: 42, |
| 3047 |
expectedErrno: wasip1.ErrnoBadf, |
| 3048 |
expectedLog: ` |
| 3049 |
==> wasi_snapshot_preview1.path_create_directory(fd=42,path=) |
| 3050 |
<== errno=EBADF |
| 3051 |
`, |
| 3052 |
}, |
| 3053 |
{ |
| 3054 |
name: "Fd not a directory", |
| 3055 |
fd: fileFD, |
| 3056 |
pathName: file, |
| 3057 |
path: 0, |
| 3058 |
pathLen: uint32(len(file)), |
| 3059 |
expectedErrno: wasip1.ErrnoNotdir, |
| 3060 |
expectedLog: ` |
| 3061 |
==> wasi_snapshot_preview1.path_create_directory(fd=4,path=file) |
| 3062 |
<== errno=ENOTDIR |
| 3063 |
`, |
| 3064 |
}, |
| 3065 |
{ |
| 3066 |
name: "out-of-memory reading path", |
| 3067 |
fd: sys.FdPreopen, |
| 3068 |
path: mod.Memory().Size(), |
| 3069 |
pathLen: 1, |
| 3070 |
expectedErrno: wasip1.ErrnoFault, |
| 3071 |
expectedLog: ` |
| 3072 |
==> wasi_snapshot_preview1.path_create_directory(fd=3,path=OOM(65536,1)) |
| 3073 |
<== errno=EFAULT |
| 3074 |
`, |
| 3075 |
}, |
| 3076 |
{ |
| 3077 |
name: "out-of-memory reading pathLen", |
| 3078 |
fd: sys.FdPreopen, |
| 3079 |
path: 0, |
| 3080 |
pathLen: mod.Memory().Size() + 1, |
| 3081 |
expectedErrno: wasip1.ErrnoFault, |
| 3082 |
expectedLog: ` |
| 3083 |
==> wasi_snapshot_preview1.path_create_directory(fd=3,path=OOM(0,65537)) |
| 3084 |
<== errno=EFAULT |
| 3085 |
`, |
| 3086 |
}, |
| 3087 |
{ |
| 3088 |
name: "file exists", |
| 3089 |
fd: sys.FdPreopen, |
| 3090 |
pathName: file, |
| 3091 |
path: 0, |
| 3092 |
pathLen: uint32(len(file)), |
| 3093 |
expectedErrno: wasip1.ErrnoExist, |
| 3094 |
expectedLog: ` |
| 3095 |
==> wasi_snapshot_preview1.path_create_directory(fd=3,path=file) |
| 3096 |
<== errno=EEXIST |
| 3097 |
`, |
| 3098 |
}, |
| 3099 |
{ |
| 3100 |
name: "dir exists", |
| 3101 |
fd: sys.FdPreopen, |
| 3102 |
pathName: dir, |
| 3103 |
path: 0, |
| 3104 |
pathLen: uint32(len(dir)), |
| 3105 |
expectedErrno: wasip1.ErrnoExist, |
| 3106 |
expectedLog: ` |
| 3107 |
==> wasi_snapshot_preview1.path_create_directory(fd=3,path=dir) |
| 3108 |
<== errno=EEXIST |
| 3109 |
`, |
| 3110 |
}, |
| 3111 |
} |
| 3112 |
|
| 3113 |
for _, tt := range tests { |
| 3114 |
tc := tt |
| 3115 |
t.Run(tc.name, func(t *testing.T) { |
| 3116 |
defer log.Reset() |
| 3117 |
|
| 3118 |
mod.Memory().Write(tc.path, []byte(tc.pathName)) |
| 3119 |
|
| 3120 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.PathCreateDirectoryName, uint64(tc.fd), uint64(tc.path), uint64(tc.pathLen)) |
| 3121 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 3122 |
}) |
| 3123 |
} |
| 3124 |
} |
| 3125 |
|
| 3126 |
func Test_pathFilestatGet(t *testing.T) { |
| 3127 |
file, dir, fileInDir := "animals.txt", "sub", "sub/test.txt" |
| 3128 |
|
| 3129 |
initialMemoryFile := append([]byte{'?'}, file...) |
| 3130 |
initialMemoryDir := append([]byte{'?'}, dir...) |
| 3131 |
initialMemoryFileInDir := append([]byte{'?'}, fileInDir...) |
| 3132 |
initialMemoryNotExists := []byte{'?', '?'} |
| 3133 |
|
| 3134 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(fstest.FS)) |
| 3135 |
defer r.Close(testCtx) |
| 3136 |
memorySize := mod.Memory().Size() |
| 3137 |
|
| 3138 |
fileFD := requireOpenFD(t, mod, file) |
| 3139 |
|
| 3140 |
tests := []struct { |
| 3141 |
name string |
| 3142 |
fd int32 |
| 3143 |
pathLen, resultFilestat uint32 |
| 3144 |
flags uint16 |
| 3145 |
memory, expectedMemory []byte |
| 3146 |
expectedErrno wasip1.Errno |
| 3147 |
expectedLog string |
| 3148 |
}{ |
| 3149 |
{ |
| 3150 |
name: "file under root", |
| 3151 |
fd: sys.FdPreopen, |
| 3152 |
memory: initialMemoryFile, |
| 3153 |
pathLen: uint32(len(file)), |
| 3154 |
resultFilestat: uint32(len(file)) + 1, |
| 3155 |
expectedMemory: append( |
| 3156 |
initialMemoryFile, |
| 3157 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 3158 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 3159 |
4, 0, 0, 0, 0, 0, 0, 0, |
| 3160 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 3161 |
30, 0, 0, 0, 0, 0, 0, 0, |
| 3162 |
0x0, 0x82, 0x13, 0x80, 0x6b, 0x16, 0x24, 0x17, |
| 3163 |
0x0, 0x82, 0x13, 0x80, 0x6b, 0x16, 0x24, 0x17, |
| 3164 |
0x0, 0x82, 0x13, 0x80, 0x6b, 0x16, 0x24, 0x17, |
| 3165 |
), |
| 3166 |
expectedLog: ` |
| 3167 |
==> wasi_snapshot_preview1.path_filestat_get(fd=3,flags=,path=animals.txt) |
| 3168 |
<== (filestat={filetype=REGULAR_FILE,size=30,mtim=1667482413000000000},errno=ESUCCESS) |
| 3169 |
`, |
| 3170 |
}, |
| 3171 |
{ |
| 3172 |
name: "file under dir", |
| 3173 |
fd: sys.FdPreopen, |
| 3174 |
memory: initialMemoryFileInDir, |
| 3175 |
pathLen: uint32(len(fileInDir)), |
| 3176 |
resultFilestat: uint32(len(fileInDir)) + 1, |
| 3177 |
expectedMemory: append( |
| 3178 |
initialMemoryFileInDir, |
| 3179 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 3180 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 3181 |
4, 0, 0, 0, 0, 0, 0, 0, |
| 3182 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 3183 |
14, 0, 0, 0, 0, 0, 0, 0, |
| 3184 |
0x0, 0x0, 0xc2, 0xd3, 0x43, 0x6, 0x36, 0x17, |
| 3185 |
0x0, 0x0, 0xc2, 0xd3, 0x43, 0x6, 0x36, 0x17, |
| 3186 |
0x0, 0x0, 0xc2, 0xd3, 0x43, 0x6, 0x36, 0x17, |
| 3187 |
), |
| 3188 |
expectedLog: ` |
| 3189 |
==> wasi_snapshot_preview1.path_filestat_get(fd=3,flags=,path=sub/test.txt) |
| 3190 |
<== (filestat={filetype=REGULAR_FILE,size=14,mtim=1672531200000000000},errno=ESUCCESS) |
| 3191 |
`, |
| 3192 |
}, |
| 3193 |
{ |
| 3194 |
name: "dir under root", |
| 3195 |
fd: sys.FdPreopen, |
| 3196 |
memory: initialMemoryDir, |
| 3197 |
pathLen: uint32(len(dir)), |
| 3198 |
resultFilestat: uint32(len(dir)) + 1, |
| 3199 |
expectedMemory: append( |
| 3200 |
initialMemoryDir, |
| 3201 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 3202 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 3203 |
3, 0, 0, 0, 0, 0, 0, 0, |
| 3204 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 3205 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 3206 |
0x0, 0x0, 0x1f, 0xa6, 0x70, 0xfc, 0xc5, 0x16, |
| 3207 |
0x0, 0x0, 0x1f, 0xa6, 0x70, 0xfc, 0xc5, 0x16, |
| 3208 |
0x0, 0x0, 0x1f, 0xa6, 0x70, 0xfc, 0xc5, 0x16, |
| 3209 |
), |
| 3210 |
expectedLog: ` |
| 3211 |
==> wasi_snapshot_preview1.path_filestat_get(fd=3,flags=,path=sub) |
| 3212 |
<== (filestat={filetype=DIRECTORY,size=0,mtim=1640995200000000000},errno=ESUCCESS) |
| 3213 |
`, |
| 3214 |
}, |
| 3215 |
{ |
| 3216 |
name: "unopened FD", |
| 3217 |
fd: -1, |
| 3218 |
expectedErrno: wasip1.ErrnoBadf, |
| 3219 |
expectedLog: ` |
| 3220 |
==> wasi_snapshot_preview1.path_filestat_get(fd=-1,flags=,path=) |
| 3221 |
<== (filestat=,errno=EBADF) |
| 3222 |
`, |
| 3223 |
}, |
| 3224 |
{ |
| 3225 |
name: "Fd not a directory", |
| 3226 |
fd: fileFD, |
| 3227 |
memory: initialMemoryFile, |
| 3228 |
pathLen: uint32(len(file)), |
| 3229 |
resultFilestat: 2, |
| 3230 |
expectedErrno: wasip1.ErrnoNotdir, |
| 3231 |
expectedLog: ` |
| 3232 |
==> wasi_snapshot_preview1.path_filestat_get(fd=4,flags=,path=animals.txt) |
| 3233 |
<== (filestat=,errno=ENOTDIR) |
| 3234 |
`, |
| 3235 |
}, |
| 3236 |
{ |
| 3237 |
name: "path under root doesn't exist", |
| 3238 |
fd: sys.FdPreopen, |
| 3239 |
memory: initialMemoryNotExists, |
| 3240 |
pathLen: 1, |
| 3241 |
resultFilestat: 2, |
| 3242 |
expectedErrno: wasip1.ErrnoNoent, |
| 3243 |
expectedLog: ` |
| 3244 |
==> wasi_snapshot_preview1.path_filestat_get(fd=3,flags=,path=?) |
| 3245 |
<== (filestat=,errno=ENOENT) |
| 3246 |
`, |
| 3247 |
}, |
| 3248 |
{ |
| 3249 |
name: "path is out of memory", |
| 3250 |
fd: sys.FdPreopen, |
| 3251 |
memory: initialMemoryFile, |
| 3252 |
pathLen: memorySize, |
| 3253 |
expectedErrno: wasip1.ErrnoFault, |
| 3254 |
expectedLog: ` |
| 3255 |
==> wasi_snapshot_preview1.path_filestat_get(fd=3,flags=,path=OOM(1,65536)) |
| 3256 |
<== (filestat=,errno=EFAULT) |
| 3257 |
`, |
| 3258 |
}, |
| 3259 |
{ |
| 3260 |
name: "resultFilestat exceeds the maximum valid address by 1", |
| 3261 |
fd: sys.FdPreopen, |
| 3262 |
memory: initialMemoryFile, |
| 3263 |
pathLen: uint32(len(file)), |
| 3264 |
resultFilestat: memorySize - 64 + 1, |
| 3265 |
expectedErrno: wasip1.ErrnoFault, |
| 3266 |
expectedLog: ` |
| 3267 |
==> wasi_snapshot_preview1.path_filestat_get(fd=3,flags=,path=animals.txt) |
| 3268 |
<== (filestat=,errno=EFAULT) |
| 3269 |
`, |
| 3270 |
}, |
| 3271 |
{ |
| 3272 |
name: "file under root (follow symlinks)", |
| 3273 |
fd: sys.FdPreopen, |
| 3274 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3275 |
memory: initialMemoryFile, |
| 3276 |
pathLen: uint32(len(file)), |
| 3277 |
resultFilestat: uint32(len(file)) + 1, |
| 3278 |
expectedMemory: append( |
| 3279 |
initialMemoryFile, |
| 3280 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 3281 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 3282 |
4, 0, 0, 0, 0, 0, 0, 0, |
| 3283 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 3284 |
30, 0, 0, 0, 0, 0, 0, 0, |
| 3285 |
0x0, 0x82, 0x13, 0x80, 0x6b, 0x16, 0x24, 0x17, |
| 3286 |
0x0, 0x82, 0x13, 0x80, 0x6b, 0x16, 0x24, 0x17, |
| 3287 |
0x0, 0x82, 0x13, 0x80, 0x6b, 0x16, 0x24, 0x17, |
| 3288 |
), |
| 3289 |
expectedLog: ` |
| 3290 |
==> wasi_snapshot_preview1.path_filestat_get(fd=3,flags=SYMLINK_FOLLOW,path=animals.txt) |
| 3291 |
<== (filestat={filetype=REGULAR_FILE,size=30,mtim=1667482413000000000},errno=ESUCCESS) |
| 3292 |
`, |
| 3293 |
}, |
| 3294 |
{ |
| 3295 |
name: "file under dir (follow symlinks)", |
| 3296 |
fd: sys.FdPreopen, |
| 3297 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3298 |
memory: initialMemoryFileInDir, |
| 3299 |
pathLen: uint32(len(fileInDir)), |
| 3300 |
resultFilestat: uint32(len(fileInDir)) + 1, |
| 3301 |
expectedMemory: append( |
| 3302 |
initialMemoryFileInDir, |
| 3303 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 3304 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 3305 |
4, 0, 0, 0, 0, 0, 0, 0, |
| 3306 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 3307 |
14, 0, 0, 0, 0, 0, 0, 0, |
| 3308 |
0x0, 0x0, 0xc2, 0xd3, 0x43, 0x6, 0x36, 0x17, |
| 3309 |
0x0, 0x0, 0xc2, 0xd3, 0x43, 0x6, 0x36, 0x17, |
| 3310 |
0x0, 0x0, 0xc2, 0xd3, 0x43, 0x6, 0x36, 0x17, |
| 3311 |
), |
| 3312 |
expectedLog: ` |
| 3313 |
==> wasi_snapshot_preview1.path_filestat_get(fd=3,flags=SYMLINK_FOLLOW,path=sub/test.txt) |
| 3314 |
<== (filestat={filetype=REGULAR_FILE,size=14,mtim=1672531200000000000},errno=ESUCCESS) |
| 3315 |
`, |
| 3316 |
}, |
| 3317 |
{ |
| 3318 |
name: "dir under root (follow symlinks)", |
| 3319 |
fd: sys.FdPreopen, |
| 3320 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3321 |
memory: initialMemoryDir, |
| 3322 |
pathLen: uint32(len(dir)), |
| 3323 |
resultFilestat: uint32(len(dir)) + 1, |
| 3324 |
expectedMemory: append( |
| 3325 |
initialMemoryDir, |
| 3326 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 3327 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 3328 |
3, 0, 0, 0, 0, 0, 0, 0, |
| 3329 |
1, 0, 0, 0, 0, 0, 0, 0, |
| 3330 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 3331 |
0x0, 0x0, 0x1f, 0xa6, 0x70, 0xfc, 0xc5, 0x16, |
| 3332 |
0x0, 0x0, 0x1f, 0xa6, 0x70, 0xfc, 0xc5, 0x16, |
| 3333 |
0x0, 0x0, 0x1f, 0xa6, 0x70, 0xfc, 0xc5, 0x16, |
| 3334 |
), |
| 3335 |
expectedLog: ` |
| 3336 |
==> wasi_snapshot_preview1.path_filestat_get(fd=3,flags=SYMLINK_FOLLOW,path=sub) |
| 3337 |
<== (filestat={filetype=DIRECTORY,size=0,mtim=1640995200000000000},errno=ESUCCESS) |
| 3338 |
`, |
| 3339 |
}, |
| 3340 |
{ |
| 3341 |
name: "unopened FD (follow symlinks)", |
| 3342 |
fd: -1, |
| 3343 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3344 |
expectedErrno: wasip1.ErrnoBadf, |
| 3345 |
expectedLog: ` |
| 3346 |
==> wasi_snapshot_preview1.path_filestat_get(fd=-1,flags=SYMLINK_FOLLOW,path=) |
| 3347 |
<== (filestat=,errno=EBADF) |
| 3348 |
`, |
| 3349 |
}, |
| 3350 |
{ |
| 3351 |
name: "Fd not a directory (follow symlinks)", |
| 3352 |
fd: fileFD, |
| 3353 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3354 |
memory: initialMemoryFile, |
| 3355 |
pathLen: uint32(len(file)), |
| 3356 |
resultFilestat: 2, |
| 3357 |
expectedErrno: wasip1.ErrnoNotdir, |
| 3358 |
expectedLog: ` |
| 3359 |
==> wasi_snapshot_preview1.path_filestat_get(fd=4,flags=SYMLINK_FOLLOW,path=animals.txt) |
| 3360 |
<== (filestat=,errno=ENOTDIR) |
| 3361 |
`, |
| 3362 |
}, |
| 3363 |
{ |
| 3364 |
name: "path under root doesn't exist (follow symlinks)", |
| 3365 |
fd: sys.FdPreopen, |
| 3366 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3367 |
memory: initialMemoryNotExists, |
| 3368 |
pathLen: 1, |
| 3369 |
resultFilestat: 2, |
| 3370 |
expectedErrno: wasip1.ErrnoNoent, |
| 3371 |
expectedLog: ` |
| 3372 |
==> wasi_snapshot_preview1.path_filestat_get(fd=3,flags=SYMLINK_FOLLOW,path=?) |
| 3373 |
<== (filestat=,errno=ENOENT) |
| 3374 |
`, |
| 3375 |
}, |
| 3376 |
{ |
| 3377 |
name: "path is out of memory (follow symlinks)", |
| 3378 |
fd: sys.FdPreopen, |
| 3379 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3380 |
memory: initialMemoryFile, |
| 3381 |
pathLen: memorySize, |
| 3382 |
expectedErrno: wasip1.ErrnoFault, |
| 3383 |
expectedLog: ` |
| 3384 |
==> wasi_snapshot_preview1.path_filestat_get(fd=3,flags=SYMLINK_FOLLOW,path=OOM(1,65536)) |
| 3385 |
<== (filestat=,errno=EFAULT) |
| 3386 |
`, |
| 3387 |
}, |
| 3388 |
{ |
| 3389 |
name: "resultFilestat exceeds the maximum valid address by 1 (follow symlinks)", |
| 3390 |
fd: sys.FdPreopen, |
| 3391 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3392 |
memory: initialMemoryFile, |
| 3393 |
pathLen: uint32(len(file)), |
| 3394 |
resultFilestat: memorySize - 64 + 1, |
| 3395 |
expectedErrno: wasip1.ErrnoFault, |
| 3396 |
expectedLog: ` |
| 3397 |
==> wasi_snapshot_preview1.path_filestat_get(fd=3,flags=SYMLINK_FOLLOW,path=animals.txt) |
| 3398 |
<== (filestat=,errno=EFAULT) |
| 3399 |
`, |
| 3400 |
}, |
| 3401 |
} |
| 3402 |
|
| 3403 |
for _, tt := range tests { |
| 3404 |
tc := tt |
| 3405 |
|
| 3406 |
t.Run(tc.name, func(t *testing.T) { |
| 3407 |
defer log.Reset() |
| 3408 |
|
| 3409 |
maskMemory(t, mod, len(tc.expectedMemory)) |
| 3410 |
mod.Memory().Write(0, tc.memory) |
| 3411 |
|
| 3412 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.PathFilestatGetName, uint64(tc.fd), uint64(tc.flags), uint64(1), uint64(tc.pathLen), uint64(tc.resultFilestat)) |
| 3413 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 3414 |
|
| 3415 |
actual, ok := mod.Memory().Read(0, uint32(len(tc.expectedMemory))) |
| 3416 |
require.True(t, ok) |
| 3417 |
require.Equal(t, tc.expectedMemory, actual) |
| 3418 |
}) |
| 3419 |
} |
| 3420 |
} |
| 3421 |
|
| 3422 |
func Test_pathFilestatSetTimes(t *testing.T) { |
| 3423 |
tmpDir := t.TempDir() |
| 3424 |
|
| 3425 |
file := "file" |
| 3426 |
writeFile(t, tmpDir, file, []byte("012")) |
| 3427 |
link := file + "-link" |
| 3428 |
require.NoError(t, os.Symlink(joinPath(tmpDir, file), joinPath(tmpDir, link))) |
| 3429 |
|
| 3430 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig(). |
| 3431 |
WithSysWalltime(). |
| 3432 |
WithFSConfig(wazero.NewFSConfig().WithDirMount(tmpDir, ""))) |
| 3433 |
defer r.Close(testCtx) |
| 3434 |
|
| 3435 |
tests := []struct { |
| 3436 |
name string |
| 3437 |
flags uint16 |
| 3438 |
pathName string |
| 3439 |
mtime, atime int64 |
| 3440 |
fstFlags uint16 |
| 3441 |
expectedLog string |
| 3442 |
expectedErrno wasip1.Errno |
| 3443 |
}{ |
| 3444 |
{ |
| 3445 |
name: "a=omit,m=omit", |
| 3446 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3447 |
atime: 123451, |
| 3448 |
mtime: 1234, |
| 3449 |
expectedLog: ` |
| 3450 |
==> wasi_snapshot_preview1.path_filestat_set_times(fd=3,flags=SYMLINK_FOLLOW,path=file,atim=123451,mtim=1234,fst_flags=) |
| 3451 |
<== errno=ESUCCESS |
| 3452 |
`, |
| 3453 |
}, |
| 3454 |
{ |
| 3455 |
name: "a=now,m=omit", |
| 3456 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3457 |
atime: 123451, |
| 3458 |
mtime: 1234, |
| 3459 |
fstFlags: wasip1.FstflagsAtimNow, |
| 3460 |
expectedLog: ` |
| 3461 |
==> wasi_snapshot_preview1.path_filestat_set_times(fd=3,flags=SYMLINK_FOLLOW,path=file,atim=123451,mtim=1234,fst_flags=ATIM_NOW) |
| 3462 |
<== errno=ESUCCESS |
| 3463 |
`, |
| 3464 |
}, |
| 3465 |
{ |
| 3466 |
name: "a=omit,m=now", |
| 3467 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3468 |
atime: 123451, |
| 3469 |
mtime: 1234, |
| 3470 |
fstFlags: wasip1.FstflagsMtimNow, |
| 3471 |
expectedLog: ` |
| 3472 |
==> wasi_snapshot_preview1.path_filestat_set_times(fd=3,flags=SYMLINK_FOLLOW,path=file,atim=123451,mtim=1234,fst_flags=MTIM_NOW) |
| 3473 |
<== errno=ESUCCESS |
| 3474 |
`, |
| 3475 |
}, |
| 3476 |
{ |
| 3477 |
name: "a=now,m=now", |
| 3478 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3479 |
atime: 123451, |
| 3480 |
mtime: 1234, |
| 3481 |
fstFlags: wasip1.FstflagsAtimNow | wasip1.FstflagsMtimNow, |
| 3482 |
expectedLog: ` |
| 3483 |
==> wasi_snapshot_preview1.path_filestat_set_times(fd=3,flags=SYMLINK_FOLLOW,path=file,atim=123451,mtim=1234,fst_flags=ATIM_NOW|MTIM_NOW) |
| 3484 |
<== errno=ESUCCESS |
| 3485 |
`, |
| 3486 |
}, |
| 3487 |
{ |
| 3488 |
name: "a=now,m=set", |
| 3489 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3490 |
atime: 1234, |
| 3491 |
mtime: 55555500, |
| 3492 |
fstFlags: wasip1.FstflagsAtimNow | wasip1.FstflagsMtim, |
| 3493 |
expectedLog: ` |
| 3494 |
==> wasi_snapshot_preview1.path_filestat_set_times(fd=3,flags=SYMLINK_FOLLOW,path=file,atim=1234,mtim=55555500,fst_flags=ATIM_NOW|MTIM) |
| 3495 |
<== errno=ESUCCESS |
| 3496 |
`, |
| 3497 |
}, |
| 3498 |
{ |
| 3499 |
name: "a=set,m=now", |
| 3500 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3501 |
atime: 55555500, |
| 3502 |
mtime: 1234, |
| 3503 |
fstFlags: wasip1.FstflagsAtim | wasip1.FstflagsMtimNow, |
| 3504 |
expectedLog: ` |
| 3505 |
==> wasi_snapshot_preview1.path_filestat_set_times(fd=3,flags=SYMLINK_FOLLOW,path=file,atim=55555500,mtim=1234,fst_flags=ATIM|MTIM_NOW) |
| 3506 |
<== errno=ESUCCESS |
| 3507 |
`, |
| 3508 |
}, |
| 3509 |
{ |
| 3510 |
name: "a=set,m=omit", |
| 3511 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3512 |
atime: 55555500, |
| 3513 |
mtime: 1234, |
| 3514 |
fstFlags: wasip1.FstflagsAtim, |
| 3515 |
expectedLog: ` |
| 3516 |
==> wasi_snapshot_preview1.path_filestat_set_times(fd=3,flags=SYMLINK_FOLLOW,path=file,atim=55555500,mtim=1234,fst_flags=ATIM) |
| 3517 |
<== errno=ESUCCESS |
| 3518 |
`, |
| 3519 |
}, |
| 3520 |
{ |
| 3521 |
name: "a=omit,m=set", |
| 3522 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3523 |
atime: 1234, |
| 3524 |
mtime: 55555500, |
| 3525 |
fstFlags: wasip1.FstflagsMtim, |
| 3526 |
expectedLog: ` |
| 3527 |
==> wasi_snapshot_preview1.path_filestat_set_times(fd=3,flags=SYMLINK_FOLLOW,path=file,atim=1234,mtim=55555500,fst_flags=MTIM) |
| 3528 |
<== errno=ESUCCESS |
| 3529 |
`, |
| 3530 |
}, |
| 3531 |
{ |
| 3532 |
name: "a=set,m=set", |
| 3533 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3534 |
atime: 6666666600, |
| 3535 |
mtime: 55555500, |
| 3536 |
fstFlags: wasip1.FstflagsAtim | wasip1.FstflagsMtim, |
| 3537 |
expectedLog: ` |
| 3538 |
==> wasi_snapshot_preview1.path_filestat_set_times(fd=3,flags=SYMLINK_FOLLOW,path=file,atim=6666666600,mtim=55555500,fst_flags=ATIM|MTIM) |
| 3539 |
<== errno=ESUCCESS |
| 3540 |
`, |
| 3541 |
}, |
| 3542 |
{ |
| 3543 |
name: "not found", |
| 3544 |
pathName: "nope", |
| 3545 |
flags: wasip1.LOOKUP_SYMLINK_FOLLOW, |
| 3546 |
fstFlags: wasip1.FstflagsAtimNow, |
| 3547 |
expectedLog: ` |
| 3548 |
==> wasi_snapshot_preview1.path_filestat_set_times(fd=3,flags=SYMLINK_FOLLOW,path=nope,atim=0,mtim=0,fst_flags=ATIM_NOW) |
| 3549 |
<== errno=ENOENT |
| 3550 |
`, |
| 3551 |
expectedErrno: wasip1.ErrnoNoent, |
| 3552 |
}, |
| 3553 |
{ |
| 3554 |
name: "no_symlink_follow", |
| 3555 |
pathName: link, |
| 3556 |
flags: 0, |
| 3557 |
atime: 123451, |
| 3558 |
mtime: 1234, |
| 3559 |
fstFlags: wasip1.FstflagsMtimNow, |
| 3560 |
expectedLog: ` |
| 3561 |
==> wasi_snapshot_preview1.path_filestat_set_times(fd=3,flags=,path=file-link,atim=123451,mtim=1234,fst_flags=MTIM_NOW) |
| 3562 |
<== errno=ESUCCESS |
| 3563 |
`, |
| 3564 |
}, |
| 3565 |
} |
| 3566 |
|
| 3567 |
for _, tt := range tests { |
| 3568 |
tc := tt |
| 3569 |
|
| 3570 |
t.Run(tc.name, func(t *testing.T) { |
| 3571 |
defer log.Reset() |
| 3572 |
|
| 3573 |
pathName := tc.pathName |
| 3574 |
if pathName == "" { |
| 3575 |
pathName = file |
| 3576 |
} |
| 3577 |
mod.Memory().Write(0, []byte(pathName)) |
| 3578 |
|
| 3579 |
fd := sys.FdPreopen |
| 3580 |
path := 0 |
| 3581 |
pathLen := uint32(len(pathName)) |
| 3582 |
|
| 3583 |
sys := mod.(*wasm.ModuleInstance).Sys |
| 3584 |
fsc := sys.FS() |
| 3585 |
|
| 3586 |
preopen := getPreopen(t, fsc) |
| 3587 |
|
| 3588 |
var oldSt sysapi.Stat_t |
| 3589 |
var errno experimentalsys.Errno |
| 3590 |
if tc.expectedErrno == wasip1.ErrnoSuccess { |
| 3591 |
oldSt, errno = preopen.Stat(pathName) |
| 3592 |
require.EqualErrno(t, 0, errno) |
| 3593 |
} |
| 3594 |
|
| 3595 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.PathFilestatSetTimesName, uint64(fd), uint64(tc.flags), |
| 3596 |
uint64(path), uint64(pathLen), uint64(tc.atime), uint64(tc.mtime), uint64(tc.fstFlags)) |
| 3597 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 3598 |
|
| 3599 |
if tc.expectedErrno != wasip1.ErrnoSuccess { |
| 3600 |
return |
| 3601 |
} |
| 3602 |
|
| 3603 |
newSt, errno := preopen.Stat(pathName) |
| 3604 |
require.EqualErrno(t, 0, errno) |
| 3605 |
|
| 3606 |
if platform.CompilerSupported() { |
| 3607 |
if tc.fstFlags&wasip1.FstflagsAtim != 0 { |
| 3608 |
require.Equal(t, tc.atime, newSt.Atim) |
| 3609 |
} else if tc.fstFlags&wasip1.FstflagsAtimNow != 0 { |
| 3610 |
now := time.Now().UnixNano() |
| 3611 |
require.True(t, newSt.Atim <= now, "expected atim %d <= now %d", newSt.Atim, now) |
| 3612 |
} else { |
| 3613 |
require.Equal(t, oldSt.Atim, newSt.Atim) |
| 3614 |
} |
| 3615 |
} |
| 3616 |
|
| 3617 |
|
| 3618 |
if tc.fstFlags&wasip1.FstflagsMtim != 0 { |
| 3619 |
require.Equal(t, tc.mtime, newSt.Mtim) |
| 3620 |
} else if tc.fstFlags&wasip1.FstflagsMtimNow != 0 { |
| 3621 |
now := time.Now().UnixNano() |
| 3622 |
require.True(t, newSt.Mtim <= now, "expected mtim %d <= now %d", newSt.Mtim, now) |
| 3623 |
} else { |
| 3624 |
require.Equal(t, oldSt.Mtim, newSt.Mtim) |
| 3625 |
} |
| 3626 |
}) |
| 3627 |
} |
| 3628 |
} |
| 3629 |
|
| 3630 |
func Test_pathLink(t *testing.T) { |
| 3631 |
tmpDir := t.TempDir() |
| 3632 |
|
| 3633 |
oldDirName := "my-old-dir" |
| 3634 |
oldDirPath := joinPath(tmpDir, oldDirName) |
| 3635 |
mod, oldFd, log, r := requireOpenFile(t, tmpDir, oldDirName, nil, false) |
| 3636 |
defer r.Close(testCtx) |
| 3637 |
|
| 3638 |
newDirName := "my-new-dir/sub" |
| 3639 |
newDirPath := joinPath(tmpDir, newDirName) |
| 3640 |
require.NoError(t, os.MkdirAll(joinPath(tmpDir, newDirName), 0o700)) |
| 3641 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 3642 |
preopen := getPreopen(t, fsc) |
| 3643 |
newFd, errno := fsc.OpenFile(preopen, newDirName, 0o600, 0) |
| 3644 |
require.EqualErrno(t, 0, errno) |
| 3645 |
|
| 3646 |
mem := mod.Memory() |
| 3647 |
|
| 3648 |
fileName := "file" |
| 3649 |
err := os.WriteFile(joinPath(oldDirPath, fileName), []byte{1, 2, 3, 4}, 0o700) |
| 3650 |
require.NoError(t, err) |
| 3651 |
|
| 3652 |
file := uint32(0xff) |
| 3653 |
ok := mem.Write(file, []byte(fileName)) |
| 3654 |
require.True(t, ok) |
| 3655 |
|
| 3656 |
notFoundFile := uint32(0xaa) |
| 3657 |
notFoundFileName := "nope" |
| 3658 |
ok = mem.Write(notFoundFile, []byte(notFoundFileName)) |
| 3659 |
require.True(t, ok) |
| 3660 |
|
| 3661 |
destination := uint32(0xcc) |
| 3662 |
destinationName := "hard-linked" |
| 3663 |
ok = mem.Write(destination, []byte(destinationName)) |
| 3664 |
require.True(t, ok) |
| 3665 |
|
| 3666 |
destinationRealPath := joinPath(newDirPath, destinationName) |
| 3667 |
|
| 3668 |
t.Run("success", func(t *testing.T) { |
| 3669 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.PathLinkName, |
| 3670 |
uint64(oldFd), 0, uint64(file), uint64(len(fileName)), |
| 3671 |
uint64(newFd), uint64(destination), uint64(len(destinationName))) |
| 3672 |
require.Contains(t, log.String(), wasip1.ErrnoName(wasip1.ErrnoSuccess)) |
| 3673 |
|
| 3674 |
f := openFile(t, destinationRealPath, experimentalsys.O_RDONLY, 0) |
| 3675 |
defer f.Close() |
| 3676 |
|
| 3677 |
st, errno := f.Stat() |
| 3678 |
require.EqualErrno(t, 0, errno) |
| 3679 |
require.False(t, st.Mode&os.ModeSymlink == os.ModeSymlink) |
| 3680 |
require.Equal(t, uint64(2), st.Nlink) |
| 3681 |
}) |
| 3682 |
|
| 3683 |
t.Run("errors", func(t *testing.T) { |
| 3684 |
for _, tc := range []struct { |
| 3685 |
errno wasip1.Errno |
| 3686 |
oldFd int32 |
| 3687 |
oldPath, oldPathLen uint32 |
| 3688 |
newFd int32 |
| 3689 |
newPath, newPathLen uint32 |
| 3690 |
}{ |
| 3691 |
{errno: wasip1.ErrnoBadf, oldFd: 1000}, |
| 3692 |
{errno: wasip1.ErrnoBadf, oldFd: oldFd, newFd: 1000}, |
| 3693 |
{errno: wasip1.ErrnoNotdir, oldFd: oldFd, newFd: 1}, |
| 3694 |
{errno: wasip1.ErrnoNotdir, oldFd: 1, newFd: 1}, |
| 3695 |
{errno: wasip1.ErrnoNotdir, oldFd: 1, newFd: newFd}, |
| 3696 |
{errno: wasip1.ErrnoFault, oldFd: oldFd, newFd: newFd, oldPathLen: math.MaxUint32}, |
| 3697 |
{errno: wasip1.ErrnoFault, oldFd: oldFd, newFd: newFd, newPathLen: math.MaxUint32}, |
| 3698 |
{ |
| 3699 |
errno: wasip1.ErrnoFault, oldFd: oldFd, newFd: newFd, |
| 3700 |
oldPath: math.MaxUint32, oldPathLen: 100, newPathLen: 100, |
| 3701 |
}, |
| 3702 |
{ |
| 3703 |
errno: wasip1.ErrnoFault, oldFd: oldFd, newFd: newFd, |
| 3704 |
oldPath: 1, oldPathLen: 100, newPath: math.MaxUint32, newPathLen: 100, |
| 3705 |
}, |
| 3706 |
} { |
| 3707 |
name := wasip1.ErrnoName(tc.errno) |
| 3708 |
t.Run(name, func(t *testing.T) { |
| 3709 |
requireErrnoResult(t, tc.errno, mod, wasip1.PathLinkName, |
| 3710 |
uint64(tc.oldFd), 0, uint64(tc.oldPath), uint64(tc.oldPathLen), |
| 3711 |
uint64(tc.newFd), uint64(tc.newPath), uint64(tc.newPathLen)) |
| 3712 |
require.Contains(t, log.String(), name) |
| 3713 |
}) |
| 3714 |
} |
| 3715 |
}) |
| 3716 |
} |
| 3717 |
|
| 3718 |
func Test_pathOpen(t *testing.T) { |
| 3719 |
dir := t.TempDir() |
| 3720 |
writeFS := sysfs.DirFS(dir) |
| 3721 |
readFS := &sysfs.ReadFS{FS: writeFS} |
| 3722 |
|
| 3723 |
fileName := "file" |
| 3724 |
fileContents := []byte("012") |
| 3725 |
writeFile(t, dir, fileName, fileContents) |
| 3726 |
|
| 3727 |
appendName := "append" |
| 3728 |
appendContents := []byte("345") |
| 3729 |
writeFile(t, dir, appendName, appendContents) |
| 3730 |
|
| 3731 |
truncName := "trunc" |
| 3732 |
truncContents := []byte("678") |
| 3733 |
writeFile(t, dir, truncName, truncContents) |
| 3734 |
|
| 3735 |
dirName := "dir" |
| 3736 |
mkdir(t, dir, dirName) |
| 3737 |
|
| 3738 |
dirFileName := joinPath(dirName, fileName) |
| 3739 |
dirFileContents := []byte("def") |
| 3740 |
writeFile(t, dir, dirFileName, dirFileContents) |
| 3741 |
|
| 3742 |
expectedOpenedFd := sys.FdPreopen + 1 |
| 3743 |
|
| 3744 |
tests := []struct { |
| 3745 |
name string |
| 3746 |
fs experimentalsys.FS |
| 3747 |
path func(t *testing.T) string |
| 3748 |
oflags uint16 |
| 3749 |
fdflags uint16 |
| 3750 |
rights uint32 |
| 3751 |
expected func(t *testing.T, fsc *sys.FSContext) |
| 3752 |
expectedErrno wasip1.Errno |
| 3753 |
expectedLog string |
| 3754 |
}{ |
| 3755 |
{ |
| 3756 |
name: "sysfs.ReadFS", |
| 3757 |
fs: readFS, |
| 3758 |
path: func(*testing.T) string { return fileName }, |
| 3759 |
expected: func(t *testing.T, fsc *sys.FSContext) { |
| 3760 |
requireContents(t, fsc, expectedOpenedFd, fileName, fileContents) |
| 3761 |
}, |
| 3762 |
expectedLog: ` |
| 3763 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=file,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 3764 |
<== (opened_fd=4,errno=ESUCCESS) |
| 3765 |
`, |
| 3766 |
}, |
| 3767 |
{ |
| 3768 |
name: "sysfs.DirFS", |
| 3769 |
fs: writeFS, |
| 3770 |
path: func(*testing.T) string { return fileName }, |
| 3771 |
expected: func(t *testing.T, fsc *sys.FSContext) { |
| 3772 |
requireContents(t, fsc, expectedOpenedFd, fileName, fileContents) |
| 3773 |
}, |
| 3774 |
expectedLog: ` |
| 3775 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=file,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 3776 |
<== (opened_fd=4,errno=ESUCCESS) |
| 3777 |
`, |
| 3778 |
}, |
| 3779 |
{ |
| 3780 |
name: "sysfs.ReadFS FD_APPEND", |
| 3781 |
fs: readFS, |
| 3782 |
fdflags: wasip1.FD_APPEND, |
| 3783 |
path: func(t *testing.T) (file string) { return appendName }, |
| 3784 |
expectedErrno: wasip1.ErrnoNosys, |
| 3785 |
expectedLog: ` |
| 3786 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=append,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=APPEND) |
| 3787 |
<== (opened_fd=,errno=ENOSYS) |
| 3788 |
`, |
| 3789 |
}, |
| 3790 |
{ |
| 3791 |
name: "sysfs.DirFS FD_APPEND", |
| 3792 |
fs: writeFS, |
| 3793 |
path: func(t *testing.T) (file string) { return appendName }, |
| 3794 |
fdflags: wasip1.FD_APPEND, |
| 3795 |
expected: func(t *testing.T, fsc *sys.FSContext) { |
| 3796 |
contents := writeAndCloseFile(t, fsc, expectedOpenedFd) |
| 3797 |
|
| 3798 |
|
| 3799 |
b := readFile(t, dir, appendName) |
| 3800 |
require.Equal(t, append(appendContents, contents...), b) |
| 3801 |
}, |
| 3802 |
expectedLog: ` |
| 3803 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=append,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=APPEND) |
| 3804 |
<== (opened_fd=4,errno=ESUCCESS) |
| 3805 |
`, |
| 3806 |
}, |
| 3807 |
{ |
| 3808 |
name: "sysfs.ReadFS O_CREAT", |
| 3809 |
fs: readFS, |
| 3810 |
oflags: wasip1.O_CREAT, |
| 3811 |
expectedErrno: wasip1.ErrnoNosys, |
| 3812 |
path: func(*testing.T) string { return "creat" }, |
| 3813 |
expectedLog: ` |
| 3814 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=creat,oflags=CREAT,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 3815 |
<== (opened_fd=,errno=ENOSYS) |
| 3816 |
`, |
| 3817 |
}, |
| 3818 |
{ |
| 3819 |
name: "sysfs.DirFS O_CREAT", |
| 3820 |
fs: writeFS, |
| 3821 |
path: func(t *testing.T) (file string) { return "creat" }, |
| 3822 |
oflags: wasip1.O_CREAT, |
| 3823 |
expected: func(t *testing.T, fsc *sys.FSContext) { |
| 3824 |
|
| 3825 |
contents := writeAndCloseFile(t, fsc, expectedOpenedFd) |
| 3826 |
|
| 3827 |
|
| 3828 |
b := readFile(t, dir, "creat") |
| 3829 |
require.Equal(t, contents, b) |
| 3830 |
}, |
| 3831 |
expectedLog: ` |
| 3832 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=creat,oflags=CREAT,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 3833 |
<== (opened_fd=4,errno=ESUCCESS) |
| 3834 |
`, |
| 3835 |
}, |
| 3836 |
{ |
| 3837 |
name: "sysfs.ReadFS O_CREAT O_TRUNC", |
| 3838 |
fs: readFS, |
| 3839 |
oflags: wasip1.O_CREAT | wasip1.O_TRUNC, |
| 3840 |
expectedErrno: wasip1.ErrnoNosys, |
| 3841 |
path: func(t *testing.T) (file string) { return joinPath(dirName, "O_CREAT-O_TRUNC") }, |
| 3842 |
expectedLog: ` |
| 3843 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=dir/O_CREAT-O_TRUNC,oflags=CREAT|TRUNC,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 3844 |
<== (opened_fd=,errno=ENOSYS) |
| 3845 |
`, |
| 3846 |
}, |
| 3847 |
{ |
| 3848 |
name: "sysfs.DirFS O_CREAT O_TRUNC", |
| 3849 |
fs: writeFS, |
| 3850 |
path: func(t *testing.T) (file string) { return joinPath(dirName, "O_CREAT-O_TRUNC") }, |
| 3851 |
oflags: wasip1.O_CREAT | wasip1.O_TRUNC, |
| 3852 |
expected: func(t *testing.T, fsc *sys.FSContext) { |
| 3853 |
|
| 3854 |
contents := writeAndCloseFile(t, fsc, expectedOpenedFd) |
| 3855 |
|
| 3856 |
|
| 3857 |
b := readFile(t, dir, joinPath(dirName, "O_CREAT-O_TRUNC")) |
| 3858 |
require.Equal(t, contents, b) |
| 3859 |
}, |
| 3860 |
expectedLog: ` |
| 3861 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=dir/O_CREAT-O_TRUNC,oflags=CREAT|TRUNC,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 3862 |
<== (opened_fd=4,errno=ESUCCESS) |
| 3863 |
`, |
| 3864 |
}, |
| 3865 |
{ |
| 3866 |
name: "sysfs.ReadFS O_DIRECTORY", |
| 3867 |
fs: readFS, |
| 3868 |
oflags: wasip1.O_DIRECTORY, |
| 3869 |
path: func(*testing.T) string { return dirName }, |
| 3870 |
expected: func(t *testing.T, fsc *sys.FSContext) { |
| 3871 |
f, ok := fsc.LookupFile(expectedOpenedFd) |
| 3872 |
require.True(t, ok) |
| 3873 |
isDir, errno := f.File.IsDir() |
| 3874 |
require.EqualErrno(t, 0, errno) |
| 3875 |
require.True(t, isDir) |
| 3876 |
}, |
| 3877 |
expectedLog: ` |
| 3878 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=dir,oflags=DIRECTORY,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 3879 |
<== (opened_fd=4,errno=ESUCCESS) |
| 3880 |
`, |
| 3881 |
}, |
| 3882 |
{ |
| 3883 |
name: "sysfs.DirFS O_DIRECTORY", |
| 3884 |
fs: writeFS, |
| 3885 |
path: func(*testing.T) string { return dirName }, |
| 3886 |
oflags: wasip1.O_DIRECTORY, |
| 3887 |
expected: func(t *testing.T, fsc *sys.FSContext) { |
| 3888 |
f, ok := fsc.LookupFile(expectedOpenedFd) |
| 3889 |
require.True(t, ok) |
| 3890 |
isDir, errno := f.File.IsDir() |
| 3891 |
require.EqualErrno(t, 0, errno) |
| 3892 |
require.True(t, isDir) |
| 3893 |
}, |
| 3894 |
expectedLog: ` |
| 3895 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=dir,oflags=DIRECTORY,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 3896 |
<== (opened_fd=4,errno=ESUCCESS) |
| 3897 |
`, |
| 3898 |
}, |
| 3899 |
{ |
| 3900 |
name: "sysfs.ReadFS O_TRUNC", |
| 3901 |
fs: readFS, |
| 3902 |
oflags: wasip1.O_TRUNC, |
| 3903 |
expectedErrno: wasip1.ErrnoNosys, |
| 3904 |
path: func(*testing.T) string { return "trunc" }, |
| 3905 |
expectedLog: ` |
| 3906 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=trunc,oflags=TRUNC,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 3907 |
<== (opened_fd=,errno=ENOSYS) |
| 3908 |
`, |
| 3909 |
}, |
| 3910 |
{ |
| 3911 |
name: "sysfs.DirFS O_TRUNC", |
| 3912 |
fs: writeFS, |
| 3913 |
path: func(t *testing.T) (file string) { return "trunc" }, |
| 3914 |
oflags: wasip1.O_TRUNC, |
| 3915 |
expected: func(t *testing.T, fsc *sys.FSContext) { |
| 3916 |
contents := writeAndCloseFile(t, fsc, expectedOpenedFd) |
| 3917 |
|
| 3918 |
|
| 3919 |
b := readFile(t, dir, "trunc") |
| 3920 |
require.Equal(t, contents, b) |
| 3921 |
}, |
| 3922 |
expectedLog: ` |
| 3923 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=trunc,oflags=TRUNC,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 3924 |
<== (opened_fd=4,errno=ESUCCESS) |
| 3925 |
`, |
| 3926 |
}, |
| 3927 |
{ |
| 3928 |
name: "sysfs.DirFS RIGHT_FD_READ|RIGHT_FD_WRITE", |
| 3929 |
fs: writeFS, |
| 3930 |
path: func(*testing.T) string { return fileName }, |
| 3931 |
oflags: 0, |
| 3932 |
rights: wasip1.RIGHT_FD_READ | wasip1.RIGHT_FD_WRITE, |
| 3933 |
expected: func(t *testing.T, fsc *sys.FSContext) { |
| 3934 |
requireContents(t, fsc, expectedOpenedFd, fileName, fileContents) |
| 3935 |
}, |
| 3936 |
expectedLog: ` |
| 3937 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=file,oflags=,fs_rights_base=FD_READ|FD_WRITE,fs_rights_inheriting=,fdflags=) |
| 3938 |
<== (opened_fd=4,errno=ESUCCESS) |
| 3939 |
`, |
| 3940 |
}, |
| 3941 |
{ |
| 3942 |
name: "sysfs.DirFS file O_DIRECTORY RIGHTS_FD_WRITE", |
| 3943 |
fs: writeFS, |
| 3944 |
path: func(*testing.T) string { return fileName }, |
| 3945 |
oflags: wasip1.O_DIRECTORY, |
| 3946 |
rights: wasip1.RIGHT_FD_WRITE, |
| 3947 |
expectedErrno: wasip1.ErrnoNotdir, |
| 3948 |
expectedLog: ` |
| 3949 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=file,oflags=DIRECTORY,fs_rights_base=FD_WRITE,fs_rights_inheriting=,fdflags=) |
| 3950 |
<== (opened_fd=,errno=ENOTDIR) |
| 3951 |
`, |
| 3952 |
}, |
| 3953 |
{ |
| 3954 |
name: "sysfs.DirFS dir O_DIRECTORY RIGHTS_FD_WRITE", |
| 3955 |
fs: writeFS, |
| 3956 |
path: func(*testing.T) string { return dirName }, |
| 3957 |
oflags: wasip1.O_DIRECTORY, |
| 3958 |
rights: wasip1.RIGHT_FD_WRITE, |
| 3959 |
expectedErrno: wasip1.ErrnoIsdir, |
| 3960 |
expectedLog: ` |
| 3961 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=dir,oflags=DIRECTORY,fs_rights_base=FD_WRITE,fs_rights_inheriting=,fdflags=) |
| 3962 |
<== (opened_fd=,errno=EISDIR) |
| 3963 |
`, |
| 3964 |
}, |
| 3965 |
} |
| 3966 |
|
| 3967 |
for _, tt := range tests { |
| 3968 |
tc := tt |
| 3969 |
|
| 3970 |
t.Run(tc.name, func(t *testing.T) { |
| 3971 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig()) |
| 3972 |
defer r.Close(testCtx) |
| 3973 |
|
| 3974 |
mod.(*wasm.ModuleInstance).Sys = sys.DefaultContext(tc.fs) |
| 3975 |
|
| 3976 |
pathName := tc.path(t) |
| 3977 |
mod.Memory().Write(0, []byte(pathName)) |
| 3978 |
|
| 3979 |
path := uint32(0) |
| 3980 |
pathLen := uint32(len(pathName)) |
| 3981 |
resultOpenedFd := pathLen |
| 3982 |
fd := sys.FdPreopen |
| 3983 |
|
| 3984 |
|
| 3985 |
|
| 3986 |
dirflags := 0 |
| 3987 |
|
| 3988 |
|
| 3989 |
fsRightsInheriting := uint64(0) |
| 3990 |
|
| 3991 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.PathOpenName, uint64(fd), uint64(dirflags), uint64(path), |
| 3992 |
uint64(pathLen), uint64(tc.oflags), uint64(tc.rights), fsRightsInheriting, uint64(tc.fdflags), uint64(resultOpenedFd)) |
| 3993 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 3994 |
|
| 3995 |
if tc.expectedErrno == wasip1.ErrnoSuccess { |
| 3996 |
openedFd, ok := mod.Memory().ReadUint32Le(pathLen) |
| 3997 |
require.True(t, ok) |
| 3998 |
require.Equal(t, expectedOpenedFd, int32(openedFd)) |
| 3999 |
|
| 4000 |
tc.expected(t, mod.(*wasm.ModuleInstance).Sys.FS()) |
| 4001 |
} |
| 4002 |
}) |
| 4003 |
} |
| 4004 |
} |
| 4005 |
|
| 4006 |
func writeAndCloseFile(t *testing.T, fsc *sys.FSContext, fd int32) []byte { |
| 4007 |
contents := []byte("hello") |
| 4008 |
f, ok := fsc.LookupFile(fd) |
| 4009 |
require.True(t, ok) |
| 4010 |
_, errno := f.File.Write([]byte("hello")) |
| 4011 |
require.EqualErrno(t, 0, errno) |
| 4012 |
require.EqualErrno(t, 0, fsc.CloseFile(fd)) |
| 4013 |
return contents |
| 4014 |
} |
| 4015 |
|
| 4016 |
func requireOpenFD(t *testing.T, mod api.Module, path string) int32 { |
| 4017 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 4018 |
preopen := getPreopen(t, fsc) |
| 4019 |
|
| 4020 |
fd, errno := fsc.OpenFile(preopen, path, experimentalsys.O_RDONLY, 0) |
| 4021 |
require.EqualErrno(t, 0, errno) |
| 4022 |
return fd |
| 4023 |
} |
| 4024 |
|
| 4025 |
func requireContents(t *testing.T, fsc *sys.FSContext, expectedOpenedFd int32, fileName string, fileContents []byte) { |
| 4026 |
|
| 4027 |
f, ok := fsc.LookupFile(expectedOpenedFd) |
| 4028 |
require.True(t, ok) |
| 4029 |
require.Equal(t, fileName, f.Name) |
| 4030 |
|
| 4031 |
|
| 4032 |
buf := readAll(t, f.File) |
| 4033 |
require.Equal(t, fileContents, buf) |
| 4034 |
} |
| 4035 |
|
| 4036 |
func readAll(t *testing.T, f experimentalsys.File) []byte { |
| 4037 |
st, errno := f.Stat() |
| 4038 |
require.EqualErrno(t, 0, errno) |
| 4039 |
buf := make([]byte, st.Size) |
| 4040 |
_, errno = f.Read(buf) |
| 4041 |
require.EqualErrno(t, 0, errno) |
| 4042 |
return buf |
| 4043 |
} |
| 4044 |
|
| 4045 |
func mkdir(t *testing.T, tmpDir, dir string) { |
| 4046 |
err := os.Mkdir(joinPath(tmpDir, dir), 0o700) |
| 4047 |
require.NoError(t, err) |
| 4048 |
} |
| 4049 |
|
| 4050 |
func readFile(t *testing.T, tmpDir, file string) []byte { |
| 4051 |
contents, err := os.ReadFile(joinPath(tmpDir, file)) |
| 4052 |
require.NoError(t, err) |
| 4053 |
return contents |
| 4054 |
} |
| 4055 |
|
| 4056 |
func writeFile(t *testing.T, tmpDir, file string, contents []byte) { |
| 4057 |
err := os.WriteFile(joinPath(tmpDir, file), contents, 0o600) |
| 4058 |
require.NoError(t, err) |
| 4059 |
} |
| 4060 |
|
| 4061 |
func Test_pathOpen_Errors(t *testing.T) { |
| 4062 |
tmpDir := t.TempDir() |
| 4063 |
fsConfig := wazero.NewFSConfig().WithDirMount(tmpDir, "/") |
| 4064 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFSConfig(fsConfig)) |
| 4065 |
defer r.Close(testCtx) |
| 4066 |
|
| 4067 |
file := "file" |
| 4068 |
err := os.WriteFile(joinPath(tmpDir, file), []byte{}, 0o700) |
| 4069 |
require.NoError(t, err) |
| 4070 |
fileFD := requireOpenFD(t, mod, file) |
| 4071 |
|
| 4072 |
dir := "dir" |
| 4073 |
err = os.Mkdir(joinPath(tmpDir, dir), 0o700) |
| 4074 |
require.NoError(t, err) |
| 4075 |
|
| 4076 |
nested := "dir/nested" |
| 4077 |
err = os.Mkdir(joinPath(tmpDir, nested), 0o700) |
| 4078 |
require.NoError(t, err) |
| 4079 |
|
| 4080 |
nestedFile := "dir/nested/file" |
| 4081 |
err = os.WriteFile(joinPath(tmpDir, nestedFile), []byte{}, 0o700) |
| 4082 |
require.NoError(t, err) |
| 4083 |
|
| 4084 |
tests := []struct { |
| 4085 |
name, pathName string |
| 4086 |
fd int32 |
| 4087 |
path, pathLen, oflags, resultOpenedFd uint32 |
| 4088 |
expectedErrno wasip1.Errno |
| 4089 |
expectedLog string |
| 4090 |
}{ |
| 4091 |
{ |
| 4092 |
name: "unopened FD", |
| 4093 |
fd: 42, |
| 4094 |
expectedErrno: wasip1.ErrnoBadf, |
| 4095 |
expectedLog: ` |
| 4096 |
==> wasi_snapshot_preview1.path_open(fd=42,dirflags=,path=,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 4097 |
<== (opened_fd=,errno=EBADF) |
| 4098 |
`, |
| 4099 |
}, |
| 4100 |
{ |
| 4101 |
name: "Fd not a directory", |
| 4102 |
fd: fileFD, |
| 4103 |
pathName: file, |
| 4104 |
path: 0, |
| 4105 |
pathLen: uint32(len(file)), |
| 4106 |
expectedErrno: wasip1.ErrnoNotdir, |
| 4107 |
expectedLog: ` |
| 4108 |
==> wasi_snapshot_preview1.path_open(fd=4,dirflags=,path=file,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 4109 |
<== (opened_fd=,errno=ENOTDIR) |
| 4110 |
`, |
| 4111 |
}, |
| 4112 |
{ |
| 4113 |
name: "out-of-memory reading path", |
| 4114 |
fd: sys.FdPreopen, |
| 4115 |
path: mod.Memory().Size(), |
| 4116 |
pathLen: uint32(len(file)), |
| 4117 |
expectedErrno: wasip1.ErrnoFault, |
| 4118 |
expectedLog: ` |
| 4119 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=OOM(65536,4),oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 4120 |
<== (opened_fd=,errno=EFAULT) |
| 4121 |
`, |
| 4122 |
}, |
| 4123 |
{ |
| 4124 |
name: "empty path", |
| 4125 |
fd: sys.FdPreopen, |
| 4126 |
path: 0, |
| 4127 |
pathLen: 0, |
| 4128 |
expectedErrno: wasip1.ErrnoInval, |
| 4129 |
expectedLog: ` |
| 4130 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 4131 |
<== (opened_fd=,errno=EINVAL) |
| 4132 |
`, |
| 4133 |
}, |
| 4134 |
{ |
| 4135 |
name: "out-of-memory reading pathLen", |
| 4136 |
fd: sys.FdPreopen, |
| 4137 |
path: 0, |
| 4138 |
pathLen: mod.Memory().Size() + 1, |
| 4139 |
expectedErrno: wasip1.ErrnoFault, |
| 4140 |
expectedLog: ` |
| 4141 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=OOM(0,65537),oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 4142 |
<== (opened_fd=,errno=EFAULT) |
| 4143 |
`, |
| 4144 |
}, |
| 4145 |
{ |
| 4146 |
name: "no such file exists", |
| 4147 |
fd: sys.FdPreopen, |
| 4148 |
pathName: dir, |
| 4149 |
path: 0, |
| 4150 |
pathLen: uint32(len(dir)) - 1, |
| 4151 |
expectedErrno: wasip1.ErrnoNoent, |
| 4152 |
expectedLog: ` |
| 4153 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=di,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 4154 |
<== (opened_fd=,errno=ENOENT) |
| 4155 |
`, |
| 4156 |
}, |
| 4157 |
{ |
| 4158 |
name: "trailing slash on directory", |
| 4159 |
fd: sys.FdPreopen, |
| 4160 |
pathName: nested + "/", |
| 4161 |
path: 0, |
| 4162 |
pathLen: uint32(len(nested)) + 1, |
| 4163 |
expectedErrno: wasip1.ErrnoSuccess, |
| 4164 |
expectedLog: ` |
| 4165 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=dir/nested/,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 4166 |
<== (opened_fd=5,errno=ESUCCESS) |
| 4167 |
`, |
| 4168 |
}, |
| 4169 |
{ |
| 4170 |
name: "path under preopen", |
| 4171 |
fd: sys.FdPreopen, |
| 4172 |
pathName: "../" + file, |
| 4173 |
path: 0, |
| 4174 |
pathLen: uint32(len(file)) + 3, |
| 4175 |
expectedErrno: wasip1.ErrnoPerm, |
| 4176 |
expectedLog: ` |
| 4177 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=../file,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 4178 |
<== (opened_fd=,errno=EPERM) |
| 4179 |
`, |
| 4180 |
}, |
| 4181 |
{ |
| 4182 |
name: "rooted path", |
| 4183 |
fd: sys.FdPreopen, |
| 4184 |
pathName: "/" + file, |
| 4185 |
path: 0, |
| 4186 |
pathLen: uint32(len(file)) + 1, |
| 4187 |
expectedErrno: wasip1.ErrnoPerm, |
| 4188 |
expectedLog: ` |
| 4189 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=/file,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 4190 |
<== (opened_fd=,errno=EPERM) |
| 4191 |
`, |
| 4192 |
}, |
| 4193 |
{ |
| 4194 |
name: "trailing slash on file", |
| 4195 |
fd: sys.FdPreopen, |
| 4196 |
pathName: nestedFile + "/", |
| 4197 |
path: 0, |
| 4198 |
pathLen: uint32(len(nestedFile)) + 1, |
| 4199 |
expectedErrno: wasip1.ErrnoNotdir, |
| 4200 |
expectedLog: ` |
| 4201 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=dir/nested/file/,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 4202 |
<== (opened_fd=,errno=ENOTDIR) |
| 4203 |
`, |
| 4204 |
}, |
| 4205 |
{ |
| 4206 |
name: "out-of-memory writing resultOpenedFd", |
| 4207 |
fd: sys.FdPreopen, |
| 4208 |
pathName: dir, |
| 4209 |
path: 0, |
| 4210 |
pathLen: uint32(len(dir)), |
| 4211 |
resultOpenedFd: mod.Memory().Size(), |
| 4212 |
expectedErrno: wasip1.ErrnoFault, |
| 4213 |
expectedLog: ` |
| 4214 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=dir,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 4215 |
<== (opened_fd=,errno=EFAULT) |
| 4216 |
`, |
| 4217 |
}, |
| 4218 |
{ |
| 4219 |
name: "O_DIRECTORY, but not a directory", |
| 4220 |
oflags: uint32(wasip1.O_DIRECTORY), |
| 4221 |
fd: sys.FdPreopen, |
| 4222 |
pathName: file, |
| 4223 |
path: 0, |
| 4224 |
pathLen: uint32(len(file)), |
| 4225 |
expectedErrno: wasip1.ErrnoNotdir, |
| 4226 |
expectedLog: ` |
| 4227 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=file,oflags=DIRECTORY,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 4228 |
<== (opened_fd=,errno=ENOTDIR) |
| 4229 |
`, |
| 4230 |
}, |
| 4231 |
{ |
| 4232 |
name: "oflags=directory and create invalid", |
| 4233 |
oflags: uint32(wasip1.O_DIRECTORY | wasip1.O_CREAT), |
| 4234 |
fd: sys.FdPreopen, |
| 4235 |
pathName: file, |
| 4236 |
path: 0, |
| 4237 |
pathLen: uint32(len(file)), |
| 4238 |
expectedErrno: wasip1.ErrnoInval, |
| 4239 |
expectedLog: ` |
| 4240 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=file,oflags=CREAT|DIRECTORY,fs_rights_base=,fs_rights_inheriting=,fdflags=) |
| 4241 |
<== (opened_fd=,errno=EINVAL) |
| 4242 |
`, |
| 4243 |
}, |
| 4244 |
} |
| 4245 |
|
| 4246 |
for _, tt := range tests { |
| 4247 |
tc := tt |
| 4248 |
t.Run(tc.name, func(t *testing.T) { |
| 4249 |
defer log.Reset() |
| 4250 |
|
| 4251 |
mod.Memory().Write(tc.path, []byte(tc.pathName)) |
| 4252 |
|
| 4253 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.PathOpenName, uint64(tc.fd), uint64(0), uint64(tc.path), |
| 4254 |
uint64(tc.pathLen), uint64(tc.oflags), 0, 0, 0, uint64(tc.resultOpenedFd)) |
| 4255 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 4256 |
}) |
| 4257 |
} |
| 4258 |
} |
| 4259 |
|
| 4260 |
func Test_pathReadlink(t *testing.T) { |
| 4261 |
tmpDir := t.TempDir() |
| 4262 |
|
| 4263 |
dirName := "dir" |
| 4264 |
dirPath := joinPath(tmpDir, dirName) |
| 4265 |
mod, dirFD, log, r := requireOpenFile(t, tmpDir, dirName, nil, false) |
| 4266 |
defer r.Close(testCtx) |
| 4267 |
|
| 4268 |
subDirName := "sub-dir" |
| 4269 |
require.NoError(t, os.Mkdir(joinPath(dirPath, subDirName), 0o700)) |
| 4270 |
|
| 4271 |
mem := mod.Memory() |
| 4272 |
|
| 4273 |
originalFileName := "top-original-file" |
| 4274 |
destinationPath := uint32(0x77) |
| 4275 |
destinationPathName := "top-symlinked" |
| 4276 |
ok := mem.Write(destinationPath, []byte(destinationPathName)) |
| 4277 |
require.True(t, ok) |
| 4278 |
|
| 4279 |
originalSubDirFileName := joinPath(subDirName, "subdir-original-file") |
| 4280 |
destinationSubDirFileName := joinPath(subDirName, "subdir-symlinked") |
| 4281 |
destinationSubDirPathNamePtr := uint32(0xcc) |
| 4282 |
ok = mem.Write(destinationSubDirPathNamePtr, []byte(destinationSubDirFileName)) |
| 4283 |
require.True(t, ok) |
| 4284 |
|
| 4285 |
|
| 4286 |
originalRelativePath := joinPath(dirName, originalFileName) |
| 4287 |
err := os.WriteFile(joinPath(tmpDir, originalRelativePath), []byte{4, 3, 2, 1}, 0o700) |
| 4288 |
require.NoError(t, err) |
| 4289 |
err = os.Symlink(originalRelativePath, joinPath(dirPath, destinationPathName)) |
| 4290 |
require.NoError(t, err) |
| 4291 |
originalSubDirRelativePath := joinPath(dirName, originalSubDirFileName) |
| 4292 |
err = os.WriteFile(joinPath(tmpDir, originalSubDirRelativePath), []byte{1, 2, 3, 4}, 0o700) |
| 4293 |
require.NoError(t, err) |
| 4294 |
err = os.Symlink(originalSubDirRelativePath, joinPath(dirPath, destinationSubDirFileName)) |
| 4295 |
require.NoError(t, err) |
| 4296 |
|
| 4297 |
t.Run("ok", func(t *testing.T) { |
| 4298 |
for _, tc := range []struct { |
| 4299 |
name string |
| 4300 |
path, pathLen uint32 |
| 4301 |
expectedBuf string |
| 4302 |
}{ |
| 4303 |
{ |
| 4304 |
name: "top", |
| 4305 |
path: destinationPath, |
| 4306 |
pathLen: uint32(len(destinationPathName)), |
| 4307 |
expectedBuf: originalRelativePath, |
| 4308 |
}, |
| 4309 |
{ |
| 4310 |
name: "subdir", |
| 4311 |
path: destinationSubDirPathNamePtr, |
| 4312 |
pathLen: uint32(len(destinationSubDirFileName)), |
| 4313 |
expectedBuf: originalSubDirRelativePath, |
| 4314 |
}, |
| 4315 |
} { |
| 4316 |
t.Run(tc.name, func(t *testing.T) { |
| 4317 |
const buf, bufLen, resultBufused = 0x100, 0xff, 0x200 |
| 4318 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.PathReadlinkName, |
| 4319 |
uint64(dirFD), uint64(tc.path), uint64(tc.pathLen), |
| 4320 |
buf, bufLen, resultBufused) |
| 4321 |
require.Contains(t, log.String(), wasip1.ErrnoName(wasip1.ErrnoSuccess)) |
| 4322 |
|
| 4323 |
size, ok := mem.ReadUint32Le(resultBufused) |
| 4324 |
require.True(t, ok) |
| 4325 |
actual, ok := mem.Read(buf, size) |
| 4326 |
require.True(t, ok) |
| 4327 |
require.Equal(t, tc.expectedBuf, string(actual)) |
| 4328 |
}) |
| 4329 |
} |
| 4330 |
}) |
| 4331 |
|
| 4332 |
t.Run("errors", func(t *testing.T) { |
| 4333 |
for _, tc := range []struct { |
| 4334 |
name string |
| 4335 |
fd int32 |
| 4336 |
path, pathLen, buf, bufLen, resultBufused uint32 |
| 4337 |
expectedErrno wasip1.Errno |
| 4338 |
}{ |
| 4339 |
{expectedErrno: wasip1.ErrnoInval}, |
| 4340 |
{expectedErrno: wasip1.ErrnoInval, pathLen: 100}, |
| 4341 |
{expectedErrno: wasip1.ErrnoInval, bufLen: 100}, |
| 4342 |
{ |
| 4343 |
name: "bufLen too short", |
| 4344 |
expectedErrno: wasip1.ErrnoRange, |
| 4345 |
fd: dirFD, |
| 4346 |
bufLen: 10, |
| 4347 |
path: destinationPath, |
| 4348 |
pathLen: uint32(len(destinationPathName)), |
| 4349 |
buf: 0, |
| 4350 |
}, |
| 4351 |
{ |
| 4352 |
name: "path past memory", |
| 4353 |
expectedErrno: wasip1.ErrnoFault, |
| 4354 |
bufLen: 100, |
| 4355 |
pathLen: 100, |
| 4356 |
buf: 50, |
| 4357 |
path: math.MaxUint32, |
| 4358 |
}, |
| 4359 |
{expectedErrno: wasip1.ErrnoNotdir, bufLen: 100, pathLen: 100, buf: 50, path: 50, fd: 1}, |
| 4360 |
{expectedErrno: wasip1.ErrnoBadf, bufLen: 100, pathLen: 100, buf: 50, path: 50, fd: 1000}, |
| 4361 |
{ |
| 4362 |
expectedErrno: wasip1.ErrnoNoent, |
| 4363 |
bufLen: 100, buf: 50, |
| 4364 |
path: destinationPath, pathLen: uint32(len(destinationPathName)) - 1, |
| 4365 |
fd: dirFD, |
| 4366 |
}, |
| 4367 |
} { |
| 4368 |
name := tc.name |
| 4369 |
if name == "" { |
| 4370 |
name = wasip1.ErrnoName(tc.expectedErrno) |
| 4371 |
} |
| 4372 |
t.Run(name, func(t *testing.T) { |
| 4373 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.PathReadlinkName, |
| 4374 |
uint64(tc.fd), uint64(tc.path), uint64(tc.pathLen), uint64(tc.buf), |
| 4375 |
uint64(tc.bufLen), uint64(tc.resultBufused)) |
| 4376 |
require.Contains(t, log.String(), wasip1.ErrnoName(tc.expectedErrno)) |
| 4377 |
}) |
| 4378 |
} |
| 4379 |
}) |
| 4380 |
} |
| 4381 |
|
| 4382 |
func Test_pathRemoveDirectory(t *testing.T) { |
| 4383 |
tmpDir := t.TempDir() |
| 4384 |
fsConfig := wazero.NewFSConfig().WithDirMount(tmpDir, "/") |
| 4385 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFSConfig(fsConfig)) |
| 4386 |
defer r.Close(testCtx) |
| 4387 |
|
| 4388 |
|
| 4389 |
pathName := "wazero" |
| 4390 |
realPath := joinPath(tmpDir, pathName) |
| 4391 |
ok := mod.Memory().Write(0, append([]byte{'?'}, pathName...)) |
| 4392 |
require.True(t, ok) |
| 4393 |
|
| 4394 |
|
| 4395 |
err := os.Mkdir(realPath, 0o700) |
| 4396 |
require.NoError(t, err) |
| 4397 |
|
| 4398 |
fd := sys.FdPreopen |
| 4399 |
name := 1 |
| 4400 |
nameLen := len(pathName) |
| 4401 |
|
| 4402 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.PathRemoveDirectoryName, uint64(fd), uint64(name), uint64(nameLen)) |
| 4403 |
require.Equal(t, ` |
| 4404 |
==> wasi_snapshot_preview1.path_remove_directory(fd=3,path=wazero) |
| 4405 |
<== errno=ESUCCESS |
| 4406 |
`, "\n"+log.String()) |
| 4407 |
|
| 4408 |
|
| 4409 |
_, err = os.Stat(realPath) |
| 4410 |
require.Error(t, err) |
| 4411 |
} |
| 4412 |
|
| 4413 |
func Test_pathRemoveDirectory_Errors(t *testing.T) { |
| 4414 |
tmpDir := t.TempDir() |
| 4415 |
fsConfig := wazero.NewFSConfig().WithDirMount(tmpDir, "/") |
| 4416 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFSConfig(fsConfig)) |
| 4417 |
defer r.Close(testCtx) |
| 4418 |
|
| 4419 |
file := "file" |
| 4420 |
err := os.WriteFile(joinPath(tmpDir, file), []byte{}, 0o700) |
| 4421 |
require.NoError(t, err) |
| 4422 |
fileFD := requireOpenFD(t, mod, file) |
| 4423 |
|
| 4424 |
dirNotEmpty := "notempty" |
| 4425 |
dirNotEmptyPath := joinPath(tmpDir, dirNotEmpty) |
| 4426 |
err = os.Mkdir(dirNotEmptyPath, 0o700) |
| 4427 |
require.NoError(t, err) |
| 4428 |
|
| 4429 |
dir := "dir" |
| 4430 |
err = os.Mkdir(joinPath(dirNotEmptyPath, dir), 0o700) |
| 4431 |
require.NoError(t, err) |
| 4432 |
|
| 4433 |
tests := []struct { |
| 4434 |
name, pathName string |
| 4435 |
fd int32 |
| 4436 |
path, pathLen uint32 |
| 4437 |
expectedErrno wasip1.Errno |
| 4438 |
expectedLog string |
| 4439 |
}{ |
| 4440 |
{ |
| 4441 |
name: "unopened FD", |
| 4442 |
fd: 42, |
| 4443 |
expectedErrno: wasip1.ErrnoBadf, |
| 4444 |
expectedLog: ` |
| 4445 |
==> wasi_snapshot_preview1.path_remove_directory(fd=42,path=) |
| 4446 |
<== errno=EBADF |
| 4447 |
`, |
| 4448 |
}, |
| 4449 |
{ |
| 4450 |
name: "Fd not a directory", |
| 4451 |
fd: fileFD, |
| 4452 |
pathName: file, |
| 4453 |
path: 0, |
| 4454 |
pathLen: uint32(len(file)), |
| 4455 |
expectedErrno: wasip1.ErrnoNotdir, |
| 4456 |
expectedLog: ` |
| 4457 |
==> wasi_snapshot_preview1.path_remove_directory(fd=4,path=file) |
| 4458 |
<== errno=ENOTDIR |
| 4459 |
`, |
| 4460 |
}, |
| 4461 |
{ |
| 4462 |
name: "out-of-memory reading path", |
| 4463 |
fd: sys.FdPreopen, |
| 4464 |
path: mod.Memory().Size(), |
| 4465 |
pathLen: 1, |
| 4466 |
expectedErrno: wasip1.ErrnoFault, |
| 4467 |
expectedLog: ` |
| 4468 |
==> wasi_snapshot_preview1.path_remove_directory(fd=3,path=OOM(65536,1)) |
| 4469 |
<== errno=EFAULT |
| 4470 |
`, |
| 4471 |
}, |
| 4472 |
{ |
| 4473 |
name: "out-of-memory reading pathLen", |
| 4474 |
fd: sys.FdPreopen, |
| 4475 |
path: 0, |
| 4476 |
pathLen: mod.Memory().Size() + 1, |
| 4477 |
expectedErrno: wasip1.ErrnoFault, |
| 4478 |
expectedLog: ` |
| 4479 |
==> wasi_snapshot_preview1.path_remove_directory(fd=3,path=OOM(0,65537)) |
| 4480 |
<== errno=EFAULT |
| 4481 |
`, |
| 4482 |
}, |
| 4483 |
{ |
| 4484 |
name: "no such file exists", |
| 4485 |
fd: sys.FdPreopen, |
| 4486 |
pathName: file, |
| 4487 |
path: 0, |
| 4488 |
pathLen: uint32(len(file) - 1), |
| 4489 |
expectedErrno: wasip1.ErrnoNoent, |
| 4490 |
expectedLog: ` |
| 4491 |
==> wasi_snapshot_preview1.path_remove_directory(fd=3,path=fil) |
| 4492 |
<== errno=ENOENT |
| 4493 |
`, |
| 4494 |
}, |
| 4495 |
{ |
| 4496 |
name: "file not dir", |
| 4497 |
fd: sys.FdPreopen, |
| 4498 |
pathName: file, |
| 4499 |
path: 0, |
| 4500 |
pathLen: uint32(len(file)), |
| 4501 |
expectedErrno: wasip1.ErrnoNotdir, |
| 4502 |
expectedLog: fmt.Sprintf(` |
| 4503 |
==> wasi_snapshot_preview1.path_remove_directory(fd=3,path=file) |
| 4504 |
<== errno=%s |
| 4505 |
`, wasip1.ErrnoName(wasip1.ErrnoNotdir)), |
| 4506 |
}, |
| 4507 |
{ |
| 4508 |
name: "dir not empty", |
| 4509 |
fd: sys.FdPreopen, |
| 4510 |
pathName: dirNotEmpty, |
| 4511 |
path: 0, |
| 4512 |
pathLen: uint32(len(dirNotEmpty)), |
| 4513 |
expectedErrno: wasip1.ErrnoNotempty, |
| 4514 |
expectedLog: ` |
| 4515 |
==> wasi_snapshot_preview1.path_remove_directory(fd=3,path=notempty) |
| 4516 |
<== errno=ENOTEMPTY |
| 4517 |
`, |
| 4518 |
}, |
| 4519 |
} |
| 4520 |
|
| 4521 |
for _, tt := range tests { |
| 4522 |
tc := tt |
| 4523 |
t.Run(tc.name, func(t *testing.T) { |
| 4524 |
defer log.Reset() |
| 4525 |
|
| 4526 |
mod.Memory().Write(tc.path, []byte(tc.pathName)) |
| 4527 |
|
| 4528 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.PathRemoveDirectoryName, uint64(tc.fd), uint64(tc.path), uint64(tc.pathLen)) |
| 4529 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 4530 |
}) |
| 4531 |
} |
| 4532 |
} |
| 4533 |
|
| 4534 |
func Test_pathSymlink(t *testing.T) { |
| 4535 |
tmpDir := t.TempDir() |
| 4536 |
|
| 4537 |
dirName := "dir" |
| 4538 |
dirPath := joinPath(tmpDir, dirName) |
| 4539 |
mod, fd, log, r := requireOpenFile(t, tmpDir, dirName, nil, false) |
| 4540 |
defer r.Close(testCtx) |
| 4541 |
|
| 4542 |
mem := mod.Memory() |
| 4543 |
|
| 4544 |
fileName := "file" |
| 4545 |
err := os.WriteFile(joinPath(dirPath, fileName), []byte{1, 2, 3, 4}, 0o700) |
| 4546 |
require.NoError(t, err) |
| 4547 |
|
| 4548 |
file := uint32(0xff) |
| 4549 |
ok := mem.Write(file, []byte(fileName)) |
| 4550 |
require.True(t, ok) |
| 4551 |
|
| 4552 |
notFoundFile := uint32(0xaa) |
| 4553 |
notFoundFileName := "nope" |
| 4554 |
ok = mem.Write(notFoundFile, []byte(notFoundFileName)) |
| 4555 |
require.True(t, ok) |
| 4556 |
|
| 4557 |
link := uint32(0xcc) |
| 4558 |
linkName := fileName + "-link" |
| 4559 |
ok = mem.Write(link, []byte(linkName)) |
| 4560 |
require.True(t, ok) |
| 4561 |
|
| 4562 |
successCases := []struct { |
| 4563 |
name string |
| 4564 |
fd int32 |
| 4565 |
oldPath string |
| 4566 |
newPath string |
| 4567 |
}{ |
| 4568 |
{ |
| 4569 |
name: "dir", |
| 4570 |
fd: fd, |
| 4571 |
oldPath: fileName, |
| 4572 |
newPath: linkName, |
| 4573 |
}, |
| 4574 |
{ |
| 4575 |
name: "preopened root", |
| 4576 |
fd: sys.FdPreopen, |
| 4577 |
oldPath: fileName, |
| 4578 |
newPath: linkName, |
| 4579 |
}, |
| 4580 |
} |
| 4581 |
|
| 4582 |
for _, tt := range successCases { |
| 4583 |
tc := tt |
| 4584 |
|
| 4585 |
t.Run(tc.name, func(t *testing.T) { |
| 4586 |
file := uint32(0xbb) |
| 4587 |
ok := mem.Write(file, []byte(fileName)) |
| 4588 |
require.True(t, ok) |
| 4589 |
|
| 4590 |
link := uint32(0xdd) |
| 4591 |
ok = mem.Write(link, []byte(linkName)) |
| 4592 |
require.True(t, ok) |
| 4593 |
|
| 4594 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.PathSymlinkName, |
| 4595 |
uint64(file), uint64(len(tc.oldPath)), uint64(tc.fd), uint64(link), uint64(len(tc.newPath))) |
| 4596 |
require.Contains(t, log.String(), wasip1.ErrnoName(wasip1.ErrnoSuccess)) |
| 4597 |
st, err := os.Lstat(joinPath(dirPath, linkName)) |
| 4598 |
require.NoError(t, err) |
| 4599 |
require.Equal(t, st.Mode()&os.ModeSymlink, os.ModeSymlink) |
| 4600 |
}) |
| 4601 |
} |
| 4602 |
|
| 4603 |
t.Run("errors", func(t *testing.T) { |
| 4604 |
for _, tc := range []struct { |
| 4605 |
errno wasip1.Errno |
| 4606 |
oldPath, oldPathLen uint32 |
| 4607 |
fd int32 |
| 4608 |
newPath, newPathLen uint32 |
| 4609 |
}{ |
| 4610 |
{errno: wasip1.ErrnoBadf, fd: 1000}, |
| 4611 |
{errno: wasip1.ErrnoNotdir, fd: 2}, |
| 4612 |
|
| 4613 |
{errno: wasip1.ErrnoInval, fd: fd}, |
| 4614 |
{errno: wasip1.ErrnoInval, oldPathLen: 100, fd: fd}, |
| 4615 |
{errno: wasip1.ErrnoInval, newPathLen: 100, fd: fd}, |
| 4616 |
|
| 4617 |
{errno: wasip1.ErrnoFault, oldPath: math.MaxUint32, oldPathLen: 100, newPathLen: 100, fd: fd}, |
| 4618 |
{errno: wasip1.ErrnoFault, newPath: math.MaxUint32, oldPathLen: 100, newPathLen: 100, fd: fd}, |
| 4619 |
{errno: wasip1.ErrnoFault, oldPath: math.MaxUint32, newPath: math.MaxUint32, oldPathLen: 100, newPathLen: 100, fd: fd}, |
| 4620 |
|
| 4621 |
{ |
| 4622 |
errno: wasip1.ErrnoInval, oldPath: notFoundFile, oldPathLen: uint32(len(notFoundFileName)), |
| 4623 |
newPath: 0, newPathLen: 5, fd: fd, |
| 4624 |
}, |
| 4625 |
|
| 4626 |
{ |
| 4627 |
errno: wasip1.ErrnoExist, oldPath: file, oldPathLen: uint32(len(fileName)), |
| 4628 |
newPath: file, newPathLen: uint32(len(fileName)), fd: fd, |
| 4629 |
}, |
| 4630 |
} { |
| 4631 |
name := wasip1.ErrnoName(tc.errno) |
| 4632 |
t.Run(name, func(t *testing.T) { |
| 4633 |
requireErrnoResult(t, tc.errno, mod, wasip1.PathSymlinkName, |
| 4634 |
uint64(tc.oldPath), uint64(tc.oldPathLen), uint64(tc.fd), uint64(tc.newPath), uint64(tc.newPathLen)) |
| 4635 |
require.Contains(t, log.String(), name) |
| 4636 |
}) |
| 4637 |
} |
| 4638 |
}) |
| 4639 |
} |
| 4640 |
|
| 4641 |
func Test_pathRename(t *testing.T) { |
| 4642 |
tmpDir := t.TempDir() |
| 4643 |
fsConfig := wazero.NewFSConfig().WithDirMount(tmpDir, "/") |
| 4644 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFSConfig(fsConfig)) |
| 4645 |
defer r.Close(testCtx) |
| 4646 |
|
| 4647 |
|
| 4648 |
oldfd := sys.FdPreopen |
| 4649 |
oldPathName := "wazero" |
| 4650 |
realOldPath := joinPath(tmpDir, oldPathName) |
| 4651 |
oldPath := uint32(0) |
| 4652 |
oldPathLen := len(oldPathName) |
| 4653 |
ok := mod.Memory().Write(oldPath, []byte(oldPathName)) |
| 4654 |
require.True(t, ok) |
| 4655 |
|
| 4656 |
|
| 4657 |
err := os.WriteFile(realOldPath, []byte{}, 0o600) |
| 4658 |
require.NoError(t, err) |
| 4659 |
|
| 4660 |
newfd := sys.FdPreopen |
| 4661 |
newPathName := "wahzero" |
| 4662 |
realNewPath := joinPath(tmpDir, newPathName) |
| 4663 |
newPath := uint32(16) |
| 4664 |
newPathLen := len(newPathName) |
| 4665 |
ok = mod.Memory().Write(newPath, []byte(newPathName)) |
| 4666 |
require.True(t, ok) |
| 4667 |
|
| 4668 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.PathRenameName, |
| 4669 |
uint64(oldfd), uint64(oldPath), uint64(oldPathLen), |
| 4670 |
uint64(newfd), uint64(newPath), uint64(newPathLen)) |
| 4671 |
require.Equal(t, ` |
| 4672 |
==> wasi_snapshot_preview1.path_rename(fd=3,old_path=wazero,new_fd=3,new_path=wahzero) |
| 4673 |
<== errno=ESUCCESS |
| 4674 |
`, "\n"+log.String()) |
| 4675 |
|
| 4676 |
|
| 4677 |
_, err = os.Stat(realOldPath) |
| 4678 |
require.Error(t, err) |
| 4679 |
_, err = os.Stat(realNewPath) |
| 4680 |
require.NoError(t, err) |
| 4681 |
} |
| 4682 |
|
| 4683 |
func Test_pathRename_Errors(t *testing.T) { |
| 4684 |
tmpDir := t.TempDir() |
| 4685 |
fsConfig := wazero.NewFSConfig().WithDirMount(tmpDir, "/") |
| 4686 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFSConfig(fsConfig)) |
| 4687 |
defer r.Close(testCtx) |
| 4688 |
|
| 4689 |
file := "file" |
| 4690 |
err := os.WriteFile(joinPath(tmpDir, file), []byte{}, 0o700) |
| 4691 |
require.NoError(t, err) |
| 4692 |
|
| 4693 |
|
| 4694 |
|
| 4695 |
|
| 4696 |
file1 := "file1" |
| 4697 |
err = os.WriteFile(joinPath(tmpDir, file1), []byte{}, 0o700) |
| 4698 |
require.NoError(t, err) |
| 4699 |
fileFD := requireOpenFD(t, mod, file1) |
| 4700 |
|
| 4701 |
dirNotEmpty := "notempty" |
| 4702 |
err = os.Mkdir(joinPath(tmpDir, dirNotEmpty), 0o700) |
| 4703 |
require.NoError(t, err) |
| 4704 |
|
| 4705 |
dir := joinPath(dirNotEmpty, "dir") |
| 4706 |
err = os.Mkdir(joinPath(tmpDir, dir), 0o700) |
| 4707 |
require.NoError(t, err) |
| 4708 |
|
| 4709 |
tests := []struct { |
| 4710 |
name, oldPathName, newPathName string |
| 4711 |
oldFd int32 |
| 4712 |
oldPath, oldPathLen uint32 |
| 4713 |
newFd int32 |
| 4714 |
newPath, newPathLen uint32 |
| 4715 |
expectedErrno wasip1.Errno |
| 4716 |
expectedLog string |
| 4717 |
}{ |
| 4718 |
{ |
| 4719 |
name: "unopened old FD", |
| 4720 |
oldFd: 42, |
| 4721 |
newFd: sys.FdPreopen, |
| 4722 |
expectedErrno: wasip1.ErrnoBadf, |
| 4723 |
expectedLog: ` |
| 4724 |
==> wasi_snapshot_preview1.path_rename(fd=42,old_path=,new_fd=3,new_path=) |
| 4725 |
<== errno=EBADF |
| 4726 |
`, |
| 4727 |
}, |
| 4728 |
{ |
| 4729 |
name: "old FD not a directory", |
| 4730 |
oldFd: fileFD, |
| 4731 |
newFd: sys.FdPreopen, |
| 4732 |
expectedErrno: wasip1.ErrnoNotdir, |
| 4733 |
expectedLog: ` |
| 4734 |
==> wasi_snapshot_preview1.path_rename(fd=4,old_path=,new_fd=3,new_path=) |
| 4735 |
<== errno=ENOTDIR |
| 4736 |
`, |
| 4737 |
}, |
| 4738 |
{ |
| 4739 |
name: "unopened new FD", |
| 4740 |
oldFd: sys.FdPreopen, |
| 4741 |
newFd: 42, |
| 4742 |
expectedErrno: wasip1.ErrnoBadf, |
| 4743 |
expectedLog: ` |
| 4744 |
==> wasi_snapshot_preview1.path_rename(fd=3,old_path=,new_fd=42,new_path=) |
| 4745 |
<== errno=EBADF |
| 4746 |
`, |
| 4747 |
}, |
| 4748 |
{ |
| 4749 |
name: "new FD not a directory", |
| 4750 |
oldFd: sys.FdPreopen, |
| 4751 |
newFd: fileFD, |
| 4752 |
expectedErrno: wasip1.ErrnoNotdir, |
| 4753 |
expectedLog: ` |
| 4754 |
==> wasi_snapshot_preview1.path_rename(fd=3,old_path=,new_fd=4,new_path=) |
| 4755 |
<== errno=ENOTDIR |
| 4756 |
`, |
| 4757 |
}, |
| 4758 |
{ |
| 4759 |
name: "out-of-memory reading old path", |
| 4760 |
oldFd: sys.FdPreopen, |
| 4761 |
newFd: sys.FdPreopen, |
| 4762 |
oldPath: mod.Memory().Size(), |
| 4763 |
oldPathLen: 1, |
| 4764 |
expectedErrno: wasip1.ErrnoFault, |
| 4765 |
expectedLog: ` |
| 4766 |
==> wasi_snapshot_preview1.path_rename(fd=3,old_path=OOM(65536,1),new_fd=3,new_path=) |
| 4767 |
<== errno=EFAULT |
| 4768 |
`, |
| 4769 |
}, |
| 4770 |
{ |
| 4771 |
name: "out-of-memory reading new path", |
| 4772 |
oldFd: sys.FdPreopen, |
| 4773 |
newFd: sys.FdPreopen, |
| 4774 |
oldPath: 0, |
| 4775 |
oldPathName: "a", |
| 4776 |
oldPathLen: 1, |
| 4777 |
newPath: mod.Memory().Size(), |
| 4778 |
newPathLen: 1, |
| 4779 |
expectedErrno: wasip1.ErrnoFault, |
| 4780 |
expectedLog: ` |
| 4781 |
==> wasi_snapshot_preview1.path_rename(fd=3,old_path=a,new_fd=3,new_path=OOM(65536,1)) |
| 4782 |
<== errno=EFAULT |
| 4783 |
`, |
| 4784 |
}, |
| 4785 |
{ |
| 4786 |
name: "out-of-memory reading old pathLen", |
| 4787 |
oldFd: sys.FdPreopen, |
| 4788 |
newFd: sys.FdPreopen, |
| 4789 |
oldPath: 0, |
| 4790 |
oldPathLen: mod.Memory().Size() + 1, |
| 4791 |
expectedErrno: wasip1.ErrnoFault, |
| 4792 |
expectedLog: ` |
| 4793 |
==> wasi_snapshot_preview1.path_rename(fd=3,old_path=OOM(0,65537),new_fd=3,new_path=) |
| 4794 |
<== errno=EFAULT |
| 4795 |
`, |
| 4796 |
}, |
| 4797 |
{ |
| 4798 |
name: "out-of-memory reading new pathLen", |
| 4799 |
oldFd: sys.FdPreopen, |
| 4800 |
newFd: sys.FdPreopen, |
| 4801 |
oldPathName: file, |
| 4802 |
oldPathLen: uint32(len(file)), |
| 4803 |
newPath: 0, |
| 4804 |
newPathLen: mod.Memory().Size() + 1, |
| 4805 |
expectedErrno: wasip1.ErrnoFault, |
| 4806 |
expectedLog: ` |
| 4807 |
==> wasi_snapshot_preview1.path_rename(fd=3,old_path=file,new_fd=3,new_path=OOM(0,65537)) |
| 4808 |
<== errno=EFAULT |
| 4809 |
`, |
| 4810 |
}, |
| 4811 |
{ |
| 4812 |
name: "no such file exists", |
| 4813 |
oldFd: sys.FdPreopen, |
| 4814 |
newFd: sys.FdPreopen, |
| 4815 |
oldPathName: file, |
| 4816 |
oldPathLen: uint32(len(file)) - 1, |
| 4817 |
newPath: 16, |
| 4818 |
newPathName: file, |
| 4819 |
newPathLen: uint32(len(file)), |
| 4820 |
expectedErrno: wasip1.ErrnoNoent, |
| 4821 |
expectedLog: ` |
| 4822 |
==> wasi_snapshot_preview1.path_rename(fd=3,old_path=fil,new_fd=3,new_path=file) |
| 4823 |
<== errno=ENOENT |
| 4824 |
`, |
| 4825 |
}, |
| 4826 |
{ |
| 4827 |
name: "dir not file", |
| 4828 |
oldFd: sys.FdPreopen, |
| 4829 |
newFd: sys.FdPreopen, |
| 4830 |
oldPathName: file, |
| 4831 |
oldPathLen: uint32(len(file)), |
| 4832 |
newPath: 16, |
| 4833 |
newPathName: dir, |
| 4834 |
newPathLen: uint32(len(dir)), |
| 4835 |
expectedErrno: wasip1.ErrnoIsdir, |
| 4836 |
expectedLog: ` |
| 4837 |
==> wasi_snapshot_preview1.path_rename(fd=3,old_path=file,new_fd=3,new_path=notempty/dir) |
| 4838 |
<== errno=EISDIR |
| 4839 |
`, |
| 4840 |
}, |
| 4841 |
} |
| 4842 |
|
| 4843 |
for _, tt := range tests { |
| 4844 |
tc := tt |
| 4845 |
t.Run(tc.name, func(t *testing.T) { |
| 4846 |
defer log.Reset() |
| 4847 |
|
| 4848 |
mod.Memory().Write(tc.oldPath, []byte(tc.oldPathName)) |
| 4849 |
mod.Memory().Write(tc.newPath, []byte(tc.newPathName)) |
| 4850 |
|
| 4851 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.PathRenameName, |
| 4852 |
uint64(tc.oldFd), uint64(tc.oldPath), uint64(tc.oldPathLen), |
| 4853 |
uint64(tc.newFd), uint64(tc.newPath), uint64(tc.newPathLen)) |
| 4854 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 4855 |
}) |
| 4856 |
} |
| 4857 |
} |
| 4858 |
|
| 4859 |
func Test_pathUnlinkFile(t *testing.T) { |
| 4860 |
tmpDir := t.TempDir() |
| 4861 |
fsConfig := wazero.NewFSConfig().WithDirMount(tmpDir, "/") |
| 4862 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFSConfig(fsConfig)) |
| 4863 |
defer r.Close(testCtx) |
| 4864 |
|
| 4865 |
|
| 4866 |
pathName := "wazero" |
| 4867 |
realPath := joinPath(tmpDir, pathName) |
| 4868 |
ok := mod.Memory().Write(0, append([]byte{'?'}, pathName...)) |
| 4869 |
require.True(t, ok) |
| 4870 |
|
| 4871 |
|
| 4872 |
err := os.WriteFile(realPath, []byte{}, 0o600) |
| 4873 |
require.NoError(t, err) |
| 4874 |
|
| 4875 |
fd := sys.FdPreopen |
| 4876 |
name := 1 |
| 4877 |
nameLen := len(pathName) |
| 4878 |
|
| 4879 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.PathUnlinkFileName, uint64(fd), uint64(name), uint64(nameLen)) |
| 4880 |
require.Equal(t, ` |
| 4881 |
==> wasi_snapshot_preview1.path_unlink_file(fd=3,path=wazero) |
| 4882 |
<== errno=ESUCCESS |
| 4883 |
`, "\n"+log.String()) |
| 4884 |
|
| 4885 |
|
| 4886 |
_, err = os.Stat(realPath) |
| 4887 |
require.Error(t, err) |
| 4888 |
} |
| 4889 |
|
| 4890 |
func Test_pathUnlinkFile_Errors(t *testing.T) { |
| 4891 |
tmpDir := t.TempDir() |
| 4892 |
fsConfig := wazero.NewFSConfig().WithDirMount(tmpDir, "/") |
| 4893 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFSConfig(fsConfig)) |
| 4894 |
defer r.Close(testCtx) |
| 4895 |
|
| 4896 |
file := "file" |
| 4897 |
err := os.WriteFile(joinPath(tmpDir, file), []byte{}, 0o700) |
| 4898 |
require.NoError(t, err) |
| 4899 |
fileFD := requireOpenFD(t, mod, file) |
| 4900 |
|
| 4901 |
dir := "dir" |
| 4902 |
err = os.Mkdir(joinPath(tmpDir, dir), 0o700) |
| 4903 |
require.NoError(t, err) |
| 4904 |
|
| 4905 |
tests := []struct { |
| 4906 |
name, pathName string |
| 4907 |
fd int32 |
| 4908 |
path, pathLen uint32 |
| 4909 |
expectedErrno wasip1.Errno |
| 4910 |
expectedLog string |
| 4911 |
}{ |
| 4912 |
{ |
| 4913 |
name: "unopened FD", |
| 4914 |
fd: 42, |
| 4915 |
expectedErrno: wasip1.ErrnoBadf, |
| 4916 |
expectedLog: ` |
| 4917 |
==> wasi_snapshot_preview1.path_unlink_file(fd=42,path=) |
| 4918 |
<== errno=EBADF |
| 4919 |
`, |
| 4920 |
}, |
| 4921 |
{ |
| 4922 |
name: "Fd not a directory", |
| 4923 |
fd: fileFD, |
| 4924 |
expectedErrno: wasip1.ErrnoNotdir, |
| 4925 |
expectedLog: ` |
| 4926 |
==> wasi_snapshot_preview1.path_unlink_file(fd=4,path=) |
| 4927 |
<== errno=ENOTDIR |
| 4928 |
`, |
| 4929 |
}, |
| 4930 |
{ |
| 4931 |
name: "out-of-memory reading path", |
| 4932 |
fd: sys.FdPreopen, |
| 4933 |
path: mod.Memory().Size(), |
| 4934 |
pathLen: 1, |
| 4935 |
expectedErrno: wasip1.ErrnoFault, |
| 4936 |
expectedLog: ` |
| 4937 |
==> wasi_snapshot_preview1.path_unlink_file(fd=3,path=OOM(65536,1)) |
| 4938 |
<== errno=EFAULT |
| 4939 |
`, |
| 4940 |
}, |
| 4941 |
{ |
| 4942 |
name: "out-of-memory reading pathLen", |
| 4943 |
fd: sys.FdPreopen, |
| 4944 |
path: 0, |
| 4945 |
pathLen: mod.Memory().Size() + 1, |
| 4946 |
expectedErrno: wasip1.ErrnoFault, |
| 4947 |
expectedLog: ` |
| 4948 |
==> wasi_snapshot_preview1.path_unlink_file(fd=3,path=OOM(0,65537)) |
| 4949 |
<== errno=EFAULT |
| 4950 |
`, |
| 4951 |
}, |
| 4952 |
{ |
| 4953 |
name: "no such file exists", |
| 4954 |
fd: sys.FdPreopen, |
| 4955 |
pathName: file, |
| 4956 |
path: 0, |
| 4957 |
pathLen: uint32(len(file) - 1), |
| 4958 |
expectedErrno: wasip1.ErrnoNoent, |
| 4959 |
expectedLog: ` |
| 4960 |
==> wasi_snapshot_preview1.path_unlink_file(fd=3,path=fil) |
| 4961 |
<== errno=ENOENT |
| 4962 |
`, |
| 4963 |
}, |
| 4964 |
{ |
| 4965 |
name: "dir not file", |
| 4966 |
fd: sys.FdPreopen, |
| 4967 |
pathName: dir, |
| 4968 |
path: 0, |
| 4969 |
pathLen: uint32(len(dir)), |
| 4970 |
expectedErrno: wasip1.ErrnoIsdir, |
| 4971 |
expectedLog: ` |
| 4972 |
==> wasi_snapshot_preview1.path_unlink_file(fd=3,path=dir) |
| 4973 |
<== errno=EISDIR |
| 4974 |
`, |
| 4975 |
}, |
| 4976 |
} |
| 4977 |
|
| 4978 |
for _, tt := range tests { |
| 4979 |
tc := tt |
| 4980 |
t.Run(tc.name, func(t *testing.T) { |
| 4981 |
defer log.Reset() |
| 4982 |
|
| 4983 |
mod.Memory().Write(tc.path, []byte(tc.pathName)) |
| 4984 |
|
| 4985 |
requireErrnoResult(t, tc.expectedErrno, mod, wasip1.PathUnlinkFileName, uint64(tc.fd), uint64(tc.path), uint64(tc.pathLen)) |
| 4986 |
require.Equal(t, tc.expectedLog, "\n"+log.String()) |
| 4987 |
}) |
| 4988 |
} |
| 4989 |
} |
| 4990 |
|
| 4991 |
func requireOpenFile(t *testing.T, tmpDir string, pathName string, data []byte, readOnly bool) (api.Module, int32, *bytes.Buffer, api.Closer) { |
| 4992 |
oflags := experimentalsys.O_RDWR |
| 4993 |
if readOnly { |
| 4994 |
oflags = experimentalsys.O_RDONLY |
| 4995 |
} |
| 4996 |
|
| 4997 |
realPath := joinPath(tmpDir, pathName) |
| 4998 |
if data == nil { |
| 4999 |
oflags = experimentalsys.O_RDONLY |
| 5000 |
require.NoError(t, os.Mkdir(realPath, 0o700)) |
| 5001 |
} else { |
| 5002 |
require.NoError(t, os.WriteFile(realPath, data, 0o600)) |
| 5003 |
} |
| 5004 |
|
| 5005 |
fsConfig := wazero.NewFSConfig() |
| 5006 |
|
| 5007 |
if readOnly { |
| 5008 |
fsConfig = fsConfig.WithReadOnlyDirMount(tmpDir, "/") |
| 5009 |
} else { |
| 5010 |
fsConfig = fsConfig.WithDirMount(tmpDir, "preopen") |
| 5011 |
} |
| 5012 |
|
| 5013 |
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFSConfig(fsConfig)) |
| 5014 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 5015 |
preopen := getPreopen(t, fsc) |
| 5016 |
|
| 5017 |
fd, errno := fsc.OpenFile(preopen, pathName, oflags, 0) |
| 5018 |
require.EqualErrno(t, 0, errno) |
| 5019 |
|
| 5020 |
return mod, fd, log, r |
| 5021 |
} |
| 5022 |
|
| 5023 |
|
| 5024 |
func Test_fdReaddir_dotEntryHasARealInode(t *testing.T) { |
| 5025 |
root := t.TempDir() |
| 5026 |
mod, r, _ := requireProxyModule(t, wazero.NewModuleConfig(). |
| 5027 |
WithFSConfig(wazero.NewFSConfig().WithDirMount(root, "/")), |
| 5028 |
) |
| 5029 |
defer r.Close(testCtx) |
| 5030 |
|
| 5031 |
mem := mod.Memory() |
| 5032 |
|
| 5033 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 5034 |
preopen := getPreopen(t, fsc) |
| 5035 |
|
| 5036 |
readDirTarget := "dir" |
| 5037 |
mem.Write(0, []byte(readDirTarget)) |
| 5038 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.PathCreateDirectoryName, |
| 5039 |
uint64(sys.FdPreopen), uint64(0), uint64(len(readDirTarget))) |
| 5040 |
|
| 5041 |
|
| 5042 |
fd, errno := fsc.OpenFile(preopen, readDirTarget, experimentalsys.O_RDONLY, 0) |
| 5043 |
require.EqualErrno(t, 0, errno) |
| 5044 |
|
| 5045 |
|
| 5046 |
st, errno := preopen.Stat(readDirTarget) |
| 5047 |
require.EqualErrno(t, 0, errno) |
| 5048 |
dirents := []byte{1, 0, 0, 0, 0, 0, 0, 0} |
| 5049 |
dirents = append(dirents, u64.LeBytes(st.Ino)...) |
| 5050 |
dirents = append(dirents, 1, 0, 0, 0) |
| 5051 |
dirents = append(dirents, 3, 0, 0, 0) |
| 5052 |
dirents = append(dirents, '.') |
| 5053 |
|
| 5054 |
require.EqualErrno(t, 0, errno) |
| 5055 |
dirents = append(dirents, 2, 0, 0, 0, 0, 0, 0, 0) |
| 5056 |
|
| 5057 |
dirents = append(dirents, 0, 0, 0, 0, 0, 0, 0, 0) |
| 5058 |
dirents = append(dirents, 2, 0, 0, 0) |
| 5059 |
dirents = append(dirents, 3, 0, 0, 0) |
| 5060 |
dirents = append(dirents, '.', '.') |
| 5061 |
|
| 5062 |
|
| 5063 |
resultBufused := uint32(0) |
| 5064 |
buf := uint32(8) |
| 5065 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdReaddirName, |
| 5066 |
uint64(fd), uint64(buf), uint64(0x2000), 0, uint64(resultBufused)) |
| 5067 |
|
| 5068 |
used, _ := mem.ReadUint32Le(resultBufused) |
| 5069 |
|
| 5070 |
results, _ := mem.Read(buf, used) |
| 5071 |
require.Equal(t, dirents, results) |
| 5072 |
} |
| 5073 |
|
| 5074 |
|
| 5075 |
|
| 5076 |
|
| 5077 |
func Test_fdReaddir_opened_file_written(t *testing.T) { |
| 5078 |
tmpDir := t.TempDir() |
| 5079 |
mod, r, _ := requireProxyModule(t, wazero.NewModuleConfig(). |
| 5080 |
WithFSConfig(wazero.NewFSConfig().WithDirMount(tmpDir, "/")), |
| 5081 |
) |
| 5082 |
defer r.Close(testCtx) |
| 5083 |
|
| 5084 |
mem := mod.Memory() |
| 5085 |
|
| 5086 |
fsc := mod.(*wasm.ModuleInstance).Sys.FS() |
| 5087 |
preopen := getPreopen(t, fsc) |
| 5088 |
|
| 5089 |
dirName := "dir" |
| 5090 |
dirPath := joinPath(tmpDir, dirName) |
| 5091 |
mem.Write(0, []byte(dirName)) |
| 5092 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.PathCreateDirectoryName, |
| 5093 |
uint64(sys.FdPreopen), uint64(0), uint64(len(dirName))) |
| 5094 |
|
| 5095 |
|
| 5096 |
dirFD, errno := fsc.OpenFile(preopen, dirName, experimentalsys.O_RDONLY, 0) |
| 5097 |
require.EqualErrno(t, 0, errno) |
| 5098 |
|
| 5099 |
|
| 5100 |
f := openFile(t, joinPath(dirPath, "file"), experimentalsys.O_CREAT, 0) |
| 5101 |
defer f.Close() |
| 5102 |
|
| 5103 |
|
| 5104 |
st, errno := preopen.Stat(dirName) |
| 5105 |
require.EqualErrno(t, 0, errno) |
| 5106 |
dirents := []byte{1, 0, 0, 0, 0, 0, 0, 0} |
| 5107 |
dirents = append(dirents, u64.LeBytes(st.Ino)...) |
| 5108 |
dirents = append(dirents, 1, 0, 0, 0) |
| 5109 |
dirents = append(dirents, 3, 0, 0, 0) |
| 5110 |
dirents = append(dirents, '.') |
| 5111 |
|
| 5112 |
|
| 5113 |
st, errno = preopen.Stat(".") |
| 5114 |
require.EqualErrno(t, 0, errno) |
| 5115 |
dirents = append(dirents, 2, 0, 0, 0, 0, 0, 0, 0) |
| 5116 |
|
| 5117 |
dirents = append(dirents, 0, 0, 0, 0, 0, 0, 0, 0) |
| 5118 |
dirents = append(dirents, 2, 0, 0, 0) |
| 5119 |
dirents = append(dirents, 3, 0, 0, 0) |
| 5120 |
dirents = append(dirents, '.', '.') |
| 5121 |
|
| 5122 |
|
| 5123 |
st, errno = f.Stat() |
| 5124 |
require.EqualErrno(t, 0, errno) |
| 5125 |
dirents = append(dirents, 3, 0, 0, 0, 0, 0, 0, 0) |
| 5126 |
dirents = append(dirents, u64.LeBytes(st.Ino)...) |
| 5127 |
dirents = append(dirents, 4, 0, 0, 0) |
| 5128 |
dirents = append(dirents, 4, 0, 0, 0) |
| 5129 |
dirents = append(dirents, 'f', 'i', 'l', 'e') |
| 5130 |
|
| 5131 |
|
| 5132 |
resultBufused := uint32(0) |
| 5133 |
buf := uint32(8) |
| 5134 |
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdReaddirName, |
| 5135 |
uint64(dirFD), uint64(buf), uint64(0x2000), 0, uint64(resultBufused)) |
| 5136 |
|
| 5137 |
used, _ := mem.ReadUint32Le(resultBufused) |
| 5138 |
|
| 5139 |
results, _ := mem.Read(buf, used) |
| 5140 |
require.Equal(t, dirents, results) |
| 5141 |
} |
| 5142 |
|
| 5143 |
|
| 5144 |
|
| 5145 |
func joinPath(dirName, baseName string) string { |
| 5146 |
return path.Join(dirName, baseName) |
| 5147 |
} |
| 5148 |
|
| 5149 |
func openFile(t *testing.T, path string, flag experimentalsys.Oflag, perm fs.FileMode) experimentalsys.File { |
| 5150 |
f, errno := sysfs.OpenOSFile(path, flag, perm) |
| 5151 |
require.EqualErrno(t, 0, errno) |
| 5152 |
return f |
| 5153 |
} |