Skip to content
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

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

Open
yagehu opened this issue Dec 21, 2023 · 4 comments
Open

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

yagehu opened this issue Dec 21, 2023 · 4 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@yagehu
Copy link

yagehu commented Dec 21, 2023

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
Copy link
Author

yagehu commented Dec 21, 2023

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.

@Robbepop
Copy link
Collaborator

Robbepop commented Dec 21, 2023

@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 Robbepop added the bug Something isn't working label Dec 21, 2023
@Robbepop Robbepop changed the title Can't open a file with both truncate and append flags WASI: Can't open a file with both truncate and append flags Dec 21, 2023
@yagehu
Copy link
Author

yagehu commented Dec 27, 2023

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.

@Robbepop
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants