Skip to content

Commit 48459c6

Browse files
authored
[WasmFS][NFC] Assert that error codes are negative (#17786)
To catch mistakes in backend implementations, assert where possible that error codes are negative.
1 parent 43e3009 commit 48459c6

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

system/lib/wasmfs/file_table.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ int FileTable::Handle::setEntry(__wasi_fd_t fd,
4141
auto file = fileTable.entries[fd]->locked().getFile();
4242
if (auto f = file->dynCast<DataFile>()) {
4343
ret = f->locked().close();
44+
assert(ret <= 0);
4445
}
4546
}
4647
fileTable.entries[fd] = openFile;

system/lib/wasmfs/syscalls.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,10 @@ __wasi_errno_t __wasi_fd_sync(__wasi_fd_t fd) {
297297
// way. TODO: in the future we may want syncing of directories.
298298
auto dataFile = openFile->locked().getFile()->dynCast<DataFile>();
299299
if (dataFile) {
300+
auto ret = dataFile->locked().flush();
301+
assert(ret <= 0);
300302
// Translate to WASI standard of positive return codes.
301-
return -dataFile->locked().flush();
303+
return -ret;
302304
}
303305

304306
return __WASI_ERRNO_SUCCESS;
@@ -467,6 +469,7 @@ static __wasi_fd_t doOpen(path::ParsedParent parsed,
467469

468470
std::shared_ptr<OpenFileState> openFile;
469471
if (auto err = OpenFileState::create(created, flags, openFile)) {
472+
assert(err < 0);
470473
return err;
471474
}
472475
return wasmFS.getFileTable().locked().addEntry(openFile);
@@ -517,6 +520,7 @@ static __wasi_fd_t doOpen(path::ParsedParent parsed,
517520
// truncate opened files more efficiently (e.g. OPFS).
518521
std::shared_ptr<OpenFileState> openFile;
519522
if (auto err = OpenFileState::create(child, flags, openFile)) {
523+
assert(err < 0);
520524
return err;
521525
}
522526

@@ -1011,7 +1015,11 @@ int __syscall_renameat(int olddirfd,
10111015
}
10121016

10131017
// Perform the move.
1014-
return lockedNewParent.insertMove(newFileName, oldFile);
1018+
if (auto err = lockedNewParent.insertMove(newFileName, oldFile)) {
1019+
assert(err < 0);
1020+
return err;
1021+
}
1022+
return 0;
10151023
}
10161024

10171025
int __syscall_rename(intptr_t oldpath, intptr_t newpath) {
@@ -1204,7 +1212,9 @@ static int doTruncate(std::shared_ptr<File>& file, off_t size) {
12041212
return -EINVAL;
12051213
}
12061214

1207-
return locked.setSize(size);
1215+
int ret = locked.setSize(size);
1216+
assert(ret <= 0);
1217+
return ret;
12081218
}
12091219

12101220
int __syscall_truncate64(intptr_t path, uint64_t size) {
@@ -1373,6 +1383,7 @@ int __syscall_fallocate(int fd, int mode, uint64_t off, uint64_t len) {
13731383
}
13741384
if (newNeededSize > size) {
13751385
if (auto err = locked.setSize(newNeededSize)) {
1386+
assert(err < 0);
13761387
return err;
13771388
}
13781389
}

0 commit comments

Comments
 (0)