From fb482de82e1c0d6f0041689ea0dcafed96c95b61 Mon Sep 17 00:00:00 2001 From: jameshu15869 Date: Sun, 4 Jun 2023 10:39:51 -0400 Subject: [PATCH 1/8] allocate with type conversion error --- emcc.py | 1 + src/library_wasmfs.js | 6 ++++++ system/lib/wasmfs/js_api.cpp | 9 +++++++++ test/fs/test_fs_js_api.c | 38 ++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/emcc.py b/emcc.py index feeafe09cbf68..e55326ceca99a 100755 --- a/emcc.py +++ b/emcc.py @@ -2317,6 +2317,7 @@ def phase_linker_setup(options, state, newargs): settings.REQUIRED_EXPORTS += [ '_wasmfs_write_file', '_wasmfs_open', + '_wasmfs_allocate', '_wasmfs_close', '_wasmfs_mkdir', '_wasmfs_unlink', diff --git a/src/library_wasmfs.js b/src/library_wasmfs.js index 30b4ef98f8558..4bd3d17bfde3c 100644 --- a/src/library_wasmfs.js +++ b/src/library_wasmfs.js @@ -145,6 +145,12 @@ FS.createPreloadedFile = FS_createPreloadedFile; // TODO: read // TODO: write // TODO: allocate + allocate: (fd, offset, length) => { + if (length < 0) { + throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); + } + return FS.handleError(__wasmfs_allocate(fd, offset, length)); + }, // TODO: mmap // TODO: msync // TODO: munmap diff --git a/system/lib/wasmfs/js_api.cpp b/system/lib/wasmfs/js_api.cpp index b6fb0ce0f05fe..1824589c80b40 100644 --- a/system/lib/wasmfs/js_api.cpp +++ b/system/lib/wasmfs/js_api.cpp @@ -123,6 +123,15 @@ int _wasmfs_open(char* path, int flags, mode_t mode) { return err; } +int _wasmfs_allocate(int fd, long off, long len) { + printf("Vals: fd: %d, off: %ld, len: %ld\n", fd, off, len); + int err = __syscall_fallocate(fd, 0, off, len); + if (err == -1) { + return errno; + } + return err; +} + int _wasmfs_unlink(char* path) { return __syscall_unlinkat(AT_FDCWD, (intptr_t)path, 0); } diff --git a/test/fs/test_fs_js_api.c b/test/fs/test_fs_js_api.c index 5627a73d2a5a3..d9423e9007f00 100644 --- a/test/fs/test_fs_js_api.c +++ b/test/fs/test_fs_js_api.c @@ -1,5 +1,7 @@ #include #include +#include +#include int main() { /********** test FS.open() **********/ @@ -37,6 +39,40 @@ int main() { #endif ); + /********** test FS.allocate() **********/ + EM_ASM( + FS.writeFile("allocatetestfile", 'a=1\nb=2\n'); + ); + struct stat s; + stat("allocatetestfile", &s); + assert(s.st_size == 8); + + EM_ASM( + var stream = FS.open("allocatetestfile", "w"); + FS.allocate(stream, 8, 10); + ); + stat("allocatetestfile", &s); + assert(s.st_size == 18); + + EM_ASM( + var stream = FS.open("allocatetestfile", "w"); + FS.allocate(stream, 0, 4); + ); + stat("allocatetestfile", &s); + assert(s.st_size == 4); + + EM_ASM( + var stream = FS.open("allocatetestfile", "w"); + + var ex; + try { + FS.allocate(stream, 0, -1); + } catch (err) { + ex = err; + } + assert(ex.name === "ErrnoError" && ex.errno === 28 /* EINVAL */); + ); + /********** test FS.close() **********/ EM_ASM( FS.writeFile("closetestfile", 'a=1\nb=2\n'); @@ -59,5 +95,7 @@ int main() { assert(ex.name === "ErrnoError" && ex.errno === 8 /* EBADF */) ); + remove("allocatetestfile"); + puts("success"); } From 69c08068e25b4376e691c05ceff7df14ca6eb882 Mon Sep 17 00:00:00 2001 From: jameshu15869 Date: Wed, 7 Jun 2023 15:30:23 -0400 Subject: [PATCH 2/8] update to match open object --- src/library_wasmfs.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/library_wasmfs.js b/src/library_wasmfs.js index 8ba4816e4ad56..a5140776c543e 100644 --- a/src/library_wasmfs.js +++ b/src/library_wasmfs.js @@ -183,12 +183,11 @@ FS.createPreloadedFile = FS_createPreloadedFile; return bytesRead; }, - // TODO: allocate - allocate: (fd, offset, length) => { + allocate: (stream, offset, length) => { if (length < 0) { throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); } - return FS.handleError(__wasmfs_allocate(fd, offset, length)); + return FS.handleError(__wasmfs_allocate(stream.fd, offset, length)); }, // TODO: mmap // TODO: msync From 8747bd63ddfb03690defed41db39c4d5e623e567 Mon Sep 17 00:00:00 2001 From: jameshu15869 Date: Tue, 13 Jun 2023 11:55:08 -0400 Subject: [PATCH 3/8] use spliti64 --- src/library_wasmfs.js | 5 +- .../libc/musl/arch/emscripten/syscall_arch.h | 2 +- system/lib/wasmfs/js_api.cpp | 3 +- system/lib/wasmfs/syscalls.cpp | 2 +- test/fs/test_fs_js_api.c | 71 ++++++++++--------- test/test_core.py | 2 + 6 files changed, 43 insertions(+), 42 deletions(-) diff --git a/src/library_wasmfs.js b/src/library_wasmfs.js index 7ab933795f3f6..1c67a8ff08e26 100644 --- a/src/library_wasmfs.js +++ b/src/library_wasmfs.js @@ -190,10 +190,7 @@ FS.createPreloadedFile = FS_createPreloadedFile; return bytesRead; }, allocate: (stream, offset, length) => { - if (length < 0) { - throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); - } - return FS.handleError(__wasmfs_allocate(stream.fd, offset, length)); + return FS.handleError(__wasmfs_allocate(stream.fd, {{{ splitI64('offset') }}}, {{{ splitI64('length') }}})); }, // TODO: mmap // TODO: msync diff --git a/system/lib/libc/musl/arch/emscripten/syscall_arch.h b/system/lib/libc/musl/arch/emscripten/syscall_arch.h index bad86ad4e98df..89dcfeadc7188 100644 --- a/system/lib/libc/musl/arch/emscripten/syscall_arch.h +++ b/system/lib/libc/musl/arch/emscripten/syscall_arch.h @@ -96,7 +96,7 @@ int __syscall_fchmodat(int dirfd, intptr_t path, int mode, ...); int __syscall_faccessat(int dirfd, intptr_t path, int amode, int flags); int __syscall_pselect6(int nfds, intptr_t readfds, intptr_t writefds, intptr_t exceptfds, intptr_t timeout, intptr_t sigmaks); int __syscall_utimensat(int dirfd, intptr_t path, intptr_t times, int flags); -int __syscall_fallocate(int fd, int mode, uint64_t off, uint64_t len); +int __syscall_fallocate(int fd, int mode, int64_t off, int64_t len); int __syscall_dup3(int fd, int suggestfd, int flags); int __syscall_pipe2(intptr_t fds, int flags); int __syscall_recvmmsg(int sockfd, intptr_t msgvec, size_t vlen, int flags, ...); diff --git a/system/lib/wasmfs/js_api.cpp b/system/lib/wasmfs/js_api.cpp index 0dad71ce88546..00f23867a0721 100644 --- a/system/lib/wasmfs/js_api.cpp +++ b/system/lib/wasmfs/js_api.cpp @@ -133,8 +133,7 @@ int _wasmfs_open(char* path, int flags, mode_t mode) { return err; } -int _wasmfs_allocate(int fd, long off, long len) { - printf("Vals: fd: %d, off: %ld, len: %ld\n", fd, off, len); +int _wasmfs_allocate(int fd, uint64_t off, uint64_t len) { return __syscall_fallocate(fd, 0, off, len); } diff --git a/system/lib/wasmfs/syscalls.cpp b/system/lib/wasmfs/syscalls.cpp index 9eb1e0fb5c33d..0e581d7dabe93 100644 --- a/system/lib/wasmfs/syscalls.cpp +++ b/system/lib/wasmfs/syscalls.cpp @@ -1358,7 +1358,7 @@ int __syscall_poll(intptr_t fds_, int nfds, int timeout) { return nonzero; } -int __syscall_fallocate(int fd, int mode, uint64_t off, uint64_t len) { +int __syscall_fallocate(int fd, int mode, int64_t off, int64_t len) { assert(mode == 0); // TODO, but other modes were never supported in the old FS auto fileTable = wasmFS.getFileTable().locked(); diff --git a/test/fs/test_fs_js_api.c b/test/fs/test_fs_js_api.c index dc20cf6da1110..daad68fea7f83 100644 --- a/test/fs/test_fs_js_api.c +++ b/test/fs/test_fs_js_api.c @@ -4,6 +4,41 @@ #include #include +void test_fs_allocate() { + EM_ASM( + FS.writeFile("allocatetestfile", 'a=1\nb=2\n'); + ); + struct stat allocateStat; + stat("allocatetestfile", &allocateStat); + assert(allocateStat.st_size == 8); + + EM_ASM( + var stream = FS.open("allocatetestfile", "w"); + FS.allocate(stream, 8, 10); + ); + stat("allocatetestfile", &allocateStat); + assert(allocateStat.st_size == 18); + + EM_ASM( + var stream = FS.open("allocatetestfile", "w"); + FS.allocate(stream, 0, 4); + ); + stat("allocatetestfile", &allocateStat); + assert(allocateStat.st_size == 4); + + EM_ASM( + var stream = FS.open("allocatetestfile", "w"); + + var ex; + try { + FS.allocate(stream, 0, -1); + } catch (err) { + ex = err; + } + assert(ex.name === "ErrnoError" && ex.errno === 28 /* EINVAL */); + ); +} + int main() { /********** test FS.open() **********/ EM_ASM( @@ -110,40 +145,6 @@ int main() { } assert(ex.name === 'ErrnoError' && ex.errno == 8 /* EBADF */); ); - - /********** test FS.allocate() **********/ - EM_ASM( - FS.writeFile("allocatetestfile", 'a=1\nb=2\n'); - ); - struct stat allocateStat; - stat("allocatetestfile", &allocateStat); - assert(allocateStat.st_size == 8); - - EM_ASM( - var stream = FS.open("allocatetestfile", "w"); - FS.allocate(stream, 8, 10); - ); - stat("allocatetestfile", &allocateStat); - assert(allocateStat.st_size == 18); - - EM_ASM( - var stream = FS.open("allocatetestfile", "w"); - FS.allocate(stream, 0, 4); - ); - stat("allocatetestfile", &allocateStat); - assert(allocateStat.st_size == 4); - - EM_ASM( - var stream = FS.open("allocatetestfile", "w"); - - var ex; - try { - FS.allocate(stream, 0, -1); - } catch (err) { - ex = err; - } - assert(ex.name === "ErrnoError" && ex.errno === 28 /* EINVAL */); - ); /********** test FS.rmdir() **********/ EM_ASM( @@ -235,6 +236,8 @@ int main() { assert(S_ISREG(mknodStats.st_mode)); assert(mknodStats.st_mode & 0400); + test_fs_allocate(); + remove("mknodtest"); remove("createtest"); remove("testfile"); diff --git a/test/test_core.py b/test/test_core.py index 0b7a2be160584..29bfe9c862ffe 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -6017,6 +6017,8 @@ def test_fs_writeFile(self): def test_fs_js_api(self): self.set_setting("FORCE_FILESYSTEM") + self.emcc_args += ['--profiling'] + self.emcc_args += ['--profiling-funcs'] self.do_runf(test_file('fs/test_fs_js_api.c'), 'success') def test_fs_write(self): From 2d9b1e1812b73d420012a6d69bc62764a0340a26 Mon Sep 17 00:00:00 2001 From: jameshu15869 Date: Tue, 13 Jun 2023 11:56:27 -0400 Subject: [PATCH 4/8] clean up whitespace --- test/fs/test_fs_js_api.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/fs/test_fs_js_api.c b/test/fs/test_fs_js_api.c index daad68fea7f83..aef33323800e1 100644 --- a/test/fs/test_fs_js_api.c +++ b/test/fs/test_fs_js_api.c @@ -145,7 +145,7 @@ int main() { } assert(ex.name === 'ErrnoError' && ex.errno == 8 /* EBADF */); ); - + /********** test FS.rmdir() **********/ EM_ASM( // Create multiple directories @@ -159,7 +159,6 @@ int main() { stat("/dir2", &rmdirStat); assert(S_ISDIR(rmdirStat.st_mode)); - EM_ASM( // Remove the multiple directories FS.rmdir('/dir1'); From 693273fd7eb84735cc6950af671f7b3507fa41ea Mon Sep 17 00:00:00 2001 From: jameshu15869 Date: Tue, 13 Jun 2023 11:57:24 -0400 Subject: [PATCH 5/8] clean up test --- test/fs/test_fs_js_api.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/fs/test_fs_js_api.c b/test/fs/test_fs_js_api.c index aef33323800e1..f72e708e4e6c1 100644 --- a/test/fs/test_fs_js_api.c +++ b/test/fs/test_fs_js_api.c @@ -37,6 +37,8 @@ void test_fs_allocate() { } assert(ex.name === "ErrnoError" && ex.errno === 28 /* EINVAL */); ); + + remove("allocatetestfile"); } int main() { @@ -243,7 +245,6 @@ int main() { remove("renametestfile"); remove("readtestfile"); remove("closetestfile"); - remove("allocatetestfile"); puts("success"); } From 1f7431388bb4ee913dfa3d3d9ac5e4464d9340de Mon Sep 17 00:00:00 2001 From: jameshu15869 Date: Tue, 13 Jun 2023 12:01:34 -0400 Subject: [PATCH 6/8] remove profiling --- test/test_core.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/test_core.py b/test/test_core.py index 29bfe9c862ffe..0b7a2be160584 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -6017,8 +6017,6 @@ def test_fs_writeFile(self): def test_fs_js_api(self): self.set_setting("FORCE_FILESYSTEM") - self.emcc_args += ['--profiling'] - self.emcc_args += ['--profiling-funcs'] self.do_runf(test_file('fs/test_fs_js_api.c'), 'success') def test_fs_write(self): From 91339bcec933ea5f5aefeeb26487ed94a6701af0 Mon Sep 17 00:00:00 2001 From: jameshu15869 Date: Tue, 13 Jun 2023 12:05:48 -0400 Subject: [PATCH 7/8] fix types --- system/lib/wasmfs/js_api.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/lib/wasmfs/js_api.cpp b/system/lib/wasmfs/js_api.cpp index 00f23867a0721..11300415c5463 100644 --- a/system/lib/wasmfs/js_api.cpp +++ b/system/lib/wasmfs/js_api.cpp @@ -133,7 +133,7 @@ int _wasmfs_open(char* path, int flags, mode_t mode) { return err; } -int _wasmfs_allocate(int fd, uint64_t off, uint64_t len) { +int _wasmfs_allocate(int fd, int64_t off, int64_t len) { return __syscall_fallocate(fd, 0, off, len); } From 07e3bbba4c4519645ad2267c270bc4cddc8a8122 Mon Sep 17 00:00:00 2001 From: jameshu15869 Date: Tue, 13 Jun 2023 19:33:55 -0400 Subject: [PATCH 8/8] add comments --- test/fs/test_fs_js_api.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/fs/test_fs_js_api.c b/test/fs/test_fs_js_api.c index f72e708e4e6c1..ae5414b0b629b 100644 --- a/test/fs/test_fs_js_api.c +++ b/test/fs/test_fs_js_api.c @@ -13,6 +13,7 @@ void test_fs_allocate() { assert(allocateStat.st_size == 8); EM_ASM( + // Allocate more space at the very end. var stream = FS.open("allocatetestfile", "w"); FS.allocate(stream, 8, 10); ); @@ -20,6 +21,7 @@ void test_fs_allocate() { assert(allocateStat.st_size == 18); EM_ASM( + // Reduce allocated space at the very start. var stream = FS.open("allocatetestfile", "w"); FS.allocate(stream, 0, 4); ); @@ -31,6 +33,7 @@ void test_fs_allocate() { var ex; try { + // Attempt to allocate negative length. FS.allocate(stream, 0, -1); } catch (err) { ex = err;