epitaphial opened issue #15327:
wasi-preview1-component-adapter: fd_renumber can associate a cached directory stream with the wrong descriptor
Summary
fd_renumberreplaces a descriptor without transferring or invalidating the globaldirent_cache. A subsequentfd_readdiron the target FD can therefore resume the old directory's stream and return entries from the wrong directory.Minimal reproducer
#include <fcntl.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #include <wasi/api.h> static void make_dir(const char *path, const char *name) { mkdir(path, 0755); char child[64]; snprintf(child, sizeof(child), "%s/%s", path, name); close(open(child, O_CREAT | O_WRONLY, 0644)); } int main(void) { unsigned char small[23], large[4096]; __wasi_size_t used1 = 0, used2 = 0; make_dir("a", "from-a.txt"); make_dir("b", "from-b.txt"); int a = open("a", O_RDONLY | O_DIRECTORY); int b = open("b", O_RDONLY | O_DIRECTORY); __wasi_fd_readdir(a, small, sizeof(small), 0, &used1); __wasi_errno_t moved = __wasi_fd_renumber(b, a); __wasi_fd_readdir(a, large, sizeof(large), 0, &used2); printf("renumber=%u used=%u\n", moved, (unsigned)used2); for (size_t off = 0; off + sizeof(__wasi_dirent_t) <= used2;) { __wasi_dirent_t entry; memcpy(&entry, large + off, sizeof(entry)); if (off + sizeof(entry) + entry.d_namlen > used2) break; printf("entry: %.*s\n", (int)entry.d_namlen, large + off + sizeof(entry)); off += sizeof(entry) + entry.d_namlen; } return 0; }Run with the standard P1-to-component adaptation commands:
/opt/wasi-sdk/bin/clang --target=wasm32-wasip1 -O2 repro.c -o repro.wasm wasm-tools component new repro.wasm --adapt wasi_snapshot_preview1=wasi_snapshot_preview1.command.wasm -o repro.component.wasm wasmtime run --dir . repro.wasm wasmtime run --dir . repro.component.wasmExpected vs. actual output
Direct P1:
entries: . .. from-b.txtAdapter:
entries: . .. . from-a.txtThe behavior was stable in 5/5 fresh-directory executions on each of v46 Release, v46 Debug, v47.0.2, and v50.0.1.
Root cause
In the v46 baseline,
lib.rs:1578-1580directly delegatesfd_renumbertoDescriptors::renumber;descriptors.rs:356-384replaces the descriptor without updatingdirent_cache. The cache is subsequently selected using onlyfor_fdandcookieatlib.rs:1350-1356. The same delegation remains at v50.0.1lib.rs:1621-1623.This is distinct from the truncated-entry ordering issue in #15297: here descriptor identity changes, and the observed stream belongs to a different directory.
Suggested fix and causal validation
Drop the cached directory stream whenever either renumber endpoint matches
dirent_cache.for_fd, then perform the descriptor move.The minimal patch passed the following experiment:
- Unpatched: 3/3 executions diverged from P1.
- Patched: 3/3 executions exactly matched P1.
- Reverted: 3/3 executions diverged again, and both source and Adapter SHA-256 matched the unpatched build.
alexcrichton added the wasi label to Issue #15327.
Last updated: Nov 19 2026 at 20:05 UTC