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

Support for wasm32-wasi #261

Closed
1c3t3a opened this issue Sep 5, 2021 · 6 comments
Closed

Support for wasm32-wasi #261

1c3t3a opened this issue Sep 5, 2021 · 6 comments

Comments

@1c3t3a
Copy link

1c3t3a commented Sep 5, 2021

This crate currently doesn't compile to the WebAssembly wasm32-unknown-unknown target. It yields the following compile error:

cargo build --target wasm32-unknown-unknown
   Compiling net2 v0.2.37
   Compiling block-padding v0.1.5
   Compiling unicase v1.4.2
   Compiling httparse v1.5.1
   Compiling base64 v0.9.3
error[E0432]: unresolved import `sys`
  --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/tcp.rs:18:5
   |
18 | use sys::c;
   |     ^^^ maybe a missing crate `sys`?
error[E0432]: unresolved import `sys`
  --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/socket.rs:20:5
   |
20 | use sys;
   |     ^^^ no `sys` in the root

error[E0432]: unresolved import `sys`
  --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/socket.rs:21:5
   |
21 | use sys::c;
   |     ^^^ maybe a missing crate `sys`?

error[E0432]: unresolved import `sys`
  --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/ext.rs:19:5
   |
19 | use sys;
   |     ^^^ no `sys` in the root

error[E0432]: unresolved import `sys`
  --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/ext.rs:20:5
   |
20 | use sys::c;
   |     ^^^ maybe a missing crate `sys`?

error[E0412]: cannot find type `c_int` in this scope
  --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/socket.rs:28:24
   |
28 |     pub fn new(family: c_int, ty: c_int) -> io::Result<Socket> {
   |                        ^^^^^ not found in this scope
   |
help: consider importing this type alias
   |
11 | use std::os::raw::c_int;
   |

and so on...

Support for this target would enable usage in Wasm applications that are either running in the browser with crates like web-sys or in any freestanding runtime like wasmer.

@vi
Copy link
Member

vi commented Sep 5, 2021

  1. Are you sure you have the toolchain set correctly? Do other typical libraries get compiled properly for wasm32-unknown-unknown?
  2. How are you planning to have Websocket library do IO in wasm32-unknown-unknown?
  3. Have you tried wasm32-wasi instead of wasm32-unknown-unknown? It has a part of libstd, so more things may try to work. WASI's imports may actually be missing from the library you are building unless you actually use WASI things, so WASI mode may be hackily used even for non-WASI things (especially if you help it by providing defining some trivial-ish functions that tricked through anyway).

@1c3t3a
Copy link
Author

1c3t3a commented Sep 7, 2021

  1. Yes, that was an old error message which I misleadingly copied first. The one you see in the edit version is the one I am seeing right now.
  2. As mentioned above crates like web-sys could help interacting with IO - if I am planning to run my code in a browser. But you're right, WASI sounds more reasonable.
  3. Yes, but it errors when trying to compile openssl-sys. I ran into a similar issue when trying to compile another crate with an openssl-sys dependency.

The error looks like this:

cargo build --target wasm32-wasi             
   Compiling openssl-sys v0.9.66
   Compiling net2 v0.2.37
   Compiling time v0.1.43
   Compiling rand_chacha v0.1.1
error[E0432]: unresolved import `libc::__wasi_fd_t`
  --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/sys/wasi/mod.rs:12:25
   |
12 | use libc::{self, c_int, __wasi_fd_t};
   |                         ^^^^^^^^^^^ no `__wasi_fd_t` in the root

error[E0432]: unresolved import `sys::c::__wasi_fd_t`
 --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/sys/wasi/impls.rs:5:17
  |
5 | use sys::{self, c::__wasi_fd_t};
  |                 ^^^^^^^^^^^^^^ no `__wasi_fd_t` in `sys::c`

error[E0425]: cannot find function `addr2raw_v4` in this scope
   --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/socket.rs:105:36
    |
105 |         &SocketAddr::V4(ref v4) => addr2raw_v4(v4),
    |                                    ^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `addr2raw_v6` in this scope
   --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/socket.rs:106:36
    |
106 |         &SocketAddr::V6(ref v6) => addr2raw_v6(v6),
    |                                    ^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `__wasi_sock_send` in crate `libc`
    --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/ext.rs:1221:32
     |
1221 |             let _errno = libc::__wasi_sock_send(
     |                                ^^^^^^^^^^^^^^^^ not found in `libc`

error[E0412]: cannot find type `__wasi_fd_t` in crate `libc`
    --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/ext.rs:1222:41
     |
1222 |                 self.as_sock() as libc::__wasi_fd_t,
     |                                         ^^^^^^^^^^^ not found in `libc`

error[E0412]: cannot find type `__wasi_roflags_t` in module `sys::c`
    --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/ext.rs:1262:37
     |
1262 |         let _ro_flags: *mut sys::c::__wasi_roflags_t = &mut 0;
     |                                     ^^^^^^^^^^^^^^^^ help: a type alias with a similar name exists: `__wasi_rights_t`
     | 
    ::: /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.101/src/wasi.rs:40:1
     |
40   | pub type __wasi_rights_t = u64;
     | ------------------------------- similarly named type alias `__wasi_rights_t` defined here

error[E0425]: cannot find function `__wasi_sock_recv` in this scope
    --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/ext.rs:1264:26
     |
1264 |             let _errno = __wasi_sock_recv(
     |                          ^^^^^^^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `__wasi_fd_t` in this scope
   --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/sys/wasi/mod.rs:115:40
    |
115 |     pub unsafe fn getsockname(_socket: __wasi_fd_t, _address: *mut sockaddr,
    |                                        ^^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `__wasi_fd_t` in this scope
   --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/sys/wasi/mod.rs:119:36
    |
119 |     pub unsafe fn connect(_socket: __wasi_fd_t, _address: *const sockaddr,
    |                                    ^^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `__wasi_fd_t` in this scope
   --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/sys/wasi/mod.rs:123:35
    |
123 |     pub unsafe fn listen(_socket: __wasi_fd_t, _backlog: c_int) -> c_int {
    |                                   ^^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `__wasi_fd_t` in this scope
   --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/sys/wasi/mod.rs:126:33
    |
126 |     pub unsafe fn bind(_socket: __wasi_fd_t, _address: *const sockaddr,
    |                                 ^^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `__wasi_fd_t` in crate `libc`
   --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/sys/wasi/mod.rs:149:32
    |
149 |     pub fn raw(&self) -> libc::__wasi_fd_t {
    |                                ^^^^^^^^^^^ not found in `libc`

error[E0412]: cannot find type `__wasi_fd_t` in crate `libc`
   --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/sys/wasi/mod.rs:153:31
    |
153 |     fn into_fd(self) -> libc::__wasi_fd_t {
    |                               ^^^^^^^^^^^ not found in `libc`

error[E0412]: cannot find type `__wasi_fd_t` in crate `libc`
   --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/sys/wasi/mod.rs:173:24
    |
173 |     type Inner = libc::__wasi_fd_t;
    |                        ^^^^^^^^^^^ not found in `libc`

error[E0412]: cannot find type `__wasi_fd_t` in crate `libc`
   --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/sys/wasi/mod.rs:174:29
    |
174 |     fn from_inner(fd: libc::__wasi_fd_t) -> Socket {
    |                             ^^^^^^^^^^^ not found in `libc`

error[E0554]: `#![feature]` may not be used on the stable release channel
  --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/lib.rs:47:33
   |
47 | #![cfg_attr(target_os = "wasi", feature(wasi_ext))]
   |                                 ^^^^^^^^^^^^^^^^^

error[E0658]: unions with non-`Copy` fields other than `ManuallyDrop<T>` are unstable
  --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/socket.rs:93:5
   |
93 |     v4: c::sockaddr_in,
   |     ^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #55149 <https://github.com/rust-lang/rust/issues/55149> for more information

error[E0658]: unions with non-`Copy` fields other than `ManuallyDrop<T>` are unstable
  --> /Users/basti/.cargo/registry/src/github.com-1ecc6299db9ec823/net2-0.2.37/src/socket.rs:94:5
   |
94 |     v6: c::sockaddr_in6,
   |     ^^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #55149 <https://github.com/rust-lang/rust/issues/55149> for more information

error: aborting due to 19 previous errors

Some errors have detailed explanations: E0412, E0425, E0432, E0554, E0658.
For more information about an error, try `rustc --explain E0412`.
error: could not compile `net2`

To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed

It seems like the error is coming from net2

@1c3t3a 1c3t3a changed the title Support for wasm32-unknown-unknown Support for wasm32-wasi Sep 7, 2021
@1c3t3a
Copy link
Author

1c3t3a commented Sep 7, 2021

I looked into this and it looks like the problem lies inside Tokio. But there is a plan to support wasm in the future and also a related tracking issue: #1597

As expected, the sync version of this crate compiles:
cargo build --no-default-features --features sync --target wasm32-wasi

@vi
Copy link
Member

vi commented Sep 7, 2021

Note that if Tokio gains wasm support, it would likely be Tokio v1 (or later).

rust-websocket library is still based on Tokio 0.1 (pre-async/await), so that new Tokio thing would probably not be benefitial for that.

This library is semi-abandoned. Have you tried other libraries like tungstenite instead?

@vi
Copy link
Member

vi commented Sep 7, 2021

web-sys

If you want to use client WebSockets from browser, you'll probably want to use browser implementation of websockets, not do the WebSocket negotiation manually from within WebAssembly.

Maybe you just need web_sys::WebSocket?

@1c3t3a
Copy link
Author

1c3t3a commented Sep 14, 2021

Note that if Tokio gains wasm support, it would likely be Tokio v1 (or later).

rust-websocket library is still based on Tokio 0.1 (pre-async/await), so that new Tokio thing would probably not be benefitial for that.

This library is semi-abandoned. Have you tried other libraries like tungstenite instead?

Yep that might be the best idea. I am researching this for a socket.io library I maintain, so there is no external pressure for WASM support right now.

web-sys

If you want to use client WebSockets from browser, you'll probably want to use browser implementation of websockets, not do the WebSocket negotiation manually from within WebAssembly.

Maybe you just need web_sys::WebSocket?

Yep, that would be the way to go.

@1c3t3a 1c3t3a closed this as completed Sep 14, 2021
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

2 participants