Skip to content
Draft
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ add_link_options(
-resource-dir ${tmp_resource_dir}
)

if (HAVE_WASM_LIBCALL_THREAD_CONTEXT)
# TODO: shouldn't have to pass these as it means it's required by all end
# users as well. Ideally this would be part of the driver in rustc/clang, but
# that's left for a future change.
add_link_options(
-Wl,--export-if-defined=__wasm_init_tls
-Wl,--export-if-defined=__tls_size
-Wl,--export-if-defined=__tls_align
)
endif()

# Expose the public headers to the implementation. We use `-isystem` for
# purpose for two reasons:
#
Expand Down
2 changes: 1 addition & 1 deletion cmake/scripts/run-check-symbols.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ foreach(symbol IN LISTS final_undefined_symbols)
NOT symbol MATCHES "__stack_pointer" AND
NOT symbol MATCHES "__init_stack_pointer" AND
NOT symbol MATCHES "__wasm_component_model_builtin.*" AND
NOT symbol STREQUAL "__tls_base")
NOT symbol MATCHES "^__tls_(base|size|align)$")
file(APPEND ${out_undefined_symbols} "${symbol}\n")
endif()
endforeach()
Expand Down
1 change: 0 additions & 1 deletion expected/wasm32-wasip1-threads/include-all.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <__errno.h>
#include <__errno_values.h>
#include <__fd_set.h>
#include <__function___isatty.h>
Expand Down
1 change: 0 additions & 1 deletion expected/wasm32-wasip1-threads/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3105,7 +3105,6 @@
#define __wasi_libc_h
#define __wasi_libc_nocwd_h
#define __wasi_wasip1_h
#define __wasilibc___errno_h
#define __wasilibc___errno_values_h
#define __wasilibc___fd_set_h
#define __wasilibc___function___isatty_h
Expand Down
2 changes: 0 additions & 2 deletions expected/wasm32-wasip1-threads/undefined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ __letf2
__lttf2
__netf2
__subtf3
__tls_align
__tls_size
__trunctfdf2
__trunctfsf2
__unordtf2
Expand Down
1 change: 0 additions & 1 deletion expected/wasm32-wasip1/include-all.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <__errno.h>
#include <__errno_values.h>
#include <__fd_set.h>
#include <__function___isatty.h>
Expand Down
1 change: 0 additions & 1 deletion expected/wasm32-wasip1/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3104,7 +3104,6 @@
#define __wasi_libc_h
#define __wasi_libc_nocwd_h
#define __wasi_wasip1_h
#define __wasilibc___errno_h
#define __wasilibc___errno_values_h
#define __wasilibc___fd_set_h
#define __wasilibc___function___isatty_h
Expand Down
1 change: 0 additions & 1 deletion expected/wasm32-wasip2/include-all.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <__errno.h>
#include <__errno_values.h>
#include <__fd_set.h>
#include <__function___isatty.h>
Expand Down
1 change: 0 additions & 1 deletion expected/wasm32-wasip2/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3102,7 +3102,6 @@
#define __wasi_libc_h
#define __wasi_libc_nocwd_h
#define __wasi_wasip1_h
#define __wasilibc___errno_h
#define __wasilibc___errno_values_h
#define __wasilibc___fd_set_h
#define __wasilibc___function___isatty_h
Expand Down
3 changes: 2 additions & 1 deletion expected/wasm32-wasip3-coop/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ __getdelim
__getentropy
__getopt_msg
__gmtime_r
__h_errno_location
__hashmap_bucket_item
__hashmap_clear
__hashmap_count
Expand Down Expand Up @@ -352,7 +353,6 @@ __wasilibc_get_service_entry_by_name
__wasilibc_get_service_entry_by_port
__wasilibc_getsockopt_timeout
__wasilibc_iftodt
__wasilibc_init_async_task
__wasilibc_init_stdio
__wasilibc_initialize_environ
__wasilibc_link
Expand Down Expand Up @@ -387,6 +387,7 @@ __wasilibc_rename_newat
__wasilibc_rename_oldat
__wasilibc_reset_preopens
__wasilibc_rmdirat
__wasilibc_set_task_tls
__wasilibc_setsockopt_timeout
__wasilibc_sockaddr_to_wasi
__wasilibc_sockaddr_validate
Expand Down
1 change: 0 additions & 1 deletion expected/wasm32-wasip3-coop/include-all.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <__errno.h>
#include <__errno_values.h>
#include <__fd_set.h>
#include <__function___isatty.h>
Expand Down
5 changes: 2 additions & 3 deletions expected/wasm32-wasip3-coop/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3109,7 +3109,6 @@
#define __wasi_libc_h
#define __wasi_libc_nocwd_h
#define __wasi_wasip1_h
#define __wasilibc___errno_h
#define __wasilibc___errno_values_h
#define __wasilibc___fd_set_h
#define __wasilibc___function___isatty_h
Expand Down Expand Up @@ -3227,7 +3226,7 @@
#define dirent64 dirent
#define erf(x) __tg_real(erf, (x))
#define erfc(x) __tg_real(erfc, (x))
#define errno errno
#define errno (*__errno_location())
#define exp(x) __tg_real_complex(exp, (x))
#define exp2(x) __tg_real(exp2, (x))
#define expm1(x) __tg_real(expm1, (x))
Expand Down Expand Up @@ -3259,7 +3258,7 @@
#define glob64_t glob_t
#define globfree64 globfree
#define h_addr h_addr_list[0]
#define h_errno h_errno
#define h_errno (*__h_errno_location())
#define howmany(n,d) (((n)+((d)-1))/(d))
#define htobe16(x) __bswap16(x)
#define htobe32(x) __bswap32(x)
Expand Down
4 changes: 2 additions & 2 deletions expected/wasm32-wasip3-coop/undefined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ __subtask_drop
__subtf3
__task_cancel
__thread_yield
__tls_align
__tls_size
__trunctfdf2
__trunctfsf2
__unordtf2
Expand Down Expand Up @@ -215,6 +213,8 @@ stream_drop_writable_unit
stream_new_unit
stream_read_unit
stream_write_unit
wasip3_context_get_1
wasip3_context_set_1
wasip3_thread_index
wasip3_thread_new_indirect
wasip3_thread_resume_later
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasip3/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ __getdelim
__getentropy
__getopt_msg
__gmtime_r
__h_errno_location
__hwcap
__inet_aton
__inhibit_ptc
Expand Down
1 change: 0 additions & 1 deletion expected/wasm32-wasip3/include-all.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <__errno.h>
#include <__errno_values.h>
#include <__fd_set.h>
#include <__function___isatty.h>
Expand Down
5 changes: 2 additions & 3 deletions expected/wasm32-wasip3/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3108,7 +3108,6 @@
#define __wasi_libc_h
#define __wasi_libc_nocwd_h
#define __wasi_wasip1_h
#define __wasilibc___errno_h
#define __wasilibc___errno_values_h
#define __wasilibc___fd_set_h
#define __wasilibc___function___isatty_h
Expand Down Expand Up @@ -3226,7 +3225,7 @@
#define dirent64 dirent
#define erf(x) __tg_real(erf, (x))
#define erfc(x) __tg_real(erfc, (x))
#define errno errno
#define errno (*__errno_location())
#define exp(x) __tg_real_complex(exp, (x))
#define exp2(x) __tg_real(exp2, (x))
#define expm1(x) __tg_real(expm1, (x))
Expand Down Expand Up @@ -3258,7 +3257,7 @@
#define glob64_t glob_t
#define globfree64 globfree
#define h_addr h_addr_list[0]
#define h_errno h_errno
#define h_errno (*__h_errno_location())
#define howmany(n,d) (((n)+((d)-1))/(d))
#define htobe16(x) __bswap16(x)
#define htobe32(x) __bswap32(x)
Expand Down
2 changes: 1 addition & 1 deletion libc-bottom-half/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ endif()

if (HAVE_WASM_LIBCALL_THREAD_CONTEXT)
list(APPEND bottom_half_sources
sources/wasip3_non_coop_tls_base.S
sources/wasip3_tls.c
sources/__wasm_init_task.S
)
endif()
Expand Down
3 changes: 3 additions & 0 deletions libc-bottom-half/cloudlibc/src/libc/errno/errno.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,7 @@ static_assert(ETXTBSY == __WASI_ERRNO_TXTBSY, "Value mismatch");
static_assert(EXDEV == __WASI_ERRNO_XDEV, "Value mismatch");
#endif

// `<errno.h>` may define `errno` as a call to `__errno_location`, so undo that
// here where the thread-local it ultimately refers to is defined.
#undef errno
thread_local int errno = 0;
11 changes: 4 additions & 7 deletions libc-bottom-half/crt/wasip3_symbol_references.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ __attribute__((used)) static void *__wasm_init_async_task_ref =
__attribute__((used)) static void *cabi_realloc_ref = cabi_realloc;

// Force `__wasm_{g,s}et_{stack_pointer,tls_base}` to exist as defined symbols.
// These end up as imported functions which `wit-component` recognizes. Note
// that for coop threads the tls_base symbols are defined but without coop
// threads they're defined within libc itself to access a locally-defined
// global (see `wasip3_non_coop_tls_base.S`).
// These end up as imported functions which `wit-component` recognizes, and what
// exactly they're hooked up to will depend on `wit-component` when this is
// turned into a component.
__asm__(
".globl __wasm_get_stack_pointer\n"
".type __wasm_get_stack_pointer,@function\n"
Expand All @@ -33,7 +32,6 @@ __asm__(
".import_module __wasm_set_stack_pointer, \"env\"\n"
".import_name __wasm_set_stack_pointer, \"__wasm_set_stack_pointer\"\n"

#ifdef __wasi_cooperative_threads__
".globl __wasm_get_tls_base\n"
".type __wasm_get_tls_base,@function\n"
".functype __wasm_get_tls_base () -> (i32)\n"
Expand All @@ -45,8 +43,7 @@ __asm__(
".functype __wasm_set_tls_base (i32) -> ()\n"
".import_module __wasm_set_tls_base, \"env\"\n"
".import_name __wasm_set_tls_base, \"__wasm_set_tls_base\"\n"
#endif
);

);

#endif
15 changes: 0 additions & 15 deletions libc-bottom-half/headers/public/__errno.h

This file was deleted.

26 changes: 19 additions & 7 deletions libc-bottom-half/sources/__cabi_realloc_wrapper.S
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@

// For coop-threads builds we need to configure TLS to the initial TLS
// value for all tasks/etc, so import those symbols here.
//
// Note that this saves and restores context slot 1 itself rather than this
// module's TLS base. When several modules are linked together the slot holds an
// array of per-module base pointers, so clobbering just libc's entry would both
// leave the other modules pointing at the wrong TLS and scribble through
// whatever stale pointer the slot happened to contain.
#ifdef __wasi_cooperative_threads__
.functype __wasm_set_tls_base (i32) -> ()
.functype __wasm_get_tls_base () -> (i32)
.type __wasm_set_tls_base,@function
.type __wasm_get_tls_base,@function
.functype wasip3_context_set_1 (i32) -> ()
.functype wasip3_context_get_1 () -> (i32)
.functype __wasilibc_set_task_tls (i32) -> ()
.type wasip3_context_set_1,@function
.type wasip3_context_get_1,@function
.type __wasilibc_set_task_tls,@function
.import_module wasip3_context_get_1, "$root"
.import_name wasip3_context_get_1, "[context-get-1]"
.import_module wasip3_context_set_1, "$root"
.import_name wasip3_context_set_1, "[context-set-1]"
.type __init_tls_base,@global
#ifdef __PIC__
.globaltype __init_tls_base, i32
Expand Down Expand Up @@ -68,7 +80,7 @@ cabi_realloc:
call __wasm_get_stack_pointer
local.set 4
#ifdef __wasi_cooperative_threads__
call __wasm_get_tls_base
call wasip3_context_get_1
local.set 5
#endif

Expand All @@ -85,7 +97,7 @@ cabi_realloc:
// Next up configure TLS. This is only required in coop-threads builds.
#ifdef __wasi_cooperative_threads__
global.get __init_tls_base
call __wasm_set_tls_base
call __wasilibc_set_task_tls
#endif

// And finally forward to the actual implementation of `cabi_realloc`
Expand All @@ -100,7 +112,7 @@ cabi_realloc:
call __wasm_set_stack_pointer
#ifdef __wasi_cooperative_threads__
local.get 5
call __wasm_set_tls_base
call wasip3_context_set_1
#endif

end_function
Expand Down
5 changes: 5 additions & 0 deletions libc-bottom-half/sources/__errno_location.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#include <errno.h>

// `<errno.h>` may define `errno` as a call to this function, so reach the
// underlying thread-local directly here to avoid recursing into ourselves.
#undef errno
extern _Thread_local int errno;

int *__errno_location(void) { return &errno; }
12 changes: 6 additions & 6 deletions libc-bottom-half/sources/__wasm_init_task.S
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#else
.globaltype __init_tls_base, i32, immutable
#endif
.functype __wasm_set_tls_base (i32) -> ()
.functype __wasilibc_set_task_tls (i32) -> ()
#endif


Expand All @@ -30,10 +30,10 @@
// the same TLS values are used across all tasks, which seem reasonable-enough
// for now.
//
// Note that configuring TLS is only required in coop threads builds since when
// coop-threads are disabled TLS is routed through a global in wasi-libc which
// needs no extra handling (unlike coop threads where TLS routes through
// `context.{get,set}` which needs setting up).
// Note that configuring TLS is only required in coop threads builds. When
// threads are disabled TLS is handled by `wit-component`'s synthesis of
// functions. With coop threads though delegation goes into C in wasi-libc
// itself to do a bit of detection logic.
.globl __wasm_init_task
.export_name __wasm_init_task, __wasm_init_task
.type __wasm_init_task,@function
Expand All @@ -45,7 +45,7 @@ __wasm_init_task:

#ifdef __wasi_cooperative_threads__
global.get __init_tls_base
call __wasm_set_tls_base
call __wasilibc_set_task_tls
#endif
end_function

Expand Down
1 change: 0 additions & 1 deletion libc-bottom-half/sources/isatty.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <__errno.h>
#include <__function___isatty.h>
#include <errno.h>
#include <features.h>
Expand Down
7 changes: 7 additions & 0 deletions libc-bottom-half/sources/netdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ static_assert(SOCK_STREAM == __WASI_FILETYPE_SOCKET_STREAM, "value mismatch");
NETWORK_ERROR_CODE_PERMANENT_RESOLVER_FAILURE
#endif

// `<netdb.h>` may define `h_errno` as a call to `__h_errno_location`, so undo
// that here where the thread-local it ultimately refers to is defined.
#undef h_errno
_Thread_local int h_errno = 0;

#if !defined(__wasip1__) && !defined(__wasip2__)
int *__h_errno_location(void) { return &h_errno; }
#endif

static struct servent global_serv = {0};

static int map_error(ip_name_lookup_error_code_t *error) {
Expand Down
Loading
Loading