Skip to content

fix assumptions about how fd_prestat_get may be called #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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.

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.

1 participant