Stream: git-wasmtime

Topic: wasmtime / issue #15327 wasi-preview1-component-adapter: ...


view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 10:09):

epitaphial opened issue #15327:

wasi-preview1-component-adapter: fd_renumber can associate a cached directory stream with the wrong descriptor

Summary

fd_renumber replaces a descriptor without transferring or invalidating the global dirent_cache. A subsequent fd_readdir on 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.wasm

Expected vs. actual output

Direct P1:

entries: . .. from-b.txt

Adapter:

entries: . .. . from-a.txt

The 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-1580 directly delegates fd_renumber to Descriptors::renumber; descriptors.rs:356-384 replaces the descriptor without updating dirent_cache. The cache is subsequently selected using only for_fd and cookie at lib.rs:1350-1356. The same delegation remains at v50.0.1 lib.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:

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 17:05):

alexcrichton added the wasi label to Issue #15327.


Last updated: Nov 19 2026 at 20:05 UTC