Skip to content
Merged
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
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 \
Expand Down
48 changes: 3 additions & 45 deletions expected/wasm32-wasip2/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
45 changes: 0 additions & 45 deletions expected/wasm32-wasip2/undefined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions libc-bottom-half/cloudlibc/src/libc/sched/sched_yield.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
//
// SPDX-License-Identifier: BSD-2-Clause

#ifndef __wasilibc_use_wasip2
#include <wasi/api.h>
#endif
#include <errno.h>
#include <sched.h>

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;
}

12 changes: 12 additions & 0 deletions libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
{
Expand Down Expand Up @@ -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);
Expand All @@ -136,6 +147,7 @@ int ioctl(int fildes, int request, ...) {
return -1;
}
return 0;
#endif
}
default:
// Invalid request.
Expand Down
2 changes: 1 addition & 1 deletion libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions libc-bottom-half/headers/public/wasi/wasip2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions libc-bottom-half/sources/__main_void.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifdef __wasilibc_use_wasip2
#include <wasi/wasip2.h>
#include <wasi/libc-environ.h>
#else
#include <wasi/api.h>
#endif
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions libc-bottom-half/sources/__wasilibc_fd_renumber.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 7 additions & 0 deletions libc-bottom-half/sources/__wasilibc_real.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* must be modified to change this file.
*/

#ifndef __wasilibc_use_wasip2

#include <wasi/api.h>
#include <string.h>

Expand Down Expand Up @@ -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
20 changes: 19 additions & 1 deletion libc-bottom-half/sources/wasip2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Binary file modified libc-bottom-half/sources/wasip2_component_type.o
Binary file not shown.
7 changes: 7 additions & 0 deletions libc-top-half/sources/arc4random.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#ifdef __wasilibc_use_wasip2
#include <wasi/libc.h>
#endif

void arc4random_buf(void* buffer, size_t len)
{
Expand All @@ -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) {
Expand Down
9 changes: 2 additions & 7 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down