diff --git a/Makefile b/Makefile index c0837ddbf..265a312e3 100644 --- a/Makefile +++ b/Makefile @@ -927,8 +927,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. @@ -1059,7 +1061,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 \ diff --git a/expected/wasm32-wasip2/defined-symbols.txt b/expected/wasm32-wasip2/defined-symbols.txt index a7f87669d..17d12a996 100644 --- a/expected/wasm32-wasip2/defined-symbols.txt +++ b/expected/wasm32-wasip2/defined-symbols.txt @@ -288,52 +288,7 @@ __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_init_tp -__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 @@ -394,6 +349,7 @@ __wasilibc_tell __wasilibc_unlinkat __wasilibc_utimens __wasm_call_dtors +__wasm_export_exports_wasi_cli_run_run __wcscoll_l __wcsftime_l __wcsxfrm_l @@ -614,6 +570,8 @@ explicit_bzero expm1 expm1f expm1l +exports_wasi_cli_run_result_void_void_free +exports_wasi_cli_run_run fabs fabsf fabsl diff --git a/expected/wasm32-wasip2/undefined-symbols.txt b/expected/wasm32-wasip2/undefined-symbols.txt index 746d3df8b..8cdd2c7aa 100644 --- a/expected/wasm32-wasip2/undefined-symbols.txt +++ b/expected/wasm32-wasip2/undefined-symbols.txt @@ -14,51 +14,6 @@ __global_base __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 diff --git a/libc-bottom-half/cloudlibc/src/libc/sched/sched_yield.c b/libc-bottom-half/cloudlibc/src/libc/sched/sched_yield.c index 6100ea561..06f47edb6 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sched/sched_yield.c +++ b/libc-bottom-half/cloudlibc/src/libc/sched/sched_yield.c @@ -2,15 +2,20 @@ // // SPDX-License-Identifier: BSD-2-Clause +#ifndef __wasilibc_use_wasip2 #include +#endif #include #include int sched_yield(void) { +#ifndef __wasilibc_use_wasip2 __wasi_errno_t error = __wasi_sched_yield(); if (error != 0) { errno = error; return -1; } +#endif return 0; } + diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c b/libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c index 74dd0f89a..f506b4499 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c @@ -68,6 +68,11 @@ int ioctl(int fildes, int request, ...) { switch (request) { case FIONREAD: { +#ifdef __wasilibc_use_wasip2 + // wasip2 doesn't support this operation + errno = ENOTSUP; + return -1; +#else // Poll the file descriptor to determine how many bytes can be read. __wasi_subscription_t subscriptions[2] = { { @@ -110,8 +115,14 @@ int ioctl(int fildes, int request, ...) { // No data available for reading. *result = 0; return 0; +#endif } case FIONBIO: { +#ifdef __wasilibc_use_wasip2 + // wasip2 doesn't support setting the non-blocking flag + errno = ENOTSUP; + return -1; +#else // Obtain the current file descriptor flags. __wasi_fdstat_t fds; __wasi_errno_t error = __wasi_fd_fdstat_get(fildes, &fds); @@ -136,6 +147,7 @@ int ioctl(int fildes, int request, ...) { return -1; } return 0; +#endif } default: // Invalid request. diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c b/libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c index 94d93f2b8..188a2f06a 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c @@ -29,10 +29,10 @@ ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { errno = EINVAL; return -1; } - size_t bytes_read; #ifdef __wasilibc_use_wasip2 return preadv(fildes, iov, iovcnt, 0); #else + size_t bytes_read; __wasi_errno_t error = __wasi_fd_read( fildes, (const __wasi_iovec_t *)iov, iovcnt, &bytes_read); if (error != 0) { diff --git a/libc-bottom-half/headers/public/wasi/wasip2.h b/libc-bottom-half/headers/public/wasi/wasip2.h index df21eab4d..2428f7e79 100644 --- a/libc-bottom-half/headers/public/wasi/wasip2.h +++ b/libc-bottom-half/headers/public/wasi/wasip2.h @@ -1066,6 +1066,10 @@ typedef struct { uint64_t f1; } wasip2_tuple2_u64_u64_t; +typedef struct { + bool is_err; +} exports_wasi_cli_run_result_void_void_t; + // Imported Functions from `wasi:cli/environment@0.2.0` // Get the POSIX-style environment variables. // @@ -2254,6 +2258,9 @@ extern uint64_t random_insecure_get_insecure_random_u64(void); // protection. extern void random_insecure_seed_insecure_seed(wasip2_tuple2_u64_u64_t *ret); +// Exported Functions from `wasi:cli/run@0.2.0` +bool exports_wasi_cli_run_run(void); + // Helper Functions void wasip2_tuple2_string_string_free(wasip2_tuple2_string_string_t *ptr); @@ -2472,6 +2479,8 @@ void ip_name_lookup_option_ip_address_free(ip_name_lookup_option_ip_address_t *p void ip_name_lookup_result_option_ip_address_error_code_free(ip_name_lookup_result_option_ip_address_error_code_t *ptr); +void exports_wasi_cli_run_result_void_void_free(exports_wasi_cli_run_result_void_void_t *ptr); + // Transfers ownership of `s` into the string `ret` void wasip2_string_set(wasip2_string_t *ret, char*s); diff --git a/libc-bottom-half/sources/__main_void.c b/libc-bottom-half/sources/__main_void.c index c40241fa3..9b6e829c5 100644 --- a/libc-bottom-half/sources/__main_void.c +++ b/libc-bottom-half/sources/__main_void.c @@ -1,5 +1,6 @@ #ifdef __wasilibc_use_wasip2 #include +#include #else #include #endif @@ -108,3 +109,11 @@ int __main_void(void) { return __main_argc_argv(argc, argv); #endif } + +#ifdef __wasilibc_use_wasip2 +bool exports_wasi_cli_run_run(void) { + // TODO: this is supposed to be unnecessary, but functional/env.c fails without it + __wasilibc_initialize_environ(); + return __main_void() == 0; +} +#endif diff --git a/libc-bottom-half/sources/__wasilibc_fd_renumber.c b/libc-bottom-half/sources/__wasilibc_fd_renumber.c index d5c30314b..0d0e4e646 100644 --- a/libc-bottom-half/sources/__wasilibc_fd_renumber.c +++ b/libc-bottom-half/sources/__wasilibc_fd_renumber.c @@ -137,13 +137,13 @@ int close(int fd) { return 0; } -#endif // __wasilibc_use_wasip2 - +#else __wasi_errno_t error = __wasi_fd_close(fd); if (error != 0) { errno = error; return -1; } +#endif // __wasilibc_use_wasip2 return 0; } diff --git a/libc-bottom-half/sources/__wasilibc_real.c b/libc-bottom-half/sources/__wasilibc_real.c index 186de0183..03c9874c8 100644 --- a/libc-bottom-half/sources/__wasilibc_real.c +++ b/libc-bottom-half/sources/__wasilibc_real.c @@ -10,6 +10,8 @@ * must be modified to change this file. */ +#ifndef __wasilibc_use_wasip2 + #include #include @@ -669,3 +671,8 @@ int32_t __wasi_thread_spawn(void* start_arg) { return __imported_wasi_thread_spawn((int32_t) start_arg); } #endif + +#else +// Suppress the "no symbols" linker error +static void __wasi_noop() {} +#endif // __wasilibc_use_wasip2 diff --git a/libc-bottom-half/sources/wasip2.c b/libc-bottom-half/sources/wasip2.c index b81d58134..cd8e2d28b 100644 --- a/libc-bottom-half/sources/wasip2.c +++ b/libc-bottom-half/sources/wasip2.c @@ -1013,6 +1013,11 @@ void ip_name_lookup_result_option_ip_address_error_code_free(ip_name_lookup_resu } } +void exports_wasi_cli_run_result_void_void_free(exports_wasi_cli_run_result_void_void_t *ptr) { + if (!ptr->is_err) { + } +} + void wasip2_string_set(wasip2_string_t *ret, char*s) { ret->ptr = (uint8_t*) s; ret->len = strlen(s); @@ -1071,7 +1076,7 @@ bool environment_initial_cwd(wasip2_string_t *ret) { return option.is_some; } -_Noreturn void exit_exit(exit_result_void_void_t *status) { +void exit_exit(exit_result_void_void_t *status) { int32_t result; if ((*status).is_err) { result = 1; @@ -4323,6 +4328,19 @@ void random_insecure_seed_insecure_seed(wasip2_tuple2_u64_u64_t *ret) { }; } +__attribute__((__export_name__("wasi:cli/run@0.2.0#run"))) +int32_t __wasm_export_exports_wasi_cli_run_run(void) { + exports_wasi_cli_run_result_void_void_t ret; + ret.is_err = !exports_wasi_cli_run_run(); + int32_t result; + if ((ret).is_err) { + result = 1; + } else { + result = 0; + } + return result; +} + extern void __component_type_object_force_link_wasip2(void); void __component_type_object_force_link_wasip2_public_use_in_this_compilation_unit(void) { __component_type_object_force_link_wasip2(); diff --git a/libc-bottom-half/sources/wasip2_component_type.o b/libc-bottom-half/sources/wasip2_component_type.o index 0548e3e97..ec0b2468b 100644 Binary files a/libc-bottom-half/sources/wasip2_component_type.o and b/libc-bottom-half/sources/wasip2_component_type.o differ diff --git a/libc-top-half/sources/arc4random.c b/libc-top-half/sources/arc4random.c index b86ec03aa..47681703d 100644 --- a/libc-top-half/sources/arc4random.c +++ b/libc-top-half/sources/arc4random.c @@ -3,6 +3,9 @@ #include #include #include +#ifdef __wasilibc_use_wasip2 +#include +#endif void arc4random_buf(void* buffer, size_t len) { @@ -13,7 +16,11 @@ void arc4random_buf(void* buffer, size_t len) // We therefore effectively expect that `__wasi_random_get` is "fast", // presumably using a PRNG-style implementation rather than a slower // raw-entropy-style implementation. +#ifdef __wasilibc_use_wasip2 + int r = __wasilibc_random(buffer, len); +#else int r = __wasi_random_get(buffer, len); +#endif // `__wasi_random_get` should always succeed. if (r != __WASI_ERRNO_SUCCESS) { diff --git a/test/Makefile b/test/Makefile index 04518b0bd..40f7191fb 100644 --- a/test/Makefile +++ b/test/Makefile @@ -49,8 +49,6 @@ WASMTIME_URL ?= https://github.com/bytecodealliance/wasmtime/releases/download/v WASMTIME = $(abspath $(DOWNDIR)/$(shell basename $(WASMTIME_URL) .tar.xz)/wasmtime) WASM_TOOLS_URL ?= https://github.com/bytecodealliance/wasm-tools/releases/download/v1.224.0/wasm-tools-1.224.0-$(ARCH)-linux.tar.gz WASM_TOOLS = $(DOWNDIR)/$(shell basename $(WASM_TOOLS_URL) .tar.gz)/wasm-tools -ADAPTER_URL ?= https://github.com/bytecodealliance/wasmtime/releases/download/v29.0.1/wasi_snapshot_preview1.command.wasm -ADAPTER = $(DOWNDIR)/wasi_snapshot_preview1.command.wasm $(DOWNDIR): @mkdir -p $@ @@ -66,13 +64,10 @@ $(WASM_TOOLS): | $(DOWNDIR) wget --no-clobber --directory-prefix=$(DOWNDIR) $(WASM_TOOLS_URL) tar --extract --file=$(DOWNDIR)/$(shell basename $(WASM_TOOLS_URL)) --directory=$(DOWNDIR)/ -$(ADAPTER): | $(DOWNDIR) - wget --no-clobber --directory-prefix=$(DOWNDIR) $(ADAPTER_URL) - # Target to download all necessary dependencies. TO_DOWNLOAD = $(LIBC_TEST) $(WASMTIME) ifeq ($(TARGET_TRIPLE), wasm32-wasip2) -TO_DOWNLOAD += $(ADAPTER) $(WASM_TOOLS) +TO_DOWNLOAD += $(WASM_TOOLS) endif DOWNLOADED := $(DOWNDIR)/downloaded.stamp $(DOWNLOADED): $(TO_DOWNLOAD) @@ -171,7 +166,7 @@ $(OBJDIR)/%.core.wasm: $(OBJDIR)/%.wasm.o $(INFRA_WASM_OBJS) | $(BUILTINS_STAMP) # For wasip2, we include an additional step to wrap up the core module into # a component. $(OBJDIR)/%.component.wasm: $(OBJDIR)/%.core.wasm - $(WASM_TOOLS) component new --adapt $(ADAPTER) $< -o $@ + $(WASM_TOOLS) component new $< -o $@ # Compile each selected test using Clang. Note that failures here are likely # due to a missing `libclang_rt.builtins-wasm32.a` in the Clang lib directory.