Skip to content

WASI preview 1: fd_readdir ignores cookie argument #47193

Closed
nodejs/uvwasi
#298
@uhyo

Description

@uhyo

Version

v19.8.1

Platform

Darwin uhyo 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000 arm64

Subsystem

No response

What steps will reproduce the bug?

Create a WASM binary that calls the fd_readdir function with a non-zero cookie argument.

Reproduction repository: https://github.com/uhyo/rust-wasi-readdir

Example Rust code that demonstrates the problem
use std::{env, fs, io};

fn main() -> io::Result<()> {
    println!("Calling fs::read_dir");
    for entry in fs::read_dir(env::current_dir()?)? {
        let entry = entry?;
        println!("Entry {}", entry.path().display());
    }
    Ok(())
}

How often does it reproduce? Is there a required condition?

always

What is the expected behavior? Why is that the expected behavior?

A typical use case of the fd_readdir function is to repeatedly call it until the caller can retrieve all entries in a directory. The cookie parameter is used as a cursor so that the function can know where in the given directory to continue from. The fd_readdir should respect the cookie parameter. (Read more in the reproduction repository's README)

What do you see instead?

Actually, the fd_readdir function ignores the cookie parameters and always returns the same set of entries. The caller falls into an infinite loop.

Log of `fd_readdir` calls and how Node.js responded to them
fd_readdir(4,1065824,128,0,1047548) = 0
offset = 0 {
  d_next: 0n,
  d_ino: 0n,
  d_namlen: 17,
  d_type: 4,
  name: 'run-node-wasi.mjs'
}
offset = 41 { d_next: 1n, d_ino: 0n, d_namlen: 10, d_type: 4, name: 'Cargo.toml' }
offset = 75 {
  d_next: 2n,
  d_ino: 0n,
  d_namlen: 20,
  d_type: 4,
  name: 'run-wasmtime-wasi.sh'
}
(offset = 119)
Entry /run-node-wasi.mjs
Entry /Cargo.toml
Entry /run-wasmtime-wasi.sh
fd_readdir(4,1065824,128,2,1047548) = 0
offset = 0 {
  d_next: 0n,
  d_ino: 0n,
  d_namlen: 17,
  d_type: 4,
  name: 'run-node-wasi.mjs'
}
offset = 41 { d_next: 1n, d_ino: 0n, d_namlen: 10, d_type: 4, name: 'Cargo.toml' }
offset = 75 {
  d_next: 2n,
  d_ino: 0n,
  d_namlen: 20,
  d_type: 4,
  name: 'run-wasmtime-wasi.sh'
}
(offset = 119)
Entry /run-node-wasi.mjs
Entry /Cargo.toml
Entry /run-wasmtime-wasi.sh
fd_readdir(4,1065824,128,2,1047548) = 0
offset = 0 {
  d_next: 0n,
  d_ino: 0n,
  d_namlen: 17,
  d_type: 4,
  name: 'run-node-wasi.mjs'
}
offset = 41 { d_next: 1n, d_ino: 0n, d_namlen: 10, d_type: 4, name: 'Cargo.toml' }
offset = 75 {
  d_next: 2n,
  d_ino: 0n,
  d_namlen: 20,
  d_type: 4,
  name: 'run-wasmtime-wasi.sh'
}
(offset = 119)
Entry /run-node-wasi.mjs
Entry /Cargo.toml
Entry /run-wasmtime-wasi.sh
fd_readdir(4,1065824,128,2,1047548) = 0
offset = 0 {
  d_next: 0n,
  d_ino: 0n,
  d_namlen: 17,
  d_type: 4,
  name: 'run-node-wasi.mjs'
}
offset = 41 { d_next: 1n, d_ino: 0n, d_namlen: 10, d_type: 4, name: 'Cargo.toml' }
offset = 75 {
  d_next: 2n,
  d_ino: 0n,
  d_namlen: 20,
  d_type: 4,
  name: 'run-wasmtime-wasi.sh'
}
(offset = 119)
Entry /run-node-wasi.mjs
Entry /Cargo.toml
Entry /run-wasmtime-wasi.sh
fd_readdir(4,1065824,128,2,1047548) = 0
offset = 0 {
  d_next: 0n,
  d_ino: 0n,
  d_namlen: 17,
  d_type: 4,
  name: 'run-node-wasi.mjs'
}
offset = 41 { d_next: 1n, d_ino: 0n, d_namlen: 10, d_type: 4, name: 'Cargo.toml' }
offset = 75 {
  d_next: 2n,
  d_ino: 0n,
  d_namlen: 20,
  d_type: 4,
  name: 'run-wasmtime-wasi.sh'
}
(offset = 119)

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    uvwasi-agendawasiIssues and PRs related to the WebAssembly System Interface.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions