Skip to content

Initialize the working directory from wasi:cli/environment.initial-cwd on WASIp2/WASIp3 - #917

Open
sd2k wants to merge 3 commits into
WebAssembly:mainfrom
sd2k:initial-cwd
Open

sd2k wants to merge 3 commits into
WebAssembly:mainfrom
sd2k:initial-cwd

Conversation

@sd2k

@sd2k sd2k commented Nov 16, 2026 •

Copy link
Copy Markdown

On WASIp2 and WASIp3, the emulated working directory now starts at wasi:cli/environment.initial-cwd when the host returns an absolute path, and stays at / otherwise. WASIp1 is unchanged.

  • The host is asked lazily, the first time a path is resolved or getcwd is called (__wasilibc_ensure_cwd in chdir.c). The import is called outside the cwd lock and the result is installed under it, so a second thread that loses the race frees its copy. If copying the path fails, the lookup or getcwd fails with ENOMEM and a later call tries again.
  • __wasilibc_reset_preopens also resets the cwd, so a snapshot taken after a path lookup (for example by Wizer) picks up the runtime's initial cwd instead of the build-time one.
  • __wasilibc_find_relpath_alloc is no longer weak on WASIp2/WASIp3. Before, a program that never called chdir didn't link chdir.o and resolved relative paths against / regardless of the cwd. Since the cwd may not be / at startup, relative paths now always go through the cwd. Programs that resolve paths link about 750 bytes more code plus the initial-cwd import. Programs that don't resolve paths are unaffected.
  • An empty or relative initial cwd is ignored, so the cwd stays /. An absolute initial cwd that no preopen covers is still used: getcwd returns it and relative paths fail with ENOENT. Absolute paths and chdir work as before. Falling back to / instead would silently resolve relative paths against a different directory than the host asked for.
  • Tests use Wasmtime's -Scwd=... and cover:
    • a relative open with and without getcwd/chdir linked (the static build without them is the one that catches the weak-symbol issue);
    • chdir relative to the initial cwd;
    • reset;
    • an initial cwd outside every preopen;
    • the default case with no initial cwd.
  • Two small harness changes: clang-format.cmake lets one test source back two tests, and register_test gains a DIR option for extra preopens.

Fixes #915

Investigated by an AI agent (Claude) on my behalf.

sd2k and others added 2 commits November 16, 2026 18:14
On WASIp2 and WASIp3 the emulated working directory now starts at the
host's initial-cwd when that is an absolute path, and stays at / otherwise.
It's read lazily the first time a path is resolved or getcwd is called.

Because the cwd may no longer be / at startup, __wasilibc_find_relpath_alloc
is no longer weak on WASIp2/3, so relative paths honour the initial cwd even
in programs that never call chdir. WASIp1 is unchanged.

Fixes WebAssembly#915

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
__wasilibc_reset_preopens now also resets the cwd on WASIp2/3, so a
snapshot taken after a path lookup picks up the runtime's initial cwd
rather than the build-time one.

If copying the initial cwd fails, path lookups and getcwd now fail with
ENOMEM and a later call tries again, instead of exiting.

Also moves the declarations to a private header and adds tests for chdir
after the initial cwd, for reset, and for an initial cwd that no preopen
covers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sd2k
sd2k marked this pull request as ready for review November 16, 2026 19:50

@alexcrichton alexcrichton left a comment

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.

Thanks for the PR, and looking over this and a few things stand out to me:

  • This looks like it just ignores relative initial-cwd values -- why is that?
  • Could this use stddefer.h and defer <expr> to clean up resources, similar to how locks are managed everywhere else?
  • To the extent that find_relpath_alloc is size-wise important to avoid in some builds I'd like to uniformly apply that same logic across all builds.

The first two points here are relatively simple to resolve one way or another, but the latter highlights longstanding weaknesses in wasi-libc's handling of paths. It seems to me that ideally what would happen is that there'd be a fd somewhere open which is the cwd, probably lazily initialized. This fd would handle all relative lookups and might change with chdir as well. Maintaining this is quite difficult however because chdir("..") needs to work and no WASI API supports that.

In lieu of that, could the API be called and handled in the chdir-not-used path to avoid pulling in all that code? Basically keeping the split as-is but still getting things implemented on both halves

Address review feedback:

- Restore the weak __wasilibc_find_relpath_alloc on every target. When
  chdir isn't linked, __wasilibc_find_relpath and posix.c now prefix
  relative paths with the initial cwd themselves, so such programs don't
  pull in chdir.o. When the cwd is / paths resolve exactly as before.
- Resolve a relative initial cwd against /, so `.` and `` mean /.
- Use defer for cleanup.

The lazy initial cwd state moves from chdir.c to getcwd.c so that both
halves can use it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sd2k

sd2k commented Nov 16, 2026

Copy link
Copy Markdown
Author

Thanks for the review! I've pushed changes for all three points:

  • Relative initial-cwd: the WIT docs only say "interpreting . as shorthand for this" and don't rule out relative values. A relative value is now resolved against /, the root of the namespace wasi-libc emulates, so sub and ./sub mean /sub, . or an empty string mean /, and leading .. stops at the root as /.. does in POSIX.
  • defer: the cleanup now uses defer, for both the binding string and the cwd lock.
  • The find_relpath_alloc split: it's weak on every target again, as before. When chdir isn't linked, __wasilibc_find_relpath and posix.c now prefix relative paths with the lazily read initial cwd themselves. If the cwd is /, paths resolve exactly as before. The lazy cwd state moved to getcwd.c so both halves can share it, and __wasilibc_reset_preopens still resets it. For a program that opens a relative path without chdir, this costs about 820 bytes over main, against about 1.2 KB for the previous strong-symbol version, so not much difference, so if you'd rather have the simpler strong-symbol approach back, I'm happy to switch. Programs that don't resolve paths don't import initial-cwd either way.

A cwd fd would be the better long-term model, but as you say chdir("..") makes that hard, so this keeps the existing string-based emulation.

@alexcrichton alexcrichton left a comment

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.

Thanks! Reading over this some more, I'm going to have to apologize about going back-and-forth on this. The original intention of the split of find_relpath and find_relpath_alloc was file size for programs that don't call chdir, but that's more-or-less not applicable with an environment-defined initial cwd. Given that, and the complexity here in maintaining the split, I think it might actually best to just jettison the split entirely and only support the find_relpath_alloc interface. The old find_relpath would stick around since it's a public symbol, but we could purge all usage of it from wasi-libc itself.

Again sorry for the back-and-forth, but the current diff as-is of preserving the status quo has convinced me that this isn't the right way to approach this. Would you be up for sketching out the other direction?

Additionally, I think it's fine to drop the reset of cwd on __wasilibc_reset_preopens. The cwd already isn't reset as part of that operation and it's not clear how exactly it should interact with the rest of the system when a component is suspended or such. I think it'd be best to avoid proactively trying to design something here (unless you have a need for it) and instead wait for the use case to arise.

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.

Initialize the working directory from wasi:cli/environment.initial-cwd on wasip2/wasip3

2 participants