Repository navigation
Conversation
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>
alexcrichton
left a comment
There was a problem hiding this comment.
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.handdefer <expr>to clean up resources, similar to how locks are managed everywhere else? - To the extent that
find_relpath_allocis 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>
|
Thanks for the review! I've pushed changes for all three points:
A |
alexcrichton
left a comment
There was a problem hiding this comment.
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.
On WASIp2 and WASIp3, the emulated working directory now starts at
wasi:cli/environment.initial-cwdwhen the host returns an absolute path, and stays at/otherwise. WASIp1 is unchanged.getcwdis called (__wasilibc_ensure_cwdinchdir.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 orgetcwdfails withENOMEMand a later call tries again.__wasilibc_reset_preopensalso 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_allocis no longer weak on WASIp2/WASIp3. Before, a program that never calledchdirdidn't linkchdir.oand 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 theinitial-cwdimport. Programs that don't resolve paths are unaffected./. An absolute initial cwd that no preopen covers is still used:getcwdreturns it and relative paths fail withENOENT. Absolute paths andchdirwork as before. Falling back to/instead would silently resolve relative paths against a different directory than the host asked for.-Scwd=...and cover:getcwd/chdirlinked (the static build without them is the one that catches the weak-symbol issue);chdirrelative to the initial cwd;clang-format.cmakelets one test source back two tests, andregister_testgains aDIRoption for extra preopens.Fixes #915
Investigated by an AI agent (Claude) on my behalf.