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

WASIX sockets #44

Closed
wants to merge 3 commits into from
Closed

WASIX sockets #44

wants to merge 3 commits into from

Conversation

Pryz
Copy link
Contributor

@Pryz Pryz commented Jun 8, 2023

WIP for #42

Current status:

  • Add the first socket API.
  • Trying to create a very simple example to test the API. Every single example coming from WASIX requires the WASI threads [1] proposal (https://github.com/WebAssembly/threads). With WASI threads I don't think we will be able to run binaries/projects targeted for wasmer.
  • The WASIX ecosystem is very new and sometimes unstable.

Next steps:

  • Split into multiple host modules to match what is expected by wasmer targeted binaries. See below.
  • Compile a simple example with Rust or C to iterate and finish the socket API
  • Implement resolve.

Imports example:

  (import "wasix_32v1" "getcwd" (func $wasix::lib_generated32::wasix_32v1::getcwd::hde9bdae5d79d7468 (type 6)))
  (import "wasix_32v1" "futex_wait" (func $wasix::lib_generated32::wasix_32v1::futex_wait::h39f3cb721b28037e (type 10)))
  (import "wasix_32v1" "futex_wake" (func $wasix::lib_generated32::wasix_32v1::futex_wake::h0dd0314d1018fbe9 (type 6)))
  (import "wasix_32v1" "futex_wake_all" (func $wasix::lib_generated32::wasix_32v1::futex_wake_all::h72d8153137b5cad9 (type 6)))
  (import "wasix_32v1" "sock_open" (func $wasix::lib_generated32::wasix_32v1::sock_open::ha75b32b840663a9d (type 10)))
  (import "wasix_32v1" "sock_set_opt_flag" (func $wasix::lib_generated32::wasix_32v1::sock_set_opt_flag::h27959743738cba80 (type 8)))
  (import "wasix_32v1" "sock_bind" (func $wasix::lib_generated32::wasix_32v1::sock_bind::hdd9cc4506c6610f9 (type 6)))
  (import "wasix_32v1" "sock_listen" (func $wasix::lib_generated32::wasix_32v1::sock_listen::h1d4e40e410a9e789 (type 6)))
  (import "wasix_32v1" "sock_accept_v2" (func $wasix::lib_generated32::wasix_32v1::sock_accept_v2::h87e928500da3bed3 (type 10)))
  (import "wasix_32v1" "resolve" (func $wasix::lib_generated32::wasix_32v1::resolve::h9f1425f52a604d99 (type 14)))
  (import "wasi_snapshot_preview1" "fd_close" (func $wasi::lib_generated::wasi_snapshot_preview1::fd_close::h549bef3802dac542 (type 3)))
  (import "wasi_snapshot_preview1" "fd_write" (func $wasi::lib_generated::wasi_snapshot_preview1::fd_write::h0d8b34deb09d60b1 (type 10)))
  (import "wasi_snapshot_preview1" "sock_recv" (func $wasi::lib_generated::wasi_snapshot_preview1::sock_recv::h5312ba4aabe037ad (type 14)))
  (import "wasi_snapshot_preview1" "sock_send" (func $wasi::lib_generated::wasi_snapshot_preview1::sock_send::h6af7a7878da33ec4 (type 12)))
  (import "wasi_snapshot_preview1" "environ_get" (func $__imported_wasi_snapshot_preview1_environ_get (type 6)))
  (import "wasi_snapshot_preview1" "environ_sizes_get" (func $__imported_wasi_snapshot_preview1_environ_sizes_get (type 6)))
  (import "wasi_snapshot_preview1" "clock_time_get" (func $__imported_wasi_snapshot_preview1_clock_time_get (type 19)))
  (import "wasi_snapshot_preview1" "fd_seek" (func $__imported_wasi_snapshot_preview1_fd_seek (type 21)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (type 2)))
  (import "wasi_snapshot_preview1" "sched_yield" (func $__imported_wasi_snapshot_preview1_sched_yield (type 1)))
  (import "wasix_32v1" "callback_signal" (func $__imported_wasix_32v1_callback_signal (type 5)))
  (import "wasix_32v1" "thread_signal" (func $__imported_wasix_32v1_thread_signal (type 6)))
  (import "wasix_32v1" "thread_exit" (func $__imported_wasix_32v1_thread_exit (type 2)))
  (import "wasi" "thread-spawn" (func $__imported_wasi_thread_spawn (type 3)))

Somehow the following example, will be compiled with WASM threads:

use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream, SocketAddr};
use socket2::{Socket, Domain, Type};

fn handle_client(mut stream: TcpStream) {
    let mut buffer = [0; 1024];

    loop {
        match stream.read(&mut buffer) {
            Ok(0) => return,
            Ok(n) => {
                stream.write_all(&buffer[..n]).unwrap();
            }
            Err(_) => return,
        }
    }
}

fn main() {
    let socket = Socket::new(Domain::IPV6, Type::STREAM, None).expect("Failed to create socket");

    let address: SocketAddr = "127.0.0.1:8080".parse().unwrap();
    socket.bind(&address.into()).expect("Failed to bind socket");
    socket.listen(128).expect("Failed to listen on socket");

    let listener: TcpListener = socket.into();
    println!("Server listening on http://127.0.0.1:8080");

    for stream in listener.incoming() {
        match stream {
            Ok(stream) => {
                handle_client(stream);
            }
            Err(e) => {
                eprintln!("Error: {}", e);
            }
        }
    }
}

[1] - https://github.com/WebAssembly/wasi-threads

Fixes #32

@Pryz
Copy link
Contributor Author

Pryz commented Jun 8, 2023

Compiling the simple example and running it with wasmer fails with:

❯  wasmer run  ./target/wasm32-wasmer-wasi/debug/simplehttp.wasi.wasm
thread 'tokio-runtime-worker' panicked at 'not yet implemented: could not serialize number 6 to enum Socktype', lib/wasi-types/src/wasi/bindings.rs:2834:18
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'main thread terminated without a result, this normally means a panic occurred: RecvError', lib/wasi/src/state/builder.rs:849:32

@Pryz
Copy link
Contributor Author

Pryz commented Jun 8, 2023

With wasirun, since we don't have threads:

✖  go run cmd/wasirun/main.go /Users/julienfabre/code/github.com/wasix-org/wasix-rust-examples/simplehttp/target/wasm32-wasmer-wasi/debug/simplehttp.wasi.wasm
error: import[0] memory[env.memory]: invalid byte for limits: 0x3 != 0x00 or 0x01
exit status 1

@syrusakbary
Copy link

This is awesome.

Let us know if we (Wasmer) can help in any way / cc @theduke @john-sharratt

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

Successfully merging this pull request may close these issues.

WASIX sockets support
2 participants