Skip to content

Conversation

Techatrix
Copy link

The current implementation of fd_prestat_get does not appears to handle the following cases properly:

  • fd_prestat_get is called on the same file descriptor again
  • fd_prestat_get is called on stdio file descriptors (fd < 3)
  • fd_prestat_get is called in non sequential order of file descriptors

For testing, I used the ./wasm-wasi/example project and modified the hello.c and extension.ts:

#include <stdio.h>
#include <string.h>

// Relies on https://github.com/WebAssembly/wasi-libc

int main() {
  int i = 2;
  while (i--) {
    __wasi_fd_t fd = 6;
    while (fd--) {
      __wasi_prestat_t prestat;
      __wasi_errno_t ret = __wasi_fd_prestat_get(fd, &prestat);
      printf("fd_prestat_get(%d): %s (%d)\n", fd, strerror(ret), ret);
    }
    printf("\n");
  }
}

I modified the call to createProcess in extension.ts to add an additional preopen directory:

const process = await wasm.createProcess('hello', module, {
	stdio: pty.stdio,
	trace: true,
	mountPoints: [{ kind: "memoryFileSystem", fileSystem: await wasm.createMemoryFileSystem(), mountPoint: "/foo" }]
});

Before:

fd_prestat_get(5): Success (0)
fd_prestat_get(4): Success (0)
fd_prestat_get(3): Bad file descriptor (8)
fd_prestat_get(2): Invalid argument (28)
fd_prestat_get(1): Invalid argument (28)
fd_prestat_get(0): Invalid argument (28)

fd_prestat_get(5): Invalid argument (28)
fd_prestat_get(4): Invalid argument (28)
fd_prestat_get(3): Invalid argument (28)
fd_prestat_get(2): Invalid argument (28)
fd_prestat_get(1): Invalid argument (28)
fd_prestat_get(0): Invalid argument (28)

After:

fd_prestat_get(5): Bad file descriptor (8)
fd_prestat_get(4): Success (0)
fd_prestat_get(3): Success (0)
fd_prestat_get(2): Bad file descriptor (8)
fd_prestat_get(1): Bad file descriptor (8)
fd_prestat_get(0): Bad file descriptor (8)

fd_prestat_get(5): Bad file descriptor (8)
fd_prestat_get(4): Success (0)
fd_prestat_get(3): Success (0)
fd_prestat_get(2): Bad file descriptor (8)
fd_prestat_get(1): Bad file descriptor (8)
fd_prestat_get(0): Bad file descriptor (8)

The new output matches exactly what I got from wasmtime with wasmtime run --dir=.::/foo --dir=.::/bar hello.wasm.

andy0130tw pushed a commit to agda-web/vscode-wasm that referenced this pull request Jul 28, 2025
The current implementation of `fd_prestat_get` does not appears to handle the following cases properly:

- fd_prestat_get is called on the same file descriptor again
- fd_prestat_get is called on stdio file descriptors (fd < 3)
- fd_prestat_get is called in non sequential order of file descriptors
@andy0130tw
Copy link

I discovered that I have to deal with the same problem in my test program. However this broke the tests, as the tests all request fd=4 as the only preopen (the first preopen should be 3). Here is my patch to the test: agda-web@9681eb1.

The current implementation of `fd_prestat_get` does not appears to handle the following cases properly:

- fd_prestat_get is called on the same file descriptor again
- fd_prestat_get is called on stdio file descriptors (fd < 3)
- fd_prestat_get is called in non sequential order of file descriptors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants