Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions system/lib/wasmfs/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Backend {
virtual std::shared_ptr<Directory> createDirectory(mode_t mode) = 0;
virtual std::shared_ptr<Symlink> createSymlink(std::string target) = 0;

virtual void populateRoot(Directory::Handle& lockedRoot) {}

virtual ~Backend() = default;
};

Expand Down
4 changes: 4 additions & 0 deletions system/lib/wasmfs/backends/ignore_case_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ class IgnoreCaseBackend : public Backend {
std::shared_ptr<Symlink> createSymlink(std::string target) override {
return virtualize(backend->createSymlink(target), this);
}

void populateRoot(Directory::Handle& lockedRoot) override {
backend->populateRoot(lockedRoot);
}
};

// Create an ignore case backend by supplying another backend.
Expand Down
22 changes: 22 additions & 0 deletions system/lib/wasmfs/backends/memory_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "memory_backend.h"
#include "backend.h"
#include "special_files.h"
#include "wasmfs.h"

namespace wasmfs {
Expand Down Expand Up @@ -104,6 +105,27 @@ class MemoryBackend : public Backend {
std::shared_ptr<Symlink> createSymlink(std::string target) override {
return std::make_shared<MemorySymlink>(target, this);
}

// Initializes default directories including /dev/stdin, /dev/stdout,
// /dev/stderr. Refers to the same std streams in the open file table.
void populateRoot(Directory::Handle& lockedRoot) override {
// If the /dev/ directory does not already exist, create it. (It may already
// exist if those files have been preloaded.)
auto devDir = lockedRoot.insertDirectory("dev", S_IRUGO | S_IXUGO);
if (devDir) {
auto lockedDev = devDir->locked();
lockedDev.mountChild("null", SpecialFiles::getNull());
lockedDev.mountChild("stdin", SpecialFiles::getStdin());
lockedDev.mountChild("stdout", SpecialFiles::getStdout());
lockedDev.mountChild("stderr", SpecialFiles::getStderr());
lockedDev.mountChild("random", SpecialFiles::getRandom());
lockedDev.mountChild("urandom", SpecialFiles::getURandom());
}

// As with the /dev/ directory, it is not an error for /tmp/ to already
// exist.
lockedRoot.insertDirectory("tmp", S_IRWXUGO);
}
};

backend_t createMemoryBackend() {
Expand Down
18 changes: 2 additions & 16 deletions system/lib/wasmfs/wasmfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,8 @@ std::shared_ptr<Directory> WasmFS::initRootDirectory() {
// The root directory is its own parent.
lockedRoot.setParent(rootDirectory);

// If the /dev/ directory does not already exist, create it. (It may already
// exist in NODERAWFS mode, or if those files have been preloaded.)
auto devDir = lockedRoot.insertDirectory("dev", S_IRUGO | S_IXUGO);
if (devDir) {
auto lockedDev = devDir->locked();
lockedDev.mountChild("null", SpecialFiles::getNull());
lockedDev.mountChild("stdin", SpecialFiles::getStdin());
lockedDev.mountChild("stdout", SpecialFiles::getStdout());
lockedDev.mountChild("stderr", SpecialFiles::getStderr());
lockedDev.mountChild("random", SpecialFiles::getRandom());
lockedDev.mountChild("urandom", SpecialFiles::getURandom());
}

// As with the /dev/ directory, it is not an error for /tmp/ to already
// exist.
lockedRoot.insertDirectory("tmp", S_IRWXUGO);
// Set up the root directory.
rootBackend->populateRoot(lockedRoot);

return rootDirectory;
}
Expand Down
2 changes: 0 additions & 2 deletions system/lib/wasmfs/wasmfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class WasmFS {
std::mutex mutex;

// Private method to initialize root directory once.
// Initializes default directories including dev/stdin, dev/stdout,
// dev/stderr. Refers to the same std streams in the open file table.
std::shared_ptr<Directory> initRootDirectory();

// Initialize files specified by --preload-file option.
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_wasmfs.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 6725,
"a.out.js.gz": 3184,
"a.out.nodebug.wasm": 172740,
"a.out.nodebug.wasm.gz": 63346,
"total": 179465,
"total_gz": 66530,
"a.out.nodebug.wasm": 172782,
"a.out.nodebug.wasm.gz": 63311,
"total": 179507,
"total_gz": 66495,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
9 changes: 5 additions & 4 deletions test/codesize/test_codesize_files_wasmfs.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 5158,
"a.out.js.gz": 2458,
"a.out.nodebug.wasm": 58520,
"a.out.nodebug.wasm.gz": 18224,
"total": 63678,
"total_gz": 20682,
"a.out.nodebug.wasm": 58562,
"a.out.nodebug.wasm.gz": 18269,
"total": 63720,
"total_gz": 20727,
"sent": [
"a (emscripten_date_now)",
"b (emscripten_err)",
Expand Down Expand Up @@ -135,6 +135,7 @@
"$wasmfs::MemoryBackend::createDirectory\\28unsigned\\20int\\29",
"$wasmfs::MemoryBackend::createFile\\28unsigned\\20int\\29",
"$wasmfs::MemoryBackend::createSymlink\\28std::__2::basic_string<char\\2c\\20std::__2::char_traits<char>\\2c\\20std::__2::allocator<char>>\\29",
"$wasmfs::MemoryBackend::populateRoot\\28wasmfs::Directory::Handle&\\29",
"$wasmfs::MemoryDataFile::getSize\\28\\29",
"$wasmfs::MemoryDataFile::read\\28unsigned\\20char*\\2c\\20unsigned\\20long\\2c\\20long\\20long\\29",
"$wasmfs::MemoryDataFile::setSize\\28long\\20long\\29",
Expand Down
4 changes: 4 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -5609,6 +5609,8 @@ def test_fsync(self, args):
def test_fs_dev_random(self):
if WINDOWS and self.get_setting('NODERAWFS'):
self.skipTest('Crashes on Windows and NodeFS')
if self.get_setting('NODERAWFS') and self.get_setting('WASMFS'):
self.skipTest('https://github.com/emscripten-core/emscripten/issues/24830')
self.do_runf('fs/test_fs_dev_random.c', 'done\n')

@parameterized({
Expand Down Expand Up @@ -13480,6 +13482,8 @@ def test_unistd_chown(self):

@wasmfs_all_backends

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why remove this?

Does wasmfs_getdents.c need to get re-written to handle the rawfs mabye?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we just skip under NODERAWFS rather than removing wasmfs_all_backends completely?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I re-added the @wasmfs_all_backends decorator and skipped this only under NODERAWFS with commit f3b85c9.

See for details:

------------- Reading from /dev Directory via JS -------------
.
..
null
random
stderr
stdin
stdout
urandom

def test_wasmfs_getdents(self):
if self.get_setting('NODERAWFS'):
self.skipTest('test expectations assumes /dev is virtualized')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Are there no tests that can now be enabled by this change?

Seems a little odd that this change would strictly make more tests fail.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

PR #24733 (from which this change is split out) would allow more tests to be enabled (e.g. it removes the skip for other.test_fs_dev_random which is currently disabled in this PR).

The downside of splitting this change early is that absolute path access under -sNODERAWFS mode in WasmFS is still broken. So, opening /dev/random under -sNODERAWFS -sWASMFS after this PR lands will no longer work, since it's a absolute path that is no longer virtualized. Though, this only affects the -sNODERAWFS -sWASMFS combo, which is likely not widely used (and -sWASMFS is still marked experimental).

# Run only in WASMFS for now.
self.set_setting('FORCE_FILESYSTEM')
self.do_run_in_out_file_test('wasmfs/wasmfs_getdents.c')
Expand Down