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] Fix FS.readFile + WASM_BIGINT #17316

Merged
merged 10 commits into from
Jul 18, 2022
2 changes: 1 addition & 1 deletion src/library_wasmfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ mergeInto(LibraryManager.library, {

// Copy the file into a JS buffer on the heap.
var buf = __wasmfs_read_file(pathName);
// The integer length is returned in the first 8 bytes of the buffer.
// The integer length resides in the first 8 bytes of the buffer.
var length = {{{ makeGetValue('buf', '0', 'i64') }}};

// Default return type is binary.
Expand Down
12 changes: 7 additions & 5 deletions system/lib/wasmfs/js_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ __wasi_fd_t wasmfs_create_file(char* pathname, mode_t mode, backend_t backend);
// The buffer will also contain the file length.
// Caller must free the returned pointer.
void* _wasmfs_read_file(char* path) {
struct stat file;
static_assert(sizeof(off64_t) == 8, "File offset type must be 64-bit");
kripken marked this conversation as resolved.
Show resolved Hide resolved

struct stat64 file;
int err = 0;
err = stat(path, &file);
err = stat64(path, &file);
if (err < 0) {
emscripten_console_error("Fatal error in FS.readFile");
abort();
Expand All @@ -32,9 +34,9 @@ void* _wasmfs_read_file(char* path) {
// The function will return a pointer to a buffer with the file length in the
// first 8 bytes. The remaining bytes will contain the buffer contents. This
// allows the caller to use HEAPU8.subarray(buf + 8, buf + 8 + length).
off_t size = file.st_size;
uint8_t* result = (uint8_t*)malloc((size + sizeof(size)));
*(uint32_t*)result = size;
off64_t size = file.st_size;
uint8_t* result = (uint8_t*)malloc(size + sizeof(size));
*(off64_t*)result = size;

int fd = open(path, O_RDONLY);
if (fd < 0) {
Expand Down
6 changes: 6 additions & 0 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -11888,6 +11888,12 @@ def test_wasmfs_getdents(self):
def test_wasmfs_readfile(self):
self.do_run_in_out_file_test(test_file('wasmfs/wasmfs_readfile.c'))

@wasmfs_all_backends
def test_wasmfs_readfile_bigint(self):
self.set_setting('WASM_BIGINT')
kripken marked this conversation as resolved.
Show resolved Hide resolved
self.node_args += ['--experimental-wasm-bigint']
self.do_run_in_out_file_test(test_file('wasmfs/wasmfs_readfile.c'))

def test_wasmfs_jsfile(self):
self.set_setting('WASMFS')
self.do_run_in_out_file_test('wasmfs/wasmfs_jsfile.c')
Expand Down
8 changes: 6 additions & 2 deletions tests/wasmfs/wasmfs_readfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

Expand All @@ -25,10 +26,13 @@ int main() {
errno = 0;
write(fd, msg, strlen(msg));
assert(errno == 0);
err = close(fd);
assert(err == 0);

EM_ASM({
var output = FS.readFile("/root/test", {encoding : 'utf8'});
out(output);
var output = FS.readFile("/root/test");
out(UTF8ArrayToString(output, 0));
out("Length: " + output.byteLength);
});

EM_ASM({
Expand Down
1 change: 1 addition & 0 deletions tests/wasmfs/wasmfs_readfile.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Success

Length: 8
Fatal error in FS.readFile
Aborted(native code called abort())
Fatal error in FS.readFile
Expand Down