wasi-preview1-component-adapter: fd_readdir duplicates a truncated entry after synthetic dot entries

Đang mở
#15,297 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
69/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust, wasm
Lĩnh vực
backend, testing

Hướng nghiên cứu

Start in crates/wasi-preview1-component-adapter/src/lib.rs at fd_readdir and DirectoryEntryIterator::next, then trace how the cached truncated entry is handled when resuming a directory stream. Use the standalone C reproducer and its split/resume comparison to confirm the duplicate entry. Done means resumed output matches a single full read without duplicated entries or shifted cookies; the issue reports 30 regression cases for broader verification.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

wasi

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, and wasmtime must 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.wasm

Expected 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_readdir saves the stream, cookie - 1, and cached_dirent (lib.rs:1502-1506). On resume, it sets use_cache: true (lib.rs:1413-1419). However, DirectoryEntryIterator::next handles synthetic ./.. from current_cookie before checking use_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_dirent before the synthetic dot-entry branches. The existing v46 cookie_resume_fix.patch used 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.

Ngôn ngữ chính
Rust
Star
18.9k
Fork
2k
Merge trung bình
1 ngày 6 giờ
Pull request đã merge (30 ngày)
238

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của bytecodealliance/wasmtime

Tất cả issue của bytecodealliance/wasmtime

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.