Skip to content
Merged
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
18 changes: 13 additions & 5 deletions system/lib/wasmfs/syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ static __wasi_errno_t writeAtOffset(OffsetHandling setOffset,
return __WASI_ERRNO_ISDIR;
}

// A file opened for reading only (O_RDONLY) cannot be written. POSIX write(2)
// returns EBADF when the file descriptor is not open for writing.
if ((lockedOpenFile.getFlags() & O_ACCMODE) == O_RDONLY) {
return __WASI_ERRNO_BADF;
}

auto lockedFile = file->locked();

if (setOffset == OffsetHandling::OpenFileState) {
Expand All @@ -128,8 +134,6 @@ static __wasi_errno_t writeAtOffset(OffsetHandling setOffset,
}
}

// TODO: Check open file access mode for write permissions.

size_t bytesWritten = 0;
for (size_t i = 0; i < iovs_len; i++) {
const uint8_t* buf = iovs[i].buf;
Expand Down Expand Up @@ -198,15 +202,19 @@ static __wasi_errno_t readAtOffset(OffsetHandling setOffset,
return __WASI_ERRNO_INVAL;
}

// TODO: Check open file access mode for read permissions.

auto file = lockedOpenFile.getFile()->dynCast<DataFile>();

// If file is nullptr, then the file was not a DataFile.
if (!file) {
return __WASI_ERRNO_ISDIR;
}

// A file opened for writing only (O_WRONLY) cannot be read. POSIX read(2)
// returns EBADF when the file descriptor is not open for reading.
if ((lockedOpenFile.getFlags() & O_ACCMODE) == O_WRONLY) {
return __WASI_ERRNO_BADF;
}

auto lockedFile = file->locked();

size_t bytesRead = 0;
Expand Down Expand Up @@ -568,7 +576,7 @@ int wasmfs_create_file(char* pathname, mode_t mode, backend_t backend) {
static_assert(std::is_same_v<decltype(doOpen(0, 0, 0, 0)), unsigned int>,
"unexpected conversion from result of doOpen to int");
return doOpen(
path::parseParent((char*)pathname), O_CREAT | O_EXCL, mode, backend);
path::parseParent(pathname), O_CREAT | O_EXCL | O_RDWR, mode, backend);
}

// TODO: Test this with non-AT_FDCWD values.
Expand Down