Skip to content

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

@carlocab

Description

@carlocab

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions