Skip to content

Commit 7f968f5

Browse files
authored
wasi_socket_ext.c: avoid tls to make this library-friendly (bytecodealliance#4338)
1 parent 07c23cb commit 7f968f5

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

core/iwasm/libraries/lib-socket/src/wasi/wasi_socket_ext.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@
1212
#include <wasi/api.h>
1313
#include <wasi_socket_ext.h>
1414

15+
/*
16+
* Avoid direct TLS access to allow a single library to be
17+
* linked to both of threaded and non-threaded applications.
18+
*
19+
* wasi-libc's errno is a TLS variable, exposed directly via
20+
* errno.h. if we use it here, LLVM may lower it differently,
21+
* depending on enabled features like atomcs and bulk-memory.
22+
* we tweak the way to access errno here in order to make us
23+
* compatible with both of threaded and non-threaded applications.
24+
* __errno_location() should be reasonably stable because
25+
* it was introduced as an alternative ABI for non-C software.
26+
* https://github.com/WebAssembly/wasi-libc/pull/347
27+
*/
28+
#if defined(errno)
29+
#undef errno
30+
#endif
31+
int *
32+
__errno_location(void);
33+
#define errno (*__errno_location())
34+
1535
#define HANDLE_ERROR(error) \
1636
if (error != __WASI_ERRNO_SUCCESS) { \
1737
errno = error; \

0 commit comments

Comments
 (0)