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

Failed to run the wasm32-wasip2 code #9034

Closed
hungryzzz opened this issue Jul 29, 2024 · 5 comments
Closed

Failed to run the wasm32-wasip2 code #9034

hungryzzz opened this issue Jul 29, 2024 · 5 comments

Comments

@hungryzzz
Copy link
Contributor

hungryzzz commented Jul 29, 2024

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;
}
@alexcrichton
Copy link
Member

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.

@hungryzzz
Copy link
Contributor Author

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

@alexcrichton
Copy link
Member

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.

@dicej
Copy link
Contributor

dicej commented Jul 29, 2024

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.

@dicej
Copy link
Contributor

dicej commented Jul 29, 2024

I've opened an issue on the wasi-libc repo: WebAssembly/wasi-libc#521

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants