FastVM / Web49

Web49: WebAssembly Interpeter
MIT License
324 stars 15 forks source link

Does WASI stdio work? #15

Open konsumer opened 10 months ago

konsumer commented 10 months ago

I am using this to route WASI calls to web49_api_wasi

web49_env_func_t web49_main_import_func(void* state, const char* mod, const char* func) {
  printf("called: %s.%s\n", mod, func);

  if (strcmp(mod, "null0") == 0) {
    return web49_api_null0(state, mod, func);
  } else if (strcmp(mod, "wasi_snapshot_preview1") == 0) {
    return web49_api_wasi(state, mod, func);
  }
  fprintf(stderr, "Unhandled import: %s.%s\n", mod, func);
  return NULL;
}

and for a printf from the wasm, it seems to just output huge chunks of memory. Do I need to put more in state?

konsumer commented 10 months ago

Ah, I see this I will try to set it up.

Update: still seems to be a problem.

extern const char** environ;
const char** args;

args = argv + 1;
web49_wasi_t* wasi = web49_wasi_new(args, environ);
web49_interp_add_import_func(&interp, wasi, &web49_main_import_func);
konsumer commented 10 months ago

Even compiling miniwasm and running on this code:

#include "stdio.h"

int main() {
  printf("hello!\n");
  printf("You should see a newline at end of both of these lines, and no memory-dump.\n");
  return 0;
}

compiled like this:

clang test.c --target=wasm32-wasi --sysroot=${WASI_SYSROOT} -Oz -std=c11 -o test.wasm

does not output newlines:

./bin/miniwasm test.wasm
hello!You should see a newline at end of both of these lines, and no memory-dump.

Versions:

(in debian:latest docker)
Linux deda87241c50 5.15.49-linuxkit #1 SMP PREEMPT Tue Sep 13 07:51:32 UTC 2022 aarch64 GNU/Linux
Debian clang version 14.0.6
Mac M1 running 13.5.2 (22G91)
Apple clang version 14.0.0 (clang-1400.0.29.202)