Skip to content

Add CWD handling to standalone WASI mode, and some related syscalls #24247

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jiixyj
Copy link

@jiixyj jiixyj commented May 2, 2025

This PR builds on top of #24246 and adds "proper" CWD handling in standalone WASI mode. It takes more code from PR #18285 (modified were needed).

Unlike wasi-libc, all of chdir, fchdir and getcwd are properly supported. This is done by maintaining a global directory fd in O_SEARCH mode and modifying the path resolution function __paths_resolve_path() from paths.{h,c} accordingly.

getcwd is implemented by walking from the current CWD fd "up" until a root, i.e. a preopened fd is reached, and comparing dirent inodes along the way.

To support this, the following syscalls are implemented:

  • F_GETFD, F_SETFD, F_GETFL of fcntl
  • fstat
  • chdir
  • fchdir
  • getcwd
  • getdents64 (note that this now accesses the tell member of the DIR* with some offsetof hackery in order to find out the current offset inside the directory)

The following tests work now in standalone WASM mode:

  • test_readdir: wastime and toywasm work
  • test_unistd_curdir: just toywasm works -- on wasmtime, openat(AT_FDCWD, "..") always seems to fail with a permission error, so the implementation strategy of getcwd does not work right now. Wasmer can do this, but fails because of a symlink issue.
  • test_unistd_cwd: just toywasm works

I had do to some minor adjustments to the tests.

@jiixyj jiixyj marked this pull request as draft May 2, 2025 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant