epitaphial opened issue #15297:
wasi-preview1-component-adapter: fd_readdir duplicates a truncated entry after synthetic dot entries
Summary
When a 23-byte buffer truncates the first dirent and cookie 0 is used for the next large read, Direct P1 returns
.,.., then the directory files. The adapter returns.,..,.again, then the files. This violates split/resume equivalence with one full read.Confirmed with the standalone C program below, without any custom test driver or trace format. Each lane reproduced the same output 5/5 times in fresh directories on Wasmtime 46.0.0 (Release and Debug), 47.0.2, and 50.0.1. Wasmtime 50.0.1 was the latest upstream release at verification time.
Minimal reproducer (
repro.c)#define _POSIX_C_SOURCE 200809L #include <fcntl.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #include <wasi/api.h> int main(void) { unsigned char small[23], large[4096]; __wasi_size_t used1 = 0, used2 = 0; mkdir("testdir", 0755); for (char name = 'a'; name <= 'd'; name++) { char path[32]; snprintf(path, sizeof(path), "testdir/%c.txt", name); close(open(path, O_CREAT | O_WRONLY, 0644)); } int fd = open("testdir", O_RDONLY | O_DIRECTORY); __wasi_errno_t error1 = __wasi_fd_readdir(fd, small, 23, 0, &used1); __wasi_errno_t error2 = __wasi_fd_readdir(fd, large, 4096, 0, &used2); printf("first: errno=%u used=%u\n", error1, (unsigned)used1); printf("second: errno=%u used=%u entries:", error2, (unsigned)used2); for (size_t offset = 0; offset + sizeof(__wasi_dirent_t) <= used2;) { __wasi_dirent_t entry; memcpy(&entry, large + offset, sizeof(entry)); if (offset + sizeof(entry) + entry.d_namlen > used2) break; printf(" %.*s(next=%llu)", (int)entry.d_namlen, large + offset + sizeof(entry), (unsigned long long)entry.d_next); offset += sizeof(entry) + entry.d_namlen; } printf("\n"); close(fd); return 0; }Exact commands
Run in a new empty directory. The first command downloads the official v50.0.1 command adapter;
clang,wasm-tools, andwasmtimemust be on the stated paths or adjusted to local installations.curl -fLO https://github.com/bytecodealliance/wasmtime/releases/download/v50.0.1/wasi_snapshot_preview1.command.wasm /opt/wasi-sdk/bin/clang --target=wasm32-wasip1 -std=c17 -O2 -Wall -Wextra -Werror repro_rc_dir_split_resume.c -o repro_p1.wasm wasm-tools component new repro_p1.wasm --adapt wasi_snapshot_preview1=wasi_snapshot_preview1.command.wasm -o repro_adapter.wasm wasmtime run --dir . repro_p1.wasm; wasmtime run --dir . repro_adapter.wasmExpected vs. actual output (Wasmtime 50.0.1)
Direct P1 (expected):
first: errno=0 used=23 second: errno=0 used=167 entries: .(next=1) ..(next=2) c.txt(next=3) b.txt(next=4) d.txt(next=5) a.txt(next=6)Adapter P2 (actual):
first: errno=0 used=23 second: errno=0 used=192 entries: .(next=1) ..(next=2) .(next=1) c.txt(next=4) b.txt(next=5) d.txt(next=6) a.txt(next=7)Root cause
On truncation,
fd_readdirsaves the stream,cookie - 1, andcached_dirent(lib.rs:1502-1506). On resume, it setsuse_cache: true(lib.rs:1413-1419). However,DirectoryEntryIterator::nexthandles synthetic./..fromcurrent_cookiebefore checkinguse_cache(lib.rs:1536-1571). The cached truncated entry is consequently emitted after the synthetic entries, duplicating it and shifting later cookies. The ordering remains in v50.0.1.Suggested fix and causal evidence
When resuming a cached stream, yield
cached_direntbefore the synthetic dot-entry branches. The existing v46cookie_resume_fix.patchused that ordering. Across 30 regression cases, violations went from 8 unpatched to 0 patched and back to 8 reverted; the reverted adapter binary matched the original.
alexcrichton added the wasi label to Issue #15297.
Last updated: Nov 19 2026 at 20:05 UTC