Skip to content

Commit a6e22e4

Browse files
committed
[WasmFS] Fix absolute path access under NODERAWFS
Resolves: #24830.
1 parent 57df161 commit a6e22e4

File tree

6 files changed

+36
-13
lines changed

6 files changed

+36
-13
lines changed

system/lib/wasmfs/backend.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class Backend {
2121
virtual std::shared_ptr<Directory> createDirectory(mode_t mode) = 0;
2222
virtual std::shared_ptr<Symlink> createSymlink(std::string target) = 0;
2323

24+
virtual bool isVirtualized() { return true; }
25+
2426
virtual ~Backend() = default;
2527
};
2628

system/lib/wasmfs/backends/node_backend.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ class NodeDirectory : public Directory {
180180

181181
private:
182182
std::string getChildPath(const std::string& name) {
183+
if (state.path.empty()) {
184+
return name;
185+
}
183186
return state.path + '/' + name;
184187
}
185188

@@ -294,6 +297,8 @@ class NodeBackend : public Backend {
294297
std::shared_ptr<Symlink> createSymlink(std::string target) override {
295298
WASMFS_UNREACHABLE("TODO: implement NodeBackend::createSymlink");
296299
}
300+
301+
virtual bool isVirtualized() override { return false; }
297302
};
298303

299304
// TODO: symlink

system/lib/wasmfs/backends/noderawfs_root.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
#include "emscripten/wasmfs.h"
77

88
backend_t wasmfs_create_root_dir(void) {
9-
return wasmfs_create_node_backend(".");
9+
return wasmfs_create_node_backend("");
1010
}

system/lib/wasmfs/paths.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,21 @@ ParsedParent doParseParent(std::string_view path,
6666
size_t& recursions) {
6767
// Empty paths never exist.
6868
if (path.empty()) {
69-
return {-ENOENT};
69+
return -ENOENT;
70+
}
71+
72+
auto root = wasmFS.getRootDirectory();
73+
74+
// If the root backend is not virtualized, we assume there is only a single
75+
// backend, making parent traversal unnecessary. Simply return the current
76+
// directory along with the full path.
77+
if (!root->getBackend()->isVirtualized()) {
78+
return {std::make_pair(std::move(curr), path)};
7079
}
7180

7281
// Handle absolute paths.
7382
if (path.front() == '/') {
74-
curr = wasmFS.getRootDirectory();
83+
curr = root;
7584
path.remove_prefix(1);
7685
}
7786

@@ -84,7 +93,7 @@ ParsedParent doParseParent(std::string_view path,
8493
// contain a child segment for us to return. The root is its own parent, so we
8594
// can handle this by returning (root, ".").
8695
if (path.empty()) {
87-
return {std::make_pair(std::move(curr), std::string_view("."))};
96+
return {std::make_pair(std::move(curr), ".")};
8897
}
8998

9099
while (true) {

test/test_core.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6151,9 +6151,12 @@ def test_unistd_unlink(self):
61516151

61526152
# Several differences/bugs on non-linux including https://github.com/nodejs/node/issues/18014
61536153
# TODO: NODERAWFS in WasmFS
6154-
if '-DNODERAWFS' in self.cflags and os.geteuid() == 0:
6154+
if '-DNODERAWFS' in self.cflags:
61556155
# 0 if root user
6156-
self.cflags += ['-DSKIP_ACCESS_TESTS']
6156+
if os.geteuid() == 0:
6157+
self.cflags += ['-DSKIP_ACCESS_TESTS']
6158+
if self.get_setting('WASMFS'):
6159+
self.skipTest('https://github.com/emscripten-core/emscripten/issues/18112')
61576160

61586161
self.do_runf('unistd/unlink.c', 'success')
61596162

test/test_other.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9291,12 +9291,19 @@ def test_noderawfs_disables_embedding(self):
92919291
self.assert_fail(base + ['--preload-file', 'somefile'], expected)
92929292
self.assert_fail(base + ['--embed-file', 'somefile'], expected)
92939293

9294+
@crossplatform
9295+
@also_with_wasmfs
92949296
def test_noderawfs_access_abspath(self):
92959297
create_file('foo', 'bar')
92969298
create_file('access.c', r'''
9299+
#include <stdio.h>
9300+
#include <assert.h>
92979301
#include <unistd.h>
92989302
int main(int argc, char** argv) {
9299-
return access(argv[1], F_OK);
9303+
printf("testing access to %s\n", argv[1]);
9304+
int rtn = access(argv[1], F_OK);
9305+
assert(rtn == 0);
9306+
return 0;
93009307
}
93019308
''')
93029309
self.do_runf('access.c', cflags=['-sNODERAWFS'], args=[os.path.abspath('foo')])
@@ -13254,11 +13261,10 @@ def test_unistd_chown(self):
1325413261
self.set_setting('WASMFS')
1325513262
self.do_run_in_out_file_test('wasmfs/wasmfs_chown.c')
1325613263

13257-
@wasmfs_all_backends
1325813264
def test_wasmfs_getdents(self):
1325913265
# Run only in WASMFS for now.
1326013266
self.set_setting('FORCE_FILESYSTEM')
13261-
self.do_run_in_out_file_test('wasmfs/wasmfs_getdents.c')
13267+
self.do_run_in_out_file_test('wasmfs/wasmfs_getdents.c', cflags=['-sWASMFS'])
1326213268

1326313269
def test_wasmfs_jsfile(self):
1326413270
self.set_setting('WASMFS')
@@ -13844,15 +13850,13 @@ def test_fs_icase(self):
1384413850
@crossplatform
1384513851
@with_all_fs
1384613852
def test_std_filesystem(self):
13847-
if self.get_setting('NODERAWFS') and self.get_setting('WASMFS'):
13848-
self.skipTest('https://github.com/emscripten-core/emscripten/issues/24830')
13853+
if (WINDOWS or MACOS) and self.get_setting('NODERAWFS') and self.get_setting('WASMFS'):
13854+
self.skipTest('fails with ENOTEMPTY (Directory not empty) during fs::remove_all')
1384913855
self.do_other_test('test_std_filesystem.cpp')
1385013856

1385113857
@crossplatform
1385213858
@with_all_fs
1385313859
def test_std_filesystem_tempdir(self):
13854-
if self.get_setting('NODERAWFS') and self.get_setting('WASMFS'):
13855-
self.skipTest('https://github.com/emscripten-core/emscripten/issues/24830')
1385613860
self.do_other_test('test_std_filesystem_tempdir.cpp', cflags=['-g'])
1385713861

1385813862
def test_strict_js_closure(self):

0 commit comments

Comments
 (0)