Skip to content

wasi_socket_ext.c: avoid tls to make this library-friendly #4338

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

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions core/iwasm/libraries/lib-socket/src/wasi/wasi_socket_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@
#include <wasi/api.h>
#include <wasi_socket_ext.h>

/*
* Avoid direct TLS access to allow a single library to be
* linked to both of threaded and non-threaded applications.
*
* wasi-libc's errno is a TLS variable, exposed directly via
* errno.h. if we use it here, LLVM may lower it differently,
* depending on enabled features like atomcs and bulk-memory.
* we tweak the way to access errno here in order to make us
* compatible with both of threaded and non-threaded applications.
* __errno_location() should be reasonably stable because
* it was introduced as an alternative ABI for non-C software.
* https://github.com/WebAssembly/wasi-libc/pull/347
*/
#if defined(errno)
#undef errno
#endif
int *
__errno_location(void);
#define errno (*__errno_location())

#define HANDLE_ERROR(error) \
if (error != __WASI_ERRNO_SUCCESS) { \
errno = error; \
Expand Down
Loading