Skip to content

DRAFT: Remove uses of preview1 component adapter #613

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

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ NM ?= $(patsubst %clang,%llvm-nm,$(filter-out ccache sccache,$(CC)))
ifeq ($(origin AR), default)
AR = $(patsubst %clang,%llvm-ar,$(filter-out ccache sccache,$(CC)))
endif
ifeq ($(DEBUG), true)
EXTRA_CFLAGS ?= -O0 -g
else
EXTRA_CFLAGS ?= -O2 -DNDEBUG
endif
# The directory where we build the sysroot.
SYSROOT ?= $(CURDIR)/sysroot
# A directory to install to for "make install".
Expand Down Expand Up @@ -924,8 +928,10 @@ check-symbols: $(STARTUP_FILES) libc
|grep ' U ' |sed 's/.* U //' |LC_ALL=C sort |uniq); do \
grep -q '\<'$$undef_sym'\>' "$(DEFINED_SYMBOLS)" || echo $$undef_sym; \
done | grep -E -v "^__mul|__memory_base|__indirect_function_table|__tls_base" > "$(UNDEFINED_SYMBOLS)"
ifneq ($(WASI_SNAPSHOT), p2)
grep '^_*imported_wasi_' "$(UNDEFINED_SYMBOLS)" \
> "$(SYSROOT_LIB)/libc.imports"
endif

#
# Generate a test file that includes all public C header files.
Expand Down Expand Up @@ -1056,7 +1062,7 @@ bindings: $(BINDING_WORK_DIR)/wasi-cli $(BINDING_WORK_DIR)/wit-bindgen
--autodrop-borrows yes \
--rename-world wasip2 \
--type-section-suffix __wasi_libc \
--world wasi:cli/imports@0.2.0 \
--world wasi:cli/command@0.2.0 \
--rename wasi:clocks/monotonic-clock@0.2.0=monotonic_clock \
--rename wasi:clocks/wall-clock@0.2.0=wall_clock \
--rename wasi:filesystem/preopens@0.2.0=filesystem_preopens \
Expand Down
52 changes: 6 additions & 46 deletions expected/wasm32-wasip2/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -287,51 +287,6 @@ __uflow
__unlist_locked_file
__uselocale
__utc
__wasi_args_get
__wasi_args_sizes_get
__wasi_clock_res_get
__wasi_clock_time_get
__wasi_environ_get
__wasi_environ_sizes_get
__wasi_fd_advise
__wasi_fd_allocate
__wasi_fd_close
__wasi_fd_datasync
__wasi_fd_fdstat_get
__wasi_fd_fdstat_set_flags
__wasi_fd_fdstat_set_rights
__wasi_fd_filestat_get
__wasi_fd_filestat_set_size
__wasi_fd_filestat_set_times
__wasi_fd_pread
__wasi_fd_prestat_dir_name
__wasi_fd_prestat_get
__wasi_fd_pwrite
__wasi_fd_read
__wasi_fd_readdir
__wasi_fd_renumber
__wasi_fd_seek
__wasi_fd_sync
__wasi_fd_tell
__wasi_fd_write
__wasi_path_create_directory
__wasi_path_filestat_get
__wasi_path_filestat_set_times
__wasi_path_link
__wasi_path_open
__wasi_path_readlink
__wasi_path_remove_directory
__wasi_path_rename
__wasi_path_symlink
__wasi_path_unlink_file
__wasi_poll_oneoff
__wasi_proc_exit
__wasi_random_get
__wasi_sched_yield
__wasi_sock_accept
__wasi_sock_recv
__wasi_sock_send
__wasi_sock_shutdown
__wasi_sockets_services_db
__wasi_sockets_utils__any_addr
__wasi_sockets_utils__borrow_network
Expand Down Expand Up @@ -392,6 +347,7 @@ __wasilibc_tell
__wasilibc_unlinkat
__wasilibc_utimens
__wasm_call_dtors
__wasm_export_exports_wasi_cli_run_run
__wcscoll_l
__wcsftime_l
__wcsxfrm_l
Expand Down Expand Up @@ -567,6 +523,7 @@ ctime_r
descriptor_table_get_ref
descriptor_table_insert
descriptor_table_remove
descriptor_table_update
difftime
dirfd
dirname
Expand All @@ -575,6 +532,8 @@ dprintf
drand48
drem
dremf
drop_directory_stream
drop_file_handle
drop_tcp_socket
drop_udp_socket
drop_udp_socket_streams
Expand Down Expand Up @@ -609,6 +568,8 @@ explicit_bzero
expm1
expm1f
expm1l
exports_wasi_cli_run_result_void_void_free
exports_wasi_cli_run_run
fabs
fabsf
fabsl
Expand Down Expand Up @@ -1247,7 +1208,6 @@ scalbnl
scandir
scandirat
scanf
sched_yield
seed48
seekdir
select
Expand Down
47 changes: 0 additions & 47 deletions expected/wasm32-wasip2/undefined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,6 @@ __getf2
__gttf2
__heap_base
__heap_end
__imported_wasi_snapshot_preview1_args_get
__imported_wasi_snapshot_preview1_args_sizes_get
__imported_wasi_snapshot_preview1_clock_res_get
__imported_wasi_snapshot_preview1_clock_time_get
__imported_wasi_snapshot_preview1_environ_get
__imported_wasi_snapshot_preview1_environ_sizes_get
__imported_wasi_snapshot_preview1_fd_advise
__imported_wasi_snapshot_preview1_fd_allocate
__imported_wasi_snapshot_preview1_fd_close
__imported_wasi_snapshot_preview1_fd_datasync
__imported_wasi_snapshot_preview1_fd_fdstat_get
__imported_wasi_snapshot_preview1_fd_fdstat_set_flags
__imported_wasi_snapshot_preview1_fd_fdstat_set_rights
__imported_wasi_snapshot_preview1_fd_filestat_get
__imported_wasi_snapshot_preview1_fd_filestat_set_size
__imported_wasi_snapshot_preview1_fd_filestat_set_times
__imported_wasi_snapshot_preview1_fd_pread
__imported_wasi_snapshot_preview1_fd_prestat_dir_name
__imported_wasi_snapshot_preview1_fd_prestat_get
__imported_wasi_snapshot_preview1_fd_pwrite
__imported_wasi_snapshot_preview1_fd_read
__imported_wasi_snapshot_preview1_fd_readdir
__imported_wasi_snapshot_preview1_fd_renumber
__imported_wasi_snapshot_preview1_fd_seek
__imported_wasi_snapshot_preview1_fd_sync
__imported_wasi_snapshot_preview1_fd_tell
__imported_wasi_snapshot_preview1_fd_write
__imported_wasi_snapshot_preview1_path_create_directory
__imported_wasi_snapshot_preview1_path_filestat_get
__imported_wasi_snapshot_preview1_path_filestat_set_times
__imported_wasi_snapshot_preview1_path_link
__imported_wasi_snapshot_preview1_path_open
__imported_wasi_snapshot_preview1_path_readlink
__imported_wasi_snapshot_preview1_path_remove_directory
__imported_wasi_snapshot_preview1_path_rename
__imported_wasi_snapshot_preview1_path_symlink
__imported_wasi_snapshot_preview1_path_unlink_file
__imported_wasi_snapshot_preview1_poll_oneoff
__imported_wasi_snapshot_preview1_proc_exit
__imported_wasi_snapshot_preview1_random_get
__imported_wasi_snapshot_preview1_sched_yield
__imported_wasi_snapshot_preview1_sock_accept
__imported_wasi_snapshot_preview1_sock_recv
__imported_wasi_snapshot_preview1_sock_send
__imported_wasi_snapshot_preview1_sock_shutdown
__letf2
__lttf2
__netf2
Expand All @@ -65,8 +20,6 @@ __subtf3
__trunctfdf2
__trunctfsf2
__unordtf2
__wasi_preview1_adapter_close_badfd
__wasi_preview1_adapter_open_badfd
__wasm_call_ctors
__wasm_import_environment_get_arguments
__wasm_import_environment_get_environment
Expand Down
31 changes: 31 additions & 0 deletions libc-bottom-half/clocks/clock.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
#define _WASI_EMULATED_PROCESS_CLOCKS
#include <time.h>
#ifdef __wasilibc_use_wasip2
#include <wasi/wasip2.h>
#else
#include <wasi/api.h>
#endif
#include <common/time.h>

_Static_assert(
CLOCKS_PER_SEC == NSEC_PER_SEC,
"This implementation assumes that `clock` is in nanoseconds"
);

#ifdef __wasilibc_use_wasip2
// Snapshot of the monotonic clock at the start of the program.
static wall_clock_datetime_t start;

// Use a priority of 10 to run fairly early in the implementation-reserved
// constructor priority range.
__attribute__((constructor(10)))
static void init(void) {
wall_clock_now(&start);
}

// Define the libc symbol as `__clock` so that we can reliably call it
// from elsewhere in libc.
clock_t __clock(void) {
// Use `MONOTONIC` instead of `PROCESS_CPUTIME_ID` since WASI doesn't have
// an inherent concept of a process. Note that this means we'll incorrectly
// include time from other processes, so this function is only declared by
// the headers if `_WASI_EMULATED_PROCESS_CLOCKS` is defined.
wall_clock_datetime_t now;
wall_clock_now(&now);
now.seconds -= start.seconds;
now.nanoseconds -= start.nanoseconds;
return (now.seconds + (now.nanoseconds * NSEC_PER_SEC));
}
#else

// Snapshot of the monotonic clock at the start of the program.
static __wasi_timestamp_t start;

Expand All @@ -29,6 +59,7 @@ clock_t __clock(void) {
(void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &now);
return now - start;
}
#endif

// Define a user-visible alias as a weak symbol.
__attribute__((__weak__, __alias__("__clock")))
Expand Down
11 changes: 11 additions & 0 deletions libc-bottom-half/clocks/getrusage.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
#include <sys/resource.h>
#include <errno.h>
#include <time.h>
#ifdef __wasilibc_use_wasip2
#include <wasi/wasip2.h>
#else
#include <wasi/api.h>
#endif
#include <common/time.h>

// `clock` is a weak symbol so that application code can override it.
Expand All @@ -12,10 +16,17 @@ clock_t __clock(void);
int getrusage(int who, struct rusage *r_usage) {
switch (who) {
case RUSAGE_SELF: {
#ifdef __wasilibc_use_wasip2
clock_t usertime = __clock();
*r_usage = (struct rusage) {
.ru_utime = instant_to_timeval(usertime)
};
#else
__wasi_timestamp_t usertime = __clock();
*r_usage = (struct rusage) {
.ru_utime = timestamp_to_timeval(usertime)
};
#endif
return 0;
}
case RUSAGE_CHILDREN:
Expand Down
16 changes: 16 additions & 0 deletions libc-bottom-half/clocks/times.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#define _WASI_EMULATED_PROCESS_CLOCKS
#include <time.h>
#include <sys/times.h>
#ifdef __wasilibc_use_wasip2
#include <wasi/wasip2.h>
#else
#include <wasi/api.h>
#endif
#include <common/time.h>

_Static_assert(
Expand All @@ -14,6 +18,17 @@ _Static_assert(
clock_t __clock(void);

clock_t times(struct tms *buffer) {
#ifdef __wasilibc_use_wasip2
clock_t user = __clock();
*buffer = (struct tms){
.tms_utime = user,
// WASI doesn't provide a way to spawn a new process, so always 0.
.tms_cutime = 0
};

monotonic_clock_instant_t realtime = monotonic_clock_now();
#else

__wasi_timestamp_t user = __clock();
*buffer = (struct tms){
.tms_utime = user,
Expand All @@ -23,5 +38,6 @@ clock_t times(struct tms *buffer) {

__wasi_timestamp_t realtime = 0;
(void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &realtime);
#endif
return realtime;
}
Loading
Loading