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

Thread-related asserts trigger in C++ even when targeting thread-enabled WASI #558

Open
carlocab opened this issue Dec 13, 2024 · 3 comments

Comments

@carlocab
Copy link

carlocab commented Dec 13, 2024

Compiling a simple hello world program in C++ while targeting wasm32-wasip1-threads triggers an assertion here:

#include <assert.h>
#define pthread_create(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; \
to enable stub functions which always fail, \
compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
#define pthread_detach(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; \
to enable stub functions which always fail, \
compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})
#define pthread_join(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; \
to enable stub functions which always fail, \
compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread"); 0;})

This seems wrong because this mode of WASI does have threads enabled (unless I'm misunderstanding the meaning of the -threads suffix).

To reproduce:

curl -OL https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/wasi-sdk-25.0-arm64-macos.tar.gz
tar xf wasi-sdk-25.0-arm64-macos.tar.gz

cat <<CXX >test.cc
#include <iostream>
int main() {
  std::cout << "hello, world\n";
  return 0;
}
CXX

./wasi-sdk-25.0-arm64-macos/bin/clang++ \
  --sysroot=./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot \
  --target=wasm32-wasip1-threads \
  test.cc \
  -o test.wasm

This produces:

Error Log
In file included from test.cc:1:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/iostream:42:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/ios:220:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__locale:24:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/string:647:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/string_view:941:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/algorithm:1842:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__algorithm/for_each.h:16:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__ranges/movable_box.h:21:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/optional:1288:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/atomic:596:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__atomic/aliases.h:12:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__atomic/atomic.h:12:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__atomic/atomic_base.h:12:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__atomic/atomic_sync.h:20:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__thread/support.h:112:
./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__thread/support/pthread.h:182:10: error: static assertion failed: This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread
  182 |   return pthread_create(__t, nullptr, __func, __arg);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/pthread.h:92:47: note: expanded from macro 'pthread_create'
   92 | #define pthread_create(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; \
      |                                               ^
In file included from test.cc:1:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/iostream:42:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/ios:220:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__locale:24:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/string:647:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/string_view:941:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/algorithm:1842:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__algorithm/for_each.h:16:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__ranges/movable_box.h:21:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/optional:1288:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/atomic:596:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__atomic/aliases.h:12:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__atomic/atomic.h:12:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__atomic/atomic_base.h:12:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__atomic/atomic_sync.h:20:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__thread/support.h:112:
./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__thread/support/pthread.h:190:88: error: static assertion failed: This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread
  190 | inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_join(__libcpp_thread_t* __t) { return pthread_join(*__t, nullptr); }
      |                                                                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~
./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/pthread.h:98:45: note: expanded from macro 'pthread_join'
   98 | #define pthread_join(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; \
      |                                             ^
In file included from test.cc:1:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/iostream:42:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/ios:220:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__locale:24:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/string:647:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/string_view:941:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/algorithm:1842:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__algorithm/for_each.h:16:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__ranges/movable_box.h:21:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/optional:1288:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/atomic:596:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__atomic/aliases.h:12:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__atomic/atomic.h:12:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__atomic/atomic_base.h:12:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__atomic/atomic_sync.h:20:
In file included from ./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__thread/support.h:112:
./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/c++/v1/__thread/support/pthread.h:192:90: error: static assertion failed: This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread
  192 | inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_detach(__libcpp_thread_t* __t) { return pthread_detach(*__t); }
      |                                                                                          ^~~~~~~~~~~~~~~~~~~~
./wasi-sdk-25.0-arm64-macos/share/wasi-sysroot/include/wasm32-wasip1-threads/pthread.h:95:47: note: expanded from macro 'pthread_detach'
   95 | #define pthread_detach(...) ({ _Static_assert(0, "This mode of WASI does not have threads enabled; \
      |                                               ^
3 errors generated.

Of course, passing -D_WASI_EMULATED_PTHREAD to clang++ allows compilation to succeed, but it doesn't seem like this should be necessary.

Moreover, the message about -lwasi-emulated-pthread is also misleading. This library is not available for wasm32-wasip1-threads, so following the error message blindly leads to a linking error too.

The ideal solution here would be to make sure that the assertion doesn't fire when targeting a WASI mode with threads enabled. If that's not doable, then ideally the message shouldn't erroneously suggest using -lwasi-emulated-pthread when it isn't needed and will lead to a linking error.

Related: #518, #518 (comment)

@carlocab
Copy link
Author

It looks like passing -pthread instead of -D_WASI_EMULATED_PTHREAD also avoids the assertion failure, which explains why the wasi-sdk tests succeeded: https://github.com/WebAssembly/wasi-sdk/blob/a4d918fa119c9beb712bf08bbb8fa9996aab0a71/tests/CMakeLists.txt#L81-L84

@carlocab carlocab changed the title Thread-related static asserts trigger in C++ even when targeting thread-enabled WASI Thread-related asserts trigger in C++ even when targeting thread-enabled WASI Dec 13, 2024
@bjorn3
Copy link

bjorn3 commented Dec 13, 2024

To be fair portable usage of pthreads requires passing -pthread to gcc or clang. On some OSes it works without either (if pthread support is part of libc itself), on some OSes it links the required libpthread.so and on yet other OSes it links against an entirely different library that provides the pthread primitives. That said, I guess there is something to be said for making -pthread the default for wasm32-wasip1-threads.

@whitequark
Copy link
Contributor

cc @ArcaneNibble

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