wasmi icon indicating copy to clipboard operation
wasmi copied to clipboard

WASI: Can't open a file with both truncate and append flags

Open yagehu opened this issue 1 year ago • 4 comments
trafficstars

Compile this snippet with wasi-sdk and run it with wasmi will generate an invalid argument error. It does NOT error on other runtimes like Wasmtime or on native Linux.

$ clang wasmi-creat.c
$ wasmi_cli --dir . a.out
executing File("a.out")::_start() ...
open: Invalid argument
#include <fcntl.h>
#include <stdio.h>

int main(void) {
  int fd = open("a", O_CREAT | O_WRONLY | O_APPEND | O_TRUNC);
  if (fd == -1) {
    perror("open");
    return 1;
  }

  return 0;
}

yagehu avatar Dec 21 '23 04:12 yagehu

I believe this is a result of this check in cap-std. Wasmtime does not have this problem because it no longer uses cap-std in preview1.

yagehu avatar Dec 21 '23 04:12 yagehu

@yagehu Thank you for reporting this issue!

Wasmi uses Wasmtime's WASI implementation because it is a huge amount of work to re-implement WASI yourself. However, since then their WASI implementation made it impossible for other runtime such as Wasmi to use newer versions and thus we cannot easily fix this problem. Furthermore the WASI implementation for Wasmi was thankfully contributed externally by @OLUWAMUYIWA so I am not particularly expert in this part of the codebase.

There are 2 options forward to fix WASI issues for Wasmi:

  1. Fork the old Wasmtime WASI implementation and provide patches.
    • Pros: Probably less work to fix bugs.
    • Cons: We are kinda stuck with WASI preview 1.
  2. Provide our own WASI implementation.
    • Pros: We have full control of over WASI stack and can update it according to the spec.
    • Cons: A huge amount of (unpaid) work.

Robbepop avatar Dec 21 '23 07:12 Robbepop

Totally fair assessment. I tested removing the check I linked in cap-std and it indeed works without it. Apparently Wasmtime now uses wasmtime-wasi crate instead of cap-std so this issue is not observable in Wasmtime. There might be a third option which is to use wasmtime-wasi.

yagehu avatar Dec 27 '23 18:12 yagehu

You are welcome to experiment with integrating wasmtime-wasi into Wasmi. :)

Robbepop avatar Dec 27 '23 21:12 Robbepop

I close this issue and put a link to it in https://github.com/wasmi-labs/wasmi/issues/943.

Robbepop avatar May 31 '24 09:05 Robbepop