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

WasmFS JS API: Implement truncate #19543

Merged
merged 26 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2338,6 +2338,8 @@ def phase_linker_setup(options, state, newargs):
'_wasmfs_read',
'_wasmfs_pread',
'_wasmfs_symlink',
'__syscall_truncate64',
'__syscall_ftruncate64',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a reason to use the raw syscalls and not a _wasmfs_* wrapper? Wrappers have the benefit that changes to the syscalls don't affect them.

Copy link
Contributor Author

@jameshu15869 jameshu15869 Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thomas suggested a little earlier that since truncate doesn't have any additional processing, it might make sense to directly use the raw syscall. However, I do see your point that a wrapper would be beneficial. Would it be preferable to go back to calling the wrapper?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for now you should continue to use wrapper functions and we can separately/later consider weather they are actually needed since (IIUC) these are not the only once that are simple pass-through functions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't block landing anything on this conversation, but I still think pass-through wrappers aren't that useful. If we update the syscalls (which should be extremely rare), then we would still have to update their callers. Whether those callers are in C or JS doesn't make much of a difference as far as I can tell.

'_wasmfs_stat',
'_wasmfs_lstat',
'_wasmfs_chmod',
Expand Down
8 changes: 6 additions & 2 deletions src/library_wasmfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,12 @@ FS.createPreloadedFile = FS_createPreloadedFile;
// TDOO: chown
// TODO: lchown
// TODO: fchown
// TODO: truncate
// TODO: ftruncate
truncate: (path, len) => {
return FS.handleError(withStackSave(() => (___syscall_truncate64(stringToUTF8OnStack(path), {{{ splitI64('len') }}}))));
},
ftruncate: (fd, len) => {
return FS.handleError(___syscall_ftruncate64(fd, {{{ splitI64('len') }}}));
},
// TODO: utime
findObject: (path) => {
var result = __wasmfs_identify(path);
Expand Down
30 changes: 5 additions & 25 deletions system/lib/wasmfs/js_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,11 @@ int _wasmfs_mkdir(char* path, int mode) {
int _wasmfs_rmdir(char* path){ return __syscall_unlinkat(AT_FDCWD, (intptr_t)path, AT_REMOVEDIR); }

int _wasmfs_open(char* path, int flags, mode_t mode) {
int err = __syscall_openat(AT_FDCWD, (intptr_t)path, flags, mode);
if (err == -1) {
return -errno;
}
return err;
return __syscall_openat(AT_FDCWD, (intptr_t)path, flags, mode);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these bugfixes?

Copy link
Contributor Author

@jameshu15869 jameshu15869 Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We realized that this check isn't needed, since the syscalls do not actually set errno. The tests were passing before because err was never equal to -1, so the js_api.cpp function directly returned the error without using errno, giving the desired effect.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make a separate PR for this cleanup stuff?

}

int _wasmfs_mknod(char* path, mode_t mode, dev_t dev) {
int err = __syscall_mknodat(AT_FDCWD, (intptr_t)path, mode, dev);
if (err == -1) {
return errno;
}
return err;
return __syscall_mknodat(AT_FDCWD, (intptr_t)path, mode, dev);
}

int _wasmfs_unlink(char* path) {
Expand Down Expand Up @@ -190,11 +182,7 @@ int _wasmfs_lchmod(char* path, mode_t mode) {
}

int _wasmfs_rename(char* oldpath, char* newpath) {
int err = __syscall_renameat(AT_FDCWD, (intptr_t)oldpath, AT_FDCWD, (intptr_t)newpath);
if (err == -1) {
return errno;
}
return err;
return __syscall_renameat(AT_FDCWD, (intptr_t)oldpath, AT_FDCWD, (intptr_t)newpath);
};

int _wasmfs_read(int fd, void *buf, size_t count) {
Expand Down Expand Up @@ -228,19 +216,11 @@ int _wasmfs_close(int fd) {
}

int _wasmfs_stat(char* path, struct stat* statBuf) {
int err = __syscall_stat64((intptr_t)path, (intptr_t)statBuf);
if (err == -1) {
return errno;
}
return err;
return __syscall_stat64((intptr_t)path, (intptr_t)statBuf);
}

int _wasmfs_lstat(char* path, struct stat* statBuf) {
int err = __syscall_lstat64((intptr_t)path, (intptr_t)statBuf);
if (err == -1) {
return errno;
}
return err;
return __syscall_lstat64((intptr_t)path, (intptr_t)statBuf);
}

// Helper method that identifies what a path is:
Expand Down
66 changes: 66 additions & 0 deletions test/fs/test_fs_js_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,70 @@
#include <assert.h>
#include <fcntl.h>

void test_fs_truncate() {
EM_ASM(
FS.writeFile('truncatetest', 'a=1\nb=2\n');
);

struct stat s;
stat("truncatetest", &s);
assert(s.st_size == 8);

EM_ASM(
FS.truncate('truncatetest', 2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with all these changes it would be good to look for other tests that already use this API, if any? If we were previously disabling, or not running, those tests under WASMFS it would be good to enable them now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see anything explicitly calling FS.truncate() in the test dir, so I added them to test_fs_js_api for now. Are there any places besides the test directory that could potentially contain disabled tests?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it would only be in the test dir. I guess we had not tests for that API. Thanks for looking into it. Hopefully after we are done with this we will have much better test coverage for the JS API!

);
stat("truncatetest", &s);
assert(s.st_size == 2);

EM_ASM(
FS.truncate('truncatetest', 10);
);
stat("truncatetest", &s);
assert(s.st_size == 10);

EM_ASM(
var truncateStream = FS.open('truncatetest', 'w');
FS.ftruncate(truncateStream.fd, 4);
);
stat("truncatetest", &s);
assert(s.st_size == 4);

EM_ASM(
var ex;
try {
FS.truncate('truncatetest', -10);
} catch(err) {
ex = err;
}
assert(ex.name === "ErrnoError" && ex.errno === 28 /* EINVAL */);

try {
var truncateStream = FS.open('truncatetest', 'w');
FS.ftruncate(truncateStream.fd, -10);
} catch(err) {
ex = err;
}
assert(ex.name === "ErrnoError" && ex.errno === 28 /* EINVAL */);

try {
FS.truncate('nonexistent', 10);
} catch(err) {
ex = err;
}
assert(ex.name === "ErrnoError" && ex.errno === 44 /* ENOENT */);

var ex;
try {
FS.ftruncate(99, 10);
} catch(err) {
ex = err;
}
assert(ex.name === "ErrnoError" && ex.errno === 8 /* EBADF */);
);

remove("truncatetest");
}

int main() {
/********** test FS.open() **********/
EM_ASM(
Expand Down Expand Up @@ -201,6 +265,8 @@ int main() {
assert(S_ISREG(stats.st_mode));
assert(stats.st_mode & 0400);

test_fs_truncate();

remove("mknodtest");
remove("createtest");
remove("testfile");
Expand Down