Stream: git-wasmtime

Topic: wasmtime / issue #9034 Failed to run the wasm32-wasip2 code


view this post on Zulip Wasmtime GitHub notifications bot (Jul 29 2024 at 13:42):

hungryzzz opened issue #9034:

Hi, I have a C program which would send a HTTP request to a server, and use wasi-sdk-23 to compile it to wasm code, but fail to run it on Wasmtime, the error message is connect: Permission denied.

# wasi-sdk command
~/wasi-sdk-23.0-x86_64-linux/bin/clang-18 --sysroot=~/wasi-sdk-23.0-x86_64-linux/share/wasi-sysroot --target=wasm32-wasip2 client.c -o client.wasm

# run on wasmtime-cli ~ wasmtime client.wasm
connect: Permission denied

➜  ~ wasmtime --version
wasmtime-cli 23.0.0 (6b892131d 2024-06-20)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int socket_connect(char *host, in_port_t port){struct sockaddr_in addr;
    int on = 1, sock;

    addr.sin_port = htons(port);
    addr.sin_family = AF_INET;
        if (inet_pton(AF_INET, host, &addr.sin_addr) <= 0) {
          perror("inet_pton");
          exit(1);
        }

    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&on, sizeof(int));

    if(sock == -1){
        perror("setsockopt");
        exit(1);
    }

    if(connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == -1){
        perror("connect");
        exit(1);

    }
    return sock;
}

#define BUFFER_SIZE 1024

int main(){
    int fd;
    char buffer[BUFFER_SIZE];
        char header[] = "GET / HTTP/1.1\r\n\r\n";
        char hostname[] = "127.0.0.1";
        char port[] = "6000";

        fd = socket_connect(hostname, atoi(port));
    write(fd, header, strlen(header));
    bzero(buffer, BUFFER_SIZE);

    while(read(fd, buffer, BUFFER_SIZE - 1) != 0){
        fprintf(stderr, "%s", buffer);
        bzero(buffer, BUFFER_SIZE);
    }

    shutdown(fd, SHUT_RDWR);
    close(fd);

    return 0;
}

view this post on Zulip Wasmtime GitHub notifications bot (Jul 29 2024 at 13:45):

hungryzzz edited issue #9034:

Hi, I have a C program which would send a HTTP request to a server, and use wasi-sdk-23 to compile it to wasm code, but fail to run it on Wasmtime, the error message is connect: Permission denied. Does Wasmtime support wasi-socket now? Or the error is caused by other reasons?

# wasi-sdk command
~/wasi-sdk-23.0-x86_64-linux/bin/clang-18 --sysroot=~/wasi-sdk-23.0-x86_64-linux/share/wasi-sysroot --target=wasm32-wasip2 client.c -o client.wasm

# run on wasmtime-cli ~ wasmtime client.wasm
connect: Permission denied

➜  ~ wasmtime --version
wasmtime-cli 23.0.0 (6b892131d 2024-06-20)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int socket_connect(char *host, in_port_t port){struct sockaddr_in addr;
    int on = 1, sock;

    addr.sin_port = htons(port);
    addr.sin_family = AF_INET;
        if (inet_pton(AF_INET, host, &addr.sin_addr) <= 0) {
          perror("inet_pton");
          exit(1);
        }

    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&on, sizeof(int));

    if(sock == -1){
        perror("setsockopt");
        exit(1);
    }

    if(connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == -1){
        perror("connect");
        exit(1);

    }
    return sock;
}

#define BUFFER_SIZE 1024

int main(){
    int fd;
    char buffer[BUFFER_SIZE];
        char header[] = "GET / HTTP/1.1\r\n\r\n";
        char hostname[] = "127.0.0.1";
        char port[] = "6000";

        fd = socket_connect(hostname, atoi(port));
    write(fd, header, strlen(header));
    bzero(buffer, BUFFER_SIZE);

    while(read(fd, buffer, BUFFER_SIZE - 1) != 0){
        fprintf(stderr, "%s", buffer);
        bzero(buffer, BUFFER_SIZE);
    }

    shutdown(fd, SHUT_RDWR);
    close(fd);

    return 0;
}

view this post on Zulip Wasmtime GitHub notifications bot (Jul 29 2024 at 14:12):

alexcrichton closed issue #9034:

Hi, I have a C program which would send a HTTP request to a server, and use wasi-sdk-23 to compile it to wasm code, but fail to run it on Wasmtime, the error message is connect: Permission denied. Does Wasmtime support wasi-socket now? Or the error is caused by other reasons?

# wasi-sdk command
~/wasi-sdk-23.0-x86_64-linux/bin/clang-18 --sysroot=~/wasi-sdk-23.0-x86_64-linux/share/wasi-sysroot --target=wasm32-wasip2 client.c -o client.wasm

# run on wasmtime-cli ~ wasmtime client.wasm
connect: Permission denied

➜  ~ wasmtime --version
wasmtime-cli 23.0.0 (6b892131d 2024-06-20)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int socket_connect(char *host, in_port_t port){struct sockaddr_in addr;
    int on = 1, sock;

    addr.sin_port = htons(port);
    addr.sin_family = AF_INET;
        if (inet_pton(AF_INET, host, &addr.sin_addr) <= 0) {
          perror("inet_pton");
          exit(1);
        }

    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&on, sizeof(int));

    if(sock == -1){
        perror("setsockopt");
        exit(1);
    }

    if(connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == -1){
        perror("connect");
        exit(1);

    }
    return sock;
}

#define BUFFER_SIZE 1024

int main(){
    int fd;
    char buffer[BUFFER_SIZE];
        char header[] = "GET / HTTP/1.1\r\n\r\n";
        char hostname[] = "127.0.0.1";
        char port[] = "6000";

        fd = socket_connect(hostname, atoi(port));
    write(fd, header, strlen(header));
    bzero(buffer, BUFFER_SIZE);

    while(read(fd, buffer, BUFFER_SIZE - 1) != 0){
        fprintf(stderr, "%s", buffer);
        bzero(buffer, BUFFER_SIZE);
    }

    shutdown(fd, SHUT_RDWR);
    close(fd);

    return 0;
}

view this post on Zulip Wasmtime GitHub notifications bot (Jul 29 2024 at 14:12):

alexcrichton commented on issue #9034:

Wasmtime by default doesn't allow access to the network, but on the CLI you can pass -S inherit-network to give the program access to the network. When doing that though the write call is returning -1 which I think is also a bug, but I believe that's perhaps a bug for wasi-libc rather than Wasmtime.

view this post on Zulip Wasmtime GitHub notifications bot (Jul 29 2024 at 14:30):

hungryzzz commented on issue #9034:

Hi, I run Wasmtime with -S inherit-network option, but it hangs and I got no response in several minutes.

view this post on Zulip Wasmtime GitHub notifications bot (Jul 29 2024 at 15:01):

alexcrichton commented on issue #9034:

Yes, as I mentioned above as well, I saw that the write function call was returning -1. I think that's probably a bug in wasi-libc, not for here.

view this post on Zulip Wasmtime GitHub notifications bot (Jul 29 2024 at 15:21):

dicej commented on issue #9034:

Yeah, looks like I forgot to update write.c to check if the file descriptor is a socket and use wasi-sockets in that case, analogous to what I did here for close. send should work, though.

Looks like read.c needs an update, also.

view this post on Zulip Wasmtime GitHub notifications bot (Jul 29 2024 at 17:16):

dicej commented on issue #9034:

I've opened an issue on the wasi-libc repo: https://github.com/WebAssembly/wasi-libc/issues/521


Last updated: Nov 22 2024 at 17:03 UTC