1

I'm trying to follow a WASI-tutorial but all I'm getting are errors. I'm currently using wasmtime-cli version 1.0.0, and clang 14.0.0.

The tutorial has 3 simple steps.

  1. save sample code to demo.c. This code is shown at the bottom of this question.
  2. compile with clang demo.c -o demo.wasm
  3. run with wasmtime demo.wasm. This results in the error,
Error: failed to run main module `demo.wasm`

Caused by:
    cannot load precompiled module `demo.wasm` unless --allow-precompiled is passed

Running again with wasmtime --allow-precompiled demo.wasm results in another error.

Error: failed to run main module `demo.wasm`

Caused by:
    bytes are not a compatible serialized wasmtime module

What could be causing this? How can I get this demo working?

// demo.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc, char **argv) {
    ssize_t n, m;
    char buf[BUFSIZ];

    if (argc != 3) {
        fprintf(stderr, "usage: %s <from> <to>\n", argv[0]);
        exit(1);
    }

    int in = open(argv[1], O_RDONLY);
    if (in < 0) {
        fprintf(stderr, "error opening input %s: %s\n", argv[1], strerror(errno));
        exit(1);
    }

    int out = open(argv[2], O_WRONLY | O_CREAT, 0660);
    if (out < 0) {
        fprintf(stderr, "error opening output %s: %s\n", argv[2], strerror(errno));
        exit(1);
    }

    while ((n = read(in, buf, BUFSIZ)) > 0) {
        char *ptr = buf;
        while (n > 0) {
            m = write(out, ptr, (size_t)n);
            if (m < 0) {
                fprintf(stderr, "write error: %s\n", strerror(errno));
                exit(1);
            }
            n -= m;
            ptr += m;
        }
    }

    if (n < 0) {
        fprintf(stderr, "read error: %s\n", strerror(errno));
        exit(1);
    }

    return EXIT_SUCCESS;
}
J'e
  • 3,014
  • 4
  • 31
  • 55
  • 4
    *The wasi-sdk provides a clang which is configured to target WASI and use the WASI sysroot by default if you put the extracted tree into /, so we can compile our program like...* - so, do you actually use the "right" `clang`? – Eugene Sh. Sep 22 '22 at 18:53
  • I installed wasmtime using `curl https://wasmtime.dev/install.sh -sSf | bash`. I tried removing clang, then rerunning the curl command. there is now no clang installed. I suspect you may be right. If you have clang, and wasmtime working, what version of clang do you have? – J'e Sep 22 '22 at 18:57
  • Wasmtime should install its own Clang, the only question is where it's located. – HolyBlackCat Sep 22 '22 at 18:59
  • I don't have any of these, but this command seem to only install `wasmtime` and not the SDK – Eugene Sh. Sep 22 '22 at 19:00
  • 1
    @EugeneSh. I installed the SDK and it works! it comes with clang v14.0.4 – J'e Sep 22 '22 at 19:01

0 Answers0