wasmerio / wasmer

🚀 The leading Wasm Runtime supporting WASIX, WASI and Emscripten
https://wasmer.io
MIT License
18.28k stars 775 forks source link

`path_open` not working with wasi-sdk #4361

Open yagehu opened 7 months ago

yagehu commented 7 months ago

Describe the bug

This following simple C program compiled with wasi-sdk does not work with Wasmer.

$ wasmer -vV; rustc -vV
wasmer 4.2.4 (1abcd2c 2023-12-16)
binary: wasmer-cli
commit-hash: 1abcd2cc63deeca13af6506add6029edaba4d657
commit-date: 2023-12-16
host: x86_64-unknown-linux-gnu
compiler: singlepass,cranelift
rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2

Steps to reproduce

Compile with wasi-sdk and run with Wasmer.

$ clang wasmer-creat.c
$ mkdir -p tmptest
$ wasmer run --dir . a.out
#include <fcntl.h>
#include <stdio.h>

int main(void) {
  int fd = open("tmptest/a", O_CREAT | O_RDWR);
  if (fd == -1) {
    perror("open");
    return 1;
  }

  return 0;
}

Expected behavior

This snippet should create a file a in the tmptest directory.

Actual behavior

open: No such file or directory

Additional context

It seems the parent directory is resolved to the virtual root in the path_open handler. I'll be able to submit a patch.

linear[bot] commented 7 months ago

RUN-24 `path_open` not working with wasi-sdk

theduke commented 7 months ago

The file system is sandboxed by default.

You need to mount host directories into the WASM context by using --mapdir <GUEST_PATH>:<HOST_PATH>.

In your example: --mapdir tmptest:tmptest.

theduke commented 7 months ago

Feel free to comment/reopen if you run into additional issues.

yagehu commented 7 months ago

@theduke I'm aware. I am mounting a directory with --dir .. I've raised a PR that fixes this. https://github.com/wasmerio/wasmer/pull/4363

yagehu commented 7 months ago

I believe this is just an error with mapping . whichthe PR fixes.