emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Fix `sendmsg()` implementation

Open adamscott opened this issue 7 months ago • 2 comments

~~One-char fix!~~ Finally, a little more complicated.

This PR introduces a new test. This test calls getifaddrs(), which internally would ultimately call setmsg().

The problem was there: calling getifaddrs() makes dest here undefined, but sock.type === {{{ cDefs.SOCK_DGRAM }}} would be true.

        if (!dest || dest.socket.readyState !== dest.socket.OPEN) {
          // if we're not connected, open a new connection
          if (sock.type === {{{ cDefs.SOCK_DGRAM }}}) {
            if (!dest || dest.socket.readyState === dest.socket.CLOSING || dest.socket.readyState === dest.socket.CLOSED) {
              dest = SOCKFS.websocket_sock_ops.createPeer(sock, addr, port);
            }

The current behaviour would skip then the assignement and dest would be called just after, but throw as dest would be undefined.

My solution is to make sure to set dest even if the sock.type matches. Just to be sure, after the condition, I force the sock type. Therefore, I think the intent of #22630 is respected.

        if (!dest || dest.socket.readyState !== dest.socket.OPEN) {
          // if we're not connected, open a new connection
          if (!dest || sock.type === {{{ cDefs.SOCK_DGRAM }}}) {
            sock.type === {{{ cDefs.SOCK_DGRAM }}};
            if (!dest || dest.socket.readyState === dest.socket.CLOSING || dest.socket.readyState === dest.socket.CLOSED) {
              dest = SOCKFS.websocket_sock_ops.createPeer(sock, addr, port);
            }

Fixes #23046.

adamscott avatar Apr 25 '25 02:04 adamscott

@sbc100 I completely rewrote my PR from the ground up. I added a test, changed the fix, and I updated the PR description.

adamscott avatar Apr 25 '25 19:04 adamscott

@sbc100 ci/circleci seems to break because emcc: error: LLVM version for clang executable "/root/emsdk/upstream/bin/clang" appears incorrect (seeing "21.0", expected "20") [-Wversion-check] [-Werror]

adamscott avatar Apr 25 '25 19:04 adamscott

In the PR description I assume you meant to write sendmsg and not setmsg?

sbc100 avatar May 19 '25 15:05 sbc100

I wonder if the problem can instead be fixed at a higher level. It seems that getifaddrs in musl is using __rtnetlink_enumerate which uses PF_NETLINK protocol family.. which emscripten does not support.

Do i'm pretty sure getifaddrs does not work at all right? I don't how it fails? Presumably this crash is just the tip if the iceberg.

sbc100 avatar May 19 '25 15:05 sbc100

I think I found a better solution which is simply not allow the AF_NETLINK socket to be created: https://github.com/emscripten-core/emscripten/pull/24364

sbc100 avatar May 19 '25 16:05 sbc100