Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fd_fdstat_set_flags truncating with O_TRUNC #1863

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions imports/wasi_snapshot_preview1/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,42 @@ func Test_fdFdstatGet_StdioNonblock(t *testing.T) {
}
}

func Test_fdFdstatSetFlagsWithTrunc(t *testing.T) {
tmpDir := t.TempDir()
fileName := "test"

mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().
WithFSConfig(wazero.NewFSConfig().WithDirMount(tmpDir, "/")))
defer r.Close(testCtx)

fsc := mod.(*wasm.ModuleInstance).Sys.FS()
preopen := fsc.RootFS()

fd, errno := fsc.OpenFile(preopen, fileName, experimentalsys.O_RDWR|experimentalsys.O_CREAT|experimentalsys.O_EXCL|experimentalsys.O_TRUNC, 0o600)
require.EqualErrno(t, 0, errno)

// Write the initial text to the file.
f, ok := fsc.LookupFile(fd)
require.True(t, ok)
n, _ := f.File.Write([]byte("abc"))
require.Equal(t, n, 3)

buf, err := os.ReadFile(joinPath(tmpDir, fileName))
require.NoError(t, err)
require.Equal(t, "abc", string(buf))

requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.FdFdstatSetFlagsName, uint64(fd), uint64(0))
require.Equal(t, `
==> wasi_snapshot_preview1.fd_fdstat_set_flags(fd=4,flags=)
<== errno=ESUCCESS
`, "\n"+log.String())
log.Reset()

buf, err = os.ReadFile(joinPath(tmpDir, fileName))
require.NoError(t, err)
require.Equal(t, "abc", string(buf))
}

func Test_fdFdstatSetFlags(t *testing.T) {
tmpDir := t.TempDir() // open before loop to ensure no locking problems.

Expand Down
4 changes: 2 additions & 2 deletions internal/sysfs/osfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (f *osFile) SetAppend(enable bool) (errno experimentalsys.Errno) {
f.flag &= ^experimentalsys.O_APPEND
}

// Clear any create flag, as we are re-opening, not re-creating.
f.flag &= ^experimentalsys.O_CREAT
// Clear any create or trunc flag, as we are re-opening, not re-creating.
f.flag &= ^(experimentalsys.O_CREAT | experimentalsys.O_TRUNC)

// appendMode (bool) cannot be changed later, so we have to re-open the
// file. https://github.com/golang/go/blob/go1.20/src/os/file_unix.go#L60
Expand Down
Loading