emscripten-core / emscripten-core/emscripten

OpenSSL dlopen abort error

Open
#14,492 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Disclaimer: I already read the issue here with an similar name, but the solution there (adding the no-engine and no-dso parameters) did not work for me.

My plan was to have a ssl connection running on top of a emscripten websocket bridge so i can do end to end encryption like i did in my native project. I have a simple openssl server running (port 7777) and also the websocket_to_posix_proxy (port 8088).

I successfuly compiled openssl with emscripten using the following script:

```
#!/bin/sh

OPENSSL="openssl-1.1.1d"

if [ -d ${OPENSSL} ]; then
rm -rf ${OPENSSL}
fi

if [ ! -f ${OPENSSL}.tar.gz ]; then
curl -O https://www.openssl.org/source/${OPENSSL}.tar.gz
fi

tar xf ${OPENSSL}.tar.gz
cd ${OPENSSL} || exit 1

emconfigure ./Configure \
linux-generic64 \
no-asm \
no-engine \
no-hw \
no-weak-ssl-ciphers \
no-dtls \
no-shared \
no-dso \
--prefix=$EMSCRIPTEN/system

sed -i 's|^CROSS_COMPILE.*$|CROSS_COMPILE=|g' Makefile
sed -i '/^CFLAGS/ s/$/ -D__STDC_NO_ATOMICS__=1/' Makefile
sed -i '/^CXXFLAGS/ s/$/ -D__STDC_NO_ATOMICS__=1/' Makefile

emmake make -j 1 build_generated libssl.a libcrypto.a

cd ..
```

Then i tried linking it statically so i can use the openssl api on top of posix socket.

```
em++ -std=c++17 \
-s ALLOW_MEMORY_GROWTH=1 \
main.cpp \
lib/libssl.a \
lib/libcrypto.a \
-I E:\DATA\Libs\openssl-1.1.1d\include \
-o test.html
--shell-file html_template/shell_minimal.html \
-s EXPORTED_FUNCTIONS="['_main']" \
-s ASSERTIONS=1 \
-lwebsocket.js \
-s PROXY_POSIX_SOCKETS=1 \
-s USE_PTHREADS=1 \
-s PROXY_TO_PTHREAD=1 \
-static
```

My c++ code is just like in the sample "tests/websocket/tcp_echo_client.cpp".
But instead of sending data i try to open an tls connection:

```
#ifdef __EMSCRIPTEN__
bridgeSocket = emscripten_init_websocket_to_posix_socket_bridge("ws://localhost:8088");
// Synchronously wait until connection has been established.
uint16_t readyState = 0;
do {
emscripten_websocket_get_ready_state(bridgeSocket, &readyState);
emscripten_thread_sleep(100);
} while (readyState == 0);
#endif

//lookup_host("google.com");

// Create socket
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
printf("Could not create socket");
exit(1);
}
printf("Socket created: %d\n", sock);

struct sockaddr_in server;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons(7777);

if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}

SSL_CTX* ctx = InitSSL_CTX();
SSL* ssl = SSL_new(ctx);
if (ssl == nullptr)
{
fprintf(stderr, "SSL_new() failed\n");
exit(EXIT_FAILURE);
}

SSL_set_fd(ssl, sock);

const int status = SSL_connect(ssl);
if (status != 1)
{
SSL_get_error(ssl, status);
ERR_print_errors_fp(stderr); //High probability this doesn't do anything
fprintf(stderr, "SSL_connect failed with SSL_get_error code %d\n", status);
exit(EXIT_FAILURE);
}
```

I think the error im getting occurs at the `SSL_CTX* ctx = InitSSL_CTX();`.
The error is:
```
Pthread aborting at Error
at abort (http://localhost/test/test.js:1:24436)
at _dlopen (http://localhost/test/test.js:1:103187)
at http://localhost/test/test.wasm:wasm-function[1046]:0x55753
...
```

and also:
```
Uncaught RuntimeError: abort(To use dlopen, you need to use Emscripten's linking support, see https://github.com/emscripten-core/emscripten/wiki/Linking) at Error
at jsStackTrace (test.js:1)
at stackTrace (test.js:1)
at abort (test.js:1)
at _dlopen (test.js:1)
...
```

Also, i removed the -no-threads option from the openssl build script because i got a thread error. I dont know if that was the right way to do it and maybe this also has something to do with the problem im facing right now.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.