From 54980297ffb9f9ea1e28ca66c338e911bea648de Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Wed, 30 Sep 2026 10:46:53 -0400 Subject: [PATCH 01/96] Big ugly WIP --- .gitattributes | 2 + CMakeLists.txt | 10 +- cmake/bindings.cmake | 31 ++-- cmake/check-symbols.cmake | 2 + libc-bottom-half/CMakeLists.txt | 136 ++++++++++-------- libc-bottom-half/clocks/getrusage.c | 2 +- .../cloudlibc/src/common/errors.h | 2 +- libc-bottom-half/cloudlibc/src/common/time.h | 8 +- .../cloudlibc/src/libc/fcntl/fcntl.c | 4 +- .../cloudlibc/src/libc/stdlib/_Exit.c | 2 +- .../cloudlibc/src/libc/sys/ioctl/ioctl.c | 4 +- .../cloudlibc/src/libc/sys/stat/stat_impl.h | 2 +- .../cloudlibc/src/libc/sys/uio/readv.c | 2 +- .../cloudlibc/src/libc/time/clock_gettime.c | 2 +- .../cloudlibc/src/libc/time/time.c | 2 +- .../cloudlibc/src/libc/unistd/lseek.c | 2 +- .../headers/public/__header_sys_socket.h | 2 +- .../headers/public/wasi/__generated_webp2.h | 15 ++ libc-bottom-half/headers/public/wasi/api.h | 2 + .../headers/public/wasi/version.h.in | 9 +- libc-bottom-half/headers/public/wasi/webp2.h | 10 ++ libc-bottom-half/sources/webp2.c | 24 ++++ .../sources/webp2_component_type.o | Bin 0 -> 269 bytes libc-bottom-half/sources/webp2_filesystem.c | 1 + web/p2/wit/wasi-libc.wit | 3 + 25 files changed, 189 insertions(+), 90 deletions(-) create mode 100644 libc-bottom-half/headers/public/wasi/__generated_webp2.h create mode 100644 libc-bottom-half/headers/public/wasi/webp2.h create mode 100644 libc-bottom-half/sources/webp2.c create mode 100644 libc-bottom-half/sources/webp2_component_type.o create mode 100644 libc-bottom-half/sources/webp2_filesystem.c create mode 100644 web/p2/wit/wasi-libc.wit diff --git a/.gitattributes b/.gitattributes index 17416b3b0..583dabf79 100644 --- a/.gitattributes +++ b/.gitattributes @@ -10,5 +10,7 @@ expected linguist-generated # Flag some wit-bindgen-generated files checked into the repo as generated __generated_wasip2.h linguist-generated __generated_wasip3.h linguist-generated +__generated_webp2.h linguist-generated libc-bottom-half/sources/wasip2.c linguist-generated libc-bottom-half/sources/wasip3.c linguist-generated +libc-bottom-half/sources/webp2.c linguist-generated diff --git a/CMakeLists.txt b/CMakeLists.txt index e3dcfd96a..e1916f216 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,8 +79,11 @@ elseif(TARGET_TRIPLE MATCHES "-wasip2$") elseif(TARGET_TRIPLE MATCHES "-wasip3$") set(WASI p3) set(SHARED ON) +elseif(TARGET_TRIPLE MATCHES "-webp2") + set(WEB p2) + set(SHARED ON) else() - message(FATAL_ERROR "Unknown WASI target triple: ${TARGET_TRIPLE}") + message(FATAL_ERROR "Unknown target triple: ${TARGET_TRIPLE}") endif() if(BUILD_SHARED) @@ -149,12 +152,15 @@ elseif(WASI STREQUAL "p2") set(__wasip2__ ON) elseif(WASI STREQUAL "p3") set(__wasip3__ ON) +elseif(WEB STREQUAL "p2") + set(__webp2__ ON) else() message(FATAL_ERROR "Unknown WASI version: ${WASI}") endif() set(wasip2-version 0.2.12) set(wasip3-version 0.3.0) +set(webp2-version 0.2.0) # ============================================================================= # Detection of a libcall thread context for wasip3 @@ -251,7 +257,7 @@ check_c_compiler_flag(-fdefer-ts HAVE_FDEFER_TS) if(HAVE_FDEFER_TS) add_compile_options(-fdefer-ts) elseif(NOT WASI STREQUAL "p1") - message(FATAL_ERROR "wasip2/wasip3 require clang 22 or later for `-fdefer-ts`") + message(FATAL_ERROR "wasip2/wasip3/webp2 require clang 22 or later for `-fdefer-ts`") endif() # ============================================================================= diff --git a/cmake/bindings.cmake b/cmake/bindings.cmake index edc5d02ec..fb8250ef0 100644 --- a/cmake/bindings.cmake +++ b/cmake/bindings.cmake @@ -38,7 +38,7 @@ endif() set(bottom_half "${CMAKE_SOURCE_DIR}/libc-bottom-half") add_custom_target( - bindings-p2 + bindings-wasip2 COMMAND ${wit_bindgen} c --autodrop-borrows yes @@ -80,7 +80,7 @@ add_custom_target( ) add_custom_target( - bindings-p3 + bindings-wasip3 COMMAND ${wit_bindgen} c --autodrop-borrows yes @@ -138,7 +138,21 @@ add_custom_target( DEPENDS wit-bindgen wasip3-wits ) -add_custom_target(bindings DEPENDS bindings-p2 bindings-p3) +add_custom_target( + bindings-webp2 + COMMAND + ${wit_bindgen} c + --autodrop-borrows yes + --rename-world webp2 + --type-section-suffix __wasi_libc + ${CMAKE_SOURCE_DIR}/web/p2/wit + COMMAND cmake -E copy webp2.h ${bottom_half}/headers/public/wasi/__generated_webp2.h + COMMAND cmake -E copy webp2_component_type.o ${bottom_half}/sources + COMMAND cmake -E copy webp2.c ${bottom_half}/sources + DEPENDS wit-bindgen +) + +add_custom_target(bindings DEPENDS bindings-wasip2 bindings-wasip3 bindings-webp2) if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin") set(SED_INPLACE_ARGS -i '') @@ -149,13 +163,14 @@ endif() function(wit_bindgen_edit p) add_custom_target( bindings-${p}-edit - COMMAND sed ${SED_INPLACE_ARGS} "'s_#include .wasi${p}\.h._#include \"wasi/wasi${p}.h\"_'" ${bottom_half}/sources/wasi${p}.c - COMMAND sed ${SED_INPLACE_ARGS} "s/extern void exit_exit/_Noreturn extern void exit_exit/" ${bottom_half}/headers/public/wasi/__generated_wasi${p}.h - COMMAND sed ${SED_INPLACE_ARGS} "s/extern void __wasm_import_exit_exit/_Noreturn extern void __wasm_import_exit_exit/" ${bottom_half}/sources/wasi${p}.c + COMMAND sed ${SED_INPLACE_ARGS} "'s_#include .${p}\.h._#include \"wasi/${p}.h\"_'" ${bottom_half}/sources/${p}.c + COMMAND sed ${SED_INPLACE_ARGS} "s/extern void exit_exit/_Noreturn extern void exit_exit/" ${bottom_half}/headers/public/wasi/__generated_${p}.h + COMMAND sed ${SED_INPLACE_ARGS} "s/extern void __wasm_import_exit_exit/_Noreturn extern void __wasm_import_exit_exit/" ${bottom_half}/sources/${p}.c DEPENDS bindings-${p} ) add_dependencies(bindings bindings-${p}-edit) endfunction() -wit_bindgen_edit(p2) -wit_bindgen_edit(p3) +wit_bindgen_edit(wasip2) +wit_bindgen_edit(wasip3) +wit_bindgen_edit(webp2) diff --git a/cmake/check-symbols.cmake b/cmake/check-symbols.cmake index dfa35c17d..1430eb4d2 100644 --- a/cmake/check-symbols.cmake +++ b/cmake/check-symbols.cmake @@ -36,6 +36,8 @@ elseif(WASI STREQUAL "p3") else() set(expected "${CMAKE_SOURCE_DIR}/expected/wasm32-wasip3") endif() +elseif(WEB STREQUAL "p2") + set(expected "${CMAKE_SOURCE_DIR}/expected/wasm32-webp2") else() message(FATAL_ERROR "Unknown WASI version: ${WASI}") endif() diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index 118a6b280..a36134803 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -31,6 +31,10 @@ if (NOT (WASI STREQUAL "p3")) list(REMOVE_ITEM globbed_headers headers/public/wasi/__generated_wasip3.h) endif() +if (NOT (WEB STREQUAL "p2")) + list(REMOVE_ITEM globbed_headers headers/public/wasi/__generated_webp2.h) +endif() + foreach(header ${globbed_headers}) string(REPLACE "headers/public/" "" dst ${header}) add_sysroot_header(${header} ${dst}) @@ -51,36 +55,16 @@ endforeach() # building bottom-half sources # set(bottom_half_sources - cloudlibc/src/libc/dirent/closedir.c - cloudlibc/src/libc/dirent/dirfd.c - cloudlibc/src/libc/dirent/fdclosedir.c - cloudlibc/src/libc/dirent/fdopendir.c - cloudlibc/src/libc/dirent/opendirat.c - cloudlibc/src/libc/dirent/readdir.c - cloudlibc/src/libc/dirent/rewinddir.c - cloudlibc/src/libc/dirent/scandirat.c - cloudlibc/src/libc/dirent/seekdir.c - cloudlibc/src/libc/dirent/telldir.c cloudlibc/src/libc/errno/errno.c cloudlibc/src/libc/fcntl/fcntl.c - cloudlibc/src/libc/fcntl/openat.c - cloudlibc/src/libc/fcntl/posix_fadvise.c - cloudlibc/src/libc/fcntl/posix_fallocate.c cloudlibc/src/libc/poll/poll.c cloudlibc/src/libc/sched/sched_yield.c - cloudlibc/src/libc/stdio/renameat.c cloudlibc/src/libc/stdlib/_Exit.c cloudlibc/src/libc/sys/ioctl/ioctl.c cloudlibc/src/libc/sys/select/pselect.c cloudlibc/src/libc/sys/select/select.c cloudlibc/src/libc/sys/stat/fstat.c - cloudlibc/src/libc/sys/stat/fstatat.c - cloudlibc/src/libc/sys/stat/futimens.c - cloudlibc/src/libc/sys/stat/mkdirat.c - cloudlibc/src/libc/sys/stat/utimensat.c cloudlibc/src/libc/sys/time/gettimeofday.c - cloudlibc/src/libc/sys/uio/preadv.c - cloudlibc/src/libc/sys/uio/pwritev.c cloudlibc/src/libc/sys/uio/readv.c cloudlibc/src/libc/sys/uio/writev.c cloudlibc/src/libc/time/CLOCK_MONOTONIC.c @@ -90,19 +74,11 @@ set(bottom_half_sources cloudlibc/src/libc/time/clock_nanosleep.c cloudlibc/src/libc/time/nanosleep.c cloudlibc/src/libc/time/time.c - cloudlibc/src/libc/unistd/faccessat.c - cloudlibc/src/libc/unistd/fdatasync.c - cloudlibc/src/libc/unistd/fsync.c - cloudlibc/src/libc/unistd/ftruncate.c - cloudlibc/src/libc/unistd/linkat.c cloudlibc/src/libc/unistd/lseek.c cloudlibc/src/libc/unistd/pread.c cloudlibc/src/libc/unistd/pwrite.c cloudlibc/src/libc/unistd/read.c - cloudlibc/src/libc/unistd/readlinkat.c cloudlibc/src/libc/unistd/sleep.c - cloudlibc/src/libc/unistd/symlinkat.c - cloudlibc/src/libc/unistd/unlinkat.c cloudlibc/src/libc/unistd/usleep.c cloudlibc/src/libc/unistd/write.c sources/__errno_location.c @@ -113,49 +89,69 @@ set(bottom_half_sources sources/__wasilibc_initialize_environ.c sources/__wasilibc_random.c sources/__wasilibc_real.c - sources/__wasilibc_rmdirat.c sources/__wasilibc_tell.c - sources/__wasilibc_unlinkat.c sources/abort.c - sources/at_fdcwd.c - sources/chdir.c sources/complex-builtins.c sources/dup.c sources/environ.c sources/errno.c - sources/getcwd.c sources/getentropy.c sources/isatty.c sources/math/fmin-fmax.c sources/math/math-builtins.c sources/pipe.c - sources/posix.c sources/ppoll.c - sources/preopens.c sources/reallocarray.c sources/sbrk.c - sources/truncate.c sources/__cabi_realloc_wrapper.S ) -if (WASI STREQUAL "p1") - list(APPEND bottom_half_sources - cloudlibc/src/libc/sys/socket/getsockopt.c - cloudlibc/src/libc/sys/socket/recv.c - cloudlibc/src/libc/sys/socket/send.c - cloudlibc/src/libc/sys/socket/shutdown.c - sources/accept-wasip1.c - ) -else() +if (NOT WEB STREQUAL "p2") list(APPEND bottom_half_sources + cloudlibc/src/libc/dirent/closedir.c + cloudlibc/src/libc/dirent/dirfd.c + cloudlibc/src/libc/dirent/fdclosedir.c + cloudlibc/src/libc/dirent/fdopendir.c + cloudlibc/src/libc/dirent/opendirat.c + cloudlibc/src/libc/dirent/readdir.c + cloudlibc/src/libc/dirent/rewinddir.c + cloudlibc/src/libc/dirent/scandirat.c + cloudlibc/src/libc/dirent/seekdir.c + cloudlibc/src/libc/dirent/telldir.c + cloudlibc/src/libc/fcntl/openat.c + cloudlibc/src/libc/fcntl/posix_fadvise.c + cloudlibc/src/libc/fcntl/posix_fallocate.c + cloudlibc/src/libc/stdio/renameat.c + cloudlibc/src/libc/sys/stat/fstatat.c + cloudlibc/src/libc/sys/stat/futimens.c + cloudlibc/src/libc/sys/stat/mkdirat.c + cloudlibc/src/libc/sys/stat/utimensat.c + cloudlibc/src/libc/sys/uio/preadv.c + cloudlibc/src/libc/sys/uio/pwritev.c + cloudlibc/src/libc/unistd/faccessat.c + cloudlibc/src/libc/unistd/fdatasync.c + cloudlibc/src/libc/unistd/fsync.c + cloudlibc/src/libc/unistd/ftruncate.c + cloudlibc/src/libc/unistd/linkat.c + cloudlibc/src/libc/unistd/readlinkat.c + cloudlibc/src/libc/unistd/symlinkat.c + cloudlibc/src/libc/unistd/unlinkat.c + sources/__wasilibc_rmdirat.c + sources/__wasilibc_unlinkat.c sources/accept.c + sources/at_fdcwd.c sources/bind.c + sources/chdir.c sources/connect.c sources/descriptor_table.c + sources/file.c + sources/file_utils.c + sources/getcwd.c sources/getsockpeername.c sources/listen.c - sources/file_utils.c - sources/file.c + sources/netdb.c + sources/posix.c + sources/preopens.c sources/recv.c sources/send.c sources/shutdown.c @@ -163,8 +159,18 @@ else() sources/sockets_utils.c sources/sockopt.c sources/tcp.c + sources/truncate.c sources/udp.c - sources/netdb.c + ) +endif() + +if (WASI STREQUAL "p1") + list(APPEND bottom_half_sources + cloudlibc/src/libc/sys/socket/getsockopt.c + cloudlibc/src/libc/sys/socket/recv.c + cloudlibc/src/libc/sys/socket/send.c + cloudlibc/src/libc/sys/socket/shutdown.c + sources/accept-wasip1.c ) endif() @@ -183,6 +189,12 @@ if (WASI STREQUAL "p3") ) endif() +if (WEB STREQUAL "p2") + list(APPEND bottom_half_sources + sources/webp2.c + ) +endif() + if (HAVE_WASM_LIBCALL_THREAD_CONTEXT) list(APPEND bottom_half_sources sources/__wasm_task_hook.S sources/wasip3_tls.c) @@ -240,6 +252,9 @@ if (WASI STREQUAL "p1") COMMAND ${CMAKE_COMMAND} -E copy $ ${crt_sysroot}/crt1-command.o DEPENDS crt1-command $ ) +elseif(WEB STREQUAL "p2") + # webp2: crt1-command.o is not necessary + # TODO(webp2): Set world like below? else() if (WASI STREQUAL "p2") set(world wasi:cli/command@${wasip2-version}) @@ -279,18 +294,21 @@ else() ) endif() -# Provide a plain crt1.o for toolchain compatibility, identical to -# `crt1-command.c` -add_custom_command( - OUTPUT ${crt_sysroot}/crt1.o - COMMAND ${CMAKE_COMMAND} -E copy ${crt_sysroot}/crt1-command.o ${crt_sysroot}/crt1.o - DEPENDS ${crt_sysroot}/crt1-command.o -) +set(startup_objects ${crt_sysroot}/crt1-reactor.o) +if (NOT WEB STREQUAL "p2") + # Provide a plain crt1.o for toolchain compatibility, identical to + # `crt1-command.c` + add_custom_command( + OUTPUT ${crt_sysroot}/crt1.o + COMMAND ${CMAKE_COMMAND} -E copy ${crt_sysroot}/crt1-command.o ${crt_sysroot}/crt1.o + DEPENDS ${crt_sysroot}/crt1-command.o + ) -add_custom_target(sysroot-startup-objects - DEPENDS - ${crt_sysroot}/crt1-reactor.o + list(APPEND startup_objects ${crt_sysroot}/crt1-command.o ${crt_sysroot}/crt1.o -) + ) +endif() + +add_custom_target(sysroot-startup-objects DEPENDS ${startup_objects}) add_dependencies(sysroot sysroot-startup-objects) diff --git a/libc-bottom-half/clocks/getrusage.c b/libc-bottom-half/clocks/getrusage.c index a77cf4299..166b4f70a 100644 --- a/libc-bottom-half/clocks/getrusage.c +++ b/libc-bottom-half/clocks/getrusage.c @@ -15,7 +15,7 @@ int getrusage(int who, struct rusage *r_usage) { #if defined(__wasip1__) __wasi_timestamp_t usertime = __clock(); *r_usage = (struct rusage){.ru_utime = timestamp_to_timeval(usertime)}; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) clock_t usertime = __clock(); *r_usage = (struct rusage){.ru_utime = instant_to_timeval(usertime)}; #else diff --git a/libc-bottom-half/cloudlibc/src/common/errors.h b/libc-bottom-half/cloudlibc/src/common/errors.h index a024be2ea..0488702ad 100644 --- a/libc-bottom-half/cloudlibc/src/common/errors.h +++ b/libc-bottom-half/cloudlibc/src/common/errors.h @@ -1,6 +1,6 @@ #include -#if defined(__wasip2__) || defined(__wasip3__) +#if defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) #include #include #include diff --git a/libc-bottom-half/cloudlibc/src/common/time.h b/libc-bottom-half/cloudlibc/src/common/time.h index dbb101536..0fa8918db 100644 --- a/libc-bottom-half/cloudlibc/src/common/time.h +++ b/libc-bottom-half/cloudlibc/src/common/time.h @@ -18,7 +18,7 @@ #if defined(__wasip1__) typedef __wasi_timestamp_t wasilibc_timestamp_t; -#elif defined(__wasip2__) +#elif defined(__wasip2__) || defined(__webp2__) typedef wall_clock_datetime_t wasilibc_timestamp_t; #elif defined(__wasip3__) typedef filesystem_instant_t wasilibc_timestamp_t; @@ -43,7 +43,7 @@ static inline bool timespec_to_timestamp_exact( // Make sure our timestamp does not overflow. return !__builtin_mul_overflow(timespec->tv_sec, NSEC_PER_SEC, timestamp) && !__builtin_add_overflow(*timestamp, timespec->tv_nsec, timestamp); -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) timestamp->seconds = timespec->tv_sec; timestamp->nanoseconds = timespec->tv_nsec; return true; @@ -67,7 +67,7 @@ static inline bool timespec_to_timestamp_clamp( // Make sure our timestamp does not overflow. *timestamp = NUMERIC_MAX(__wasi_timestamp_t); } -#elif defined(__wasip2__) +#elif defined(__wasip2__) || defined(__webp2__) if (timespec->tv_sec < 0) { // Timestamps before the Epoch are not supported. timestamp->seconds = 0; @@ -100,7 +100,7 @@ static inline struct timeval timestamp_to_timeval( return (struct timeval){.tv_sec = ts.tv_sec, ts.tv_nsec / 1000}; } -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) static inline struct timespec timestamp_to_timespec( wasilibc_timestamp_t *timestamp) { diff --git a/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c b/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c index 3c44db86c..195e6615c 100644 --- a/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c +++ b/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c @@ -53,7 +53,7 @@ int fcntl(int fildes, int cmd, ...) { oflags |= O_SEARCH; } return oflags; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) if (!entry.vtable->fcntl_getfl) { errno = EINVAL; return -1; @@ -78,7 +78,7 @@ int fcntl(int fildes, int cmd, ...) { errno = error; return -1; } -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) if (!entry.vtable->fcntl_setfl) { errno = EINVAL; return -1; diff --git a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c index 64a410d68..aeba4dee3 100644 --- a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c +++ b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c @@ -10,7 +10,7 @@ noreturn void _Exit(int status) { #if defined(__wasip1__) __wasi_proc_exit(status); -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) if (status >= 0 && status <= 255) exit_exit_with_code(status); exit_result_void_void_t exit_status = { .is_err = status != 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 8da1a6b42..592c62c58 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c @@ -59,7 +59,7 @@ int ioctl(int fildes, int request, ...) { // No data available for reading. *result = 0; return 0; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) // wasip{2,3} doesn't support this operation errno = ENOTSUP; return -1; @@ -93,7 +93,7 @@ int ioctl(int fildes, int request, ...) { return -1; } return 0; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(fildes, &entry) < 0) return -1; diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h b/libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h index c55075fc1..e289f1a44 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h +++ b/libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h @@ -65,7 +65,7 @@ static inline void to_public_stat(const __wasi_filestat_t *in, break; } } -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) #ifdef __wasip3__ typedef filesystem_instant_t filesystem_datetime_t; #endif 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 90ed31a9d..951282088 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c @@ -41,7 +41,7 @@ ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { return -1; } return bytes_read; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) // Skip empty iovecs and then delegate to `read` with the first non-empty // iovec. while (iovcnt) { diff --git a/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c b/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c index df3d0e723..5010ba10d 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c @@ -17,7 +17,7 @@ int __clock_gettime(clockid_t clock_id, struct timespec *tp) { return -1; } *tp = timestamp_to_timespec(ts); -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) if (tp == NULL) return 0; diff --git a/libc-bottom-half/cloudlibc/src/libc/time/time.c b/libc-bottom-half/cloudlibc/src/libc/time/time.c index fbdb4b602..61d5b62d5 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/time.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/time.c @@ -10,7 +10,7 @@ time_t time(time_t *tloc) { #if defined(__wasip1__) __wasi_timestamp_t ts = 0; (void)__wasi_clock_time_get(__WASI_CLOCKID_REALTIME, NSEC_PER_SEC, &ts); -#elif defined(__wasip2__) +#elif defined(__wasip2__) || defined(__webp2__) wasilibc_timestamp_t res; wall_clock_now(&res); uint64_t ts = (res.seconds * NSEC_PER_SEC) + res.nanoseconds; diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c b/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c index 8fe2024e4..cf07d7a2f 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c @@ -30,7 +30,7 @@ off_t __lseek(int fildes, off_t offset, int whence) { return -1; } return new_offset; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) // Look up a stream for fildes descriptor_table_entry_t entry; if (descriptor_table_get(fildes, &entry) < 0) diff --git a/libc-bottom-half/headers/public/__header_sys_socket.h b/libc-bottom-half/headers/public/__header_sys_socket.h index a7ee66e08..da971c895 100644 --- a/libc-bottom-half/headers/public/__header_sys_socket.h +++ b/libc-bottom-half/headers/public/__header_sys_socket.h @@ -16,7 +16,7 @@ #define MSG_PEEK __WASI_RIFLAGS_RECV_PEEK #define MSG_WAITALL __WASI_RIFLAGS_RECV_WAITALL #define MSG_TRUNC __WASI_ROFLAGS_RECV_DATA_TRUNCATED -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) #define MSG_DONTWAIT 0x0040 #define MSG_NOSIGNAL 0x4000 #define MSG_PEEK 0x0002 diff --git a/libc-bottom-half/headers/public/wasi/__generated_webp2.h b/libc-bottom-half/headers/public/wasi/__generated_webp2.h new file mode 100644 index 000000000..369e80fcd --- /dev/null +++ b/libc-bottom-half/headers/public/wasi/__generated_webp2.h @@ -0,0 +1,15 @@ +// Generated by `wit-bindgen` 0.60.0. DO NOT EDIT! +#ifndef __BINDINGS_WEBP2_H +#define __BINDINGS_WEBP2_H +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#ifdef __cplusplus +} +#endif +#endif diff --git a/libc-bottom-half/headers/public/wasi/api.h b/libc-bottom-half/headers/public/wasi/api.h index 24ccddbb5..125dc0669 100644 --- a/libc-bottom-half/headers/public/wasi/api.h +++ b/libc-bottom-half/headers/public/wasi/api.h @@ -27,6 +27,8 @@ #include #elif defined(__wasip3__) #include +#elif defined(__webp2__) +#include #else #error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/headers/public/wasi/version.h.in b/libc-bottom-half/headers/public/wasi/version.h.in index d36ba8cb4..887d18e9e 100644 --- a/libc-bottom-half/headers/public/wasi/version.h.in +++ b/libc-bottom-half/headers/public/wasi/version.h.in @@ -1,15 +1,16 @@ #ifndef __WASI_VERSION_H #define __WASI_VERSION_H -#ifndef __wasi__ -#error is only supported on WASI platforms. -#endif - // After llvm/llvm-project#165345 these can be conditionally defined by a clang // version test here to use the upstream definitions. #cmakedefine __wasip1__ #cmakedefine __wasip2__ #cmakedefine __wasip3__ +#cmakedefine __webp2__ + +#if !defined(__wasi__) && !defined(__webp2__) +#error is only supported on WASI platforms. +#endif // The `__wasi_cooperative_threads__` define is used to test whether cooperative // threads are enabled for this build of wasi-libc. This is detected at diff --git a/libc-bottom-half/headers/public/wasi/webp2.h b/libc-bottom-half/headers/public/wasi/webp2.h new file mode 100644 index 000000000..470218d54 --- /dev/null +++ b/libc-bottom-half/headers/public/wasi/webp2.h @@ -0,0 +1,10 @@ +#ifndef __WASI_WEBP2_H +#define __WASI_WEBP2_H + +#include + +#ifdef __webp2__ +#include +#endif + +#endif diff --git a/libc-bottom-half/sources/webp2.c b/libc-bottom-half/sources/webp2.c new file mode 100644 index 000000000..3ee8ba78a --- /dev/null +++ b/libc-bottom-half/sources/webp2.c @@ -0,0 +1,24 @@ +// Generated by `wit-bindgen` 0.60.0. DO NOT EDIT! +#include "wasi/webp2.h" +#include + +// Canonical ABI intrinsics + +__attribute__((__weak__, __export_name__("cabi_realloc"))) +void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) { + (void) old_size; + if (new_size == 0) return (void*) align; + void *ret = realloc(ptr, new_size); + if (!ret) abort(); + return ret; +} + +// Component Adapters + +// Ensure that the *_component_type.o object is linked in + +extern void __component_type_object_force_link_webp2(void); +__attribute__((used)) +void __component_type_object_force_link_webp2_public_use_in_this_compilation_unit(void) { + __component_type_object_force_link_webp2(); +} diff --git a/libc-bottom-half/sources/webp2_component_type.o b/libc-bottom-half/sources/webp2_component_type.o new file mode 100644 index 0000000000000000000000000000000000000000..3d3b1744cb5c09f5e79a5f7b4aad8434a62b031a GIT binary patch literal 269 zcmY+9%WlFj5JhKbd(QPmw1|1n@^K;~gbajJ+v#_{;t{E zP&$8WlerudBkL;Cw(#2XA8a}g&^XDWWMyl(!B>I)3t_lTnV%yLm)Rt2hf2LmH0f(_o AO#lD@ literal 0 HcmV?d00001 diff --git a/libc-bottom-half/sources/webp2_filesystem.c b/libc-bottom-half/sources/webp2_filesystem.c new file mode 100644 index 000000000..04b73c073 --- /dev/null +++ b/libc-bottom-half/sources/webp2_filesystem.c @@ -0,0 +1 @@ +// There is no filesystem. \ No newline at end of file diff --git a/web/p2/wit/wasi-libc.wit b/web/p2/wit/wasi-libc.wit new file mode 100644 index 000000000..5f726b245 --- /dev/null +++ b/web/p2/wit/wasi-libc.wit @@ -0,0 +1,3 @@ +package wasi-libc:webp2; + +world webp2 {} From 522af17de8648a54acd9ebc4eb3706c404e2c33b Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Wed, 30 Sep 2026 11:42:01 -0400 Subject: [PATCH 02/96] time.h --- libc-bottom-half/cloudlibc/src/common/time.h | 14 ++++++++--- .../headers/private/wasi/descriptor_table.h | 4 +-- .../headers/public/wasi/__generated_webp2.h | 18 ++++++++++++++ libc-bottom-half/sources/webp2.c | 23 ++++++++++++++++++ .../sources/webp2_component_type.o | Bin 269 -> 304 bytes web/p2/wit/wasi-libc.wit | 6 ++++- 6 files changed, 59 insertions(+), 6 deletions(-) diff --git a/libc-bottom-half/cloudlibc/src/common/time.h b/libc-bottom-half/cloudlibc/src/common/time.h index 0fa8918db..4185a5457 100644 --- a/libc-bottom-half/cloudlibc/src/common/time.h +++ b/libc-bottom-half/cloudlibc/src/common/time.h @@ -18,11 +18,18 @@ #if defined(__wasip1__) typedef __wasi_timestamp_t wasilibc_timestamp_t; -#elif defined(__wasip2__) || defined(__webp2__) +#elif defined(__wasip2__) typedef wall_clock_datetime_t wasilibc_timestamp_t; #elif defined(__wasip3__) typedef filesystem_instant_t wasilibc_timestamp_t; typedef monotonic_clock_mark_t monotonic_clock_instant_t; +#elif defined(__webp2__) +typedef struct wasilibc_timestamp_t { + uint64_t seconds; + uint32_t nanoseconds; +} wasilibc_timestamp_t; +typedef uint64_t monotonic_clock_instant_t; +typedef uint64_t monotonic_clock_duration_t; #else # error "Unknown WASI version" #endif @@ -104,14 +111,15 @@ static inline struct timeval timestamp_to_timeval( static inline struct timespec timestamp_to_timespec( wasilibc_timestamp_t *timestamp) { -#if defined(__wasip2__) +#if defined(__wasip2__) || defined(__webp2__) // Check for overflow when converting unsigned to signed if (timestamp->seconds > INT64_MAX) { return (struct timespec){.tv_sec = INT64_MAX, .tv_nsec = NSEC_PER_SEC - 1}; } -#endif +#else return (struct timespec){.tv_sec = timestamp->seconds, .tv_nsec = timestamp->nanoseconds}; +#endif } static inline struct timespec instant_to_timespec( diff --git a/libc-bottom-half/headers/private/wasi/descriptor_table.h b/libc-bottom-half/headers/private/wasi/descriptor_table.h index b06b8e6df..83a357f99 100644 --- a/libc-bottom-half/headers/private/wasi/descriptor_table.h +++ b/libc-bottom-half/headers/private/wasi/descriptor_table.h @@ -137,7 +137,7 @@ typedef struct wasi_read_t { bool blocking; // The timeout, in nanoseconds, for this operation. monotonic_clock_duration_t timeout; -#ifdef __wasip2__ +#if defined(__wasip2__) // The `wasi:io/streams.input-stream` that this is reading from. streams_borrow_input_stream_t input; // A required pointer to an owned pollable for `input`. This is lazily @@ -159,7 +159,7 @@ typedef struct wasi_write_t { bool blocking; monotonic_clock_duration_t timeout; -#ifdef __wasip2__ +#if defined(__wasip2__) streams_borrow_output_stream_t output; poll_own_pollable_t *pollable; #else diff --git a/libc-bottom-half/headers/public/wasi/__generated_webp2.h b/libc-bottom-half/headers/public/wasi/__generated_webp2.h index 369e80fcd..fcfb73d92 100644 --- a/libc-bottom-half/headers/public/wasi/__generated_webp2.h +++ b/libc-bottom-half/headers/public/wasi/__generated_webp2.h @@ -9,6 +9,24 @@ extern "C" { #include #include +typedef struct webp2_own_date_t { + int32_t __handle; +} webp2_own_date_t; + +typedef struct webp2_borrow_date_t { + int32_t __handle; +} webp2_borrow_date_t; + +// Imported Functions from `webp2` +extern double webp2_static_date_now(void); + +// Helper Functions + +extern void webp2_date_drop_own(webp2_own_date_t handle); + +extern webp2_borrow_date_t webp2_borrow_date(webp2_own_date_t handle); + + #ifdef __cplusplus } #endif diff --git a/libc-bottom-half/sources/webp2.c b/libc-bottom-half/sources/webp2.c index 3ee8ba78a..3a2b01e49 100644 --- a/libc-bottom-half/sources/webp2.c +++ b/libc-bottom-half/sources/webp2.c @@ -2,6 +2,11 @@ #include "wasi/webp2.h" #include +// Imported Functions from `webp2` + +__attribute__((__import_module__("$root"), __import_name__("[static]date.now"))) +extern double __wasm_import_webp2_static_date_now(void); + // Canonical ABI intrinsics __attribute__((__weak__, __export_name__("cabi_realloc"))) @@ -13,8 +18,26 @@ void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) { return ret; } +// Helper Functions + +__attribute__((__import_module__("$root"), __import_name__("[resource-drop]date"))) +extern void __wasm_import_webp2_date_drop(int32_t handle); + +void webp2_date_drop_own(webp2_own_date_t handle) { + __wasm_import_webp2_date_drop(handle.__handle); +} + +webp2_borrow_date_t webp2_borrow_date(webp2_own_date_t arg) { + return (webp2_borrow_date_t) { arg.__handle }; +} + // Component Adapters +double webp2_static_date_now(void) { + double ret = __wasm_import_webp2_static_date_now(); + return ret; +} + // Ensure that the *_component_type.o object is linked in extern void __component_type_object_force_link_webp2(void); diff --git a/libc-bottom-half/sources/webp2_component_type.o b/libc-bottom-half/sources/webp2_component_type.o index 3d3b1744cb5c09f5e79a5f7b4aad8434a62b031a..5ac42e1fa322fe73e8c285d8023e70a9d1aaf2dd 100644 GIT binary patch delta 57 zcmeBW+Q2kHlJWLLDOW9LMn@(_M`mUQmXySjRAxp-2L^^xW(I-i;*!LY%;Z>*s9s)v KIV0o5wB-PheGeZ1 delta 22 dcmdnM)XOwMl5xjGDOYv{Mn@(_M}~==%K=6T1_A&8 diff --git a/web/p2/wit/wasi-libc.wit b/web/p2/wit/wasi-libc.wit index 5f726b245..4d25588a2 100644 --- a/web/p2/wit/wasi-libc.wit +++ b/web/p2/wit/wasi-libc.wit @@ -1,3 +1,7 @@ package wasi-libc:webp2; -world webp2 {} +world webp2 { + resource date { + now: static func() -> f64; + } +} From cc8c7c35857acc433e53bc91a4f25097329e3e64 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Wed, 30 Sep 2026 11:57:57 -0400 Subject: [PATCH 03/96] Try to bind performance.now() in anticipation of monotonic clocks --- libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c | 4 +++- web/p2/wit/wasi-libc.wit | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c b/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c index 5010ba10d..700bc7153 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c @@ -17,7 +17,7 @@ int __clock_gettime(clockid_t clock_id, struct timespec *tp) { return -1; } *tp = timestamp_to_timespec(ts); -#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) +#elif defined(__wasip2__) || defined(__wasip3__) if (tp == NULL) return 0; @@ -36,6 +36,8 @@ int __clock_gettime(clockid_t clock_id, struct timespec *tp) { errno = EINVAL; return -1; // wasip{2,3} only supports wall and monotonic clocks } +#elif defined(__webp2__) +# error "TODO(webp2)" #else # error "Unsupported WASI version" #endif diff --git a/web/p2/wit/wasi-libc.wit b/web/p2/wit/wasi-libc.wit index 4d25588a2..6f417d2ee 100644 --- a/web/p2/wit/wasi-libc.wit +++ b/web/p2/wit/wasi-libc.wit @@ -1,6 +1,11 @@ package wasi-libc:webp2; world webp2 { + resource performance { + now: func() -> f64; + } + performance: get() -> performance; + resource date { now: static func() -> f64; } From 518ba9a7f8382d9ff9d68fe7ff714fb246182417 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 3 Oct 2026 09:37:55 -0400 Subject: [PATCH 04/96] Well, wit-bindgen is 1% happier --- cmake/bindings.cmake | 4 ++-- web/p2/wit/wasi-libc.wit | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmake/bindings.cmake b/cmake/bindings.cmake index fb8250ef0..8db9b2ec3 100644 --- a/cmake/bindings.cmake +++ b/cmake/bindings.cmake @@ -15,8 +15,8 @@ if(WIT_BINDGEN_EXECUTABLE) OUTPUT_VARIABLE WIT_BINDGEN_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE) - if (NOT (WIT_BINDGEN_VERSION MATCHES "0\\.60\\.0")) - message(WARNING "wit-bindgen version 0.60.0 is required, found: ${WIT_BINDGEN_VERSION}") + if (NOT (WIT_BINDGEN_VERSION MATCHES "0\\.62\\.0")) + message(WARNING "wit-bindgen version 0.62.0 is required, found: ${WIT_BINDGEN_VERSION}") set(WIT_BINDGEN_EXECUTABLE "") endif() endif() diff --git a/web/p2/wit/wasi-libc.wit b/web/p2/wit/wasi-libc.wit index 6f417d2ee..014fe5257 100644 --- a/web/p2/wit/wasi-libc.wit +++ b/web/p2/wit/wasi-libc.wit @@ -4,7 +4,7 @@ world webp2 { resource performance { now: func() -> f64; } - performance: get() -> performance; + import performance: get() -> performance; resource date { now: static func() -> f64; From 13fd47484d696f89554dc7a93226dcab1128d3ba Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 3 Oct 2026 13:46:06 -0500 Subject: [PATCH 05/96] Lots of time fixes + web-specific error reporting? --- libc-bottom-half/CMakeLists.txt | 2 + libc-bottom-half/cloudlibc/src/common/time.h | 35 +++- .../src/libc/sys/time/gettimeofday.c | 3 + .../cloudlibc/src/libc/time/clock_getres.c | 13 ++ .../cloudlibc/src/libc/time/clock_gettime.c | 15 +- .../cloudlibc/src/libc/time/clock_nanosleep.c | 4 + .../cloudlibc/src/libc/time/nanosleep.c | 2 +- .../cloudlibc/src/libc/time/time.c | 5 +- .../cloudlibc/src/libc/unistd/sleep.c | 10 +- .../cloudlibc/src/libc/unistd/usleep.c | 2 +- .../headers/public/wasi/__generated_webp2.h | 71 +++++++- .../sources/__wasilibc_report_error.c | 15 ++ .../sources/__wasilibc_report_errorf.c | 52 ++++++ libc-bottom-half/sources/webp2.c | 155 +++++++++++++++++- .../sources/webp2_component_type.o | Bin 304 -> 596 bytes .../headers/private/wasi/report-error.h | 8 + .../headers/private/wasi/report-errorf.h | 9 + libc-top-half/musl/src/exit/assert.c | 4 +- web/p2/wit/wasi-libc.wit | 14 +- 19 files changed, 398 insertions(+), 21 deletions(-) create mode 100644 libc-bottom-half/sources/__wasilibc_report_error.c create mode 100644 libc-bottom-half/sources/__wasilibc_report_errorf.c create mode 100644 libc-top-half/headers/private/wasi/report-error.h create mode 100644 libc-top-half/headers/private/wasi/report-errorf.h diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index a36134803..5f2ed2367 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -89,6 +89,8 @@ set(bottom_half_sources sources/__wasilibc_initialize_environ.c sources/__wasilibc_random.c sources/__wasilibc_real.c + sources/__wasilibc_report_error.c + sources/__wasilibc_report_errorf.c sources/__wasilibc_tell.c sources/abort.c sources/complex-builtins.c diff --git a/libc-bottom-half/cloudlibc/src/common/time.h b/libc-bottom-half/cloudlibc/src/common/time.h index 4185a5457..aec7f9939 100644 --- a/libc-bottom-half/cloudlibc/src/common/time.h +++ b/libc-bottom-half/cloudlibc/src/common/time.h @@ -9,12 +9,17 @@ #include +#include #include #include #include #define NSEC_PER_SEC 1000000000 +#define NSEC_PER_MSEC 1000000 +#define NSEC_PER_USEC 1000 #define USEC_PER_SEC 1000000 +#define USEC_PER_MSEC 1000 +#define MSEC_PER_SEC 1000 #if defined(__wasip1__) typedef __wasi_timestamp_t wasilibc_timestamp_t; @@ -28,8 +33,8 @@ typedef struct wasilibc_timestamp_t { uint64_t seconds; uint32_t nanoseconds; } wasilibc_timestamp_t; -typedef uint64_t monotonic_clock_instant_t; -typedef uint64_t monotonic_clock_duration_t; +typedef uint64_t monotonic_clock_instant_t; // nanoseconds +typedef uint64_t monotonic_clock_duration_t; // nanoseconds #else # error "Unknown WASI version" #endif @@ -40,11 +45,11 @@ static inline bool timespec_to_timestamp_exact( if (timespec->tv_nsec < 0 || timespec->tv_nsec >= NSEC_PER_SEC) return false; - #if defined(__wasip1__) || defined(__wasip2__) +#if defined(__wasip1__) || defined(__wasip2__) || defined(__webp2__) // Timestamps before the Epoch are not supported. if (timespec->tv_sec < 0) return false; - #endif +#endif #if defined(__wasip1__) // Make sure our timestamp does not overflow. @@ -116,10 +121,9 @@ static inline struct timespec timestamp_to_timespec( if (timestamp->seconds > INT64_MAX) { return (struct timespec){.tv_sec = INT64_MAX, .tv_nsec = NSEC_PER_SEC - 1}; } -#else +#endif return (struct timespec){.tv_sec = timestamp->seconds, .tv_nsec = timestamp->nanoseconds}; -#endif } static inline struct timespec instant_to_timespec( @@ -190,4 +194,23 @@ static inline struct timeval timestamp_to_timeval( #else # error "Unknown WASI version" #endif + +#ifdef __webp2__ +static inline wasilibc_timestamp_t web_wall_clock_now() { + double clock_ms = webp2_static_date_now(); + return (wasilibc_timestamp_t){ + .seconds = (uint64_t)(clock_ms / MSEC_PER_SEC), + .nanoseconds = fmod(clock_ms, MSEC_PER_SEC) * NSEC_PER_MSEC, + }; +} + +static inline monotonic_clock_instant_t web_monotonic_clock_now() { + webp2_own_performance_impl_t perf = webp2_get_performance(); + double time_origin = webp2_method_get_performance_impl_time_origin(webp2_borrow_performance_impl(perf)); + double clock_ms = time_origin + webp2_method_performance_impl_now(webp2_borrow_performance_impl(perf)); + webp2_performance_impl_drop_own(perf); + return (monotonic_clock_instant_t)(clock_ms * NSEC_PER_MSEC); +} +#endif + #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/time/gettimeofday.c b/libc-bottom-half/cloudlibc/src/libc/sys/time/gettimeofday.c index a1bd7911c..2d7ce5e0c 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/time/gettimeofday.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/time/gettimeofday.c @@ -21,6 +21,9 @@ int gettimeofday(struct timeval *restrict tp, void *tz) { system_clock_instant_t time_result; system_clock_now(&time_result); *tp = timestamp_to_timeval(&time_result); +#elif defined(__webp2__) + wasilibc_timestamp_t time_result = web_wall_clock_now(); + *tp = timestamp_to_timeval(&time_result); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/time/clock_getres.c b/libc-bottom-half/cloudlibc/src/libc/time/clock_getres.c index 7363e145c..69a44e538 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/clock_getres.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/clock_getres.c @@ -39,6 +39,19 @@ int clock_getres(clockid_t clock_id, struct timespec *res) { return -1; } } +#elif defined(__webp2__) + if (res != NULL) { + if (clock_id == CLOCK_REALTIME) { + // Date.now() is always limited to 1ms resolution. + *res = (struct timespec){ .tv_nsec = NSEC_PER_MSEC }; + } else if (clock_id == CLOCK_MONOTONIC) { + // Performance.now() has up to five-microsecond precision. + *res = (struct timespec){ .tv_nsec = 5 * NSEC_PER_USEC }; + } else { + errno = EINVAL; // webp2 only supports wall and monotonic clocks + return -1; + } + } #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c b/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c index 700bc7153..85d9bfed7 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -37,7 +38,19 @@ int __clock_gettime(clockid_t clock_id, struct timespec *tp) { return -1; // wasip{2,3} only supports wall and monotonic clocks } #elif defined(__webp2__) -# error "TODO(webp2)" + if (tp == NULL) { + return 0; + } + + if (clock_id->id == CLOCKID_MONOTONIC) { + *tp = instant_to_timespec(web_monotonic_clock_now()); + } else if (clock_id->id == CLOCKID_REALTIME) { + wasilibc_timestamp_t now = web_wall_clock_now(); + *tp = timestamp_to_timespec(&now); + } else { + errno = EINVAL; + return -1; + } #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c b/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c index 4246d3c55..77966ffea 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c @@ -67,6 +67,10 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, #endif return 0; +#elif defined(__webp2__) + // TODO(webp2): Rather than return ENOTSUP, I figure I'd rather trap right + // now so we encounter issues more noisily. + __builtin_trap(); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c b/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c index 31ba4faa9..ffdc9d526 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c @@ -7,7 +7,7 @@ #include int nanosleep(const struct timespec *rqtp, struct timespec *rem) { -#if defined(__wasip2__) || defined(__wasip3__) +#if defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) // FIXME(WebAssembly/WASI#857): wasip2/p3 only supports the monotonic clock for // sleeping. clockid_t clock = CLOCK_MONOTONIC; diff --git a/libc-bottom-half/cloudlibc/src/libc/time/time.c b/libc-bottom-half/cloudlibc/src/libc/time/time.c index 61d5b62d5..ed9c7cfb2 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/time.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/time.c @@ -10,7 +10,7 @@ time_t time(time_t *tloc) { #if defined(__wasip1__) __wasi_timestamp_t ts = 0; (void)__wasi_clock_time_get(__WASI_CLOCKID_REALTIME, NSEC_PER_SEC, &ts); -#elif defined(__wasip2__) || defined(__webp2__) +#elif defined(__wasip2__) wasilibc_timestamp_t res; wall_clock_now(&res); uint64_t ts = (res.seconds * NSEC_PER_SEC) + res.nanoseconds; @@ -18,6 +18,9 @@ time_t time(time_t *tloc) { wasilibc_timestamp_t res; system_clock_now(&res); time_t ts = (res.seconds * NSEC_PER_SEC) + res.nanoseconds; +#elif defined(__webp2__) + double epoch_ms = webp2_static_date_now(); + time_t ts = epoch_ms * NSEC_PER_MSEC; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c b/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c index 4f2926a2b..e5828686a 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c @@ -10,13 +10,13 @@ unsigned int sleep(unsigned int seconds) { struct timespec ts = {.tv_sec = seconds, .tv_nsec = 0}; // FIXME(WebAssembly/WASI#857): wasip2/p3 only supports the monotonic clock for // sleeping. - #if defined(__wasip1__) +#if defined(__wasip1__) clockid_t clock_id = CLOCK_REALTIME; - #elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) clockid_t clock_id = CLOCK_MONOTONIC; - #else - #error "Unsupported WASI version" - #endif +#else +# error "Unsupported WASI version" +#endif if (clock_nanosleep(clock_id, 0, &ts, NULL) != 0) return seconds; return 0; diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c b/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c index 6af5e80da..6bb3f783a 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c @@ -12,7 +12,7 @@ int usleep(useconds_t useconds) { .tv_nsec = useconds % 1000000 * 1000}; #if defined(__wasip1__) clockid_t clock_id = CLOCK_REALTIME; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) // FIXME(WebAssembly/WASI#857): wasip2/p3 only supports the monotonic clock // for sleeping. clockid_t clock_id = CLOCK_MONOTONIC; diff --git a/libc-bottom-half/headers/public/wasi/__generated_webp2.h b/libc-bottom-half/headers/public/wasi/__generated_webp2.h index fcfb73d92..b8469fadc 100644 --- a/libc-bottom-half/headers/public/wasi/__generated_webp2.h +++ b/libc-bottom-half/headers/public/wasi/__generated_webp2.h @@ -1,4 +1,4 @@ -// Generated by `wit-bindgen` 0.60.0. DO NOT EDIT! +// Generated by `wit-bindgen` 0.62.0. DO NOT EDIT! #ifndef __BINDINGS_WEBP2_H #define __BINDINGS_WEBP2_H #ifdef __cplusplus @@ -9,6 +9,19 @@ extern "C" { #include #include +typedef struct webp2_string_t { + uint8_t*ptr; + size_t len; +} webp2_string_t; + +typedef struct webp2_own_performance_impl_t { + int32_t __handle; +} webp2_own_performance_impl_t; + +typedef struct webp2_borrow_performance_impl_t { + int32_t __handle; +} webp2_borrow_performance_impl_t; + typedef struct webp2_own_date_t { int32_t __handle; } webp2_own_date_t; @@ -17,15 +30,71 @@ typedef struct webp2_borrow_date_t { int32_t __handle; } webp2_borrow_date_t; +typedef struct { + bool is_some; + webp2_string_t val; +} webp2_option_string_t; + +typedef struct webp2_new_error_options_t { + webp2_option_string_t cause; +} webp2_new_error_options_t; + +typedef struct webp2_own_error_t { + int32_t __handle; +} webp2_own_error_t; + +typedef struct webp2_borrow_error_t { + int32_t __handle; +} webp2_borrow_error_t; + +typedef struct { + bool is_some; + webp2_new_error_options_t val; +} webp2_option_new_error_options_t; + // Imported Functions from `webp2` +extern webp2_own_performance_impl_t webp2_get_performance(void); +extern void webp2_report_error(webp2_borrow_error_t throwable); +extern double webp2_method_performance_impl_now(webp2_borrow_performance_impl_t self); +extern double webp2_method_get_performance_impl_time_origin(webp2_borrow_performance_impl_t self); extern double webp2_static_date_now(void); +extern webp2_own_error_t webp2_constructor_error(webp2_string_t *maybe_message, webp2_new_error_options_t *maybe_options); // Helper Functions +extern void webp2_performance_impl_drop_own(webp2_own_performance_impl_t handle); + +extern webp2_borrow_performance_impl_t webp2_borrow_performance_impl(webp2_own_performance_impl_t handle); + extern void webp2_date_drop_own(webp2_own_date_t handle); extern webp2_borrow_date_t webp2_borrow_date(webp2_own_date_t handle); +void webp2_option_string_free(webp2_option_string_t *ptr); + +void webp2_new_error_options_free(webp2_new_error_options_t *ptr); + +extern void webp2_error_drop_own(webp2_own_error_t handle); + +extern webp2_borrow_error_t webp2_borrow_error(webp2_own_error_t handle); + +void webp2_option_new_error_options_free(webp2_option_new_error_options_t *ptr); + +// Sets the string `ret` to reference the input string `s` without copying it +void webp2_string_set(webp2_string_t *ret, const char*s); + +// Creates a copy of the input nul-terminated string `s` and +// stores it into the component model string `ret`. +void webp2_string_dup(webp2_string_t *ret, const char*s); + +// Creates a copy of the input string `s` with length `len` and +// stores it into the component model string `ret`. +// The length is specified in code units (bytes for UTF-8, 16-bit values for UTF-16). +void webp2_string_dup_n(webp2_string_t *ret, const char*s, size_t len); + +// Deallocates the string pointed to by `ret`, deallocating +// the memory behind the string. +void webp2_string_free(webp2_string_t *ret); #ifdef __cplusplus } diff --git a/libc-bottom-half/sources/__wasilibc_report_error.c b/libc-bottom-half/sources/__wasilibc_report_error.c new file mode 100644 index 000000000..7df453f82 --- /dev/null +++ b/libc-bottom-half/sources/__wasilibc_report_error.c @@ -0,0 +1,15 @@ +#include +#include +#include + +void __wasilibc_report_error(const char* message) { +#if defined(__wasip1__) || defined(__wasip2__) || defined(__wasip3__) + fprintf(stderr, "%s\n", message); +#elif defined(__webp2__) + webp2_string_t msg; + webp2_string_set(&msg, message); + webp2_own_error_t err = webp2_constructor_error(&msg, NULL); + webp2_report_error(webp2_borrow_error(err)); + webp2_error_drop_own(err); +#endif +} diff --git a/libc-bottom-half/sources/__wasilibc_report_errorf.c b/libc-bottom-half/sources/__wasilibc_report_errorf.c new file mode 100644 index 000000000..f1f8f2d33 --- /dev/null +++ b/libc-bottom-half/sources/__wasilibc_report_errorf.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +#if defined(__webp2__) +// Ensure we have well-formed UTF-8 in case of truncation. Assumes valid UTF-8 +// coming in. +static void fix_truncated_utf8(char* buf, size_t len) { + if (len == 0) { + return; + } + size_t last_codepoint_start = len - 1; + for (size_t i = 0; i < 3; i++) { + if (((unsigned char)buf[last_codepoint_start] & 0xC0) != 0x80) { + break; + } + last_codepoint_start--; + } + unsigned char b1 = (unsigned char)buf[last_codepoint_start]; + size_t codepoint_len; + if (b1 >= 0xF0) { + codepoint_len = 4; + } else if (b1 >= 0xE0) { + codepoint_len = 3; + } else if (b1 >= 0xC0) { + codepoint_len = 2; + } else { + codepoint_len = 1; + } + if (len - last_codepoint_start < codepoint_len) { + buf[last_codepoint_start] = '\0'; + } +} +#endif + +void __wasilibc_report_errorf(const char *format, ...) { + va_list ap; + va_start(ap, format); +#if defined(__webp2__) + char buf[512]; + int n = vsnprintf(buf, sizeof(buf), format, ap); + if (n >= (int)sizeof(buf)) { + fix_truncated_utf8(buf, sizeof(buf) - 1); // subtract 1 for vnsprintf's \0 + } + __wasilibc_report_error(buf); +#else + vfprintf(stderr, format, ap); + fputc('\n', stderr); +#endif + va_end(ap); +} diff --git a/libc-bottom-half/sources/webp2.c b/libc-bottom-half/sources/webp2.c index 3a2b01e49..095dbc06b 100644 --- a/libc-bottom-half/sources/webp2.c +++ b/libc-bottom-half/sources/webp2.c @@ -1,12 +1,28 @@ -// Generated by `wit-bindgen` 0.60.0. DO NOT EDIT! +// Generated by `wit-bindgen` 0.62.0. DO NOT EDIT! #include "wasi/webp2.h" #include +#include // Imported Functions from `webp2` +__attribute__((__import_module__("$root"), __import_name__("[get]performance"))) +extern int32_t __wasm_import_webp2_get_performance(void); + +__attribute__((__import_module__("$root"), __import_name__("report-error"))) +extern void __wasm_import_webp2_report_error(int32_t); + +__attribute__((__import_module__("$root"), __import_name__("[method]performance-impl.now"))) +extern double __wasm_import_webp2_method_performance_impl_now(int32_t); + +__attribute__((__import_module__("$root"), __import_name__("[method][get]performance-impl.time-origin"))) +extern double __wasm_import_webp2_method_get_performance_impl_time_origin(int32_t); + __attribute__((__import_module__("$root"), __import_name__("[static]date.now"))) extern double __wasm_import_webp2_static_date_now(void); +__attribute__((__import_module__("$root"), __import_name__("[constructor]error"))) +extern int32_t __wasm_import_webp2_constructor_error(int32_t, uint8_t *, size_t, int32_t, int32_t, uint8_t *, size_t); + // Canonical ABI intrinsics __attribute__((__weak__, __export_name__("cabi_realloc"))) @@ -20,6 +36,17 @@ void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) { // Helper Functions +__attribute__((__import_module__("$root"), __import_name__("[resource-drop]performance-impl"))) +extern void __wasm_import_webp2_performance_impl_drop(int32_t handle); + +void webp2_performance_impl_drop_own(webp2_own_performance_impl_t handle) { + __wasm_import_webp2_performance_impl_drop(handle.__handle); +} + +webp2_borrow_performance_impl_t webp2_borrow_performance_impl(webp2_own_performance_impl_t arg) { + return (webp2_borrow_performance_impl_t) { arg.__handle }; +} + __attribute__((__import_module__("$root"), __import_name__("[resource-drop]date"))) extern void __wasm_import_webp2_date_drop(int32_t handle); @@ -31,13 +58,139 @@ webp2_borrow_date_t webp2_borrow_date(webp2_own_date_t arg) { return (webp2_borrow_date_t) { arg.__handle }; } +void webp2_option_string_free(webp2_option_string_t *ptr) { + if (ptr->is_some) { + webp2_string_free(&ptr->val); + } +} + +void webp2_new_error_options_free(webp2_new_error_options_t *ptr) { + webp2_option_string_free(&ptr->cause); +} + +__attribute__((__import_module__("$root"), __import_name__("[resource-drop]error"))) +extern void __wasm_import_webp2_error_drop(int32_t handle); + +void webp2_error_drop_own(webp2_own_error_t handle) { + __wasm_import_webp2_error_drop(handle.__handle); +} + +webp2_borrow_error_t webp2_borrow_error(webp2_own_error_t arg) { + return (webp2_borrow_error_t) { arg.__handle }; +} + +void webp2_option_new_error_options_free(webp2_option_new_error_options_t *ptr) { + if (ptr->is_some) { + webp2_new_error_options_free(&ptr->val); + } +} + +void webp2_string_set(webp2_string_t *ret, const char*s) { + ret->ptr = (uint8_t*) s; + ret->len = strlen(s); +} + +void webp2_string_dup(webp2_string_t *ret, const char*s) { + ret->len = strlen(s); + ret->ptr = (uint8_t*) cabi_realloc(NULL, 0, 1, ret->len * 1); + memcpy(ret->ptr, s, ret->len * 1); +} + +void webp2_string_dup_n(webp2_string_t *ret, const char*s, size_t len) { + ret->len = len; + ret->ptr = (uint8_t*) cabi_realloc(NULL, 0, 1, ret->len * 1); + memcpy(ret->ptr, s, ret->len * 1); +} + +void webp2_string_free(webp2_string_t *ret) { + if (ret->len > 0) { + free(ret->ptr); + } + ret->ptr = NULL; + ret->len = 0; +} + // Component Adapters +webp2_own_performance_impl_t webp2_get_performance(void) { + int32_t ret = __wasm_import_webp2_get_performance(); + return (webp2_own_performance_impl_t) { ret }; +} + +void webp2_report_error(webp2_borrow_error_t throwable) { + __wasm_import_webp2_report_error((throwable).__handle); +} + +double webp2_method_performance_impl_now(webp2_borrow_performance_impl_t self) { + double ret = __wasm_import_webp2_method_performance_impl_now((self).__handle); + return ret; +} + +double webp2_method_get_performance_impl_time_origin(webp2_borrow_performance_impl_t self) { + double ret = __wasm_import_webp2_method_get_performance_impl_time_origin((self).__handle); + return ret; +} + double webp2_static_date_now(void) { double ret = __wasm_import_webp2_static_date_now(); return ret; } +webp2_own_error_t webp2_constructor_error(webp2_string_t *maybe_message, webp2_new_error_options_t *maybe_options) { + webp2_option_string_t message; + message.is_some = maybe_message != NULL;if (maybe_message) { + message.val = *maybe_message; + } + webp2_option_new_error_options_t options; + options.is_some = maybe_options != NULL;if (maybe_options) { + options.val = *maybe_options; + } + int32_t option; + uint8_t * option1; + size_t option2; + if ((message).is_some) { + const webp2_string_t *payload0 = &(message).val; + option = 1; + option1 = (uint8_t *) (*payload0).ptr; + option2 = (*payload0).len; + } else { + option = 0; + option1 = 0; + option2 = 0; + } + int32_t option10; + int32_t option11; + uint8_t * option12; + size_t option13; + if ((options).is_some) { + const webp2_new_error_options_t *payload4 = &(options).val; + int32_t option7; + uint8_t * option8; + size_t option9; + if (((*payload4).cause).is_some) { + const webp2_string_t *payload6 = &((*payload4).cause).val; + option7 = 1; + option8 = (uint8_t *) (*payload6).ptr; + option9 = (*payload6).len; + } else { + option7 = 0; + option8 = 0; + option9 = 0; + } + option10 = 1; + option11 = option7; + option12 = option8; + option13 = option9; + } else { + option10 = 0; + option11 = 0; + option12 = 0; + option13 = 0; + } + int32_t ret = __wasm_import_webp2_constructor_error(option, option1, option2, option10, option11, option12, option13); + return (webp2_own_error_t) { ret }; +} + // Ensure that the *_component_type.o object is linked in extern void __component_type_object_force_link_webp2(void); diff --git a/libc-bottom-half/sources/webp2_component_type.o b/libc-bottom-half/sources/webp2_component_type.o index 5ac42e1fa322fe73e8c285d8023e70a9d1aaf2dd..c8defc2a80ef55a0c7a637f5ca930c164bbdf74a 100644 GIT binary patch delta 358 zcmZ9HF;2r!5JY$X+6f^E1PV|D7oczyA(4tANF0EI&Pw6f8~ZEq|JM3jE>qLtn{XJ; zzzOKVNGXm1Ows1irYi6hgnNNujI|@7%GD6FlU$9UN3h=3JIh%BuVGWe6t(=V9F5a#F?4%3^m6Pn(zL%h_}?9oE_fOKp;r OO%Qjcli6+adH)U5K544} delta 67 zcmcb@vVm!WB-3riiPA0t&Ww&sjE>CA3@j;$C8^Aelj9jB85t+9WegEFHPJKREzc~` XP0GwmNl(qwO=dIDGc%Yh!DIsfGwcws diff --git a/libc-top-half/headers/private/wasi/report-error.h b/libc-top-half/headers/private/wasi/report-error.h new file mode 100644 index 000000000..8a7a0c16e --- /dev/null +++ b/libc-top-half/headers/private/wasi/report-error.h @@ -0,0 +1,8 @@ +#ifndef __WASI_REPORT_ERROR_H +#define __WASI_REPORT_ERROR_H + +#include + +hidden void __wasilibc_report_error(const char* message); + +#endif diff --git a/libc-top-half/headers/private/wasi/report-errorf.h b/libc-top-half/headers/private/wasi/report-errorf.h new file mode 100644 index 000000000..a4f03fb18 --- /dev/null +++ b/libc-top-half/headers/private/wasi/report-errorf.h @@ -0,0 +1,9 @@ +#ifndef __WASI_REPORT_ERRORF_H +#define __WASI_REPORT_ERRORF_H + +#include + +hidden void __wasilibc_report_errorf(const char *format, ...) + __attribute__((__format__(__printf__, 1, 2))); + +#endif diff --git a/libc-top-half/musl/src/exit/assert.c b/libc-top-half/musl/src/exit/assert.c index 94edd8272..8a7956e71 100644 --- a/libc-top-half/musl/src/exit/assert.c +++ b/libc-top-half/musl/src/exit/assert.c @@ -1,8 +1,8 @@ -#include #include +#include _Noreturn void __assert_fail(const char *expr, const char *file, int line, const char *func) { - fprintf(stderr, "Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line); + __wasilibc_report_errorf("Assertion failed: %s (%s: %s: %d)", expr, file, func, line); abort(); } diff --git a/web/p2/wit/wasi-libc.wit b/web/p2/wit/wasi-libc.wit index 014fe5257..e497d7893 100644 --- a/web/p2/wit/wasi-libc.wit +++ b/web/p2/wit/wasi-libc.wit @@ -1,12 +1,22 @@ package wasi-libc:webp2; world webp2 { - resource performance { + @external-id("Performance") + resource performance-impl { now: func() -> f64; + time-origin: get() -> f64; } - import performance: get() -> performance; + import performance: get() -> performance-impl; resource date { now: static func() -> f64; } + + record new-error-options { + cause: option, // TODO(webp2): cause -> option + } + resource error { + constructor(message: option, options: option); // TODO(webp2): message -> option + } + import report-error: func(throwable: borrow); } From 1031ca32c849d8d008a9cf98ec168d2b6a3b4618 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 3 Oct 2026 14:32:42 -0500 Subject: [PATCH 06/96] Don't include translate_error in webp2 --- libc-bottom-half/cloudlibc/src/common/errors.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libc-bottom-half/cloudlibc/src/common/errors.h b/libc-bottom-half/cloudlibc/src/common/errors.h index 0488702ad..a024be2ea 100644 --- a/libc-bottom-half/cloudlibc/src/common/errors.h +++ b/libc-bottom-half/cloudlibc/src/common/errors.h @@ -1,6 +1,6 @@ #include -#if defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) +#if defined(__wasip2__) || defined(__wasip3__) #include #include #include From a5661ed2b1cdca2423d2e23f3f0da12ec7b6406c Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 3 Oct 2026 16:52:47 -0500 Subject: [PATCH 07/96] Organize libc-bottom-half sources by category --- libc-bottom-half/CMakeLists.txt | 108 ++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 41 deletions(-) diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index 5f2ed2367..31ffde37d 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -54,19 +54,13 @@ endforeach() # ============================================================================= # building bottom-half sources # + +# All targets set(bottom_half_sources cloudlibc/src/libc/errno/errno.c - cloudlibc/src/libc/fcntl/fcntl.c - cloudlibc/src/libc/poll/poll.c cloudlibc/src/libc/sched/sched_yield.c cloudlibc/src/libc/stdlib/_Exit.c - cloudlibc/src/libc/sys/ioctl/ioctl.c - cloudlibc/src/libc/sys/select/pselect.c - cloudlibc/src/libc/sys/select/select.c - cloudlibc/src/libc/sys/stat/fstat.c cloudlibc/src/libc/sys/time/gettimeofday.c - cloudlibc/src/libc/sys/uio/readv.c - cloudlibc/src/libc/sys/uio/writev.c cloudlibc/src/libc/time/CLOCK_MONOTONIC.c cloudlibc/src/libc/time/CLOCK_REALTIME.c cloudlibc/src/libc/time/clock_getres.c @@ -74,40 +68,59 @@ set(bottom_half_sources cloudlibc/src/libc/time/clock_nanosleep.c cloudlibc/src/libc/time/nanosleep.c cloudlibc/src/libc/time/time.c - cloudlibc/src/libc/unistd/lseek.c - cloudlibc/src/libc/unistd/pread.c - cloudlibc/src/libc/unistd/pwrite.c - cloudlibc/src/libc/unistd/read.c cloudlibc/src/libc/unistd/sleep.c cloudlibc/src/libc/unistd/usleep.c - cloudlibc/src/libc/unistd/write.c sources/__errno_location.c sources/__main_void.c sources/__wasilibc_dt.c sources/__wasilibc_environ.c - sources/__wasilibc_fd_renumber.c sources/__wasilibc_initialize_environ.c sources/__wasilibc_random.c sources/__wasilibc_real.c sources/__wasilibc_report_error.c sources/__wasilibc_report_errorf.c - sources/__wasilibc_tell.c sources/abort.c sources/complex-builtins.c - sources/dup.c sources/environ.c sources/errno.c sources/getentropy.c - sources/isatty.c sources/math/fmin-fmax.c sources/math/math-builtins.c - sources/pipe.c - sources/ppoll.c sources/reallocarray.c sources/sbrk.c sources/__cabi_realloc_wrapper.S ) +# File descriptors +if (NOT WEB STREQUAL "p2") + list(APPEND bottom_half_sources + cloudlibc/src/libc/fcntl/fcntl.c + cloudlibc/src/libc/poll/poll.c + cloudlibc/src/libc/sys/ioctl/ioctl.c + cloudlibc/src/libc/sys/select/pselect.c + cloudlibc/src/libc/sys/select/select.c + cloudlibc/src/libc/sys/stat/fstat.c + cloudlibc/src/libc/sys/uio/readv.c + cloudlibc/src/libc/sys/uio/writev.c + cloudlibc/src/libc/unistd/lseek.c + cloudlibc/src/libc/unistd/read.c + cloudlibc/src/libc/unistd/write.c + sources/__wasilibc_fd_renumber.c + sources/__wasilibc_tell.c + sources/dup.c + sources/isatty.c + sources/pipe.c + sources/ppoll.c + ) + if (WASI STREQUAL "p2" OR WASI STREQUAL "p3") + list(APPEND bottom_half_sources + sources/descriptor_table.c + sources/file_utils.c + ) + endif() +endif() + +# Filesystem if (NOT WEB STREQUAL "p2") list(APPEND bottom_half_sources cloudlibc/src/libc/dirent/closedir.c @@ -135,37 +148,28 @@ if (NOT WEB STREQUAL "p2") cloudlibc/src/libc/unistd/fsync.c cloudlibc/src/libc/unistd/ftruncate.c cloudlibc/src/libc/unistd/linkat.c + cloudlibc/src/libc/unistd/pread.c + cloudlibc/src/libc/unistd/pwrite.c cloudlibc/src/libc/unistd/readlinkat.c cloudlibc/src/libc/unistd/symlinkat.c cloudlibc/src/libc/unistd/unlinkat.c sources/__wasilibc_rmdirat.c sources/__wasilibc_unlinkat.c - sources/accept.c sources/at_fdcwd.c - sources/bind.c sources/chdir.c - sources/connect.c - sources/descriptor_table.c - sources/file.c - sources/file_utils.c sources/getcwd.c - sources/getsockpeername.c - sources/listen.c - sources/netdb.c sources/posix.c sources/preopens.c - sources/recv.c - sources/send.c - sources/shutdown.c - sources/socket.c - sources/sockets_utils.c - sources/sockopt.c - sources/tcp.c sources/truncate.c - sources/udp.c ) + if (WASI STREQUAL "p2" OR WASI STREQUAL "p3") + list(APPEND bottom_half_sources + sources/file.c + ) + endif() endif() +# Sockets if (WASI STREQUAL "p1") list(APPEND bottom_half_sources cloudlibc/src/libc/sys/socket/getsockopt.c @@ -174,8 +178,26 @@ if (WASI STREQUAL "p1") cloudlibc/src/libc/sys/socket/shutdown.c sources/accept-wasip1.c ) +elseif(WASI STREQUAL "p2" OR WASI STREQUAL "p3") + list(APPEND bottom_half_sources + sources/accept.c + sources/bind.c + sources/connect.c + sources/getsockpeername.c + sources/listen.c + sources/netdb.c + sources/recv.c + sources/send.c + sources/shutdown.c + sources/socket.c + sources/sockets_utils.c + sources/sockopt.c + sources/tcp.c + sources/udp.c + ) endif() +# wasip2-specific if(WASI STREQUAL "p2") list(APPEND bottom_half_sources sources/wasip2.c @@ -183,25 +205,29 @@ if(WASI STREQUAL "p2") ) endif() +# wasip3-specific if (WASI STREQUAL "p3") list(APPEND bottom_half_sources sources/wasip3.c sources/wasip3_block_on.c sources/wasip3_stdio.c ) + if (HAVE_WASM_LIBCALL_THREAD_CONTEXT) + list(APPEND bottom_half_sources sources/__wasm_task_hook.S + sources/wasip3_tls.c + ) + endif() endif() + +# webp2-specific if (WEB STREQUAL "p2") list(APPEND bottom_half_sources sources/webp2.c + sources/webp2_filesystem.c ) endif() -if (HAVE_WASM_LIBCALL_THREAD_CONTEXT) - list(APPEND bottom_half_sources sources/__wasm_task_hook.S - sources/wasip3_tls.c) -endif() - # Don't export symbols of generated code in shared libraries, so specifically # pass `-fvisibility=hidden`. set_source_files_properties(sources/wasip2.c PROPERTIES From 4f5370f50295ab57f8be885ba0edfffd3f120b67 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 3 Oct 2026 18:11:05 -0500 Subject: [PATCH 08/96] Random implementation (disaster) --- .../cloudlibc/src/libc/stdlib/_Exit.c | 8 +- .../cloudlibc/src/libc/time/clock_nanosleep.c | 2 + .../headers/public/wasi/__generated_webp2.h | 52 +++++++++++ libc-bottom-half/sources/__wasilibc_random.c | 51 ++++++++++- libc-bottom-half/sources/webp2.c | 86 ++++++++++++++++++ .../sources/webp2_component_type.o | Bin 596 -> 843 bytes web/p2/wit/wasi-libc.wit | 21 +++++ 7 files changed, 215 insertions(+), 5 deletions(-) diff --git a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c index aeba4dee3..aace6c8e5 100644 --- a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c +++ b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c @@ -3,6 +3,7 @@ // SPDX-License-Identifier: BSD-2-Clause #include +#include #include <_/cdefs.h> #include #include @@ -10,11 +11,16 @@ noreturn void _Exit(int status) { #if defined(__wasip1__) __wasi_proc_exit(status); -#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) +#elif defined(__wasip2__) || defined(__wasip3__) if (status >= 0 && status <= 255) exit_exit_with_code(status); exit_result_void_void_t exit_status = { .is_err = status != 0 }; exit_exit(&exit_status); +#elif defined(__webp2__) + if (status != 0) { + __wasilibc_report_error("exit with nonzero status"); + __builtin_trap(); + } #else # error "Unknown WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c b/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c index 77966ffea..8a79129b6 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c @@ -71,6 +71,8 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, // TODO(webp2): Rather than return ENOTSUP, I figure I'd rather trap right // now so we encounter issues more noisily. __builtin_trap(); + (void)clock_id; + (void)rqtp; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/headers/public/wasi/__generated_webp2.h b/libc-bottom-half/headers/public/wasi/__generated_webp2.h index b8469fadc..d9af9c444 100644 --- a/libc-bottom-half/headers/public/wasi/__generated_webp2.h +++ b/libc-bottom-half/headers/public/wasi/__generated_webp2.h @@ -47,18 +47,58 @@ typedef struct webp2_borrow_error_t { int32_t __handle; } webp2_borrow_error_t; +typedef struct webp2_own_uint_8_array_t { + int32_t __handle; +} webp2_own_uint_8_array_t; + +typedef struct webp2_borrow_uint_8_array_t { + int32_t __handle; +} webp2_borrow_uint_8_array_t; + +typedef struct webp2_own_crypto_impl_t { + int32_t __handle; +} webp2_own_crypto_impl_t; + +typedef struct webp2_borrow_crypto_impl_t { + int32_t __handle; +} webp2_borrow_crypto_impl_t; + typedef struct { bool is_some; webp2_new_error_options_t val; } webp2_option_new_error_options_t; +typedef struct { + bool is_some; + uint32_t val; +} webp2_option_u32_t; + +typedef struct { + uint8_t *ptr; + size_t len; +} webp2_list_u8_t; + // Imported Functions from `webp2` extern webp2_own_performance_impl_t webp2_get_performance(void); extern void webp2_report_error(webp2_borrow_error_t throwable); +extern webp2_own_crypto_impl_t webp2_get_crypto(void); extern double webp2_method_performance_impl_now(webp2_borrow_performance_impl_t self); extern double webp2_method_get_performance_impl_time_origin(webp2_borrow_performance_impl_t self); extern double webp2_static_date_now(void); extern webp2_own_error_t webp2_constructor_error(webp2_string_t *maybe_message, webp2_new_error_options_t *maybe_options); +extern webp2_own_uint_8_array_t webp2_constructor_uint_8_array(uint32_t length); +extern webp2_own_uint_8_array_t webp2_method_uint_8_array_subarray(webp2_borrow_uint_8_array_t self, uint32_t begin, uint32_t *maybe_end); +// TODO(webp2): This is very jank, but basically, the whole design of +// Crypto.getRandomValues() doesn't make sense for the component model +// because it modifies its parameter in place. The component model has +// no concept of an out-parameter (nor should it, frankly), but in this +// case we have no choice but to uselessly copy an entire array for it +// to fill "in place" and then copy back to us. By passing a handle to +// a pre-existing Uint8Array we can at least avoid the first copy. +// +// We desperately need to find a better way to do this. Some new +// builtin? New web APIs? +extern void webp2_method_crypto_impl_get_random_values(webp2_borrow_crypto_impl_t self, webp2_borrow_uint_8_array_t typed_array, webp2_list_u8_t *ret); // Helper Functions @@ -78,8 +118,20 @@ extern void webp2_error_drop_own(webp2_own_error_t handle); extern webp2_borrow_error_t webp2_borrow_error(webp2_own_error_t handle); +extern void webp2_uint_8_array_drop_own(webp2_own_uint_8_array_t handle); + +extern webp2_borrow_uint_8_array_t webp2_borrow_uint_8_array(webp2_own_uint_8_array_t handle); + +extern void webp2_crypto_impl_drop_own(webp2_own_crypto_impl_t handle); + +extern webp2_borrow_crypto_impl_t webp2_borrow_crypto_impl(webp2_own_crypto_impl_t handle); + void webp2_option_new_error_options_free(webp2_option_new_error_options_t *ptr); +void webp2_option_u32_free(webp2_option_u32_t *ptr); + +void webp2_list_u8_free(webp2_list_u8_t *ptr); + // Sets the string `ret` to reference the input string `s` without copying it void webp2_string_set(webp2_string_t *ret, const char*s); diff --git a/libc-bottom-half/sources/__wasilibc_random.c b/libc-bottom-half/sources/__wasilibc_random.c index 054f7c08d..c62bf357a 100644 --- a/libc-bottom-half/sources/__wasilibc_random.c +++ b/libc-bottom-half/sources/__wasilibc_random.c @@ -3,6 +3,7 @@ #include #ifndef __wasip1__ + #include #include #include @@ -14,18 +15,18 @@ typedef wasip2_list_u8_t list_u8_t; #elif defined(__wasip3__) typedef wasip3_list_u8_t list_u8_t; #define list_u8_free wasip3_list_u8_free -#else +#elif !defined(__webp2__) #error "Unknown WASI version" #endif int __wasilibc_random(void *buffer, size_t len) { - +#if defined(__wasip2__) || defined(__wasip3__) // Set up a WASI byte list to receive the results list_u8_t wasi_list; - + // Get random bytes random_get_random_bytes(len, &wasi_list); - + // The spec for get-random-bytes specifies that wasi_list.len // will be equal to len. if (wasi_list.len != len) @@ -37,7 +38,49 @@ int __wasilibc_random(void *buffer, size_t len) { // Free the WASI byte list list_u8_free(&wasi_list); +#elif defined(__webp2__) + webp2_own_crypto_impl_t crypto = webp2_get_crypto(); + const size_t MAX_CHUNK_SIZE = 65536; + + // Attempt to reduce copies by repeatedly reusing the same Uint8Array. This + // is obviously extremely jank, and doesn't avoid the obvious extra copy + // (host -> ret -> buffer). But at least we reduce allocation and don't copy + // the param. + webp2_own_uint_8_array_t arr = webp2_constructor_uint_8_array(MAX_CHUNK_SIZE); + for (size_t done = 0; done < len;) { + size_t remaining = len - done; + if (remaining < MAX_CHUNK_SIZE) { + // "Resize" the input array. + uint32_t remaining32 = remaining; + webp2_own_uint_8_array_t arr2 = webp2_method_uint_8_array_subarray( + webp2_borrow_uint_8_array(arr), + 0, &remaining32 + ); + webp2_uint_8_array_drop_own(arr); + arr = arr2; + } + + webp2_list_u8_t ret; + webp2_method_crypto_impl_get_random_values( + webp2_borrow_crypto_impl(crypto), + webp2_borrow_uint_8_array(arr), + &ret + ); + if (ret.len != (remaining < MAX_CHUNK_SIZE ? remaining : MAX_CHUNK_SIZE)) { + _Exit(EX_OSERR); + } + memcpy((char*)buffer + done, ret.ptr, ret.len); + done += ret.len; + webp2_list_u8_free(&ret); + } + + webp2_uint_8_array_drop_own(arr); + webp2_crypto_impl_drop_own(crypto); +#else +# error "Unknown WASI version" +#endif return 0; } + #endif diff --git a/libc-bottom-half/sources/webp2.c b/libc-bottom-half/sources/webp2.c index 095dbc06b..a2199944a 100644 --- a/libc-bottom-half/sources/webp2.c +++ b/libc-bottom-half/sources/webp2.c @@ -11,6 +11,9 @@ extern int32_t __wasm_import_webp2_get_performance(void); __attribute__((__import_module__("$root"), __import_name__("report-error"))) extern void __wasm_import_webp2_report_error(int32_t); +__attribute__((__import_module__("$root"), __import_name__("[get]crypto"))) +extern int32_t __wasm_import_webp2_get_crypto(void); + __attribute__((__import_module__("$root"), __import_name__("[method]performance-impl.now"))) extern double __wasm_import_webp2_method_performance_impl_now(int32_t); @@ -23,6 +26,15 @@ extern double __wasm_import_webp2_static_date_now(void); __attribute__((__import_module__("$root"), __import_name__("[constructor]error"))) extern int32_t __wasm_import_webp2_constructor_error(int32_t, uint8_t *, size_t, int32_t, int32_t, uint8_t *, size_t); +__attribute__((__import_module__("$root"), __import_name__("[constructor]uint-8-array"))) +extern int32_t __wasm_import_webp2_constructor_uint_8_array(int32_t); + +__attribute__((__import_module__("$root"), __import_name__("[method]uint-8-array.subarray"))) +extern int32_t __wasm_import_webp2_method_uint_8_array_subarray(int32_t, int32_t, int32_t, int32_t); + +__attribute__((__import_module__("$root"), __import_name__("[method]crypto-impl.get-random-values"))) +extern void __wasm_import_webp2_method_crypto_impl_get_random_values(int32_t, int32_t, uint8_t *); + // Canonical ABI intrinsics __attribute__((__weak__, __export_name__("cabi_realloc"))) @@ -79,12 +91,49 @@ webp2_borrow_error_t webp2_borrow_error(webp2_own_error_t arg) { return (webp2_borrow_error_t) { arg.__handle }; } +__attribute__((__import_module__("$root"), __import_name__("[resource-drop]uint-8-array"))) +extern void __wasm_import_webp2_uint_8_array_drop(int32_t handle); + +void webp2_uint_8_array_drop_own(webp2_own_uint_8_array_t handle) { + __wasm_import_webp2_uint_8_array_drop(handle.__handle); +} + +webp2_borrow_uint_8_array_t webp2_borrow_uint_8_array(webp2_own_uint_8_array_t arg) { + return (webp2_borrow_uint_8_array_t) { arg.__handle }; +} + +__attribute__((__import_module__("$root"), __import_name__("[resource-drop]crypto-impl"))) +extern void __wasm_import_webp2_crypto_impl_drop(int32_t handle); + +void webp2_crypto_impl_drop_own(webp2_own_crypto_impl_t handle) { + __wasm_import_webp2_crypto_impl_drop(handle.__handle); +} + +webp2_borrow_crypto_impl_t webp2_borrow_crypto_impl(webp2_own_crypto_impl_t arg) { + return (webp2_borrow_crypto_impl_t) { arg.__handle }; +} + void webp2_option_new_error_options_free(webp2_option_new_error_options_t *ptr) { if (ptr->is_some) { webp2_new_error_options_free(&ptr->val); } } +void webp2_option_u32_free(webp2_option_u32_t *ptr) { + if (ptr->is_some) { + } +} + +void webp2_list_u8_free(webp2_list_u8_t *ptr) { + size_t list_len = ptr->len; + if (list_len > 0) { + uint8_t *list_ptr = ptr->ptr; + for (size_t i = 0; i < list_len; i++) { + } + free(list_ptr); + } +} + void webp2_string_set(webp2_string_t *ret, const char*s) { ret->ptr = (uint8_t*) s; ret->len = strlen(s); @@ -121,6 +170,11 @@ void webp2_report_error(webp2_borrow_error_t throwable) { __wasm_import_webp2_report_error((throwable).__handle); } +webp2_own_crypto_impl_t webp2_get_crypto(void) { + int32_t ret = __wasm_import_webp2_get_crypto(); + return (webp2_own_crypto_impl_t) { ret }; +} + double webp2_method_performance_impl_now(webp2_borrow_performance_impl_t self) { double ret = __wasm_import_webp2_method_performance_impl_now((self).__handle); return ret; @@ -191,6 +245,38 @@ webp2_own_error_t webp2_constructor_error(webp2_string_t *maybe_message, webp2_n return (webp2_own_error_t) { ret }; } +webp2_own_uint_8_array_t webp2_constructor_uint_8_array(uint32_t length) { + int32_t ret = __wasm_import_webp2_constructor_uint_8_array((int32_t) (length)); + return (webp2_own_uint_8_array_t) { ret }; +} + +webp2_own_uint_8_array_t webp2_method_uint_8_array_subarray(webp2_borrow_uint_8_array_t self, uint32_t begin, uint32_t *maybe_end) { + webp2_option_u32_t end; + end.is_some = maybe_end != NULL;if (maybe_end) { + end.val = *maybe_end; + } + int32_t option; + int32_t option1; + if ((end).is_some) { + const uint32_t *payload0 = &(end).val; + option = 1; + option1 = (int32_t) (*payload0); + } else { + option = 0; + option1 = 0; + } + int32_t ret = __wasm_import_webp2_method_uint_8_array_subarray((self).__handle, (int32_t) (begin), option, option1); + return (webp2_own_uint_8_array_t) { ret }; +} + +void webp2_method_crypto_impl_get_random_values(webp2_borrow_crypto_impl_t self, webp2_borrow_uint_8_array_t typed_array, webp2_list_u8_t *ret) { + __attribute__((__aligned__(sizeof(void*)))) + uint8_t ret_area[(2*sizeof(void*))]; + uint8_t *ptr = (uint8_t *) &ret_area; + __wasm_import_webp2_method_crypto_impl_get_random_values((self).__handle, (typed_array).__handle, ptr); + *ret = (webp2_list_u8_t) { (uint8_t*)(*((uint8_t **) (ptr + 0))), (*((size_t*) (ptr + sizeof(void*)))) }; +} + // Ensure that the *_component_type.o object is linked in extern void __component_type_object_force_link_webp2(void); diff --git a/libc-bottom-half/sources/webp2_component_type.o b/libc-bottom-half/sources/webp2_component_type.o index c8defc2a80ef55a0c7a637f5ca930c164bbdf74a..d150b94cd2cf6467a0295dd17285d36f35156e5e 100644 GIT binary patch delta 425 zcmY+A!A`&yUlis>A~;{5+1^nPvX_9 zui@FH!~`#snPmQN{`tSmPwUM!ej4k+WAod@*ueO99X+imX_q`x-o58b=~oogZ~|H7 zN_afY6ByP*JyC!Yj0kB`(w#(1O>-vaLZ*HeFtiXiFhWbkQoQ)HgdKorvShiC%41Rr z2?yNX0@{=~HDG(Ds{{@aYckA&CG*avgKHdZ?%?VA40qMV!UWfJ!%kUI_z^Q~kt;5; z;+XU(=}iLNDp>|f$m#CX3Am++R>HDK#T7ZF#QnFn|1!=HTUb;W(b_`4F=G+WD$26( zoGi5VYLYS)i*ULx8y4lPjtm!wZRD@oVXb!^Rpl()<=H2fl-&I3_ktbj2tDa%p-8=@ MpOmb?VDo|ZUoo3_k^lez delta 155 zcmX@jc7?tLb%EqUMoC7V$-5b)q<9(lm>Gnklk@Y6ONvU9OY)0iA)5IoFJW2^0BFD| Axc~qF diff --git a/web/p2/wit/wasi-libc.wit b/web/p2/wit/wasi-libc.wit index e497d7893..b2ef69017 100644 --- a/web/p2/wit/wasi-libc.wit +++ b/web/p2/wit/wasi-libc.wit @@ -19,4 +19,25 @@ world webp2 { constructor(message: option, options: option); // TODO(webp2): message -> option } import report-error: func(throwable: borrow); + + resource uint-8-array { + constructor(length: u32); + subarray: func(begin: u32, end: option) -> uint-8-array; + } + + @external-id("Crypto") + resource crypto-impl { + // TODO(webp2): This is very jank, but basically, the whole design of + // Crypto.getRandomValues() doesn't make sense for the component model + // because it modifies its parameter in place. The component model has + // no concept of an out-parameter (nor should it, frankly), but in this + // case we have no choice but to uselessly copy an entire array for it + // to fill "in place" and then copy back to us. By passing a handle to + // a pre-existing Uint8Array we can at least avoid the first copy. + // + // We desperately need to find a better way to do this. Some new + // builtin? New web APIs? + get-random-values: func(typed-array: borrow) -> list; + } + import crypto: get() -> crypto-impl; } From 648d44270406272a2623be30e33339ca542d6b8d Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 3 Oct 2026 18:13:55 -0500 Subject: [PATCH 09/96] Always trap on exit --- libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c index aace6c8e5..31d255b7b 100644 --- a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c +++ b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c @@ -19,8 +19,10 @@ noreturn void _Exit(int status) { #elif defined(__webp2__) if (status != 0) { __wasilibc_report_error("exit with nonzero status"); - __builtin_trap(); } + // We really don't have any other way to halt execution except to trap. Quite + // unfortunate that this will manifest as a RuntimeError. + __builtin_trap(); #else # error "Unknown WASI version" #endif From 4027b903523553bc920a520b7623230fc2f27618 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 3 Oct 2026 22:05:05 -0500 Subject: [PATCH 10/96] Main ramblings --- libc-bottom-half/sources/__main_void.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libc-bottom-half/sources/__main_void.c b/libc-bottom-half/sources/__main_void.c index b6a412470..bc504af02 100644 --- a/libc-bottom-half/sources/__main_void.c +++ b/libc-bottom-half/sources/__main_void.c @@ -116,6 +116,18 @@ __attribute__((__weak__, nodebug)) int __main_void(void) { // Call `__main_argc_argv` with the arguments! return __main_argc_argv(argc, argv); +#elif defined(__webp2__) + // TODO(webp2): I think what we actually want is some kind of reactor-ish but + // not quite reactor model, where a main function will be registered as a + // component start function but called with no arguments. This is analogous + // to the arbitrary side effect code that runs when you import a JS module. + // Core wasm start functions are almost certainly not what you want for many + // reasons, but particularly the lack of any ability to, you know, call out + // to the host. That will require component start functions to be somewhat + // more standardized than they are, which unfortunately might require more + // standardization of value imports/exports. + static char* argv[] = { NULL }; + return __main_argc_argv(0, argv); #else #error "Unknown WASI version" #endif From 7f2fa0f2593a720a75a197765213cc978f2515e4 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 3 Oct 2026 22:44:08 -0500 Subject: [PATCH 11/96] Empty environment for web --- libc-bottom-half/sources/__wasilibc_initialize_environ.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libc-bottom-half/sources/__wasilibc_initialize_environ.c b/libc-bottom-half/sources/__wasilibc_initialize_environ.c index f2557c130..d88ca6c22 100644 --- a/libc-bottom-half/sources/__wasilibc_initialize_environ.c +++ b/libc-bottom-half/sources/__wasilibc_initialize_environ.c @@ -137,6 +137,8 @@ void __wasilibc_initialize_environ(void) { software: list_tuple2_string_string_free(&wasi_environment); _Exit(EX_SOFTWARE); +#elif defined(__webp2__) + __wasilibc_environ = empty_environ; #else #error "Unsupported WASI version" #endif From 24fe7021dadb95267ff377d61a42acf60c316e80 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 3 Oct 2026 22:44:22 -0500 Subject: [PATCH 12/96] Use many randomness hooray --- libc-bottom-half/sources/getentropy.c | 2 +- libc-top-half/musl/src/env/__stack_chk_fail.c | 6 +++--- libc-top-half/sources/arc4random.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libc-bottom-half/sources/getentropy.c b/libc-bottom-half/sources/getentropy.c index 4bb5b7f49..938711cf6 100644 --- a/libc-bottom-half/sources/getentropy.c +++ b/libc-bottom-half/sources/getentropy.c @@ -11,7 +11,7 @@ int __getentropy(void *buffer, size_t len) { #if defined(__wasip1__) int r = __wasi_random_get(buffer, len); -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) int r = __wasilibc_random(buffer, len); #else #error "Unsupported WASI version" diff --git a/libc-top-half/musl/src/env/__stack_chk_fail.c b/libc-top-half/musl/src/env/__stack_chk_fail.c index 17d77d890..023571fac 100644 --- a/libc-top-half/musl/src/env/__stack_chk_fail.c +++ b/libc-top-half/musl/src/env/__stack_chk_fail.c @@ -46,9 +46,9 @@ static void __wasilibc_init_ssp(void) { uintptr_t entropy; #if defined(__wasip1__) int r = __wasi_random_get((uint8_t *)&entropy, sizeof(uintptr_t)); -#elif defined(__wasip2__) || defined(__wasip3__) - int len = sizeof(uintptr_t); - int r = __wasilibc_random(&entropy, len); +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) + int len = sizeof(uintptr_t); + int r = __wasilibc_random(&entropy, len); #else # error "Unknown WASI version" #endif diff --git a/libc-top-half/sources/arc4random.c b/libc-top-half/sources/arc4random.c index f108a92aa..840105fb4 100644 --- a/libc-top-half/sources/arc4random.c +++ b/libc-top-half/sources/arc4random.c @@ -17,7 +17,7 @@ void arc4random_buf(void* buffer, size_t len) // raw-entropy-style implementation. #if defined(__wasip1__) int r = __wasi_random_get(buffer, len); -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) int r = __wasilibc_random(buffer, len); #else # error "Unknown WASI version" From 1cecdc154d84c3ea0d502b028c5bd47afdbf150b Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 3 Oct 2026 23:00:15 -0500 Subject: [PATCH 13/96] Actually build enough to get linker errors!! --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index e1916f216..d4efb68b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -210,6 +210,12 @@ add_link_options( -resource-dir ${tmp_resource_dir} ) +if(__webp2__) + # TODO(webp2): Manually instruct clang to look at our new webp2 sysroot until + # it has been patched in wasi-sdk to know about "webp2" as an OS. + add_link_options(-B${SYSROOT_LIB} -L${SYSROOT_LIB}) +endif() + # Expose the public headers to the implementation. We use `-isystem` for # purpose for two reasons: # From 7b6bc3a2e090a3d12e2c83aab7c0d7992de37d00 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 3 Oct 2026 23:40:26 -0500 Subject: [PATCH 14/96] Slog through many linker errors --- CMakeLists.txt | 2 + libc-bottom-half/CMakeLists.txt | 6 ++ .../sources/webp2_file_descriptors.c | 37 ++++++++++++ libc-bottom-half/sources/webp2_filesystem.c | 57 ++++++++++++++++++- .../headers/private/wasi/report-error.h | 9 +++ 5 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 libc-bottom-half/sources/webp2_file_descriptors.c diff --git a/CMakeLists.txt b/CMakeLists.txt index d4efb68b0..d363da7a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -384,6 +384,8 @@ if (WASI STREQUAL "p2") set(libc_extra_objects libc-bottom-half/sources/wasip2_component_type.o) elseif (WASI STREQUAL "p3") set(libc_extra_objects libc-bottom-half/sources/wasip3_component_type.o) +elseif (WEB STREQUAL "p2") + set(libc_extra_objects libc-bottom-half/sources/webp2_component_type.o) endif() add_library(c SHARED EXCLUDE_FROM_ALL diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index 31ffde37d..92af689ed 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -222,8 +222,14 @@ endif() # webp2-specific if (WEB STREQUAL "p2") + set_source_files_properties( + sources/webp2_file_descriptors.c + sources/webp2_filesystem.c + PROPERTIES COMPILE_OPTIONS -Wno-unused-parameter + ) list(APPEND bottom_half_sources sources/webp2.c + sources/webp2_file_descriptors.c sources/webp2_filesystem.c ) endif() diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c new file mode 100644 index 000000000..448cd6ee4 --- /dev/null +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -0,0 +1,37 @@ +// All things file-descriptory are stubbed out on the web, since there is no +// comparable model of files and streams. + +#include "sys/uio.h" +#include "wasi/report-error.h" + +int __dup3(int fd, int newfd, int flags) { + WEBP2_UNSUPPORTED("dup3"); +} + +int __isatty(int fd) { + WEBP2_UNSUPPORTED("isatty"); +} + +off_t __lseek(int fildes, off_t offset, int whence) { + WEBP2_UNSUPPORTED("lseek"); +} + +int close(int fd) { + WEBP2_UNSUPPORTED("close"); +} + +int fcntl(int fildes, int cmd, ...) { + WEBP2_UNSUPPORTED("fcntl"); +} + +ssize_t read(int fildes, void *buf, size_t nbyte) { + WEBP2_UNSUPPORTED("read"); +} + +ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { + WEBP2_UNSUPPORTED("readv"); +} + +ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { + WEBP2_UNSUPPORTED("writev"); +} diff --git a/libc-bottom-half/sources/webp2_filesystem.c b/libc-bottom-half/sources/webp2_filesystem.c index 04b73c073..e26674f5a 100644 --- a/libc-bottom-half/sources/webp2_filesystem.c +++ b/libc-bottom-half/sources/webp2_filesystem.c @@ -1 +1,56 @@ -// There is no filesystem. \ No newline at end of file +// There is no filesystem. + +#include "__struct_stat.h" +#include <__typedef_DIR.h> +#include "dirent.h" +#include "wasi/report-error.h" + +int __wasilibc_open_nomode(const char *path, int oflag) { + WEBP2_UNSUPPORTED("__wasilibc_open_nomode"); +} + +int closedir(DIR *dirp) { + WEBP2_UNSUPPORTED("closedir"); +} + +int dirfd(DIR *dirp) { + WEBP2_UNSUPPORTED("dirfd"); +} + +DIR *fdopendir(int fd) { + WEBP2_UNSUPPORTED("fdopendir"); +} + +char *getcwd(char *buf, size_t size) { + WEBP2_UNSUPPORTED("getcwd"); +} + +int lstat(const char *restrict path, struct stat *restrict buf) { + WEBP2_UNSUPPORTED("lstat"); +} + +int open(const char *path, int oflag, ...) { + WEBP2_UNSUPPORTED("open"); +} + +DIR *opendir(const char *dirname) { + WEBP2_UNSUPPORTED("opendir"); +} + +struct dirent *readdir(DIR *dirp) { + WEBP2_UNSUPPORTED("readdir"); +} + +ssize_t readlink(const char *restrict path, char *restrict buf, + size_t bufsize) { + WEBP2_UNSUPPORTED("readlink"); +} + +int stat(const char *restrict path, struct stat *restrict buf) { + WEBP2_UNSUPPORTED("stat"); +} + +int utimensat(int dirfd, const char *pathname, const struct timespec times[2], + int flags) { + WEBP2_UNSUPPORTED("utimensat"); +} diff --git a/libc-top-half/headers/private/wasi/report-error.h b/libc-top-half/headers/private/wasi/report-error.h index 8a7a0c16e..9597c7fcd 100644 --- a/libc-top-half/headers/private/wasi/report-error.h +++ b/libc-top-half/headers/private/wasi/report-error.h @@ -2,7 +2,16 @@ #define __WASI_REPORT_ERROR_H #include +#include hidden void __wasilibc_report_error(const char* message); +#ifdef __webp2__ +#define WEBP2_UNSUPPORTED(name) \ + do { \ + __wasilibc_report_error(name " is not supported on the web"); \ + __builtin_trap(); \ + } while (0) +#endif + #endif From e4e27cf6440a624a7a01264c0881cc10400c0d11 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sun, 4 Oct 2026 14:15:43 -0500 Subject: [PATCH 15/96] Exit 0!!! --- libc-bottom-half/CMakeLists.txt | 9 +++++++-- libc-bottom-half/headers/private/wasi/descriptor_table.h | 2 ++ libc-bottom-half/sources/webp2_filesystem.c | 4 ++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index 92af689ed..406387a87 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -259,8 +259,13 @@ endforeach() # Clang will link by default based on compiler flags. These are compiled into # "object libraries" with CMake and then they're copied into the final location # with adjustments based on WASI versions. -foreach(file crt/crt1-command.c - crt/crt1-reactor.c) + +set(crt_sources crt/crt1-reactor.c) +if (NOT WEB STREQUAL "p2") + list(APPEND crt_sources crt/crt1-command.c) +endif() + +foreach(file ${crt_sources}) cmake_path(GET file STEM stem) add_library(${stem} OBJECT ${file}) clang_format_target(${stem}) diff --git a/libc-bottom-half/headers/private/wasi/descriptor_table.h b/libc-bottom-half/headers/private/wasi/descriptor_table.h index 83a357f99..f0bbe5bc2 100644 --- a/libc-bottom-half/headers/private/wasi/descriptor_table.h +++ b/libc-bottom-half/headers/private/wasi/descriptor_table.h @@ -11,6 +11,7 @@ #include #include +#ifndef __webp2__ #ifdef __wasip3__ #include "lock.h" @@ -364,5 +365,6 @@ void __wasilibc_assert_no_descriptor_leaks(void); #endif #endif // __wasip1__ +#endif // __webp2__ #endif // DESCRIPTOR_TABLE_H diff --git a/libc-bottom-half/sources/webp2_filesystem.c b/libc-bottom-half/sources/webp2_filesystem.c index e26674f5a..8a1db1033 100644 --- a/libc-bottom-half/sources/webp2_filesystem.c +++ b/libc-bottom-half/sources/webp2_filesystem.c @@ -37,6 +37,10 @@ DIR *opendir(const char *dirname) { WEBP2_UNSUPPORTED("opendir"); } +ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset) { + WEBP2_UNSUPPORTED("pread"); +} + struct dirent *readdir(DIR *dirp) { WEBP2_UNSUPPORTED("readdir"); } From 69112195ce4c76724729821e9b53f06c113a6b3d Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 5 Oct 2026 14:16:19 -0500 Subject: [PATCH 16/96] Set -fvisibiliyt-hidden on webp2.c --- libc-bottom-half/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index 406387a87..320b27b91 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -240,6 +240,8 @@ set_source_files_properties(sources/wasip2.c PROPERTIES COMPILE_OPTIONS -fvisibility=hidden) set_source_files_properties(sources/wasip3.c PROPERTIES COMPILE_OPTIONS -fvisibility=hidden) +set_source_files_properties(sources/webp2.c PROPERTIES + COMPILE_OPTIONS -fvisibility=hidden) add_object_library(bottom-half ${bottom_half_sources}) foreach(obj bottom-half-shared bottom-half-static) From 8bdaabdc360b84244928f15cacafbd1bb146e84f Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 5 Oct 2026 14:45:35 -0500 Subject: [PATCH 17/96] Hopefully fix CI errors --- .github/workflows/main.yml | 3 +++ cmake/bindings.cmake | 2 +- .../wasm32-wasip1-threads/defined-symbols.txt | 2 ++ expected/wasm32-wasip1-threads/include-all.c | 1 + .../predefined-macros.txt | 3 ++- expected/wasm32-wasip1/defined-symbols.txt | 2 ++ expected/wasm32-wasip1/include-all.c | 1 + expected/wasm32-wasip1/predefined-macros.txt | 3 ++- expected/wasm32-wasip2/defined-symbols.txt | 2 ++ expected/wasm32-wasip2/include-all.c | 1 + expected/wasm32-wasip2/predefined-macros.txt | 3 ++- .../wasm32-wasip3-coop/defined-symbols.txt | 2 ++ expected/wasm32-wasip3-coop/include-all.c | 1 + .../wasm32-wasip3-coop/predefined-macros.txt | 3 ++- expected/wasm32-wasip3/defined-symbols.txt | 2 ++ expected/wasm32-wasip3/include-all.c | 1 + expected/wasm32-wasip3/predefined-macros.txt | 3 ++- .../headers/public/wasi/__generated_wasip2.h | 2 +- .../headers/public/wasi/__generated_wasip3.h | 2 +- libc-bottom-half/sources/__main_void.c | 2 +- libc-bottom-half/sources/__wasilibc_random.c | 17 ++++++----------- .../sources/__wasilibc_report_error.c | 14 +++++++------- .../sources/__wasilibc_report_errorf.c | 2 +- libc-bottom-half/sources/wasip2.c | 2 +- .../sources/wasip2_component_type.o | Bin 10911 -> 10911 bytes libc-bottom-half/sources/wasip3.c | 2 +- .../sources/wasip3_component_type.o | Bin 8007 -> 8007 bytes 27 files changed, 48 insertions(+), 30 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 85a03ab24..6ad78a34f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -279,6 +279,9 @@ jobs: - uses: actions/checkout@v6 with: submodules: true + # TODO(webp2): Switch back to a released wit-bindgen once one ships with + # getter/setter support (bytecodealliance/wit-bindgen#1726). + - run: cargo install wit-bindgen-cli --locked --git https://github.com/bytecodealliance/wit-bindgen --rev 7fb7a6a072a2c7d225fa183b93acfabdc36e744b - run: CC=clang cmake -S . -B build -G Ninja - run: ninja -C build bindings - run: git diff --exit-code diff --git a/cmake/bindings.cmake b/cmake/bindings.cmake index 8db9b2ec3..e9da59e29 100644 --- a/cmake/bindings.cmake +++ b/cmake/bindings.cmake @@ -26,7 +26,7 @@ if (NOT WIT_BINDGEN_EXECUTABLE) ba_download( wit-bindgen "https://github.com/bytecodealliance/wit-bindgen" - "0.60.0" + "0.62.0" ) ExternalProject_Get_Property(wit-bindgen SOURCE_DIR) set(wit_bindgen "${SOURCE_DIR}/wit-bindgen") diff --git a/expected/wasm32-wasip1-threads/defined-symbols.txt b/expected/wasm32-wasip1-threads/defined-symbols.txt index d8a4c8c77..0ccd9a729 100644 --- a/expected/wasm32-wasip1-threads/defined-symbols.txt +++ b/expected/wasm32-wasip1-threads/defined-symbols.txt @@ -397,6 +397,8 @@ __wasilibc_pthread_self __wasilibc_register_preopened_fd __wasilibc_rename_newat __wasilibc_rename_oldat +__wasilibc_report_error +__wasilibc_report_errorf __wasilibc_reset_preopens __wasilibc_rmdirat __wasilibc_stat diff --git a/expected/wasm32-wasip1-threads/include-all.c b/expected/wasm32-wasip1-threads/include-all.c index 8c6422925..4161520f2 100644 --- a/expected/wasm32-wasip1-threads/include-all.c +++ b/expected/wasm32-wasip1-threads/include-all.c @@ -174,5 +174,6 @@ #include #include #include +#include #include #include diff --git a/expected/wasm32-wasip1-threads/predefined-macros.txt b/expected/wasm32-wasip1-threads/predefined-macros.txt index c1c3749d7..859cb1137 100644 --- a/expected/wasm32-wasip1-threads/predefined-macros.txt +++ b/expected/wasm32-wasip1-threads/predefined-macros.txt @@ -3060,7 +3060,8 @@ #define __WASI_SUBCLOCKFLAGS_SUBSCRIPTION_CLOCK_ABSTIME ((__wasi_subclockflags_t)(1 << 0)) #define __WASI_VERSION_H #define __WASI_WASIP2_H -#define __WASI_WASIP3_H +#define __WASI_WASIP3_H +#define __WASI_WEBP2_H #define __WASI_WHENCE_CUR (UINT8_C(1)) #define __WASI_WHENCE_END (UINT8_C(2)) #define __WASI_WHENCE_SET (UINT8_C(0)) diff --git a/expected/wasm32-wasip1/defined-symbols.txt b/expected/wasm32-wasip1/defined-symbols.txt index ce4d2c21d..790038b41 100644 --- a/expected/wasm32-wasip1/defined-symbols.txt +++ b/expected/wasm32-wasip1/defined-symbols.txt @@ -385,6 +385,8 @@ __wasilibc_pthread_self __wasilibc_register_preopened_fd __wasilibc_rename_newat __wasilibc_rename_oldat +__wasilibc_report_error +__wasilibc_report_errorf __wasilibc_reset_preopens __wasilibc_rmdirat __wasilibc_stat diff --git a/expected/wasm32-wasip1/include-all.c b/expected/wasm32-wasip1/include-all.c index 8c6422925..4161520f2 100644 --- a/expected/wasm32-wasip1/include-all.c +++ b/expected/wasm32-wasip1/include-all.c @@ -174,5 +174,6 @@ #include #include #include +#include #include #include diff --git a/expected/wasm32-wasip1/predefined-macros.txt b/expected/wasm32-wasip1/predefined-macros.txt index b32032660..ff77bdd13 100644 --- a/expected/wasm32-wasip1/predefined-macros.txt +++ b/expected/wasm32-wasip1/predefined-macros.txt @@ -3059,7 +3059,8 @@ #define __WASI_SUBCLOCKFLAGS_SUBSCRIPTION_CLOCK_ABSTIME ((__wasi_subclockflags_t)(1 << 0)) #define __WASI_VERSION_H #define __WASI_WASIP2_H -#define __WASI_WASIP3_H +#define __WASI_WASIP3_H +#define __WASI_WEBP2_H #define __WASI_WHENCE_CUR (UINT8_C(1)) #define __WASI_WHENCE_END (UINT8_C(2)) #define __WASI_WHENCE_SET (UINT8_C(0)) diff --git a/expected/wasm32-wasip2/defined-symbols.txt b/expected/wasm32-wasip2/defined-symbols.txt index bfae8453c..14f58d0c7 100644 --- a/expected/wasm32-wasip2/defined-symbols.txt +++ b/expected/wasm32-wasip2/defined-symbols.txt @@ -357,6 +357,8 @@ __wasilibc_random __wasilibc_read __wasilibc_rename_newat __wasilibc_rename_oldat +__wasilibc_report_error +__wasilibc_report_errorf __wasilibc_reset_preopens __wasilibc_rmdirat __wasilibc_setsockopt_timeout diff --git a/expected/wasm32-wasip2/include-all.c b/expected/wasm32-wasip2/include-all.c index 21b3d78ca..a408d8e4f 100644 --- a/expected/wasm32-wasip2/include-all.c +++ b/expected/wasm32-wasip2/include-all.c @@ -176,5 +176,6 @@ #include #include #include +#include #include #include diff --git a/expected/wasm32-wasip2/predefined-macros.txt b/expected/wasm32-wasip2/predefined-macros.txt index 18a526b18..7a9b33cf7 100644 --- a/expected/wasm32-wasip2/predefined-macros.txt +++ b/expected/wasm32-wasip2/predefined-macros.txt @@ -3060,7 +3060,8 @@ #define __USE_TIME_BITS64 1 #define __WASI_VERSION_H #define __WASI_WASIP2_H -#define __WASI_WASIP3_H +#define __WASI_WASIP3_H +#define __WASI_WEBP2_H #define __WCHAR_TYPE__ int #define __WCHAR_WIDTH__ 32 #define __WINT_TYPE__ int diff --git a/expected/wasm32-wasip3-coop/defined-symbols.txt b/expected/wasm32-wasip3-coop/defined-symbols.txt index bb630702e..2e7aa8719 100644 --- a/expected/wasm32-wasip3-coop/defined-symbols.txt +++ b/expected/wasm32-wasip3-coop/defined-symbols.txt @@ -385,6 +385,8 @@ __wasilibc_read __wasilibc_read_poll __wasilibc_rename_newat __wasilibc_rename_oldat +__wasilibc_report_error +__wasilibc_report_errorf __wasilibc_reset_preopens __wasilibc_rmdirat __wasilibc_setsockopt_timeout diff --git a/expected/wasm32-wasip3-coop/include-all.c b/expected/wasm32-wasip3-coop/include-all.c index 112d20de0..fb7aff2f0 100644 --- a/expected/wasm32-wasip3-coop/include-all.c +++ b/expected/wasm32-wasip3-coop/include-all.c @@ -176,5 +176,6 @@ #include #include #include +#include #include #include diff --git a/expected/wasm32-wasip3-coop/predefined-macros.txt b/expected/wasm32-wasip3-coop/predefined-macros.txt index 6afca371b..cf9eaabc4 100644 --- a/expected/wasm32-wasip3-coop/predefined-macros.txt +++ b/expected/wasm32-wasip3-coop/predefined-macros.txt @@ -3067,7 +3067,8 @@ #define __USE_TIME_BITS64 1 #define __WASI_VERSION_H #define __WASI_WASIP2_H -#define __WASI_WASIP3_H +#define __WASI_WASIP3_H +#define __WASI_WEBP2_H #define __WCHAR_TYPE__ int #define __WCHAR_WIDTH__ 32 #define __WINT_TYPE__ int diff --git a/expected/wasm32-wasip3/defined-symbols.txt b/expected/wasm32-wasip3/defined-symbols.txt index 5c2c793b4..67173584a 100644 --- a/expected/wasm32-wasip3/defined-symbols.txt +++ b/expected/wasm32-wasip3/defined-symbols.txt @@ -359,6 +359,8 @@ __wasilibc_read __wasilibc_read_poll __wasilibc_rename_newat __wasilibc_rename_oldat +__wasilibc_report_error +__wasilibc_report_errorf __wasilibc_reset_preopens __wasilibc_rmdirat __wasilibc_setsockopt_timeout diff --git a/expected/wasm32-wasip3/include-all.c b/expected/wasm32-wasip3/include-all.c index 112d20de0..fb7aff2f0 100644 --- a/expected/wasm32-wasip3/include-all.c +++ b/expected/wasm32-wasip3/include-all.c @@ -176,5 +176,6 @@ #include #include #include +#include #include #include diff --git a/expected/wasm32-wasip3/predefined-macros.txt b/expected/wasm32-wasip3/predefined-macros.txt index a4aeb5959..56f75d2f5 100644 --- a/expected/wasm32-wasip3/predefined-macros.txt +++ b/expected/wasm32-wasip3/predefined-macros.txt @@ -3066,7 +3066,8 @@ #define __USE_TIME_BITS64 1 #define __WASI_VERSION_H #define __WASI_WASIP2_H -#define __WASI_WASIP3_H +#define __WASI_WASIP3_H +#define __WASI_WEBP2_H #define __WCHAR_TYPE__ int #define __WCHAR_WIDTH__ 32 #define __WINT_TYPE__ int diff --git a/libc-bottom-half/headers/public/wasi/__generated_wasip2.h b/libc-bottom-half/headers/public/wasi/__generated_wasip2.h index d961d7aa0..b1f7da370 100644 --- a/libc-bottom-half/headers/public/wasi/__generated_wasip2.h +++ b/libc-bottom-half/headers/public/wasi/__generated_wasip2.h @@ -1,4 +1,4 @@ -// Generated by `wit-bindgen` 0.60.0. DO NOT EDIT! +// Generated by `wit-bindgen` 0.62.0. DO NOT EDIT! #ifndef __BINDINGS_WASIP2_H #define __BINDINGS_WASIP2_H #ifdef __cplusplus diff --git a/libc-bottom-half/headers/public/wasi/__generated_wasip3.h b/libc-bottom-half/headers/public/wasi/__generated_wasip3.h index e867d7682..acdf940ab 100644 --- a/libc-bottom-half/headers/public/wasi/__generated_wasip3.h +++ b/libc-bottom-half/headers/public/wasi/__generated_wasip3.h @@ -1,4 +1,4 @@ -// Generated by `wit-bindgen` 0.60.0. DO NOT EDIT! +// Generated by `wit-bindgen` 0.62.0. DO NOT EDIT! #ifndef __BINDINGS_WASIP3_H #define __BINDINGS_WASIP3_H #ifdef __cplusplus diff --git a/libc-bottom-half/sources/__main_void.c b/libc-bottom-half/sources/__main_void.c index bc504af02..cc52e9c62 100644 --- a/libc-bottom-half/sources/__main_void.c +++ b/libc-bottom-half/sources/__main_void.c @@ -126,7 +126,7 @@ __attribute__((__weak__, nodebug)) int __main_void(void) { // to the host. That will require component start functions to be somewhat // more standardized than they are, which unfortunately might require more // standardization of value imports/exports. - static char* argv[] = { NULL }; + static char *argv[] = {NULL}; return __main_argc_argv(0, argv); #else #error "Unknown WASI version" diff --git a/libc-bottom-half/sources/__wasilibc_random.c b/libc-bottom-half/sources/__wasilibc_random.c index c62bf357a..5ce6d531f 100644 --- a/libc-bottom-half/sources/__wasilibc_random.c +++ b/libc-bottom-half/sources/__wasilibc_random.c @@ -23,10 +23,10 @@ int __wasilibc_random(void *buffer, size_t len) { #if defined(__wasip2__) || defined(__wasip3__) // Set up a WASI byte list to receive the results list_u8_t wasi_list; - + // Get random bytes random_get_random_bytes(len, &wasi_list); - + // The spec for get-random-bytes specifies that wasi_list.len // will be equal to len. if (wasi_list.len != len) @@ -53,23 +53,18 @@ int __wasilibc_random(void *buffer, size_t len) { // "Resize" the input array. uint32_t remaining32 = remaining; webp2_own_uint_8_array_t arr2 = webp2_method_uint_8_array_subarray( - webp2_borrow_uint_8_array(arr), - 0, &remaining32 - ); + webp2_borrow_uint_8_array(arr), 0, &remaining32); webp2_uint_8_array_drop_own(arr); arr = arr2; } webp2_list_u8_t ret; webp2_method_crypto_impl_get_random_values( - webp2_borrow_crypto_impl(crypto), - webp2_borrow_uint_8_array(arr), - &ret - ); + webp2_borrow_crypto_impl(crypto), webp2_borrow_uint_8_array(arr), &ret); if (ret.len != (remaining < MAX_CHUNK_SIZE ? remaining : MAX_CHUNK_SIZE)) { _Exit(EX_OSERR); } - memcpy((char*)buffer + done, ret.ptr, ret.len); + memcpy((char *)buffer + done, ret.ptr, ret.len); done += ret.len; webp2_list_u8_free(&ret); } @@ -77,7 +72,7 @@ int __wasilibc_random(void *buffer, size_t len) { webp2_uint_8_array_drop_own(arr); webp2_crypto_impl_drop_own(crypto); #else -# error "Unknown WASI version" +#error "Unknown WASI version" #endif return 0; diff --git a/libc-bottom-half/sources/__wasilibc_report_error.c b/libc-bottom-half/sources/__wasilibc_report_error.c index 7df453f82..af393d047 100644 --- a/libc-bottom-half/sources/__wasilibc_report_error.c +++ b/libc-bottom-half/sources/__wasilibc_report_error.c @@ -2,14 +2,14 @@ #include #include -void __wasilibc_report_error(const char* message) { +void __wasilibc_report_error(const char *message) { #if defined(__wasip1__) || defined(__wasip2__) || defined(__wasip3__) - fprintf(stderr, "%s\n", message); + fprintf(stderr, "%s\n", message); #elif defined(__webp2__) - webp2_string_t msg; - webp2_string_set(&msg, message); - webp2_own_error_t err = webp2_constructor_error(&msg, NULL); - webp2_report_error(webp2_borrow_error(err)); - webp2_error_drop_own(err); + webp2_string_t msg; + webp2_string_set(&msg, message); + webp2_own_error_t err = webp2_constructor_error(&msg, NULL); + webp2_report_error(webp2_borrow_error(err)); + webp2_error_drop_own(err); #endif } diff --git a/libc-bottom-half/sources/__wasilibc_report_errorf.c b/libc-bottom-half/sources/__wasilibc_report_errorf.c index f1f8f2d33..0f0bbe639 100644 --- a/libc-bottom-half/sources/__wasilibc_report_errorf.c +++ b/libc-bottom-half/sources/__wasilibc_report_errorf.c @@ -6,7 +6,7 @@ #if defined(__webp2__) // Ensure we have well-formed UTF-8 in case of truncation. Assumes valid UTF-8 // coming in. -static void fix_truncated_utf8(char* buf, size_t len) { +static void fix_truncated_utf8(char *buf, size_t len) { if (len == 0) { return; } diff --git a/libc-bottom-half/sources/wasip2.c b/libc-bottom-half/sources/wasip2.c index caf7f9399..3231d1a6e 100644 --- a/libc-bottom-half/sources/wasip2.c +++ b/libc-bottom-half/sources/wasip2.c @@ -1,4 +1,4 @@ -// Generated by `wit-bindgen` 0.60.0. DO NOT EDIT! +// Generated by `wit-bindgen` 0.62.0. DO NOT EDIT! #include "wasi/wasip2.h" #include #include diff --git a/libc-bottom-half/sources/wasip2_component_type.o b/libc-bottom-half/sources/wasip2_component_type.o index 197cfd45a0094330bca84a57ccb113b6d714edd5..22e1cbd04d1f6b8673d7ae28a8b92543ce8abdcc 100644 GIT binary patch delta 35 qcmbOqIzM!Sx0bk>ft~?xd1i@jQf6LCdTO3-GMj;(nbG7VEn5K7EDHtz delta 35 qcmbOqIzM!Sx0bl6iJk#(d1i@jQf6LCdTO3-GMj;(nZe{FEn5K7Zwm(i diff --git a/libc-bottom-half/sources/wasip3.c b/libc-bottom-half/sources/wasip3.c index a73a140c1..f1f9bca84 100644 --- a/libc-bottom-half/sources/wasip3.c +++ b/libc-bottom-half/sources/wasip3.c @@ -1,4 +1,4 @@ -// Generated by `wit-bindgen` 0.60.0. DO NOT EDIT! +// Generated by `wit-bindgen` 0.62.0. DO NOT EDIT! #include "wasi/wasip3.h" #include #include diff --git a/libc-bottom-half/sources/wasip3_component_type.o b/libc-bottom-half/sources/wasip3_component_type.o index e819df40548540f4044247af6279a48d7a21a2e1..cc10cf11735a90ec82cd2467a1795734e624a4d3 100644 GIT binary patch delta 35 qcmX?Zcie8nXE|{*13d%Y^2`$5q|CgO^wd1vWHtjmGo#5|^0ol!6boSh delta 35 qcmX?Zcie8nXE||G6Fmdo^2`$5q|CgO^wd1vWHtjmGlR)o^0ol!R|{eQ From 31dff18a63506a8a154eeaa8614ff73bb5875f61 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 6 Oct 2026 09:31:18 -0500 Subject: [PATCH 18/96] Ensure trapping stubs for weakly aliased things we excluded --- libc-bottom-half/sources/webp2_file_descriptors.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index 448cd6ee4..f713ca1c3 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -20,10 +20,22 @@ int close(int fd) { WEBP2_UNSUPPORTED("close"); } +int dup3(int fd, int newfd, int flags) { + WEBP2_UNSUPPORTED("dup3"); +} + int fcntl(int fildes, int cmd, ...) { WEBP2_UNSUPPORTED("fcntl"); } +int isatty(int fd) { + WEBP2_UNSUPPORTED("isatty"); +} + +off_t lseek(int fildes, off_t offset, int whence) { + WEBP2_UNSUPPORTED("lseek"); +} + ssize_t read(int fildes, void *buf, size_t nbyte) { WEBP2_UNSUPPORTED("read"); } From ef154ba373bf0c8670cc8cedc894920bc44f1c36 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 6 Oct 2026 10:18:14 -0500 Subject: [PATCH 19/96] Exclude some headers and add some stubs --- libc-bottom-half/CMakeLists.txt | 7 ++++++- libc-bottom-half/sources/webp2_filesystem.c | 9 +++++++++ libc-top-half/CMakeLists.txt | 6 +++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index 320b27b91..d29566f7e 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -31,7 +31,12 @@ if (NOT (WASI STREQUAL "p3")) list(REMOVE_ITEM globbed_headers headers/public/wasi/__generated_wasip3.h) endif() -if (NOT (WEB STREQUAL "p2")) +if (WEB STREQUAL "p2") + # Just remove some headers that contain only __wasilibc filesystem utility + # functions so that we don't have to stub them all out. + list(REMOVE_ITEM globbed_headers headers/public/wasi/libc-find-relpath.h) + list(REMOVE_ITEM globbed_headers headers/public/wasi/libc-nocwd.h) +else () list(REMOVE_ITEM globbed_headers headers/public/wasi/__generated_webp2.h) endif() diff --git a/libc-bottom-half/sources/webp2_filesystem.c b/libc-bottom-half/sources/webp2_filesystem.c index 8a1db1033..87bfd295e 100644 --- a/libc-bottom-half/sources/webp2_filesystem.c +++ b/libc-bottom-half/sources/webp2_filesystem.c @@ -2,6 +2,7 @@ #include "__struct_stat.h" #include <__typedef_DIR.h> +#include #include "dirent.h" #include "wasi/report-error.h" @@ -9,6 +10,14 @@ int __wasilibc_open_nomode(const char *path, int oflag) { WEBP2_UNSUPPORTED("__wasilibc_open_nomode"); } +void __wasilibc_populate_preopens(void) { + WEBP2_UNSUPPORTED("__wasilibc_populate_preopens"); +} + +void __wasilibc_reset_preopens(void) { + WEBP2_UNSUPPORTED("__wasilibc_reset_preopens"); +} + int closedir(DIR *dirp) { WEBP2_UNSUPPORTED("closedir"); } diff --git a/libc-top-half/CMakeLists.txt b/libc-top-half/CMakeLists.txt index 39c175b51..5e2116285 100644 --- a/libc-top-half/CMakeLists.txt +++ b/libc-top-half/CMakeLists.txt @@ -155,8 +155,8 @@ list(REMOVE_ITEM globbed_headers musl/include/wordexp.h ) -# Exclude `netdb.h` from all of the p1 targets. -if(WASI STREQUAL "p1") +# Exclude `netdb.h` from all of the p1 targets and webp2. +if(WASI STREQUAL "p1" OR WEB STREQUAL "p2") list(REMOVE_ITEM globbed_headers musl/include/netdb.h ) @@ -442,7 +442,7 @@ list(REMOVE_ITEM globbed_sources list(APPEND top_half_sources ${globbed_sources}) -if (NOT (WASI STREQUAL "p1")) +if (WASI STREQUAL "p2" OR WASI STREQUAL "p3") list(APPEND top_half_sources musl/src/network/gai_strerror.c ) From 42ad0d911d72b1b10f5608eb552f1a7f6d341046 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 6 Oct 2026 10:26:48 -0500 Subject: [PATCH 20/96] Stub remaining header symbols --- libc-bottom-half/CMakeLists.txt | 2 + .../sources/webp2_file_descriptors.c | 60 +++++ libc-bottom-half/sources/webp2_filesystem.c | 228 ++++++++++++++++++ libc-bottom-half/sources/webp2_socket.c | 74 ++++++ 4 files changed, 364 insertions(+) create mode 100644 libc-bottom-half/sources/webp2_socket.c diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index d29566f7e..a9b31217e 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -230,12 +230,14 @@ if (WEB STREQUAL "p2") set_source_files_properties( sources/webp2_file_descriptors.c sources/webp2_filesystem.c + sources/webp2_socket.c PROPERTIES COMPILE_OPTIONS -Wno-unused-parameter ) list(APPEND bottom_half_sources sources/webp2.c sources/webp2_file_descriptors.c sources/webp2_filesystem.c + sources/webp2_socket.c ) endif() diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index f713ca1c3..5e1b54e17 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -1,9 +1,17 @@ // All things file-descriptory are stubbed out on the web, since there is no // comparable model of files and streams. +#include +#include +#include +#include +#include #include "sys/uio.h" #include "wasi/report-error.h" +// unistd.h defines an `lseek` macro that would mangle the definition below. +#undef lseek + int __dup3(int fd, int newfd, int flags) { WEBP2_UNSUPPORTED("dup3"); } @@ -16,10 +24,22 @@ off_t __lseek(int fildes, off_t offset, int whence) { WEBP2_UNSUPPORTED("lseek"); } +off_t __wasilibc_tell(int fd) { + WEBP2_UNSUPPORTED("__wasilibc_tell"); +} + int close(int fd) { WEBP2_UNSUPPORTED("close"); } +int dup(int fd) { + WEBP2_UNSUPPORTED("dup"); +} + +int dup2(int fd, int newfd) { + WEBP2_UNSUPPORTED("dup2"); +} + int dup3(int fd, int newfd, int flags) { WEBP2_UNSUPPORTED("dup3"); } @@ -28,6 +48,14 @@ int fcntl(int fildes, int cmd, ...) { WEBP2_UNSUPPORTED("fcntl"); } +int fstat(int fildes, struct stat *buf) { + WEBP2_UNSUPPORTED("fstat"); +} + +int ioctl(int fildes, int request, ...) { + WEBP2_UNSUPPORTED("ioctl"); +} + int isatty(int fd) { WEBP2_UNSUPPORTED("isatty"); } @@ -36,6 +64,29 @@ off_t lseek(int fildes, off_t offset, int whence) { WEBP2_UNSUPPORTED("lseek"); } +int pipe(int fd[2]) { + WEBP2_UNSUPPORTED("pipe"); +} + +int pipe2(int fd[2], int flags) { + WEBP2_UNSUPPORTED("pipe2"); +} + +int poll(struct pollfd *fds, nfds_t nfds, int timeout) { + WEBP2_UNSUPPORTED("poll"); +} + +int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, + const sigset_t *sigmask) { + WEBP2_UNSUPPORTED("ppoll"); +} + +int pselect(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, + fd_set *restrict errorfds, const struct timespec *restrict timeout, + const sigset_t *restrict sigmask) { + WEBP2_UNSUPPORTED("pselect"); +} + ssize_t read(int fildes, void *buf, size_t nbyte) { WEBP2_UNSUPPORTED("read"); } @@ -44,6 +95,15 @@ ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { WEBP2_UNSUPPORTED("readv"); } +int select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, + fd_set *restrict errorfds, struct timeval *restrict timeout) { + WEBP2_UNSUPPORTED("select"); +} + +ssize_t write(int fildes, const void *buf, size_t nbyte) { + WEBP2_UNSUPPORTED("write"); +} + ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { WEBP2_UNSUPPORTED("writev"); } diff --git a/libc-bottom-half/sources/webp2_filesystem.c b/libc-bottom-half/sources/webp2_filesystem.c index 87bfd295e..49c35cd77 100644 --- a/libc-bottom-half/sources/webp2_filesystem.c +++ b/libc-bottom-half/sources/webp2_filesystem.c @@ -2,10 +2,35 @@ #include "__struct_stat.h" #include <__typedef_DIR.h> +#include +#include +#include +#include +#include +#include +#include #include #include "dirent.h" #include "wasi/report-error.h" +int __wasilibc_access(const char *pathname, int mode, int flags) { + WEBP2_UNSUPPORTED("__wasilibc_access"); +} + +int __wasilibc_link(const char *oldpath, const char *newpath, int flags) { + WEBP2_UNSUPPORTED("__wasilibc_link"); +} + +int __wasilibc_link_newat(const char *oldpath, int newdirfd, + const char *newpath, int flags) { + WEBP2_UNSUPPORTED("__wasilibc_link_newat"); +} + +int __wasilibc_link_oldat(int olddirfd, const char *oldpath, + const char *newpath, int flags) { + WEBP2_UNSUPPORTED("__wasilibc_link_oldat"); +} + int __wasilibc_open_nomode(const char *path, int oflag) { WEBP2_UNSUPPORTED("__wasilibc_open_nomode"); } @@ -14,10 +39,50 @@ void __wasilibc_populate_preopens(void) { WEBP2_UNSUPPORTED("__wasilibc_populate_preopens"); } +int __wasilibc_rename_newat(const char *oldpath, int newdirfd, + const char *newpath) { + WEBP2_UNSUPPORTED("__wasilibc_rename_newat"); +} + +int __wasilibc_rename_oldat(int olddirfd, const char *oldpath, + const char *newpath) { + WEBP2_UNSUPPORTED("__wasilibc_rename_oldat"); +} + void __wasilibc_reset_preopens(void) { WEBP2_UNSUPPORTED("__wasilibc_reset_preopens"); } +int __wasilibc_rmdirat(int fd, const char *path) { + WEBP2_UNSUPPORTED("__wasilibc_rmdirat"); +} + +int __wasilibc_stat(const char *restrict pathname, struct stat *restrict statbuf, + int flags) { + WEBP2_UNSUPPORTED("__wasilibc_stat"); +} + +int __wasilibc_unlinkat(int fd, const char *path) { + WEBP2_UNSUPPORTED("__wasilibc_unlinkat"); +} + +int __wasilibc_utimens(const char *pathname, const struct timespec times[2], + int flags) { + WEBP2_UNSUPPORTED("__wasilibc_utimens"); +} + +int access(const char *path, int amode) { + WEBP2_UNSUPPORTED("access"); +} + +int chdir(const char *path) { + WEBP2_UNSUPPORTED("chdir"); +} + +int chmod(const char *path, mode_t mode) { + WEBP2_UNSUPPORTED("chmod"); +} + int closedir(DIR *dirp) { WEBP2_UNSUPPORTED("closedir"); } @@ -26,30 +91,116 @@ int dirfd(DIR *dirp) { WEBP2_UNSUPPORTED("dirfd"); } +int faccessat(int fd, const char *path, int amode, int flag) { + WEBP2_UNSUPPORTED("faccessat"); +} + +int fchmod(int fildes, mode_t mode) { + WEBP2_UNSUPPORTED("fchmod"); +} + +int fchmodat(int fd, const char *path, mode_t mode, int flag) { + WEBP2_UNSUPPORTED("fchmodat"); +} + +int fdatasync(int fildes) { + WEBP2_UNSUPPORTED("fdatasync"); +} + +int fdclosedir(DIR *dirp) { + WEBP2_UNSUPPORTED("fdclosedir"); +} + DIR *fdopendir(int fd) { WEBP2_UNSUPPORTED("fdopendir"); } +int fstatat(int fd, const char *restrict path, struct stat *restrict buf, + int flag) { + WEBP2_UNSUPPORTED("fstatat"); +} + +int fstatvfs(int fildes, struct statvfs *buf) { + WEBP2_UNSUPPORTED("fstatvfs"); +} + +int fsync(int fildes) { + WEBP2_UNSUPPORTED("fsync"); +} + +int ftruncate(int fildes, off_t length) { + WEBP2_UNSUPPORTED("ftruncate"); +} + +int futimens(int fd, const struct timespec times[2]) { + WEBP2_UNSUPPORTED("futimens"); +} + char *getcwd(char *buf, size_t size) { WEBP2_UNSUPPORTED("getcwd"); } +int link(const char *path1, const char *path2) { + WEBP2_UNSUPPORTED("link"); +} + +int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag) { + WEBP2_UNSUPPORTED("linkat"); +} + int lstat(const char *restrict path, struct stat *restrict buf) { WEBP2_UNSUPPORTED("lstat"); } +int mkdir(const char *path, mode_t mode) { + WEBP2_UNSUPPORTED("mkdir"); +} + +int mkdirat(int fd, const char *path, mode_t mode) { + WEBP2_UNSUPPORTED("mkdirat"); +} + int open(const char *path, int oflag, ...) { WEBP2_UNSUPPORTED("open"); } +int openat(int fd, const char *path, int oflag, ...) { + WEBP2_UNSUPPORTED("openat"); +} + DIR *opendir(const char *dirname) { WEBP2_UNSUPPORTED("opendir"); } +DIR *opendirat(int dir, const char *dirname) { + WEBP2_UNSUPPORTED("opendirat"); +} + +int posix_fadvise(int fd, off_t offset, off_t len, int advice) { + WEBP2_UNSUPPORTED("posix_fadvise"); +} + +int posix_fallocate(int fd, off_t offset, off_t len) { + WEBP2_UNSUPPORTED("posix_fallocate"); +} + ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset) { WEBP2_UNSUPPORTED("pread"); } +ssize_t preadv(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { + WEBP2_UNSUPPORTED("preadv"); +} + +ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) { + WEBP2_UNSUPPORTED("pwrite"); +} + +ssize_t pwritev(int fildes, const struct iovec *iov, int iovcnt, + off_t offset) { + WEBP2_UNSUPPORTED("pwritev"); +} + struct dirent *readdir(DIR *dirp) { WEBP2_UNSUPPORTED("readdir"); } @@ -59,11 +210,88 @@ ssize_t readlink(const char *restrict path, char *restrict buf, WEBP2_UNSUPPORTED("readlink"); } +ssize_t readlinkat(int fd, const char *restrict path, char *restrict buf, + size_t bufsize) { + WEBP2_UNSUPPORTED("readlinkat"); +} + +int remove(const char *path) { + WEBP2_UNSUPPORTED("remove"); +} + +int rename(const char *old, const char *new) { + WEBP2_UNSUPPORTED("rename"); +} + +int renameat(int oldfd, const char *old, int newfd, const char *new) { + WEBP2_UNSUPPORTED("renameat"); +} + +void rewinddir(DIR *dirp) { + WEBP2_UNSUPPORTED("rewinddir"); +} + +int rmdir(const char *path) { + WEBP2_UNSUPPORTED("rmdir"); +} + +int scandir(const char *dir, struct dirent ***namelist, + int (*sel)(const struct dirent *), + int (*compar)(const struct dirent **, const struct dirent **)) { + WEBP2_UNSUPPORTED("scandir"); +} + +int scandirat(int dirfd, const char *dir, struct dirent ***namelist, + int (*sel)(const struct dirent *), + int (*compar)(const struct dirent **, const struct dirent **)) { + WEBP2_UNSUPPORTED("scandirat"); +} + +void seekdir(DIR *dirp, long loc) { + WEBP2_UNSUPPORTED("seekdir"); +} + int stat(const char *restrict path, struct stat *restrict buf) { WEBP2_UNSUPPORTED("stat"); } +int statvfs(const char *restrict path, struct statvfs *restrict buf) { + WEBP2_UNSUPPORTED("statvfs"); +} + +int symlink(const char *path1, const char *path2) { + WEBP2_UNSUPPORTED("symlink"); +} + +int symlinkat(const char *path1, int fd, const char *path2) { + WEBP2_UNSUPPORTED("symlinkat"); +} + +long telldir(DIR *dirp) { + WEBP2_UNSUPPORTED("telldir"); +} + +int truncate(const char *path, off_t length) { + WEBP2_UNSUPPORTED("truncate"); +} + +int unlink(const char *path) { + WEBP2_UNSUPPORTED("unlink"); +} + +int unlinkat(int fd, const char *path, int flag) { + WEBP2_UNSUPPORTED("unlinkat"); +} + +int utime(const char *path, const struct utimbuf *times) { + WEBP2_UNSUPPORTED("utime"); +} + int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) { WEBP2_UNSUPPORTED("utimensat"); } + +int utimes(const char *path, const struct timeval times[2]) { + WEBP2_UNSUPPORTED("utimes"); +} diff --git a/libc-bottom-half/sources/webp2_socket.c b/libc-bottom-half/sources/webp2_socket.c new file mode 100644 index 000000000..7ddd03f32 --- /dev/null +++ b/libc-bottom-half/sources/webp2_socket.c @@ -0,0 +1,74 @@ +// There are no sockets. + +#include +#include "wasi/report-error.h" + +int accept(int socket, struct sockaddr *restrict address, + socklen_t *restrict address_len) { + WEBP2_UNSUPPORTED("accept"); +} + +int accept4(int socket, struct sockaddr *restrict address, + socklen_t *restrict address_len, int flags) { + WEBP2_UNSUPPORTED("accept4"); +} + +int bind(int socket, const struct sockaddr *address, socklen_t address_len) { + WEBP2_UNSUPPORTED("bind"); +} + +int connect(int socket, const struct sockaddr *address, + socklen_t address_len) { + WEBP2_UNSUPPORTED("connect"); +} + +int getpeername(int socket, struct sockaddr *restrict address, + socklen_t *restrict address_len) { + WEBP2_UNSUPPORTED("getpeername"); +} + +int getsockname(int socket, struct sockaddr *restrict address, + socklen_t *restrict address_len) { + WEBP2_UNSUPPORTED("getsockname"); +} + +int getsockopt(int socket, int level, int option_name, + void *restrict option_value, socklen_t *restrict option_len) { + WEBP2_UNSUPPORTED("getsockopt"); +} + +int listen(int socket, int backlog) { + WEBP2_UNSUPPORTED("listen"); +} + +ssize_t recv(int socket, void *buffer, size_t length, int flags) { + WEBP2_UNSUPPORTED("recv"); +} + +ssize_t recvfrom(int socket, void *restrict buffer, size_t length, int flags, + struct sockaddr *restrict address, + socklen_t *restrict address_len) { + WEBP2_UNSUPPORTED("recvfrom"); +} + +ssize_t send(int socket, const void *buffer, size_t length, int flags) { + WEBP2_UNSUPPORTED("send"); +} + +ssize_t sendto(int socket, const void *message, size_t length, int flags, + const struct sockaddr *dest_addr, socklen_t dest_len) { + WEBP2_UNSUPPORTED("sendto"); +} + +int setsockopt(int socket, int level, int option_name, + const void *option_value, socklen_t option_len) { + WEBP2_UNSUPPORTED("setsockopt"); +} + +int shutdown(int socket, int how) { + WEBP2_UNSUPPORTED("shutdown"); +} + +int socket(int domain, int type, int protocol) { + WEBP2_UNSUPPORTED("socket"); +} From edfdcd55e10f9abbe854459e963b5f913a8f3b80 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 6 Oct 2026 10:29:42 -0500 Subject: [PATCH 21/96] Run update-symbols for webp2 --- expected/wasm32-webp2/defined-symbols.txt | 1378 ++++++++ expected/wasm32-webp2/include-all.c | 178 + expected/wasm32-webp2/predefined-macros.txt | 3327 +++++++++++++++++++ expected/wasm32-webp2/undefined-symbols.txt | 39 + 4 files changed, 4922 insertions(+) create mode 100644 expected/wasm32-webp2/defined-symbols.txt create mode 100644 expected/wasm32-webp2/include-all.c create mode 100644 expected/wasm32-webp2/predefined-macros.txt create mode 100644 expected/wasm32-webp2/undefined-symbols.txt diff --git a/expected/wasm32-webp2/defined-symbols.txt b/expected/wasm32-webp2/defined-symbols.txt new file mode 100644 index 000000000..c17154ed8 --- /dev/null +++ b/expected/wasm32-webp2/defined-symbols.txt @@ -0,0 +1,1378 @@ +_CLOCK_MONOTONIC +_CLOCK_REALTIME +_Exit +_IO_feof_unlocked +_IO_ferror_unlocked +_IO_getc +_IO_getc_unlocked +_IO_putc +_IO_putc_unlocked +__EINVAL +__ENOMEM +__SIG_ERR +__SIG_IGN +__acquire_ptc +__asctime_r +__assert_fail +__c_dot_utf8 +__c_dot_utf8_locale +__c_locale +__clock +__clock_gettime +__clock_nanosleep +__component_type_object_force_link_webp2 +__component_type_object_force_link_webp2_public_use_in_this_compilation_unit +__cos +__cosdf +__cosl +__crypt_blowfish +__crypt_des +__crypt_md5 +__crypt_r +__crypt_sha256 +__crypt_sha512 +__ctype_b_loc +__ctype_get_mb_cur_max +__ctype_tolower_loc +__ctype_toupper_loc +__cxa_atexit +__cxa_finalize +__default_guardsize +__default_stacksize +__des_setkey +__do_cleanup_pop +__do_cleanup_push +__do_des +__do_orphaned_stdio_locks +__dup3 +__duplocale +__env_rm_add +__errno_location +__exp2f_data +__exp_data +__expo2 +__expo2f +__fbufsize +__fclose_ca +__fdopen +__fesetround +__fgetwc_unlocked +__flbf +__floatscan +__fmodeflags +__fopen_rb_ca +__fpending +__fpurge +__fputwc_unlocked +__freadable +__freadahead +__freading +__freadptr +__freadptrinc +__freelocale +__fseeko +__fseeko_unlocked +__fseterr +__fsetlocking +__fsmu8 +__ftello +__ftello_unlocked +__funcs_on_exit +__funcs_on_quick_exit +__futexwait +__futimesat +__fwritable +__fwritex +__fwriting +__get_locale +__getdelim +__getentropy +__getopt_msg +__gmtime_r +__hwcap +__inet_aton +__inhibit_ptc +__init_ssp +__init_tp +__intscan +__invtrigl_R +__isalnum_l +__isalpha_l +__isatty +__isblank_l +__iscntrl_l +__isdigit_l +__isgraph_l +__islower_l +__isoc99_fscanf +__isoc99_fwscanf +__isoc99_scanf +__isoc99_sscanf +__isoc99_swscanf +__isoc99_vfscanf +__isoc99_vfwscanf +__isoc99_vscanf +__isoc99_vsscanf +__isoc99_vswscanf +__isoc99_vwscanf +__isoc99_wscanf +__isprint_l +__ispunct_l +__isspace_l +__isupper_l +__iswalnum_l +__iswalpha_l +__iswblank_l +__iswcntrl_l +__iswctype_l +__iswdigit_l +__iswgraph_l +__iswlower_l +__iswprint_l +__iswpunct_l +__iswspace_l +__iswupper_l +__iswxdigit_l +__isxdigit_l +__lctrans +__lctrans_cur +__lctrans_impl +__ldexp_cexp +__ldexp_cexpf +__lgamma_r +__lgammaf_r +__lgammal_r +__libc +__libc_calloc +__libc_free +__libc_malloc +__loc_is_allocated +__localtime_r +__lock +__lockfile +__log2_data +__log2f_data +__log_data +__logf_data +__lseek +__main_void +__math_divzero +__math_divzerof +__math_invalid +__math_invalidf +__math_invalidl +__math_oflow +__math_oflowf +__math_uflow +__math_uflowf +__math_xflow +__math_xflowf +__memrchr +__mo_lookup +__month_to_secs +__newlocale +__nl_langinfo +__nl_langinfo_l +__ofl_add +__ofl_lock +__ofl_unlock +__optpos +__optreset +__overflow +__p1evll +__pio2_hi +__pio2_lo +__pleval +__polevll +__posix_getopt +__pow_log_data +__powf_log2_data +__private_cond_signal +__progname +__progname_full +__pthread_cond_timedwait +__pthread_create +__pthread_join +__pthread_key_create +__pthread_key_delete +__pthread_mutex_lock +__pthread_mutex_timedlock +__pthread_mutex_trylock +__pthread_mutex_trylock_owner +__pthread_mutex_unlock +__pthread_once +__pthread_once_full +__pthread_rwlock_rdlock +__pthread_rwlock_timedrdlock +__pthread_rwlock_timedwrlock +__pthread_rwlock_tryrdlock +__pthread_rwlock_trywrlock +__pthread_rwlock_unlock +__pthread_rwlock_wrlock +__pthread_setcancelstate +__pthread_testcancel +__pthread_tsd_main +__pthread_tsd_run_dtors +__pthread_tsd_size +__putenv +__qsort_r +__rand48_step +__reallocarray +__register_locked_file +__release_ptc +__rem_pio2 +__rem_pio2_large +__rem_pio2f +__rem_pio2l +__rsqrt_tab +__secs_to_tm +__secs_to_zone +__seed48 +__sem_open_lockptr +__shgetc +__shlim +__signgam +__sin +__sindf +__sinl +__small_printf +__stack_chk_fail +__stack_chk_fail_local +__stack_chk_guard +__stderr_FILE +__stderr_used +__stdin_FILE +__stdin_used +__stdio_close +__stdio_exit +__stdio_exit_needed +__stdio_read +__stdio_seek +__stdio_write +__stdout_FILE +__stdout_used +__stdout_write +__stpcpy +__stpncpy +__strcasecmp_l +__strchrnul +__strcoll_l +__strerror_l +__strftime_fmt_1 +__strftime_l +__strncasecmp_l +__strtod_l +__strtof_l +__strtoimax_internal +__strtol_internal +__strtold_l +__strtoll_internal +__strtoul_internal +__strtoull_internal +__strtoumax_internal +__strxfrm_l +__sysinfo +__sysv_signal +__tan +__tandf +__tanl +__testcancel +__timedwait +__timedwait_cp +__tl_lock +__tl_unlock +__tm_to_secs +__tm_to_tzname +__tolower_l +__toread +__toread_needs_stdio_exit +__toupper_l +__towctrans_l +__towlower_l +__towrite +__towrite_needs_stdio_exit +__towupper_l +__tre_mem_alloc_impl +__tre_mem_destroy +__tre_mem_new_impl +__tsearch_balance +__uflow +__unlist_locked_file +__unlock +__unlockfile +__uselocale +__utc +__wait +__wake +__wasilibc_access +__wasilibc_deinitialize_environ +__wasilibc_dttoif +__wasilibc_ensure_environ +__wasilibc_environ +__wasilibc_futex_wait +__wasilibc_futex_wake +__wasilibc_get_environ +__wasilibc_iftodt +__wasilibc_initialize_environ +__wasilibc_link +__wasilibc_link_newat +__wasilibc_link_oldat +__wasilibc_maybe_reinitialize_environ_eagerly +__wasilibc_open_nomode +__wasilibc_populate_preopens +__wasilibc_pthread_self +__wasilibc_random +__wasilibc_rename_newat +__wasilibc_rename_oldat +__wasilibc_report_error +__wasilibc_report_errorf +__wasilibc_reset_preopens +__wasilibc_rmdirat +__wasilibc_stat +__wasilibc_tell +__wasilibc_unlinkat +__wasilibc_utimens +__wasm_call_dtors +__wcscoll_l +__wcsftime_l +__wcsxfrm_l +__wctrans_l +__wctype_l +__xpg_basename +__xpg_strerror_r +__year_to_secs +_environ +_exit +_flushlbf +_initialize +_pthread_cleanup_pop +_pthread_cleanup_push +a64l +abort +abs +accept +accept4 +access +acos +acosf +acosh +acoshf +acoshl +acosl +aligned_alloc +alphasort +alphasort64 +arc4random +arc4random_buf +arc4random_uniform +asctime +asctime_r +asin +asinf +asinh +asinhf +asinhl +asinl +asprintf +at_quick_exit +atan +atan2 +atan2f +atan2l +atanf +atanh +atanhf +atanhl +atanl +atexit +atof +atoi +atol +atoll +basename +bcmp +bcopy +bind +bsd_signal +bsearch +btowc +bzero +c16rtomb +c32rtomb +cabi_realloc +cabs +cabsf +cabsl +cacos +cacosf +cacosh +cacoshf +cacoshl +cacosl +calloc +carg +cargf +cargl +casin +casinf +casinh +casinhf +casinhl +casinl +catan +catanf +catanh +catanhf +catanhl +catanl +catclose +catgets +catopen +cbrt +cbrtf +cbrtl +ccos +ccosf +ccosh +ccoshf +ccoshl +ccosl +ceil +ceilf +ceill +cexp +cexpf +cexpl +chdir +chmod +cimag +cimagf +cimagl +clearenv +clearerr +clearerr_unlocked +clock +clock_getres +clock_gettime +clock_nanosleep +clog +clogf +clogl +close +closedir +confstr +conj +conjf +conjl +connect +copysign +copysignf +copysignl +cos +cosf +cosh +coshf +coshl +cosl +cpow +cpowf +cpowl +cproj +cprojf +cprojl +creal +crealf +creall +creat +creat64 +crypt +crypt_r +csin +csinf +csinh +csinhf +csinhl +csinl +csqrt +csqrtf +csqrtl +ctan +ctanf +ctanh +ctanhf +ctanhl +ctanl +ctime +ctime_r +difftime +dirfd +dirname +div +dprintf +drand48 +drem +dremf +dup +dup2 +dup3 +duplocale +ecvt +encrypt +environ +erand48 +erf +erfc +erfcf +erfcl +erff +erfl +errno +exit +exp +exp10 +exp10f +exp10l +exp2 +exp2f +exp2l +expf +expl +explicit_bzero +expm1 +expm1f +expm1l +fabs +fabsf +fabsl +faccessat +fchmod +fchmodat +fclose +fcntl +fcvt +fdatasync +fdclosedir +fdim +fdimf +fdiml +fdopen +fdopendir +feclearexcept +fegetenv +fegetexceptflag +fegetround +feholdexcept +feof +feof_unlocked +feraiseexcept +ferror +ferror_unlocked +fesetenv +fesetexceptflag +fesetround +fetestexcept +feupdateenv +fflush +fflush_unlocked +ffs +ffsl +ffsll +fgetc +fgetc_unlocked +fgetln +fgetpos +fgetpos64 +fgets +fgets_unlocked +fgetwc +fgetwc_unlocked +fgetws +fgetws_unlocked +fileno +fileno_unlocked +finite +finitef +flockfile +floor +floorf +floorl +fma +fmaf +fmal +fmax +fmaxf +fmaxl +fmemopen +fmin +fminf +fminl +fmod +fmodf +fmodl +fmtmsg +fnmatch +fopen +fopen64 +fopencookie +fpathconf +fprintf +fpurge +fputc +fputc_unlocked +fputs +fputs_unlocked +fputwc +fputwc_unlocked +fputws +fputws_unlocked +fread +fread_unlocked +free +freelocale +freopen +freopen64 +frexp +frexpf +frexpl +fscanf +fseek +fseeko +fseeko64 +fsetpos +fsetpos64 +fstat +fstatat +fstatvfs +fsync +ftell +ftello +ftello64 +ftime +ftruncate +ftrylockfile +fts_children +fts_close +fts_open +fts_read +fts_set +funlockfile +futimens +futimesat +fwide +fwprintf +fwrite +fwrite_unlocked +fwscanf +gcvt +get_avphys_pages +get_nprocs +get_nprocs_conf +get_phys_pages +getc +getc_unlocked +getchar +getchar_unlocked +getcwd +getdate +getdate_err +getdelim +getdomainname +getentropy +getenv +gethostid +gethostname +getline +getopt +getopt_long +getopt_long_only +getpagesize +getpeername +getpid +getrusage +getsockname +getsockopt +getsubopt +gettimeofday +getw +getwc +getwc_unlocked +getwchar +getwchar_unlocked +glob +glob64 +globfree +globfree64 +gmtime +gmtime_r +hcreate +hcreate_r +hdestroy +hdestroy_r +hsearch +hsearch_r +htonl +htons +hypot +hypotf +hypotl +iconv +iconv_close +iconv_open +ilogb +ilogbf +ilogbl +imaxabs +imaxdiv +in6addr_any +in6addr_loopback +index +inet_addr +inet_aton +inet_lnaof +inet_makeaddr +inet_netof +inet_network +inet_ntoa +inet_ntop +inet_pton +initstate +insque +ioctl +iprintf +isalnum +isalnum_l +isalpha +isalpha_l +isascii +isatty +isblank +isblank_l +iscntrl +iscntrl_l +isdigit +isdigit_l +isgraph +isgraph_l +islower +islower_l +isprint +isprint_l +ispunct +ispunct_l +isspace +isspace_l +isupper +isupper_l +iswalnum +iswalnum_l +iswalpha +iswalpha_l +iswblank +iswblank_l +iswcntrl +iswcntrl_l +iswctype +iswctype_l +iswdigit +iswdigit_l +iswgraph +iswgraph_l +iswlower +iswlower_l +iswprint +iswprint_l +iswpunct +iswpunct_l +iswspace +iswspace_l +iswupper +iswupper_l +iswxdigit +iswxdigit_l +isxdigit +isxdigit_l +j0 +j0f +j1 +j1f +jn +jnf +jrand48 +l64a +labs +lcong48 +ldexp +ldexpf +ldexpl +ldiv +lfind +lgamma +lgamma_r +lgammaf +lgammaf_r +lgammal +lgammal_r +link +linkat +listen +llabs +lldiv +llrint +llrintf +llrintl +llround +llroundf +llroundl +localeconv +localtime +localtime_r +log +log10 +log10f +log10l +log1p +log1pf +log1pl +log2 +log2f +log2l +logb +logbf +logbl +logf +logl +lrand48 +lrint +lrintf +lrintl +lround +lroundf +lroundl +lsearch +lseek +lstat +malloc +malloc_usable_size +mblen +mbrlen +mbrtoc16 +mbrtoc32 +mbrtowc +mbsinit +mbsnrtowcs +mbsrtowcs +mbstowcs +mbtowc +memccpy +memchr +memcmp +memcpy +memmem +memmove +mempcpy +memrchr +memset +mkdir +mkdirat +mktime +mmap +modf +modff +modfl +mprotect +mrand48 +mtx_destroy +mtx_init +mtx_lock +mtx_timedlock +mtx_trylock +munmap +nan +nanf +nanl +nanosleep +nearbyint +nearbyintf +nearbyintl +newlocale +nextafter +nextafterf +nextafterl +nexttoward +nexttowardf +nexttowardl +nftw +nftw64 +nl_langinfo +nl_langinfo_l +nrand48 +ntohl +ntohs +open +open_memstream +open_wmemstream +openat +opendir +opendirat +optarg +opterr +optind +optopt +optreset +pathconf +perror +pipe +pipe2 +poll +posix_close +posix_fadvise +posix_fallocate +posix_memalign +pow +pow10 +pow10f +pow10l +powf +powl +ppoll +pread +preadv +printf +program_invocation_name +program_invocation_short_name +pselect +psignal +pthread_attr_destroy +pthread_attr_getdetachstate +pthread_attr_getguardsize +pthread_attr_getschedparam +pthread_attr_getstack +pthread_attr_getstacksize +pthread_attr_init +pthread_attr_setdetachstate +pthread_attr_setguardsize +pthread_attr_setschedparam +pthread_attr_setstack +pthread_attr_setstacksize +pthread_barrier_destroy +pthread_barrier_init +pthread_barrier_wait +pthread_barrierattr_destroy +pthread_barrierattr_getpshared +pthread_barrierattr_init +pthread_barrierattr_setpshared +pthread_cancel +pthread_cond_broadcast +pthread_cond_destroy +pthread_cond_init +pthread_cond_signal +pthread_cond_timedwait +pthread_cond_wait +pthread_condattr_destroy +pthread_condattr_getclock +pthread_condattr_getpshared +pthread_condattr_init +pthread_condattr_setclock +pthread_condattr_setpshared +pthread_create +pthread_detach +pthread_equal +pthread_getattr_np +pthread_getcpuclockid +pthread_getschedparam +pthread_getspecific +pthread_join +pthread_key_create +pthread_key_delete +pthread_mutex_consistent +pthread_mutex_destroy +pthread_mutex_getprioceiling +pthread_mutex_init +pthread_mutex_lock +pthread_mutex_setprioceiling +pthread_mutex_timedlock +pthread_mutex_trylock +pthread_mutex_unlock +pthread_mutexattr_destroy +pthread_mutexattr_getprotocol +pthread_mutexattr_getpshared +pthread_mutexattr_getrobust +pthread_mutexattr_gettype +pthread_mutexattr_init +pthread_mutexattr_setprotocol +pthread_mutexattr_setpshared +pthread_mutexattr_setrobust +pthread_mutexattr_settype +pthread_once +pthread_rwlock_destroy +pthread_rwlock_init +pthread_rwlock_rdlock +pthread_rwlock_timedrdlock +pthread_rwlock_timedwrlock +pthread_rwlock_tryrdlock +pthread_rwlock_trywrlock +pthread_rwlock_unlock +pthread_rwlock_wrlock +pthread_rwlockattr_destroy +pthread_rwlockattr_getpshared +pthread_rwlockattr_init +pthread_rwlockattr_setpshared +pthread_self +pthread_setcancelstate +pthread_setcanceltype +pthread_setconcurrency +pthread_setschedparam +pthread_setschedprio +pthread_setspecific +pthread_spin_destroy +pthread_spin_init +pthread_spin_lock +pthread_spin_trylock +pthread_spin_unlock +pthread_testcancel +pthread_timedjoin_np +pthread_tryjoin_np +putc +putc_unlocked +putchar +putchar_unlocked +putenv +puts +putw +putwc +putwc_unlocked +putwchar +putwchar_unlocked +pwrite +pwritev +qsort +qsort_r +quick_exit +raise +rand +rand_r +random +read +readdir +readlink +readlinkat +readv +realloc +reallocarray +realpath +recv +recvfrom +regcomp +regerror +regexec +regfree +remainder +remainderf +remainderl +remove +remque +remquo +remquof +remquol +rename +renameat +rewind +rewinddir +rindex +rint +rintf +rintl +rmdir +round +roundf +roundl +sbrk +scalb +scalbf +scalbln +scalblnf +scalblnl +scalbn +scalbnf +scalbnl +scandir +scandirat +scanf +sched_yield +seed48 +seekdir +select +sem_close +sem_destroy +sem_getvalue +sem_init +sem_open +sem_post +sem_timedwait +sem_trywait +sem_wait +send +sendto +setbuf +setbuffer +setenv +setkey +setlinebuf +setlocale +setsockopt +setstate +setvbuf +shutdown +signal +signgam +significand +significandf +sin +sincos +sincosf +sincosl +sinf +sinh +sinhf +sinhl +sinl +sleep +snprintf +socket +sprintf +sqrt +sqrtf +sqrtl +srand +srand48 +srandom +sscanf +stat +statvfs +stderr +stdin +stdout +stpcpy +stpncpy +strcasecmp +strcasecmp_l +strcasestr +strcat +strchr +strchrnul +strcmp +strcoll +strcoll_l +strcpy +strcspn +strdup +strerror +strerror_l +strerror_r +strfmon +strfmon_l +strftime +strftime_l +strlcat +strlcpy +strlen +strncasecmp +strncasecmp_l +strncat +strncmp +strncpy +strndup +strnlen +strpbrk +strptime +strrchr +strsep +strsignal +strspn +strstr +strtod +strtod_l +strtof +strtof_l +strtoimax +strtok +strtok_r +strtol +strtold +strtold_l +strtoll +strtoul +strtoull +strtoumax +strverscmp +strxfrm +strxfrm_l +swab +swprintf +swscanf +symlink +symlinkat +sysconf +tan +tanf +tanh +tanhf +tanhl +tanl +tdelete +tdestroy +telldir +tfind +tgamma +tgammaf +tgammal +thrd_current +thrd_detach +thrd_equal +thrd_join +thrd_sleep +thrd_yield +time +timegm +times +timespec_get +toascii +tolower +tolower_l +toupper +toupper_l +towctrans +towctrans_l +towlower +towlower_l +towupper +towupper_l +trunc +truncate +truncf +truncl +tsearch +tss_get +twalk +uname +ungetc +ungetwc +unlink +unlinkat +unsetenv +uselocale +usleep +utime +utimensat +utimes +vasprintf +vdprintf +versionsort +versionsort64 +vfprintf +vfscanf +vfwprintf +vfwscanf +vprintf +vscanf +vsnprintf +vsprintf +vsscanf +vswprintf +vswscanf +vwprintf +vwscanf +wcpcpy +wcpncpy +wcrtomb +wcscasecmp +wcscasecmp_l +wcscat +wcschr +wcscmp +wcscoll +wcscoll_l +wcscpy +wcscspn +wcsdup +wcsftime +wcsftime_l +wcslen +wcsncasecmp +wcsncasecmp_l +wcsncat +wcsncmp +wcsncpy +wcsnlen +wcsnrtombs +wcspbrk +wcsrchr +wcsrtombs +wcsspn +wcsstr +wcstod +wcstof +wcstoimax +wcstok +wcstol +wcstold +wcstoll +wcstombs +wcstoul +wcstoull +wcstoumax +wcswcs +wcswidth +wcsxfrm +wcsxfrm_l +wctob +wctomb +wctrans +wctrans_l +wctype +wctype_l +wcwidth +webp2_borrow_crypto_impl +webp2_borrow_date +webp2_borrow_error +webp2_borrow_performance_impl +webp2_borrow_uint_8_array +webp2_constructor_error +webp2_constructor_uint_8_array +webp2_crypto_impl_drop_own +webp2_date_drop_own +webp2_error_drop_own +webp2_get_crypto +webp2_get_performance +webp2_list_u8_free +webp2_method_crypto_impl_get_random_values +webp2_method_get_performance_impl_time_origin +webp2_method_performance_impl_now +webp2_method_uint_8_array_subarray +webp2_new_error_options_free +webp2_option_new_error_options_free +webp2_option_string_free +webp2_option_u32_free +webp2_performance_impl_drop_own +webp2_report_error +webp2_static_date_now +webp2_string_dup +webp2_string_dup_n +webp2_string_free +webp2_string_set +webp2_uint_8_array_drop_own +wmemchr +wmemcmp +wmemcpy +wmemmove +wmemset +wprintf +write +writev +wscanf +y0 +y0f +y1 +y1f +yn +ynf \ No newline at end of file diff --git a/expected/wasm32-webp2/include-all.c b/expected/wasm32-webp2/include-all.c new file mode 100644 index 000000000..e5159d66b --- /dev/null +++ b/expected/wasm32-webp2/include-all.c @@ -0,0 +1,178 @@ +#include <__errno_values.h> +#include <__fd_set.h> +#include <__function___isatty.h> +#include <__functions_malloc.h> +#include <__functions_memcpy.h> +#include <__header_dirent.h> +#include <__header_fcntl.h> +#include <__header_inttypes.h> +#include <__header_netinet_in.h> +#include <__header_poll.h> +#include <__header_stdlib.h> +#include <__header_string.h> +#include <__header_sys_ioctl.h> +#include <__header_sys_resource.h> +#include <__header_sys_socket.h> +#include <__header_sys_stat.h> +#include <__header_time.h> +#include <__header_unistd.h> +#include <__macro_FD_SETSIZE.h> +#include <__macro_PAGESIZE.h> +#include <__mode_t.h> +#include <__seek.h> +#include <__struct_dirent.h> +#include <__struct_in6_addr.h> +#include <__struct_in_addr.h> +#include <__struct_iovec.h> +#include <__struct_msghdr.h> +#include <__struct_pollfd.h> +#include <__struct_rusage.h> +#include <__struct_sockaddr.h> +#include <__struct_sockaddr_in.h> +#include <__struct_sockaddr_in6.h> +#include <__struct_sockaddr_storage.h> +#include <__struct_sockaddr_un.h> +#include <__struct_stat.h> +#include <__struct_timespec.h> +#include <__struct_timeval.h> +#include <__struct_tm.h> +#include <__struct_tms.h> +#include <__typedef_DIR.h> +#include <__typedef_blkcnt_t.h> +#include <__typedef_blksize_t.h> +#include <__typedef_clock_t.h> +#include <__typedef_clockid_t.h> +#include <__typedef_dev_t.h> +#include <__typedef_fd_set.h> +#include <__typedef_gid_t.h> +#include <__typedef_in_addr_t.h> +#include <__typedef_in_port_t.h> +#include <__typedef_ino_t.h> +#include <__typedef_mode_t.h> +#include <__typedef_nfds_t.h> +#include <__typedef_nlink_t.h> +#include <__typedef_off_t.h> +#include <__typedef_sa_family_t.h> +#include <__typedef_sigset_t.h> +#include <__typedef_socklen_t.h> +#include <__typedef_ssize_t.h> +#include <__typedef_suseconds_t.h> +#include <__typedef_time_t.h> +#include <__typedef_uid_t.h> +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include diff --git a/expected/wasm32-webp2/predefined-macros.txt b/expected/wasm32-webp2/predefined-macros.txt new file mode 100644 index 000000000..1cf8ab862 --- /dev/null +++ b/expected/wasm32-webp2/predefined-macros.txt @@ -0,0 +1,3327 @@ +#define ABDAY_1 0x20000 +#define ABDAY_2 0x20001 +#define ABDAY_3 0x20002 +#define ABDAY_4 0x20003 +#define ABDAY_5 0x20004 +#define ABDAY_6 0x20005 +#define ABDAY_7 0x20006 +#define ABMON_1 0x2000E +#define ABMON_10 0x20017 +#define ABMON_11 0x20018 +#define ABMON_12 0x20019 +#define ABMON_2 0x2000F +#define ABMON_3 0x20010 +#define ABMON_4 0x20011 +#define ABMON_5 0x20012 +#define ABMON_6 0x20013 +#define ABMON_7 0x20014 +#define ABMON_8 0x20015 +#define ABMON_9 0x20016 +#define ABORT 238 +#define ACK 04 +#define ADD ns_uop_add +#define ADJ_ESTERROR 0x0008 +#define ADJ_FREQUENCY 0x0002 +#define ADJ_MAXERROR 0x0004 +#define ADJ_MICRO 0x1000 +#define ADJ_NANO 0x2000 +#define ADJ_OFFSET 0x0001 +#define ADJ_OFFSET_SINGLESHOT 0x8001 +#define ADJ_OFFSET_SS_READ 0xa001 +#define ADJ_SETOFFSET 0x0100 +#define ADJ_STATUS 0x0010 +#define ADJ_TAI 0x0080 +#define ADJ_TICK 0x4000 +#define ADJ_TIMECONST 0x0020 +#define AF_INET PF_INET +#define AF_INET6 PF_INET6 +#define AF_UNIX 3 +#define AF_UNSPEC PF_UNSPEC +#define ALT_DIGITS 0x2002F +#define AM_STR 0x20026 +#define ANYMARK 0x01 +#define AO 245 +#define AREGTYPE '\0' +#define ARFMAG "`\n" +#define ARG_MAX 131072 +#define ARMAG "!\n" +#define AT_EACCESS (0x0) +#define AT_FDCWD (-2) +#define AT_REMOVEDIR (0x4) +#define AT_SYMLINK_FOLLOW (0x2) +#define AT_SYMLINK_NOFOLLOW (0x1) +#define AUTHTYPE_CNT 5 +#define AUTHTYPE_KERBEROS_V4 1 +#define AUTHTYPE_KERBEROS_V5 2 +#define AUTHTYPE_MINK 4 +#define AUTHTYPE_NAME(x) authtype_names[x] +#define AUTHTYPE_NAME_OK(x) ((unsigned int)(x) < AUTHTYPE_CNT) +#define AUTHTYPE_NULL 0 +#define AUTHTYPE_SPX 3 +#define AUTHTYPE_TEST 99 +#define AUTH_HOW_MASK 2 +#define AUTH_HOW_MUTUAL 2 +#define AUTH_HOW_ONE_WAY 0 +#define AUTH_WHO_CLIENT 0 +#define AUTH_WHO_MASK 1 +#define AUTH_WHO_SERVER 1 +#define AYT 246 +#define BIG_ENDIAN __BIG_ENDIAN +#define BITSPERBYTE CHAR_BIT +#define BLKTYPE '4' +#define BLK_BYTECOUNT 2 +#define BLK_EOF 0x40 +#define BLK_EOR 0x80 +#define BLK_ERRORS 0x20 +#define BLK_RESTART 0x10 +#define BREAK 243 +#define BUFSIZ 1024 +#define BYTE_ORDER __BYTE_ORDER +#define CANBSIZ 255 +#define CBRK CEOL +#define CDISCARD CTRL('o') +#define CDSUSP CTRL('y') +#define CEOF CTRL('d') +#define CEOL '\0' +#define CEOT CEOF +#define CERASE 0177 +#define CFLUSH CDISCARD +#define CHARBITS (sizeof(char) * 8) +#define CHARCLASS_NAME_MAX 14 +#define CHAR_BIT 8 +#define CHAR_MAX 127 +#define CHAR_MIN (-128) +#define CHRTYPE '3' +#define CINTR CTRL('c') +#define CKILL CTRL('u') +#define CLNEXT CTRL('v') +#define CLOCKS_PER_SEC ((clock_t)1000000000) +#define CLOCK_MONOTONIC (&_CLOCK_MONOTONIC) +#define CLOCK_REALTIME (&_CLOCK_REALTIME) +#define CMIN 1 +#define CMPLX(x,y) __CMPLX(x, y, double) +#define CMPLXF(x,y) __CMPLX(x, y, float) +#define CMPLXL(x,y) __CMPLX(x, y, long double) +#define CODESET 14 +#define COLL_WEIGHTS_MAX 2 +#define COMPLETE 2 +#define CONTINUE 3 +#define CONTTYPE '7' +#define CQUIT 034 +#define CREPRINT CTRL('r') +#define CRNCYSTR 0x4000F +#define CRPRNT CREPRINT +#define CSTART CTRL('q') +#define CSTATUS '\0' +#define CSTOP CTRL('s') +#define CSUSP CTRL('z') +#define CTIME 0 +#define CTRL(x) ((x)&037) +#define CWERASE CTRL('w') +#define C_ANY ns_c_any +#define C_CHAOS ns_c_chaos +#define C_HS ns_c_hs +#define C_IN ns_c_in +#define C_IRGRP 000040 +#define C_IROTH 000004 +#define C_IRUSR 000400 +#define C_ISBLK 060000 +#define C_ISCHR 020000 +#define C_ISCTG 0110000 +#define C_ISDIR 040000 +#define C_ISFIFO 010000 +#define C_ISGID 002000 +#define C_ISLNK 0120000 +#define C_ISREG 0100000 +#define C_ISSOCK 0140000 +#define C_ISUID 004000 +#define C_ISVTX 001000 +#define C_IWGRP 000020 +#define C_IWOTH 000002 +#define C_IWUSR 000200 +#define C_IXGRP 000010 +#define C_IXOTH 000001 +#define C_IXUSR 000100 +#define C_NONE ns_c_none +#define DATA 03 +#define DAY_1 0x20007 +#define DAY_2 0x20008 +#define DAY_3 0x20009 +#define DAY_4 0x2000A +#define DAY_5 0x2000B +#define DAY_6 0x2000C +#define DAY_7 0x2000D +#define DBL_DECIMAL_DIG 17 +#define DBL_DIG 15 +#define DBL_EPSILON 2.22044604925031308085e-16 +#define DBL_HAS_SUBNORM 1 +#define DBL_MANT_DIG 53 +#define DBL_MAX 1.79769313486231570815e+308 +#define DBL_MAX_10_EXP 308 +#define DBL_MAX_EXP 1024 +#define DBL_MIN 2.22507385850720138309e-308 +#define DBL_MIN_10_EXP (-307) +#define DBL_MIN_EXP (-1021) +#define DBL_TRUE_MIN 4.94065645841246544177e-324 +#define DECIMAL_DIG 36 +#define DELAYTIMER_MAX 0x7fffffff +#define DELETE ns_uop_delete +#define DEV_BSIZE 512 +#define DIRTYPE '5' +#define DM 242 +#define DMAXEXP DBL_MAX_EXP +#define DMINEXP DBL_MIN_EXP +#define DO 253 +#define DONT 254 +#define DOUBLEBITS (sizeof(double) * 8) +#define DTTOIF(x) (__wasilibc_dttoif(x)) +#define DT_BLK 1 +#define DT_CHR 2 +#define DT_DIR 3 +#define DT_FIFO 6 +#define DT_LNK 7 +#define DT_REG 4 +#define DT_SOCK 20 +#define DT_UNKNOWN 0 +#define D_FMT 0x20029 +#define D_T_FMT 0x20028 +#define E2BIG 1 +#define EACCES 2 +#define EACCESS 2 +#define EADDRINUSE 3 +#define EADDRNOTAVAIL 4 +#define EAFNOSUPPORT 5 +#define EAGAIN 6 +#define EALREADY 7 +#define EBADF 8 +#define EBADID 5 +#define EBADMSG 9 +#define EBADOP 4 +#define EBUSY 10 +#define EC 247 +#define ECANCELED 11 +#define ECHILD 12 +#define ECONNABORTED 13 +#define ECONNREFUSED 14 +#define ECONNRESET 15 +#define EDEADLK 16 +#define EDESTADDRREQ 17 +#define EDOM 18 +#define EDQUOT 19 +#define EEXIST 20 +#define EEXISTS 6 +#define EFAULT 21 +#define EFBIG 22 +#define EFD_CLOEXEC O_CLOEXEC +#define EFD_NONBLOCK O_NONBLOCK +#define EFD_SEMAPHORE 1 +#define EHOSTUNREACH 23 +#define EIDRM 24 +#define EILSEQ 25 +#define EINPROGRESS 26 +#define EINTR 27 +#define EINVAL 28 +#define EIO 29 +#define EISCONN 30 +#define EISDIR 31 +#define EL 248 +#define ELOOP 32 +#define EMFILE 33 +#define EMLINK 34 +#define EMSGSIZE 35 +#define EMULTIHOP 36 +#define ENAMETOOLONG 37 +#define ENCRYPT_CNT 9 +#define ENCRYPT_DEC_KEYID 8 +#define ENCRYPT_ENC_KEYID 7 +#define ENCRYPT_END 4 +#define ENCRYPT_IS 0 +#define ENCRYPT_NAME(x) encrypt_names[x] +#define ENCRYPT_NAME_OK(x) ((unsigned int)(x) < ENCRYPT_CNT) +#define ENCRYPT_REPLY 2 +#define ENCRYPT_REQEND 6 +#define ENCRYPT_REQSTART 5 +#define ENCRYPT_START 3 +#define ENCRYPT_SUPPORT 1 +#define ENCTYPE_ANY 0 +#define ENCTYPE_CNT 3 +#define ENCTYPE_DES_CFB64 1 +#define ENCTYPE_DES_OFB64 2 +#define ENCTYPE_NAME(x) enctype_names[x] +#define ENCTYPE_NAME_OK(x) ((unsigned int)(x) < ENCTYPE_CNT) +#define ENETDOWN 38 +#define ENETRESET 39 +#define ENETUNREACH 40 +#define ENFILE 41 +#define ENOBUFS 42 +#define ENODEV 43 +#define ENOENT 44 +#define ENOEXEC 45 +#define ENOLCK 46 +#define ENOLINK 47 +#define ENOMEM 48 +#define ENOMSG 49 +#define ENOPROTOOPT 50 +#define ENOSPACE 3 +#define ENOSPC 51 +#define ENOSYS 52 +#define ENOTCAPABLE 76 +#define ENOTCONN 53 +#define ENOTDIR 54 +#define ENOTEMPTY 55 +#define ENOTFOUND 1 +#define ENOTRECOVERABLE 56 +#define ENOTSOCK 57 +#define ENOTSUP 58 +#define ENOTTY 59 +#define ENOUSER 7 +#define ENV_ESC 2 +#define ENV_USERVAR 3 +#define ENXIO 60 +#define EOF (-1) +#define EOPNOTSUPP ENOTSUP +#define EOR 239 +#define EOVERFLOW 61 +#define EOWNERDEAD 62 +#define EPERM 63 +#define EPIPE 64 +#define EPROTO 65 +#define EPROTONOSUPPORT 66 +#define EPROTOTYPE 67 +#define ERA 0x2002C +#define ERANGE 68 +#define ERA_D_FMT 0x2002E +#define ERA_D_T_FMT 0x20030 +#define ERA_T_FMT 0x20031 +#define EROFS 69 +#define ERROR 05 +#define ESPIPE 70 +#define ESRCH 71 +#define ESTALE 72 +#define ETIMEDOUT 73 +#define ETXTBSY 74 +#define EUNDEF 0 +#define EWOULDBLOCK EAGAIN +#define EXDEV 75 +#define EXIT_FAILURE 1 +#define EXIT_SUCCESS 0 +#define EX_CANTCREAT 73 +#define EX_CONFIG 78 +#define EX_DATAERR 65 +#define EX_IOERR 74 +#define EX_NOHOST 68 +#define EX_NOINPUT 66 +#define EX_NOPERM 77 +#define EX_NOUSER 67 +#define EX_OK 0 +#define EX_OSERR 71 +#define EX_OSFILE 72 +#define EX_PROTOCOL 76 +#define EX_SOFTWARE 70 +#define EX_TEMPFAIL 75 +#define EX_UNAVAILABLE 69 +#define EX_USAGE 64 +#define EX__BASE 64 +#define EX__MAX 78 +#define FD_CLOEXEC (1) +#define FD_CLR(fd,set) (FD_CLR((fd), (set))) +#define FD_COPY(from,to) (FD_COPY((from), (to))) +#define FD_ISSET(fd,set) (FD_ISSET((fd), (set))) +#define FD_SET(fd,set) (FD_SET((fd), (set))) +#define FD_SETSIZE 1024 +#define FD_ZERO(set) (FD_ZERO((set))) +#define FE_ALL_EXCEPT 0 +#define FE_DFL_ENV ((const fenv_t *) -1) +#define FE_TONEAREST 0 +#define FIFOTYPE '6' +#define FILENAME_MAX 4096 +#define FILESIZEBITS 64 +#define FIONBIO 2 +#define FIONREAD 1 +#define FLOATBITS (sizeof(float) * 8) +#define FLT_DECIMAL_DIG 9 +#define FLT_DIG 6 +#define FLT_EPSILON 1.1920928955078125e-07F +#define FLT_EVAL_METHOD 0 +#define FLT_HAS_SUBNORM 1 +#define FLT_MANT_DIG 24 +#define FLT_MAX 3.40282346638528859812e+38F +#define FLT_MAX_10_EXP 38 +#define FLT_MAX_EXP 128 +#define FLT_MIN 1.17549435082228750797e-38F +#define FLT_MIN_10_EXP (-37) +#define FLT_MIN_EXP (-125) +#define FLT_RADIX 2 +#define FLT_ROUNDS (__builtin_flt_rounds()) +#define FLT_TRUE_MIN 1.40129846432481707092e-45F +#define FLUSHBAND 0x04 +#define FLUSHR 0x01 +#define FLUSHRW 0x03 +#define FLUSHW 0x02 +#define FMAXEXP FLT_MAX_EXP +#define FMINEXP FLT_MIN_EXP +#define FMNAMESZ 8 +#define FNM_CASEFOLD 0x10 +#define FNM_FILE_NAME FNM_PATHNAME +#define FNM_LEADING_DIR 0x8 +#define FNM_NOESCAPE 0x2 +#define FNM_NOMATCH 1 +#define FNM_NOSYS (-1) +#define FNM_PATHNAME 0x1 +#define FNM_PERIOD 0x4 +#define FOPEN_MAX 1000 +#define FORMERR ns_r_formerr +#define FORM_C 3 +#define FORM_N 1 +#define FORM_T 2 +#define FP_ILOGB0 FP_ILOGBNAN +#define FP_ILOGBNAN (-1-0x7fffffff) +#define FP_INFINITE 1 +#define FP_NAN 0 +#define FP_NORMAL 4 +#define FP_SUBNORMAL 3 +#define FP_ZERO 2 +#define FSETLOCKING_BYCALLER 2 +#define FSETLOCKING_INTERNAL 1 +#define FSETLOCKING_QUERY 0 +#define FTS_AGAIN 1 +#define FTS_COMFOLLOW 0x001 +#define FTS_D 1 +#define FTS_DC 2 +#define FTS_DEFAULT 3 +#define FTS_DNR 4 +#define FTS_DONTCHDIR 0x01 +#define FTS_DOT 5 +#define FTS_DP 6 +#define FTS_ERR 7 +#define FTS_F 8 +#define FTS_FOLLOW 2 +#define FTS_INIT 9 +#define FTS_ISW 0x04 +#define FTS_LOGICAL 0x002 +#define FTS_NAMEONLY 0x100 +#define FTS_NOCHDIR 0x004 +#define FTS_NOINSTR 3 +#define FTS_NOSTAT 0x008 +#define FTS_NS 10 +#define FTS_NSOK 11 +#define FTS_OPTIONMASK 0x0ff +#define FTS_PHYSICAL 0x010 +#define FTS_ROOTLEVEL 0 +#define FTS_ROOTPARENTLEVEL -1 +#define FTS_SEEDOT 0x020 +#define FTS_SKIP 4 +#define FTS_SL 12 +#define FTS_SLNONE 13 +#define FTS_STOP 0x200 +#define FTS_SYMFOLLOW 0x02 +#define FTS_W 14 +#define FTS_WHITEOUT 0x080 +#define FTS_XDEV 0x040 +#define FTW_CHDIR 4 +#define FTW_D 2 +#define FTW_DEPTH 8 +#define FTW_DNR 3 +#define FTW_DP 6 +#define FTW_F 1 +#define FTW_MOUNT 2 +#define FTW_NS 4 +#define FTW_PHYS 1 +#define FTW_SL 5 +#define FTW_SLN 7 +#define F_DUPFD (5) +#define F_DUPFD_CLOEXEC (6) +#define F_GETFD (1) +#define F_GETFL (3) +#define F_GETLK (7) +#define F_LOCK 1 +#define F_OK (0) +#define F_RDLCK (0) +#define F_SETFD (2) +#define F_SETFL (4) +#define F_SETLK (8) +#define F_SETLKW (9) +#define F_TEST 3 +#define F_TLOCK 2 +#define F_ULOCK 0 +#define F_UNLCK (2) +#define F_WRLCK (1) +#define GA 249 +#define GETLONG NS_GET32 +#define GETSHORT NS_GET16 +#define GLOB_ABORTED 2 +#define GLOB_APPEND 0x20 +#define GLOB_DOOFFS 0x08 +#define GLOB_ERR 0x01 +#define GLOB_MARK 0x02 +#define GLOB_NOCHECK 0x10 +#define GLOB_NOESCAPE 0x40 +#define GLOB_NOMATCH 3 +#define GLOB_NOSORT 0x04 +#define GLOB_NOSPACE 1 +#define GLOB_NOSYS 4 +#define GLOB_PERIOD 0x80 +#define GROUP_FILTER_SIZE(numsrc) (sizeof(struct group_filter) - sizeof(struct sockaddr_storage) + (numsrc) * sizeof(struct sockaddr_storage)) +#define HFIXEDSZ NS_HFIXEDSZ +#define HIBITL MINLONG +#define HIBITS MINSHORT +#define HOST_NAME_MAX 255 +#define HUGE 3.40282346638528859812e+38F +#define HUGE_VAL ((double)INFINITY) +#define HUGE_VALF INFINITY +#define HUGE_VALL ((long double)INFINITY) +#define I _Complex_I +#define IAC 255 +#define ICMP6_DST_UNREACH 1 +#define ICMP6_DST_UNREACH_ADDR 3 +#define ICMP6_DST_UNREACH_ADMIN 1 +#define ICMP6_DST_UNREACH_BEYONDSCOPE 2 +#define ICMP6_DST_UNREACH_NOPORT 4 +#define ICMP6_DST_UNREACH_NOROUTE 0 +#define ICMP6_ECHO_REPLY 129 +#define ICMP6_ECHO_REQUEST 128 +#define ICMP6_FILTER 1 +#define ICMP6_FILTER_BLOCK 1 +#define ICMP6_FILTER_BLOCKOTHERS 3 +#define ICMP6_FILTER_PASS 2 +#define ICMP6_FILTER_PASSONLY 4 +#define ICMP6_FILTER_SETBLOCK(type,filterp) ((((filterp)->icmp6_filt[(type) >> 5]) |= (1 << ((type) & 31)))) +#define ICMP6_FILTER_SETBLOCKALL(filterp) memset (filterp, 0xFF, sizeof (struct icmp6_filter)); +#define ICMP6_FILTER_SETPASS(type,filterp) ((((filterp)->icmp6_filt[(type) >> 5]) &= ~(1 << ((type) & 31)))) +#define ICMP6_FILTER_SETPASSALL(filterp) memset (filterp, 0, sizeof (struct icmp6_filter)); +#define ICMP6_FILTER_WILLBLOCK(type,filterp) ((((filterp)->icmp6_filt[(type) >> 5]) & (1 << ((type) & 31))) != 0) +#define ICMP6_FILTER_WILLPASS(type,filterp) ((((filterp)->icmp6_filt[(type) >> 5]) & (1 << ((type) & 31))) == 0) +#define ICMP6_INFOMSG_MASK 0x80 +#define ICMP6_PACKET_TOO_BIG 2 +#define ICMP6_PARAMPROB_HEADER 0 +#define ICMP6_PARAMPROB_NEXTHEADER 1 +#define ICMP6_PARAMPROB_OPTION 2 +#define ICMP6_PARAM_PROB 4 +#define ICMP6_ROUTER_RENUMBERING 138 +#define ICMP6_RR_FLAGS_FORCEAPPLY 0x20 +#define ICMP6_RR_FLAGS_PREVDONE 0x08 +#define ICMP6_RR_FLAGS_REQRESULT 0x40 +#define ICMP6_RR_FLAGS_SPECSITE 0x10 +#define ICMP6_RR_FLAGS_TEST 0x80 +#define ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME 0x40 +#define ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME 0x80 +#define ICMP6_RR_PCOUSE_RAFLAGS_AUTO 0x10 +#define ICMP6_RR_PCOUSE_RAFLAGS_ONLINK 0x20 +#define ICMP6_RR_RESULT_FLAGS_FORBIDDEN 0x0100 +#define ICMP6_RR_RESULT_FLAGS_OOB 0x0200 +#define ICMP6_TIME_EXCEEDED 3 +#define ICMP6_TIME_EXCEED_REASSEMBLY 1 +#define ICMP6_TIME_EXCEED_TRANSIT 0 +#define ICMP_ADDRESS 17 +#define ICMP_ADDRESSREPLY 18 +#define ICMP_ADVLEN(p) (8 + ((p)->icmp_ip.ip_hl << 2) + 8) +#define ICMP_ADVLENMIN (8 + sizeof (struct ip) + 8) +#define ICMP_DEST_UNREACH 3 +#define ICMP_ECHO 8 +#define ICMP_ECHOREPLY 0 +#define ICMP_EXC_FRAGTIME 1 +#define ICMP_EXC_TTL 0 +#define ICMP_FRAG_NEEDED 4 +#define ICMP_HOST_ANO 10 +#define ICMP_HOST_ISOLATED 8 +#define ICMP_HOST_UNKNOWN 7 +#define ICMP_HOST_UNREACH 1 +#define ICMP_HOST_UNR_TOS 12 +#define ICMP_INFOTYPE(type) ((type) == ICMP_ECHOREPLY || (type) == ICMP_ECHO || (type) == ICMP_ROUTERADVERT || (type) == ICMP_ROUTERSOLICIT || (type) == ICMP_TSTAMP || (type) == ICMP_TSTAMPREPLY || (type) == ICMP_IREQ || (type) == ICMP_IREQREPLY || (type) == ICMP_MASKREQ || (type) == ICMP_MASKREPLY) +#define ICMP_INFO_REPLY 16 +#define ICMP_INFO_REQUEST 15 +#define ICMP_IREQ 15 +#define ICMP_IREQREPLY 16 +#define ICMP_MASKLEN 12 +#define ICMP_MASKREPLY 18 +#define ICMP_MASKREQ 17 +#define ICMP_MAXTYPE 18 +#define ICMP_MINLEN 8 +#define ICMP_NET_ANO 9 +#define ICMP_NET_UNKNOWN 6 +#define ICMP_NET_UNREACH 0 +#define ICMP_NET_UNR_TOS 11 +#define ICMP_PARAMETERPROB 12 +#define ICMP_PARAMPROB 12 +#define ICMP_PARAMPROB_OPTABSENT 1 +#define ICMP_PKT_FILTERED 13 +#define ICMP_PORT_UNREACH 3 +#define ICMP_PREC_CUTOFF 15 +#define ICMP_PREC_VIOLATION 14 +#define ICMP_PROT_UNREACH 2 +#define ICMP_REDIRECT 5 +#define ICMP_REDIRECT_HOST 1 +#define ICMP_REDIRECT_NET 0 +#define ICMP_REDIRECT_TOSHOST 3 +#define ICMP_REDIRECT_TOSNET 2 +#define ICMP_REDIR_HOST 1 +#define ICMP_REDIR_HOSTTOS 3 +#define ICMP_REDIR_NET 0 +#define ICMP_REDIR_NETTOS 2 +#define ICMP_ROUTERADVERT 9 +#define ICMP_ROUTERSOLICIT 10 +#define ICMP_SOURCEQUENCH 4 +#define ICMP_SOURCE_QUENCH 4 +#define ICMP_SR_FAILED 5 +#define ICMP_TIMESTAMP 13 +#define ICMP_TIMESTAMPREPLY 14 +#define ICMP_TIME_EXCEEDED 11 +#define ICMP_TIMXCEED 11 +#define ICMP_TIMXCEED_INTRANS 0 +#define ICMP_TIMXCEED_REASS 1 +#define ICMP_TSLEN (8 + 3 * sizeof (n_time)) +#define ICMP_TSTAMP 13 +#define ICMP_TSTAMPREPLY 14 +#define ICMP_UNREACH 3 +#define ICMP_UNREACH_FILTER_PROHIB 13 +#define ICMP_UNREACH_HOST 1 +#define ICMP_UNREACH_HOST_PRECEDENCE 14 +#define ICMP_UNREACH_HOST_PROHIB 10 +#define ICMP_UNREACH_HOST_UNKNOWN 7 +#define ICMP_UNREACH_ISOLATED 8 +#define ICMP_UNREACH_NEEDFRAG 4 +#define ICMP_UNREACH_NET 0 +#define ICMP_UNREACH_NET_PROHIB 9 +#define ICMP_UNREACH_NET_UNKNOWN 6 +#define ICMP_UNREACH_PORT 3 +#define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 +#define ICMP_UNREACH_PROTOCOL 2 +#define ICMP_UNREACH_SRCFAIL 5 +#define ICMP_UNREACH_TOSHOST 12 +#define ICMP_UNREACH_TOSNET 11 +#define IFTODT(x) (__wasilibc_iftodt(x)) +#define IGMP_AWAKENING_MEMBER 5 +#define IGMP_DELAYING_MEMBER 1 +#define IGMP_DVMRP 0x13 +#define IGMP_HOST_LEAVE_MESSAGE IGMP_V2_LEAVE_GROUP +#define IGMP_HOST_MEMBERSHIP_QUERY IGMP_MEMBERSHIP_QUERY +#define IGMP_HOST_MEMBERSHIP_REPORT IGMP_V1_MEMBERSHIP_REPORT +#define IGMP_HOST_NEW_MEMBERSHIP_REPORT IGMP_V2_MEMBERSHIP_REPORT +#define IGMP_IDLE_MEMBER 2 +#define IGMP_LAZY_MEMBER 3 +#define IGMP_MAX_HOST_REPORT_DELAY 10 +#define IGMP_MEMBERSHIP_QUERY 0x11 +#define IGMP_MINLEN 8 +#define IGMP_MTRACE 0x1f +#define IGMP_MTRACE_RESP 0x1e +#define IGMP_PIM 0x14 +#define IGMP_SLEEPING_MEMBER 4 +#define IGMP_TIMER_SCALE 10 +#define IGMP_TRACE 0x15 +#define IGMP_V1_MEMBERSHIP_REPORT 0x12 +#define IGMP_V2_LEAVE_GROUP 0x17 +#define IGMP_V2_MEMBERSHIP_REPORT 0x16 +#define IGMP_v1_ROUTER 1 +#define IGMP_v2_ROUTER 2 +#define IN6ADDRSZ NS_IN6ADDRSZ +#define IN6ADDR_ANY_INIT { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } +#define IN6ADDR_LOOPBACK_INIT { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } } +#define IN6_ARE_ADDR_EQUAL(a,b) __ARE_4_EQUAL((const uint32_t *)(a), (const uint32_t *)(b)) +#define IN6_IS_ADDR_LINKLOCAL(a) ((((uint8_t *) (a))[0]) == 0xfe && (((uint8_t *) (a))[1] & 0xc0) == 0x80) +#define IN6_IS_ADDR_LOOPBACK(a) (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && ((uint32_t *) (a))[2] == 0 && ((uint8_t *) (a))[12] == 0 && ((uint8_t *) (a))[13] == 0 && ((uint8_t *) (a))[14] == 0 && ((uint8_t *) (a))[15] == 1 ) +#define IN6_IS_ADDR_MC_GLOBAL(a) (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0xe)) +#define IN6_IS_ADDR_MC_LINKLOCAL(a) (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x2)) +#define IN6_IS_ADDR_MC_NODELOCAL(a) (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x1)) +#define IN6_IS_ADDR_MC_ORGLOCAL(a) (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x8)) +#define IN6_IS_ADDR_MC_SITELOCAL(a) (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x5)) +#define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff) +#define IN6_IS_ADDR_SITELOCAL(a) ((((uint8_t *) (a))[0]) == 0xfe && (((uint8_t *) (a))[1] & 0xc0) == 0xc0) +#define IN6_IS_ADDR_UNSPECIFIED(a) (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0) +#define IN6_IS_ADDR_V4COMPAT(a) (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && ((uint32_t *) (a))[2] == 0 && !IN6_IS_ADDR_UNSPECIFIED(a) && !IN6_IS_ADDR_LOOPBACK(a)) +#define IN6_IS_ADDR_V4MAPPED(a) (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && ((uint8_t *) (a))[8] == 0 && ((uint8_t *) (a))[9] == 0 && ((uint8_t *) (a))[10] == 0xff && ((uint8_t *) (a))[11] == 0xff) +#define INADDRSZ NS_INADDRSZ +#define INADDR_ALLHOSTS_GROUP ((in_addr_t) 0xe0000001) +#define INADDR_ALLRTRS_GROUP ((in_addr_t) 0xe0000002) +#define INADDR_ALLSNOOPERS_GROUP ((in_addr_t) 0xe000006a) +#define INADDR_ANY ((in_addr_t) 0x00000000) +#define INADDR_BROADCAST ((in_addr_t) 0xffffffff) +#define INADDR_DUMMY ((in_addr_t) 0xc0000008) +#define INADDR_LOOPBACK ((in_addr_t) 0x7f000001) +#define INADDR_MAX_LOCAL_GROUP ((in_addr_t) 0xe00000ff) +#define INADDR_NONE ((in_addr_t) 0xffffffff) +#define INADDR_UNSPEC_GROUP ((in_addr_t) 0xe0000000) +#define INDIR_MASK NS_CMPRSFLGS +#define INET6_ADDRSTRLEN 46 +#define INET_ADDRSTRLEN 16 +#define INFINITY 1e5000f +#define INT16SZ NS_INT16SZ +#define INT16_C(c) c +#define INT16_MAX (0x7fff) +#define INT16_MIN (-1-0x7fff) +#define INT32SZ NS_INT32SZ +#define INT32_C(c) c +#define INT32_MAX (0x7fffffff) +#define INT32_MIN (-1-0x7fffffff) +#define INT64_C(c) c ## LL +#define INT64_MAX (0x7fffffffffffffff) +#define INT64_MIN (-1-0x7fffffffffffffff) +#define INT8SZ NS_INT8SZ +#define INT8_C(c) c +#define INT8_MAX (0x7f) +#define INT8_MIN (-1-0x7f) +#define INTBITS (sizeof(int) * 8) +#define INTMAX_C(c) c ## LL +#define INTMAX_MAX INT64_MAX +#define INTMAX_MIN INT64_MIN +#define INTPTR_MAX INT32_MAX +#define INTPTR_MIN INT32_MIN +#define INT_FAST16_MAX INT32_MAX +#define INT_FAST16_MIN INT32_MIN +#define INT_FAST32_MAX INT32_MAX +#define INT_FAST32_MIN INT32_MIN +#define INT_FAST64_MAX INT64_MAX +#define INT_FAST64_MIN INT64_MIN +#define INT_FAST8_MAX INT8_MAX +#define INT_FAST8_MIN INT8_MIN +#define INT_LEAST16_MAX INT16_MAX +#define INT_LEAST16_MIN INT16_MIN +#define INT_LEAST32_MAX INT32_MAX +#define INT_LEAST32_MIN INT32_MIN +#define INT_LEAST64_MAX INT64_MAX +#define INT_LEAST64_MIN INT64_MIN +#define INT_LEAST8_MAX INT8_MAX +#define INT_LEAST8_MIN INT8_MIN +#define INT_MAX 0x7fffffff +#define INT_MIN (-1-0x7fffffff) +#define IN_BADCLASS(a) ((((in_addr_t)(a)) & 0xf0000000) == 0xf0000000) +#define IN_CLASSA(a) ((((in_addr_t)(a)) & 0x80000000) == 0) +#define IN_CLASSA_HOST (0xffffffff & ~IN_CLASSA_NET) +#define IN_CLASSA_MAX 128 +#define IN_CLASSA_NET 0xff000000 +#define IN_CLASSA_NSHIFT 24 +#define IN_CLASSB(a) ((((in_addr_t)(a)) & 0xc0000000) == 0x80000000) +#define IN_CLASSB_HOST (0xffffffff & ~IN_CLASSB_NET) +#define IN_CLASSB_MAX 65536 +#define IN_CLASSB_NET 0xffff0000 +#define IN_CLASSB_NSHIFT 16 +#define IN_CLASSC(a) ((((in_addr_t)(a)) & 0xe0000000) == 0xc0000000) +#define IN_CLASSC_HOST (0xffffffff & ~IN_CLASSC_NET) +#define IN_CLASSC_NET 0xffffff00 +#define IN_CLASSC_NSHIFT 8 +#define IN_CLASSD(a) ((((in_addr_t)(a)) & 0xf0000000) == 0xe0000000) +#define IN_EXPERIMENTAL(a) ((((in_addr_t)(a)) & 0xe0000000) == 0xe0000000) +#define IN_LOOPBACKNET 127 +#define IN_MULTICAST(a) IN_CLASSD(a) +#define IOV_MAX 1024 +#define IP 244 +#define IP6F_MORE_FRAG 0x0100 +#define IP6F_OFF_MASK 0xf8ff +#define IP6F_RESERVED_MASK 0x0600 +#define IP6OPT_JUMBO 0xc2 +#define IP6OPT_JUMBO_LEN 6 +#define IP6OPT_NSAP_ADDR 0xc3 +#define IP6OPT_PAD1 0 +#define IP6OPT_PADN 1 +#define IP6OPT_ROUTER_ALERT 0x05 +#define IP6OPT_TUNNEL_LIMIT 0x04 +#define IP6OPT_TYPE(o) ((o) & 0xc0) +#define IP6OPT_TYPE_DISCARD 0x40 +#define IP6OPT_TYPE_FORCEICMP 0x80 +#define IP6OPT_TYPE_ICMP 0xc0 +#define IP6OPT_TYPE_MUTABLE 0x20 +#define IP6OPT_TYPE_SKIP 0x00 +#define IP6_ALERT_AN 0x0200 +#define IP6_ALERT_MLD 0x0000 +#define IP6_ALERT_RSVP 0x0100 +#define IPDEFTTL 64 +#define IPFRAGTTL 60 +#define IPOPT_CLASS(o) ((o) & IPOPT_CLASS_MASK) +#define IPOPT_CLASS_MASK 0x60 +#define IPOPT_CONTROL 0x00 +#define IPOPT_COPIED(o) ((o) & IPOPT_COPY) +#define IPOPT_COPY 0x80 +#define IPOPT_DEBMEAS 0x40 +#define IPOPT_END IPOPT_EOL +#define IPOPT_EOL 0 +#define IPOPT_LSRR 131 +#define IPOPT_MEASUREMENT IPOPT_DEBMEAS +#define IPOPT_MINOFF 4 +#define IPOPT_NOOP IPOPT_NOP +#define IPOPT_NOP 1 +#define IPOPT_NUMBER(o) ((o) & IPOPT_NUMBER_MASK) +#define IPOPT_NUMBER_MASK 0x1f +#define IPOPT_OFFSET 2 +#define IPOPT_OLEN 1 +#define IPOPT_OPTVAL 0 +#define IPOPT_RA 148 +#define IPOPT_RESERVED1 0x20 +#define IPOPT_RESERVED2 0x60 +#define IPOPT_RR 7 +#define IPOPT_SATID 136 +#define IPOPT_SEC IPOPT_SECURITY +#define IPOPT_SECURITY 130 +#define IPOPT_SECUR_CONFID 0xf135 +#define IPOPT_SECUR_EFTO 0x789a +#define IPOPT_SECUR_MMMM 0xbc4d +#define IPOPT_SECUR_RESTR 0xaf13 +#define IPOPT_SECUR_SECRET 0xd788 +#define IPOPT_SECUR_TOPSECRET 0x6bc5 +#define IPOPT_SECUR_UNCLASS 0x0000 +#define IPOPT_SID IPOPT_SATID +#define IPOPT_SSRR 137 +#define IPOPT_TIMESTAMP IPOPT_TS +#define IPOPT_TS 68 +#define IPOPT_TS_PRESPEC 3 +#define IPOPT_TS_TSANDADDR 1 +#define IPOPT_TS_TSONLY 0 +#define IPPORT_RESERVED 1024 +#define IPPROTO_ICMP 1 +#define IPPROTO_IP 0 +#define IPPROTO_IPV6 41 +#define IPPROTO_RAW 255 +#define IPPROTO_TCP 6 +#define IPPROTO_UDP 17 +#define IPTOS_CLASS(x) ((x) & IPTOS_CLASS_MASK) +#define IPTOS_CLASS_CS0 0x00 +#define IPTOS_CLASS_CS1 0x20 +#define IPTOS_CLASS_CS2 0x40 +#define IPTOS_CLASS_CS3 0x60 +#define IPTOS_CLASS_CS4 0x80 +#define IPTOS_CLASS_CS5 0xa0 +#define IPTOS_CLASS_CS6 0xc0 +#define IPTOS_CLASS_CS7 0xe0 +#define IPTOS_CLASS_DEFAULT IPTOS_CLASS_CS0 +#define IPTOS_CLASS_MASK 0xe0 +#define IPTOS_DSCP(x) ((x) & IPTOS_DSCP_MASK) +#define IPTOS_DSCP_AF11 0x28 +#define IPTOS_DSCP_AF12 0x30 +#define IPTOS_DSCP_AF13 0x38 +#define IPTOS_DSCP_AF21 0x48 +#define IPTOS_DSCP_AF22 0x50 +#define IPTOS_DSCP_AF23 0x58 +#define IPTOS_DSCP_AF31 0x68 +#define IPTOS_DSCP_AF32 0x70 +#define IPTOS_DSCP_AF33 0x78 +#define IPTOS_DSCP_AF41 0x88 +#define IPTOS_DSCP_AF42 0x90 +#define IPTOS_DSCP_AF43 0x98 +#define IPTOS_DSCP_EF 0xb8 +#define IPTOS_DSCP_MASK 0xfc +#define IPTOS_ECN(x) ((x) & IPTOS_ECN_MASK) +#define IPTOS_ECN_CE 0x03 +#define IPTOS_ECN_ECT0 0x02 +#define IPTOS_ECN_ECT1 0x01 +#define IPTOS_ECN_MASK 0x03 +#define IPTOS_ECN_NOT_ECT 0x00 +#define IPTOS_LOWCOST 0x02 +#define IPTOS_LOWDELAY 0x10 +#define IPTOS_MINCOST IPTOS_LOWCOST +#define IPTOS_PREC(tos) ((tos) & IPTOS_PREC_MASK) +#define IPTOS_PREC_CRITIC_ECP 0xa0 +#define IPTOS_PREC_FLASH 0x60 +#define IPTOS_PREC_FLASHOVERRIDE 0x80 +#define IPTOS_PREC_IMMEDIATE 0x40 +#define IPTOS_PREC_INTERNETCONTROL 0xc0 +#define IPTOS_PREC_MASK 0xe0 +#define IPTOS_PREC_NETCONTROL 0xe0 +#define IPTOS_PREC_PRIORITY 0x20 +#define IPTOS_PREC_ROUTINE 0x00 +#define IPTOS_RELIABILITY 0x04 +#define IPTOS_THROUGHPUT 0x08 +#define IPTOS_TOS(tos) ((tos) & IPTOS_TOS_MASK) +#define IPTOS_TOS_MASK 0x1E +#define IPTTLDEC 1 +#define IPV6_2292DSTOPTS 4 +#define IPV6_2292HOPLIMIT 8 +#define IPV6_2292HOPOPTS 3 +#define IPV6_2292PKTINFO 2 +#define IPV6_2292PKTOPTIONS 6 +#define IPV6_2292RTHDR 5 +#define IPV6_ADDRFORM 1 +#define IPV6_ADDR_PREFERENCES 72 +#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP +#define IPV6_AUTHHDR 10 +#define IPV6_AUTOFLOWLABEL 70 +#define IPV6_CHECKSUM 7 +#define IPV6_DONTFRAG 62 +#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP +#define IPV6_DSTOPTS 59 +#define IPV6_FREEBIND 78 +#define IPV6_HDRINCL 36 +#define IPV6_HOPLIMIT 52 +#define IPV6_HOPOPTS 54 +#define IPV6_IPSEC_POLICY 34 +#define IPV6_JOIN_ANYCAST 27 +#define IPV6_JOIN_GROUP 20 +#define IPV6_LEAVE_ANYCAST 28 +#define IPV6_LEAVE_GROUP 21 +#define IPV6_MINHOPCOUNT 73 +#define IPV6_MTU 24 +#define IPV6_MTU_DISCOVER 23 +#define IPV6_MULTICAST_ALL 29 +#define IPV6_MULTICAST_HOPS 18 +#define IPV6_MULTICAST_IF 17 +#define IPV6_MULTICAST_LOOP 19 +#define IPV6_NEXTHOP 9 +#define IPV6_ORIGDSTADDR 74 +#define IPV6_PATHMTU 61 +#define IPV6_PKTINFO 50 +#define IPV6_PMTUDISC_DO 2 +#define IPV6_PMTUDISC_DONT 0 +#define IPV6_PMTUDISC_INTERFACE 4 +#define IPV6_PMTUDISC_OMIT 5 +#define IPV6_PMTUDISC_PROBE 3 +#define IPV6_PMTUDISC_WANT 1 +#define IPV6_PREFER_SRC_CGA 0x0008 +#define IPV6_PREFER_SRC_COA 0x0004 +#define IPV6_PREFER_SRC_HOME 0x0400 +#define IPV6_PREFER_SRC_NONCGA 0x0800 +#define IPV6_PREFER_SRC_PUBLIC 0x0002 +#define IPV6_PREFER_SRC_PUBTMP_DEFAULT 0x0100 +#define IPV6_PREFER_SRC_TMP 0x0001 +#define IPV6_RECVDSTOPTS 58 +#define IPV6_RECVERR 25 +#define IPV6_RECVFRAGSIZE 77 +#define IPV6_RECVHOPLIMIT 51 +#define IPV6_RECVHOPOPTS 53 +#define IPV6_RECVORIGDSTADDR IPV6_ORIGDSTADDR +#define IPV6_RECVPATHMTU 60 +#define IPV6_RECVPKTINFO 49 +#define IPV6_RECVRTHDR 56 +#define IPV6_RECVTCLASS 66 +#define IPV6_ROUTER_ALERT 22 +#define IPV6_ROUTER_ALERT_ISOLATE 30 +#define IPV6_RTHDR 57 +#define IPV6_RTHDRDSTOPTS 55 +#define IPV6_RTHDR_LOOSE 0 +#define IPV6_RTHDR_STRICT 1 +#define IPV6_RTHDR_TYPE_0 0 +#define IPV6_RXDSTOPTS IPV6_DSTOPTS +#define IPV6_RXHOPOPTS IPV6_HOPOPTS +#define IPV6_TCLASS 67 +#define IPV6_TRANSPARENT 75 +#define IPV6_UNICAST_HOPS 16 +#define IPV6_UNICAST_IF 76 +#define IPV6_V6ONLY 26 +#define IPV6_XFRM_POLICY 35 +#define IPVERSION 4 +#define IP_ADD_MEMBERSHIP 35 +#define IP_ADD_SOURCE_MEMBERSHIP 39 +#define IP_BIND_ADDRESS_NO_PORT 24 +#define IP_BLOCK_SOURCE 38 +#define IP_CHECKSUM 23 +#define IP_DEFAULT_MULTICAST_LOOP 1 +#define IP_DEFAULT_MULTICAST_TTL 1 +#define IP_DF 0x4000 +#define IP_DROP_MEMBERSHIP 36 +#define IP_DROP_SOURCE_MEMBERSHIP 40 +#define IP_FREEBIND 15 +#define IP_HDRINCL 3 +#define IP_IPSEC_POLICY 16 +#define IP_MAXPACKET 65535 +#define IP_MAX_MEMBERSHIPS 20 +#define IP_MF 0x2000 +#define IP_MINTTL 21 +#define IP_MSFILTER 41 +#define IP_MSFILTER_SIZE(numsrc) (sizeof(struct ip_msfilter) - sizeof(struct in_addr) + (numsrc) * sizeof(struct in_addr)) +#define IP_MSS 576 +#define IP_MTU 14 +#define IP_MTU_DISCOVER 10 +#define IP_MULTICAST_ALL 49 +#define IP_MULTICAST_IF 32 +#define IP_MULTICAST_LOOP 34 +#define IP_MULTICAST_TTL 33 +#define IP_NODEFRAG 22 +#define IP_OFFMASK 0x1fff +#define IP_OPTIONS 4 +#define IP_ORIGDSTADDR 20 +#define IP_PASSSEC 18 +#define IP_PKTINFO 8 +#define IP_PKTOPTIONS 9 +#define IP_PMTUDISC 10 +#define IP_PMTUDISC_DO 2 +#define IP_PMTUDISC_DONT 0 +#define IP_PMTUDISC_INTERFACE 4 +#define IP_PMTUDISC_OMIT 5 +#define IP_PMTUDISC_PROBE 3 +#define IP_PMTUDISC_WANT 1 +#define IP_RECVERR 11 +#define IP_RECVERR_RFC4884 26 +#define IP_RECVFRAGSIZE 25 +#define IP_RECVOPTS 6 +#define IP_RECVORIGDSTADDR IP_ORIGDSTADDR +#define IP_RECVRETOPTS IP_RETOPTS +#define IP_RECVTOS 13 +#define IP_RECVTTL 12 +#define IP_RETOPTS 7 +#define IP_RF 0x8000 +#define IP_ROUTER_ALERT 5 +#define IP_TOS 1 +#define IP_TRANSPARENT 19 +#define IP_TTL 2 +#define IP_UNBLOCK_SOURCE 37 +#define IP_UNICAST_IF 50 +#define IP_XFRM_POLICY 17 +#define IQUERY ns_o_iquery +#define I_ATMARK (__SID |31) +#define I_CANPUT (__SID |34) +#define I_CKBAND (__SID |29) +#define I_FDINSERT (__SID |16) +#define I_FIND (__SID |11) +#define I_FLUSH (__SID | 5) +#define I_FLUSHBAND (__SID |28) +#define I_GETBAND (__SID |30) +#define I_GETCLTIME (__SID |33) +#define I_GETSIG (__SID |10) +#define I_GRDOPT (__SID | 7) +#define I_GWROPT (__SID |20) +#define I_LINK (__SID |12) +#define I_LIST (__SID |21) +#define I_LOOK (__SID | 4) +#define I_NREAD (__SID | 1) +#define I_PEEK (__SID |15) +#define I_PLINK (__SID |22) +#define I_POP (__SID | 3) +#define I_PUNLINK (__SID |23) +#define I_PUSH (__SID | 2) +#define I_RECVFD (__SID |14) +#define I_SENDFD (__SID |17) +#define I_SETCLTIME (__SID |32) +#define I_SETSIG (__SID | 9) +#define I_SRDOPT (__SID | 6) +#define I_STR (__SID | 8) +#define I_SWROPT (__SID |19) +#define I_UNLINK (__SID |13) +#define LASTMARK 0x02 +#define LC_ALL 6 +#define LC_ALL_MASK 0x7fffffff +#define LC_COLLATE 3 +#define LC_COLLATE_MASK (1<(b))?(a):(b)) +#define MAXCDNAME NS_MAXCDNAME +#define MAXDNAME NS_MAXDNAME +#define MAXDOUBLE DBL_MAX +#define MAXFLOAT FLT_MAX +#define MAXHOSTNAMELEN 64 +#define MAXINT INT_MAX +#define MAXLABEL NS_MAXLABEL +#define MAXLONG LONG_MAX +#define MAXNAMLEN 255 +#define MAXPATHLEN 4096 +#define MAXSHORT SHRT_MAX +#define MAXSYMLINKS 20 +#define MAXTC 6 +#define MAXTTL 255 +#define MAX_IPOPTLEN 40 +#define MB_CUR_MAX (__ctype_get_mb_cur_max()) +#define MB_LEN_MAX 4 +#define MCAST_BLOCK_SOURCE 43 +#define MCAST_EXCLUDE 0 +#define MCAST_INCLUDE 1 +#define MCAST_JOIN_GROUP 42 +#define MCAST_JOIN_SOURCE_GROUP 46 +#define MCAST_LEAVE_GROUP 45 +#define MCAST_LEAVE_SOURCE_GROUP 47 +#define MCAST_MSFILTER 48 +#define MCAST_UNBLOCK_SOURCE 44 +#define MIN(a,b) (((a)<(b))?(a):(b)) +#define MINDOUBLE DBL_MIN +#define MINFLOAT FLT_MIN +#define MININT INT_MIN +#define MINLONG LONG_MIN +#define MINSHORT SHRT_MIN +#define MLD_LISTENER_QUERY 130 +#define MLD_LISTENER_REDUCTION 132 +#define MLD_LISTENER_REPORT 131 +#define MM_APPL 8 +#define MM_CONSOLE 512 +#define MM_ERROR 2 +#define MM_FIRM 4 +#define MM_HALT 1 +#define MM_HARD 1 +#define MM_INFO 4 +#define MM_NOCON 4 +#define MM_NOMSG 1 +#define MM_NOSEV 0 +#define MM_NOTOK (-1) +#define MM_NRECOV 128 +#define MM_NULLACT ((char*)0) +#define MM_NULLLBL ((char*)0) +#define MM_NULLMC 0L +#define MM_NULLSEV 0 +#define MM_NULLTAG ((char*)0) +#define MM_NULLTXT ((char*)0) +#define MM_OK 0 +#define MM_OPSYS 32 +#define MM_PRINT 256 +#define MM_RECOVER 64 +#define MM_SOFT 2 +#define MM_UTIL 16 +#define MM_WARNING 3 +#define MODE_ACK 0x04 +#define MODE_B 2 +#define MODE_C 3 +#define MODE_ECHO 0x0200 +#define MODE_EDIT 0x01 +#define MODE_FLOW 0x0100 +#define MODE_FORCE 0x1000 +#define MODE_INBIN 0x0400 +#define MODE_LIT_ECHO 0x10 +#define MODE_MASK 0x1f +#define MODE_OUTBIN 0x0800 +#define MODE_S 1 +#define MODE_SOFT_TAB 0x08 +#define MODE_TRAPSIG 0x02 +#define MOD_CLKA ADJ_OFFSET_SINGLESHOT +#define MOD_CLKB ADJ_TICK +#define MOD_ESTERROR ADJ_ESTERROR +#define MOD_FREQUENCY ADJ_FREQUENCY +#define MOD_MAXERROR ADJ_MAXERROR +#define MOD_MICRO ADJ_MICRO +#define MOD_NANO ADJ_NANO +#define MOD_OFFSET ADJ_OFFSET +#define MOD_STATUS ADJ_STATUS +#define MOD_TAI ADJ_TAI +#define MOD_TIMECONST ADJ_TIMECONST +#define MON_1 0x2001A +#define MON_10 0x20023 +#define MON_11 0x20024 +#define MON_12 0x20025 +#define MON_2 0x2001B +#define MON_3 0x2001C +#define MON_4 0x2001D +#define MON_5 0x2001E +#define MON_6 0x2001F +#define MON_7 0x20020 +#define MON_8 0x20021 +#define MON_9 0x20022 +#define MORECTL 1 +#define MOREDATA 2 +#define MSG_ANY 0x02 +#define MSG_BAND 0x04 +#define MSG_DONTWAIT 0x0040 +#define MSG_HIPRI 0x01 +#define MSG_NOSIGNAL 0x4000 +#define MSG_PEEK 0x0002 +#define MSG_TRUNC 0x0020 +#define MSG_WAITALL 0x0100 +#define MUXID_ALL (-1) +#define M_1_PI 0.31830988618379067154 +#define M_2_PI 0.63661977236758134308 +#define M_2_SQRTPI 1.12837916709551257390 +#define M_E 2.7182818284590452354 +#define M_LN10 2.30258509299404568402 +#define M_LN2 0.69314718055994530942 +#define M_LOG10E 0.43429448190325182765 +#define M_LOG2E 1.4426950408889634074 +#define M_PI 3.14159265358979323846 +#define M_PI_2 1.57079632679489661923 +#define M_PI_4 0.78539816339744830962 +#define M_SQRT1_2 0.70710678118654752440 +#define M_SQRT2 1.41421356237309504880 +#define NAMESERVER_PORT NS_DEFAULTPORT +#define NAME_MAX 255 +#define NAN (0.0f/0.0f) +#define NBBY 8 +#define NCARGS 131072 +#define ND_NA_FLAG_OVERRIDE 0x00000020 +#define ND_NA_FLAG_ROUTER 0x00000080 +#define ND_NA_FLAG_SOLICITED 0x00000040 +#define ND_NEIGHBOR_ADVERT 136 +#define ND_NEIGHBOR_SOLICIT 135 +#define ND_OPT_HOME_AGENT_INFO 8 +#define ND_OPT_MTU 5 +#define ND_OPT_PI_FLAG_AUTO 0x40 +#define ND_OPT_PI_FLAG_ONLINK 0x80 +#define ND_OPT_PI_FLAG_RADDR 0x20 +#define ND_OPT_PREFIX_INFORMATION 3 +#define ND_OPT_REDIRECTED_HEADER 4 +#define ND_OPT_RTR_ADV_INTERVAL 7 +#define ND_OPT_SOURCE_LINKADDR 1 +#define ND_OPT_TARGET_LINKADDR 2 +#define ND_RA_FLAG_HOME_AGENT 0x20 +#define ND_RA_FLAG_MANAGED 0x80 +#define ND_RA_FLAG_OTHER 0x40 +#define ND_REDIRECT 137 +#define ND_ROUTER_ADVERT 134 +#define ND_ROUTER_SOLICIT 133 +#define NEW_ENV_VALUE 1 +#define NEW_ENV_VAR 0 +#define NGROUPS 32 +#define NGROUPS_MAX 32 +#define NL_ARGMAX 9 +#define NL_CAT_LOCALE 1 +#define NL_LANGMAX 32 +#define NL_LOCALE_NAME(cat) _NL_LOCALE_NAME(cat) +#define NL_MSGMAX 32767 +#define NL_NMAX 16 +#define NL_SETD 1 +#define NL_SETMAX 255 +#define NL_TEXTMAX 2048 +#define NOERROR ns_r_noerror +#define NOEXPR 0x50001 +#define NOFILE 256 +#define NOGROUP (-1) +#define NOP 241 +#define NOSTR 0x50003 +#define NOTAUTH ns_r_notauth +#define NOTIMP ns_r_notimpl +#define NOTZONE ns_r_notzone +#define NR_ICMP_TYPES 18 +#define NR_ICMP_UNREACH 15 +#define NSLC 18 +#define NS_ALG_DH 2 +#define NS_ALG_DSA 3 +#define NS_ALG_DSS NS_ALG_DSA +#define NS_ALG_EXPIRE_ONLY 253 +#define NS_ALG_MD5RSA 1 +#define NS_ALG_PRIVATE_OID 254 +#define NS_CMPRSFLGS 0xc0 +#define NS_DEFAULTPORT 53 +#define NS_DSA_MAX_BYTES 405 +#define NS_DSA_MIN_SIZE 213 +#define NS_DSA_SIG_SIZE 41 +#define NS_GET16(s,cp) (void)((s) = ns_get16(((cp)+=2)-2)) +#define NS_GET32(l,cp) (void)((l) = ns_get32(((cp)+=4)-4)) +#define NS_HFIXEDSZ 12 +#define NS_IN6ADDRSZ 16 +#define NS_INADDRSZ 4 +#define NS_INT16SZ 2 +#define NS_INT32SZ 4 +#define NS_INT8SZ 1 +#define NS_KEY_EXTENDED_FLAGS 0x1000 +#define NS_KEY_NAME_ENTITY 0x0200 +#define NS_KEY_NAME_RESERVED 0x0300 +#define NS_KEY_NAME_TYPE 0x0300 +#define NS_KEY_NAME_USER 0x0000 +#define NS_KEY_NAME_ZONE 0x0100 +#define NS_KEY_NO_AUTH 0x8000 +#define NS_KEY_NO_CONF 0x4000 +#define NS_KEY_PROT_ANY 255 +#define NS_KEY_PROT_DNSSEC 3 +#define NS_KEY_PROT_EMAIL 2 +#define NS_KEY_PROT_IPSEC 4 +#define NS_KEY_PROT_TLS 1 +#define NS_KEY_RESERVED10 0x0020 +#define NS_KEY_RESERVED11 0x0010 +#define NS_KEY_RESERVED2 0x2000 +#define NS_KEY_RESERVED4 0x0800 +#define NS_KEY_RESERVED5 0x0400 +#define NS_KEY_RESERVED8 0x0080 +#define NS_KEY_RESERVED9 0x0040 +#define NS_KEY_RESERVED_BITMASK ( NS_KEY_RESERVED2 | NS_KEY_RESERVED4 | NS_KEY_RESERVED5 | NS_KEY_RESERVED8 | NS_KEY_RESERVED9 | NS_KEY_RESERVED10 | NS_KEY_RESERVED11 ) +#define NS_KEY_RESERVED_BITMASK2 0xFFFF +#define NS_KEY_SIGNATORYMASK 0x000F +#define NS_KEY_TYPEMASK 0xC000 +#define NS_KEY_TYPE_AUTH_CONF 0x0000 +#define NS_KEY_TYPE_AUTH_ONLY 0x4000 +#define NS_KEY_TYPE_CONF_ONLY 0x8000 +#define NS_KEY_TYPE_NO_KEY 0xC000 +#define NS_MAXCDNAME 255 +#define NS_MAXDNAME 1025 +#define NS_MAXLABEL 63 +#define NS_MAXMSG 65535 +#define NS_MD5RSA_MAX_BASE64 (((NS_MD5RSA_MAX_BYTES+2)/3)*4) +#define NS_MD5RSA_MAX_BITS 4096 +#define NS_MD5RSA_MAX_BYTES ((NS_MD5RSA_MAX_BITS+7/8)*2+3) +#define NS_MD5RSA_MAX_SIZE ((NS_MD5RSA_MAX_BITS+7)/8) +#define NS_MD5RSA_MIN_BITS 512 +#define NS_MD5RSA_MIN_SIZE ((NS_MD5RSA_MIN_BITS+7)/8) +#define NS_NOTIFY_OP ns_o_notify +#define NS_NXT_BITS 8 +#define NS_NXT_BIT_CLEAR(n,p) (p[(n)/NS_NXT_BITS] &= ~(0x80>>((n)%NS_NXT_BITS))) +#define NS_NXT_BIT_ISSET(n,p) (p[(n)/NS_NXT_BITS] & (0x80>>((n)%NS_NXT_BITS))) +#define NS_NXT_BIT_SET(n,p) (p[(n)/NS_NXT_BITS] |= (0x80>>((n)%NS_NXT_BITS))) +#define NS_NXT_MAX 127 +#define NS_OPT_DNSSEC_OK 0x8000U +#define NS_OPT_NSID 3 +#define NS_PACKETSZ 512 +#define NS_PUT16(s,cp) ns_put16((s), ((cp)+=2)-2) +#define NS_PUT32(l,cp) ns_put32((l), ((cp)+=4)-4) +#define NS_QFIXEDSZ 4 +#define NS_RRFIXEDSZ 10 +#define NS_SIG_ALG 2 +#define NS_SIG_EXPIR 8 +#define NS_SIG_FOOT 16 +#define NS_SIG_LABELS 3 +#define NS_SIG_OTTL 4 +#define NS_SIG_SIGNED 12 +#define NS_SIG_SIGNER 18 +#define NS_SIG_TYPE 0 +#define NS_TSIG_ALG_HMAC_MD5 "HMAC-MD5.SIG-ALG.REG.INT" +#define NS_TSIG_ERROR_FORMERR -12 +#define NS_TSIG_ERROR_NO_SPACE -11 +#define NS_TSIG_ERROR_NO_TSIG -10 +#define NS_TSIG_FUDGE 300 +#define NS_TSIG_TCP_COUNT 100 +#define NS_UPDATE_OP ns_o_update +#define NTELOPTS (1+TELOPT_NEW_ENVIRON) +#define NULL ((void*)0) +#define NXDOMAIN ns_r_nxdomain +#define NXRRSET ns_r_nxrrset +#define NZERO 20 +#define OLD_ENV_VALUE 0 +#define OLD_ENV_VAR 1 +#define ONCE_FLAG_INIT 0 +#define O_ACCMODE (O_EXEC | O_RDWR | O_SEARCH) +#define O_APPEND 0x0001 +#define O_CLOEXEC (0) +#define O_CREAT (0x0001 << 12) +#define O_DIRECTORY (0x0002 << 12) +#define O_DSYNC 0x0002 +#define O_EXCL (0x0004 << 12) +#define O_EXEC (0x02000000) +#define O_NOCTTY (0) +#define O_NOFOLLOW (0x01000000) +#define O_NONBLOCK 0x0004 +#define O_RDONLY (0x04000000) +#define O_RDWR (O_RDONLY | O_WRONLY) +#define O_RSYNC 0x0008 +#define O_SEARCH (0x08000000) +#define O_SYNC 0x0010 +#define O_TRUNC (0x0008 << 12) +#define O_TTY_INIT (0) +#define O_WRONLY (0x10000000) +#define PACKETSZ NS_PACKETSZ +#define PACKET_ADD_MEMBERSHIP 1 +#define PACKET_AUXDATA 8 +#define PACKET_BROADCAST 1 +#define PACKET_COPY_THRESH 7 +#define PACKET_DROP_MEMBERSHIP 2 +#define PACKET_FANOUT 18 +#define PACKET_FANOUT_DATA 22 +#define PACKET_FASTROUTE 6 +#define PACKET_HDRLEN 11 +#define PACKET_HOST 0 +#define PACKET_IGNORE_OUTGOING 23 +#define PACKET_LOOPBACK 5 +#define PACKET_LOSS 14 +#define PACKET_MR_ALLMULTI 2 +#define PACKET_MR_MULTICAST 0 +#define PACKET_MR_PROMISC 1 +#define PACKET_MR_UNICAST 3 +#define PACKET_MULTICAST 2 +#define PACKET_ORIGDEV 9 +#define PACKET_OTHERHOST 3 +#define PACKET_OUTGOING 4 +#define PACKET_QDISC_BYPASS 20 +#define PACKET_RECV_OUTPUT 3 +#define PACKET_RESERVE 12 +#define PACKET_ROLLOVER_STATS 21 +#define PACKET_RX_RING 5 +#define PACKET_STATISTICS 6 +#define PACKET_TIMESTAMP 17 +#define PACKET_TX_HAS_OFF 19 +#define PACKET_TX_RING 13 +#define PACKET_TX_TIMESTAMP 16 +#define PACKET_VERSION 10 +#define PACKET_VNET_HDR 15 +#define PAGESIZE (0x10000) +#define PAGE_SIZE PAGESIZE +#define PATH_MAX 4096 +#define PDP_ENDIAN __PDP_ENDIAN +#define PF_INET 1 +#define PF_INET6 2 +#define PF_UNSPEC 0 +#define PIPE_BUF 4096 +#define PM_STR 0x20027 +#define POLLERR 0x1000 +#define POLLHUP 0x2000 +#define POLLIN POLLRDNORM +#define POLLNVAL 0x4000 +#define POLLOUT POLLWRNORM +#define POLLPRI 0x0200 +#define POLLRDNORM 0x1 +#define POLLWRNORM 0x2 +#define POSIX_CLOSE_RESTART 0 +#define POSIX_FADV_DONTNEED 4 +#define POSIX_FADV_NOREUSE 5 +#define POSIX_FADV_NORMAL 0 +#define POSIX_FADV_RANDOM 2 +#define POSIX_FADV_SEQUENTIAL 1 +#define POSIX_FADV_WILLNEED 3 +#define PRELIM 1 +#define PRIX16 __UINT16_FMTX__ +#define PRIX32 __UINT32_FMTX__ +#define PRIX64 __UINT64_FMTX__ +#define PRIX8 __UINT8_FMTX__ +#define PRIXFAST16 __UINT_FAST16_FMTX__ +#define PRIXFAST32 __UINT_FAST32_FMTX__ +#define PRIXFAST64 __UINT_FAST64_FMTX__ +#define PRIXFAST8 __UINT_FAST8_FMTX__ +#define PRIXLEAST16 __UINT_LEAST16_FMTX__ +#define PRIXLEAST32 __UINT_LEAST32_FMTX__ +#define PRIXLEAST64 __UINT_LEAST64_FMTX__ +#define PRIXLEAST8 __UINT_LEAST8_FMTX__ +#define PRIXMAX __UINTMAX_FMTX__ +#define PRIXPTR __UINTPTR_FMTX__ +#define PRId16 __INT16_FMTd__ +#define PRId32 __INT32_FMTd__ +#define PRId64 __INT64_FMTd__ +#define PRId8 __INT8_FMTd__ +#define PRIdFAST16 __INT_FAST16_FMTd__ +#define PRIdFAST32 __INT_FAST32_FMTd__ +#define PRIdFAST64 __INT_FAST64_FMTd__ +#define PRIdFAST8 __INT_FAST8_FMTd__ +#define PRIdLEAST16 __INT_LEAST16_FMTd__ +#define PRIdLEAST32 __INT_LEAST32_FMTd__ +#define PRIdLEAST64 __INT_LEAST64_FMTd__ +#define PRIdLEAST8 __INT_LEAST8_FMTd__ +#define PRIdMAX __INTMAX_FMTd__ +#define PRIdPTR __INTPTR_FMTd__ +#define PRIi16 __INT16_FMTi__ +#define PRIi32 __INT32_FMTi__ +#define PRIi64 __INT64_FMTi__ +#define PRIi8 __INT8_FMTi__ +#define PRIiFAST16 __INT_FAST16_FMTi__ +#define PRIiFAST32 __INT_FAST32_FMTi__ +#define PRIiFAST64 __INT_FAST64_FMTi__ +#define PRIiFAST8 __INT_FAST8_FMTi__ +#define PRIiLEAST16 __INT_LEAST16_FMTi__ +#define PRIiLEAST32 __INT_LEAST32_FMTi__ +#define PRIiLEAST64 __INT_LEAST64_FMTi__ +#define PRIiLEAST8 __INT_LEAST8_FMTi__ +#define PRIiMAX __INTMAX_FMTi__ +#define PRIiPTR __INTPTR_FMTi__ +#define PRIo16 __UINT16_FMTo__ +#define PRIo32 __UINT32_FMTo__ +#define PRIo64 __UINT64_FMTo__ +#define PRIo8 __UINT8_FMTo__ +#define PRIoFAST16 __UINT_FAST16_FMTo__ +#define PRIoFAST32 __UINT_FAST32_FMTo__ +#define PRIoFAST64 __UINT_FAST64_FMTo__ +#define PRIoFAST8 __UINT_FAST8_FMTo__ +#define PRIoLEAST16 __UINT_LEAST16_FMTo__ +#define PRIoLEAST32 __UINT_LEAST32_FMTo__ +#define PRIoLEAST64 __UINT_LEAST64_FMTo__ +#define PRIoLEAST8 __UINT_LEAST8_FMTo__ +#define PRIoMAX __UINTMAX_FMTo__ +#define PRIoPTR __UINTPTR_FMTo__ +#define PRIu16 __UINT16_FMTu__ +#define PRIu32 __UINT32_FMTu__ +#define PRIu64 __UINT64_FMTu__ +#define PRIu8 __UINT8_FMTu__ +#define PRIuFAST16 __UINT_FAST16_FMTu__ +#define PRIuFAST32 __UINT_FAST32_FMTu__ +#define PRIuFAST64 __UINT_FAST64_FMTu__ +#define PRIuFAST8 __UINT_FAST8_FMTu__ +#define PRIuLEAST16 __UINT_LEAST16_FMTu__ +#define PRIuLEAST32 __UINT_LEAST32_FMTu__ +#define PRIuLEAST64 __UINT_LEAST64_FMTu__ +#define PRIuLEAST8 __UINT_LEAST8_FMTu__ +#define PRIuMAX __UINTMAX_FMTu__ +#define PRIuPTR __UINTPTR_FMTu__ +#define PRIx16 __UINT16_FMTx__ +#define PRIx32 __UINT32_FMTx__ +#define PRIx64 __UINT64_FMTx__ +#define PRIx8 __UINT8_FMTx__ +#define PRIxFAST16 __UINT_FAST16_FMTx__ +#define PRIxFAST32 __UINT_FAST32_FMTx__ +#define PRIxFAST64 __UINT_FAST64_FMTx__ +#define PRIxFAST8 __UINT_FAST8_FMTx__ +#define PRIxLEAST16 __UINT_LEAST16_FMTx__ +#define PRIxLEAST32 __UINT_LEAST32_FMTx__ +#define PRIxLEAST64 __UINT_LEAST64_FMTx__ +#define PRIxLEAST8 __UINT_LEAST8_FMTx__ +#define PRIxMAX __UINTMAX_FMTx__ +#define PRIxPTR __UINTPTR_FMTx__ +#define PTHREAD_BARRIER_SERIAL_THREAD (-1) +#define PTHREAD_CANCELED ((void *)-1) +#define PTHREAD_CANCEL_ASYNCHRONOUS 1 +#define PTHREAD_CANCEL_DEFERRED 0 +#define PTHREAD_CANCEL_DISABLE 1 +#define PTHREAD_CANCEL_ENABLE 0 +#define PTHREAD_CANCEL_MASKED 2 +#define PTHREAD_COND_INITIALIZER {{{0}}} +#define PTHREAD_CREATE_DETACHED 1 +#define PTHREAD_CREATE_JOINABLE 0 +#define PTHREAD_DESTRUCTOR_ITERATIONS 4 +#define PTHREAD_EXPLICIT_SCHED 1 +#define PTHREAD_INHERIT_SCHED 0 +#define PTHREAD_KEYS_MAX 128 +#define PTHREAD_MUTEX_DEFAULT 0 +#define PTHREAD_MUTEX_ERRORCHECK 2 +#define PTHREAD_MUTEX_INITIALIZER {{{0}}} +#define PTHREAD_MUTEX_NORMAL 0 +#define PTHREAD_MUTEX_RECURSIVE 1 +#define PTHREAD_MUTEX_ROBUST 1 +#define PTHREAD_MUTEX_STALLED 0 +#define PTHREAD_NULL ((pthread_t)0) +#define PTHREAD_ONCE_INIT 0 +#define PTHREAD_PRIO_INHERIT 1 +#define PTHREAD_PRIO_NONE 0 +#define PTHREAD_PRIO_PROTECT 2 +#define PTHREAD_PROCESS_PRIVATE 0 +#define PTHREAD_PROCESS_SHARED 1 +#define PTHREAD_RWLOCK_INITIALIZER {{{0}}} +#define PTHREAD_SCOPE_PROCESS 1 +#define PTHREAD_SCOPE_SYSTEM 0 +#define PTHREAD_STACK_MIN 2048 +#define PTRBITS (sizeof(char *) * 8) +#define PTRDIFF_MAX INT32_MAX +#define PTRDIFF_MIN INT32_MIN +#define PUTLONG NS_PUT32 +#define PUTSHORT NS_PUT16 +#define QFIXEDSZ NS_QFIXEDSZ +#define QUERY ns_o_query +#define RADIXCHAR 0x10000 +#define RAND_MAX (0x7fffffff) +#define REC_EOF '\002' +#define REC_EOR '\001' +#define REC_ESC '\377' +#define REFUSED ns_r_refused +#define REGTYPE '0' +#define REG_BADBR 10 +#define REG_BADPAT 2 +#define REG_BADRPT 13 +#define REG_EBRACE 9 +#define REG_EBRACK 7 +#define REG_ECOLLATE 3 +#define REG_ECTYPE 4 +#define REG_EESCAPE 5 +#define REG_ENOSYS -1 +#define REG_EPAREN 8 +#define REG_ERANGE 11 +#define REG_ESPACE 12 +#define REG_ESUBREG 6 +#define REG_EXTENDED 1 +#define REG_ICASE 2 +#define REG_NEWLINE 4 +#define REG_NOMATCH 1 +#define REG_NOSUB 8 +#define REG_NOTBOL 1 +#define REG_NOTEOL 2 +#define REG_OK 0 +#define RENAME_EXCHANGE (1 << 1) +#define RENAME_NOREPLACE (1 << 0) +#define RENAME_WHITEOUT (1 << 2) +#define RE_DUP_MAX 255 +#define RMSGD 0x0001 +#define RMSGN 0x0002 +#define RNORM 0x0000 +#define RPM_PCO_ADD 1 +#define RPM_PCO_CHANGE 2 +#define RPM_PCO_SETGLOBAL 3 +#define RPROTDAT 0x0004 +#define RPROTDIS 0x0008 +#define RPROTMASK 0x001C +#define RPROTNORM 0x0010 +#define RRFIXEDSZ NS_RRFIXEDSZ +#define RRQ 01 +#define RS_HIPRI 0x01 +#define RTLD_DEFAULT ((void *)0) +#define RTLD_GLOBAL 256 +#define RTLD_LAZY 1 +#define RTLD_LOCAL 8 +#define RTLD_NEXT ((void *)-1) +#define RTLD_NODELETE 4096 +#define RTLD_NOLOAD 4 +#define RTLD_NOW 2 +#define RUSAGE_CHILDREN 2 +#define RUSAGE_SELF 1 +#define R_OK (4) +#define SARMAG 8 +#define SB 250 +#define SCHAR_MAX 127 +#define SCHAR_MIN (-128) +#define SCNd16 __INT16_FMTd__ +#define SCNd32 __INT32_FMTd__ +#define SCNd64 __INT64_FMTd__ +#define SCNd8 __INT8_FMTd__ +#define SCNdFAST16 __INT_FAST16_FMTd__ +#define SCNdFAST32 __INT_FAST32_FMTd__ +#define SCNdFAST64 __INT_FAST64_FMTd__ +#define SCNdFAST8 __INT_FAST8_FMTd__ +#define SCNdLEAST16 __INT_LEAST16_FMTd__ +#define SCNdLEAST32 __INT_LEAST32_FMTd__ +#define SCNdLEAST64 __INT_LEAST64_FMTd__ +#define SCNdLEAST8 __INT_LEAST8_FMTd__ +#define SCNdMAX __INTMAX_FMTd__ +#define SCNdPTR __INTPTR_FMTd__ +#define SCNi16 __INT16_FMTi__ +#define SCNi32 __INT32_FMTi__ +#define SCNi64 __INT64_FMTi__ +#define SCNi8 __INT8_FMTi__ +#define SCNiFAST16 __INT_FAST16_FMTi__ +#define SCNiFAST32 __INT_FAST32_FMTi__ +#define SCNiFAST64 __INT_FAST64_FMTi__ +#define SCNiFAST8 __INT_FAST8_FMTi__ +#define SCNiLEAST16 __INT_LEAST16_FMTi__ +#define SCNiLEAST32 __INT_LEAST32_FMTi__ +#define SCNiLEAST64 __INT_LEAST64_FMTi__ +#define SCNiLEAST8 __INT_LEAST8_FMTi__ +#define SCNiMAX __INTMAX_FMTi__ +#define SCNiPTR __INTPTR_FMTi__ +#define SCNo16 __UINT16_FMTo__ +#define SCNo32 __UINT32_FMTo__ +#define SCNo64 __UINT64_FMTo__ +#define SCNo8 __UINT8_FMTo__ +#define SCNoFAST16 __UINT_FAST16_FMTo__ +#define SCNoFAST32 __UINT_FAST32_FMTo__ +#define SCNoFAST64 __UINT_FAST64_FMTo__ +#define SCNoFAST8 __UINT_FAST8_FMTo__ +#define SCNoLEAST16 __UINT_LEAST16_FMTo__ +#define SCNoLEAST32 __UINT_LEAST32_FMTo__ +#define SCNoLEAST64 __UINT_LEAST64_FMTo__ +#define SCNoLEAST8 __UINT_LEAST8_FMTo__ +#define SCNoMAX __UINTMAX_FMTo__ +#define SCNoPTR __UINTPTR_FMTo__ +#define SCNu16 __UINT16_FMTu__ +#define SCNu32 __UINT32_FMTu__ +#define SCNu64 __UINT64_FMTu__ +#define SCNu8 __UINT8_FMTu__ +#define SCNuFAST16 __UINT_FAST16_FMTu__ +#define SCNuFAST32 __UINT_FAST32_FMTu__ +#define SCNuFAST64 __UINT_FAST64_FMTu__ +#define SCNuFAST8 __UINT_FAST8_FMTu__ +#define SCNuLEAST16 __UINT_LEAST16_FMTu__ +#define SCNuLEAST32 __UINT_LEAST32_FMTu__ +#define SCNuLEAST64 __UINT_LEAST64_FMTu__ +#define SCNuLEAST8 __UINT_LEAST8_FMTu__ +#define SCNuMAX __UINTMAX_FMTu__ +#define SCNuPTR __UINTPTR_FMTu__ +#define SCNx16 __UINT16_FMTx__ +#define SCNx32 __UINT32_FMTx__ +#define SCNx64 __UINT64_FMTx__ +#define SCNx8 __UINT8_FMTx__ +#define SCNxFAST16 __UINT_FAST16_FMTx__ +#define SCNxFAST32 __UINT_FAST32_FMTx__ +#define SCNxFAST64 __UINT_FAST64_FMTx__ +#define SCNxFAST8 __UINT_FAST8_FMTx__ +#define SCNxLEAST16 __UINT_LEAST16_FMTx__ +#define SCNxLEAST32 __UINT_LEAST32_FMTx__ +#define SCNxLEAST64 __UINT_LEAST64_FMTx__ +#define SCNxLEAST8 __UINT_LEAST8_FMTx__ +#define SCNxMAX __UINTMAX_FMTx__ +#define SCNxPTR __UINTPTR_FMTx__ +#define SE 240 +#define SEEK_CUR 1 +#define SEEK_END 2 +#define SEEK_SET 0 +#define SEGSIZE 512 +#define SEM_FAILED ((sem_t *)0) +#define SEM_NSEMS_MAX 256 +#define SEM_VALUE_MAX 0x7fffffff +#define SERVFAIL ns_r_servfail +#define SHORTBITS (sizeof(short) * 8) +#define SHRT_MAX 0x7fff +#define SHRT_MIN (-1-0x7fff) +#define SHUT_RD 1 +#define SHUT_RDWR (SHUT_RD | SHUT_WR) +#define SHUT_WR 2 +#define SIG_ATOMIC_MAX INT32_MAX +#define SIG_ATOMIC_MIN INT32_MIN +#define SIZE_MAX UINT32_MAX +#define SI_LOAD_SHIFT 16 +#define SLC_ABORT 7 +#define SLC_ACK 0x80 +#define SLC_AO 4 +#define SLC_AYT 5 +#define SLC_BRK 2 +#define SLC_CANTCHANGE 1 +#define SLC_DEFAULT 3 +#define SLC_EC 10 +#define SLC_EL 11 +#define SLC_EOF 8 +#define SLC_EOR 6 +#define SLC_EW 12 +#define SLC_FLAGS 1 +#define SLC_FLUSHIN 0x40 +#define SLC_FLUSHOUT 0x20 +#define SLC_FORW1 17 +#define SLC_FORW2 18 +#define SLC_FUNC 0 +#define SLC_IP 3 +#define SLC_LEVELBITS 0x03 +#define SLC_LNEXT 14 +#define SLC_NAME(x) slc_names[x] +#define SLC_NAMELIST "0", "SYNCH", "BRK", "IP", "AO", "AYT", "EOR", "ABORT", "EOF", "SUSP", "EC", "EL", "EW", "RP", "LNEXT", "XON", "XOFF", "FORW1", "FORW2", 0, +#define SLC_NAMES SLC_NAMELIST +#define SLC_NAME_OK(x) ((unsigned int)(x) <= NSLC) +#define SLC_NOSUPPORT 0 +#define SLC_RP 13 +#define SLC_SUSP 9 +#define SLC_SYNCH 1 +#define SLC_VALUE 2 +#define SLC_VARIABLE 2 +#define SLC_XOFF 16 +#define SLC_XON 15 +#define SNDPIPE 0x002 +#define SNDZERO 0x001 +#define SOCK_CLOEXEC (0x00002000) +#define SOCK_DGRAM 5 +#define SOCK_NONBLOCK (0x00004000) +#define SOCK_STREAM 6 +#define SOL_IP 0 +#define SOL_IPV6 41 +#define SOL_SOCKET 0x7fffffff +#define SOL_TCP 6 +#define SOL_UDP 17 +#define SOMAXCONN 128 +#define SO_ACCEPTCONN 30 +#define SO_DOMAIN 39 +#define SO_ERROR 4 +#define SO_KEEPALIVE 9 +#define SO_PROTOCOL 38 +#define SO_RCVBUF 8 +#define SO_RCVTIMEO 66 +#define SO_REUSEADDR 2 +#define SO_SNDBUF 7 +#define SO_SNDTIMEO 67 +#define SO_TYPE 3 +#define SSIZE_MAX LONG_MAX +#define STATUS ns_o_status +#define STA_CLK 0x8000 +#define STA_CLOCKERR 0x1000 +#define STA_DEL 0x0020 +#define STA_FLL 0x0008 +#define STA_FREQHOLD 0x0080 +#define STA_INS 0x0010 +#define STA_MODE 0x4000 +#define STA_NANO 0x2000 +#define STA_PLL 0x0001 +#define STA_PPSERROR 0x0800 +#define STA_PPSFREQ 0x0002 +#define STA_PPSJITTER 0x0200 +#define STA_PPSSIGNAL 0x0100 +#define STA_PPSTIME 0x0004 +#define STA_PPSWANDER 0x0400 +#define STA_RONLY (STA_PPSSIGNAL | STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR | STA_CLOCKERR | STA_NANO | STA_MODE | STA_CLK) +#define STA_UNSYNC 0x0040 +#define STDERR_FILENO 2 +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STRU_F 1 +#define STRU_P 3 +#define STRU_R 2 +#define ST_APPEND 256 +#define ST_IMMUTABLE 512 +#define ST_MANDLOCK 64 +#define ST_NOATIME 1024 +#define ST_NODEV 4 +#define ST_NODIRATIME 2048 +#define ST_NOEXEC 8 +#define ST_NOSUID 2 +#define ST_RDONLY 1 +#define ST_RELATIME 4096 +#define ST_SYNCHRONOUS 16 +#define ST_WRITE 128 +#define SUN_LEN(s) (2+strlen((s)->sun_path)) +#define SUSP 237 +#define SYMLOOP_MAX 40 +#define SYMTYPE '2' +#define SYNCH 242 +#define S_ADDT ns_s_ar +#define S_BANDURG 0x0200 +#define S_ERROR 0x0010 +#define S_HANGUP 0x0020 +#define S_HIPRI 0x0002 +#define S_IEXEC S_IXUSR +#define S_IFBLK (0x6000) +#define S_IFCHR (0x2000) +#define S_IFDIR (0x4000) +#define S_IFIFO (0x1000) +#define S_IFLNK (0xa000) +#define S_IFMT (S_IFBLK | S_IFCHR | S_IFDIR | S_IFIFO | S_IFLNK | S_IFREG | S_IFSOCK) +#define S_IFREG (0x8000) +#define S_IFSOCK (0xc000) +#define S_INPUT 0x0001 +#define S_IREAD S_IRUSR +#define S_IRGRP (0x20) +#define S_IROTH (0x4) +#define S_IRUSR (0x100) +#define S_IRWXG (S_IXGRP | S_IWGRP | S_IRGRP) +#define S_IRWXO (S_IXOTH | S_IWOTH | S_IROTH) +#define S_IRWXU (S_IXUSR | S_IWUSR | S_IRUSR) +#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) +#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) +#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) +#define S_ISGID (0x400) +#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) +#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) +#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) +#define S_ISUID (0x800) +#define S_ISVTX (0x200) +#define S_IWGRP (0x10) +#define S_IWOTH (0x2) +#define S_IWRITE S_IWUSR +#define S_IWUSR (0x80) +#define S_IXGRP (0x8) +#define S_IXOTH (0x1) +#define S_IXUSR (0x40) +#define S_MSG 0x0008 +#define S_OUTPUT 0x0004 +#define S_PREREQ ns_s_pr +#define S_RDBAND 0x0080 +#define S_RDNORM 0x0040 +#define S_UPDATE ns_s_ud +#define S_WRBAND 0x0100 +#define S_WRNORM S_OUTPUT +#define S_ZONE ns_s_zn +#define TCPI_OPT_ECN 8 +#define TCPI_OPT_SACK 2 +#define TCPI_OPT_TIMESTAMPS 1 +#define TCPI_OPT_WSCALE 4 +#define TCPOLEN_MAXSEG 4 +#define TCPOLEN_SACK_PERMITTED 2 +#define TCPOLEN_TIMESTAMP 10 +#define TCPOLEN_WINDOW 3 +#define TCPOPT_EOL 0 +#define TCPOPT_MAXSEG 2 +#define TCPOPT_NOP 1 +#define TCPOPT_SACK 5 +#define TCPOPT_SACK_PERMITTED 4 +#define TCPOPT_TIMESTAMP 8 +#define TCPOPT_WINDOW 3 +#define TCP_CA_CWR 2 +#define TCP_CA_Disorder 1 +#define TCP_CA_Loss 4 +#define TCP_CA_Open 0 +#define TCP_CA_Recovery 3 +#define TCP_CC_INFO 26 +#define TCP_CLOSE 7 +#define TCP_CLOSE_WAIT 8 +#define TCP_CLOSING 11 +#define TCP_CM_INQ TCP_INQ +#define TCP_CONGESTION 13 +#define TCP_CORK 3 +#define TCP_DEFER_ACCEPT 9 +#define TCP_ENCAP_ESPINTCP 7 +#define TCP_ESTABLISHED 1 +#define TCP_FASTOPEN 23 +#define TCP_FASTOPEN_CONNECT 30 +#define TCP_FASTOPEN_KEY 33 +#define TCP_FASTOPEN_NO_COOKIE 34 +#define TCP_FIN_WAIT1 4 +#define TCP_FIN_WAIT2 5 +#define TCP_INFO 11 +#define TCP_INQ 36 +#define TCP_KEEPCNT 6 +#define TCP_KEEPIDLE 4 +#define TCP_KEEPINTVL 5 +#define TCP_LAST_ACK 9 +#define TCP_LINGER2 8 +#define TCP_LISTEN 10 +#define TCP_MAXSEG 2 +#define TCP_MD5SIG 14 +#define TCP_MD5SIG_EXT 32 +#define TCP_MD5SIG_FLAG_IFINDEX 0x2 +#define TCP_MD5SIG_FLAG_PREFIX 0x1 +#define TCP_MD5SIG_MAXKEYLEN 80 +#define TCP_NODELAY 1 +#define TCP_NOTSENT_LOWAT 25 +#define TCP_QUEUE_SEQ 21 +#define TCP_QUICKACK 12 +#define TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT 0x1 +#define TCP_REPAIR 19 +#define TCP_REPAIR_OFF 0 +#define TCP_REPAIR_OFF_NO_WP -1 +#define TCP_REPAIR_ON 1 +#define TCP_REPAIR_OPTIONS 22 +#define TCP_REPAIR_QUEUE 20 +#define TCP_REPAIR_WINDOW 29 +#define TCP_SAVED_SYN 28 +#define TCP_SAVE_SYN 27 +#define TCP_SYNCNT 7 +#define TCP_SYN_RECV 3 +#define TCP_SYN_SENT 2 +#define TCP_THIN_DUPACK 17 +#define TCP_THIN_LINEAR_TIMEOUTS 16 +#define TCP_TIMESTAMP 24 +#define TCP_TIME_WAIT 6 +#define TCP_TX_DELAY 37 +#define TCP_ULP 31 +#define TCP_USER_TIMEOUT 18 +#define TCP_WINDOW_CLAMP 10 +#define TCP_ZEROCOPY_RECEIVE 35 +#define TELCMD(x) telcmds[(x)-TELCMD_FIRST] +#define TELCMD_FIRST xEOF +#define TELCMD_LAST IAC +#define TELCMD_OK(x) ((unsigned int)(x) <= TELCMD_LAST && (unsigned int)(x) >= TELCMD_FIRST) +#define TELOPT_3270REGIME 29 +#define TELOPT_AUTHENTICATION 37 +#define TELOPT_BINARY 0 +#define TELOPT_BM 19 +#define TELOPT_DET 20 +#define TELOPT_ECHO 1 +#define TELOPT_ENCRYPT 38 +#define TELOPT_EOR 25 +#define TELOPT_EXOPL 255 +#define TELOPT_LFLOW 33 +#define TELOPT_LINEMODE 34 +#define TELOPT_LOGOUT 18 +#define TELOPT_NAMS 4 +#define TELOPT_NAOCRD 10 +#define TELOPT_NAOFFD 13 +#define TELOPT_NAOHTD 12 +#define TELOPT_NAOHTS 11 +#define TELOPT_NAOL 8 +#define TELOPT_NAOLFD 16 +#define TELOPT_NAOP 9 +#define TELOPT_NAOVTD 15 +#define TELOPT_NAOVTS 14 +#define TELOPT_NAWS 31 +#define TELOPT_NEW_ENVIRON 39 +#define TELOPT_OLD_ENVIRON 36 +#define TELOPT_OUTMRK 27 +#define TELOPT_RCP 2 +#define TELOPT_RCTE 7 +#define TELOPT_SGA 3 +#define TELOPT_SNDLOC 23 +#define TELOPT_STATUS 5 +#define TELOPT_SUPDUP 21 +#define TELOPT_SUPDUPOUTPUT 22 +#define TELOPT_TM 6 +#define TELOPT_TSPEED 32 +#define TELOPT_TTYLOC 28 +#define TELOPT_TTYPE 24 +#define TELOPT_TUID 26 +#define TELOPT_X3PAD 30 +#define TELOPT_XASCII 17 +#define TELOPT_XDISPLOC 35 +#define TELQUAL_INFO 2 +#define TELQUAL_IS 0 +#define TELQUAL_NAME 3 +#define TELQUAL_REPLY 2 +#define TELQUAL_SEND 1 +#define TGEXEC 00010 +#define TGREAD 00040 +#define TGWRITE 00020 +#define THOUSEP 0x10001 +#define TH_ACK 0x10 +#define TH_FIN 0x01 +#define TH_PUSH 0x08 +#define TH_RST 0x04 +#define TH_SYN 0x02 +#define TH_URG 0x20 +#define TIMER_ABSTIME 1 +#define TIMESPEC_TO_TIMEVAL(tv,ts) ( (tv)->tv_sec = (ts)->tv_sec, (tv)->tv_usec = (ts)->tv_nsec / 1000, (void)0 ) +#define TIMEVAL_TO_TIMESPEC(tv,ts) ( (ts)->tv_sec = (tv)->tv_sec, (ts)->tv_nsec = (tv)->tv_usec * 1000, (void)0 ) +#define TIME_BAD TIME_ERROR +#define TIME_DEL 2 +#define TIME_ERROR 5 +#define TIME_INS 1 +#define TIME_OK 0 +#define TIME_OOP 3 +#define TIME_UTC 1 +#define TIME_WAIT 4 +#define TMAGIC "ustar" +#define TMAGLEN 6 +#define TOEXEC 00001 +#define TOREAD 00004 +#define TOWRITE 00002 +#define TRANSIENT 4 +#define TSGID 02000 +#define TSS_DTOR_ITERATIONS 4 +#define TSUID 04000 +#define TSVTX 01000 +#define TTYDEF_CFLAG (CREAD | CS7 | PARENB | HUPCL) +#define TTYDEF_IFLAG (BRKINT | ISTRIP | ICRNL | IMAXBEL | IXON | IXANY) +#define TTYDEF_LFLAG (ECHO | ICANON | ISIG | IEXTEN | ECHOE|ECHOKE|ECHOCTL) +#define TTYDEF_OFLAG (OPOST | ONLCR | XTABS) +#define TTYDEF_SPEED (B9600) +#define TTY_NAME_MAX 32 +#define TUEXEC 00100 +#define TUREAD 00400 +#define TUWRITE 00200 +#define TVERSION "00" +#define TVERSLEN 2 +#define TYPE_A 1 +#define TYPE_E 2 +#define TYPE_I 3 +#define TYPE_L 4 +#define TZNAME_MAX 6 +#define T_A ns_t_a +#define T_A6 ns_t_a6 +#define T_AAAA ns_t_aaaa +#define T_AFSDB ns_t_afsdb +#define T_ANY ns_t_any +#define T_ATMA ns_t_atma +#define T_AVC ns_t_avc +#define T_AXFR ns_t_axfr +#define T_CAA ns_t_caa +#define T_CDNSKEY ns_t_cdnskey +#define T_CDS ns_t_cds +#define T_CNAME ns_t_cname +#define T_CSYNC ns_t_csync +#define T_DHCID ns_t_dhcid +#define T_DLV ns_t_dlv +#define T_DNAME ns_t_dname +#define T_DNSKEY ns_t_dnskey +#define T_DS ns_t_ds +#define T_EID ns_t_eid +#define T_EUI48 ns_t_eui48 +#define T_EUI64 ns_t_eui64 +#define T_FMT 0x2002A +#define T_FMT_AMPM 0x2002B +#define T_GID ns_t_gid +#define T_GPOS ns_t_gpos +#define T_HINFO ns_t_hinfo +#define T_HIP ns_t_hip +#define T_IPSECKEY ns_t_ipseckey +#define T_ISDN ns_t_isdn +#define T_IXFR ns_t_ixfr +#define T_KEY ns_t_key +#define T_L32 ns_t_l32 +#define T_L64 ns_t_l64 +#define T_LOC ns_t_loc +#define T_LP ns_t_lp +#define T_MAILA ns_t_maila +#define T_MAILB ns_t_mailb +#define T_MB ns_t_mb +#define T_MD ns_t_md +#define T_MF ns_t_mf +#define T_MG ns_t_mg +#define T_MINFO ns_t_minfo +#define T_MR ns_t_mr +#define T_MX ns_t_mx +#define T_NAPTR ns_t_naptr +#define T_NID ns_t_nid +#define T_NIMLOC ns_t_nimloc +#define T_NINFO ns_t_ninfo +#define T_NS ns_t_ns +#define T_NSAP ns_t_nsap +#define T_NSAP_PTR ns_t_nsap_ptr +#define T_NSEC ns_t_nsec +#define T_NSEC3 ns_t_nsec3 +#define T_NSEC3PARAM ns_t_nsec3param +#define T_NULL ns_t_null +#define T_NXT ns_t_nxt +#define T_OPENPGPKEY ns_t_openpgpkey +#define T_PTR ns_t_ptr +#define T_PX ns_t_px +#define T_RKEY ns_t_rkey +#define T_RP ns_t_rp +#define T_RRSIG ns_t_rrsig +#define T_RT ns_t_rt +#define T_SIG ns_t_sig +#define T_SMIMEA ns_t_smimea +#define T_SOA ns_t_soa +#define T_SPF ns_t_spf +#define T_SRV ns_t_srv +#define T_SSHFP ns_t_sshfp +#define T_TA ns_t_ta +#define T_TALINK ns_t_talink +#define T_TKEY ns_t_tkey +#define T_TLSA ns_t_tlsa +#define T_TSIG ns_t_tsig +#define T_TXT ns_t_txt +#define T_UID ns_t_uid +#define T_UINFO ns_t_uinfo +#define T_UNSPEC ns_t_unspec +#define T_URI ns_t_uri +#define T_WKS ns_t_wks +#define T_X25 ns_t_x25 +#define UCHAR_MAX 255 +#define UDP_CORK 1 +#define UDP_ENCAP 100 +#define UDP_ENCAP_ESPINUDP 2 +#define UDP_ENCAP_ESPINUDP_NON_IKE 1 +#define UDP_ENCAP_GTP0 4 +#define UDP_ENCAP_GTP1U 5 +#define UDP_ENCAP_L2TPINUDP 3 +#define UDP_ENCAP_RXRPC 6 +#define UDP_GRO 104 +#define UDP_NO_CHECK6_RX 102 +#define UDP_NO_CHECK6_TX 101 +#define UDP_SEGMENT 103 +#define UINT16_C(c) c +#define UINT16_MAX (0xffff) +#define UINT32_C(c) c ## U +#define UINT32_MAX (0xffffffffu) +#define UINT64_C(c) c ## ULL +#define UINT64_MAX (0xffffffffffffffffu) +#define UINT8_C(c) c +#define UINT8_MAX (0xff) +#define UINTMAX_C(c) c ## ULL +#define UINTMAX_MAX UINT64_MAX +#define UINTPTR_MAX UINT32_MAX +#define UINT_FAST16_MAX UINT32_MAX +#define UINT_FAST32_MAX UINT32_MAX +#define UINT_FAST64_MAX UINT64_MAX +#define UINT_FAST8_MAX UINT8_MAX +#define UINT_LEAST16_MAX UINT16_MAX +#define UINT_LEAST32_MAX UINT32_MAX +#define UINT_LEAST64_MAX UINT64_MAX +#define UINT_LEAST8_MAX UINT8_MAX +#define UINT_MAX 0xffffffffU +#define UIO_MAXIOV 1024 +#define ULLONG_MAX (2ULL*LLONG_MAX+1) +#define ULONG_MAX (2UL*LONG_MAX+1) +#define USHRT_MAX 0xffff +#define UTIME_NOW (-1) +#define UTIME_OMIT (-2) +#define WCHAR_MAX (0x7fffffff+L'\0') +#define WCHAR_MIN (-1-0x7fffffff+L'\0') +#define WEOF 0xffffffffU +#define WILL 251 +#define WINT_MAX UINT32_MAX +#define WINT_MIN 0U +#define WONT 252 +#define WORD_BIT 32 +#define WRQ 02 +#define W_OK (2) +#define X_OK (1) +#define YESEXPR 0x50000 +#define YESSTR 0x50002 +#define YXDOMAIN ns_r_yxdomain +#define YXRRSET ns_r_yxrrset +#define _ALLOCA_H +#define _ALL_SOURCE 1 +#define _ARPA_FTP_H +#define _ARPA_INET_H +#define _ARPA_NAMESER_H +#define _ARPA_TELNET_H +#define _ARPA_TFTP_H +#define _AR_H +#define _BYTESWAP_H +#define _COMPLEX_H +#define _CPIO_H +#define _CRYPT_H +#define _CS_GNU_LIBC_VERSION 2 +#define _CS_GNU_LIBPTHREAD_VERSION 3 +#define _CS_PATH 0 +#define _CS_POSIX_V5_WIDTH_RESTRICTED_ENVS 4 +#define _CS_POSIX_V6_ILP32_OFF32_CFLAGS 1116 +#define _CS_POSIX_V6_ILP32_OFF32_LDFLAGS 1117 +#define _CS_POSIX_V6_ILP32_OFF32_LIBS 1118 +#define _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS 1119 +#define _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS 1120 +#define _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS 1121 +#define _CS_POSIX_V6_ILP32_OFFBIG_LIBS 1122 +#define _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS 1123 +#define _CS_POSIX_V6_LP64_OFF64_CFLAGS 1124 +#define _CS_POSIX_V6_LP64_OFF64_LDFLAGS 1125 +#define _CS_POSIX_V6_LP64_OFF64_LIBS 1126 +#define _CS_POSIX_V6_LP64_OFF64_LINTFLAGS 1127 +#define _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS 1128 +#define _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS 1129 +#define _CS_POSIX_V6_LPBIG_OFFBIG_LIBS 1130 +#define _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS 1131 +#define _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS 1 +#define _CS_POSIX_V7_ILP32_OFF32_CFLAGS 1132 +#define _CS_POSIX_V7_ILP32_OFF32_LDFLAGS 1133 +#define _CS_POSIX_V7_ILP32_OFF32_LIBS 1134 +#define _CS_POSIX_V7_ILP32_OFF32_LINTFLAGS 1135 +#define _CS_POSIX_V7_ILP32_OFFBIG_CFLAGS 1136 +#define _CS_POSIX_V7_ILP32_OFFBIG_LDFLAGS 1137 +#define _CS_POSIX_V7_ILP32_OFFBIG_LIBS 1138 +#define _CS_POSIX_V7_ILP32_OFFBIG_LINTFLAGS 1139 +#define _CS_POSIX_V7_LP64_OFF64_CFLAGS 1140 +#define _CS_POSIX_V7_LP64_OFF64_LDFLAGS 1141 +#define _CS_POSIX_V7_LP64_OFF64_LIBS 1142 +#define _CS_POSIX_V7_LP64_OFF64_LINTFLAGS 1143 +#define _CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS 1144 +#define _CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS 1145 +#define _CS_POSIX_V7_LPBIG_OFFBIG_LIBS 1146 +#define _CS_POSIX_V7_LPBIG_OFFBIG_LINTFLAGS 1147 +#define _CS_POSIX_V7_THREADS_CFLAGS 1150 +#define _CS_POSIX_V7_THREADS_LDFLAGS 1151 +#define _CS_POSIX_V7_WIDTH_RESTRICTED_ENVS 5 +#define _CS_V6_ENV 1148 +#define _CS_V7_ENV 1149 +#define _CTYPE_H +#define _Complex_I (0.0f+1.0fi) +#define _DIRENT_H +#define _DIRENT_HAVE_D_TYPE +#define _DLFCN_H +#define _ENDIAN_H +#define _ERRNO_H +#define _ERR_H +#define _FCNTL_H +#define _FEATURES_H +#define _FENV_H +#define _FLOAT_H +#define _FMTMSG_H +#define _FNMATCH_H +#define _FTS_H_ +#define _FTW_H +#define _GETOPT_H +#define _GLOB_H +#define _GNU_SOURCE 1 +#define _ICONV_H +#define _IFADDRS_H +#define _ILP32 1 +#define _INTTYPES_H +#define _IOFBF 0 +#define _IOLBF 1 +#define _IONBF 2 +#define _ISO646_H +#define _LANGINFO_H +#define _LIBGEN_H +#define _LIMITS_H +#define _LOCALE_H +#define _MALLOC_H +#define _MATH_H +#define _MONETARY_H +#define _MQUEUE_H +#define _NETINET_ICMP6_H +#define _NETINET_IGMP_H +#define _NETINET_IN_H +#define _NETINET_IN_SYSTM_H +#define _NETINET_IP6_H +#define _NETINET_IP_H +#define _NETINET_IP_ICMP_H +#define _NETINET_TCP_H +#define _NETINET_UDP_H +#define _NETPACKET_PACKET_H +#define _NL_LOCALE_NAME(cat) (((cat)<<16) | 0xffff) +#define _NL_TYPES_H +#define _PC_2_SYMLINKS 20 +#define _PC_ALLOC_SIZE_MIN 18 +#define _PC_ASYNC_IO 10 +#define _PC_CHOWN_RESTRICTED 6 +#define _PC_FILESIZEBITS 13 +#define _PC_LINK_MAX 0 +#define _PC_MAX_CANON 1 +#define _PC_MAX_INPUT 2 +#define _PC_NAME_MAX 3 +#define _PC_NO_TRUNC 7 +#define _PC_PATH_MAX 4 +#define _PC_PIPE_BUF 5 +#define _PC_PRIO_IO 11 +#define _PC_REC_INCR_XFER_SIZE 14 +#define _PC_REC_MAX_XFER_SIZE 15 +#define _PC_REC_MIN_XFER_SIZE 16 +#define _PC_REC_XFER_ALIGN 17 +#define _PC_SOCK_MAXBUF 12 +#define _PC_SYMLINK_MAX 19 +#define _PC_SYNC_IO 9 +#define _PC_VDISABLE 8 +#define _POLL_H +#define _POSIX2_BC_BASE_MAX 99 +#define _POSIX2_BC_DIM_MAX 2048 +#define _POSIX2_BC_SCALE_MAX 99 +#define _POSIX2_BC_STRING_MAX 1000 +#define _POSIX2_CHARCLASS_NAME_MAX 14 +#define _POSIX2_COLL_WEIGHTS_MAX 2 +#define _POSIX2_C_BIND _POSIX_VERSION +#define _POSIX2_EXPR_NEST_MAX 32 +#define _POSIX2_LINE_MAX 2048 +#define _POSIX2_RE_DUP_MAX 255 +#define _POSIX2_VERSION _POSIX_VERSION +#define _POSIX_ADVISORY_INFO _POSIX_VERSION +#define _POSIX_AIO_LISTIO_MAX 2 +#define _POSIX_AIO_MAX 1 +#define _POSIX_ARG_MAX 4096 +#define _POSIX_BARRIERS _POSIX_VERSION +#define _POSIX_CHILD_MAX 25 +#define _POSIX_CHOWN_RESTRICTED 1 +#define _POSIX_CLOCKRES_MIN 20000000 +#define _POSIX_CLOCK_SELECTION _POSIX_VERSION +#define _POSIX_CPUTIME _POSIX_VERSION +#define _POSIX_DELAYTIMER_MAX 32 +#define _POSIX_FSYNC _POSIX_VERSION +#define _POSIX_HOST_NAME_MAX 255 +#define _POSIX_IPV6 _POSIX_VERSION +#define _POSIX_LINK_MAX 8 +#define _POSIX_LOGIN_NAME_MAX 9 +#define _POSIX_MAX_CANON 255 +#define _POSIX_MAX_INPUT 255 +#define _POSIX_MONOTONIC_CLOCK _POSIX_VERSION +#define _POSIX_MQ_OPEN_MAX 8 +#define _POSIX_MQ_PRIO_MAX 32 +#define _POSIX_NAME_MAX 14 +#define _POSIX_NGROUPS_MAX 8 +#define _POSIX_NO_TRUNC 1 +#define _POSIX_OPEN_MAX 20 +#define _POSIX_PATH_MAX 256 +#define _POSIX_PIPE_BUF 512 +#define _POSIX_READER_WRITER_LOCKS _POSIX_VERSION +#define _POSIX_REALTIME_SIGNALS _POSIX_VERSION +#define _POSIX_REGEXP 1 +#define _POSIX_RE_DUP_MAX 255 +#define _POSIX_RTSIG_MAX 8 +#define _POSIX_SEM_NSEMS_MAX 256 +#define _POSIX_SEM_VALUE_MAX 32767 +#define _POSIX_SIGQUEUE_MAX 32 +#define _POSIX_SPIN_LOCKS _POSIX_VERSION +#define _POSIX_SSIZE_MAX 32767 +#define _POSIX_SS_REPL_MAX 4 +#define _POSIX_STREAM_MAX 8 +#define _POSIX_SYMLINK_MAX 255 +#define _POSIX_SYMLOOP_MAX 8 +#define _POSIX_THREADS _POSIX_VERSION +#define _POSIX_THREAD_ATTR_STACKSIZE _POSIX_VERSION +#define _POSIX_THREAD_DESTRUCTOR_ITERATIONS 4 +#define _POSIX_THREAD_KEYS_MAX 128 +#define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION +#define _POSIX_THREAD_SAFE_FUNCTIONS _POSIX_VERSION +#define _POSIX_THREAD_THREADS_MAX 64 +#define _POSIX_TIMEOUTS _POSIX_VERSION +#define _POSIX_TIMERS _POSIX_VERSION +#define _POSIX_TIMER_MAX 32 +#define _POSIX_TRACE_EVENT_NAME_MAX 30 +#define _POSIX_TRACE_NAME_MAX 8 +#define _POSIX_TRACE_SYS_MAX 8 +#define _POSIX_TRACE_USER_EVENT_MAX 32 +#define _POSIX_TTY_NAME_MAX 9 +#define _POSIX_TZNAME_MAX 6 +#define _POSIX_V6_ILP32_OFFBIG 1 +#define _POSIX_V7_ILP32_OFFBIG 1 +#define _POSIX_VDISABLE 0 +#define _POSIX_VERSION 200809L +#define _PTHREAD_H +#define _PTRDIFF_T +#define _REGEX_H +#define _SCHED_H +#define _SC_2_CHAR_TERM 95 +#define _SC_2_C_BIND 47 +#define _SC_2_C_DEV 48 +#define _SC_2_FORT_DEV 49 +#define _SC_2_FORT_RUN 50 +#define _SC_2_LOCALEDEF 52 +#define _SC_2_PBS 168 +#define _SC_2_PBS_ACCOUNTING 169 +#define _SC_2_PBS_CHECKPOINT 175 +#define _SC_2_PBS_LOCATE 170 +#define _SC_2_PBS_MESSAGE 171 +#define _SC_2_PBS_TRACK 172 +#define _SC_2_SW_DEV 51 +#define _SC_2_UPE 97 +#define _SC_2_VERSION 46 +#define _SC_ADVISORY_INFO 132 +#define _SC_AIO_LISTIO_MAX 23 +#define _SC_AIO_MAX 24 +#define _SC_AIO_PRIO_DELTA_MAX 25 +#define _SC_ARG_MAX 0 +#define _SC_ASYNCHRONOUS_IO 12 +#define _SC_ATEXIT_MAX 87 +#define _SC_AVPHYS_PAGES 86 +#define _SC_BARRIERS 133 +#define _SC_BC_BASE_MAX 36 +#define _SC_BC_DIM_MAX 37 +#define _SC_BC_SCALE_MAX 38 +#define _SC_BC_STRING_MAX 39 +#define _SC_CHILD_MAX 1 +#define _SC_CLK_TCK 2 +#define _SC_CLOCK_SELECTION 137 +#define _SC_COLL_WEIGHTS_MAX 40 +#define _SC_CPUTIME 138 +#define _SC_DELAYTIMER_MAX 26 +#define _SC_EXPR_NEST_MAX 42 +#define _SC_FSYNC 15 +#define _SC_GETGR_R_SIZE_MAX 69 +#define _SC_GETPW_R_SIZE_MAX 70 +#define _SC_HOST_NAME_MAX 180 +#define _SC_IOV_MAX 60 +#define _SC_IPV6 235 +#define _SC_JOB_CONTROL 7 +#define _SC_LINE_MAX 43 +#define _SC_LOGIN_NAME_MAX 71 +#define _SC_MAPPED_FILES 16 +#define _SC_MEMLOCK 17 +#define _SC_MEMLOCK_RANGE 18 +#define _SC_MEMORY_PROTECTION 19 +#define _SC_MESSAGE_PASSING 20 +#define _SC_MINSIGSTKSZ 249 +#define _SC_MONOTONIC_CLOCK 149 +#define _SC_MQ_OPEN_MAX 27 +#define _SC_MQ_PRIO_MAX 28 +#define _SC_NGROUPS_MAX 3 +#define _SC_NPROCESSORS_CONF 83 +#define _SC_NPROCESSORS_ONLN 84 +#define _SC_NZERO 109 +#define _SC_OPEN_MAX 4 +#define _SC_PAGESIZE 30 +#define _SC_PAGE_SIZE 30 +#define _SC_PASS_MAX 88 +#define _SC_PHYS_PAGES 85 +#define _SC_PRIORITIZED_IO 13 +#define _SC_PRIORITY_SCHEDULING 10 +#define _SC_RAW_SOCKETS 236 +#define _SC_READER_WRITER_LOCKS 153 +#define _SC_REALTIME_SIGNALS 9 +#define _SC_REGEXP 155 +#define _SC_RE_DUP_MAX 44 +#define _SC_RTSIG_MAX 31 +#define _SC_SAVED_IDS 8 +#define _SC_SEMAPHORES 21 +#define _SC_SEM_NSEMS_MAX 32 +#define _SC_SEM_VALUE_MAX 33 +#define _SC_SHARED_MEMORY_OBJECTS 22 +#define _SC_SHELL 157 +#define _SC_SIGQUEUE_MAX 34 +#define _SC_SIGSTKSZ 250 +#define _SC_SPAWN 159 +#define _SC_SPIN_LOCKS 154 +#define _SC_SPORADIC_SERVER 160 +#define _SC_SS_REPL_MAX 241 +#define _SC_STREAMS 174 +#define _SC_STREAM_MAX 5 +#define _SC_SYMLOOP_MAX 173 +#define _SC_SYNCHRONIZED_IO 14 +#define _SC_THREADS 67 +#define _SC_THREAD_ATTR_STACKADDR 77 +#define _SC_THREAD_ATTR_STACKSIZE 78 +#define _SC_THREAD_CPUTIME 139 +#define _SC_THREAD_DESTRUCTOR_ITERATIONS 73 +#define _SC_THREAD_KEYS_MAX 74 +#define _SC_THREAD_PRIORITY_SCHEDULING 79 +#define _SC_THREAD_PRIO_INHERIT 80 +#define _SC_THREAD_PRIO_PROTECT 81 +#define _SC_THREAD_PROCESS_SHARED 82 +#define _SC_THREAD_ROBUST_PRIO_INHERIT 247 +#define _SC_THREAD_ROBUST_PRIO_PROTECT 248 +#define _SC_THREAD_SAFE_FUNCTIONS 68 +#define _SC_THREAD_SPORADIC_SERVER 161 +#define _SC_THREAD_STACK_MIN 75 +#define _SC_THREAD_THREADS_MAX 76 +#define _SC_TIMEOUTS 164 +#define _SC_TIMERS 11 +#define _SC_TIMER_MAX 35 +#define _SC_TRACE 181 +#define _SC_TRACE_EVENT_FILTER 182 +#define _SC_TRACE_EVENT_NAME_MAX 242 +#define _SC_TRACE_INHERIT 183 +#define _SC_TRACE_LOG 184 +#define _SC_TRACE_NAME_MAX 243 +#define _SC_TRACE_SYS_MAX 244 +#define _SC_TRACE_USER_EVENT_MAX 245 +#define _SC_TTY_NAME_MAX 72 +#define _SC_TYPED_MEMORY_OBJECTS 165 +#define _SC_TZNAME_MAX 6 +#define _SC_UIO_MAXIOV 60 +#define _SC_V6_ILP32_OFF32 176 +#define _SC_V6_ILP32_OFFBIG 177 +#define _SC_V6_LP64_OFF64 178 +#define _SC_V6_LPBIG_OFFBIG 179 +#define _SC_V7_ILP32_OFF32 237 +#define _SC_V7_ILP32_OFFBIG 238 +#define _SC_V7_LP64_OFF64 239 +#define _SC_V7_LPBIG_OFFBIG 240 +#define _SC_VERSION 29 +#define _SC_XBS5_ILP32_OFF32 125 +#define _SC_XBS5_ILP32_OFFBIG 126 +#define _SC_XBS5_LP64_OFF64 127 +#define _SC_XBS5_LPBIG_OFFBIG 128 +#define _SC_XOPEN_CRYPT 92 +#define _SC_XOPEN_ENH_I18N 93 +#define _SC_XOPEN_LEGACY 129 +#define _SC_XOPEN_REALTIME 130 +#define _SC_XOPEN_REALTIME_THREADS 131 +#define _SC_XOPEN_SHM 94 +#define _SC_XOPEN_STREAMS 246 +#define _SC_XOPEN_UNIX 91 +#define _SC_XOPEN_VERSION 89 +#define _SC_XOPEN_XCU_VERSION 90 +#define _SC_XOPEN_XPG2 98 +#define _SC_XOPEN_XPG3 99 +#define _SC_XOPEN_XPG4 100 +#define _SEARCH_H +#define _SEMAPHORE_H +#define _SIZE_T +#define _STDALIGN_H +#define _STDBOOL_H +#define _STDC_PREDEF_H +#define _STDINT_H +#define _STDIO_EXT_H +#define _STDIO_H +#define _STDLIB_H +#define _STDNORETURN_H +#define _STRINGS_H +#define _STRING_H +#define _STROPTS_H +#define _SYSEXITS_H +#define _SYS_EVENTFD_H +#define _SYS_FILE_H +#define _SYS_IOCTL_H +#define _SYS_PARAM_H +#define _SYS_RANDOM_H +#define _SYS_REG_H +#define _SYS_SELECT_H +#define _SYS_SOCKET_H +#define _SYS_STATVFS_H +#define _SYS_STAT_H +#define _SYS_SYSCALL_H +#define _SYS_SYSINFO_H +#define _SYS_TIMEB_H +#define _SYS_TIMEX_H +#define _SYS_TIME_H +#define _SYS_TTYDEFAULTS_H +#define _SYS_TYPES_H +#define _SYS_UIO_H +#define _SYS_UN_H +#define _SYS_UTSNAME_H +#define _TAR_H +#define _TGMATH_H +#define _THREADS_H +#define _TIME_H +#define _UCHAR_H +#define _UNISTD_H +#define _UTIME_H +#define _VALUES_H +#define _VA_LIST +#define _WCHAR_H +#define _WCHAR_T +#define _WCTYPE_H +#define _WINT_T +#define _XOPEN_ENH_I18N 1 +#define _XOPEN_IOV_MAX 16 +#define _XOPEN_NAME_MAX 255 +#define _XOPEN_PATH_MAX 1024 +#define _XOPEN_UNIX 1 +#define _XOPEN_VERSION 700 +#define __ARE_4_EQUAL(a,b) (!( (0[a]-0[b]) | (1[a]-1[b]) | (2[a]-2[b]) | (3[a]-3[b]) )) +#define __ATOMIC_ACQUIRE 2 +#define __ATOMIC_ACQ_REL 4 +#define __ATOMIC_CONSUME 1 +#define __ATOMIC_RELAXED 0 +#define __ATOMIC_RELEASE 3 +#define __ATOMIC_SEQ_CST 5 +#define __BIGGEST_ALIGNMENT__ 16 +#define __BIG_ENDIAN 4321 +#define __BIND 19950621 +#define __BINDINGS_WEBP2_H +#define __BYTE_ORDER __BYTE_ORDER__ +#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ +#define __CHAR16_TYPE__ unsigned short +#define __CHAR32_TYPE__ unsigned int +#define __CHAR_BIT__ 8 +#define __compiler_ATOMIC_BOOL_LOCK_FREE 2 +#define __compiler_ATOMIC_CHAR16_T_LOCK_FREE 2 +#define __compiler_ATOMIC_CHAR32_T_LOCK_FREE 2 +#define __compiler_ATOMIC_CHAR_LOCK_FREE 2 +#define __compiler_ATOMIC_INT_LOCK_FREE 2 +#define __compiler_ATOMIC_LLONG_LOCK_FREE 2 +#define __compiler_ATOMIC_LONG_LOCK_FREE 2 +#define __compiler_ATOMIC_POINTER_LOCK_FREE 2 +#define __compiler_ATOMIC_SHORT_LOCK_FREE 2 +#define __compiler_ATOMIC_WCHAR_T_LOCK_FREE 2 +#define __CLANG_MAX_ALIGN_T_DEFINED +#define __CMPLX(x,y,t) (__builtin_complex((t)(x), (t)(y))) +#define __CONSTANT_CFSTRINGS__ 1 +#define __DBLCX(x) (__IS_CX(x) && sizeof(x) == sizeof(double complex)) +#define __DBL_DECIMAL_DIG__ 17 +#define __DBL_DIG__ 15 +#define __DBL_EPSILON__ 2.2204460492503131e-16 +#define __DBL_HAS_DENORM__ 1 +#define __DBL_HAS_INFINITY__ 1 +#define __DBL_HAS_QUIET_NAN__ 1 +#define __DBL_MANT_DIG__ 53 +#define __DBL_MAX_10_EXP__ 308 +#define __DBL_MAX_EXP__ 1024 +#define __DBL_MIN_10_EXP__ (-307) +#define __DBL_MIN_EXP__ (-1021) +#define __DECIMAL_DIG__ __LDBL_DECIMAL_DIG__ +#define __DEFINED_FILE +#define __DEFINED___isoc_va_list +#define __DEFINED_blkcnt_t +#define __DEFINED_blksize_t +#define __DEFINED_clock_t +#define __DEFINED_clockid_t +#define __DEFINED_cnd_t +#define __DEFINED_dev_t +#define __DEFINED_double_t +#define __DEFINED_float_t +#define __DEFINED_fsblkcnt_t +#define __DEFINED_fsfilcnt_t +#define __DEFINED_gid_t +#define __DEFINED_id_t +#define __DEFINED_ino_t +#define __DEFINED_int16_t +#define __DEFINED_int32_t +#define __DEFINED_int64_t +#define __DEFINED_int8_t +#define __DEFINED_intmax_t +#define __DEFINED_intptr_t +#define __DEFINED_key_t +#define __DEFINED_locale_t +#define __DEFINED_mbstate_t +#define __DEFINED_mode_t +#define __DEFINED_mtx_t +#define __DEFINED_nlink_t +#define __DEFINED_off_t +#define __DEFINED_pid_t +#define __DEFINED_pthread_attr_t +#define __DEFINED_pthread_barrier_t +#define __DEFINED_pthread_barrierattr_t +#define __DEFINED_pthread_cond_t +#define __DEFINED_pthread_condattr_t +#define __DEFINED_pthread_key_t +#define __DEFINED_pthread_mutex_t +#define __DEFINED_pthread_mutexattr_t +#define __DEFINED_pthread_once_t +#define __DEFINED_pthread_rwlock_t +#define __DEFINED_pthread_rwlockattr_t +#define __DEFINED_pthread_spinlock_t +#define __DEFINED_pthread_t +#define __DEFINED_register_t +#define __DEFINED_regoff_t +#define __DEFINED_sa_family_t +#define __DEFINED_sigset_t +#define __DEFINED_size_t +#define __DEFINED_socklen_t +#define __DEFINED_ssize_t +#define __DEFINED_suseconds_t +#define __DEFINED_time_t +#define __DEFINED_timer_t +#define __DEFINED_u_int64_t +#define __DEFINED_uid_t +#define __DEFINED_uint16_t +#define __DEFINED_uint32_t +#define __DEFINED_uint64_t +#define __DEFINED_uint8_t +#define __DEFINED_uintmax_t +#define __DEFINED_uintptr_t +#define __DEFINED_useconds_t +#define __DEFINED_va_list +#define __DEFINED_wchar_t +#define __DEFINED_wctype_t +#define __DEFINED_wint_t +#define __FINITE_MATH_ONLY__ 0 +#define __FLOAT128__ 1 +#define __FLT(x) (__IS_REAL(x) && sizeof(x) == sizeof(float)) +#define __FLTCX(x) (__IS_CX(x) && sizeof(x) == sizeof(float complex)) +#define __FLT_DECIMAL_DIG__ 9 +#define __FLT_DIG__ 6 +#define __FLT_EPSILON__ 1.19209290e-7F +#define __FLT_HAS_DENORM__ 1 +#define __FLT_HAS_INFINITY__ 1 +#define __FLT_HAS_QUIET_NAN__ 1 +#define __FLT_MANT_DIG__ 24 +#define __FLT_MAX_10_EXP__ 38 +#define __FLT_MAX_EXP__ 128 +#define __FLT_MIN_10_EXP__ (-37) +#define __FLT_MIN_EXP__ (-125) +#define __FLT_RADIX__ 2 +#define __compiler_ATOMIC_BOOL_LOCK_FREE 2 +#define __compiler_ATOMIC_CHAR16_T_LOCK_FREE 2 +#define __compiler_ATOMIC_CHAR32_T_LOCK_FREE 2 +#define __compiler_ATOMIC_CHAR_LOCK_FREE 2 +#define __compiler_ATOMIC_INT_LOCK_FREE 2 +#define __compiler_ATOMIC_LLONG_LOCK_FREE 2 +#define __compiler_ATOMIC_LONG_LOCK_FREE 2 +#define __compiler_ATOMIC_POINTER_LOCK_FREE 2 +#define __compiler_ATOMIC_SHORT_LOCK_FREE 2 +#define __GCC_ATOMIC_TEST_AND_SET_TRUEVAL 1 +#define __compiler_ATOMIC_WCHAR_T_LOCK_FREE 2 +#define __GNUC_STDC_INLINE__ 1 +#define __GNUC_VA_LIST 1 +#define __GXX_ABI_VERSION 1002 +#define __ILP32__ 1 +#define __INT16_C_SUFFIX__ +#define __INT16_FMTd__ "hd" +#define __INT16_FMTi__ "hi" +#define __INT16_TYPE__ short +#define __INT32_C_SUFFIX__ +#define __INT32_FMTd__ "d" +#define __INT32_FMTi__ "i" +#define __INT32_TYPE__ int +#define __INT64_C_SUFFIX__ LL +#define __INT64_FMTd__ "lld" +#define __INT64_FMTi__ "lli" +#define __INT64_TYPE__ long long int +#define __INT8_C_SUFFIX__ +#define __INT8_FMTd__ "hhd" +#define __INT8_FMTi__ "hhi" +#define __INT8_TYPE__ signed char +#define __INTMAX_C_SUFFIX__ LL +#define __INTMAX_FMTd__ "lld" +#define __INTMAX_FMTi__ "lli" +#define __INTMAX_TYPE__ long long int +#define __INTMAX_WIDTH__ 64 +#define __INTPTR_FMTd__ "ld" +#define __INTPTR_FMTi__ "li" +#define __INTPTR_TYPE__ long int +#define __INTPTR_WIDTH__ 32 +#define __INT_FAST16_FMTd__ "hd" +#define __INT_FAST16_FMTi__ "hi" +#define __INT_FAST16_TYPE__ short +#define __INT_FAST32_FMTd__ "d" +#define __INT_FAST32_FMTi__ "i" +#define __INT_FAST32_TYPE__ int +#define __INT_FAST64_FMTd__ "lld" +#define __INT_FAST64_FMTi__ "lli" +#define __INT_FAST64_TYPE__ long long int +#define __INT_FAST8_FMTd__ "hhd" +#define __INT_FAST8_FMTi__ "hhi" +#define __INT_FAST8_TYPE__ signed char +#define __INT_LEAST16_FMTd__ "hd" +#define __INT_LEAST16_FMTi__ "hi" +#define __INT_LEAST16_TYPE__ short +#define __INT_LEAST32_FMTd__ "d" +#define __INT_LEAST32_FMTi__ "i" +#define __INT_LEAST32_TYPE__ int +#define __INT_LEAST64_FMTd__ "lld" +#define __INT_LEAST64_FMTi__ "lli" +#define __INT_LEAST64_TYPE__ long long int +#define __INT_LEAST8_FMTd__ "hhd" +#define __INT_LEAST8_FMTi__ "hhi" +#define __INT_LEAST8_TYPE__ signed char +#define __IS_CX(x) (__IS_FP(x) && sizeof(x) == sizeof((x)+I)) +#define __IS_FP(x) (sizeof((x)+1ULL) == sizeof((x)+1.0f)) +#define __IS_REAL(x) (__IS_FP(x) && 2*sizeof(x) == sizeof((x)+I)) +#define __LDBL(x) (__IS_REAL(x) && sizeof(x) == sizeof(long double) && sizeof(long double) != sizeof(double)) +#define __LDBLCX(x) (__IS_CX(x) && sizeof(x) == sizeof(long double complex) && sizeof(long double) != sizeof(double)) +#define __LDBL_DECIMAL_DIG__ 36 +#define __LDBL_DIG__ 33 +#define __LDBL_EPSILON__ 1.92592994438723585305597794258492732e-34L +#define __LDBL_HAS_DENORM__ 1 +#define __LDBL_HAS_INFINITY__ 1 +#define __LDBL_HAS_QUIET_NAN__ 1 +#define __LDBL_MANT_DIG__ 113 +#define __LDBL_MAX_10_EXP__ 4932 +#define __LDBL_MAX_EXP__ 16384 +#define __LDBL_MIN_10_EXP__ (-4931) +#define __LDBL_MIN_EXP__ (-16381) +#define __LITTLE_ENDIAN 1234 +#define __LITTLE_ENDIAN__ 1 +#define __NAMESER 19991006 +#define __NEED_FILE +#define __NEED___isoc_va_list +#define __NEED_blkcnt_t +#define __NEED_blksize_t +#define __NEED_clock_t +#define __NEED_clockid_t +#define __NEED_cnd_t +#define __NEED_dev_t +#define __NEED_double_t +#define __NEED_float_t +#define __NEED_fsblkcnt_t +#define __NEED_fsfilcnt_t +#define __NEED_gid_t +#define __NEED_id_t +#define __NEED_ino_t +#define __NEED_int16_t +#define __NEED_int32_t +#define __NEED_int64_t +#define __NEED_int8_t +#define __NEED_intmax_t +#define __NEED_intptr_t +#define __NEED_key_t +#define __NEED_locale_t +#define __NEED_mbstate_t +#define __NEED_mode_t +#define __NEED_mtx_t +#define __NEED_nlink_t +#define __NEED_off_t +#define __NEED_pid_t +#define __NEED_pthread_attr_t +#define __NEED_pthread_barrier_t +#define __NEED_pthread_barrierattr_t +#define __NEED_pthread_cond_t +#define __NEED_pthread_condattr_t +#define __NEED_pthread_key_t +#define __NEED_pthread_mutex_t +#define __NEED_pthread_mutexattr_t +#define __NEED_pthread_once_t +#define __NEED_pthread_rwlock_t +#define __NEED_pthread_rwlockattr_t +#define __NEED_pthread_spinlock_t +#define __NEED_pthread_t +#define __NEED_register_t +#define __NEED_regoff_t +#define __NEED_sa_family_t +#define __NEED_sigset_t +#define __NEED_size_t +#define __NEED_socklen_t +#define __NEED_ssize_t +#define __NEED_struct_iovec +#define __NEED_struct_timespec +#define __NEED_struct_timeval +#define __NEED_suseconds_t +#define __NEED_time_t +#define __NEED_timer_t +#define __NEED_u_int64_t +#define __NEED_uid_t +#define __NEED_uint16_t +#define __NEED_uint32_t +#define __NEED_uint64_t +#define __NEED_uint8_t +#define __NEED_uintmax_t +#define __NEED_uintptr_t +#define __NEED_useconds_t +#define __NEED_va_list +#define __NEED_wchar_t +#define __NEED_wctype_t +#define __NEED_wint_t +#define __OBJC_BOOL_IS_BOOL 0 +#define __OPENCL_MEMORY_SCOPE_ALL_SVM_DEVICES 3 +#define __OPENCL_MEMORY_SCOPE_DEVICE 2 +#define __OPENCL_MEMORY_SCOPE_SUB_GROUP 4 +#define __OPENCL_MEMORY_SCOPE_WORK_GROUP 1 +#define __OPENCL_MEMORY_SCOPE_WORK_ITEM 0 +#define __ORDER_BIG_ENDIAN__ 4321 +#define __ORDER_LITTLE_ENDIAN__ 1234 +#define __ORDER_PDP_ENDIAN__ 3412 +#define __PDP_ENDIAN 3412 +#define __POINTER_WIDTH__ 32 +#define __PRAGMA_REDEFINE_EXTNAME 1 +#define __PTRDIFF_FMTd__ "ld" +#define __PTRDIFF_FMTi__ "li" +#define __PTRDIFF_TYPE__ long int +#define __PTRDIFF_WIDTH__ 32 +#define __REDIR(x,y) __typeof__(x) x __asm__(#y) +#define __RETCAST(x) +#define __RETCAST_2(x,y) +#define __RETCAST_3(x,y,z) +#define __RETCAST_CX(x) +#define __RETCAST_REAL(x) +#define __SID ('S' << 8) +#define __SIZEOF_DOUBLE__ 8 +#define __SIZEOF_FLOAT__ 4 +#define __SIZEOF_INT128__ 16 +#define __SIZEOF_INT__ 4 +#define __SIZEOF_LONG_DOUBLE__ 16 +#define __SIZEOF_LONG_LONG__ 8 +#define __SIZEOF_LONG__ 4 +#define __SIZEOF_POINTER__ 4 +#define __SIZEOF_PTRDIFF_T__ 4 +#define __SIZEOF_SHORT__ 2 +#define __SIZEOF_SIZE_T__ 4 +#define __SIZEOF_WCHAR_T__ 4 +#define __SIZEOF_WINT_T__ 4 +#define __SIZE_FMTX__ "lX" +#define __SIZE_FMTo__ "lo" +#define __SIZE_FMTu__ "lu" +#define __SIZE_FMTx__ "lx" +#define __SIZE_TYPE__ long unsigned int +#define __SIZE_WIDTH__ 32 +#define __STDARG_H +#define __STDC_HOSTED__ 1 +#define __STDC_IEC_559__ 1 +#define __STDC_ISO_10646__ 201206L +#define __STDC_UTF_16__ 1 +#define __STDC_UTF_32__ 1 +#define __STDC_VERSION__ 201710L +#define __STDC__ 1 +#define __STDDEF_H +#define __UAPI_DEF_IN6_ADDR 0 +#define __UAPI_DEF_IN6_ADDR_ALT 0 +#define __UAPI_DEF_IN6_PKTINFO 0 +#define __UAPI_DEF_IN_ADDR 0 +#define __UAPI_DEF_IN_CLASS 0 +#define __UAPI_DEF_IN_IPPROTO 0 +#define __UAPI_DEF_IN_PKTINFO 0 +#define __UAPI_DEF_IP6_MTUINFO 0 +#define __UAPI_DEF_IPHDR 0 +#define __UAPI_DEF_IPPROTO_V6 0 +#define __UAPI_DEF_IPV6_MREQ 0 +#define __UAPI_DEF_IPV6_OPTIONS 0 +#define __UAPI_DEF_IP_MREQ 0 +#define __UAPI_DEF_SOCKADDR_IN 0 +#define __UAPI_DEF_SOCKADDR_IN6 0 +#define __UINT16_C_SUFFIX__ +#define __UINT16_FMTX__ "hX" +#define __UINT16_FMTo__ "ho" +#define __UINT16_FMTu__ "hu" +#define __UINT16_FMTx__ "hx" +#define __UINT16_TYPE__ unsigned short +#define __UINT32_C_SUFFIX__ U +#define __UINT32_FMTX__ "X" +#define __UINT32_FMTo__ "o" +#define __UINT32_FMTu__ "u" +#define __UINT32_FMTx__ "x" +#define __UINT32_TYPE__ unsigned int +#define __UINT64_C_SUFFIX__ ULL +#define __UINT64_FMTX__ "llX" +#define __UINT64_FMTo__ "llo" +#define __UINT64_FMTu__ "llu" +#define __UINT64_FMTx__ "llx" +#define __UINT64_TYPE__ long long unsigned int +#define __UINT8_C_SUFFIX__ +#define __UINT8_FMTX__ "hhX" +#define __UINT8_FMTo__ "hho" +#define __UINT8_FMTu__ "hhu" +#define __UINT8_FMTx__ "hhx" +#define __UINT8_TYPE__ unsigned char +#define __UINTMAX_C_SUFFIX__ ULL +#define __UINTMAX_FMTX__ "llX" +#define __UINTMAX_FMTo__ "llo" +#define __UINTMAX_FMTu__ "llu" +#define __UINTMAX_FMTx__ "llx" +#define __UINTMAX_TYPE__ long long unsigned int +#define __UINTMAX_WIDTH__ 64 +#define __UINTPTR_FMTX__ "lX" +#define __UINTPTR_FMTo__ "lo" +#define __UINTPTR_FMTu__ "lu" +#define __UINTPTR_FMTx__ "lx" +#define __UINTPTR_TYPE__ long unsigned int +#define __UINTPTR_WIDTH__ 32 +#define __UINT_FAST16_FMTX__ "hX" +#define __UINT_FAST16_FMTo__ "ho" +#define __UINT_FAST16_FMTu__ "hu" +#define __UINT_FAST16_FMTx__ "hx" +#define __UINT_FAST16_TYPE__ unsigned short +#define __UINT_FAST32_FMTX__ "X" +#define __UINT_FAST32_FMTo__ "o" +#define __UINT_FAST32_FMTu__ "u" +#define __UINT_FAST32_FMTx__ "x" +#define __UINT_FAST32_TYPE__ unsigned int +#define __UINT_FAST64_FMTX__ "llX" +#define __UINT_FAST64_FMTo__ "llo" +#define __UINT_FAST64_FMTu__ "llu" +#define __UINT_FAST64_FMTx__ "llx" +#define __UINT_FAST64_TYPE__ long long unsigned int +#define __UINT_FAST8_FMTX__ "hhX" +#define __UINT_FAST8_FMTo__ "hho" +#define __UINT_FAST8_FMTu__ "hhu" +#define __UINT_FAST8_FMTx__ "hhx" +#define __UINT_FAST8_TYPE__ unsigned char +#define __UINT_LEAST16_FMTX__ "hX" +#define __UINT_LEAST16_FMTo__ "ho" +#define __UINT_LEAST16_FMTu__ "hu" +#define __UINT_LEAST16_FMTx__ "hx" +#define __UINT_LEAST16_TYPE__ unsigned short +#define __UINT_LEAST32_FMTX__ "X" +#define __UINT_LEAST32_FMTo__ "o" +#define __UINT_LEAST32_FMTu__ "u" +#define __UINT_LEAST32_FMTx__ "x" +#define __UINT_LEAST32_TYPE__ unsigned int +#define __UINT_LEAST64_FMTX__ "llX" +#define __UINT_LEAST64_FMTo__ "llo" +#define __UINT_LEAST64_FMTu__ "llu" +#define __UINT_LEAST64_FMTx__ "llx" +#define __UINT_LEAST64_TYPE__ long long unsigned int +#define __UINT_LEAST8_FMTX__ "hhX" +#define __UINT_LEAST8_FMTo__ "hho" +#define __UINT_LEAST8_FMTu__ "hhu" +#define __UINT_LEAST8_FMTx__ "hhx" +#define __UINT_LEAST8_TYPE__ unsigned char +#define __USER_LABEL_PREFIX__ +#define __USE_TIME_BITS64 1 +#define __WASI_VERSION_H +#define __WASI_WASIP2_H +#define __WASI_WASIP3_H +#define __WASI_WEBP2_H +#define __WCHAR_TYPE__ int +#define __WCHAR_WIDTH__ 32 +#define __WINT_TYPE__ int +#define __WINT_WIDTH__ 32 +#define __WORDSIZE 32 +#define __alignas_is_defined 1 +#define __alignof_is_defined 1 +#define __bitop(x,i,o) ((x)[(i)/8] o (1<<(i)%8)) +#define __bool_true_false_are_defined 1 +#define __fts_dev_t dev_t +#define __fts_ino_t ino_t +#define __fts_length_t unsigned int +#define __fts_level_t int +#define __fts_nlink_t nlink_t +#define __fts_number_t int64_t +#define __fts_stat_t struct stat +#define __inline inline +#define __restrict restrict +#define __tg_complex(fun,x) (__RETCAST_CX(x)( __FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : __LDBLCX((x)+I) ? fun ## l (x) : fun(x) )) +#define __tg_complex_retreal(fun,x) (__RETCAST_REAL(x)( __FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : __LDBLCX((x)+I) ? fun ## l (x) : fun(x) )) +#define __tg_real(fun,x) (__RETCAST(x)__tg_real_nocast(fun, x)) +#define __tg_real_2(fun,x,y) (__RETCAST_2(x, y)( __FLT(x) && __FLT(y) ? fun ## f (x, y) : __LDBL((x)+(y)) ? fun ## l (x, y) : fun(x, y) )) +#define __tg_real_2_1(fun,x,y) (__RETCAST(x)( __FLT(x) ? fun ## f (x, y) : __LDBL(x) ? fun ## l (x, y) : fun(x, y) )) +#define __tg_real_complex(fun,x) (__RETCAST(x)( __FLTCX(x) ? c ## fun ## f (x) : __DBLCX(x) ? c ## fun (x) : __LDBLCX(x) ? c ## fun ## l (x) : __FLT(x) ? fun ## f (x) : __LDBL(x) ? fun ## l (x) : fun(x) )) +#define __tg_real_complex_fabs(x) (__RETCAST_REAL(x)( __FLTCX(x) ? cabsf(x) : __DBLCX(x) ? cabs(x) : __LDBLCX(x) ? cabsl(x) : __FLT(x) ? fabsf(x) : __LDBL(x) ? fabsl(x) : fabs(x) )) +#define __tg_real_complex_pow(x,y) (__RETCAST_2(x, y)( __FLTCX((x)+(y)) && __IS_FP(x) && __IS_FP(y) ? cpowf(x, y) : __FLTCX((x)+(y)) ? cpow(x, y) : __DBLCX((x)+(y)) ? cpow(x, y) : __LDBLCX((x)+(y)) ? cpowl(x, y) : __FLT(x) && __FLT(y) ? powf(x, y) : __LDBL((x)+(y)) ? powl(x, y) : pow(x, y) )) +#define __tg_real_fma(x,y,z) (__RETCAST_3(x, y, z)( __FLT(x) && __FLT(y) && __FLT(z) ? fmaf(x, y, z) : __LDBL((x)+(y)+(z)) ? fmal(x, y, z) : fma(x, y, z) )) +#define __tg_real_nocast(fun,x) ( __FLT(x) ? fun ## f (x) : __LDBL(x) ? fun ## l (x) : fun(x) ) +#define __tg_real_remquo(x,y,z) (__RETCAST_2(x, y)( __FLT(x) && __FLT(y) ? remquof(x, y, z) : __LDBL((x)+(y)) ? remquol(x, y, z) : remquo(x, y, z) )) +#define __tm_gmtoff tm_gmtoff +#define __tm_zone tm_zone +#define __va_copy(d,s) __builtin_va_copy(d, s) +#define __wasi_api_h +#define __wasi_libc_busywait_h +#define __wasi_libc_environ_h +#define __wasi_libc_h +#define __wasi_wasip1_h +#define __wasilibc___errno_values_h +#define __wasilibc___fd_set_h +#define __wasilibc___function___isatty_h +#define __wasilibc___functions_malloc_h +#define __wasilibc___functions_memcpy_h +#define __wasilibc___header_dirent_h +#define __wasilibc___header_fcntl_h +#define __wasilibc___header_netinet_in_h +#define __wasilibc___header_poll_h +#define __wasilibc___header_stdlib_h +#define __wasilibc___header_string_h +#define __wasilibc___header_sys_ioctl_h +#define __wasilibc___header_sys_resource_h +#define __wasilibc___header_sys_socket_h +#define __wasilibc___header_sys_stat_h +#define __wasilibc___header_time_h +#define __wasilibc___header_unistd_h +#define __wasilibc___include_inttypes_h +#define __wasilibc___macro_FD_SETSIZE_h +#define __wasilibc___macro_PAGESIZE_h +#define __wasilibc___mode_t_h +#define __wasilibc___seek_h +#define __wasilibc___struct_dirent_h +#define __wasilibc___struct_in6_addr_h +#define __wasilibc___struct_in_addr_h +#define __wasilibc___struct_iovec_h +#define __wasilibc___struct_msghdr_h +#define __wasilibc___struct_pollfd_h +#define __wasilibc___struct_rusage_h +#define __wasilibc___struct_sockaddr_h +#define __wasilibc___struct_sockaddr_in6_h +#define __wasilibc___struct_sockaddr_in_h +#define __wasilibc___struct_sockaddr_storage_h +#define __wasilibc___struct_sockaddr_un_h +#define __wasilibc___struct_stat_h +#define __wasilibc___struct_timespec_h +#define __wasilibc___struct_timeval_h +#define __wasilibc___struct_tm_h +#define __wasilibc___struct_tms_h +#define __wasilibc___typedef_DIR_h +#define __wasilibc___typedef_blkcnt_t_h +#define __wasilibc___typedef_blksize_t_h +#define __wasilibc___typedef_clock_t_h +#define __wasilibc___typedef_clockid_t_h +#define __wasilibc___typedef_dev_t_h +#define __wasilibc___typedef_fd_set_h +#define __wasilibc___typedef_gid_t_h +#define __wasilibc___typedef_in_addr_t_h +#define __wasilibc___typedef_in_port_t_h +#define __wasilibc___typedef_ino_t_h +#define __wasilibc___typedef_mode_t_h +#define __wasilibc___typedef_nfds_t_h +#define __wasilibc___typedef_nlink_t_h +#define __wasilibc___typedef_off_t_h +#define __wasilibc___typedef_sa_family_t_h +#define __wasilibc___typedef_sigset_t_h +#define __wasilibc___typedef_socklen_t_h +#define __wasilibc___typedef_ssize_t_h +#define __wasilibc___typedef_suseconds_t_h +#define __wasilibc___typedef_time_t_h +#define __wasilibc___typedef_uid_t_h +#define __wasm 1 +#define __wasm32 1 +#define __wasm32__ 1 +#define __wasm__ 1 +#define __webp2__ +#define _tolower(a) ((a)|0x20) +#define _toupper(a) ((a)&0x5f) +#define acos(x) __tg_real_complex(acos, (x)) +#define acosh(x) __tg_real_complex(acosh, (x)) +#define alignas _Alignas +#define alignof _Alignof +#define alloca __builtin_alloca +#define alphasort64 alphasort +#define and && +#define and_eq &= +#define asin(x) __tg_real_complex(asin, (x)) +#define asinh(x) __tg_real_complex(asinh, (x)) +#define atan(x) __tg_real_complex(atan, (x)) +#define atan2(x,y) __tg_real_2(atan2, (x), (y)) +#define atanh(x) __tg_real_complex(atanh, (x)) +#define be16toh(x) __bswap16(x) +#define be32toh(x) __bswap32(x) +#define be64toh(x) __bswap64(x) +#define betoh16(x) __bswap16(x) +#define betoh32(x) __bswap32(x) +#define betoh64(x) __bswap64(x) +#define bitand & +#define bitor | +#define blkcnt64_t blkcnt_t +#define bool _Bool +#define bswap_16(x) __bswap_16(x) +#define bswap_32(x) __bswap_32(x) +#define bswap_64(x) __bswap_64(x) +#define carg(x) __tg_complex_retreal(carg, (x)) +#define cbrt(x) __tg_real(cbrt, (x)) +#define ceil(x) __tg_real(ceil, (x)) +#define cimag(x) __tg_complex_retreal(cimag, (x)) +#define cimagf(x) (__builtin_cimagf(x)) +#define cimagl(x) (__builtin_cimagl(x)) +#define clrbit(x,i) __bitop(x,i,&=~) +#define compl ~ +#define complex _Complex +#define conj(x) __tg_complex(conj, (x)) +#define copysign(x,y) __tg_real_2(copysign, (x), (y)) +#define cos(x) __tg_real_complex(cos, (x)) +#define cosh(x) __tg_real_complex(cosh, (x)) +#define cproj(x) __tg_complex(cproj, (x)) +#define creal(x) __tg_complex_retreal(creal, (x)) +#define crealf(x) (__builtin_crealf(x)) +#define creall(x) (__builtin_creall(x)) +#define creat64 creat +#define d_fileno d_ino +#define direct dirent +#define dirent64 dirent +#define erf(x) __tg_real(erf, (x)) +#define erfc(x) __tg_real(erfc, (x)) +#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)) +#define fabs(x) __tg_real_complex_fabs(x) +#define false 0 +#define fdim(x,y) __tg_real_2(fdim, (x), (y)) +#define fgetpos64 fgetpos +#define floor(x) __tg_real(floor, (x)) +#define fma(x,y,z) __tg_real_fma((x), (y), (z)) +#define fmax(x,y) __tg_real_2(fmax, (x), (y)) +#define fmin(x,y) __tg_real_2(fmin, (x), (y)) +#define fmod(x,y) __tg_real_2(fmod, (x), (y)) +#define fopen64 fopen +#define fpclassify(x) (__builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)) +#define fpos64_t fpos_t +#define freopen64 freopen +#define frexp(x,y) __tg_real_2_1(frexp, (x), (y)) +#define fsblkcnt64_t fsblkcnt_t +#define fseeko64 fseeko +#define fsetpos64 fsetpos +#define fsfilcnt64_t fsfilcnt_t +#define fstat64 fstat +#define fstatat64 fstatat +#define fstatvfs64 fstatvfs +#define ftello64 ftello +#define ftruncate64 ftruncate +#define getdents64 getdents +#define glob64 glob +#define glob64_t glob_t +#define globfree64 globfree +#define howmany(n,d) (((n)+((d)-1))/(d)) +#define htobe16(x) __bswap16(x) +#define htobe32(x) __bswap32(x) +#define htobe64(x) __bswap64(x) +#define htole16(x) (uint16_t)(x) +#define htole32(x) (uint32_t)(x) +#define htole64(x) (uint64_t)(x) +#define hypot(x,y) __tg_real_2(hypot, (x), (y)) +#define icmp6_data16 icmp6_dataun.icmp6_un_data16 +#define icmp6_data32 icmp6_dataun.icmp6_un_data32 +#define icmp6_data8 icmp6_dataun.icmp6_un_data8 +#define icmp6_id icmp6_data16[0] +#define icmp6_maxdelay icmp6_data16[0] +#define icmp6_mtu icmp6_data32[0] +#define icmp6_pptr icmp6_data32[0] +#define icmp6_seq icmp6_data16[1] +#define icmp_data icmp_dun.id_data +#define icmp_gwaddr icmp_hun.ih_gwaddr +#define icmp_id icmp_hun.ih_idseq.icd_id +#define icmp_ip icmp_dun.id_ip.idi_ip +#define icmp_lifetime icmp_hun.ih_rtradv.irt_lifetime +#define icmp_mask icmp_dun.id_mask +#define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu +#define icmp_num_addrs icmp_hun.ih_rtradv.irt_num_addrs +#define icmp_otime icmp_dun.id_ts.its_otime +#define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void +#define icmp_pptr icmp_hun.ih_pptr +#define icmp_radv icmp_dun.id_radv +#define icmp_rtime icmp_dun.id_ts.its_rtime +#define icmp_seq icmp_hun.ih_idseq.icd_seq +#define icmp_ttime icmp_dun.id_ts.its_ttime +#define icmp_void icmp_hun.ih_void +#define icmp_wpa icmp_hun.ih_rtradv.irt_wpa +#define ifa_broadaddr ifa_ifu.ifu_broadaddr +#define ifa_dstaddr ifa_ifu.ifu_dstaddr +#define ilogb(x) __tg_real_nocast(ilogb, (x)) +#define ino64_t ino_t +#define ip6_flow ip6_ctlun.ip6_un1.ip6_un1_flow +#define ip6_hlim ip6_ctlun.ip6_un1.ip6_un1_hlim +#define ip6_hops ip6_ctlun.ip6_un1.ip6_un1_hlim +#define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_nxt +#define ip6_plen ip6_ctlun.ip6_un1.ip6_un1_plen +#define ip6_vfc ip6_ctlun.ip6_un2_vfc +#define isalpha(a) (0 ? isalpha(a) : (((unsigned)(a)|32)-'a') < 26) +#define isascii(a) (0 ? isascii(a) : (unsigned)(a) < 128) +#define isclr(x,i) !isset(x,i) +#define isdigit(a) (0 ? isdigit(a) : ((unsigned)(a)-'0') < 10) +#define isfinite(x) (__builtin_isfinite(x)) +#define isgraph(a) (0 ? isgraph(a) : ((unsigned)(a)-0x21) < 0x5e) +#define isgreater(x,y) (__builtin_isgreater(x, y)) +#define isgreaterequal(x,y) (__builtin_isgreaterequal(x, y)) +#define isinf(x) (__builtin_isinf(x)) +#define isless(x,y) (__builtin_isless(x, y)) +#define islessequal(x,y) (__builtin_islessequal(x, y)) +#define islessgreater(x,y) (__builtin_islessgreater(x, y)) +#define islower(a) (0 ? islower(a) : ((unsigned)(a)-'a') < 26) +#define isnan(x) (__builtin_isnan(x)) +#define isnormal(x) (__builtin_isnormal(x)) +#define isprint(a) (0 ? isprint(a) : ((unsigned)(a)-0x20) < 0x5f) +#define isset(x,i) __bitop(x,i,&) +#define isspace(a) __isspace(a) +#define isunordered(x,y) (__builtin_isunordered(x, y)) +#define isupper(a) (0 ? isupper(a) : ((unsigned)(a)-'A') < 26) +#define iswdigit(a) (0 ? iswdigit(a) : ((unsigned)(a)-'0') < 10) +#define ldexp(x,y) __tg_real_2_1(ldexp, (x), (y)) +#define le16toh(x) (uint16_t)(x) +#define le32toh(x) (uint32_t)(x) +#define le64toh(x) (uint64_t)(x) +#define letoh16(x) (uint16_t)(x) +#define letoh32(x) (uint32_t)(x) +#define letoh64(x) (uint64_t)(x) +#define lgamma(x) __tg_real(lgamma, (x)) +#define llrint(x) __tg_real_nocast(llrint, (x)) +#define llround(x) __tg_real_nocast(llround, (x)) +#define loff_t off_t +#define log(x) __tg_real_complex(log, (x)) +#define log10(x) __tg_real(log10, (x)) +#define log1p(x) __tg_real(log1p, (x)) +#define log2(x) __tg_real(log2, (x)) +#define logb(x) __tg_real(logb, (x)) +#define lrint(x) __tg_real_nocast(lrint, (x)) +#define lround(x) __tg_real_nocast(lround, (x)) +#define lseek(fd,offset,whence) ({ off_t __f = (fd); off_t __o = (offset); off_t __w = (whence); __builtin_constant_p((offset)) && __builtin_constant_p((whence)) && __o == 0 && __w == SEEK_CUR ? __wasilibc_tell(__f) : lseek(__f, __o, __w); }) +#define lseek64 lseek +#define lstat64 lstat +#define math_errhandling 2 +#define mld_cksum mld_icmp6_hdr.icmp6_cksum +#define mld_code mld_icmp6_hdr.icmp6_code +#define mld_maxdelay mld_icmp6_hdr.icmp6_data16[0] +#define mld_reserved mld_icmp6_hdr.icmp6_data16[1] +#define mld_type mld_icmp6_hdr.icmp6_type +#define nd_na_cksum nd_na_hdr.icmp6_cksum +#define nd_na_code nd_na_hdr.icmp6_code +#define nd_na_flags_reserved nd_na_hdr.icmp6_data32[0] +#define nd_na_type nd_na_hdr.icmp6_type +#define nd_ns_cksum nd_ns_hdr.icmp6_cksum +#define nd_ns_code nd_ns_hdr.icmp6_code +#define nd_ns_reserved nd_ns_hdr.icmp6_data32[0] +#define nd_ns_type nd_ns_hdr.icmp6_type +#define nd_ra_cksum nd_ra_hdr.icmp6_cksum +#define nd_ra_code nd_ra_hdr.icmp6_code +#define nd_ra_curhoplimit nd_ra_hdr.icmp6_data8[0] +#define nd_ra_flags_reserved nd_ra_hdr.icmp6_data8[1] +#define nd_ra_router_lifetime nd_ra_hdr.icmp6_data16[1] +#define nd_ra_type nd_ra_hdr.icmp6_type +#define nd_rd_cksum nd_rd_hdr.icmp6_cksum +#define nd_rd_code nd_rd_hdr.icmp6_code +#define nd_rd_reserved nd_rd_hdr.icmp6_data32[0] +#define nd_rd_type nd_rd_hdr.icmp6_type +#define nd_rs_cksum nd_rs_hdr.icmp6_cksum +#define nd_rs_code nd_rs_hdr.icmp6_code +#define nd_rs_reserved nd_rs_hdr.icmp6_data32[0] +#define nd_rs_type nd_rs_hdr.icmp6_type +#define nearbyint(x) __tg_real(nearbyint, (x)) +#define nextafter(x,y) __tg_real_2(nextafter, (x), (y)) +#define nexttoward(x,y) __tg_real_2(nexttoward, (x), (y)) +#define nftw64 nftw +#define no_argument 0 +#define noreturn _Noreturn +#define not ! +#define not_eq != +#define ns_msg_base(handle) ((handle)._msg + 0) +#define ns_msg_count(handle,section) ((handle)._counts[section] + 0) +#define ns_msg_end(handle) ((handle)._eom + 0) +#define ns_msg_getflag(handle,flag) (((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift) +#define ns_msg_id(handle) ((handle)._id + 0) +#define ns_msg_size(handle) ((handle)._eom - (handle)._msg) +#define ns_rr_class(rr) ((ns_class)((rr).rr_class + 0)) +#define ns_rr_name(rr) (((rr).name[0] != '\0') ? (rr).name : ".") +#define ns_rr_rdata(rr) ((rr).rdata + 0) +#define ns_rr_rdlen(rr) ((rr).rdlength + 0) +#define ns_rr_ttl(rr) ((rr).ttl + 0) +#define ns_rr_type(rr) ((ns_type)((rr).type + 0)) +#define ns_t_mrr_p(t) ((t) == ns_t_tsig || (t) == ns_t_opt) +#define ns_t_qt_p(t) (ns_t_xfr_p(t) || (t) == ns_t_any || (t) == ns_t_mailb || (t) == ns_t_maila) +#define ns_t_rr_p(t) (!ns_t_qt_p(t) && !ns_t_mrr_p(t)) +#define ns_t_udp_p(t) ((t) != ns_t_axfr && (t) != ns_t_zxfr) +#define ns_t_xfr_p(t) ((t) == ns_t_axfr || (t) == ns_t_ixfr || (t) == ns_t_zxfr) +#define off64_t off_t +#define offsetof(t,d) __builtin_offsetof(t, d) +#define open64 open +#define openat64 openat +#define optional_argument 2 +#define or || +#define or_eq |= +#define posix_fadvise64 posix_fadvise +#define posix_fallocate64 posix_fallocate +#define pow(x,y) __tg_real_complex_pow((x), (y)) +#define powerof2(n) !(((n)-1) & (n)) +#define pread64 pread +#define preadv64 preadv +#define pthread_cleanup_pop(r) _pthread_cleanup_pop(&__cb, (r)); } while(0) +#define pthread_cleanup_push(f,x) do { struct __ptcb __cb; _pthread_cleanup_push(&__cb, f, x); +#define pthread_equal(x,y) ((x)==(y)) +#define pwrite64 pwrite +#define pwritev64 pwritev +#define readdir64 readdir +#define remainder(x,y) __tg_real_2(remainder, (x), (y)) +#define remquo(x,y,z) __tg_real_remquo((x), (y), (z)) +#define required_argument 1 +#define rint(x) __tg_real(rint, (x)) +#define round(x) __tg_real(round, (x)) +#define roundup(n,d) (howmany(n,d)*(d)) +#define rr_cksum rr_hdr.icmp6_cksum +#define rr_code rr_hdr.icmp6_code +#define rr_seqnum rr_hdr.icmp6_data32[0] +#define rr_type rr_hdr.icmp6_type +#define scalbln(x,y) __tg_real_2_1(scalbln, (x), (y)) +#define scalbn(x,y) __tg_real_2_1(scalbn, (x), (y)) +#define scandir64 scandir +#define setbit(x,i) __bitop(x,i,|=) +#define signbit(x) (__builtin_signbit(x)) +#define sin(x) __tg_real_complex(sin, (x)) +#define sinh(x) __tg_real_complex(sinh, (x)) +#define sqrt(x) __tg_real_complex(sqrt, (x)) +#define st_atime st_atim.tv_sec +#define st_ctime st_ctim.tv_sec +#define st_mtime st_mtim.tv_sec +#define stat64 stat +#define static_assert _Static_assert +#define statvfs64 statvfs +#define stderr (stderr) +#define stdin (stdin) +#define stdout (stdout) +#define strdupa(x) strcpy(alloca(strlen(x)+1),x) +#define tan(x) __tg_real_complex(tan, (x)) +#define tanh(x) __tg_real_complex(tanh, (x)) +#define telcmds ((char [][6]){ "EOF", "SUSP", "ABORT", "EOR", "SE", "NOP", "DMARK", "BRK", "IP", "AO", "AYT", "EC", "EL", "GA", "SB", "WILL", "WONT", "DO", "DONT", "IAC", 0 }) +#define tgamma(x) __tg_real(tgamma, (x)) +#define th_block th_u.tu_block +#define th_code th_u.tu_code +#define th_msg th_data +#define th_stuff th_u.tu_stuff +#define thrd_equal(A,B) ((A) == (B)) +#define thread_local _Thread_local +#define timeradd(s,t,a) (void) ( (a)->tv_sec = (s)->tv_sec + (t)->tv_sec, ((a)->tv_usec = (s)->tv_usec + (t)->tv_usec) >= 1000000 && ((a)->tv_usec -= 1000000, (a)->tv_sec++) ) +#define timerclear(t) ((t)->tv_sec = (t)->tv_usec = 0) +#define timercmp(s,t,op) ((s)->tv_sec == (t)->tv_sec ? (s)->tv_usec op (t)->tv_usec : (s)->tv_sec op (t)->tv_sec) +#define timerisset(t) ((t)->tv_sec || (t)->tv_usec) +#define timersub(s,t,a) (void) ( (a)->tv_sec = (s)->tv_sec - (t)->tv_sec, ((a)->tv_usec = (s)->tv_usec - (t)->tv_usec) < 0 && ((a)->tv_usec += 1000000, (a)->tv_sec--) ) +#define true 1 +#define trunc(x) __tg_real(trunc, (x)) +#define uh_dport dest +#define uh_sport source +#define uh_sum check +#define uh_ulen len +#define va_arg(ap,type) __builtin_va_arg(ap, type) +#define va_copy(dest,src) __builtin_va_copy(dest, src) +#define va_end(ap) __builtin_va_end(ap) +#define va_start(ap,param) __builtin_va_start(ap, param) +#define versionsort64 versionsort +#define xEOF 236 +#define xor ^ +#define xor_eq ^= diff --git a/expected/wasm32-webp2/undefined-symbols.txt b/expected/wasm32-webp2/undefined-symbols.txt new file mode 100644 index 000000000..6588db006 --- /dev/null +++ b/expected/wasm32-webp2/undefined-symbols.txt @@ -0,0 +1,39 @@ +__addtf3 +__data_end +__divtf3 +__eqtf2 +__extenddftf2 +__extendsftf2 +__fixtfdi +__fixtfsi +__fixunstfsi +__floatsitf +__floatunsitf +__getf2 +__global_base +__gttf2 +__heap_base +__heap_end +__letf2 +__lttf2 +__netf2 +__subtf3 +__trunctfdf2 +__trunctfsf2 +__unordtf2 +__wasm_call_ctors +__wasm_import_webp2_constructor_error +__wasm_import_webp2_constructor_uint_8_array +__wasm_import_webp2_crypto_impl_drop +__wasm_import_webp2_date_drop +__wasm_import_webp2_error_drop +__wasm_import_webp2_get_crypto +__wasm_import_webp2_get_performance +__wasm_import_webp2_method_crypto_impl_get_random_values +__wasm_import_webp2_method_get_performance_impl_time_origin +__wasm_import_webp2_method_performance_impl_now +__wasm_import_webp2_method_uint_8_array_subarray +__wasm_import_webp2_performance_impl_drop +__wasm_import_webp2_report_error +__wasm_import_webp2_static_date_now +__wasm_import_webp2_uint_8_array_drop From 92117be40aeb00679041196002f7eedba7801c2e Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 6 Oct 2026 10:36:36 -0500 Subject: [PATCH 22/96] Fix formatting, run more webp2 stuff in CI --- .github/workflows/main.yml | 10 + cmake/clang-format.cmake | 2 +- .../sources/webp2_file_descriptors.c | 78 +++---- libc-bottom-half/sources/webp2_filesystem.c | 199 +++++++----------- libc-bottom-half/sources/webp2_socket.c | 45 ++-- 5 files changed, 127 insertions(+), 207 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6ad78a34f..c7a20846b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -148,6 +148,14 @@ jobs: test: true args: -DCMAKE_BUILD_TYPE=Debug -DTARGET_TRIPLE=wasm32-wasip3 -DENABLE_COOP_THREADS=ON + - name: Test wasm32-webp2 + os: ubuntu-24.04 + clang_version: 22 + upload: wasm32-webp2 + # TODO(webp2): Enable tests once there is a way to run them. + test: false + args: -DTARGET_TRIPLE=wasm32-webp2 + - name: Test emmalloc os: ubuntu-24.04 clang_version: 20 @@ -259,6 +267,8 @@ jobs: - run: ninja -C build format-check - run: cmake -S . -B build -DTARGET_TRIPLE=wasm32-wasip3 - run: ninja -C build format-check + - run: cmake -S . -B build -DTARGET_TRIPLE=wasm32-webp2 + - run: ninja -C build format-check rustfmt: name: Rustfmt diff --git a/cmake/clang-format.cmake b/cmake/clang-format.cmake index 8271a4f66..b5b66aa56 100644 --- a/cmake/clang-format.cmake +++ b/cmake/clang-format.cmake @@ -21,7 +21,7 @@ set(formatted_sources) function(clang_format_file file) if (file MATCHES "__generated" OR - file MATCHES "wasip.\.c$" OR # Skip auto-generated files + file MATCHES "(wasip.|webp2)\.c$" OR # Skip auto-generated files file MATCHES "\.(s|S)$") # Skip assembly files return() endif() diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index 5e1b54e17..475cf301e 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -1,109 +1,81 @@ // All things file-descriptory are stubbed out on the web, since there is no // comparable model of files and streams. +#include "sys/uio.h" +#include "wasi/report-error.h" #include #include #include #include #include -#include "sys/uio.h" -#include "wasi/report-error.h" // unistd.h defines an `lseek` macro that would mangle the definition below. #undef lseek -int __dup3(int fd, int newfd, int flags) { - WEBP2_UNSUPPORTED("dup3"); -} +int __dup3(int fd, int newfd, int flags) { WEBP2_UNSUPPORTED("dup3"); } -int __isatty(int fd) { - WEBP2_UNSUPPORTED("isatty"); -} +int __isatty(int fd) { WEBP2_UNSUPPORTED("isatty"); } off_t __lseek(int fildes, off_t offset, int whence) { - WEBP2_UNSUPPORTED("lseek"); + WEBP2_UNSUPPORTED("lseek"); } -off_t __wasilibc_tell(int fd) { - WEBP2_UNSUPPORTED("__wasilibc_tell"); -} +off_t __wasilibc_tell(int fd) { WEBP2_UNSUPPORTED("__wasilibc_tell"); } -int close(int fd) { - WEBP2_UNSUPPORTED("close"); -} +int close(int fd) { WEBP2_UNSUPPORTED("close"); } -int dup(int fd) { - WEBP2_UNSUPPORTED("dup"); -} +int dup(int fd) { WEBP2_UNSUPPORTED("dup"); } -int dup2(int fd, int newfd) { - WEBP2_UNSUPPORTED("dup2"); -} +int dup2(int fd, int newfd) { WEBP2_UNSUPPORTED("dup2"); } -int dup3(int fd, int newfd, int flags) { - WEBP2_UNSUPPORTED("dup3"); -} +int dup3(int fd, int newfd, int flags) { WEBP2_UNSUPPORTED("dup3"); } -int fcntl(int fildes, int cmd, ...) { - WEBP2_UNSUPPORTED("fcntl"); -} +int fcntl(int fildes, int cmd, ...) { WEBP2_UNSUPPORTED("fcntl"); } -int fstat(int fildes, struct stat *buf) { - WEBP2_UNSUPPORTED("fstat"); -} +int fstat(int fildes, struct stat *buf) { WEBP2_UNSUPPORTED("fstat"); } -int ioctl(int fildes, int request, ...) { - WEBP2_UNSUPPORTED("ioctl"); -} +int ioctl(int fildes, int request, ...) { WEBP2_UNSUPPORTED("ioctl"); } -int isatty(int fd) { - WEBP2_UNSUPPORTED("isatty"); -} +int isatty(int fd) { WEBP2_UNSUPPORTED("isatty"); } off_t lseek(int fildes, off_t offset, int whence) { - WEBP2_UNSUPPORTED("lseek"); + WEBP2_UNSUPPORTED("lseek"); } -int pipe(int fd[2]) { - WEBP2_UNSUPPORTED("pipe"); -} +int pipe(int fd[2]) { WEBP2_UNSUPPORTED("pipe"); } -int pipe2(int fd[2], int flags) { - WEBP2_UNSUPPORTED("pipe2"); -} +int pipe2(int fd[2], int flags) { WEBP2_UNSUPPORTED("pipe2"); } int poll(struct pollfd *fds, nfds_t nfds, int timeout) { - WEBP2_UNSUPPORTED("poll"); + WEBP2_UNSUPPORTED("poll"); } int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, const sigset_t *sigmask) { - WEBP2_UNSUPPORTED("ppoll"); + WEBP2_UNSUPPORTED("ppoll"); } int pselect(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, fd_set *restrict errorfds, const struct timespec *restrict timeout, const sigset_t *restrict sigmask) { - WEBP2_UNSUPPORTED("pselect"); + WEBP2_UNSUPPORTED("pselect"); } -ssize_t read(int fildes, void *buf, size_t nbyte) { - WEBP2_UNSUPPORTED("read"); -} +ssize_t read(int fildes, void *buf, size_t nbyte) { WEBP2_UNSUPPORTED("read"); } ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { - WEBP2_UNSUPPORTED("readv"); + WEBP2_UNSUPPORTED("readv"); } int select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, fd_set *restrict errorfds, struct timeval *restrict timeout) { - WEBP2_UNSUPPORTED("select"); + WEBP2_UNSUPPORTED("select"); } ssize_t write(int fildes, const void *buf, size_t nbyte) { - WEBP2_UNSUPPORTED("write"); + WEBP2_UNSUPPORTED("write"); } ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { - WEBP2_UNSUPPORTED("writev"); + WEBP2_UNSUPPORTED("writev"); } diff --git a/libc-bottom-half/sources/webp2_filesystem.c b/libc-bottom-half/sources/webp2_filesystem.c index 49c35cd77..c3c68b021 100644 --- a/libc-bottom-half/sources/webp2_filesystem.c +++ b/libc-bottom-half/sources/webp2_filesystem.c @@ -1,6 +1,8 @@ // There is no filesystem. #include "__struct_stat.h" +#include "dirent.h" +#include "wasi/report-error.h" #include <__typedef_DIR.h> #include #include @@ -10,288 +12,231 @@ #include #include #include -#include "dirent.h" -#include "wasi/report-error.h" int __wasilibc_access(const char *pathname, int mode, int flags) { - WEBP2_UNSUPPORTED("__wasilibc_access"); + WEBP2_UNSUPPORTED("__wasilibc_access"); } int __wasilibc_link(const char *oldpath, const char *newpath, int flags) { - WEBP2_UNSUPPORTED("__wasilibc_link"); + WEBP2_UNSUPPORTED("__wasilibc_link"); } int __wasilibc_link_newat(const char *oldpath, int newdirfd, const char *newpath, int flags) { - WEBP2_UNSUPPORTED("__wasilibc_link_newat"); + WEBP2_UNSUPPORTED("__wasilibc_link_newat"); } int __wasilibc_link_oldat(int olddirfd, const char *oldpath, const char *newpath, int flags) { - WEBP2_UNSUPPORTED("__wasilibc_link_oldat"); + WEBP2_UNSUPPORTED("__wasilibc_link_oldat"); } int __wasilibc_open_nomode(const char *path, int oflag) { - WEBP2_UNSUPPORTED("__wasilibc_open_nomode"); + WEBP2_UNSUPPORTED("__wasilibc_open_nomode"); } void __wasilibc_populate_preopens(void) { - WEBP2_UNSUPPORTED("__wasilibc_populate_preopens"); + WEBP2_UNSUPPORTED("__wasilibc_populate_preopens"); } int __wasilibc_rename_newat(const char *oldpath, int newdirfd, const char *newpath) { - WEBP2_UNSUPPORTED("__wasilibc_rename_newat"); + WEBP2_UNSUPPORTED("__wasilibc_rename_newat"); } int __wasilibc_rename_oldat(int olddirfd, const char *oldpath, const char *newpath) { - WEBP2_UNSUPPORTED("__wasilibc_rename_oldat"); + WEBP2_UNSUPPORTED("__wasilibc_rename_oldat"); } void __wasilibc_reset_preopens(void) { - WEBP2_UNSUPPORTED("__wasilibc_reset_preopens"); + WEBP2_UNSUPPORTED("__wasilibc_reset_preopens"); } int __wasilibc_rmdirat(int fd, const char *path) { - WEBP2_UNSUPPORTED("__wasilibc_rmdirat"); + WEBP2_UNSUPPORTED("__wasilibc_rmdirat"); } -int __wasilibc_stat(const char *restrict pathname, struct stat *restrict statbuf, - int flags) { - WEBP2_UNSUPPORTED("__wasilibc_stat"); +int __wasilibc_stat(const char *restrict pathname, + struct stat *restrict statbuf, int flags) { + WEBP2_UNSUPPORTED("__wasilibc_stat"); } int __wasilibc_unlinkat(int fd, const char *path) { - WEBP2_UNSUPPORTED("__wasilibc_unlinkat"); + WEBP2_UNSUPPORTED("__wasilibc_unlinkat"); } int __wasilibc_utimens(const char *pathname, const struct timespec times[2], int flags) { - WEBP2_UNSUPPORTED("__wasilibc_utimens"); + WEBP2_UNSUPPORTED("__wasilibc_utimens"); } -int access(const char *path, int amode) { - WEBP2_UNSUPPORTED("access"); -} +int access(const char *path, int amode) { WEBP2_UNSUPPORTED("access"); } -int chdir(const char *path) { - WEBP2_UNSUPPORTED("chdir"); -} +int chdir(const char *path) { WEBP2_UNSUPPORTED("chdir"); } -int chmod(const char *path, mode_t mode) { - WEBP2_UNSUPPORTED("chmod"); -} +int chmod(const char *path, mode_t mode) { WEBP2_UNSUPPORTED("chmod"); } -int closedir(DIR *dirp) { - WEBP2_UNSUPPORTED("closedir"); -} +int closedir(DIR *dirp) { WEBP2_UNSUPPORTED("closedir"); } -int dirfd(DIR *dirp) { - WEBP2_UNSUPPORTED("dirfd"); -} +int dirfd(DIR *dirp) { WEBP2_UNSUPPORTED("dirfd"); } int faccessat(int fd, const char *path, int amode, int flag) { - WEBP2_UNSUPPORTED("faccessat"); + WEBP2_UNSUPPORTED("faccessat"); } -int fchmod(int fildes, mode_t mode) { - WEBP2_UNSUPPORTED("fchmod"); -} +int fchmod(int fildes, mode_t mode) { WEBP2_UNSUPPORTED("fchmod"); } int fchmodat(int fd, const char *path, mode_t mode, int flag) { - WEBP2_UNSUPPORTED("fchmodat"); + WEBP2_UNSUPPORTED("fchmodat"); } -int fdatasync(int fildes) { - WEBP2_UNSUPPORTED("fdatasync"); -} +int fdatasync(int fildes) { WEBP2_UNSUPPORTED("fdatasync"); } -int fdclosedir(DIR *dirp) { - WEBP2_UNSUPPORTED("fdclosedir"); -} +int fdclosedir(DIR *dirp) { WEBP2_UNSUPPORTED("fdclosedir"); } -DIR *fdopendir(int fd) { - WEBP2_UNSUPPORTED("fdopendir"); -} +DIR *fdopendir(int fd) { WEBP2_UNSUPPORTED("fdopendir"); } int fstatat(int fd, const char *restrict path, struct stat *restrict buf, int flag) { - WEBP2_UNSUPPORTED("fstatat"); + WEBP2_UNSUPPORTED("fstatat"); } -int fstatvfs(int fildes, struct statvfs *buf) { - WEBP2_UNSUPPORTED("fstatvfs"); -} +int fstatvfs(int fildes, struct statvfs *buf) { WEBP2_UNSUPPORTED("fstatvfs"); } -int fsync(int fildes) { - WEBP2_UNSUPPORTED("fsync"); -} +int fsync(int fildes) { WEBP2_UNSUPPORTED("fsync"); } -int ftruncate(int fildes, off_t length) { - WEBP2_UNSUPPORTED("ftruncate"); -} +int ftruncate(int fildes, off_t length) { WEBP2_UNSUPPORTED("ftruncate"); } int futimens(int fd, const struct timespec times[2]) { - WEBP2_UNSUPPORTED("futimens"); + WEBP2_UNSUPPORTED("futimens"); } -char *getcwd(char *buf, size_t size) { - WEBP2_UNSUPPORTED("getcwd"); -} +char *getcwd(char *buf, size_t size) { WEBP2_UNSUPPORTED("getcwd"); } -int link(const char *path1, const char *path2) { - WEBP2_UNSUPPORTED("link"); -} +int link(const char *path1, const char *path2) { WEBP2_UNSUPPORTED("link"); } int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag) { - WEBP2_UNSUPPORTED("linkat"); + WEBP2_UNSUPPORTED("linkat"); } int lstat(const char *restrict path, struct stat *restrict buf) { - WEBP2_UNSUPPORTED("lstat"); + WEBP2_UNSUPPORTED("lstat"); } -int mkdir(const char *path, mode_t mode) { - WEBP2_UNSUPPORTED("mkdir"); -} +int mkdir(const char *path, mode_t mode) { WEBP2_UNSUPPORTED("mkdir"); } int mkdirat(int fd, const char *path, mode_t mode) { - WEBP2_UNSUPPORTED("mkdirat"); + WEBP2_UNSUPPORTED("mkdirat"); } -int open(const char *path, int oflag, ...) { - WEBP2_UNSUPPORTED("open"); -} +int open(const char *path, int oflag, ...) { WEBP2_UNSUPPORTED("open"); } int openat(int fd, const char *path, int oflag, ...) { - WEBP2_UNSUPPORTED("openat"); + WEBP2_UNSUPPORTED("openat"); } -DIR *opendir(const char *dirname) { - WEBP2_UNSUPPORTED("opendir"); -} +DIR *opendir(const char *dirname) { WEBP2_UNSUPPORTED("opendir"); } -DIR *opendirat(int dir, const char *dirname) { - WEBP2_UNSUPPORTED("opendirat"); -} +DIR *opendirat(int dir, const char *dirname) { WEBP2_UNSUPPORTED("opendirat"); } int posix_fadvise(int fd, off_t offset, off_t len, int advice) { - WEBP2_UNSUPPORTED("posix_fadvise"); + WEBP2_UNSUPPORTED("posix_fadvise"); } int posix_fallocate(int fd, off_t offset, off_t len) { - WEBP2_UNSUPPORTED("posix_fallocate"); + WEBP2_UNSUPPORTED("posix_fallocate"); } ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset) { - WEBP2_UNSUPPORTED("pread"); + WEBP2_UNSUPPORTED("pread"); } ssize_t preadv(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { - WEBP2_UNSUPPORTED("preadv"); + WEBP2_UNSUPPORTED("preadv"); } ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) { - WEBP2_UNSUPPORTED("pwrite"); + WEBP2_UNSUPPORTED("pwrite"); } -ssize_t pwritev(int fildes, const struct iovec *iov, int iovcnt, - off_t offset) { - WEBP2_UNSUPPORTED("pwritev"); +ssize_t pwritev(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { + WEBP2_UNSUPPORTED("pwritev"); } -struct dirent *readdir(DIR *dirp) { - WEBP2_UNSUPPORTED("readdir"); -} +struct dirent *readdir(DIR *dirp) { WEBP2_UNSUPPORTED("readdir"); } ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize) { - WEBP2_UNSUPPORTED("readlink"); + WEBP2_UNSUPPORTED("readlink"); } ssize_t readlinkat(int fd, const char *restrict path, char *restrict buf, size_t bufsize) { - WEBP2_UNSUPPORTED("readlinkat"); + WEBP2_UNSUPPORTED("readlinkat"); } -int remove(const char *path) { - WEBP2_UNSUPPORTED("remove"); -} +int remove(const char *path) { WEBP2_UNSUPPORTED("remove"); } -int rename(const char *old, const char *new) { - WEBP2_UNSUPPORTED("rename"); -} +int rename(const char *old, const char *new) { WEBP2_UNSUPPORTED("rename"); } int renameat(int oldfd, const char *old, int newfd, const char *new) { - WEBP2_UNSUPPORTED("renameat"); + WEBP2_UNSUPPORTED("renameat"); } -void rewinddir(DIR *dirp) { - WEBP2_UNSUPPORTED("rewinddir"); -} +void rewinddir(DIR *dirp) { WEBP2_UNSUPPORTED("rewinddir"); } -int rmdir(const char *path) { - WEBP2_UNSUPPORTED("rmdir"); -} +int rmdir(const char *path) { WEBP2_UNSUPPORTED("rmdir"); } int scandir(const char *dir, struct dirent ***namelist, int (*sel)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) { - WEBP2_UNSUPPORTED("scandir"); + WEBP2_UNSUPPORTED("scandir"); } int scandirat(int dirfd, const char *dir, struct dirent ***namelist, int (*sel)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) { - WEBP2_UNSUPPORTED("scandirat"); + WEBP2_UNSUPPORTED("scandirat"); } -void seekdir(DIR *dirp, long loc) { - WEBP2_UNSUPPORTED("seekdir"); -} +void seekdir(DIR *dirp, long loc) { WEBP2_UNSUPPORTED("seekdir"); } int stat(const char *restrict path, struct stat *restrict buf) { - WEBP2_UNSUPPORTED("stat"); + WEBP2_UNSUPPORTED("stat"); } int statvfs(const char *restrict path, struct statvfs *restrict buf) { - WEBP2_UNSUPPORTED("statvfs"); + WEBP2_UNSUPPORTED("statvfs"); } int symlink(const char *path1, const char *path2) { - WEBP2_UNSUPPORTED("symlink"); + WEBP2_UNSUPPORTED("symlink"); } int symlinkat(const char *path1, int fd, const char *path2) { - WEBP2_UNSUPPORTED("symlinkat"); + WEBP2_UNSUPPORTED("symlinkat"); } -long telldir(DIR *dirp) { - WEBP2_UNSUPPORTED("telldir"); -} +long telldir(DIR *dirp) { WEBP2_UNSUPPORTED("telldir"); } -int truncate(const char *path, off_t length) { - WEBP2_UNSUPPORTED("truncate"); -} +int truncate(const char *path, off_t length) { WEBP2_UNSUPPORTED("truncate"); } -int unlink(const char *path) { - WEBP2_UNSUPPORTED("unlink"); -} +int unlink(const char *path) { WEBP2_UNSUPPORTED("unlink"); } int unlinkat(int fd, const char *path, int flag) { - WEBP2_UNSUPPORTED("unlinkat"); + WEBP2_UNSUPPORTED("unlinkat"); } int utime(const char *path, const struct utimbuf *times) { - WEBP2_UNSUPPORTED("utime"); + WEBP2_UNSUPPORTED("utime"); } int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) { - WEBP2_UNSUPPORTED("utimensat"); + WEBP2_UNSUPPORTED("utimensat"); } int utimes(const char *path, const struct timeval times[2]) { - WEBP2_UNSUPPORTED("utimes"); + WEBP2_UNSUPPORTED("utimes"); } diff --git a/libc-bottom-half/sources/webp2_socket.c b/libc-bottom-half/sources/webp2_socket.c index 7ddd03f32..d7ba48195 100644 --- a/libc-bottom-half/sources/webp2_socket.c +++ b/libc-bottom-half/sources/webp2_socket.c @@ -1,74 +1,67 @@ // There are no sockets. -#include #include "wasi/report-error.h" +#include int accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len) { - WEBP2_UNSUPPORTED("accept"); + WEBP2_UNSUPPORTED("accept"); } int accept4(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len, int flags) { - WEBP2_UNSUPPORTED("accept4"); + WEBP2_UNSUPPORTED("accept4"); } int bind(int socket, const struct sockaddr *address, socklen_t address_len) { - WEBP2_UNSUPPORTED("bind"); + WEBP2_UNSUPPORTED("bind"); } -int connect(int socket, const struct sockaddr *address, - socklen_t address_len) { - WEBP2_UNSUPPORTED("connect"); +int connect(int socket, const struct sockaddr *address, socklen_t address_len) { + WEBP2_UNSUPPORTED("connect"); } int getpeername(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len) { - WEBP2_UNSUPPORTED("getpeername"); + WEBP2_UNSUPPORTED("getpeername"); } int getsockname(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len) { - WEBP2_UNSUPPORTED("getsockname"); + WEBP2_UNSUPPORTED("getsockname"); } int getsockopt(int socket, int level, int option_name, void *restrict option_value, socklen_t *restrict option_len) { - WEBP2_UNSUPPORTED("getsockopt"); + WEBP2_UNSUPPORTED("getsockopt"); } -int listen(int socket, int backlog) { - WEBP2_UNSUPPORTED("listen"); -} +int listen(int socket, int backlog) { WEBP2_UNSUPPORTED("listen"); } ssize_t recv(int socket, void *buffer, size_t length, int flags) { - WEBP2_UNSUPPORTED("recv"); + WEBP2_UNSUPPORTED("recv"); } ssize_t recvfrom(int socket, void *restrict buffer, size_t length, int flags, struct sockaddr *restrict address, socklen_t *restrict address_len) { - WEBP2_UNSUPPORTED("recvfrom"); + WEBP2_UNSUPPORTED("recvfrom"); } ssize_t send(int socket, const void *buffer, size_t length, int flags) { - WEBP2_UNSUPPORTED("send"); + WEBP2_UNSUPPORTED("send"); } ssize_t sendto(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len) { - WEBP2_UNSUPPORTED("sendto"); + WEBP2_UNSUPPORTED("sendto"); } -int setsockopt(int socket, int level, int option_name, - const void *option_value, socklen_t option_len) { - WEBP2_UNSUPPORTED("setsockopt"); +int setsockopt(int socket, int level, int option_name, const void *option_value, + socklen_t option_len) { + WEBP2_UNSUPPORTED("setsockopt"); } -int shutdown(int socket, int how) { - WEBP2_UNSUPPORTED("shutdown"); -} +int shutdown(int socket, int how) { WEBP2_UNSUPPORTED("shutdown"); } -int socket(int domain, int type, int protocol) { - WEBP2_UNSUPPORTED("socket"); -} +int socket(int domain, int type, int protocol) { WEBP2_UNSUPPORTED("socket"); } From ff9460e3c9adc272db8fb9cf2b8d56c068508b3f Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 6 Oct 2026 10:47:04 -0500 Subject: [PATCH 23/96] Shrink initial __wasilibc_random allocation --- libc-bottom-half/sources/__wasilibc_random.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libc-bottom-half/sources/__wasilibc_random.c b/libc-bottom-half/sources/__wasilibc_random.c index 5ce6d531f..b6f138f5a 100644 --- a/libc-bottom-half/sources/__wasilibc_random.c +++ b/libc-bottom-half/sources/__wasilibc_random.c @@ -19,6 +19,8 @@ typedef wasip3_list_u8_t list_u8_t; #error "Unknown WASI version" #endif +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + int __wasilibc_random(void *buffer, size_t len) { #if defined(__wasip2__) || defined(__wasip3__) // Set up a WASI byte list to receive the results @@ -46,10 +48,11 @@ int __wasilibc_random(void *buffer, size_t len) { // is obviously extremely jank, and doesn't avoid the obvious extra copy // (host -> ret -> buffer). But at least we reduce allocation and don't copy // the param. - webp2_own_uint_8_array_t arr = webp2_constructor_uint_8_array(MAX_CHUNK_SIZE); + uint32_t initial_alloc = (uint32_t)MIN(len, MAX_CHUNK_SIZE); + webp2_own_uint_8_array_t arr = webp2_constructor_uint_8_array(initial_alloc); for (size_t done = 0; done < len;) { size_t remaining = len - done; - if (remaining < MAX_CHUNK_SIZE) { + if (remaining < initial_alloc) { // "Resize" the input array. uint32_t remaining32 = remaining; webp2_own_uint_8_array_t arr2 = webp2_method_uint_8_array_subarray( From dd3e50cf3bec8737139c72296ad84ff36f314cd0 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 6 Oct 2026 11:57:50 -0500 Subject: [PATCH 24/96] Properly stub sleep functions --- libc-bottom-half/CMakeLists.txt | 16 ++++++++++---- .../cloudlibc/src/libc/time/clock_nanosleep.c | 7 ------- .../cloudlibc/src/libc/time/nanosleep.c | 2 +- .../cloudlibc/src/libc/unistd/sleep.c | 2 +- .../cloudlibc/src/libc/unistd/usleep.c | 2 +- libc-bottom-half/sources/webp2_sleep.c | 21 +++++++++++++++++++ 6 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 libc-bottom-half/sources/webp2_sleep.c diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index a9b31217e..2c422c2f4 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -70,11 +70,7 @@ set(bottom_half_sources cloudlibc/src/libc/time/CLOCK_REALTIME.c cloudlibc/src/libc/time/clock_getres.c cloudlibc/src/libc/time/clock_gettime.c - cloudlibc/src/libc/time/clock_nanosleep.c - cloudlibc/src/libc/time/nanosleep.c cloudlibc/src/libc/time/time.c - cloudlibc/src/libc/unistd/sleep.c - cloudlibc/src/libc/unistd/usleep.c sources/__errno_location.c sources/__main_void.c sources/__wasilibc_dt.c @@ -202,6 +198,16 @@ elseif(WASI STREQUAL "p2" OR WASI STREQUAL "p3") ) endif() +# Sleep +if (NOT (WEB STREQUAL "p2")) + list(APPEND bottom_half_sources + cloudlibc/src/libc/time/clock_nanosleep.c + cloudlibc/src/libc/time/nanosleep.c + cloudlibc/src/libc/unistd/sleep.c + cloudlibc/src/libc/unistd/usleep.c + ) +endif() + # wasip2-specific if(WASI STREQUAL "p2") list(APPEND bottom_half_sources @@ -230,6 +236,7 @@ if (WEB STREQUAL "p2") set_source_files_properties( sources/webp2_file_descriptors.c sources/webp2_filesystem.c + sources/webp2_sleep.c sources/webp2_socket.c PROPERTIES COMPILE_OPTIONS -Wno-unused-parameter ) @@ -237,6 +244,7 @@ if (WEB STREQUAL "p2") sources/webp2.c sources/webp2_file_descriptors.c sources/webp2_filesystem.c + sources/webp2_sleep.c sources/webp2_socket.c ) endif() diff --git a/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c b/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c index 8a79129b6..f6e757926 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c @@ -66,13 +66,6 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, } #endif return 0; - -#elif defined(__webp2__) - // TODO(webp2): Rather than return ENOTSUP, I figure I'd rather trap right - // now so we encounter issues more noisily. - __builtin_trap(); - (void)clock_id; - (void)rqtp; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c b/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c index ffdc9d526..31ba4faa9 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c @@ -7,7 +7,7 @@ #include int nanosleep(const struct timespec *rqtp, struct timespec *rem) { -#if defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) +#if defined(__wasip2__) || defined(__wasip3__) // FIXME(WebAssembly/WASI#857): wasip2/p3 only supports the monotonic clock for // sleeping. clockid_t clock = CLOCK_MONOTONIC; diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c b/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c index e5828686a..4ab07cbb8 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c @@ -12,7 +12,7 @@ unsigned int sleep(unsigned int seconds) { // sleeping. #if defined(__wasip1__) clockid_t clock_id = CLOCK_REALTIME; -#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) +#elif defined(__wasip2__) || defined(__wasip3__) clockid_t clock_id = CLOCK_MONOTONIC; #else # error "Unsupported WASI version" diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c b/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c index 6bb3f783a..6af5e80da 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c @@ -12,7 +12,7 @@ int usleep(useconds_t useconds) { .tv_nsec = useconds % 1000000 * 1000}; #if defined(__wasip1__) clockid_t clock_id = CLOCK_REALTIME; -#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) +#elif defined(__wasip2__) || defined(__wasip3__) // FIXME(WebAssembly/WASI#857): wasip2/p3 only supports the monotonic clock // for sleeping. clockid_t clock_id = CLOCK_MONOTONIC; diff --git a/libc-bottom-half/sources/webp2_sleep.c b/libc-bottom-half/sources/webp2_sleep.c new file mode 100644 index 000000000..706705a38 --- /dev/null +++ b/libc-bottom-half/sources/webp2_sleep.c @@ -0,0 +1,21 @@ +// Sleeping is not a good fit for the web (until we can figure out how to +// integrate JSPI, or something.) + +#include "wasi/report-error.h" +#include +#include + +int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, + struct timespec *rmtp) { + WEBP2_UNSUPPORTED("clock_nanosleep"); +} + +weak_alias(clock_nanosleep, __clock_nanosleep); + +int nanosleep(const struct timespec *rqtp, struct timespec *rem) { + WEBP2_UNSUPPORTED("nanosleep"); +} + +unsigned int sleep(unsigned int seconds) { WEBP2_UNSUPPORTED("sleep"); } + +int usleep(useconds_t useconds) { WEBP2_UNSUPPORTED("usleep"); } From 89b96614cbe61c94c7fbc46dc31f2f7d6df9a69b Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 6 Oct 2026 12:00:37 -0500 Subject: [PATCH 25/96] Remove unnecessary webp2 guards from sources that are no longer compiled --- libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c | 4 ++-- libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c | 4 ++-- libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h | 2 +- libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c | 2 +- libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c b/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c index 195e6615c..3c44db86c 100644 --- a/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c +++ b/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c @@ -53,7 +53,7 @@ int fcntl(int fildes, int cmd, ...) { oflags |= O_SEARCH; } return oflags; -#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) +#elif defined(__wasip2__) || defined(__wasip3__) if (!entry.vtable->fcntl_getfl) { errno = EINVAL; return -1; @@ -78,7 +78,7 @@ int fcntl(int fildes, int cmd, ...) { errno = error; return -1; } -#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) +#elif defined(__wasip2__) || defined(__wasip3__) if (!entry.vtable->fcntl_setfl) { errno = EINVAL; return -1; 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 592c62c58..f8626f437 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c @@ -59,7 +59,7 @@ int ioctl(int fildes, int request, ...) { // No data available for reading. *result = 0; return 0; -#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) +#elif defined(__wasip2__) || defined(__wasip3__) // wasip{2,3} doesn't support this operation errno = ENOTSUP; return -1; @@ -93,7 +93,7 @@ int ioctl(int fildes, int request, ...) { return -1; } return 0; -#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) +#elif defined(__wasip2__) || defined(__wasip3__) descriptor_table_entry_t entry; if (descriptor_table_get(fildes, &entry) < 0) return -1; diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h b/libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h index e289f1a44..c55075fc1 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h +++ b/libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h @@ -65,7 +65,7 @@ static inline void to_public_stat(const __wasi_filestat_t *in, break; } } -#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) +#elif defined(__wasip2__) || defined(__wasip3__) #ifdef __wasip3__ typedef filesystem_instant_t filesystem_datetime_t; #endif 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 951282088..90ed31a9d 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c @@ -41,7 +41,7 @@ ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { return -1; } return bytes_read; -#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) +#elif defined(__wasip2__) || defined(__wasip3__) // Skip empty iovecs and then delegate to `read` with the first non-empty // iovec. while (iovcnt) { diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c b/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c index cf07d7a2f..8fe2024e4 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c @@ -30,7 +30,7 @@ off_t __lseek(int fildes, off_t offset, int whence) { return -1; } return new_offset; -#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) +#elif defined(__wasip2__) || defined(__wasip3__) // Look up a stream for fildes descriptor_table_entry_t entry; if (descriptor_table_get(fildes, &entry) < 0) From 115eed72fd8f0bbee4eaa3baea095add351e79ff Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 6 Oct 2026 12:13:22 -0500 Subject: [PATCH 26/96] Suppress unnecessary wasi headers --- libc-bottom-half/CMakeLists.txt | 8 ++++++-- libc-top-half/musl/src/env/__init_tls.c | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index 2c422c2f4..b25842160 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -32,10 +32,14 @@ if (NOT (WASI STREQUAL "p3")) endif() if (WEB STREQUAL "p2") - # Just remove some headers that contain only __wasilibc filesystem utility - # functions so that we don't have to stub them all out. + # Remove some headers that are unnecessary for webp2, either because their + # contents are completely ifdef'd out or because they contain only internal + # utility functions we would otherwise need to stub. list(REMOVE_ITEM globbed_headers headers/public/wasi/libc-find-relpath.h) list(REMOVE_ITEM globbed_headers headers/public/wasi/libc-nocwd.h) + list(REMOVE_ITEM globbed_headers headers/public/wasi/wasip1.h) + list(REMOVE_ITEM globbed_headers headers/public/wasi/wasip2.h) + list(REMOVE_ITEM globbed_headers headers/public/wasi/wasip3.h) else () list(REMOVE_ITEM globbed_headers headers/public/wasi/__generated_webp2.h) endif() diff --git a/libc-top-half/musl/src/env/__init_tls.c b/libc-top-half/musl/src/env/__init_tls.c index 00d31dfa7..18ccbb1c7 100644 --- a/libc-top-half/musl/src/env/__init_tls.c +++ b/libc-top-half/musl/src/env/__init_tls.c @@ -16,7 +16,10 @@ #include #include "lock.h" #include + +#ifdef __wasip3__ #include +#endif DECLARE_WEAK_LOCK(__thread_list_lock); From cabca693fcfa8496e70190055a1c73d2f2d8c6bf Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 6 Oct 2026 12:19:48 -0500 Subject: [PATCH 27/96] Update expected files --- expected/wasm32-webp2/include-all.c | 3 --- expected/wasm32-webp2/predefined-macros.txt | 8 +++----- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/expected/wasm32-webp2/include-all.c b/expected/wasm32-webp2/include-all.c index e5159d66b..339c98a6e 100644 --- a/expected/wasm32-webp2/include-all.c +++ b/expected/wasm32-webp2/include-all.c @@ -170,9 +170,6 @@ #include #include #include -#include -#include -#include #include #include #include diff --git a/expected/wasm32-webp2/predefined-macros.txt b/expected/wasm32-webp2/predefined-macros.txt index 1cf8ab862..d2a708906 100644 --- a/expected/wasm32-webp2/predefined-macros.txt +++ b/expected/wasm32-webp2/predefined-macros.txt @@ -46,6 +46,7 @@ #define ARG_MAX 131072 #define ARMAG "!\n" #define AT_EACCESS (0x0) +#define AT_EMPTY_PATH (0x8) #define AT_FDCWD (-2) #define AT_REMOVEDIR (0x4) #define AT_SYMLINK_FOLLOW (0x2) @@ -2922,9 +2923,7 @@ #define __UINT_LEAST8_TYPE__ unsigned char #define __USER_LABEL_PREFIX__ #define __USE_TIME_BITS64 1 -#define __WASI_VERSION_H -#define __WASI_WASIP2_H -#define __WASI_WASIP3_H +#define __WASI_VERSION_H #define __WASI_WEBP2_H #define __WCHAR_TYPE__ int #define __WCHAR_WIDTH__ 32 @@ -2961,8 +2960,7 @@ #define __wasi_api_h #define __wasi_libc_busywait_h #define __wasi_libc_environ_h -#define __wasi_libc_h -#define __wasi_wasip1_h +#define __wasi_libc_h #define __wasilibc___errno_values_h #define __wasilibc___fd_set_h #define __wasilibc___function___isatty_h From 28cbcb54ce6b61108eb1bcfb78020f21fb53bce5 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 6 Oct 2026 12:33:49 -0500 Subject: [PATCH 28/96] Better regex for a target triple --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d363da7a4..a2e3aa4a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,7 +79,7 @@ elseif(TARGET_TRIPLE MATCHES "-wasip2$") elseif(TARGET_TRIPLE MATCHES "-wasip3$") set(WASI p3) set(SHARED ON) -elseif(TARGET_TRIPLE MATCHES "-webp2") +elseif(TARGET_TRIPLE MATCHES "-webp2$") set(WEB p2) set(SHARED ON) else() From 39664f11f730bdf3d68d0dc5e4b7eba5d62263d3 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Wed, 7 Oct 2026 12:16:45 -0500 Subject: [PATCH 29/96] Change what gets the external-id in WIT --- libc-bottom-half/cloudlibc/src/common/time.h | 8 +-- .../headers/public/wasi/__generated_webp2.h | 34 ++++----- libc-bottom-half/sources/__wasilibc_random.c | 8 +-- libc-bottom-half/sources/webp2.c | 68 +++++++++--------- .../sources/webp2_component_type.o | Bin 843 -> 832 bytes web/p2/wit/wasi-libc.wit | 12 ++-- 6 files changed, 65 insertions(+), 65 deletions(-) diff --git a/libc-bottom-half/cloudlibc/src/common/time.h b/libc-bottom-half/cloudlibc/src/common/time.h index aec7f9939..d73430e30 100644 --- a/libc-bottom-half/cloudlibc/src/common/time.h +++ b/libc-bottom-half/cloudlibc/src/common/time.h @@ -205,10 +205,10 @@ static inline wasilibc_timestamp_t web_wall_clock_now() { } static inline monotonic_clock_instant_t web_monotonic_clock_now() { - webp2_own_performance_impl_t perf = webp2_get_performance(); - double time_origin = webp2_method_get_performance_impl_time_origin(webp2_borrow_performance_impl(perf)); - double clock_ms = time_origin + webp2_method_performance_impl_now(webp2_borrow_performance_impl(perf)); - webp2_performance_impl_drop_own(perf); + webp2_own_performance_t perf = webp2_get_global_performance(); + double time_origin = webp2_method_get_performance_time_origin(webp2_borrow_performance(perf)); + double clock_ms = time_origin + webp2_method_performance_now(webp2_borrow_performance(perf)); + webp2_performance_drop_own(perf); return (monotonic_clock_instant_t)(clock_ms * NSEC_PER_MSEC); } #endif diff --git a/libc-bottom-half/headers/public/wasi/__generated_webp2.h b/libc-bottom-half/headers/public/wasi/__generated_webp2.h index d9af9c444..3d97d49e7 100644 --- a/libc-bottom-half/headers/public/wasi/__generated_webp2.h +++ b/libc-bottom-half/headers/public/wasi/__generated_webp2.h @@ -14,13 +14,13 @@ typedef struct webp2_string_t { size_t len; } webp2_string_t; -typedef struct webp2_own_performance_impl_t { +typedef struct webp2_own_performance_t { int32_t __handle; -} webp2_own_performance_impl_t; +} webp2_own_performance_t; -typedef struct webp2_borrow_performance_impl_t { +typedef struct webp2_borrow_performance_t { int32_t __handle; -} webp2_borrow_performance_impl_t; +} webp2_borrow_performance_t; typedef struct webp2_own_date_t { int32_t __handle; @@ -55,13 +55,13 @@ typedef struct webp2_borrow_uint_8_array_t { int32_t __handle; } webp2_borrow_uint_8_array_t; -typedef struct webp2_own_crypto_impl_t { +typedef struct webp2_own_crypto_t { int32_t __handle; -} webp2_own_crypto_impl_t; +} webp2_own_crypto_t; -typedef struct webp2_borrow_crypto_impl_t { +typedef struct webp2_borrow_crypto_t { int32_t __handle; -} webp2_borrow_crypto_impl_t; +} webp2_borrow_crypto_t; typedef struct { bool is_some; @@ -79,11 +79,11 @@ typedef struct { } webp2_list_u8_t; // Imported Functions from `webp2` -extern webp2_own_performance_impl_t webp2_get_performance(void); +extern webp2_own_performance_t webp2_get_global_performance(void); extern void webp2_report_error(webp2_borrow_error_t throwable); -extern webp2_own_crypto_impl_t webp2_get_crypto(void); -extern double webp2_method_performance_impl_now(webp2_borrow_performance_impl_t self); -extern double webp2_method_get_performance_impl_time_origin(webp2_borrow_performance_impl_t self); +extern webp2_own_crypto_t webp2_get_global_crypto(void); +extern double webp2_method_performance_now(webp2_borrow_performance_t self); +extern double webp2_method_get_performance_time_origin(webp2_borrow_performance_t self); extern double webp2_static_date_now(void); extern webp2_own_error_t webp2_constructor_error(webp2_string_t *maybe_message, webp2_new_error_options_t *maybe_options); extern webp2_own_uint_8_array_t webp2_constructor_uint_8_array(uint32_t length); @@ -98,13 +98,13 @@ extern webp2_own_uint_8_array_t webp2_method_uint_8_array_subarray(webp2_borrow_ // // We desperately need to find a better way to do this. Some new // builtin? New web APIs? -extern void webp2_method_crypto_impl_get_random_values(webp2_borrow_crypto_impl_t self, webp2_borrow_uint_8_array_t typed_array, webp2_list_u8_t *ret); +extern void webp2_method_crypto_get_random_values(webp2_borrow_crypto_t self, webp2_borrow_uint_8_array_t typed_array, webp2_list_u8_t *ret); // Helper Functions -extern void webp2_performance_impl_drop_own(webp2_own_performance_impl_t handle); +extern void webp2_performance_drop_own(webp2_own_performance_t handle); -extern webp2_borrow_performance_impl_t webp2_borrow_performance_impl(webp2_own_performance_impl_t handle); +extern webp2_borrow_performance_t webp2_borrow_performance(webp2_own_performance_t handle); extern void webp2_date_drop_own(webp2_own_date_t handle); @@ -122,9 +122,9 @@ extern void webp2_uint_8_array_drop_own(webp2_own_uint_8_array_t handle); extern webp2_borrow_uint_8_array_t webp2_borrow_uint_8_array(webp2_own_uint_8_array_t handle); -extern void webp2_crypto_impl_drop_own(webp2_own_crypto_impl_t handle); +extern void webp2_crypto_drop_own(webp2_own_crypto_t handle); -extern webp2_borrow_crypto_impl_t webp2_borrow_crypto_impl(webp2_own_crypto_impl_t handle); +extern webp2_borrow_crypto_t webp2_borrow_crypto(webp2_own_crypto_t handle); void webp2_option_new_error_options_free(webp2_option_new_error_options_t *ptr); diff --git a/libc-bottom-half/sources/__wasilibc_random.c b/libc-bottom-half/sources/__wasilibc_random.c index b6f138f5a..30ec7ea65 100644 --- a/libc-bottom-half/sources/__wasilibc_random.c +++ b/libc-bottom-half/sources/__wasilibc_random.c @@ -41,7 +41,7 @@ int __wasilibc_random(void *buffer, size_t len) { // Free the WASI byte list list_u8_free(&wasi_list); #elif defined(__webp2__) - webp2_own_crypto_impl_t crypto = webp2_get_crypto(); + webp2_own_crypto_t crypto = webp2_get_global_crypto(); const size_t MAX_CHUNK_SIZE = 65536; // Attempt to reduce copies by repeatedly reusing the same Uint8Array. This @@ -62,8 +62,8 @@ int __wasilibc_random(void *buffer, size_t len) { } webp2_list_u8_t ret; - webp2_method_crypto_impl_get_random_values( - webp2_borrow_crypto_impl(crypto), webp2_borrow_uint_8_array(arr), &ret); + webp2_method_crypto_get_random_values( + webp2_borrow_crypto(crypto), webp2_borrow_uint_8_array(arr), &ret); if (ret.len != (remaining < MAX_CHUNK_SIZE ? remaining : MAX_CHUNK_SIZE)) { _Exit(EX_OSERR); } @@ -73,7 +73,7 @@ int __wasilibc_random(void *buffer, size_t len) { } webp2_uint_8_array_drop_own(arr); - webp2_crypto_impl_drop_own(crypto); + webp2_crypto_drop_own(crypto); #else #error "Unknown WASI version" #endif diff --git a/libc-bottom-half/sources/webp2.c b/libc-bottom-half/sources/webp2.c index a2199944a..654c2cea1 100644 --- a/libc-bottom-half/sources/webp2.c +++ b/libc-bottom-half/sources/webp2.c @@ -5,20 +5,20 @@ // Imported Functions from `webp2` -__attribute__((__import_module__("$root"), __import_name__("[get]performance"))) -extern int32_t __wasm_import_webp2_get_performance(void); +__attribute__((__import_module__("$root"), __import_name__("[get]global-performance"))) +extern int32_t __wasm_import_webp2_get_global_performance(void); __attribute__((__import_module__("$root"), __import_name__("report-error"))) extern void __wasm_import_webp2_report_error(int32_t); -__attribute__((__import_module__("$root"), __import_name__("[get]crypto"))) -extern int32_t __wasm_import_webp2_get_crypto(void); +__attribute__((__import_module__("$root"), __import_name__("[get]global-crypto"))) +extern int32_t __wasm_import_webp2_get_global_crypto(void); -__attribute__((__import_module__("$root"), __import_name__("[method]performance-impl.now"))) -extern double __wasm_import_webp2_method_performance_impl_now(int32_t); +__attribute__((__import_module__("$root"), __import_name__("[method]performance.now"))) +extern double __wasm_import_webp2_method_performance_now(int32_t); -__attribute__((__import_module__("$root"), __import_name__("[method][get]performance-impl.time-origin"))) -extern double __wasm_import_webp2_method_get_performance_impl_time_origin(int32_t); +__attribute__((__import_module__("$root"), __import_name__("[method][get]performance.time-origin"))) +extern double __wasm_import_webp2_method_get_performance_time_origin(int32_t); __attribute__((__import_module__("$root"), __import_name__("[static]date.now"))) extern double __wasm_import_webp2_static_date_now(void); @@ -32,8 +32,8 @@ extern int32_t __wasm_import_webp2_constructor_uint_8_array(int32_t); __attribute__((__import_module__("$root"), __import_name__("[method]uint-8-array.subarray"))) extern int32_t __wasm_import_webp2_method_uint_8_array_subarray(int32_t, int32_t, int32_t, int32_t); -__attribute__((__import_module__("$root"), __import_name__("[method]crypto-impl.get-random-values"))) -extern void __wasm_import_webp2_method_crypto_impl_get_random_values(int32_t, int32_t, uint8_t *); +__attribute__((__import_module__("$root"), __import_name__("[method]crypto.get-random-values"))) +extern void __wasm_import_webp2_method_crypto_get_random_values(int32_t, int32_t, uint8_t *); // Canonical ABI intrinsics @@ -48,15 +48,15 @@ void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) { // Helper Functions -__attribute__((__import_module__("$root"), __import_name__("[resource-drop]performance-impl"))) -extern void __wasm_import_webp2_performance_impl_drop(int32_t handle); +__attribute__((__import_module__("$root"), __import_name__("[resource-drop]performance"))) +extern void __wasm_import_webp2_performance_drop(int32_t handle); -void webp2_performance_impl_drop_own(webp2_own_performance_impl_t handle) { - __wasm_import_webp2_performance_impl_drop(handle.__handle); +void webp2_performance_drop_own(webp2_own_performance_t handle) { + __wasm_import_webp2_performance_drop(handle.__handle); } -webp2_borrow_performance_impl_t webp2_borrow_performance_impl(webp2_own_performance_impl_t arg) { - return (webp2_borrow_performance_impl_t) { arg.__handle }; +webp2_borrow_performance_t webp2_borrow_performance(webp2_own_performance_t arg) { + return (webp2_borrow_performance_t) { arg.__handle }; } __attribute__((__import_module__("$root"), __import_name__("[resource-drop]date"))) @@ -102,15 +102,15 @@ webp2_borrow_uint_8_array_t webp2_borrow_uint_8_array(webp2_own_uint_8_array_t a return (webp2_borrow_uint_8_array_t) { arg.__handle }; } -__attribute__((__import_module__("$root"), __import_name__("[resource-drop]crypto-impl"))) -extern void __wasm_import_webp2_crypto_impl_drop(int32_t handle); +__attribute__((__import_module__("$root"), __import_name__("[resource-drop]crypto"))) +extern void __wasm_import_webp2_crypto_drop(int32_t handle); -void webp2_crypto_impl_drop_own(webp2_own_crypto_impl_t handle) { - __wasm_import_webp2_crypto_impl_drop(handle.__handle); +void webp2_crypto_drop_own(webp2_own_crypto_t handle) { + __wasm_import_webp2_crypto_drop(handle.__handle); } -webp2_borrow_crypto_impl_t webp2_borrow_crypto_impl(webp2_own_crypto_impl_t arg) { - return (webp2_borrow_crypto_impl_t) { arg.__handle }; +webp2_borrow_crypto_t webp2_borrow_crypto(webp2_own_crypto_t arg) { + return (webp2_borrow_crypto_t) { arg.__handle }; } void webp2_option_new_error_options_free(webp2_option_new_error_options_t *ptr) { @@ -161,27 +161,27 @@ void webp2_string_free(webp2_string_t *ret) { // Component Adapters -webp2_own_performance_impl_t webp2_get_performance(void) { - int32_t ret = __wasm_import_webp2_get_performance(); - return (webp2_own_performance_impl_t) { ret }; +webp2_own_performance_t webp2_get_global_performance(void) { + int32_t ret = __wasm_import_webp2_get_global_performance(); + return (webp2_own_performance_t) { ret }; } void webp2_report_error(webp2_borrow_error_t throwable) { __wasm_import_webp2_report_error((throwable).__handle); } -webp2_own_crypto_impl_t webp2_get_crypto(void) { - int32_t ret = __wasm_import_webp2_get_crypto(); - return (webp2_own_crypto_impl_t) { ret }; +webp2_own_crypto_t webp2_get_global_crypto(void) { + int32_t ret = __wasm_import_webp2_get_global_crypto(); + return (webp2_own_crypto_t) { ret }; } -double webp2_method_performance_impl_now(webp2_borrow_performance_impl_t self) { - double ret = __wasm_import_webp2_method_performance_impl_now((self).__handle); +double webp2_method_performance_now(webp2_borrow_performance_t self) { + double ret = __wasm_import_webp2_method_performance_now((self).__handle); return ret; } -double webp2_method_get_performance_impl_time_origin(webp2_borrow_performance_impl_t self) { - double ret = __wasm_import_webp2_method_get_performance_impl_time_origin((self).__handle); +double webp2_method_get_performance_time_origin(webp2_borrow_performance_t self) { + double ret = __wasm_import_webp2_method_get_performance_time_origin((self).__handle); return ret; } @@ -269,11 +269,11 @@ webp2_own_uint_8_array_t webp2_method_uint_8_array_subarray(webp2_borrow_uint_8_ return (webp2_own_uint_8_array_t) { ret }; } -void webp2_method_crypto_impl_get_random_values(webp2_borrow_crypto_impl_t self, webp2_borrow_uint_8_array_t typed_array, webp2_list_u8_t *ret) { +void webp2_method_crypto_get_random_values(webp2_borrow_crypto_t self, webp2_borrow_uint_8_array_t typed_array, webp2_list_u8_t *ret) { __attribute__((__aligned__(sizeof(void*)))) uint8_t ret_area[(2*sizeof(void*))]; uint8_t *ptr = (uint8_t *) &ret_area; - __wasm_import_webp2_method_crypto_impl_get_random_values((self).__handle, (typed_array).__handle, ptr); + __wasm_import_webp2_method_crypto_get_random_values((self).__handle, (typed_array).__handle, ptr); *ret = (webp2_list_u8_t) { (uint8_t*)(*((uint8_t **) (ptr + 0))), (*((size_t*) (ptr + sizeof(void*)))) }; } diff --git a/libc-bottom-half/sources/webp2_component_type.o b/libc-bottom-half/sources/webp2_component_type.o index d150b94cd2cf6467a0295dd17285d36f35156e5e..cb9d8a269dff4208c2691611965a650dfd779c68 100644 GIT binary patch delta 184 zcmX@jc7SbyB;)IeQm#CgSQs6d7#-D^8Mq53)`jXbuq78&7L?>OGcsl}Fgh?Wa4<88 zN2jNj#HQ!uCne_S7Ni!Xl+6@5G|2z=@ delta 195 zcmX@WcA9O1B;&VVW=6(L21W-41`cKhf#~$qlGuswt=Jj3!OY1Hj1u-T(YdK58Tl!( l=mzWM<(D(^Gc#zy6oB=htD4-)_) f64; time-origin: get() -> f64; } - import performance: get() -> performance-impl; + @external-id("performance") + import global-performance: get() -> performance; resource date { now: static func() -> f64; @@ -25,8 +25,7 @@ world webp2 { subarray: func(begin: u32, end: option) -> uint-8-array; } - @external-id("Crypto") - resource crypto-impl { + resource crypto { // TODO(webp2): This is very jank, but basically, the whole design of // Crypto.getRandomValues() doesn't make sense for the component model // because it modifies its parameter in place. The component model has @@ -39,5 +38,6 @@ world webp2 { // builtin? New web APIs? get-random-values: func(typed-array: borrow) -> list; } - import crypto: get() -> crypto-impl; + @external-id("crypto") + import global-crypto: get() -> crypto; } From 96e1c0ee676719989ec102d2a4f29784a3961da0 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Wed, 7 Oct 2026 12:41:34 -0500 Subject: [PATCH 30/96] Fix formatting and expected symbols --- expected/wasm32-webp2/defined-symbols.txt | 18 +++++++++--------- expected/wasm32-webp2/undefined-symbols.txt | 14 +++++++------- libc-bottom-half/sources/__wasilibc_random.c | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/expected/wasm32-webp2/defined-symbols.txt b/expected/wasm32-webp2/defined-symbols.txt index c17154ed8..e37a563c8 100644 --- a/expected/wasm32-webp2/defined-symbols.txt +++ b/expected/wasm32-webp2/defined-symbols.txt @@ -1332,28 +1332,28 @@ wctrans_l wctype wctype_l wcwidth -webp2_borrow_crypto_impl +webp2_borrow_crypto webp2_borrow_date webp2_borrow_error -webp2_borrow_performance_impl +webp2_borrow_performance webp2_borrow_uint_8_array webp2_constructor_error webp2_constructor_uint_8_array -webp2_crypto_impl_drop_own +webp2_crypto_drop_own webp2_date_drop_own webp2_error_drop_own -webp2_get_crypto -webp2_get_performance +webp2_get_global_crypto +webp2_get_global_performance webp2_list_u8_free -webp2_method_crypto_impl_get_random_values -webp2_method_get_performance_impl_time_origin -webp2_method_performance_impl_now +webp2_method_crypto_get_random_values +webp2_method_get_performance_time_origin +webp2_method_performance_now webp2_method_uint_8_array_subarray webp2_new_error_options_free webp2_option_new_error_options_free webp2_option_string_free webp2_option_u32_free -webp2_performance_impl_drop_own +webp2_performance_drop_own webp2_report_error webp2_static_date_now webp2_string_dup diff --git a/expected/wasm32-webp2/undefined-symbols.txt b/expected/wasm32-webp2/undefined-symbols.txt index 6588db006..6b6532710 100644 --- a/expected/wasm32-webp2/undefined-symbols.txt +++ b/expected/wasm32-webp2/undefined-symbols.txt @@ -24,16 +24,16 @@ __unordtf2 __wasm_call_ctors __wasm_import_webp2_constructor_error __wasm_import_webp2_constructor_uint_8_array -__wasm_import_webp2_crypto_impl_drop +__wasm_import_webp2_crypto_drop __wasm_import_webp2_date_drop __wasm_import_webp2_error_drop -__wasm_import_webp2_get_crypto -__wasm_import_webp2_get_performance -__wasm_import_webp2_method_crypto_impl_get_random_values -__wasm_import_webp2_method_get_performance_impl_time_origin -__wasm_import_webp2_method_performance_impl_now +__wasm_import_webp2_get_global_crypto +__wasm_import_webp2_get_global_performance +__wasm_import_webp2_method_crypto_get_random_values +__wasm_import_webp2_method_get_performance_time_origin +__wasm_import_webp2_method_performance_now __wasm_import_webp2_method_uint_8_array_subarray -__wasm_import_webp2_performance_impl_drop +__wasm_import_webp2_performance_drop __wasm_import_webp2_report_error __wasm_import_webp2_static_date_now __wasm_import_webp2_uint_8_array_drop diff --git a/libc-bottom-half/sources/__wasilibc_random.c b/libc-bottom-half/sources/__wasilibc_random.c index 30ec7ea65..a0c8cbfbf 100644 --- a/libc-bottom-half/sources/__wasilibc_random.c +++ b/libc-bottom-half/sources/__wasilibc_random.c @@ -62,8 +62,8 @@ int __wasilibc_random(void *buffer, size_t len) { } webp2_list_u8_t ret; - webp2_method_crypto_get_random_values( - webp2_borrow_crypto(crypto), webp2_borrow_uint_8_array(arr), &ret); + webp2_method_crypto_get_random_values(webp2_borrow_crypto(crypto), + webp2_borrow_uint_8_array(arr), &ret); if (ret.len != (remaining < MAX_CHUNK_SIZE ? remaining : MAX_CHUNK_SIZE)) { _Exit(EX_OSERR); } From 6b78b5c1d69ad689dee9358ab39565f62e1c19fb Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 8 Oct 2026 09:22:05 -0500 Subject: [PATCH 31/96] Support new WEB system name --- CMakeLists.txt | 16 +++++++++++----- cmake/Platform/WEB.cmake | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 cmake/Platform/WEB.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index a2e3aa4a9..45c71e01d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,8 +12,17 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type to use" FORCE) endif() -# Force a cross-compile to WASI at all times. -set(CMAKE_SYSTEM_NAME WASI) +set(TARGET_TRIPLE "wasm32-wasip1" CACHE STRING "WASI target to test") +set(CMAKE_C_COMPILER_TARGET ${TARGET_TRIPLE}) + +# Force a cross-compile to WASI or WEB at all times. +if(TARGET_TRIPLE MATCHES "-wasi") + set(CMAKE_SYSTEM_NAME WASI) +elseif(TARGET_TRIPLE MATCHES "-web") + set(CMAKE_SYSTEM_NAME WEB) +else() + message(FATAL_ERROR "Unknown target triple: ${TARGET_TRIPLE}") +endif() # I'm not entirely sure why this is necessary on Windows. This may be a bug in # CMake, I'm not sure. The problem is that, at least on CI, CMake detects that @@ -26,9 +35,6 @@ if (CMAKE_HOST_WIN32) set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ") endif() -set(TARGET_TRIPLE "wasm32-wasip1" CACHE STRING "WASI target to test") -set(CMAKE_C_COMPILER_TARGET ${TARGET_TRIPLE}) - # We're building libc, and the compiler being used may not have a wasm sysroot # for libc, so skip this check. Otherwise the `project` call below attempts a # link to see if the compiler is working and if that fails it doesn't mean it diff --git a/cmake/Platform/WEB.cmake b/cmake/Platform/WEB.cmake new file mode 100644 index 000000000..b9265681c --- /dev/null +++ b/cmake/Platform/WEB.cmake @@ -0,0 +1 @@ +set(WEB 1) From 2a623e6e285a9bb38575a468d8268266a1dc7284 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 8 Oct 2026 09:22:17 -0500 Subject: [PATCH 32/96] Bump wasm-component-ld version for accessor support --- cmake/wasm-component-ld.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/wasm-component-ld.cmake b/cmake/wasm-component-ld.cmake index 70dcd4680..707f89585 100644 --- a/cmake/wasm-component-ld.cmake +++ b/cmake/wasm-component-ld.cmake @@ -3,7 +3,7 @@ if (NOT USE_WASM_COMPONENT_LD) return() endif() -set(WASM_COMPONENT_LD_REQUIRED_VERSION "0.5.30") +set(WASM_COMPONENT_LD_REQUIRED_VERSION "0.5.31") find_program(WASM_COMPONENT_LD_EXECUTABLE NAMES wasm-component-ld) From 2969570b8ddedb94d91a96c10d29815ffc111e10 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 8 Oct 2026 09:53:44 -0500 Subject: [PATCH 33/96] Hook main into _initialize for webp2 --- expected/wasm32-webp2/defined-symbols.txt | 1 + libc-bottom-half/crt/crt1-reactor.c | 23 +++++++++++++++++++ libc-bottom-half/sources/__main_void.c | 13 ++++------- .../sources/__wasilibc_report_error.c | 8 ++++++- .../headers/private/wasi/report-error.h | 4 ++++ 5 files changed, 39 insertions(+), 10 deletions(-) diff --git a/expected/wasm32-webp2/defined-symbols.txt b/expected/wasm32-webp2/defined-symbols.txt index e37a563c8..627b0fe92 100644 --- a/expected/wasm32-webp2/defined-symbols.txt +++ b/expected/wasm32-webp2/defined-symbols.txt @@ -332,6 +332,7 @@ __wasilibc_stat __wasilibc_tell __wasilibc_unlinkat __wasilibc_utimens +__wasilibc_webp2_report_error __wasm_call_dtors __wcscoll_l __wcsftime_l diff --git a/libc-bottom-half/crt/crt1-reactor.c b/libc-bottom-half/crt/crt1-reactor.c index 293d1e568..ea0fd2316 100644 --- a/libc-bottom-half/crt/crt1-reactor.c +++ b/libc-bottom-half/crt/crt1-reactor.c @@ -1,8 +1,15 @@ +#include "wasi/report-error.h" + #if defined(_REENTRANT) #include #endif + extern void __wasm_call_ctors(void); +#ifdef __webp2__ +extern int __main_void(void); +#endif + #include "wasip3_symbol_references.h" __attribute__((export_name("_initialize"))) void _initialize(void) { @@ -22,4 +29,20 @@ __attribute__((export_name("_initialize"))) void _initialize(void) { // The linker synthesizes this to call constructors. __wasm_call_ctors(); + +#ifdef __webp2__ + // The webp2 target gets a slightly modified reactor model where the main + // function is executed automatically but nothing is torn down on exit, + // mimicking Emscripten's EXIT_RUNTIME=0 behavior. + // + // This not only provides a natural way for web modules to provide + // initialization and module side effects, but also ensures that a user- + // provided main is never inadvertently dropped during linking, which tends + // to break various cmake checks when reactor is the default. + int main_status = __main_void(); + if (main_status != 0) { + __wasilibc_webp2_report_error("main exited with nonzero status"); + __builtin_trap(); + } +#endif } diff --git a/libc-bottom-half/sources/__main_void.c b/libc-bottom-half/sources/__main_void.c index cc52e9c62..58f00c37d 100644 --- a/libc-bottom-half/sources/__main_void.c +++ b/libc-bottom-half/sources/__main_void.c @@ -117,15 +117,10 @@ __attribute__((__weak__, nodebug)) int __main_void(void) { // Call `__main_argc_argv` with the arguments! return __main_argc_argv(argc, argv); #elif defined(__webp2__) - // TODO(webp2): I think what we actually want is some kind of reactor-ish but - // not quite reactor model, where a main function will be registered as a - // component start function but called with no arguments. This is analogous - // to the arbitrary side effect code that runs when you import a JS module. - // Core wasm start functions are almost certainly not what you want for many - // reasons, but particularly the lack of any ability to, you know, call out - // to the host. That will require component start functions to be somewhat - // more standardized than they are, which unfortunately might require more - // standardization of value imports/exports. + // See comments in crt1-reactor.c. + if (!__main_argc_argv) { + return 0; + } static char *argv[] = {NULL}; return __main_argc_argv(0, argv); #else diff --git a/libc-bottom-half/sources/__wasilibc_report_error.c b/libc-bottom-half/sources/__wasilibc_report_error.c index af393d047..7db377c0a 100644 --- a/libc-bottom-half/sources/__wasilibc_report_error.c +++ b/libc-bottom-half/sources/__wasilibc_report_error.c @@ -6,10 +6,16 @@ void __wasilibc_report_error(const char *message) { #if defined(__wasip1__) || defined(__wasip2__) || defined(__wasip3__) fprintf(stderr, "%s\n", message); #elif defined(__webp2__) + __wasilibc_webp2_report_error(message); +#endif +} + +#ifdef __webp2__ +void __wasilibc_webp2_report_error(const char *message) { webp2_string_t msg; webp2_string_set(&msg, message); webp2_own_error_t err = webp2_constructor_error(&msg, NULL); webp2_report_error(webp2_borrow_error(err)); webp2_error_drop_own(err); -#endif } +#endif diff --git a/libc-top-half/headers/private/wasi/report-error.h b/libc-top-half/headers/private/wasi/report-error.h index 9597c7fcd..af26b5e13 100644 --- a/libc-top-half/headers/private/wasi/report-error.h +++ b/libc-top-half/headers/private/wasi/report-error.h @@ -4,6 +4,7 @@ #include #include +// Reports an error using the system's typical error output. hidden void __wasilibc_report_error(const char* message); #ifdef __webp2__ @@ -12,6 +13,9 @@ hidden void __wasilibc_report_error(const char* message); __wasilibc_report_error(name " is not supported on the web"); \ __builtin_trap(); \ } while (0) + +// Reports an error to the console using webp2 bindings. +void __wasilibc_webp2_report_error(const char* message); #endif #endif From 4cb0ed1f119853642f9d72e44ca85428076dd8c7 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Fri, 9 Oct 2026 11:51:50 -0500 Subject: [PATCH 34/96] Import web things from wasm:js/global interface --- cmake/bindings.cmake | 1 + .../headers/public/wasi/__generated_webp2.h | 8 +- libc-bottom-half/sources/webp2.c | 72 +++++++++--------- .../sources/webp2_component_type.o | Bin 832 -> 854 bytes web/p2/wit/deps/web.wit | 43 +++++++++++ web/p2/wit/wasi-libc.wit | 40 +--------- 6 files changed, 85 insertions(+), 79 deletions(-) create mode 100644 web/p2/wit/deps/web.wit diff --git a/cmake/bindings.cmake b/cmake/bindings.cmake index e9da59e29..b3972ff1c 100644 --- a/cmake/bindings.cmake +++ b/cmake/bindings.cmake @@ -143,6 +143,7 @@ add_custom_target( COMMAND ${wit_bindgen} c --autodrop-borrows yes + --rename wasm:js/global=webp2 --rename-world webp2 --type-section-suffix __wasi_libc ${CMAKE_SOURCE_DIR}/web/p2/wit diff --git a/libc-bottom-half/headers/public/wasi/__generated_webp2.h b/libc-bottom-half/headers/public/wasi/__generated_webp2.h index 3d97d49e7..095a28e73 100644 --- a/libc-bottom-half/headers/public/wasi/__generated_webp2.h +++ b/libc-bottom-half/headers/public/wasi/__generated_webp2.h @@ -78,14 +78,13 @@ typedef struct { size_t len; } webp2_list_u8_t; -// Imported Functions from `webp2` -extern webp2_own_performance_t webp2_get_global_performance(void); -extern void webp2_report_error(webp2_borrow_error_t throwable); -extern webp2_own_crypto_t webp2_get_global_crypto(void); +// Imported Functions from `wasm:js/global` extern double webp2_method_performance_now(webp2_borrow_performance_t self); extern double webp2_method_get_performance_time_origin(webp2_borrow_performance_t self); +extern webp2_own_performance_t webp2_get_global_performance(void); extern double webp2_static_date_now(void); extern webp2_own_error_t webp2_constructor_error(webp2_string_t *maybe_message, webp2_new_error_options_t *maybe_options); +extern void webp2_report_error(webp2_borrow_error_t throwable); extern webp2_own_uint_8_array_t webp2_constructor_uint_8_array(uint32_t length); extern webp2_own_uint_8_array_t webp2_method_uint_8_array_subarray(webp2_borrow_uint_8_array_t self, uint32_t begin, uint32_t *maybe_end); // TODO(webp2): This is very jank, but basically, the whole design of @@ -99,6 +98,7 @@ extern webp2_own_uint_8_array_t webp2_method_uint_8_array_subarray(webp2_borrow_ // We desperately need to find a better way to do this. Some new // builtin? New web APIs? extern void webp2_method_crypto_get_random_values(webp2_borrow_crypto_t self, webp2_borrow_uint_8_array_t typed_array, webp2_list_u8_t *ret); +extern webp2_own_crypto_t webp2_get_global_crypto(void); // Helper Functions diff --git a/libc-bottom-half/sources/webp2.c b/libc-bottom-half/sources/webp2.c index 654c2cea1..63cb6d4ac 100644 --- a/libc-bottom-half/sources/webp2.c +++ b/libc-bottom-half/sources/webp2.c @@ -3,38 +3,38 @@ #include #include -// Imported Functions from `webp2` +// Imported Functions from `wasm:js/global` -__attribute__((__import_module__("$root"), __import_name__("[get]global-performance"))) -extern int32_t __wasm_import_webp2_get_global_performance(void); - -__attribute__((__import_module__("$root"), __import_name__("report-error"))) -extern void __wasm_import_webp2_report_error(int32_t); - -__attribute__((__import_module__("$root"), __import_name__("[get]global-crypto"))) -extern int32_t __wasm_import_webp2_get_global_crypto(void); - -__attribute__((__import_module__("$root"), __import_name__("[method]performance.now"))) +__attribute__((__import_module__("wasm:js/global"), __import_name__("[method]performance.now"))) extern double __wasm_import_webp2_method_performance_now(int32_t); -__attribute__((__import_module__("$root"), __import_name__("[method][get]performance.time-origin"))) +__attribute__((__import_module__("wasm:js/global"), __import_name__("[method][get]performance.time-origin"))) extern double __wasm_import_webp2_method_get_performance_time_origin(int32_t); -__attribute__((__import_module__("$root"), __import_name__("[static]date.now"))) +__attribute__((__import_module__("wasm:js/global"), __import_name__("[get]global-performance"))) +extern int32_t __wasm_import_webp2_get_global_performance(void); + +__attribute__((__import_module__("wasm:js/global"), __import_name__("[static]date.now"))) extern double __wasm_import_webp2_static_date_now(void); -__attribute__((__import_module__("$root"), __import_name__("[constructor]error"))) +__attribute__((__import_module__("wasm:js/global"), __import_name__("[constructor]error"))) extern int32_t __wasm_import_webp2_constructor_error(int32_t, uint8_t *, size_t, int32_t, int32_t, uint8_t *, size_t); -__attribute__((__import_module__("$root"), __import_name__("[constructor]uint-8-array"))) +__attribute__((__import_module__("wasm:js/global"), __import_name__("report-error"))) +extern void __wasm_import_webp2_report_error(int32_t); + +__attribute__((__import_module__("wasm:js/global"), __import_name__("[constructor]uint-8-array"))) extern int32_t __wasm_import_webp2_constructor_uint_8_array(int32_t); -__attribute__((__import_module__("$root"), __import_name__("[method]uint-8-array.subarray"))) +__attribute__((__import_module__("wasm:js/global"), __import_name__("[method]uint-8-array.subarray"))) extern int32_t __wasm_import_webp2_method_uint_8_array_subarray(int32_t, int32_t, int32_t, int32_t); -__attribute__((__import_module__("$root"), __import_name__("[method]crypto.get-random-values"))) +__attribute__((__import_module__("wasm:js/global"), __import_name__("[method]crypto.get-random-values"))) extern void __wasm_import_webp2_method_crypto_get_random_values(int32_t, int32_t, uint8_t *); +__attribute__((__import_module__("wasm:js/global"), __import_name__("[get]global-crypto"))) +extern int32_t __wasm_import_webp2_get_global_crypto(void); + // Canonical ABI intrinsics __attribute__((__weak__, __export_name__("cabi_realloc"))) @@ -48,7 +48,7 @@ void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) { // Helper Functions -__attribute__((__import_module__("$root"), __import_name__("[resource-drop]performance"))) +__attribute__((__import_module__("wasm:js/global"), __import_name__("[resource-drop]performance"))) extern void __wasm_import_webp2_performance_drop(int32_t handle); void webp2_performance_drop_own(webp2_own_performance_t handle) { @@ -59,7 +59,7 @@ webp2_borrow_performance_t webp2_borrow_performance(webp2_own_performance_t arg) return (webp2_borrow_performance_t) { arg.__handle }; } -__attribute__((__import_module__("$root"), __import_name__("[resource-drop]date"))) +__attribute__((__import_module__("wasm:js/global"), __import_name__("[resource-drop]date"))) extern void __wasm_import_webp2_date_drop(int32_t handle); void webp2_date_drop_own(webp2_own_date_t handle) { @@ -80,7 +80,7 @@ void webp2_new_error_options_free(webp2_new_error_options_t *ptr) { webp2_option_string_free(&ptr->cause); } -__attribute__((__import_module__("$root"), __import_name__("[resource-drop]error"))) +__attribute__((__import_module__("wasm:js/global"), __import_name__("[resource-drop]error"))) extern void __wasm_import_webp2_error_drop(int32_t handle); void webp2_error_drop_own(webp2_own_error_t handle) { @@ -91,7 +91,7 @@ webp2_borrow_error_t webp2_borrow_error(webp2_own_error_t arg) { return (webp2_borrow_error_t) { arg.__handle }; } -__attribute__((__import_module__("$root"), __import_name__("[resource-drop]uint-8-array"))) +__attribute__((__import_module__("wasm:js/global"), __import_name__("[resource-drop]uint-8-array"))) extern void __wasm_import_webp2_uint_8_array_drop(int32_t handle); void webp2_uint_8_array_drop_own(webp2_own_uint_8_array_t handle) { @@ -102,7 +102,7 @@ webp2_borrow_uint_8_array_t webp2_borrow_uint_8_array(webp2_own_uint_8_array_t a return (webp2_borrow_uint_8_array_t) { arg.__handle }; } -__attribute__((__import_module__("$root"), __import_name__("[resource-drop]crypto"))) +__attribute__((__import_module__("wasm:js/global"), __import_name__("[resource-drop]crypto"))) extern void __wasm_import_webp2_crypto_drop(int32_t handle); void webp2_crypto_drop_own(webp2_own_crypto_t handle) { @@ -161,20 +161,6 @@ void webp2_string_free(webp2_string_t *ret) { // Component Adapters -webp2_own_performance_t webp2_get_global_performance(void) { - int32_t ret = __wasm_import_webp2_get_global_performance(); - return (webp2_own_performance_t) { ret }; -} - -void webp2_report_error(webp2_borrow_error_t throwable) { - __wasm_import_webp2_report_error((throwable).__handle); -} - -webp2_own_crypto_t webp2_get_global_crypto(void) { - int32_t ret = __wasm_import_webp2_get_global_crypto(); - return (webp2_own_crypto_t) { ret }; -} - double webp2_method_performance_now(webp2_borrow_performance_t self) { double ret = __wasm_import_webp2_method_performance_now((self).__handle); return ret; @@ -185,6 +171,11 @@ double webp2_method_get_performance_time_origin(webp2_borrow_performance_t self) return ret; } +webp2_own_performance_t webp2_get_global_performance(void) { + int32_t ret = __wasm_import_webp2_get_global_performance(); + return (webp2_own_performance_t) { ret }; +} + double webp2_static_date_now(void) { double ret = __wasm_import_webp2_static_date_now(); return ret; @@ -245,6 +236,10 @@ webp2_own_error_t webp2_constructor_error(webp2_string_t *maybe_message, webp2_n return (webp2_own_error_t) { ret }; } +void webp2_report_error(webp2_borrow_error_t throwable) { + __wasm_import_webp2_report_error((throwable).__handle); +} + webp2_own_uint_8_array_t webp2_constructor_uint_8_array(uint32_t length) { int32_t ret = __wasm_import_webp2_constructor_uint_8_array((int32_t) (length)); return (webp2_own_uint_8_array_t) { ret }; @@ -277,6 +272,11 @@ void webp2_method_crypto_get_random_values(webp2_borrow_crypto_t self, webp2_bor *ret = (webp2_list_u8_t) { (uint8_t*)(*((uint8_t **) (ptr + 0))), (*((size_t*) (ptr + sizeof(void*)))) }; } +webp2_own_crypto_t webp2_get_global_crypto(void) { + int32_t ret = __wasm_import_webp2_get_global_crypto(); + return (webp2_own_crypto_t) { ret }; +} + // Ensure that the *_component_type.o object is linked in extern void __component_type_object_force_link_webp2(void); diff --git a/libc-bottom-half/sources/webp2_component_type.o b/libc-bottom-half/sources/webp2_component_type.o index cb9d8a269dff4208c2691611965a650dfd779c68..0895af9c6ec0417a9f901a36906f12c7ef3e09a6 100644 GIT binary patch delta 402 zcmYk2%}T>S6ouzbCW-yA*0#3RiWUTa7Ofx%;wp-`cU`)eCetQ1$%L5+B?}k6fLO9} z>B`se9R#1jXK>}D-3WJaxSYfJ?)|jBt=BGowwArSiys0vxR~2xf+`6UQ!XMJyUYNB zh)0zcFqa^}bZIIXCd-%h13@Mef{VgbO@mkst_D;HniUJn6Yv3aS#+7`a+1 zk-CbD@d^euA+U6z6|#7y{0!Ad{tNquTi{zT&%iNOD0OqfW1!o2lOb<`qxijT@W5a@#tYLn&H>97O7#)Fdb1z*rf$aVqgZwQufp$2QB hE(CUw*dzHF9JG1{sx7FDt{>(3EaVd!nkaw6AAi#ZW+(sv delta 351 zcmXAk!A`;2OHw8-M)=ZS`1T diff --git a/web/p2/wit/deps/web.wit b/web/p2/wit/deps/web.wit new file mode 100644 index 000000000..50390d08d --- /dev/null +++ b/web/p2/wit/deps/web.wit @@ -0,0 +1,43 @@ +package wasm:js; + +interface global { + resource performance { + now: func() -> f64; + time-origin: get() -> f64; + } + @external-id("performance") + global-performance: get() -> performance; + + resource date { + now: static func() -> f64; + } + + record new-error-options { + cause: option, // TODO(webp2): cause -> option + } + resource error { + constructor(message: option, options: option); // TODO(webp2): message -> option + } + report-error: func(throwable: borrow); + + resource uint-8-array { + constructor(length: u32); + subarray: func(begin: u32, end: option) -> uint-8-array; + } + + resource crypto { + // TODO(webp2): This is very jank, but basically, the whole design of + // Crypto.getRandomValues() doesn't make sense for the component model + // because it modifies its parameter in place. The component model has + // no concept of an out-parameter (nor should it, frankly), but in this + // case we have no choice but to uselessly copy an entire array for it + // to fill "in place" and then copy back to us. By passing a handle to + // a pre-existing Uint8Array we can at least avoid the first copy. + // + // We desperately need to find a better way to do this. Some new + // builtin? New web APIs? + get-random-values: func(typed-array: borrow) -> list; + } + @external-id("crypto") + global-crypto: get() -> crypto; +} diff --git a/web/p2/wit/wasi-libc.wit b/web/p2/wit/wasi-libc.wit index c89bd58e1..b13bc92ca 100644 --- a/web/p2/wit/wasi-libc.wit +++ b/web/p2/wit/wasi-libc.wit @@ -1,43 +1,5 @@ package wasi-libc:webp2; world webp2 { - resource performance { - now: func() -> f64; - time-origin: get() -> f64; - } - @external-id("performance") - import global-performance: get() -> performance; - - resource date { - now: static func() -> f64; - } - - record new-error-options { - cause: option, // TODO(webp2): cause -> option - } - resource error { - constructor(message: option, options: option); // TODO(webp2): message -> option - } - import report-error: func(throwable: borrow); - - resource uint-8-array { - constructor(length: u32); - subarray: func(begin: u32, end: option) -> uint-8-array; - } - - resource crypto { - // TODO(webp2): This is very jank, but basically, the whole design of - // Crypto.getRandomValues() doesn't make sense for the component model - // because it modifies its parameter in place. The component model has - // no concept of an out-parameter (nor should it, frankly), but in this - // case we have no choice but to uselessly copy an entire array for it - // to fill "in place" and then copy back to us. By passing a handle to - // a pre-existing Uint8Array we can at least avoid the first copy. - // - // We desperately need to find a better way to do this. Some new - // builtin? New web APIs? - get-random-values: func(typed-array: borrow) -> list; - } - @external-id("crypto") - import global-crypto: get() -> crypto; + import wasm:js/global; } From 8825299e2375d452e3f2b74f1912c0027f66babb Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Fri, 9 Oct 2026 12:04:17 -0500 Subject: [PATCH 35/96] Use newer wasm-tools in tests --- cmake/wasm-tools.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/wasm-tools.cmake b/cmake/wasm-tools.cmake index 593bb0044..648285380 100644 --- a/cmake/wasm-tools.cmake +++ b/cmake/wasm-tools.cmake @@ -5,7 +5,7 @@ if (NOT WASM_TOOLS_EXECUTABLE) ba_download( wasm-tools "https://github.com/bytecodealliance/wasm-tools" - "1.257.0" + "1.261.0" ) ExternalProject_Get_Property(wasm-tools SOURCE_DIR) set(wasm_tools "${SOURCE_DIR}/wasm-tools") From f6ef78937ccf6a8a8a4db3eb5178b47a337a699d Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Fri, 9 Oct 2026 18:13:56 -0500 Subject: [PATCH 36/96] WIP stdio --- cmake/bindings.cmake | 1 + .../headers/public/wasi/__generated_webp2.h | 3 + libc-bottom-half/sources/webp2.c | 9 + .../sources/webp2_component_type.o | Bin 854 -> 895 bytes .../sources/webp2_file_descriptors.c | 180 +++++++++++++++--- libc-top-half/musl/src/stdio/__stdout_write.c | 5 +- web/p2/wit/deps/web.wit | 6 + web/p2/wit/wasi-libc.wit | 1 + 8 files changed, 180 insertions(+), 25 deletions(-) diff --git a/cmake/bindings.cmake b/cmake/bindings.cmake index b3972ff1c..88cc06e12 100644 --- a/cmake/bindings.cmake +++ b/cmake/bindings.cmake @@ -144,6 +144,7 @@ add_custom_target( ${wit_bindgen} c --autodrop-borrows yes --rename wasm:js/global=webp2 + --rename wasm:js/console=webp2_console --rename-world webp2 --type-section-suffix __wasi_libc ${CMAKE_SOURCE_DIR}/web/p2/wit diff --git a/libc-bottom-half/headers/public/wasi/__generated_webp2.h b/libc-bottom-half/headers/public/wasi/__generated_webp2.h index 095a28e73..c39d338dd 100644 --- a/libc-bottom-half/headers/public/wasi/__generated_webp2.h +++ b/libc-bottom-half/headers/public/wasi/__generated_webp2.h @@ -100,6 +100,9 @@ extern webp2_own_uint_8_array_t webp2_method_uint_8_array_subarray(webp2_borrow_ extern void webp2_method_crypto_get_random_values(webp2_borrow_crypto_t self, webp2_borrow_uint_8_array_t typed_array, webp2_list_u8_t *ret); extern webp2_own_crypto_t webp2_get_global_crypto(void); +// Imported Functions from `wasm:js/console` +extern void webp2_console_log(webp2_string_t *msg); + // Helper Functions extern void webp2_performance_drop_own(webp2_own_performance_t handle); diff --git a/libc-bottom-half/sources/webp2.c b/libc-bottom-half/sources/webp2.c index 63cb6d4ac..f94d07e10 100644 --- a/libc-bottom-half/sources/webp2.c +++ b/libc-bottom-half/sources/webp2.c @@ -35,6 +35,11 @@ extern void __wasm_import_webp2_method_crypto_get_random_values(int32_t, int32_t __attribute__((__import_module__("wasm:js/global"), __import_name__("[get]global-crypto"))) extern int32_t __wasm_import_webp2_get_global_crypto(void); +// Imported Functions from `wasm:js/console` + +__attribute__((__import_module__("wasm:js/console"), __import_name__("log"))) +extern void __wasm_import_webp2_console_log(uint8_t *, size_t); + // Canonical ABI intrinsics __attribute__((__weak__, __export_name__("cabi_realloc"))) @@ -277,6 +282,10 @@ webp2_own_crypto_t webp2_get_global_crypto(void) { return (webp2_own_crypto_t) { ret }; } +void webp2_console_log(webp2_string_t *msg) { + __wasm_import_webp2_console_log((uint8_t *) (*msg).ptr, (*msg).len); +} + // Ensure that the *_component_type.o object is linked in extern void __component_type_object_force_link_webp2(void); diff --git a/libc-bottom-half/sources/webp2_component_type.o b/libc-bottom-half/sources/webp2_component_type.o index 0895af9c6ec0417a9f901a36906f12c7ef3e09a6..e6a866bf04cdf3acdad368f8dc06bf405684aec8 100644 GIT binary patch delta 70 zcmcb{_MdHnB;%@yQmz~mSs5Lf7#&$Qrh71HGCDCaIxsTl7N-|8GO#c(=j5j|GB7jn amnRnIT4fdMC+FuC=jWudGEQF4v>X6%!xCHo delta 28 icmey*c8zU&rsV*HY6$ND diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index 475cf301e..b299e14f5 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -1,29 +1,179 @@ -// All things file-descriptory are stubbed out on the web, since there is no -// comparable model of files and streams. +// File descriptors are mostly stubbed out on the web, since there is no +// comparable model of files and streams. However, stdout and stderr are +// supported by routing to console.log, and stdin is supported by immediately +// returning EOF. +#include "features.h" #include "sys/uio.h" #include "wasi/report-error.h" +#include <__errno_values.h> +#include +#include #include #include #include #include #include +#include // unistd.h defines an `lseek` macro that would mangle the definition below. #undef lseek -int __dup3(int fd, int newfd, int flags) { WEBP2_UNSUPPORTED("dup3"); } +// ============================================================================ +// Basic stdio implementation -int __isatty(int fd) { WEBP2_UNSUPPORTED("isatty"); } +#define LINE_BUF_SIZE 128 // TODO(webp2): Increase size + +typedef struct line_buffer_t { + size_t len; + uint8_t buf[LINE_BUF_SIZE]; +} line_buffer_t; + +static line_buffer_t line_buffers[2]; + +static bool fd_closed[3]; + +static void flush_line_buffer(line_buffer_t* buf) { + webp2_string_t out = (webp2_string_t){ .ptr = buf->buf, .len = buf->len }; + webp2_console_log(&out); + buf->len = 0; +} + +static bool iovecs_ok(const struct iovec *iov, int iovcnt, size_t *total_size) { + // Check if the total size of all iovecs would overflow an ssize_t + *total_size = 0; + for (int i = 0; i < iovcnt; i++) { + const struct iovec* v = &iov[i]; + if (v->iov_len > SSIZE_MAX || *total_size < SSIZE_MAX - v->iov_len) { + return false; + } + *total_size += v->iov_len; + } + return true; +} + +int __isatty(int fd) { + if (fd < STDIN_FILENO || STDERR_FILENO < fd || fd_closed[fd]) { + errno = EBADF; + return 0; + } + errno = ENOTTY; + return 0; +} +weak_alias(__isatty, isatty); + +int close(int fd) { + if (fd < STDIN_FILENO || STDERR_FILENO < fd || fd_closed[fd]) { + errno = EBADF; + return -1; + } + fd_closed[fd] = true; + return 0; +} + +ssize_t read(int fildes, void *buf, size_t nbyte) { + if (nbyte > SSIZE_MAX) { + errno = EINVAL; + return -1; + } + + switch (fildes) { + case STDIN_FILENO: + if (fd_closed[fildes]) { + errno = EBADF; + return -1; + } + return 0; // immediate EOF + default: + errno = EBADF; + return -1; + } +} + +ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { + if (iovcnt < 0 || IOV_MAX < iovcnt) { + errno = EINVAL; + return -1; + } + + size_t total_size; + if (!iovecs_ok(iov, iovcnt, &total_size)) { + errno = EINVAL; + return -1; + } + for (int i = 0; i < iovcnt; i++) { + int res = read(fildes, iov->iov_base, iov->iov_len); + if (res < 0) { + // errno already set + return res; + } + } + return total_size; +} + +ssize_t write(int fildes, const void *buf, size_t nbyte) { + if (nbyte > SSIZE_MAX) { + errno = EINVAL; + return -1; + } + + switch (fildes) { + case STDOUT_FILENO: + case STDERR_FILENO: + if (fd_closed[fildes]) { + errno = EBADF; + return -1; + } + line_buffer_t* line_buffer = &line_buffers[fildes-1]; + for (size_t i = 0; i < nbyte; i++) { + // TODO(webp2): UTF-8 + uint8_t c = ((uint8_t*)buf)[i]; + // TODO(webp2): Handle \r + if (c == '\n' || line_buffer->len >= LINE_BUF_SIZE) { + flush_line_buffer(line_buffer); + } + line_buffer->buf[line_buffer->len] = c; + line_buffer->len++; + } + return nbyte; + default: + errno = EBADF; + return -1; + } +} + +ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { + if (iovcnt < 0 || IOV_MAX < iovcnt) { + errno = EINVAL; + return -1; + } + + size_t total_size; + if (!iovecs_ok(iov, iovcnt, &total_size)) { + errno = EINVAL; + return -1; + } + for (int i = 0; i < iovcnt; i++) { + int res = write(fildes, iov->iov_base, iov->iov_len); + if (res < 0) { + // errno already set + return res; + } + } + return total_size; +} + +// ============================================================================ +// Stubs + +int __dup3(int fd, int newfd, int flags) { WEBP2_UNSUPPORTED("__dup3"); } off_t __lseek(int fildes, off_t offset, int whence) { - WEBP2_UNSUPPORTED("lseek"); + WEBP2_UNSUPPORTED("__lseek"); } off_t __wasilibc_tell(int fd) { WEBP2_UNSUPPORTED("__wasilibc_tell"); } -int close(int fd) { WEBP2_UNSUPPORTED("close"); } - int dup(int fd) { WEBP2_UNSUPPORTED("dup"); } int dup2(int fd, int newfd) { WEBP2_UNSUPPORTED("dup2"); } @@ -36,8 +186,6 @@ int fstat(int fildes, struct stat *buf) { WEBP2_UNSUPPORTED("fstat"); } int ioctl(int fildes, int request, ...) { WEBP2_UNSUPPORTED("ioctl"); } -int isatty(int fd) { WEBP2_UNSUPPORTED("isatty"); } - off_t lseek(int fildes, off_t offset, int whence) { WEBP2_UNSUPPORTED("lseek"); } @@ -61,21 +209,7 @@ int pselect(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, WEBP2_UNSUPPORTED("pselect"); } -ssize_t read(int fildes, void *buf, size_t nbyte) { WEBP2_UNSUPPORTED("read"); } - -ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { - WEBP2_UNSUPPORTED("readv"); -} - int select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, fd_set *restrict errorfds, struct timeval *restrict timeout) { WEBP2_UNSUPPORTED("select"); } - -ssize_t write(int fildes, const void *buf, size_t nbyte) { - WEBP2_UNSUPPORTED("write"); -} - -ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { - WEBP2_UNSUPPORTED("writev"); -} diff --git a/libc-top-half/musl/src/stdio/__stdout_write.c b/libc-top-half/musl/src/stdio/__stdout_write.c index 8190ec5b4..e806ded5c 100644 --- a/libc-top-half/musl/src/stdio/__stdout_write.c +++ b/libc-top-half/musl/src/stdio/__stdout_write.c @@ -13,9 +13,10 @@ size_t __stdout_write(FILE *f, const unsigned char *buf, size_t len) f->write = __stdio_write; #ifdef __wasilibc_unmodified_upstream // use isatty rather than manual ioctl if (!(f->flags & F_SVB) && __syscall(SYS_ioctl, f->fd, TIOCGWINSZ, &wsz)) -#else + f->lbf = -1; +#elif !defined(__webp2__) if (!(f->flags & F_SVB) && !__isatty(f->fd)) -#endif f->lbf = -1; +#endif return __stdio_write(f, buf, len); } diff --git a/web/p2/wit/deps/web.wit b/web/p2/wit/deps/web.wit index 50390d08d..f543154c9 100644 --- a/web/p2/wit/deps/web.wit +++ b/web/p2/wit/deps/web.wit @@ -1,5 +1,11 @@ package wasm:js; +// TODO(webp2): If we had nested interfaces we could avoid this external-id. +@external-id("wasm:js/global/console") +interface console { + log: func(msg: string); +} + interface global { resource performance { now: func() -> f64; diff --git a/web/p2/wit/wasi-libc.wit b/web/p2/wit/wasi-libc.wit index b13bc92ca..e8a601c1e 100644 --- a/web/p2/wit/wasi-libc.wit +++ b/web/p2/wit/wasi-libc.wit @@ -2,4 +2,5 @@ package wasi-libc:webp2; world webp2 { import wasm:js/global; + import wasm:js/console; } From 13a3808b66863680790bb47ff3d2366eec887220 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Fri, 9 Oct 2026 18:18:34 -0500 Subject: [PATCH 37/96] Move the console's external-id to the import --- libc-bottom-half/sources/webp2_component_type.o | Bin 895 -> 920 bytes web/p2/wit/deps/web.wit | 2 -- web/p2/wit/wasi-libc.wit | 4 ++++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/libc-bottom-half/sources/webp2_component_type.o b/libc-bottom-half/sources/webp2_component_type.o index e6a866bf04cdf3acdad368f8dc06bf405684aec8..3269376b3e017ed51aff93cad317170bd9f3754d 100644 GIT binary patch delta 56 zcmey*HiLbFB;(gWfkiu=jRpY=cF<+iNQqDbMliCb0(i- G+6Vw6^%La) delta 31 ncmbQi{-14vB;%@yQm%{>HzuSqaWe3iCl=>gWff0;&a@E#sXYq^ diff --git a/web/p2/wit/deps/web.wit b/web/p2/wit/deps/web.wit index f543154c9..e2d449a50 100644 --- a/web/p2/wit/deps/web.wit +++ b/web/p2/wit/deps/web.wit @@ -1,7 +1,5 @@ package wasm:js; -// TODO(webp2): If we had nested interfaces we could avoid this external-id. -@external-id("wasm:js/global/console") interface console { log: func(msg: string); } diff --git a/web/p2/wit/wasi-libc.wit b/web/p2/wit/wasi-libc.wit index e8a601c1e..7dfe9d05a 100644 --- a/web/p2/wit/wasi-libc.wit +++ b/web/p2/wit/wasi-libc.wit @@ -2,5 +2,9 @@ package wasi-libc:webp2; world webp2 { import wasm:js/global; + + // TODO(webp2): If we had nested interfaces we could avoid this external-id + // by putting console directly on global. + @external-id("wasm:js/global/console") import wasm:js/console; } From c9dd7b67e24409b8bed16525829c6026ca247252 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Fri, 9 Oct 2026 23:56:58 -0500 Subject: [PATCH 38/96] Fix stdio bugs --- .../sources/webp2_file_descriptors.c | 108 +++++++++++------- 1 file changed, 65 insertions(+), 43 deletions(-) diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index b299e14f5..a7b5d505e 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -22,7 +22,7 @@ // ============================================================================ // Basic stdio implementation -#define LINE_BUF_SIZE 128 // TODO(webp2): Increase size +#define LINE_BUF_SIZE 4096 typedef struct line_buffer_t { size_t len; @@ -39,15 +39,29 @@ static void flush_line_buffer(line_buffer_t* buf) { buf->len = 0; } +static bool is_readable(int fd) { + return fd == STDIN_FILENO; +} + +static bool is_writable(int fd) { + return fd == STDOUT_FILENO || fd == STDERR_FILENO; +} + static bool iovecs_ok(const struct iovec *iov, int iovcnt, size_t *total_size) { // Check if the total size of all iovecs would overflow an ssize_t - *total_size = 0; + size_t _total_size = 0; + if (total_size) { + *total_size = 0; + } for (int i = 0; i < iovcnt; i++) { const struct iovec* v = &iov[i]; - if (v->iov_len > SSIZE_MAX || *total_size < SSIZE_MAX - v->iov_len) { + if (v->iov_len > SSIZE_MAX || _total_size > SSIZE_MAX - v->iov_len) { return false; } - *total_size += v->iov_len; + _total_size += v->iov_len; + } + if (total_size) { + *total_size = _total_size; } return true; } @@ -68,6 +82,12 @@ int close(int fd) { return -1; } fd_closed[fd] = true; + if (is_writable(fd)) { + line_buffer_t *buf = &line_buffers[fd-1]; + if (buf->len > 0) { + flush_line_buffer(buf); + } + } return 0; } @@ -76,18 +96,11 @@ ssize_t read(int fildes, void *buf, size_t nbyte) { errno = EINVAL; return -1; } - - switch (fildes) { - case STDIN_FILENO: - if (fd_closed[fildes]) { - errno = EBADF; - return -1; - } - return 0; // immediate EOF - default: - errno = EBADF; - return -1; + if (!is_readable(fildes) || fd_closed[fildes]) { + errno = EBADF; + return -1; } + return 0; // immediate EOF; } ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { @@ -95,20 +108,29 @@ ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { errno = EINVAL; return -1; } - - size_t total_size; - if (!iovecs_ok(iov, iovcnt, &total_size)) { + if (!iovecs_ok(iov, iovcnt, NULL)) { errno = EINVAL; return -1; } + if (!is_readable(fildes) || fd_closed[fildes]) { + errno = EBADF; + return -1; + } + + size_t bytes_read = 0; for (int i = 0; i < iovcnt; i++) { - int res = read(fildes, iov->iov_base, iov->iov_len); + const struct iovec *v = &iov[i]; + ssize_t res = read(fildes, v->iov_base, v->iov_len); if (res < 0) { // errno already set return res; } + bytes_read += res; + if ((size_t)res < v->iov_len) { + break; + } } - return total_size; + return bytes_read; } ssize_t write(int fildes, const void *buf, size_t nbyte) { @@ -116,30 +138,25 @@ ssize_t write(int fildes, const void *buf, size_t nbyte) { errno = EINVAL; return -1; } + if (!is_writable(fildes) || fd_closed[fildes]) { + errno = EBADF; + return -1; + } - switch (fildes) { - case STDOUT_FILENO: - case STDERR_FILENO: - if (fd_closed[fildes]) { - errno = EBADF; - return -1; - } - line_buffer_t* line_buffer = &line_buffers[fildes-1]; - for (size_t i = 0; i < nbyte; i++) { - // TODO(webp2): UTF-8 - uint8_t c = ((uint8_t*)buf)[i]; - // TODO(webp2): Handle \r - if (c == '\n' || line_buffer->len >= LINE_BUF_SIZE) { - flush_line_buffer(line_buffer); - } - line_buffer->buf[line_buffer->len] = c; - line_buffer->len++; - } - return nbyte; - default: - errno = EBADF; - return -1; + line_buffer_t* line_buffer = &line_buffers[fildes-1]; + for (size_t i = 0; i < nbyte; i++) { + // TODO(webp2): UTF-8 + uint8_t c = ((uint8_t*)buf)[i]; + // TODO(webp2): Handle \r + if (c == '\n' || line_buffer->len >= LINE_BUF_SIZE) { + flush_line_buffer(line_buffer); + } + if (c != '\n') { + line_buffer->buf[line_buffer->len] = c; + line_buffer->len++; + } } + return nbyte; } ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { @@ -147,6 +164,10 @@ ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { errno = EINVAL; return -1; } + if (!is_writable(fildes) || fd_closed[fildes]) { + errno = EBADF; + return -1; + } size_t total_size; if (!iovecs_ok(iov, iovcnt, &total_size)) { @@ -154,7 +175,8 @@ ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { return -1; } for (int i = 0; i < iovcnt; i++) { - int res = write(fildes, iov->iov_base, iov->iov_len); + const struct iovec *v = &iov[i]; + int res = write(fildes, v->iov_base, v->iov_len); if (res < 0) { // errno already set return res; From aa1ecfd0fc4250a8a0f0f62e9fd09a20b9cc1c3d Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 10 Oct 2026 20:53:34 -0500 Subject: [PATCH 39/96] Only emit valid UTF-8 on stdout/stderr --- .../sources/webp2_file_descriptors.c | 119 ++++++++++++++---- 1 file changed, 98 insertions(+), 21 deletions(-) diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index a7b5d505e..64e84f0b8 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -25,24 +25,109 @@ #define LINE_BUF_SIZE 4096 typedef struct line_buffer_t { - size_t len; + int32_t len; + int32_t codepoint_start; // -1 means no current code point + int32_t codepoint_remaining; uint8_t buf[LINE_BUF_SIZE]; } line_buffer_t; -static line_buffer_t line_buffers[2]; +static line_buffer_t line_buffers[2] = { + (line_buffer_t){.codepoint_start = -1}, + (line_buffer_t){.codepoint_start = -1}, +}; static bool fd_closed[3]; -static void flush_line_buffer(line_buffer_t* buf) { - webp2_string_t out = (webp2_string_t){ .ptr = buf->buf, .len = buf->len }; +static void line_buffer_replchar(line_buffer_t *lb); + +static void line_buffer_flush(line_buffer_t *lb, bool final) { + if (final && lb->codepoint_start >= 0) { + // Closing with un-terminated sequence. Rewind and emit replacement + // character instead. + lb->len = lb->codepoint_start; + line_buffer_replchar(lb); + } + + webp2_string_t out = (webp2_string_t){.ptr = lb->buf, .len = lb->len}; webp2_console_log(&out); - buf->len = 0; + lb->len = 0; + lb->codepoint_start = -1; + lb->codepoint_remaining = 0; } -static bool is_readable(int fd) { - return fd == STDIN_FILENO; +static void line_buffer_replchar(line_buffer_t *lb) { + if (LINE_BUF_SIZE - lb->len < 3) { + line_buffer_flush(lb, false); + } + lb->buf[lb->len++] = 0xEF; + lb->buf[lb->len++] = 0xBF; + lb->buf[lb->len++] = 0xBD; +} + +// Emits a single byte to the line buffer, fixing up UTF-8 problems as it goes. +static void line_buffer_byte(line_buffer_t *lb, uint8_t b) { + bool is_1byte = (b & 0x80) == 0; + bool is_continuation = (b & 0xC0) == 0x80; + bool is_2byte = (b & 0xE0) == 0xC0; + bool is_3byte = (b & 0xF0) == 0xE0; + bool is_4byte = (b & 0xF8) == 0xF0; + + if (!is_continuation && lb->codepoint_start >= 0) { + // Unexpected non-continuation byte; rewind, emit replacement for the + // aborted sequence, and continue with the current byte + lb->len = lb->codepoint_start; + lb->codepoint_start = -1; + line_buffer_replchar(lb); + } + + if (is_1byte) { + if (b == '\n' || lb->len + 1 > LINE_BUF_SIZE) { + line_buffer_flush(lb, false); + } + if (b != '\n') { + lb->buf[lb->len++] = b; + } + } else if (is_2byte || is_3byte || is_4byte) { + int32_t codepoint_length = is_2byte ? 2 : (is_3byte ? 3 : 4); + if (lb->len + codepoint_length > LINE_BUF_SIZE) { + line_buffer_flush(lb, false); + } + lb->codepoint_start = lb->len; + lb->codepoint_remaining = codepoint_length - 1; + lb->buf[lb->len++] = b; + } else if (is_continuation) { + if (lb->codepoint_start < 0) { + // Unexpected continuation byte; emit a replacement char instead + line_buffer_replchar(lb); + } else { + // Expected continuation byte + lb->buf[lb->len++] = b; + lb->codepoint_remaining -= 1; + + // If done, validate overlong / out-of-range + if (lb->codepoint_remaining <= 0) { + uint8_t first = lb->buf[lb->codepoint_start]; + uint8_t second = lb->buf[lb->codepoint_start + 1]; + bool overlong = first == 0xC0 || first == 0xC1 || + (first == 0xE0 && second < 0xA0) || + (first == 0xF0 && second < 0x90); + bool toobig = (first == 0xF4 && second >= 0x90) || 0xF5 <= first; + bool surrogate = (first == 0xED && second >= 0xA0); + if (overlong || toobig || surrogate) { + // Rewind to start and write replacement character + lb->len = lb->codepoint_start; + line_buffer_replchar(lb); + } + lb->codepoint_start = -1; + } + } + } else { + line_buffer_replchar(lb); + } } +static bool is_readable(int fd) { return fd == STDIN_FILENO; } + static bool is_writable(int fd) { return fd == STDOUT_FILENO || fd == STDERR_FILENO; } @@ -54,7 +139,7 @@ static bool iovecs_ok(const struct iovec *iov, int iovcnt, size_t *total_size) { *total_size = 0; } for (int i = 0; i < iovcnt; i++) { - const struct iovec* v = &iov[i]; + const struct iovec *v = &iov[i]; if (v->iov_len > SSIZE_MAX || _total_size > SSIZE_MAX - v->iov_len) { return false; } @@ -83,9 +168,9 @@ int close(int fd) { } fd_closed[fd] = true; if (is_writable(fd)) { - line_buffer_t *buf = &line_buffers[fd-1]; + line_buffer_t *buf = &line_buffers[fd - 1]; if (buf->len > 0) { - flush_line_buffer(buf); + line_buffer_flush(buf, true); } } return 0; @@ -143,19 +228,11 @@ ssize_t write(int fildes, const void *buf, size_t nbyte) { return -1; } - line_buffer_t* line_buffer = &line_buffers[fildes-1]; + line_buffer_t *line_buffer = &line_buffers[fildes - 1]; for (size_t i = 0; i < nbyte; i++) { - // TODO(webp2): UTF-8 - uint8_t c = ((uint8_t*)buf)[i]; - // TODO(webp2): Handle \r - if (c == '\n' || line_buffer->len >= LINE_BUF_SIZE) { - flush_line_buffer(line_buffer); - } - if (c != '\n') { - line_buffer->buf[line_buffer->len] = c; - line_buffer->len++; - } + line_buffer_byte(line_buffer, ((uint8_t *)buf)[i]); } + return nbyte; } From 57d1b3189c3103b3c50dabc65d64dad931a3623a Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 12 Oct 2026 12:42:37 -0500 Subject: [PATCH 40/96] Implement remaining stdio-friendly stubs --- .../sources/webp2_file_descriptors.c | 73 +++++++++++++++---- 1 file changed, 57 insertions(+), 16 deletions(-) diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index 64e84f0b8..edfc88ad7 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -8,8 +8,10 @@ #include "wasi/report-error.h" #include <__errno_values.h> #include +#include #include #include +#include #include #include #include @@ -126,6 +128,10 @@ static void line_buffer_byte(line_buffer_t *lb, uint8_t b) { } } +static bool fd_ok(int fd) { + return fd >= STDIN_FILENO && STDERR_FILENO <= fd && !fd_closed[fd]; +} + static bool is_readable(int fd) { return fd == STDIN_FILENO; } static bool is_writable(int fd) { @@ -152,7 +158,7 @@ static bool iovecs_ok(const struct iovec *iov, int iovcnt, size_t *total_size) { } int __isatty(int fd) { - if (fd < STDIN_FILENO || STDERR_FILENO < fd || fd_closed[fd]) { + if (!fd_ok(fd)) { errno = EBADF; return 0; } @@ -161,8 +167,27 @@ int __isatty(int fd) { } weak_alias(__isatty, isatty); +off_t __lseek(int fildes, off_t offset, int whence) { + if (!fd_ok(fildes)) { + errno = EBADF; + return -1; + } + errno = ESPIPE; + return -1; +} +weak_alias(__lseek, lseek); + +off_t __wasilibc_tell(int fd) { + if (!fd_ok(fd)) { + errno = EBADF; + return -1; + } + errno = ESPIPE; + return -1; +} + int close(int fd) { - if (fd < STDIN_FILENO || STDERR_FILENO < fd || fd_closed[fd]) { + if (!fd_ok(fd)) { errno = EBADF; return -1; } @@ -176,6 +201,25 @@ int close(int fd) { return 0; } +int fcntl(int fildes, int cmd, ...) { + if (!fd_ok(fildes)) { + errno = EBADF; + return -1; + } + switch (cmd) { + case F_GETFD: + case F_SETFD: + // Don't bother setting FD_CLOEXEC at all. It's totally irrelevant but + // there's no reason to crash if trying to set it. + return 0; + case F_GETFL: + return is_readable(fildes) ? O_RDONLY : O_WRONLY; + default: + errno = EINVAL; + return -1; + } +} + ssize_t read(int fildes, void *buf, size_t nbyte) { if (nbyte > SSIZE_MAX) { errno = EINVAL; @@ -262,33 +306,30 @@ ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { return total_size; } +int fstat(int fildes, struct stat *buf) { + if (!fd_ok(fildes)) { + errno = EBADF; + return -1; + } + memset(buf, 0, sizeof(*buf)); + buf->st_mode = S_IFCHR; + return 0; +} + + // ============================================================================ // Stubs int __dup3(int fd, int newfd, int flags) { WEBP2_UNSUPPORTED("__dup3"); } -off_t __lseek(int fildes, off_t offset, int whence) { - WEBP2_UNSUPPORTED("__lseek"); -} - -off_t __wasilibc_tell(int fd) { WEBP2_UNSUPPORTED("__wasilibc_tell"); } - int dup(int fd) { WEBP2_UNSUPPORTED("dup"); } int dup2(int fd, int newfd) { WEBP2_UNSUPPORTED("dup2"); } int dup3(int fd, int newfd, int flags) { WEBP2_UNSUPPORTED("dup3"); } -int fcntl(int fildes, int cmd, ...) { WEBP2_UNSUPPORTED("fcntl"); } - -int fstat(int fildes, struct stat *buf) { WEBP2_UNSUPPORTED("fstat"); } - int ioctl(int fildes, int request, ...) { WEBP2_UNSUPPORTED("ioctl"); } -off_t lseek(int fildes, off_t offset, int whence) { - WEBP2_UNSUPPORTED("lseek"); -} - int pipe(int fd[2]) { WEBP2_UNSUPPORTED("pipe"); } int pipe2(int fd[2], int flags) { WEBP2_UNSUPPORTED("pipe2"); } From 8a2d6e3210b0e26e48aa6ee5d828c164c1900a1e Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 12 Oct 2026 13:44:01 -0500 Subject: [PATCH 41/96] Flush stdout/stderr on exit --- libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c | 1 + libc-bottom-half/sources/__wasilibc_report_error.c | 1 + libc-bottom-half/sources/abort.c | 4 ++++ libc-bottom-half/sources/webp2_file_descriptors.c | 11 ++++++++++- libc-top-half/headers/private/wasi/report-error.h | 10 +++++++--- 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c index 31d255b7b..0bb496100 100644 --- a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c +++ b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c @@ -22,6 +22,7 @@ noreturn void _Exit(int status) { } // We really don't have any other way to halt execution except to trap. Quite // unfortunate that this will manifest as a RuntimeError. + __wasilibc_webp2_flush_stdio(); __builtin_trap(); #else # error "Unknown WASI version" diff --git a/libc-bottom-half/sources/__wasilibc_report_error.c b/libc-bottom-half/sources/__wasilibc_report_error.c index 7db377c0a..b360d52c3 100644 --- a/libc-bottom-half/sources/__wasilibc_report_error.c +++ b/libc-bottom-half/sources/__wasilibc_report_error.c @@ -12,6 +12,7 @@ void __wasilibc_report_error(const char *message) { #ifdef __webp2__ void __wasilibc_webp2_report_error(const char *message) { + __wasilibc_webp2_flush_stdio(); webp2_string_t msg; webp2_string_set(&msg, message); webp2_own_error_t err = webp2_constructor_error(&msg, NULL); diff --git a/libc-bottom-half/sources/abort.c b/libc-bottom-half/sources/abort.c index 9db9e1bbd..869eb175f 100644 --- a/libc-bottom-half/sources/abort.c +++ b/libc-bottom-half/sources/abort.c @@ -1,6 +1,10 @@ +#include "wasi/report-error.h" #include void abort(void) { // wasm doesn't support signals, so just trap to halt the program. +#ifdef __webp2__ + __wasilibc_webp2_flush_stdio(); +#endif __builtin_trap(); } diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index edfc88ad7..c4e4d3783 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -129,7 +129,7 @@ static void line_buffer_byte(line_buffer_t *lb, uint8_t b) { } static bool fd_ok(int fd) { - return fd >= STDIN_FILENO && STDERR_FILENO <= fd && !fd_closed[fd]; + return STDIN_FILENO <= fd && fd <= STDERR_FILENO && !fd_closed[fd]; } static bool is_readable(int fd) { return fd == STDIN_FILENO; } @@ -186,6 +186,15 @@ off_t __wasilibc_tell(int fd) { return -1; } +void __wasilibc_webp2_flush_stdio(void) { + if (line_buffers[0].len > 0) { + line_buffer_flush(&line_buffers[0], true); + } + if (line_buffers[1].len > 0) { + line_buffer_flush(&line_buffers[1], true); + } +} + int close(int fd) { if (!fd_ok(fd)) { errno = EBADF; diff --git a/libc-top-half/headers/private/wasi/report-error.h b/libc-top-half/headers/private/wasi/report-error.h index af26b5e13..737bb1019 100644 --- a/libc-top-half/headers/private/wasi/report-error.h +++ b/libc-top-half/headers/private/wasi/report-error.h @@ -8,14 +8,18 @@ hidden void __wasilibc_report_error(const char* message); #ifdef __webp2__ +// Flushes any remaining content in stdout/stderr. +void __wasilibc_webp2_flush_stdio(); + +// Reports an error to the console using webp2 bindings. +void __wasilibc_webp2_report_error(const char* message); + #define WEBP2_UNSUPPORTED(name) \ do { \ + __wasilibc_webp2_flush_stdio(); \ __wasilibc_report_error(name " is not supported on the web"); \ __builtin_trap(); \ } while (0) - -// Reports an error to the console using webp2 bindings. -void __wasilibc_webp2_report_error(const char* message); #endif #endif From 67c1bc18f0540c223b6eef9b892953bcd1419d4a Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 12 Oct 2026 14:02:40 -0500 Subject: [PATCH 42/96] Simplify stdio flushing logic --- .../sources/webp2_file_descriptors.c | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index c4e4d3783..fe1346524 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -7,6 +7,7 @@ #include "sys/uio.h" #include "wasi/report-error.h" #include <__errno_values.h> +#include #include #include #include @@ -42,11 +43,20 @@ static bool fd_closed[3]; static void line_buffer_replchar(line_buffer_t *lb); -static void line_buffer_flush(line_buffer_t *lb, bool final) { - if (final && lb->codepoint_start >= 0) { - // Closing with un-terminated sequence. Rewind and emit replacement - // character instead. - lb->len = lb->codepoint_start; +static void line_buffer_rewind_current_codepoint(line_buffer_t *lb) { + assert(lb->codepoint_start >= 0); + lb->len = lb->codepoint_start; + lb->codepoint_start = -1; +} + +static void line_buffer_flush(line_buffer_t *lb) { + if (lb->codepoint_start >= 0) { + // Cannot flush with a code point in progress because we have to hand it + // off to console.log. But this should only happen when closing the stream + // early, because we always flush early when starting a code point that + // would overfill the buffer. Rewind and emit a replacement character to + // close things off. + line_buffer_rewind_current_codepoint(lb); line_buffer_replchar(lb); } @@ -58,8 +68,8 @@ static void line_buffer_flush(line_buffer_t *lb, bool final) { } static void line_buffer_replchar(line_buffer_t *lb) { - if (LINE_BUF_SIZE - lb->len < 3) { - line_buffer_flush(lb, false); + if (lb->len + 3 > LINE_BUF_SIZE) { + line_buffer_flush(lb); } lb->buf[lb->len++] = 0xEF; lb->buf[lb->len++] = 0xBF; @@ -77,14 +87,13 @@ static void line_buffer_byte(line_buffer_t *lb, uint8_t b) { if (!is_continuation && lb->codepoint_start >= 0) { // Unexpected non-continuation byte; rewind, emit replacement for the // aborted sequence, and continue with the current byte - lb->len = lb->codepoint_start; - lb->codepoint_start = -1; + line_buffer_rewind_current_codepoint(lb); line_buffer_replchar(lb); } if (is_1byte) { if (b == '\n' || lb->len + 1 > LINE_BUF_SIZE) { - line_buffer_flush(lb, false); + line_buffer_flush(lb); } if (b != '\n') { lb->buf[lb->len++] = b; @@ -92,7 +101,7 @@ static void line_buffer_byte(line_buffer_t *lb, uint8_t b) { } else if (is_2byte || is_3byte || is_4byte) { int32_t codepoint_length = is_2byte ? 2 : (is_3byte ? 3 : 4); if (lb->len + codepoint_length > LINE_BUF_SIZE) { - line_buffer_flush(lb, false); + line_buffer_flush(lb); } lb->codepoint_start = lb->len; lb->codepoint_remaining = codepoint_length - 1; @@ -117,7 +126,7 @@ static void line_buffer_byte(line_buffer_t *lb, uint8_t b) { bool surrogate = (first == 0xED && second >= 0xA0); if (overlong || toobig || surrogate) { // Rewind to start and write replacement character - lb->len = lb->codepoint_start; + line_buffer_rewind_current_codepoint(lb); line_buffer_replchar(lb); } lb->codepoint_start = -1; @@ -188,10 +197,10 @@ off_t __wasilibc_tell(int fd) { void __wasilibc_webp2_flush_stdio(void) { if (line_buffers[0].len > 0) { - line_buffer_flush(&line_buffers[0], true); + line_buffer_flush(&line_buffers[0]); } if (line_buffers[1].len > 0) { - line_buffer_flush(&line_buffers[1], true); + line_buffer_flush(&line_buffers[1]); } } @@ -204,7 +213,7 @@ int close(int fd) { if (is_writable(fd)) { line_buffer_t *buf = &line_buffers[fd - 1]; if (buf->len > 0) { - line_buffer_flush(buf, true); + line_buffer_flush(buf); } } return 0; @@ -325,7 +334,6 @@ int fstat(int fildes, struct stat *buf) { return 0; } - // ============================================================================ // Stubs From 13cde4c3b301604bd80264fc4422b74c0dcea68b Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 12 Oct 2026 14:04:59 -0500 Subject: [PATCH 43/96] Update expected symbols --- expected/wasm32-webp2/defined-symbols.txt | 2 ++ expected/wasm32-webp2/undefined-symbols.txt | 1 + 2 files changed, 3 insertions(+) diff --git a/expected/wasm32-webp2/defined-symbols.txt b/expected/wasm32-webp2/defined-symbols.txt index 627b0fe92..4f67208ac 100644 --- a/expected/wasm32-webp2/defined-symbols.txt +++ b/expected/wasm32-webp2/defined-symbols.txt @@ -332,6 +332,7 @@ __wasilibc_stat __wasilibc_tell __wasilibc_unlinkat __wasilibc_utimens +__wasilibc_webp2_flush_stdio __wasilibc_webp2_report_error __wasm_call_dtors __wcscoll_l @@ -1338,6 +1339,7 @@ webp2_borrow_date webp2_borrow_error webp2_borrow_performance webp2_borrow_uint_8_array +webp2_console_log webp2_constructor_error webp2_constructor_uint_8_array webp2_crypto_drop_own diff --git a/expected/wasm32-webp2/undefined-symbols.txt b/expected/wasm32-webp2/undefined-symbols.txt index 6b6532710..59845505c 100644 --- a/expected/wasm32-webp2/undefined-symbols.txt +++ b/expected/wasm32-webp2/undefined-symbols.txt @@ -22,6 +22,7 @@ __trunctfdf2 __trunctfsf2 __unordtf2 __wasm_call_ctors +__wasm_import_webp2_console_log __wasm_import_webp2_constructor_error __wasm_import_webp2_constructor_uint_8_array __wasm_import_webp2_crypto_drop From 4a5ec8ad1975703f04d6f223a296e80fe27969d8 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 12 Oct 2026 15:09:14 -0500 Subject: [PATCH 44/96] Remove spurious comment --- libc-bottom-half/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index b25842160..64aed0679 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -314,7 +314,6 @@ if (WASI STREQUAL "p1") ) elseif(WEB STREQUAL "p2") # webp2: crt1-command.o is not necessary - # TODO(webp2): Set world like below? else() if (WASI STREQUAL "p2") set(world wasi:cli/command@${wasip2-version}) From 266d97da1f5a38522a74421301eb9ded8b1b66c8 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 12 Oct 2026 15:41:50 -0500 Subject: [PATCH 45/96] Provide argv[0] like Emscripten --- libc-bottom-half/sources/__main_void.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libc-bottom-half/sources/__main_void.c b/libc-bottom-half/sources/__main_void.c index 58f00c37d..ccbf47f1c 100644 --- a/libc-bottom-half/sources/__main_void.c +++ b/libc-bottom-half/sources/__main_void.c @@ -121,8 +121,8 @@ __attribute__((__weak__, nodebug)) int __main_void(void) { if (!__main_argc_argv) { return 0; } - static char *argv[] = {NULL}; - return __main_argc_argv(0, argv); + static char *argv[] = {"./this.program", NULL}; + return __main_argc_argv(1, argv); #else #error "Unknown WASI version" #endif From 19063d3baa55e4b08d8fd6eae2b94a2d175211d3 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 12 Oct 2026 15:53:52 -0500 Subject: [PATCH 46/96] Use console.warn for stderr --- .../headers/public/wasi/__generated_webp2.h | 1 + libc-bottom-half/sources/webp2.c | 7 +++++++ libc-bottom-half/sources/webp2_component_type.o | Bin 920 -> 929 bytes .../sources/webp2_file_descriptors.c | 7 ++++++- web/p2/wit/deps/web.wit | 1 + 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/libc-bottom-half/headers/public/wasi/__generated_webp2.h b/libc-bottom-half/headers/public/wasi/__generated_webp2.h index c39d338dd..f57e103a4 100644 --- a/libc-bottom-half/headers/public/wasi/__generated_webp2.h +++ b/libc-bottom-half/headers/public/wasi/__generated_webp2.h @@ -102,6 +102,7 @@ extern webp2_own_crypto_t webp2_get_global_crypto(void); // Imported Functions from `wasm:js/console` extern void webp2_console_log(webp2_string_t *msg); +extern void webp2_console_warn(webp2_string_t *msg); // Helper Functions diff --git a/libc-bottom-half/sources/webp2.c b/libc-bottom-half/sources/webp2.c index f94d07e10..a80d8e89c 100644 --- a/libc-bottom-half/sources/webp2.c +++ b/libc-bottom-half/sources/webp2.c @@ -40,6 +40,9 @@ extern int32_t __wasm_import_webp2_get_global_crypto(void); __attribute__((__import_module__("wasm:js/console"), __import_name__("log"))) extern void __wasm_import_webp2_console_log(uint8_t *, size_t); +__attribute__((__import_module__("wasm:js/console"), __import_name__("warn"))) +extern void __wasm_import_webp2_console_warn(uint8_t *, size_t); + // Canonical ABI intrinsics __attribute__((__weak__, __export_name__("cabi_realloc"))) @@ -286,6 +289,10 @@ void webp2_console_log(webp2_string_t *msg) { __wasm_import_webp2_console_log((uint8_t *) (*msg).ptr, (*msg).len); } +void webp2_console_warn(webp2_string_t *msg) { + __wasm_import_webp2_console_warn((uint8_t *) (*msg).ptr, (*msg).len); +} + // Ensure that the *_component_type.o object is linked in extern void __component_type_object_force_link_webp2(void); diff --git a/libc-bottom-half/sources/webp2_component_type.o b/libc-bottom-half/sources/webp2_component_type.o index 3269376b3e017ed51aff93cad317170bd9f3754d..3100321ba03a35656583c3c3f9a8af98bcc04868 100644 GIT binary patch delta 48 zcmbQizL0%_B;%QhQm%}fHzou!Nij1zFf!*Brx!CaurM&^buf, .len = lb->len}; - webp2_console_log(&out); + if (lb->warn) { + webp2_console_warn(&out); + } else { + webp2_console_log(&out); + } lb->len = 0; lb->codepoint_start = -1; lb->codepoint_remaining = 0; diff --git a/web/p2/wit/deps/web.wit b/web/p2/wit/deps/web.wit index e2d449a50..77fc718c6 100644 --- a/web/p2/wit/deps/web.wit +++ b/web/p2/wit/deps/web.wit @@ -2,6 +2,7 @@ package wasm:js; interface console { log: func(msg: string); + warn: func(msg: string); } interface global { From 423d7e8183956592e6b4345a68124be8125e7b1b Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 13 Oct 2026 07:28:33 -0500 Subject: [PATCH 47/96] Flush musl's buffers on exit / flush out buffers on fflush --- .../cloudlibc/src/libc/stdlib/_Exit.c | 5 ++++- .../sources/__wasilibc_report_error.c | 2 +- libc-bottom-half/sources/abort.c | 2 +- .../sources/webp2_file_descriptors.c | 17 ++++++++++++----- .../headers/private/wasi/report-error.h | 9 ++++++--- libc-top-half/musl/src/stdio/fflush.c | 8 ++++++++ 6 files changed, 32 insertions(+), 11 deletions(-) diff --git a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c index 0bb496100..343b38c35 100644 --- a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c +++ b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c @@ -20,9 +20,12 @@ noreturn void _Exit(int status) { if (status != 0) { __wasilibc_report_error("exit with nonzero status"); } + // POSIX says not to flush buffers, so we don't flush musl's buffers, but + // because our line buffering exists solely for the purpose of pretty + // console.logs, we do flush those. + __wasilibc_webp2_flush_stdio(/*also_musl=*/false); // We really don't have any other way to halt execution except to trap. Quite // unfortunate that this will manifest as a RuntimeError. - __wasilibc_webp2_flush_stdio(); __builtin_trap(); #else # error "Unknown WASI version" diff --git a/libc-bottom-half/sources/__wasilibc_report_error.c b/libc-bottom-half/sources/__wasilibc_report_error.c index b360d52c3..0da3c92fa 100644 --- a/libc-bottom-half/sources/__wasilibc_report_error.c +++ b/libc-bottom-half/sources/__wasilibc_report_error.c @@ -12,7 +12,7 @@ void __wasilibc_report_error(const char *message) { #ifdef __webp2__ void __wasilibc_webp2_report_error(const char *message) { - __wasilibc_webp2_flush_stdio(); + __wasilibc_webp2_flush_stdio(/*also_musl=*/true); webp2_string_t msg; webp2_string_set(&msg, message); webp2_own_error_t err = webp2_constructor_error(&msg, NULL); diff --git a/libc-bottom-half/sources/abort.c b/libc-bottom-half/sources/abort.c index 869eb175f..824bde1f1 100644 --- a/libc-bottom-half/sources/abort.c +++ b/libc-bottom-half/sources/abort.c @@ -4,7 +4,7 @@ void abort(void) { // wasm doesn't support signals, so just trap to halt the program. #ifdef __webp2__ - __wasilibc_webp2_flush_stdio(); + __wasilibc_webp2_flush_stdio(/*also_musl=*/true); #endif __builtin_trap(); } diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index 4f6f82eb9..5dfc02a6d 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -200,12 +201,18 @@ off_t __wasilibc_tell(int fd) { return -1; } -void __wasilibc_webp2_flush_stdio(void) { - if (line_buffers[0].len > 0) { - line_buffer_flush(&line_buffers[0]); +void __wasilibc_webp2_flush_stdio(bool also_musl) { + __wasilibc_webp2_fflush(STDOUT_FILENO, also_musl); + __wasilibc_webp2_fflush(STDERR_FILENO, also_musl); +} + +void __wasilibc_webp2_fflush(int fd, bool also_musl) { + assert(fd == STDOUT_FILENO || fd == STDERR_FILENO); + if (also_musl) { + fflush(fd == STDOUT_FILENO ? stdout : stderr); } - if (line_buffers[1].len > 0) { - line_buffer_flush(&line_buffers[1]); + if (line_buffers[fd - 1].len > 0) { + line_buffer_flush(&line_buffers[fd - 1]); } } diff --git a/libc-top-half/headers/private/wasi/report-error.h b/libc-top-half/headers/private/wasi/report-error.h index 737bb1019..d3212c80c 100644 --- a/libc-top-half/headers/private/wasi/report-error.h +++ b/libc-top-half/headers/private/wasi/report-error.h @@ -8,15 +8,18 @@ hidden void __wasilibc_report_error(const char* message); #ifdef __webp2__ -// Flushes any remaining content in stdout/stderr. -void __wasilibc_webp2_flush_stdio(); +// Flushes any remaining content in stdout/stderr, optionally also running +// `fflush` on musl's stdio streams to ensure that absolutely everything goes +// out to the console. +void __wasilibc_webp2_flush_stdio(bool also_musl); +void __wasilibc_webp2_fflush(int fd, bool also_musl); // Reports an error to the console using webp2 bindings. void __wasilibc_webp2_report_error(const char* message); #define WEBP2_UNSUPPORTED(name) \ do { \ - __wasilibc_webp2_flush_stdio(); \ + __wasilibc_webp2_flush_stdio(true); \ __wasilibc_report_error(name " is not supported on the web"); \ __builtin_trap(); \ } while (0) diff --git a/libc-top-half/musl/src/stdio/fflush.c b/libc-top-half/musl/src/stdio/fflush.c index b00943764..a78c12928 100644 --- a/libc-top-half/musl/src/stdio/fflush.c +++ b/libc-top-half/musl/src/stdio/fflush.c @@ -1,4 +1,6 @@ #include "stdio_impl.h" +#include "wasi/report-error.h" +#include /* stdout.c will override this if linked */ static FILE *volatile dummy = 0; @@ -40,6 +42,12 @@ int fflush(FILE *f) f->wpos = f->wbase = f->wend = 0; f->rpos = f->rend = 0; +#ifdef __webp2__ + if (f->fd == STDOUT_FILENO || f->fd == STDERR_FILENO) { + __wasilibc_webp2_fflush(f->fd, /*also_musl=*/false); + } +#endif + FUNLOCK(f); return 0; } From e53aca32b35d31324a37e92e8c79d2706c56bfaf Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 13 Oct 2026 07:35:54 -0500 Subject: [PATCH 48/96] Always print a message on exit() --- libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c index 343b38c35..177707342 100644 --- a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c +++ b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c @@ -17,15 +17,19 @@ noreturn void _Exit(int status) { exit_result_void_void_t exit_status = { .is_err = status != 0 }; exit_exit(&exit_status); #elif defined(__webp2__) - if (status != 0) { - __wasilibc_report_error("exit with nonzero status"); - } // POSIX says not to flush buffers, so we don't flush musl's buffers, but // because our line buffering exists solely for the purpose of pretty // console.logs, we do flush those. __wasilibc_webp2_flush_stdio(/*also_musl=*/false); + // We really don't have any other way to halt execution except to trap. Quite // unfortunate that this will manifest as a RuntimeError. + if (status == 0) { + const char msg[] = "exit with status 0 (reported as trap)\n"; + write(STDOUT_FILENO, msg, sizeof(msg) - 1); + } else { + __wasilibc_report_error("exit with nonzero status"); + } __builtin_trap(); #else # error "Unknown WASI version" From 3cc738aa1908cbc43cf5d24e4066583840374c13 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 13 Oct 2026 12:18:46 -0500 Subject: [PATCH 49/96] Refactor UTF-8 code, sanitize all error output --- .../wasm32-wasip1-threads/defined-symbols.txt | 3 + expected/wasm32-wasip1/defined-symbols.txt | 3 + expected/wasm32-wasip2/defined-symbols.txt | 3 + .../wasm32-wasip3-coop/defined-symbols.txt | 3 + expected/wasm32-wasip3/defined-symbols.txt | 3 + expected/wasm32-webp2/defined-symbols.txt | 7 +- expected/wasm32-webp2/undefined-symbols.txt | 1 + libc-bottom-half/CMakeLists.txt | 1 + .../cloudlibc/src/libc/stdlib/_Exit.c | 2 +- .../sources/__wasilibc_report_error.c | 2 +- .../sources/__wasilibc_report_errorf.c | 56 ++---- libc-bottom-half/sources/abort.c | 2 +- libc-bottom-half/sources/utf8.c | 107 ++++++++++ .../sources/webp2_file_descriptors.c | 182 +++++++----------- .../headers/private/wasi/report-error.h | 14 +- libc-top-half/headers/private/wasi/utf8.h | 35 ++++ libc-top-half/musl/src/stdio/fflush.c | 2 +- 17 files changed, 267 insertions(+), 159 deletions(-) create mode 100644 libc-bottom-half/sources/utf8.c create mode 100644 libc-top-half/headers/private/wasi/utf8.h diff --git a/expected/wasm32-wasip1-threads/defined-symbols.txt b/expected/wasm32-wasip1-threads/defined-symbols.txt index 0ccd9a729..41fe3355d 100644 --- a/expected/wasm32-wasip1-threads/defined-symbols.txt +++ b/expected/wasm32-wasip1-threads/defined-symbols.txt @@ -405,6 +405,9 @@ __wasilibc_stat __wasilibc_tell __wasilibc_unlinkat __wasilibc_use_busy_futex +__wasilibc_utf8_abort_current +__wasilibc_utf8_reset +__wasilibc_utf8_write __wasilibc_utimens __wasm_call_dtors __wcscoll_l diff --git a/expected/wasm32-wasip1/defined-symbols.txt b/expected/wasm32-wasip1/defined-symbols.txt index 790038b41..b3f4a4fea 100644 --- a/expected/wasm32-wasip1/defined-symbols.txt +++ b/expected/wasm32-wasip1/defined-symbols.txt @@ -392,6 +392,9 @@ __wasilibc_rmdirat __wasilibc_stat __wasilibc_tell __wasilibc_unlinkat +__wasilibc_utf8_abort_current +__wasilibc_utf8_reset +__wasilibc_utf8_write __wasilibc_utimens __wasm_call_dtors __wcscoll_l diff --git a/expected/wasm32-wasip2/defined-symbols.txt b/expected/wasm32-wasip2/defined-symbols.txt index 14f58d0c7..29670c994 100644 --- a/expected/wasm32-wasip2/defined-symbols.txt +++ b/expected/wasm32-wasip2/defined-symbols.txt @@ -368,6 +368,9 @@ __wasilibc_stat __wasilibc_tell __wasilibc_unlinkat __wasilibc_unspecified_addr +__wasilibc_utf8_abort_current +__wasilibc_utf8_reset +__wasilibc_utf8_write __wasilibc_utimens __wasilibc_wasi_family_to_libc __wasilibc_wasi_to_sockaddr diff --git a/expected/wasm32-wasip3-coop/defined-symbols.txt b/expected/wasm32-wasip3-coop/defined-symbols.txt index 2e7aa8719..1fe928f0e 100644 --- a/expected/wasm32-wasip3-coop/defined-symbols.txt +++ b/expected/wasm32-wasip3-coop/defined-symbols.txt @@ -402,6 +402,9 @@ __wasilibc_tls_init __wasilibc_tls_size __wasilibc_unlinkat __wasilibc_unspecified_addr +__wasilibc_utf8_abort_current +__wasilibc_utf8_reset +__wasilibc_utf8_write __wasilibc_utimens __wasilibc_waitable_block_on __wasilibc_wasi_family_to_libc diff --git a/expected/wasm32-wasip3/defined-symbols.txt b/expected/wasm32-wasip3/defined-symbols.txt index 67173584a..ead42bb40 100644 --- a/expected/wasm32-wasip3/defined-symbols.txt +++ b/expected/wasm32-wasip3/defined-symbols.txt @@ -376,6 +376,9 @@ __wasilibc_tls_init __wasilibc_tls_size __wasilibc_unlinkat __wasilibc_unspecified_addr +__wasilibc_utf8_abort_current +__wasilibc_utf8_reset +__wasilibc_utf8_write __wasilibc_utimens __wasilibc_waitable_block_on __wasilibc_wasi_family_to_libc diff --git a/expected/wasm32-webp2/defined-symbols.txt b/expected/wasm32-webp2/defined-symbols.txt index 4f67208ac..4b20d97f0 100644 --- a/expected/wasm32-webp2/defined-symbols.txt +++ b/expected/wasm32-webp2/defined-symbols.txt @@ -331,8 +331,12 @@ __wasilibc_rmdirat __wasilibc_stat __wasilibc_tell __wasilibc_unlinkat +__wasilibc_utf8_abort_current +__wasilibc_utf8_reset +__wasilibc_utf8_write __wasilibc_utimens -__wasilibc_webp2_flush_stdio +__wasilibc_webp2_fflush +__wasilibc_webp2_finish_and_flush_stdio __wasilibc_webp2_report_error __wasm_call_dtors __wcscoll_l @@ -1340,6 +1344,7 @@ webp2_borrow_error webp2_borrow_performance webp2_borrow_uint_8_array webp2_console_log +webp2_console_warn webp2_constructor_error webp2_constructor_uint_8_array webp2_crypto_drop_own diff --git a/expected/wasm32-webp2/undefined-symbols.txt b/expected/wasm32-webp2/undefined-symbols.txt index 59845505c..e5bfc8c17 100644 --- a/expected/wasm32-webp2/undefined-symbols.txt +++ b/expected/wasm32-webp2/undefined-symbols.txt @@ -23,6 +23,7 @@ __trunctfsf2 __unordtf2 __wasm_call_ctors __wasm_import_webp2_console_log +__wasm_import_webp2_console_warn __wasm_import_webp2_constructor_error __wasm_import_webp2_constructor_uint_8_array __wasm_import_webp2_crypto_drop diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index 64aed0679..13d4a12fb 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -93,6 +93,7 @@ set(bottom_half_sources sources/math/math-builtins.c sources/reallocarray.c sources/sbrk.c + sources/utf8.c sources/__cabi_realloc_wrapper.S ) diff --git a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c index 177707342..01ded89fa 100644 --- a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c +++ b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c @@ -20,7 +20,7 @@ noreturn void _Exit(int status) { // POSIX says not to flush buffers, so we don't flush musl's buffers, but // because our line buffering exists solely for the purpose of pretty // console.logs, we do flush those. - __wasilibc_webp2_flush_stdio(/*also_musl=*/false); + __wasilibc_webp2_finish_and_flush_stdio(/*also_musl=*/false); // We really don't have any other way to halt execution except to trap. Quite // unfortunate that this will manifest as a RuntimeError. diff --git a/libc-bottom-half/sources/__wasilibc_report_error.c b/libc-bottom-half/sources/__wasilibc_report_error.c index 0da3c92fa..155590c91 100644 --- a/libc-bottom-half/sources/__wasilibc_report_error.c +++ b/libc-bottom-half/sources/__wasilibc_report_error.c @@ -12,7 +12,7 @@ void __wasilibc_report_error(const char *message) { #ifdef __webp2__ void __wasilibc_webp2_report_error(const char *message) { - __wasilibc_webp2_flush_stdio(/*also_musl=*/true); + __wasilibc_webp2_finish_and_flush_stdio(/*also_musl=*/true); webp2_string_t msg; webp2_string_set(&msg, message); webp2_own_error_t err = webp2_constructor_error(&msg, NULL); diff --git a/libc-bottom-half/sources/__wasilibc_report_errorf.c b/libc-bottom-half/sources/__wasilibc_report_errorf.c index 0f0bbe639..93dd7a746 100644 --- a/libc-bottom-half/sources/__wasilibc_report_errorf.c +++ b/libc-bottom-half/sources/__wasilibc_report_errorf.c @@ -2,51 +2,35 @@ #include #include #include - -#if defined(__webp2__) -// Ensure we have well-formed UTF-8 in case of truncation. Assumes valid UTF-8 -// coming in. -static void fix_truncated_utf8(char *buf, size_t len) { - if (len == 0) { - return; - } - size_t last_codepoint_start = len - 1; - for (size_t i = 0; i < 3; i++) { - if (((unsigned char)buf[last_codepoint_start] & 0xC0) != 0x80) { - break; - } - last_codepoint_start--; - } - unsigned char b1 = (unsigned char)buf[last_codepoint_start]; - size_t codepoint_len; - if (b1 >= 0xF0) { - codepoint_len = 4; - } else if (b1 >= 0xE0) { - codepoint_len = 3; - } else if (b1 >= 0xC0) { - codepoint_len = 2; - } else { - codepoint_len = 1; - } - if (len - last_codepoint_start < codepoint_len) { - buf[last_codepoint_start] = '\0'; - } -} -#endif +#include void __wasilibc_report_errorf(const char *format, ...) { va_list ap; va_start(ap, format); + #if defined(__webp2__) - char buf[512]; - int n = vsnprintf(buf, sizeof(buf), format, ap); - if (n >= (int)sizeof(buf)) { - fix_truncated_utf8(buf, sizeof(buf) - 1); // subtract 1 for vnsprintf's \0 +#define ERROR_MAX_LEN 512 + char format_buf[ERROR_MAX_LEN]; + uint8_t utf8_buf[ERROR_MAX_LEN+1]; // leave room for nul + + int n = vsnprintf(format_buf, sizeof(format_buf), format, ap); + wasilibc_utf8_writer_t utf8 = __wasilibc_make_utf8_writer(utf8_buf, ERROR_MAX_LEN); + + // Re-encode as UTF-8 (excluding vnsprintf's nul) + for (int i = 0; i < n && i < (int)sizeof(format_buf) - 1; i++) { + if (!__wasilibc_utf8_write(&utf8, format_buf[i])) { + break; + } } - __wasilibc_report_error(buf); + __wasilibc_utf8_abort_current(&utf8); + + // Nul-terminate message and pass along + utf8_buf[utf8.valid_len] = 0; + __wasilibc_report_error((char*)utf8_buf); #else vfprintf(stderr, format, ap); fputc('\n', stderr); #endif + va_end(ap); } diff --git a/libc-bottom-half/sources/abort.c b/libc-bottom-half/sources/abort.c index 824bde1f1..40dcbdc93 100644 --- a/libc-bottom-half/sources/abort.c +++ b/libc-bottom-half/sources/abort.c @@ -4,7 +4,7 @@ void abort(void) { // wasm doesn't support signals, so just trap to halt the program. #ifdef __webp2__ - __wasilibc_webp2_flush_stdio(/*also_musl=*/true); + __wasilibc_webp2_finish_and_flush_stdio(/*also_musl=*/true); #endif __builtin_trap(); } diff --git a/libc-bottom-half/sources/utf8.c b/libc-bottom-half/sources/utf8.c new file mode 100644 index 000000000..a1f9d2948 --- /dev/null +++ b/libc-bottom-half/sources/utf8.c @@ -0,0 +1,107 @@ +#include "wasi/utf8.h" + +#include +#include + +static bool utf8_replchar(wasilibc_utf8_writer_t *w) { + if (w->raw_len + 3 > w->cap) { + return false; + } + w->buf[w->raw_len++] = 0xEF; + w->buf[w->raw_len++] = 0xBF; + w->buf[w->raw_len++] = 0xBD; + w->valid_len = w->raw_len; + return true; +} + +bool __wasilibc_utf8_write(wasilibc_utf8_writer_t *w, uint8_t b) { + assert(w->valid_len <= w->raw_len); + + bool is_1byte = (b & 0x80) == 0; + bool is_continuation = (b & 0xC0) == 0x80; + bool is_2byte = (b & 0xE0) == 0xC0; + bool is_3byte = (b & 0xF0) == 0xE0; + bool is_4byte = (b & 0xF8) == 0xF0; + + if (!is_continuation && w->valid_len < w->raw_len) { + // Unexpected non-continuation byte; rewind, emit replacement for the + // aborted sequence, and continue with the current byte. + if (!__wasilibc_utf8_abort_current(w)) { + return false; + } + } + + if (is_1byte) { + if (w->raw_len + 1 > w->cap) { + return false; + } + w->buf[w->raw_len++] = b; + w->valid_len = w->raw_len; + } else if (is_2byte || is_3byte || is_4byte) { + int32_t codepoint_length = is_2byte ? 2 : (is_3byte ? 3 : 4); + if (w->raw_len + codepoint_length > w->cap) { + return false; + } + w->codepoint_remaining = codepoint_length - 1; + w->buf[w->raw_len++] = b; + // Don't update valid_len + } else if (is_continuation) { + if (w->valid_len == w->raw_len) { + // Unexpected continuation byte; emit a replacement char instead + return utf8_replchar(w); + } else { + // Expected continuation byte + w->buf[w->raw_len++] = b; + w->codepoint_remaining -= 1; + + // If done, validate overlong / out-of-range + if (w->codepoint_remaining <= 0) { + uint8_t first = w->buf[w->valid_len]; + uint8_t second = w->buf[w->valid_len + 1]; + bool overlong = first == 0xC0 || first == 0xC1 || + (first == 0xE0 && second < 0xA0) || + (first == 0xF0 && second < 0x90); + bool toobig = (first == 0xF4 && second >= 0x90) || 0xF5 <= first; + bool surrogate = (first == 0xED && second >= 0xA0); + if (overlong || toobig || surrogate) { + // Rewind to start and write replacement character. If this fails, + // restore all state to how it originally was. + if (!__wasilibc_utf8_abort_current(w)) { + w->raw_len--; + w->codepoint_remaining += 1; + return false; + } + } + w->valid_len = w->raw_len; + } + } + } else { + return utf8_replchar(w); + } + + return true; +} + +bool __wasilibc_utf8_abort_current(wasilibc_utf8_writer_t *w) { + assert(w->valid_len <= w->raw_len); + if (w->valid_len == w->raw_len) { + // No multi-byte code point in progress. + return true; + } + if (w->valid_len + 3 > w->cap) { + // No room for replacement character. + return false; + } + w->raw_len = w->valid_len; + utf8_replchar(w); // will succeed + return true; +} + +void __wasilibc_utf8_reset(wasilibc_utf8_writer_t *w) { + size_t n = w->raw_len - w->valid_len; + if (n > 0) { + memmove(&w->buf[0], &w->buf[w->valid_len], n); + } + w->raw_len = n; + w->valid_len = 0; +} diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index 5dfc02a6d..efa5eddd3 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -19,6 +19,7 @@ #include #include #include +#include // unistd.h defines an `lseek` macro that would mangle the definition below. #undef lseek @@ -29,118 +30,28 @@ #define LINE_BUF_SIZE 4096 typedef struct line_buffer_t { - int32_t len; - int32_t codepoint_start; // -1 means no current code point - int32_t codepoint_remaining; bool warn; uint8_t buf[LINE_BUF_SIZE]; } line_buffer_t; static line_buffer_t line_buffers[2] = { - (line_buffer_t){.codepoint_start = -1}, - (line_buffer_t){.codepoint_start = -1}, + (line_buffer_t){}, + (line_buffer_t){ .warn = true }, +}; +static wasilibc_utf8_writer_t utf8_writers[2] = { + __wasilibc_make_utf8_writer(line_buffers[0].buf, LINE_BUF_SIZE), + __wasilibc_make_utf8_writer(line_buffers[1].buf, LINE_BUF_SIZE), }; static bool fd_closed[3]; -static void line_buffer_replchar(line_buffer_t *lb); - -static void line_buffer_rewind_current_codepoint(line_buffer_t *lb) { - assert(lb->codepoint_start >= 0); - lb->len = lb->codepoint_start; - lb->codepoint_start = -1; -} - -static void line_buffer_flush(line_buffer_t *lb) { - if (lb->codepoint_start >= 0) { - // Cannot flush with a code point in progress because we have to hand it - // off to console.log. But this should only happen when closing the stream - // early, because we always flush early when starting a code point that - // would overfill the buffer. Rewind and emit a replacement character to - // close things off. - line_buffer_rewind_current_codepoint(lb); - line_buffer_replchar(lb); - } - - webp2_string_t out = (webp2_string_t){.ptr = lb->buf, .len = lb->len}; +static void line_buffer_out(line_buffer_t *lb, wasilibc_utf8_writer_t *utf8) { + webp2_string_t out = (webp2_string_t){.ptr = utf8->buf, .len = utf8->valid_len}; if (lb->warn) { webp2_console_warn(&out); } else { webp2_console_log(&out); } - lb->len = 0; - lb->codepoint_start = -1; - lb->codepoint_remaining = 0; -} - -static void line_buffer_replchar(line_buffer_t *lb) { - if (lb->len + 3 > LINE_BUF_SIZE) { - line_buffer_flush(lb); - } - lb->buf[lb->len++] = 0xEF; - lb->buf[lb->len++] = 0xBF; - lb->buf[lb->len++] = 0xBD; -} - -// Emits a single byte to the line buffer, fixing up UTF-8 problems as it goes. -static void line_buffer_byte(line_buffer_t *lb, uint8_t b) { - bool is_1byte = (b & 0x80) == 0; - bool is_continuation = (b & 0xC0) == 0x80; - bool is_2byte = (b & 0xE0) == 0xC0; - bool is_3byte = (b & 0xF0) == 0xE0; - bool is_4byte = (b & 0xF8) == 0xF0; - - if (!is_continuation && lb->codepoint_start >= 0) { - // Unexpected non-continuation byte; rewind, emit replacement for the - // aborted sequence, and continue with the current byte - line_buffer_rewind_current_codepoint(lb); - line_buffer_replchar(lb); - } - - if (is_1byte) { - if (b == '\n' || lb->len + 1 > LINE_BUF_SIZE) { - line_buffer_flush(lb); - } - if (b != '\n') { - lb->buf[lb->len++] = b; - } - } else if (is_2byte || is_3byte || is_4byte) { - int32_t codepoint_length = is_2byte ? 2 : (is_3byte ? 3 : 4); - if (lb->len + codepoint_length > LINE_BUF_SIZE) { - line_buffer_flush(lb); - } - lb->codepoint_start = lb->len; - lb->codepoint_remaining = codepoint_length - 1; - lb->buf[lb->len++] = b; - } else if (is_continuation) { - if (lb->codepoint_start < 0) { - // Unexpected continuation byte; emit a replacement char instead - line_buffer_replchar(lb); - } else { - // Expected continuation byte - lb->buf[lb->len++] = b; - lb->codepoint_remaining -= 1; - - // If done, validate overlong / out-of-range - if (lb->codepoint_remaining <= 0) { - uint8_t first = lb->buf[lb->codepoint_start]; - uint8_t second = lb->buf[lb->codepoint_start + 1]; - bool overlong = first == 0xC0 || first == 0xC1 || - (first == 0xE0 && second < 0xA0) || - (first == 0xF0 && second < 0x90); - bool toobig = (first == 0xF4 && second >= 0x90) || 0xF5 <= first; - bool surrogate = (first == 0xED && second >= 0xA0); - if (overlong || toobig || surrogate) { - // Rewind to start and write replacement character - line_buffer_rewind_current_codepoint(lb); - line_buffer_replchar(lb); - } - lb->codepoint_start = -1; - } - } - } else { - line_buffer_replchar(lb); - } } static bool fd_ok(int fd) { @@ -172,6 +83,23 @@ static bool iovecs_ok(const struct iovec *iov, int iovcnt, size_t *total_size) { return true; } +// Flushes all data in line buffer to the console, replacing any trailing +// multi-byte code points with the replacement character. +static void finish_and_flush(int fd) { + assert(fd == STDOUT_FILENO || fd == STDERR_FILENO); + if (fd_closed[fd]) { + return; + } + wasilibc_utf8_writer_t *utf8 = &utf8_writers[fd - 1]; + if (!__wasilibc_utf8_abort_current(utf8)) { + // Emit what is already valid in the buffer first, and carry over the + // in-progress codepoint + __wasilibc_webp2_fflush(fd); + } + __wasilibc_utf8_abort_current(utf8); + __wasilibc_webp2_fflush(fd); +} + int __isatty(int fd) { if (!fd_ok(fd)) { errno = EBADF; @@ -201,19 +129,29 @@ off_t __wasilibc_tell(int fd) { return -1; } -void __wasilibc_webp2_flush_stdio(bool also_musl) { - __wasilibc_webp2_fflush(STDOUT_FILENO, also_musl); - __wasilibc_webp2_fflush(STDERR_FILENO, also_musl); +void __wasilibc_webp2_fflush(int fd) { + assert(fd == STDOUT_FILENO || fd == STDERR_FILENO); + if (fd_closed[fd]) { + return; + } + line_buffer_t *lb = &line_buffers[fd - 1]; + wasilibc_utf8_writer_t *utf8 = &utf8_writers[fd - 1]; + if (utf8->valid_len > 0) { + line_buffer_out(lb, utf8); + __wasilibc_utf8_reset(utf8); + } } -void __wasilibc_webp2_fflush(int fd, bool also_musl) { - assert(fd == STDOUT_FILENO || fd == STDERR_FILENO); +void __wasilibc_webp2_finish_and_flush_stdio(bool also_musl) { if (also_musl) { - fflush(fd == STDOUT_FILENO ? stdout : stderr); + fflush(stdout); } - if (line_buffers[fd - 1].len > 0) { - line_buffer_flush(&line_buffers[fd - 1]); + finish_and_flush(STDOUT_FILENO); + + if (also_musl) { + fflush(stderr); } + finish_and_flush(STDERR_FILENO); } int close(int fd) { @@ -221,13 +159,11 @@ int close(int fd) { errno = EBADF; return -1; } - fd_closed[fd] = true; if (is_writable(fd)) { - line_buffer_t *buf = &line_buffers[fd - 1]; - if (buf->len > 0) { - line_buffer_flush(buf); - } + // Don't flush musl's buffers, but do flush everything we have. + finish_and_flush(fd); } + fd_closed[fd] = true; return 0; } @@ -303,8 +239,30 @@ ssize_t write(int fildes, const void *buf, size_t nbyte) { } line_buffer_t *line_buffer = &line_buffers[fildes - 1]; + wasilibc_utf8_writer_t *utf8 = &utf8_writers[fildes - 1]; for (size_t i = 0; i < nbyte; i++) { - line_buffer_byte(line_buffer, ((uint8_t *)buf)[i]); + uint8_t b = ((uint8_t *)buf)[i]; + // First write to the buffer, flushing first if there was no room for the + // results (which can occur either because the buffer was completely full + // or because there was no room for a replacement char). + bool already_logged = false; + if (!__wasilibc_utf8_write(utf8, b)) { + line_buffer_out(line_buffer, utf8); + __wasilibc_utf8_reset(utf8); + __wasilibc_utf8_write(utf8, b); // infallible + already_logged = true; + } + // Then, if there was a newline, drop it from the buffer and flush. + if (b == '\n') { + assert(utf8->valid_len == utf8->raw_len); + utf8->raw_len--; + utf8->valid_len--; + bool skip = utf8->valid_len == 0 && already_logged; + if (!skip) { + line_buffer_out(line_buffer, utf8); + } + __wasilibc_utf8_reset(utf8); + } } return nbyte; diff --git a/libc-top-half/headers/private/wasi/report-error.h b/libc-top-half/headers/private/wasi/report-error.h index d3212c80c..83a678734 100644 --- a/libc-top-half/headers/private/wasi/report-error.h +++ b/libc-top-half/headers/private/wasi/report-error.h @@ -8,18 +8,20 @@ hidden void __wasilibc_report_error(const char* message); #ifdef __webp2__ -// Flushes any remaining content in stdout/stderr, optionally also running -// `fflush` on musl's stdio streams to ensure that absolutely everything goes -// out to the console. -void __wasilibc_webp2_flush_stdio(bool also_musl); -void __wasilibc_webp2_fflush(int fd, bool also_musl); + +// Flush all valid content in one of our linebuffers (leaving trailing +// multibyte content alone to be completed later). +void __wasilibc_webp2_fflush(int fd); + +// Finalizes and flushes the contents of stdout & stderr in preparation for app +// exit. Optionally also runs `fflush` on musl's stdio streams first. +void __wasilibc_webp2_finish_and_flush_stdio(bool also_musl); // Reports an error to the console using webp2 bindings. void __wasilibc_webp2_report_error(const char* message); #define WEBP2_UNSUPPORTED(name) \ do { \ - __wasilibc_webp2_flush_stdio(true); \ __wasilibc_report_error(name " is not supported on the web"); \ __builtin_trap(); \ } while (0) diff --git a/libc-top-half/headers/private/wasi/utf8.h b/libc-top-half/headers/private/wasi/utf8.h new file mode 100644 index 000000000..cb8612529 --- /dev/null +++ b/libc-top-half/headers/private/wasi/utf8.h @@ -0,0 +1,35 @@ +#include +#include + +// Writes valid UTF-8 to a buffer. The range [0, valid_len) is always valid +// even when multi-byte code points are being written. +// +// `cap` must be at least 8. +typedef struct wasilibc_utf8_writer_t { + uint8_t *buf; + int32_t cap; + int32_t raw_len, valid_len; + int32_t codepoint_remaining; +} wasilibc_utf8_writer_t; + +#define __wasilibc_make_utf8_writer(_buf, _cap) \ + ((wasilibc_utf8_writer_t){ \ + .buf = (_buf), \ + .cap = (_cap), \ + }) + +// Emits a single byte to the writer's buffer. If the byte causes invalid +// UTF-8, a replacement character will be written instead. Returns false if +// it runs out of room in the buffer; in this case, you can either stop or call +// `wasilibc_utf8_reset` and then try again, which will always succeed. +bool __wasilibc_utf8_write(wasilibc_utf8_writer_t *w, uint8_t b); + +// Aborts the current multi-byte code point, if any, emitting a replacement +// character instead. Returns false if there is no room in the buffer; as with +// write, you can call `wasilibc_utf8_reset` and try again to guarantee +// success. +bool __wasilibc_utf8_abort_current(wasilibc_utf8_writer_t *w); + +// Resets the writer back to the start of its buffer. If there is a multi-byte +// code point in progress, it will be copied to the start of the buffer. +void __wasilibc_utf8_reset(wasilibc_utf8_writer_t *w); diff --git a/libc-top-half/musl/src/stdio/fflush.c b/libc-top-half/musl/src/stdio/fflush.c index a78c12928..6f83332ff 100644 --- a/libc-top-half/musl/src/stdio/fflush.c +++ b/libc-top-half/musl/src/stdio/fflush.c @@ -44,7 +44,7 @@ int fflush(FILE *f) #ifdef __webp2__ if (f->fd == STDOUT_FILENO || f->fd == STDERR_FILENO) { - __wasilibc_webp2_fflush(f->fd, /*also_musl=*/false); + __wasilibc_webp2_fflush(f->fd); } #endif From b8a3b6052f6355f82cf2dfcb4db22d4aac74f0c7 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 13 Oct 2026 12:42:16 -0500 Subject: [PATCH 50/96] Close stdio file descriptors when shutting down Avoids cases where we end up calling that flush function twice with different opinions about musl's buffers. --- expected/wasm32-webp2/defined-symbols.txt | 2 +- libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c | 7 ++++--- libc-bottom-half/sources/__wasilibc_report_error.c | 2 +- libc-bottom-half/sources/abort.c | 2 +- libc-bottom-half/sources/webp2_file_descriptors.c | 4 +++- libc-top-half/headers/private/wasi/report-error.h | 2 +- 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/expected/wasm32-webp2/defined-symbols.txt b/expected/wasm32-webp2/defined-symbols.txt index 4b20d97f0..da4e5492c 100644 --- a/expected/wasm32-webp2/defined-symbols.txt +++ b/expected/wasm32-webp2/defined-symbols.txt @@ -336,7 +336,7 @@ __wasilibc_utf8_reset __wasilibc_utf8_write __wasilibc_utimens __wasilibc_webp2_fflush -__wasilibc_webp2_finish_and_flush_stdio +__wasilibc_webp2_flush_and_close_stdio __wasilibc_webp2_report_error __wasm_call_dtors __wcscoll_l diff --git a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c index 01ded89fa..11ee66b5e 100644 --- a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c +++ b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c @@ -20,13 +20,14 @@ noreturn void _Exit(int status) { // POSIX says not to flush buffers, so we don't flush musl's buffers, but // because our line buffering exists solely for the purpose of pretty // console.logs, we do flush those. - __wasilibc_webp2_finish_and_flush_stdio(/*also_musl=*/false); + __wasilibc_webp2_flush_and_close_stdio(/*also_musl=*/false); // We really don't have any other way to halt execution except to trap. Quite // unfortunate that this will manifest as a RuntimeError. if (status == 0) { - const char msg[] = "exit with status 0 (reported as trap)\n"; - write(STDOUT_FILENO, msg, sizeof(msg) - 1); + webp2_string_t msg; + webp2_string_set(&msg, "exit with status 0 (reported as trap)"); + webp2_console_log(&msg); } else { __wasilibc_report_error("exit with nonzero status"); } diff --git a/libc-bottom-half/sources/__wasilibc_report_error.c b/libc-bottom-half/sources/__wasilibc_report_error.c index 155590c91..4f7fc4f99 100644 --- a/libc-bottom-half/sources/__wasilibc_report_error.c +++ b/libc-bottom-half/sources/__wasilibc_report_error.c @@ -12,7 +12,7 @@ void __wasilibc_report_error(const char *message) { #ifdef __webp2__ void __wasilibc_webp2_report_error(const char *message) { - __wasilibc_webp2_finish_and_flush_stdio(/*also_musl=*/true); + __wasilibc_webp2_flush_and_close_stdio(/*also_musl=*/true); webp2_string_t msg; webp2_string_set(&msg, message); webp2_own_error_t err = webp2_constructor_error(&msg, NULL); diff --git a/libc-bottom-half/sources/abort.c b/libc-bottom-half/sources/abort.c index 40dcbdc93..1eed0c15a 100644 --- a/libc-bottom-half/sources/abort.c +++ b/libc-bottom-half/sources/abort.c @@ -4,7 +4,7 @@ void abort(void) { // wasm doesn't support signals, so just trap to halt the program. #ifdef __webp2__ - __wasilibc_webp2_finish_and_flush_stdio(/*also_musl=*/true); + __wasilibc_webp2_flush_and_close_stdio(/*also_musl=*/true); #endif __builtin_trap(); } diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index efa5eddd3..4461988c1 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -142,16 +142,18 @@ void __wasilibc_webp2_fflush(int fd) { } } -void __wasilibc_webp2_finish_and_flush_stdio(bool also_musl) { +void __wasilibc_webp2_flush_and_close_stdio(bool also_musl) { if (also_musl) { fflush(stdout); } finish_and_flush(STDOUT_FILENO); + fd_closed[STDOUT_FILENO] = true; if (also_musl) { fflush(stderr); } finish_and_flush(STDERR_FILENO); + fd_closed[STDERR_FILENO] = true; } int close(int fd) { diff --git a/libc-top-half/headers/private/wasi/report-error.h b/libc-top-half/headers/private/wasi/report-error.h index 83a678734..1a38e3204 100644 --- a/libc-top-half/headers/private/wasi/report-error.h +++ b/libc-top-half/headers/private/wasi/report-error.h @@ -15,7 +15,7 @@ void __wasilibc_webp2_fflush(int fd); // Finalizes and flushes the contents of stdout & stderr in preparation for app // exit. Optionally also runs `fflush` on musl's stdio streams first. -void __wasilibc_webp2_finish_and_flush_stdio(bool also_musl); +void __wasilibc_webp2_flush_and_close_stdio(bool also_musl); // Reports an error to the console using webp2 bindings. void __wasilibc_webp2_report_error(const char* message); From 310c46dc714a925c191d0f25e85ef9e7f0492ee8 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 13 Oct 2026 12:49:03 -0500 Subject: [PATCH 51/96] clang-format!!!! --- libc-bottom-half/sources/__wasilibc_report_errorf.c | 7 ++++--- libc-bottom-half/sources/webp2_file_descriptors.c | 11 ++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/libc-bottom-half/sources/__wasilibc_report_errorf.c b/libc-bottom-half/sources/__wasilibc_report_errorf.c index 93dd7a746..e6de85177 100644 --- a/libc-bottom-half/sources/__wasilibc_report_errorf.c +++ b/libc-bottom-half/sources/__wasilibc_report_errorf.c @@ -11,10 +11,11 @@ void __wasilibc_report_errorf(const char *format, ...) { #if defined(__webp2__) #define ERROR_MAX_LEN 512 char format_buf[ERROR_MAX_LEN]; - uint8_t utf8_buf[ERROR_MAX_LEN+1]; // leave room for nul + uint8_t utf8_buf[ERROR_MAX_LEN + 1]; // leave room for nul int n = vsnprintf(format_buf, sizeof(format_buf), format, ap); - wasilibc_utf8_writer_t utf8 = __wasilibc_make_utf8_writer(utf8_buf, ERROR_MAX_LEN); + wasilibc_utf8_writer_t utf8 = + __wasilibc_make_utf8_writer(utf8_buf, ERROR_MAX_LEN); // Re-encode as UTF-8 (excluding vnsprintf's nul) for (int i = 0; i < n && i < (int)sizeof(format_buf) - 1; i++) { @@ -26,7 +27,7 @@ void __wasilibc_report_errorf(const char *format, ...) { // Nul-terminate message and pass along utf8_buf[utf8.valid_len] = 0; - __wasilibc_report_error((char*)utf8_buf); + __wasilibc_report_error((char *)utf8_buf); #else vfprintf(stderr, format, ap); fputc('\n', stderr); diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index 4461988c1..caa959d5f 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -35,18 +35,19 @@ typedef struct line_buffer_t { } line_buffer_t; static line_buffer_t line_buffers[2] = { - (line_buffer_t){}, - (line_buffer_t){ .warn = true }, + (line_buffer_t){}, + (line_buffer_t){.warn = true}, }; static wasilibc_utf8_writer_t utf8_writers[2] = { - __wasilibc_make_utf8_writer(line_buffers[0].buf, LINE_BUF_SIZE), - __wasilibc_make_utf8_writer(line_buffers[1].buf, LINE_BUF_SIZE), + __wasilibc_make_utf8_writer(line_buffers[0].buf, LINE_BUF_SIZE), + __wasilibc_make_utf8_writer(line_buffers[1].buf, LINE_BUF_SIZE), }; static bool fd_closed[3]; static void line_buffer_out(line_buffer_t *lb, wasilibc_utf8_writer_t *utf8) { - webp2_string_t out = (webp2_string_t){.ptr = utf8->buf, .len = utf8->valid_len}; + webp2_string_t out = + (webp2_string_t){.ptr = utf8->buf, .len = utf8->valid_len}; if (lb->warn) { webp2_console_warn(&out); } else { From a724b40c743182d8a4c4100d04de9a4bfadc1a7a Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 13 Oct 2026 12:57:55 -0500 Subject: [PATCH 52/96] Hide a few new symbols --- libc-top-half/headers/private/wasi/report-error.h | 4 ++-- libc-top-half/headers/private/wasi/utf8.h | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/libc-top-half/headers/private/wasi/report-error.h b/libc-top-half/headers/private/wasi/report-error.h index 1a38e3204..82d89ddc2 100644 --- a/libc-top-half/headers/private/wasi/report-error.h +++ b/libc-top-half/headers/private/wasi/report-error.h @@ -11,11 +11,11 @@ hidden void __wasilibc_report_error(const char* message); // Flush all valid content in one of our linebuffers (leaving trailing // multibyte content alone to be completed later). -void __wasilibc_webp2_fflush(int fd); +hidden void __wasilibc_webp2_fflush(int fd); // Finalizes and flushes the contents of stdout & stderr in preparation for app // exit. Optionally also runs `fflush` on musl's stdio streams first. -void __wasilibc_webp2_flush_and_close_stdio(bool also_musl); +hidden void __wasilibc_webp2_flush_and_close_stdio(bool also_musl); // Reports an error to the console using webp2 bindings. void __wasilibc_webp2_report_error(const char* message); diff --git a/libc-top-half/headers/private/wasi/utf8.h b/libc-top-half/headers/private/wasi/utf8.h index cb8612529..8e00d6ab5 100644 --- a/libc-top-half/headers/private/wasi/utf8.h +++ b/libc-top-half/headers/private/wasi/utf8.h @@ -1,3 +1,4 @@ +#include #include #include @@ -22,14 +23,14 @@ typedef struct wasilibc_utf8_writer_t { // UTF-8, a replacement character will be written instead. Returns false if // it runs out of room in the buffer; in this case, you can either stop or call // `wasilibc_utf8_reset` and then try again, which will always succeed. -bool __wasilibc_utf8_write(wasilibc_utf8_writer_t *w, uint8_t b); +hidden bool __wasilibc_utf8_write(wasilibc_utf8_writer_t *w, uint8_t b); // Aborts the current multi-byte code point, if any, emitting a replacement // character instead. Returns false if there is no room in the buffer; as with // write, you can call `wasilibc_utf8_reset` and try again to guarantee // success. -bool __wasilibc_utf8_abort_current(wasilibc_utf8_writer_t *w); +hidden bool __wasilibc_utf8_abort_current(wasilibc_utf8_writer_t *w); // Resets the writer back to the start of its buffer. If there is a multi-byte // code point in progress, it will be copied to the start of the buffer. -void __wasilibc_utf8_reset(wasilibc_utf8_writer_t *w); +hidden void __wasilibc_utf8_reset(wasilibc_utf8_writer_t *w); From 45a8110a4f5aeebdc4375d1d1cf93b2148c5cbaf Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 13 Oct 2026 14:26:54 -0500 Subject: [PATCH 53/96] Implement a few more fd syscalls for stdio --- libc-bottom-half/CMakeLists.txt | 6 +- .../sources/webp2_file_descriptors.c | 108 ++++++++++++------ 2 files changed, 79 insertions(+), 35 deletions(-) diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index 13d4a12fb..bc3d04e8c 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -67,8 +67,11 @@ endforeach() # All targets set(bottom_half_sources cloudlibc/src/libc/errno/errno.c + cloudlibc/src/libc/poll/poll.c cloudlibc/src/libc/sched/sched_yield.c cloudlibc/src/libc/stdlib/_Exit.c + cloudlibc/src/libc/sys/select/pselect.c + cloudlibc/src/libc/sys/select/select.c cloudlibc/src/libc/sys/time/gettimeofday.c cloudlibc/src/libc/time/CLOCK_MONOTONIC.c cloudlibc/src/libc/time/CLOCK_REALTIME.c @@ -101,10 +104,7 @@ set(bottom_half_sources if (NOT WEB STREQUAL "p2") list(APPEND bottom_half_sources cloudlibc/src/libc/fcntl/fcntl.c - cloudlibc/src/libc/poll/poll.c cloudlibc/src/libc/sys/ioctl/ioctl.c - cloudlibc/src/libc/sys/select/pselect.c - cloudlibc/src/libc/sys/select/select.c cloudlibc/src/libc/sys/stat/fstat.c cloudlibc/src/libc/sys/uio/readv.c cloudlibc/src/libc/sys/uio/writev.c diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index caa959d5f..96f06acab 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -3,6 +3,7 @@ // supported by routing to console.log, and stdin is supported by immediately // returning EOF. +#include "common/time.h" #include "features.h" #include "sys/uio.h" #include "wasi/report-error.h" @@ -12,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -189,6 +191,80 @@ int fcntl(int fildes, int cmd, ...) { } } +int fstat(int fildes, struct stat *buf) { + if (!fd_ok(fildes)) { + errno = EBADF; + return -1; + } + memset(buf, 0, sizeof(*buf)); + buf->st_mode = S_IFCHR; + return 0; +} + +int ioctl(int fildes, int request, ...) { + if (!fd_ok(fildes)) { + errno = EBADF; + return -1; + } + switch (request) { + case FIONREAD: { + // There is never anything available to read. + va_list ap; + va_start(ap, request); + int *result = va_arg(ap, int *); + va_end(ap); + *result = 0; + return 0; + } + case FIONBIO: + // There's no difference between "blocking" and "non-blocking" IO for our + // stdio implementation, so this does nothing. + return 0; + default: + errno = EINVAL; + return -1; + } +} + +int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, + const sigset_t *sigmask) { + if (timeout && (timeout->tv_sec < 0 || timeout->tv_nsec < 0 || + timeout->tv_nsec >= NSEC_PER_SEC)) { + errno = EINVAL; + return -1; + } + + int ready = 0; + for (nfds_t i = 0; i < nfds; i++) { + struct pollfd *pollfd = &fds[i]; + pollfd->revents = 0; + if (pollfd->fd < 0) { + continue; + } + if (!fd_ok(pollfd->fd)) { + pollfd->revents = POLLNVAL; + } else if (is_readable(pollfd->fd)) { + // stdin is always readable + pollfd->revents = pollfd->events & POLLRDNORM; + } else { + // stdout and stderr are always writable + pollfd->revents = pollfd->events & POLLWRNORM; + } + if (pollfd->revents != 0) { + ready++; + } + } + + // We cannot actually sit and wait for files to become available, but because + // our stdin, stdout, and stderr are always ready, most user code should + // never hit this. + bool zero_timeout = timeout && timeout->tv_sec == 0 && timeout->tv_nsec == 0; + if (ready == 0 && !zero_timeout) { + WEBP2_UNSUPPORTED("blocking ppoll"); + } + return ready; +} + ssize_t read(int fildes, void *buf, size_t nbyte) { if (nbyte > SSIZE_MAX) { errno = EINVAL; @@ -297,16 +373,6 @@ ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { return total_size; } -int fstat(int fildes, struct stat *buf) { - if (!fd_ok(fildes)) { - errno = EBADF; - return -1; - } - memset(buf, 0, sizeof(*buf)); - buf->st_mode = S_IFCHR; - return 0; -} - // ============================================================================ // Stubs @@ -318,28 +384,6 @@ int dup2(int fd, int newfd) { WEBP2_UNSUPPORTED("dup2"); } int dup3(int fd, int newfd, int flags) { WEBP2_UNSUPPORTED("dup3"); } -int ioctl(int fildes, int request, ...) { WEBP2_UNSUPPORTED("ioctl"); } - int pipe(int fd[2]) { WEBP2_UNSUPPORTED("pipe"); } int pipe2(int fd[2], int flags) { WEBP2_UNSUPPORTED("pipe2"); } - -int poll(struct pollfd *fds, nfds_t nfds, int timeout) { - WEBP2_UNSUPPORTED("poll"); -} - -int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, - const sigset_t *sigmask) { - WEBP2_UNSUPPORTED("ppoll"); -} - -int pselect(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, - fd_set *restrict errorfds, const struct timespec *restrict timeout, - const sigset_t *restrict sigmask) { - WEBP2_UNSUPPORTED("pselect"); -} - -int select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, - fd_set *restrict errorfds, struct timeval *restrict timeout) { - WEBP2_UNSUPPORTED("select"); -} From a11e8b785ffc8c567607ef48f803e759859af1be Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Wed, 14 Oct 2026 15:33:17 -0500 Subject: [PATCH 54/96] Add absurd(ly good?!) test framework --- test/CMakeLists.txt | 233 ++++++++++++++++++++---- test/src/compositions/barge_in_caller.c | 2 +- test/src/ioctl.c | 2 +- test/src/wasi-defines.c | 3 +- test/web-adapter/p2/adapter.c | 220 ++++++++++++++++++++++ test/web-adapter/p2/adapter.wit | 32 ++++ test/web-adapter/p2/run.c | 6 + 7 files changed, 463 insertions(+), 35 deletions(-) create mode 100644 test/web-adapter/p2/adapter.c create mode 100644 test/web-adapter/p2/adapter.wit create mode 100644 test/web-adapter/p2/run.c diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d3a4c32bf..34fcc7667 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -57,8 +57,12 @@ elseif(NOT ENGINE OR ENGINE STREQUAL "") # up-to-date. Note though that `wasm32-wasip1-threads` requires an explicitly # older version of Wasmtime as the latest releases no longer support # wasi-threads. + # + # TODO(webp2): Switch to v50.0.0 once it's released. set(wasmtime_version v48.0.1) - if (TARGET_TRIPLE MATCHES "-threads") + if (WEB) + set(wasmtime_version v50.0.0-rc.1) + elseif (TARGET_TRIPLE MATCHES "-threads") set(wasmtime_version v44.0.0) endif() ba_download( @@ -135,6 +139,10 @@ function(add_test_executable executable_name src) target_link_options(${executable_name} PRIVATE -pthread) endif() + if (WEB) + target_link_options(${executable_name} PRIVATE -mexec-model=reactor) + endif() + if(arg_SHARED) set_pic(${executable_name}) # Skip wit-component when linking to manually run `wasm-tools component @@ -191,6 +199,23 @@ function(add_test_executable executable_name src) target_link_libraries(${executable_name} PRIVATE -Wl,--whole-archive ${lib} -Wl,--no-whole-archive) endforeach() + + # To run in the `wasmtime` CLI web targets are composed with an adapter + # that implements web APIs in terms of WASI APIs. + if (WEB) + target_link_libraries(${executable_name} PRIVATE + -Wl,--whole-archive web_test_command_bindings -Wl,--no-whole-archive) + add_custom_command( + TARGET ${executable_name} + POST_BUILD + COMMAND + ${wasm_tools} compose $ + -d ${WEB_ADAPTER} -o $ --quiet + ) + set_property(TARGET ${executable_name} APPEND PROPERTY + LINK_DEPENDS ${WEB_ADAPTER}) + add_dependencies(${executable_name} web-adapter wasm-tools) + endif() endfunction() # Adds a new test to run. @@ -247,6 +272,9 @@ function(register_test test_name executable_name) $<$:-Wl,-mllvm=-wasm-use-legacy-eh=false> ) endif() + if (WEB) + list(APPEND wasmtime_args --wasm component-model-implements,component-model-accessors) + endif() if (arg_FILE) set(test_program ${arg_FILE}) @@ -291,11 +319,17 @@ endfunction() # on a per-test basis. All other arguments are forwarded to `add_test_executable` # and `register_test`. function(add_test_pair test_name test_file) - set(options NOSHARED) + set(options NOSHARED FS NETWORK) set(oneValueArgs) - set(multiValueArgs) + set(multiValueArgs ARGV ENV) cmake_parse_arguments(PARSE_ARGV 1 arg "${options}" "${oneValueArgs}" "${multiValueArgs}") + # The webp2 target has no filesystem or network, and the host can't provide + # arguments or environment variables. + if (WEB AND (arg_FS OR arg_NETWORK OR arg_ARGV OR arg_ENV)) + return() + endif() + add_test_executable(${test_name} ${test_file} ${ARGN}) register_test(${test_name} ${test_name} ${ARGN}) @@ -393,6 +427,152 @@ function(add_libc_test test_file) add_test_pair(${test_name} ${test_file} ${ARGN}) endfunction() +# Generates C bindings with `wit-bindgen` for `world` in `wit` into `out_dir` +# and creates a static library `name` out of them. +# +# Note that `wit` may be a list of paths, see `wit-bindgen c --help`. +# +# Optional arguments: +# +# * `FLAGS a b c` - additional flags to pass to `wit-bindgen c` +function(add_wit_bindgen_library name wit world out_dir) + cmake_parse_arguments(PARSE_ARGV 4 arg "" "" "FLAGS") + + # Generated files are named after the world, without any package qualifier, + # in `snake_case`. + string(REGEX REPLACE ".*/" "" file ${world}) + string(REPLACE "-" "_" file ${file}) + add_custom_command( + OUTPUT + ${out_dir}/${file}.c + ${out_dir}/${file}.h + ${out_dir}/${file}_component_type.o + COMMAND + ${wit_bindgen} c ${wit} --world ${world} --out-dir ${out_dir} ${arg_FLAGS} + DEPENDS wit-bindgen ${wit} + ) + add_library(${name} STATIC EXCLUDE_FROM_ALL + ${out_dir}/${file}.c + ${out_dir}/${file}_component_type.o) + target_include_directories(${name} PUBLIC ${out_dir}) + target_compile_options(${name} PRIVATE -fPIC) +endfunction() + +# ========= Web adapter ========================================= +# +# Tests for the webp2 target import `wasm:js` interfaces which are normally +# provided by a JS host. To run them in Wasmtime instead they're composed with +# an adapter component, built here, which implements those interfaces with +# WASIp2. See `web-adapter/p2/adapter.wit` for more information. + +if (WEB) + set(web_adapter_src "${CMAKE_CURRENT_SOURCE_DIR}/web-adapter/p2") + set(web_adapter_dir "${CMAKE_CURRENT_BINARY_DIR}/web-adapter") + set(web_adapter_wit + ${CMAKE_SOURCE_DIR}/web/p2/wit/deps/web.wit + ${CMAKE_SOURCE_DIR}/wasi/p2/wit + ${web_adapter_src}/adapter.wit) + + # A `wasi:cli/run` export for test programs, see the `test-command` world. + add_wit_bindgen_library(web_test_command_bindings + "${web_adapter_wit}" wasi-libc:web-adapter/test-command ${web_adapter_dir}) + target_sources(web_test_command_bindings PRIVATE ${web_adapter_src}/run.c) + add_dependencies(web_test_command_bindings sysroot) + + # Note that the library itself here is unused, only the generated sources are + # used when building the adapter below. + add_wit_bindgen_library(web_adapter_bindings + "${web_adapter_wit}" wasi-libc:web-adapter/adapter ${web_adapter_dir} + FLAGS + --rename wasm:js/global=global + --rename wasm:js/console=console) + + # The adapter is a `wasm32-wasip2` program, not linked against the webp2 libc + # under test, so it's built against a sysroot and compiler-rt downloaded from + # wasi-sdk. This way the compiler doesn't need to have its own wasip2 sysroot. + set(web_adapter_wasi_sdk_version 34) + set(web_adapter_wasi_sdk_url + "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${web_adapter_wasi_sdk_version}") + ExternalProject_Add( + web-adapter-sysroot + EXCLUDE_FROM_ALL ON + URL "${web_adapter_wasi_sdk_url}/wasi-sysroot-${web_adapter_wasi_sdk_version}.0.tar.gz" + DOWNLOAD_EXTRACT_TIMESTAMP ON + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + ) + ExternalProject_Get_Property(web-adapter-sysroot SOURCE_DIR) + set(web_adapter_sysroot ${SOURCE_DIR}) + ExternalProject_Add( + web-adapter-builtins + EXCLUDE_FROM_ALL ON + URL "${web_adapter_wasi_sdk_url}/libclang_rt-${web_adapter_wasi_sdk_version}.0.tar.gz" + DOWNLOAD_EXTRACT_TIMESTAMP ON + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + ) + ExternalProject_Get_Property(web-adapter-builtins SOURCE_DIR) + set(web_adapter_builtins_src ${SOURCE_DIR}/wasm32-unknown-wasip2/libclang_rt.builtins.a) + + # Place compiler-rt wherever the compiler expects to find it for wasip2 + # within a custom resource dir, the same as `cmake/builtins.cmake` does. + execute_process( + COMMAND ${CMAKE_C_COMPILER} --target=wasm32-wasip2 --print-libgcc-file-name + OUTPUT_VARIABLE web_adapter_system_builtins + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + cmake_path( + RELATIVE_PATH web_adapter_system_builtins + BASE_DIRECTORY ${system_resource_dir} + OUTPUT_VARIABLE web_adapter_builtins_rel + ) + set(web_adapter_resource_dir ${web_adapter_dir}/resource-dir) + set(web_adapter_builtins ${web_adapter_resource_dir}/${web_adapter_builtins_rel}) + add_custom_command( + OUTPUT ${web_adapter_builtins} + COMMAND ${CMAKE_COMMAND} -E copy ${web_adapter_builtins_src} ${web_adapter_builtins} + COMMAND ${CMAKE_COMMAND} -E create_symlink ${system_resource_dir}/include ${web_adapter_resource_dir}/include + DEPENDS web-adapter-builtins + ) + + set(web_adapter_link_flags) + set(web_adapter_link_deps) + if (WASM_COMPONENT_LD_EXECUTABLE) + list(APPEND web_adapter_link_flags -fuse-ld=${WASM_COMPONENT_LD_EXECUTABLE}) + if (TARGET wasm-component-ld) + list(APPEND web_adapter_link_deps wasm-component-ld) + endif() + endif() + + # The adapter itself. The compiler is invoked directly to avoid all the flags + # configured above for tests. + set(WEB_ADAPTER ${web_adapter_dir}/web-adapter.wasm) + add_custom_command( + OUTPUT ${WEB_ADAPTER} + COMMAND + ${CMAKE_C_COMPILER} --target=wasm32-wasip2 -mexec-model=reactor + --sysroot=${web_adapter_sysroot} + -resource-dir ${web_adapter_resource_dir} + ${web_adapter_link_flags} + -Wall -Wextra -Werror -g -I${web_adapter_dir} + ${web_adapter_src}/adapter.c + ${web_adapter_dir}/adapter.c + ${web_adapter_dir}/adapter_component_type.o + -o ${WEB_ADAPTER} + DEPENDS + ${web_adapter_src}/adapter.c + ${web_adapter_dir}/adapter.c + ${web_adapter_dir}/adapter.h + ${web_adapter_dir}/adapter_component_type.o + ${web_adapter_builtins} + web-adapter-sysroot + ${web_adapter_link_deps} + ) + add_custom_target(web-adapter DEPENDS ${WEB_ADAPTER}) +endif() + # ========= libc-test defined tests ============================= add_libc_test(functional/argv.c CFLAGS -Wno-sign-compare) @@ -464,7 +644,9 @@ if (NOT TARGET_TRIPLE MATCHES "-threads") endif() add_wasilibc_test(append.c FS) add_wasilibc_test(argv_two_args.c ARGV foo bar) -add_wasilibc_test(clock_nanosleep.c) +if (NOT WEB) + add_wasilibc_test(clock_nanosleep.c) +endif() add_wasilibc_test(chdir.c FS) add_wasilibc_test(close.c FS) add_wasilibc_test(pread.c FS) @@ -533,9 +715,11 @@ add_wasilibc_test(time.c) add_wasilibc_test(utime.c FS) add_wasilibc_test(rewinddir.c FS) add_wasilibc_test(seekdir.c FS) -add_wasilibc_test(usleep.c) -add_wasilibc_test(nanosleep.c) -add_wasilibc_test(sleep.c) +if (NOT WEB) + add_wasilibc_test(usleep.c) + add_wasilibc_test(nanosleep.c) + add_wasilibc_test(sleep.c) +endif() add_wasilibc_test(write.c FS) add_wasilibc_test(wasi-defines.c) add_wasilibc_test(dup-file.c FS) @@ -543,12 +727,14 @@ add_wasilibc_test(dup2-file.c FS) add_wasilibc_test(dup3-file.c FS) add_wasilibc_test(fcntl-dupfd.c FS) add_wasilibc_test(clear_fds.c FS) -add_wasilibc_test(pipe.c) -add_wasilibc_test(pipe_poll.c) -add_wasilibc_test(pipe_fstat.c) -add_wasilibc_test(pipe_setfl_independent.c) -add_wasilibc_test(pipe_atomic_write.c) -add_wasilibc_test(pipe_zero_len.c) +if (NOT WEB) + add_wasilibc_test(pipe.c) + add_wasilibc_test(pipe_poll.c) + add_wasilibc_test(pipe_fstat.c) + add_wasilibc_test(pipe_setfl_independent.c) + add_wasilibc_test(pipe_atomic_write.c) + add_wasilibc_test(pipe_zero_len.c) +endif() if (NOT TEST_WITH_V8) add_wasilibc_test(pipe_espipe.c NETWORK) add_wasilibc_test(task_tls_basic.c) @@ -693,23 +879,6 @@ endif() # ========= wasi-libc tests for non-main entrypoints ========================= -function(add_wit_bindgen_library name wit world out_dir) - add_custom_command( - OUTPUT - ${out_dir}/${world}.c - ${out_dir}/${world}.h - ${out_dir}/${world}_component_type.o - COMMAND - ${wit_bindgen} c ${wit} --world ${world} --out-dir ${out_dir} - DEPENDS wit-bindgen ${wit} - ) - add_library(${name} STATIC EXCLUDE_FROM_ALL - ${out_dir}/${world}.c - ${out_dir}/${world}_component_type.o) - target_include_directories(${name} PUBLIC ${out_dir}) - target_compile_options(${name} PRIVATE -fPIC) -endfunction() - add_wit_bindgen_library( entrypoint_bindings ${CMAKE_CURRENT_SOURCE_DIR}/entrypoint.wit @@ -732,7 +901,7 @@ function(add_external_entrypoint_test test_file) ) endif() - if (NOT TEST_WITH_V8) + if (NOT TEST_WITH_V8 AND NOT WEB) add_test_pair(${test_name} ${test_file} ${ARGN} ${extra_args}) endif() endfunction() @@ -741,7 +910,7 @@ add_external_entrypoint_test(external_entrypoint_pthread_tls.c) # ========= sockets-related tests =============================== -if (NOT (WASI STREQUAL "p1")) +if (NOT (WASI STREQUAL "p1") AND NOT WEB) add_wasilibc_test(poll-connect.c NETWORK) add_wasilibc_test(poll-badfd.c) add_wasilibc_test(poll-nonblocking-socket.c NETWORK) diff --git a/test/src/compositions/barge_in_caller.c b/test/src/compositions/barge_in_caller.c index d04f13eb8..172d7ecf9 100644 --- a/test/src/compositions/barge_in_caller.c +++ b/test/src/compositions/barge_in_caller.c @@ -17,7 +17,7 @@ int main() { // For wasip2 this `main` function is a synchronous entrypoint so we can't // cancel the task which would block, but for wasip3 and beyond it's ok to // block so it's ok to cancel. -#ifdef __wasip2__ +#if defined(__wasip2__) || defined(__webp2__) (void)subtask; #else status = caller_subtask_cancel(subtask); diff --git a/test/src/ioctl.c b/test/src/ioctl.c index d3494dfb4..ad7750927 100644 --- a/test/src/ioctl.c +++ b/test/src/ioctl.c @@ -28,7 +28,7 @@ int main(void) { #if defined(__wasip1__) // Always returns 0? TEST(ioctl(fd, FIONREAD, &nbytes) == 0); -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) // Not supported on wasip{2,3} TEST(ioctl(fd, FIONREAD, &nbytes) == -1); #else diff --git a/test/src/wasi-defines.c b/test/src/wasi-defines.c index 63aae27a7..fca544aff 100644 --- a/test/src/wasi-defines.c +++ b/test/src/wasi-defines.c @@ -1,7 +1,8 @@ #include int main() { -#if defined(__wasip1__) || defined(__wasip2__) || defined(__wasip3__) +#if defined(__wasip1__) || defined(__wasip2__) || defined(__wasip3__) || \ + defined(__webp2__) return 0; #else #error "Expected a wasi #define" diff --git a/test/web-adapter/p2/adapter.c b/test/web-adapter/p2/adapter.c new file mode 100644 index 000000000..b409b60ec --- /dev/null +++ b/test/web-adapter/p2/adapter.c @@ -0,0 +1,220 @@ +// Test-only implementation of the `wasm:js` interfaces imported by the webp2 +// target, written in terms of the libc of a stock `wasm32-wasip2` sysroot. See +// `adapter.wit` for more information. + +#include "adapter.h" +#include +#include +#include +#include + +static void *xmalloc(size_t size) { + void *ret = malloc(size); + if (!ret) + abort(); + return ret; +} + +// ============================================================================ +// `wasm:js/console` + +// `console.log` always terminates its output with a newline. Note that this +// component is a reactor and never exits, so flush eagerly. +void exports_console_log(adapter_string_t *msg) { + fwrite(msg->ptr, 1, msg->len, stdout); + fputc('\n', stdout); + fflush(stdout); + adapter_string_free(msg); +} + +// Browsers show `console.warn` alongside `console.log`, but here it's mirrored +// on stderr so tests can distinguish the two. +void exports_console_warn(adapter_string_t *msg) { + fwrite(msg->ptr, 1, msg->len, stderr); + fputc('\n', stderr); + fflush(stderr); + adapter_string_free(msg); +} + +// ============================================================================ +// `Performance` +// +// `performance.timeOrigin` is the wall-clock time, in milliseconds, at which +// the environment started and `performance.now()` is the monotonic time in +// milliseconds elapsed since then. Here the "start" is the first time that +// either is needed. + +struct exports_global_performance_t { + double start; + double time_origin; +}; + +static double clock_ms(clockid_t clock) { + struct timespec ts; + if (clock_gettime(clock, &ts) != 0) + abort(); + return (double)ts.tv_sec * 1000.0 + (double)ts.tv_nsec / 1e6; +} + +static exports_global_performance_t *performance(void) { + static bool init = false; + static exports_global_performance_t perf; + if (!init) { + perf.start = clock_ms(CLOCK_MONOTONIC); + perf.time_origin = clock_ms(CLOCK_REALTIME); + init = true; + } + return &perf; +} + +exports_global_own_performance_t exports_global_get_global_performance(void) { + return exports_global_performance_new(performance()); +} + +double exports_global_method_performance_now( + exports_global_borrow_performance_t self) { + return clock_ms(CLOCK_MONOTONIC) - self->start; +} + +double exports_global_method_get_performance_time_origin( + exports_global_borrow_performance_t self) { + return self->time_origin; +} + +// All handles share the same static representation so there's nothing to do. +void exports_global_performance_destructor(exports_global_performance_t *rep) { + (void)rep; +} + +// ============================================================================ +// `Date` + +// `Date.now()` is an integral number of milliseconds since the Unix epoch. +double exports_global_static_date_now(void) { + struct timespec ts; + if (clock_gettime(CLOCK_REALTIME, &ts) != 0) + abort(); + return (double)((long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000); +} + +// `date` has no constructor so this is never called. +void exports_global_date_destructor(exports_global_date_t *rep) { + (void)rep; + abort(); +} + +// ============================================================================ +// `Error` and `reportError` + +struct exports_global_error_t { + adapter_option_string_t message; +}; + +exports_global_own_error_t exports_global_constructor_error( + adapter_string_t *maybe_message, + exports_global_new_error_options_t *maybe_options) { + exports_global_error_t *error = xmalloc(sizeof(*error)); + error->message.is_some = maybe_message != NULL; + if (maybe_message) + error->message.val = *maybe_message; + // `cause` isn't currently rendered anywhere, so discard it. + if (maybe_options) + exports_global_new_error_options_free(maybe_options); + return exports_global_error_new(error); +} + +void exports_global_error_destructor(exports_global_error_t *rep) { + adapter_option_string_free(&rep->message); + free(rep); +} + +// Browsers log uncaught errors to the console, so mirror that on stderr. Note +// that `reportError` does not throw, so this returns normally. +void exports_global_report_error(exports_global_borrow_error_t throwable) { + fputs("Uncaught Error", stderr); + if (throwable->message.is_some) + fprintf(stderr, ": %.*s", (int)throwable->message.val.len, + (const char *)throwable->message.val.ptr); + fputc('\n', stderr); + fflush(stderr); +} + +// ============================================================================ +// `Uint8Array` +// +// Only the length of the array is tracked since contents never flow from the +// guest to the host through this type. + +struct exports_global_uint_8_array_t { + uint32_t length; +}; + +static exports_global_own_uint_8_array_t new_uint_8_array(uint32_t length) { + exports_global_uint_8_array_t *array = xmalloc(sizeof(*array)); + array->length = length; + return exports_global_uint_8_array_new(array); +} + +exports_global_own_uint_8_array_t +exports_global_constructor_uint_8_array(uint32_t length) { + return new_uint_8_array(length); +} + +// Mirrors `TypedArray.prototype.subarray` for non-negative indices: both ends +// are clamped to the length of the array and `end` defaults to the length. +exports_global_own_uint_8_array_t exports_global_method_uint_8_array_subarray( + exports_global_borrow_uint_8_array_t self, uint32_t begin, + uint32_t *maybe_end) { + uint32_t end = maybe_end ? *maybe_end : self->length; + if (end > self->length) + end = self->length; + if (begin > end) + begin = end; + return new_uint_8_array(end - begin); +} + +void exports_global_uint_8_array_destructor( + exports_global_uint_8_array_t *rep) { + free(rep); +} + +// ============================================================================ +// `Crypto` + +struct exports_global_crypto_t { + char unused; +}; + +static exports_global_crypto_t crypto; + +exports_global_own_crypto_t exports_global_get_global_crypto(void) { + return exports_global_crypto_new(&crypto); +} + +void exports_global_method_crypto_get_random_values( + exports_global_borrow_crypto_t self, + exports_global_borrow_uint_8_array_t typed_array, adapter_list_u8_t *ret) { + (void)self; + // Browsers throw a `QuotaExceededError` for requests larger than this, so + // mirror that limit to catch callers which don't respect it. + uint32_t len = typed_array->length; + if (len > 65536) + abort(); + ret->len = len; + ret->ptr = NULL; + if (len == 0) + return; + // This is freed by the generated post-return function. + ret->ptr = xmalloc(len); + // `getentropy` is limited to 256 bytes per call. + for (uint32_t i = 0; i < len; i += 256) { + size_t chunk = len - i < 256 ? len - i : 256; + if (getentropy(ret->ptr + i, chunk) != 0) + abort(); + } +} + +// All handles share the same static representation so there's nothing to do. +void exports_global_crypto_destructor(exports_global_crypto_t *rep) { + (void)rep; +} diff --git a/test/web-adapter/p2/adapter.wit b/test/web-adapter/p2/adapter.wit new file mode 100644 index 000000000..e74e7be55 --- /dev/null +++ b/test/web-adapter/p2/adapter.wit @@ -0,0 +1,32 @@ +// A test-only adapter which implements `wasm:js` in terms of WASIp2. This is +// objectively absurd but is intended to be temporary; as soon as there is a +// build of a browser or JS shell that supports components and web APIs, tests +// can run against that instead. +// +// Components built for webp2 import the interfaces in `web/p2/wit` which are +// expected to be provided by a JS host. To run them in Wasmtime they're +// composed with this adapter, which exports those interfaces. The adapter is +// built against a stock `wasm32-wasip2` sysroot, not the webp2 libc being +// tested, so it only imports WASI and the composition is runnable by any +// WASIp2 host. +// +// Dependencies are resolved from `web/p2/wit` and `wasi/p2/wit`, see +// `test/CMakeLists.txt`. +package wasi-libc:web-adapter; + +world adapter { + export wasm:js/global; + @external-id("wasm:js/global/console") + export wasm:js/console; +} + +// Components built for webp2 run `main` during instantiation and export +// nothing, but Wasmtime needs an export to run. Test programs are additionally +// linked with an implementation of this world, see `run.c`, which does nothing +// since by the time it's invoked `main` has already finished. +// +// Note that ideally the adapter would export this instead, but +// `wasm-tools compose` only re-exports the exports of the root component. +world test-command { + export wasi:cli/run@0.2.12; +} diff --git a/test/web-adapter/p2/run.c b/test/web-adapter/p2/run.c new file mode 100644 index 000000000..7be9c01c5 --- /dev/null +++ b/test/web-adapter/p2/run.c @@ -0,0 +1,6 @@ +// Linked into all webp2 test programs to give them a `wasi:cli/run` export. +// See the `test-command` world in `adapter.wit`. + +#include "test_command.h" + +bool exports_wasi_cli_run_run(void) { return true; } From 9a6ba1aa03d83feeb014722d7f928e35b414ce0a Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Wed, 14 Oct 2026 16:50:57 -0500 Subject: [PATCH 55/96] Switch back from reactor to command for webp2 But a modified command that doesn't run dtors! --- libc-bottom-half/CMakeLists.txt | 38 ++++++++++++----------------- libc-bottom-half/crt/crt1-command.c | 16 +++++++++++- libc-bottom-half/crt/crt1-reactor.c | 22 ----------------- test/CMakeLists.txt | 4 --- 4 files changed, 30 insertions(+), 50 deletions(-) diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index bc3d04e8c..029dcd89b 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -282,10 +282,7 @@ endforeach() # "object libraries" with CMake and then they're copied into the final location # with adjustments based on WASI versions. -set(crt_sources crt/crt1-reactor.c) -if (NOT WEB STREQUAL "p2") - list(APPEND crt_sources crt/crt1-command.c) -endif() +set(crt_sources crt/crt1-reactor.c crt/crt1-command.c) foreach(file ${crt_sources}) cmake_path(GET file STEM stem) @@ -306,15 +303,13 @@ add_custom_command( DEPENDS crt1-reactor $ ) -if (WASI STREQUAL "p1") - # wasip1: crt1-command.o is a straight copy of what CMake produces +if (WASI STREQUAL "p1" OR WEB STREQUAL "p2") + # crt1-command.o is a straight copy of what CMake produces add_custom_command( OUTPUT ${crt_sysroot}/crt1-command.o COMMAND ${CMAKE_COMMAND} -E copy $ ${crt_sysroot}/crt1-command.o DEPENDS crt1-command $ ) -elseif(WEB STREQUAL "p2") - # webp2: crt1-command.o is not necessary else() if (WASI STREQUAL "p2") set(world wasi:cli/command@${wasip2-version}) @@ -354,21 +349,18 @@ else() ) endif() -set(startup_objects ${crt_sysroot}/crt1-reactor.o) -if (NOT WEB STREQUAL "p2") - # Provide a plain crt1.o for toolchain compatibility, identical to - # `crt1-command.c` - add_custom_command( - OUTPUT ${crt_sysroot}/crt1.o - COMMAND ${CMAKE_COMMAND} -E copy ${crt_sysroot}/crt1-command.o ${crt_sysroot}/crt1.o - DEPENDS ${crt_sysroot}/crt1-command.o - ) - - list(APPEND startup_objects - ${crt_sysroot}/crt1-command.o - ${crt_sysroot}/crt1.o - ) -endif() +# Provide a plain crt1.o for toolchain compatibility, identical to +# `crt1-command.c` +add_custom_command( + OUTPUT ${crt_sysroot}/crt1.o + COMMAND ${CMAKE_COMMAND} -E copy ${crt_sysroot}/crt1-command.o ${crt_sysroot}/crt1.o + DEPENDS ${crt_sysroot}/crt1-command.o +) +set(startup_objects + ${crt_sysroot}/crt1-reactor.o + ${crt_sysroot}/crt1-command.o + ${crt_sysroot}/crt1.o +) add_custom_target(sysroot-startup-objects DEPENDS ${startup_objects}) add_dependencies(sysroot sysroot-startup-objects) diff --git a/libc-bottom-half/crt/crt1-command.c b/libc-bottom-half/crt/crt1-command.c index f047082f2..cb7f84c30 100644 --- a/libc-bottom-half/crt/crt1-command.c +++ b/libc-bottom-half/crt/crt1-command.c @@ -1,3 +1,4 @@ +#include "wasi/report-error.h" #ifdef _REENTRANT #include #endif @@ -21,10 +22,15 @@ __attribute__((export_name("_start"))) void _start(void) __attribute__((export_name("wasi:cli/run@0.2.12#run"))) int _start(void) #elif defined(__wasip3__) __attribute__((export_name("wasi:cli/run@0.3.0#run"))) int _start(void) +#elif defined(__webp2__) +// On webp2 we export as _initialize so that the linker will call this from a +// start function instead of being called by the host. +__attribute__((export_name("_initialize"))) void _start(void) #else #error "Unsupported WASI version" #endif { +#ifndef __webp2__ // Commands should only be called once per instance. This simple check // ensures that the `_start` function isn't started more than once. // @@ -44,6 +50,7 @@ __attribute__((export_name("wasi:cli/run@0.3.0#run"))) int _start(void) __builtin_trap(); } started = 1; +#endif #endif // The linker synthesizes this to call constructors. @@ -54,18 +61,25 @@ __attribute__((export_name("wasi:cli/run@0.3.0#run"))) int _start(void) // arguments and calls `__main_argv_argc`. int r = __main_void(); +#ifndef __webp2__ // Call atexit functions, destructors, stdio cleanup, etc. __wasm_call_dtors(); +#endif +#if defined(__wasip1__) // If main exited successfully, just return, otherwise call // `__wasi_proc_exit`. -#if defined(__wasip1__) if (r != 0) { __wasi_proc_exit(r); } #elif defined(__wasip2__) || defined(__wasip3__) __wasilibc_assert_no_descriptor_leaks(); return r != 0; +#elif defined(__webp2__) + if (r != 0) { + __wasilibc_webp2_report_error("main exited with nonzero status"); + __builtin_trap(); + } #else #error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/crt/crt1-reactor.c b/libc-bottom-half/crt/crt1-reactor.c index ea0fd2316..019f2df3e 100644 --- a/libc-bottom-half/crt/crt1-reactor.c +++ b/libc-bottom-half/crt/crt1-reactor.c @@ -1,15 +1,9 @@ -#include "wasi/report-error.h" - #if defined(_REENTRANT) #include #endif extern void __wasm_call_ctors(void); -#ifdef __webp2__ -extern int __main_void(void); -#endif - #include "wasip3_symbol_references.h" __attribute__((export_name("_initialize"))) void _initialize(void) { @@ -29,20 +23,4 @@ __attribute__((export_name("_initialize"))) void _initialize(void) { // The linker synthesizes this to call constructors. __wasm_call_ctors(); - -#ifdef __webp2__ - // The webp2 target gets a slightly modified reactor model where the main - // function is executed automatically but nothing is torn down on exit, - // mimicking Emscripten's EXIT_RUNTIME=0 behavior. - // - // This not only provides a natural way for web modules to provide - // initialization and module side effects, but also ensures that a user- - // provided main is never inadvertently dropped during linking, which tends - // to break various cmake checks when reactor is the default. - int main_status = __main_void(); - if (main_status != 0) { - __wasilibc_webp2_report_error("main exited with nonzero status"); - __builtin_trap(); - } -#endif } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 34fcc7667..012f6bf0e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -139,10 +139,6 @@ function(add_test_executable executable_name src) target_link_options(${executable_name} PRIVATE -pthread) endif() - if (WEB) - target_link_options(${executable_name} PRIVATE -mexec-model=reactor) - endif() - if(arg_SHARED) set_pic(${executable_name}) # Skip wit-component when linking to manually run `wasm-tools component From 3cd9c7f012100ba6d85cc61098ddb9fbccc0d1cd Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Wed, 14 Oct 2026 17:08:44 -0500 Subject: [PATCH 56/96] Enable tests in CI --- .github/workflows/main.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c7a20846b..920d513e9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -152,8 +152,7 @@ jobs: os: ubuntu-24.04 clang_version: 22 upload: wasm32-webp2 - # TODO(webp2): Enable tests once there is a way to run them. - test: false + test: true args: -DTARGET_TRIPLE=wasm32-webp2 - name: Test emmalloc From 446372859471bd55e9c06e1d75e66b7cadaa334b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 14 Oct 2026 15:14:52 -0700 Subject: [PATCH 57/96] Update wit-bindgen dep --- cmake/bindings.cmake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmake/bindings.cmake b/cmake/bindings.cmake index 88cc06e12..75954f117 100644 --- a/cmake/bindings.cmake +++ b/cmake/bindings.cmake @@ -15,8 +15,8 @@ if(WIT_BINDGEN_EXECUTABLE) OUTPUT_VARIABLE WIT_BINDGEN_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE) - if (NOT (WIT_BINDGEN_VERSION MATCHES "0\\.62\\.0")) - message(WARNING "wit-bindgen version 0.62.0 is required, found: ${WIT_BINDGEN_VERSION}") + if (NOT (WIT_BINDGEN_VERSION MATCHES "0\\.63\\.0")) + message(WARNING "wit-bindgen version 0.63.0 is required, found: ${WIT_BINDGEN_VERSION}") set(WIT_BINDGEN_EXECUTABLE "") endif() endif() @@ -26,7 +26,7 @@ if (NOT WIT_BINDGEN_EXECUTABLE) ba_download( wit-bindgen "https://github.com/bytecodealliance/wit-bindgen" - "0.62.0" + "0.63.0" ) ExternalProject_Get_Property(wit-bindgen SOURCE_DIR) set(wit_bindgen "${SOURCE_DIR}/wit-bindgen") From 3f7379698ff8f2403f8eebf7f9494e51fb35f0f6 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 14 Oct 2026 15:54:08 -0700 Subject: [PATCH 58/96] Try to fix tests --- test/CMakeLists.txt | 5 +++-- test/src/ctors_run_once.c | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 test/src/ctors_run_once.c diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 012f6bf0e..9df0b4d2b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -148,16 +148,16 @@ function(add_test_executable executable_name src) # # Note that `-fvisibility=default` is used to make the generated # `__main_void` symbol from clang visible to wasi-libc itself. - target_link_options(${executable_name} PRIVATE -Wl,--skip-wit-component,-shared) + target_link_options(${executable_name} PRIVATE -Wl,--skip-wit-component,-shared,--entry=_start) target_compile_options(${executable_name} PRIVATE -fvisibility=default) add_custom_command( TARGET ${executable_name} POST_BUILD COMMAND ${wasm_tools} component link - $ "${SYSROOT_LIB}/libc.so" ${arg_SHARED_LIBS} + $ -o ${executable_name} ) add_dependencies(${executable_name} wasm-tools) @@ -645,6 +645,7 @@ if (NOT WEB) endif() add_wasilibc_test(chdir.c FS) add_wasilibc_test(close.c FS) +add_wasilibc_test(ctors_run_once.c) add_wasilibc_test(pread.c FS) add_wasilibc_test(mmap-pread-fail.c FS CFLAGS -D_WASI_EMULATED_MMAN diff --git a/test/src/ctors_run_once.c b/test/src/ctors_run_once.c new file mode 100644 index 000000000..9cd496e5d --- /dev/null +++ b/test/src/ctors_run_once.c @@ -0,0 +1,15 @@ +#undef NDEBUG + +#include + +// Static constructors must run exactly once, including when the test is +// dynamically linked against `libc.so`. + +static int ctor_count; + +__attribute__((constructor)) static void ctor(void) { ctor_count++; } + +int main() { + assert(ctor_count == 1); + return 0; +} From b9d2628556eae404d51adfeb6954c161c631bc3c Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 14 Oct 2026 15:55:01 -0700 Subject: [PATCH 59/96] Update generated bindings --- cmake/bindings.cmake | 42 +++++++++--------- .../headers/public/wasi/__generated_wasip2.h | 2 +- .../headers/public/wasi/__generated_wasip3.h | 2 +- .../headers/public/wasi/__generated_webp2.h | 2 +- libc-bottom-half/sources/wasip2.c | 2 +- .../sources/wasip2_component_type.o | Bin 10911 -> 10911 bytes libc-bottom-half/sources/wasip3.c | 2 +- .../sources/wasip3_component_type.o | Bin 8007 -> 8007 bytes libc-bottom-half/sources/webp2.c | 2 +- .../sources/webp2_component_type.o | Bin 929 -> 929 bytes 10 files changed, 27 insertions(+), 27 deletions(-) diff --git a/cmake/bindings.cmake b/cmake/bindings.cmake index 75954f117..1600b2349 100644 --- a/cmake/bindings.cmake +++ b/cmake/bindings.cmake @@ -110,27 +110,27 @@ add_custom_target( # Disable async bindings generation for some functions which are only # ever called synchronously within libc. - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.metadata-hash" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.metadata-hash-at" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.stat" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.stat-at" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.get-flags" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.open-at" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.read-directory" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.create-directory-at" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.remove-directory-at" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.unlink-file-at" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.advise" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.sync-data" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.sync" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.set-size" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.symlink-at" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.link-at" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.readlink-at" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.rename-at" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.set-times-at" - "--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.set-times" - "--async=-wasi:sockets/ip-name-lookup@${wasip3-version}#resolve-addresses" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.metadata-hash" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.metadata-hash-at" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.stat" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.stat-at" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.get-flags" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.open-at" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.read-directory" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.create-directory-at" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.remove-directory-at" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.unlink-file-at" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.advise" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.sync-data" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.sync" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.set-size" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.symlink-at" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.link-at" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.readlink-at" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.rename-at" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.set-times-at" + "--sync=wasi:filesystem/types@${wasip3-version}#[method]descriptor.set-times" + "--sync=wasi:sockets/ip-name-lookup@${wasip3-version}#resolve-addresses" ${CMAKE_SOURCE_DIR}/wasi/p3/wit COMMAND cmake -E copy wasip3.h ${bottom_half}/headers/public/wasi/__generated_wasip3.h COMMAND cmake -E copy wasip3_component_type.o ${bottom_half}/sources diff --git a/libc-bottom-half/headers/public/wasi/__generated_wasip2.h b/libc-bottom-half/headers/public/wasi/__generated_wasip2.h index b1f7da370..d136293b8 100644 --- a/libc-bottom-half/headers/public/wasi/__generated_wasip2.h +++ b/libc-bottom-half/headers/public/wasi/__generated_wasip2.h @@ -1,4 +1,4 @@ -// Generated by `wit-bindgen` 0.62.0. DO NOT EDIT! +// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT! #ifndef __BINDINGS_WASIP2_H #define __BINDINGS_WASIP2_H #ifdef __cplusplus diff --git a/libc-bottom-half/headers/public/wasi/__generated_wasip3.h b/libc-bottom-half/headers/public/wasi/__generated_wasip3.h index acdf940ab..d87be7294 100644 --- a/libc-bottom-half/headers/public/wasi/__generated_wasip3.h +++ b/libc-bottom-half/headers/public/wasi/__generated_wasip3.h @@ -1,4 +1,4 @@ -// Generated by `wit-bindgen` 0.62.0. DO NOT EDIT! +// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT! #ifndef __BINDINGS_WASIP3_H #define __BINDINGS_WASIP3_H #ifdef __cplusplus diff --git a/libc-bottom-half/headers/public/wasi/__generated_webp2.h b/libc-bottom-half/headers/public/wasi/__generated_webp2.h index f57e103a4..865dc8b37 100644 --- a/libc-bottom-half/headers/public/wasi/__generated_webp2.h +++ b/libc-bottom-half/headers/public/wasi/__generated_webp2.h @@ -1,4 +1,4 @@ -// Generated by `wit-bindgen` 0.62.0. DO NOT EDIT! +// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT! #ifndef __BINDINGS_WEBP2_H #define __BINDINGS_WEBP2_H #ifdef __cplusplus diff --git a/libc-bottom-half/sources/wasip2.c b/libc-bottom-half/sources/wasip2.c index 3231d1a6e..076789afe 100644 --- a/libc-bottom-half/sources/wasip2.c +++ b/libc-bottom-half/sources/wasip2.c @@ -1,4 +1,4 @@ -// Generated by `wit-bindgen` 0.62.0. DO NOT EDIT! +// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT! #include "wasi/wasip2.h" #include #include diff --git a/libc-bottom-half/sources/wasip2_component_type.o b/libc-bottom-half/sources/wasip2_component_type.o index 22e1cbd04d1f6b8673d7ae28a8b92543ce8abdcc..f17884dd7ec0f875b530ba4d9438e40a938bed55 100644 GIT binary patch delta 34 pcmbOqIzM!SkCvE`o&j%pW{GZ6W?o8qYMyQ~n}MF0@#G{eTL8+C3ex}p delta 34 pcmbOqIzM!SkCvE$o&j%pW{GZ6W?o8qYMyQ~n}MF0(c~m8TL8*e3eW%m diff --git a/libc-bottom-half/sources/wasip3.c b/libc-bottom-half/sources/wasip3.c index f1f9bca84..d6e6efae9 100644 --- a/libc-bottom-half/sources/wasip3.c +++ b/libc-bottom-half/sources/wasip3.c @@ -1,4 +1,4 @@ -// Generated by `wit-bindgen` 0.62.0. DO NOT EDIT! +// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT! #include "wasi/wasip3.h" #include #include diff --git a/libc-bottom-half/sources/wasip3_component_type.o b/libc-bottom-half/sources/wasip3_component_type.o index cc10cf11735a90ec82cd2467a1795734e624a4d3..706db58f182f2044c7b79a185b3cbdc847fc0743 100644 GIT binary patch delta 34 pcmX?Zcie8n7dbH_Jp #include diff --git a/libc-bottom-half/sources/webp2_component_type.o b/libc-bottom-half/sources/webp2_component_type.o index 3100321ba03a35656583c3c3f9a8af98bcc04868..cbee87e2adbb05fd1b7eaf56e2f956effba6360b 100644 GIT binary patch delta 19 acmZ3;zL0%`KQp7z Date: Wed, 14 Oct 2026 16:17:00 -0700 Subject: [PATCH 60/96] Update defined symbols --- expected/wasm32-wasip1-threads/predefined-macros.txt | 4 ++-- expected/wasm32-wasip1/predefined-macros.txt | 4 ++-- expected/wasm32-wasip2/predefined-macros.txt | 4 ++-- expected/wasm32-wasip3-coop/predefined-macros.txt | 4 ++-- expected/wasm32-wasip3/predefined-macros.txt | 4 ++-- expected/wasm32-webp2/defined-symbols.txt | 1 + expected/wasm32-webp2/predefined-macros.txt | 4 ++-- 7 files changed, 13 insertions(+), 12 deletions(-) diff --git a/expected/wasm32-wasip1-threads/predefined-macros.txt b/expected/wasm32-wasip1-threads/predefined-macros.txt index ffe56a831..e550864d3 100644 --- a/expected/wasm32-wasip1-threads/predefined-macros.txt +++ b/expected/wasm32-wasip1-threads/predefined-macros.txt @@ -3061,8 +3061,8 @@ #define __WASI_SUBCLOCKFLAGS_SUBSCRIPTION_CLOCK_ABSTIME ((__wasi_subclockflags_t)(1 << 0)) #define __WASI_VERSION_H #define __WASI_WASIP2_H -#define __WASI_WASIP3_H -#define __WASI_WEBP2_H +#define __WASI_WASIP3_H +#define __WASI_WEBP2_H #define __WASI_WHENCE_CUR (UINT8_C(1)) #define __WASI_WHENCE_END (UINT8_C(2)) #define __WASI_WHENCE_SET (UINT8_C(0)) diff --git a/expected/wasm32-wasip1/predefined-macros.txt b/expected/wasm32-wasip1/predefined-macros.txt index 943952cbb..a040b821d 100644 --- a/expected/wasm32-wasip1/predefined-macros.txt +++ b/expected/wasm32-wasip1/predefined-macros.txt @@ -3060,8 +3060,8 @@ #define __WASI_SUBCLOCKFLAGS_SUBSCRIPTION_CLOCK_ABSTIME ((__wasi_subclockflags_t)(1 << 0)) #define __WASI_VERSION_H #define __WASI_WASIP2_H -#define __WASI_WASIP3_H -#define __WASI_WEBP2_H +#define __WASI_WASIP3_H +#define __WASI_WEBP2_H #define __WASI_WHENCE_CUR (UINT8_C(1)) #define __WASI_WHENCE_END (UINT8_C(2)) #define __WASI_WHENCE_SET (UINT8_C(0)) diff --git a/expected/wasm32-wasip2/predefined-macros.txt b/expected/wasm32-wasip2/predefined-macros.txt index d414abbf3..52bb8ffe6 100644 --- a/expected/wasm32-wasip2/predefined-macros.txt +++ b/expected/wasm32-wasip2/predefined-macros.txt @@ -3061,8 +3061,8 @@ #define __USE_TIME_BITS64 1 #define __WASI_VERSION_H #define __WASI_WASIP2_H -#define __WASI_WASIP3_H -#define __WASI_WEBP2_H +#define __WASI_WASIP3_H +#define __WASI_WEBP2_H #define __WCHAR_TYPE__ int #define __WCHAR_WIDTH__ 32 #define __WINT_TYPE__ int diff --git a/expected/wasm32-wasip3-coop/predefined-macros.txt b/expected/wasm32-wasip3-coop/predefined-macros.txt index 148e071b6..bb365169e 100644 --- a/expected/wasm32-wasip3-coop/predefined-macros.txt +++ b/expected/wasm32-wasip3-coop/predefined-macros.txt @@ -3068,8 +3068,8 @@ #define __USE_TIME_BITS64 1 #define __WASI_VERSION_H #define __WASI_WASIP2_H -#define __WASI_WASIP3_H -#define __WASI_WEBP2_H +#define __WASI_WASIP3_H +#define __WASI_WEBP2_H #define __WCHAR_TYPE__ int #define __WCHAR_WIDTH__ 32 #define __WINT_TYPE__ int diff --git a/expected/wasm32-wasip3/predefined-macros.txt b/expected/wasm32-wasip3/predefined-macros.txt index d6122f8f6..800d12b3a 100644 --- a/expected/wasm32-wasip3/predefined-macros.txt +++ b/expected/wasm32-wasip3/predefined-macros.txt @@ -3067,8 +3067,8 @@ #define __USE_TIME_BITS64 1 #define __WASI_VERSION_H #define __WASI_WASIP2_H -#define __WASI_WASIP3_H -#define __WASI_WEBP2_H +#define __WASI_WASIP3_H +#define __WASI_WEBP2_H #define __WCHAR_TYPE__ int #define __WCHAR_WIDTH__ 32 #define __WINT_TYPE__ int diff --git a/expected/wasm32-webp2/defined-symbols.txt b/expected/wasm32-webp2/defined-symbols.txt index da4e5492c..aae4a124e 100644 --- a/expected/wasm32-webp2/defined-symbols.txt +++ b/expected/wasm32-webp2/defined-symbols.txt @@ -353,6 +353,7 @@ _flushlbf _initialize _pthread_cleanup_pop _pthread_cleanup_push +_start a64l abort abs diff --git a/expected/wasm32-webp2/predefined-macros.txt b/expected/wasm32-webp2/predefined-macros.txt index d2a708906..de4fa2d59 100644 --- a/expected/wasm32-webp2/predefined-macros.txt +++ b/expected/wasm32-webp2/predefined-macros.txt @@ -2923,7 +2923,7 @@ #define __UINT_LEAST8_TYPE__ unsigned char #define __USER_LABEL_PREFIX__ #define __USE_TIME_BITS64 1 -#define __WASI_VERSION_H +#define __WASI_VERSION_H #define __WASI_WEBP2_H #define __WCHAR_TYPE__ int #define __WCHAR_WIDTH__ 32 @@ -2960,7 +2960,7 @@ #define __wasi_api_h #define __wasi_libc_busywait_h #define __wasi_libc_environ_h -#define __wasi_libc_h +#define __wasi_libc_h #define __wasilibc___errno_values_h #define __wasilibc___fd_set_h #define __wasilibc___function___isatty_h From 1b2c3cff25ed360991df67ee7621b7bd218fcf1d Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 15 Oct 2026 09:22:35 -0500 Subject: [PATCH 61/96] Stop installing specific dev version of wit-bindgen in CI --- .github/workflows/main.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 920d513e9..16fece472 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -288,9 +288,6 @@ jobs: - uses: actions/checkout@v6 with: submodules: true - # TODO(webp2): Switch back to a released wit-bindgen once one ships with - # getter/setter support (bytecodealliance/wit-bindgen#1726). - - run: cargo install wit-bindgen-cli --locked --git https://github.com/bytecodealliance/wit-bindgen --rev 7fb7a6a072a2c7d225fa183b93acfabdc36e744b - run: CC=clang cmake -S . -B build -G Ninja - run: ninja -C build bindings - run: git diff --exit-code From d3360b455f5bc90b1a056b33aa14c7dd2ec88cd6 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 15 Oct 2026 09:49:45 -0500 Subject: [PATCH 62/96] Start QUIRKS.md --- QUIRKS.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 QUIRKS.md diff --git a/QUIRKS.md b/QUIRKS.md new file mode 100644 index 000000000..44247c568 --- /dev/null +++ b/QUIRKS.md @@ -0,0 +1,19 @@ +This is a list of toolchain-specific quirks, to be migrated to better user-facing documentation in the future. + +## Web Preview 2 (webp2) + +- Some libc functionality is not implemented and will trap if called: + - Files and file descriptors (with exceptions to support stdio below) + - Sockets + - Sleeping +- `stdout` and `stderr` are mapped to `console.log` and `console.warn` respectively: + - Output is expected to be UTF-8. Invalid UTF-8 will be replaced with U+FFFD (�). + - Console output is buffered until a newline is reached. `fflush` can be used to trigger a `console.log` or `console.warn` at any time, regardless of newlines. +- `stdin` is readable but will always EOF. +- `environ` is always empty. +- All calls to `exit` will trap, including `exit(0)`. + - This is because there is currently no way to abort execution of a WebAssembly program without trapping. +- The `main` function has additional restrictions: + - `argc` is always 1, and `argv` is always `{"./this.program"}`. + - In order for exported functions to remain callable after `main`, no destructors are run when `main` finishes. + - If `main` returns a non-zero value, the program will trap, analogous to `exit`. From bc419ab2e9a929d54030b69f1dd4d02f89c955ca Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 15 Oct 2026 10:48:33 -0500 Subject: [PATCH 63/96] Spread contents of webp2_file_descriptors.c back out --- libc-bottom-half/CMakeLists.txt | 25 +- .../cloudlibc/src/libc/fcntl/fcntl.c | 17 +- .../cloudlibc/src/libc/sys/ioctl/ioctl.c | 23 +- .../cloudlibc/src/libc/sys/stat/fstat.c | 18 +- .../cloudlibc/src/libc/sys/uio/readv.c | 33 ++ .../cloudlibc/src/libc/sys/uio/writev.c | 29 +- .../cloudlibc/src/libc/unistd/lseek.c | 13 +- .../cloudlibc/src/libc/unistd/read.c | 16 +- .../cloudlibc/src/libc/unistd/write.c | 16 +- .../headers/private/wasi/webp2_stdio.h | 37 ++ .../sources/__wasilibc_fd_renumber.c | 14 +- libc-bottom-half/sources/__wasilibc_tell.c | 11 +- libc-bottom-half/sources/isatty.c | 11 +- libc-bottom-half/sources/ppoll.c | 39 +- .../sources/webp2_file_descriptors.c | 375 +----------------- libc-bottom-half/sources/webp2_stdio.c | 150 +++++++ 16 files changed, 428 insertions(+), 399 deletions(-) create mode 100644 libc-bottom-half/headers/private/wasi/webp2_stdio.h create mode 100644 libc-bottom-half/sources/webp2_stdio.c diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index 029dcd89b..da8ff8efc 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -67,33 +67,45 @@ endforeach() # All targets set(bottom_half_sources cloudlibc/src/libc/errno/errno.c + cloudlibc/src/libc/fcntl/fcntl.c cloudlibc/src/libc/poll/poll.c cloudlibc/src/libc/sched/sched_yield.c cloudlibc/src/libc/stdlib/_Exit.c + cloudlibc/src/libc/sys/ioctl/ioctl.c cloudlibc/src/libc/sys/select/pselect.c cloudlibc/src/libc/sys/select/select.c + cloudlibc/src/libc/sys/stat/fstat.c cloudlibc/src/libc/sys/time/gettimeofday.c + cloudlibc/src/libc/sys/uio/readv.c + cloudlibc/src/libc/sys/uio/writev.c cloudlibc/src/libc/time/CLOCK_MONOTONIC.c cloudlibc/src/libc/time/CLOCK_REALTIME.c cloudlibc/src/libc/time/clock_getres.c cloudlibc/src/libc/time/clock_gettime.c cloudlibc/src/libc/time/time.c + cloudlibc/src/libc/unistd/lseek.c + cloudlibc/src/libc/unistd/read.c + cloudlibc/src/libc/unistd/write.c sources/__errno_location.c sources/__main_void.c sources/__wasilibc_dt.c sources/__wasilibc_environ.c + sources/__wasilibc_fd_renumber.c sources/__wasilibc_initialize_environ.c sources/__wasilibc_random.c sources/__wasilibc_real.c sources/__wasilibc_report_error.c sources/__wasilibc_report_errorf.c + sources/__wasilibc_tell.c sources/abort.c sources/complex-builtins.c sources/environ.c sources/errno.c sources/getentropy.c + sources/isatty.c sources/math/fmin-fmax.c sources/math/math-builtins.c + sources/ppoll.c sources/reallocarray.c sources/sbrk.c sources/utf8.c @@ -103,20 +115,8 @@ set(bottom_half_sources # File descriptors if (NOT WEB STREQUAL "p2") list(APPEND bottom_half_sources - cloudlibc/src/libc/fcntl/fcntl.c - cloudlibc/src/libc/sys/ioctl/ioctl.c - cloudlibc/src/libc/sys/stat/fstat.c - cloudlibc/src/libc/sys/uio/readv.c - cloudlibc/src/libc/sys/uio/writev.c - cloudlibc/src/libc/unistd/lseek.c - cloudlibc/src/libc/unistd/read.c - cloudlibc/src/libc/unistd/write.c - sources/__wasilibc_fd_renumber.c - sources/__wasilibc_tell.c sources/dup.c - sources/isatty.c sources/pipe.c - sources/ppoll.c ) if (WASI STREQUAL "p2" OR WASI STREQUAL "p3") list(APPEND bottom_half_sources @@ -251,6 +251,7 @@ if (WEB STREQUAL "p2") sources/webp2_filesystem.c sources/webp2_sleep.c sources/webp2_socket.c + sources/webp2_stdio.c ) endif() diff --git a/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c b/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c index 3c44db86c..1eb52f1c8 100644 --- a/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c +++ b/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c @@ -7,7 +7,9 @@ #include #include -#ifndef __wasip1__ +#if defined(__webp2__) +#include +#elif !defined(__wasip1__) #include #include #include @@ -19,6 +21,11 @@ int fcntl(int fildes, int cmd, ...) { if (descriptor_table_get(fildes, &entry) < 0) return -1; defer descriptor_table_entry_dec(entry); +#elif defined(__webp2__) + if (!__wasilibc_webp2_fd_ok(fildes)) { + errno = EBADF; + return -1; + } #endif switch (cmd) { @@ -59,6 +66,8 @@ int fcntl(int fildes, int cmd, ...) { return -1; } return entry.vtable->fcntl_getfl(entry.data); +#elif defined(__webp2__) + return __wasilibc_webp2_is_readable(fildes) ? O_RDONLY : O_WRONLY; #else # error "Unknown WASI version" #endif @@ -84,12 +93,16 @@ int fcntl(int fildes, int cmd, ...) { return -1; } return entry.vtable->fcntl_setfl(entry.data, flags); +#elif defined(__webp2__) + (void)flags; + errno = EINVAL; + return -1; #else # error "Unknown WASI version" #endif return 0; } -#ifndef __wasip1__ +#if !defined(__wasip1__) && !defined(__webp2__) case F_DUPFD: case F_DUPFD_CLOEXEC: { va_list ap; 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 f8626f437..4b895fe4e 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c @@ -9,11 +9,20 @@ #include #include -#ifndef __wasip1__ +#if defined(__webp2__) +#include +#elif !defined(__wasip1__) #include #endif int ioctl(int fildes, int request, ...) { +#if defined(__webp2__) + if (!__wasilibc_webp2_fd_ok(fildes)) { + errno = EBADF; + return -1; + } +#endif + switch (request) { case FIONREAD: { #if defined(__wasip1__) @@ -63,6 +72,14 @@ int ioctl(int fildes, int request, ...) { // wasip{2,3} doesn't support this operation errno = ENOTSUP; return -1; +#elif defined(__webp2__) + // There is never anything available to read. + va_list ap; + va_start(ap, request); + int *result = va_arg(ap, int *); + va_end(ap); + *result = 0; + return 0; #else # error "Unknown WASI version" #endif @@ -108,6 +125,10 @@ int ioctl(int fildes, int request, ...) { return -1; } return entry.vtable->set_blocking(entry.data, blocking); +#elif defined(__webp2__) + // There's no difference between "blocking" and "non-blocking" IO for our + // stdio implementation, so this does nothing. + return 0; #else # error "Unknown WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstat.c b/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstat.c index 393652a90..c8d7c7997 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstat.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstat.c @@ -2,13 +2,19 @@ // // SPDX-License-Identifier: BSD-2-Clause -#include "stat_impl.h" #include #include #include #include -#ifndef __wasip1__ +#if defined(__webp2__) +#include +#include +#else +#include "stat_impl.h" +#endif + +#if !defined(__wasip1__) && !defined(__webp2__) #include #endif @@ -29,6 +35,14 @@ int fstat(int fildes, struct stat *buf) { return -1; defer descriptor_table_entry_dec(entry); return entry.vtable->fstat(entry.data, buf); +#elif defined(__webp2__) + if (!__wasilibc_webp2_fd_ok(fildes)) { + errno = EBADF; + return -1; + } + memset(buf, 0, sizeof(*buf)); + buf->st_mode = S_IFCHR; + return 0; #else # error "Unsupported WASI version" #endif 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 90ed31a9d..74e625ee0 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c @@ -10,6 +10,11 @@ #include #include +#ifdef __webp2__ +#include +#include +#endif + #ifdef __wasip1__ static_assert(offsetof(struct iovec, iov_base) == offsetof(__wasi_iovec_t, buf), @@ -52,6 +57,34 @@ ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { iov++; } return read(fildes, NULL, 0); +#elif defined(__webp2__) + if (IOV_MAX < iovcnt) { + errno = EINVAL; + return -1; + } + if (!__wasilibc_webp2_iovecs_ok(iov, iovcnt, NULL)) { + errno = EINVAL; + return -1; + } + if (!__wasilibc_webp2_is_readable(fildes)) { + errno = EBADF; + return -1; + } + + size_t bytes_read = 0; + for (int i = 0; i < iovcnt; i++) { + const struct iovec *v = &iov[i]; + ssize_t res = read(fildes, v->iov_base, v->iov_len); + if (res < 0) { + // errno already set + return res; + } + bytes_read += res; + if ((size_t)res < v->iov_len) { + break; + } + } + return bytes_read; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/uio/writev.c b/libc-bottom-half/cloudlibc/src/libc/sys/uio/writev.c index 00fe632d8..0c458e261 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/writev.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/writev.c @@ -9,7 +9,10 @@ #include #include -#ifndef __wasip1__ +#if defined(__webp2__) +#include +#include +#elif !defined(__wasip1__) #include #include #endif @@ -57,6 +60,30 @@ ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { iov++; } return write(fildes, NULL, 0); +#elif defined(__webp2__) + if (IOV_MAX < iovcnt) { + errno = EINVAL; + return -1; + } + if (!__wasilibc_webp2_is_writable(fildes)) { + errno = EBADF; + return -1; + } + + size_t total_size; + if (!__wasilibc_webp2_iovecs_ok(iov, iovcnt, &total_size)) { + errno = EINVAL; + return -1; + } + for (int i = 0; i < iovcnt; i++) { + const struct iovec *v = &iov[i]; + int res = write(fildes, v->iov_base, v->iov_len); + if (res < 0) { + // errno already set + return res; + } + } + return total_size; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c b/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c index 8fe2024e4..aa45ca345 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c @@ -7,7 +7,9 @@ #include #include -#ifndef __wasip1__ +#if defined(__webp2__) +#include +#elif !defined(__wasip1__) #include #include #include @@ -41,6 +43,15 @@ off_t __lseek(int fildes, off_t offset, int whence) { return -1; } return entry.vtable->seek(entry.data, offset, whence); +#elif defined(__webp2__) + (void)offset; + (void)whence; + if (!__wasilibc_webp2_fd_ok(fildes)) { + errno = EBADF; + return -1; + } + errno = ESPIPE; + return -1; #else # error "Unknown WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/read.c b/libc-bottom-half/cloudlibc/src/libc/unistd/read.c index 5ce69fede..c27095190 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/read.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/read.c @@ -6,7 +6,10 @@ #include #include -#ifndef __wasip1__ +#if defined(__webp2__) +#include +#include +#elif !defined(__wasip1__) #include #include #include @@ -25,6 +28,17 @@ ssize_t read(int fildes, void *buf, size_t nbyte) { return -1; } return bytes_read; +#elif defined(__webp2__) + (void)buf; + if (nbyte > SSIZE_MAX) { + errno = EINVAL; + return -1; + } + if (!__wasilibc_webp2_is_readable(fildes)) { + errno = EBADF; + return -1; + } + return 0; // immediate EOF #else descriptor_table_entry_t entry; if (descriptor_table_get(fildes, &entry) < 0) diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/write.c b/libc-bottom-half/cloudlibc/src/libc/unistd/write.c index 0b9dc4fa0..959ec1762 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/write.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/write.c @@ -6,7 +6,10 @@ #include #include -#ifndef __wasip1__ +#if defined(__webp2__) +#include +#include +#elif !defined(__wasip1__) #include #include #include @@ -26,6 +29,17 @@ ssize_t write(int fildes, const void *buf, size_t nbyte) { return -1; } return bytes_written; +#elif defined(__webp2__) + if (nbyte > SSIZE_MAX) { + errno = EINVAL; + return -1; + } + if (!__wasilibc_webp2_is_writable(fildes)) { + errno = EBADF; + return -1; + } + __wasilibc_webp2_write(fildes, buf, nbyte); + return nbyte; #else descriptor_table_entry_t entry; if (descriptor_table_get(fildes, &entry) < 0) diff --git a/libc-bottom-half/headers/private/wasi/webp2_stdio.h b/libc-bottom-half/headers/private/wasi/webp2_stdio.h new file mode 100644 index 000000000..bc6c97968 --- /dev/null +++ b/libc-bottom-half/headers/private/wasi/webp2_stdio.h @@ -0,0 +1,37 @@ +#ifndef WASI_WEBP2_STDIO_H +#define WASI_WEBP2_STDIO_H + +#include + +#ifdef __webp2__ + +#include +#include +#include +#include + +// File descriptors are mostly stubbed out on the web, since there is no +// comparable model of files and streams. However, stdout and stderr are +// supported by routing to console.log, and stdin is supported by immediately +// returning EOF. + +hidden bool __wasilibc_webp2_fd_ok(int fd); +hidden bool __wasilibc_webp2_is_readable(int fd); +hidden bool __wasilibc_webp2_is_writable(int fd); + +// Check if the total size of all iovecs would overflow an ssize_t. +// `total_size` can be NULL. +hidden bool __wasilibc_webp2_iovecs_ok(const struct iovec *iov, int iovcnt, + size_t *total_size); + +// Implementation for the `write` function, writing valid UTF-8 to the stdio +// line buffers. Aborts if `fd` is not writable. +hidden void __wasilibc_webp2_write(int fd, const void *buf, size_t nbyte); + +// Implementation for the `close` function, i.e. finish and flush any line +// buffers and close the fd. Aborts if `fd` is not open. +hidden void __wasilibc_webp2_close(int fd); + +#endif // __webp2__ + +#endif // WASI_WEBP2_STDIO_H diff --git a/libc-bottom-half/sources/__wasilibc_fd_renumber.c b/libc-bottom-half/sources/__wasilibc_fd_renumber.c index 9338cda24..98c334a54 100644 --- a/libc-bottom-half/sources/__wasilibc_fd_renumber.c +++ b/libc-bottom-half/sources/__wasilibc_fd_renumber.c @@ -3,7 +3,9 @@ #include #include -#ifndef __wasip1__ +#if defined(__webp2__) +#include +#elif !defined(__wasip1__) #include #include #endif @@ -23,8 +25,10 @@ int __wasilibc_fd_renumber(int fd, int newfd) { #endif int close(int fd) { +#ifndef __webp2__ // Scan the preopen fds before making any changes. __wasilibc_populate_preopens(); +#endif #if defined(__wasip1__) __wasi_errno_t error = __wasi_fd_close(fd); @@ -35,6 +39,12 @@ int close(int fd) { #elif defined(__wasip2__) || defined(__wasip3__) if (descriptor_table_remove(fd) < 0) return -1; +#elif defined(__webp2__) + if (!__wasilibc_webp2_fd_ok(fd)) { + errno = EBADF; + return -1; + } + __wasilibc_webp2_close(fd); #else #error "Unsupported WASI version" #endif // __wasip2__ @@ -42,8 +52,10 @@ int close(int fd) { return 0; } +#ifndef __webp2__ weak void __wasilibc_populate_preopens(void) { // This version does nothing. It may be overridden by a version which does // something if `__wasilibc_find_abspath` or `__wasilibc_find_relpath` are // used. } +#endif diff --git a/libc-bottom-half/sources/__wasilibc_tell.c b/libc-bottom-half/sources/__wasilibc_tell.c index 1a7d950d3..1ede3b348 100644 --- a/libc-bottom-half/sources/__wasilibc_tell.c +++ b/libc-bottom-half/sources/__wasilibc_tell.c @@ -2,7 +2,9 @@ #include #include -#ifndef __wasip1__ +#if defined(__webp2__) +#include +#elif !defined(__wasip1__) #include #include #include @@ -31,6 +33,13 @@ off_t __wasilibc_tell(int fildes) { return -1; } return entry.vtable->seek(entry.data, 0, SEEK_CUR); +#elif defined(__webp2__) + if (!__wasilibc_webp2_fd_ok(fildes)) { + errno = EBADF; + return -1; + } + errno = ESPIPE; + return -1; #else #error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/sources/isatty.c b/libc-bottom-half/sources/isatty.c index 707fe83c7..ad0d1f9ab 100644 --- a/libc-bottom-half/sources/isatty.c +++ b/libc-bottom-half/sources/isatty.c @@ -4,7 +4,9 @@ #include #include -#ifndef __wasip1__ +#if defined(__webp2__) +#include +#elif !defined(__wasip1__) #include #include #include @@ -40,6 +42,13 @@ int __isatty(int fd) { } return entry.vtable->isatty(entry.data); +#elif defined(__webp2__) + if (!__wasilibc_webp2_fd_ok(fd)) { + errno = EBADF; + return 0; + } + errno = ENOTTY; + return 0; #else #error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/sources/ppoll.c b/libc-bottom-half/sources/ppoll.c index 8ccd184d7..9f6352beb 100644 --- a/libc-bottom-half/sources/ppoll.c +++ b/libc-bottom-half/sources/ppoll.c @@ -6,7 +6,10 @@ #include #include -#ifndef __wasip1__ +#if defined(__webp2__) +#include +#include +#elif !defined(__wasip1__) #include #endif @@ -571,6 +574,40 @@ static int ppoll_impl(struct pollfd *fds, size_t nfds, return state.event_count; } +#elif defined(__webp2__) + +static int ppoll_impl(struct pollfd *fds, size_t nfds, + const struct timespec *timeout) { + int ready = 0; + for (size_t i = 0; i < nfds; i++) { + struct pollfd *pollfd = &fds[i]; + if (pollfd->fd < 0) { + continue; + } + if (!__wasilibc_webp2_fd_ok(pollfd->fd)) { + pollfd->revents = POLLNVAL; + } else if (__wasilibc_webp2_is_readable(pollfd->fd)) { + // stdin is always readable + pollfd->revents = pollfd->events & POLLRDNORM; + } else { + // stdout and stderr are always writable + pollfd->revents = pollfd->events & POLLWRNORM; + } + if (pollfd->revents != 0) { + ready++; + } + } + + // We cannot actually sit and wait for files to become available, but because + // our stdin, stdout, and stderr are always ready, most user code should + // never hit this. + bool zero_timeout = timeout && timeout->tv_sec == 0 && timeout->tv_nsec == 0; + if (ready == 0 && !zero_timeout) { + WEBP2_UNSUPPORTED("blocking ppoll"); + } + return ready; +} + #else #error "Unknown WASI version" #endif diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index 96f06acab..6709ce715 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -1,380 +1,7 @@ -// File descriptors are mostly stubbed out on the web, since there is no -// comparable model of files and streams. However, stdout and stderr are -// supported by routing to console.log, and stdin is supported by immediately -// returning EOF. +// There is no way to create new file descriptors on the web. -#include "common/time.h" -#include "features.h" -#include "sys/uio.h" #include "wasi/report-error.h" -#include <__errno_values.h> -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include -#include -#include - -// unistd.h defines an `lseek` macro that would mangle the definition below. -#undef lseek - -// ============================================================================ -// Basic stdio implementation - -#define LINE_BUF_SIZE 4096 - -typedef struct line_buffer_t { - bool warn; - uint8_t buf[LINE_BUF_SIZE]; -} line_buffer_t; - -static line_buffer_t line_buffers[2] = { - (line_buffer_t){}, - (line_buffer_t){.warn = true}, -}; -static wasilibc_utf8_writer_t utf8_writers[2] = { - __wasilibc_make_utf8_writer(line_buffers[0].buf, LINE_BUF_SIZE), - __wasilibc_make_utf8_writer(line_buffers[1].buf, LINE_BUF_SIZE), -}; - -static bool fd_closed[3]; - -static void line_buffer_out(line_buffer_t *lb, wasilibc_utf8_writer_t *utf8) { - webp2_string_t out = - (webp2_string_t){.ptr = utf8->buf, .len = utf8->valid_len}; - if (lb->warn) { - webp2_console_warn(&out); - } else { - webp2_console_log(&out); - } -} - -static bool fd_ok(int fd) { - return STDIN_FILENO <= fd && fd <= STDERR_FILENO && !fd_closed[fd]; -} - -static bool is_readable(int fd) { return fd == STDIN_FILENO; } - -static bool is_writable(int fd) { - return fd == STDOUT_FILENO || fd == STDERR_FILENO; -} - -static bool iovecs_ok(const struct iovec *iov, int iovcnt, size_t *total_size) { - // Check if the total size of all iovecs would overflow an ssize_t - size_t _total_size = 0; - if (total_size) { - *total_size = 0; - } - for (int i = 0; i < iovcnt; i++) { - const struct iovec *v = &iov[i]; - if (v->iov_len > SSIZE_MAX || _total_size > SSIZE_MAX - v->iov_len) { - return false; - } - _total_size += v->iov_len; - } - if (total_size) { - *total_size = _total_size; - } - return true; -} - -// Flushes all data in line buffer to the console, replacing any trailing -// multi-byte code points with the replacement character. -static void finish_and_flush(int fd) { - assert(fd == STDOUT_FILENO || fd == STDERR_FILENO); - if (fd_closed[fd]) { - return; - } - wasilibc_utf8_writer_t *utf8 = &utf8_writers[fd - 1]; - if (!__wasilibc_utf8_abort_current(utf8)) { - // Emit what is already valid in the buffer first, and carry over the - // in-progress codepoint - __wasilibc_webp2_fflush(fd); - } - __wasilibc_utf8_abort_current(utf8); - __wasilibc_webp2_fflush(fd); -} - -int __isatty(int fd) { - if (!fd_ok(fd)) { - errno = EBADF; - return 0; - } - errno = ENOTTY; - return 0; -} -weak_alias(__isatty, isatty); - -off_t __lseek(int fildes, off_t offset, int whence) { - if (!fd_ok(fildes)) { - errno = EBADF; - return -1; - } - errno = ESPIPE; - return -1; -} -weak_alias(__lseek, lseek); - -off_t __wasilibc_tell(int fd) { - if (!fd_ok(fd)) { - errno = EBADF; - return -1; - } - errno = ESPIPE; - return -1; -} - -void __wasilibc_webp2_fflush(int fd) { - assert(fd == STDOUT_FILENO || fd == STDERR_FILENO); - if (fd_closed[fd]) { - return; - } - line_buffer_t *lb = &line_buffers[fd - 1]; - wasilibc_utf8_writer_t *utf8 = &utf8_writers[fd - 1]; - if (utf8->valid_len > 0) { - line_buffer_out(lb, utf8); - __wasilibc_utf8_reset(utf8); - } -} - -void __wasilibc_webp2_flush_and_close_stdio(bool also_musl) { - if (also_musl) { - fflush(stdout); - } - finish_and_flush(STDOUT_FILENO); - fd_closed[STDOUT_FILENO] = true; - - if (also_musl) { - fflush(stderr); - } - finish_and_flush(STDERR_FILENO); - fd_closed[STDERR_FILENO] = true; -} - -int close(int fd) { - if (!fd_ok(fd)) { - errno = EBADF; - return -1; - } - if (is_writable(fd)) { - // Don't flush musl's buffers, but do flush everything we have. - finish_and_flush(fd); - } - fd_closed[fd] = true; - return 0; -} - -int fcntl(int fildes, int cmd, ...) { - if (!fd_ok(fildes)) { - errno = EBADF; - return -1; - } - switch (cmd) { - case F_GETFD: - case F_SETFD: - // Don't bother setting FD_CLOEXEC at all. It's totally irrelevant but - // there's no reason to crash if trying to set it. - return 0; - case F_GETFL: - return is_readable(fildes) ? O_RDONLY : O_WRONLY; - default: - errno = EINVAL; - return -1; - } -} - -int fstat(int fildes, struct stat *buf) { - if (!fd_ok(fildes)) { - errno = EBADF; - return -1; - } - memset(buf, 0, sizeof(*buf)); - buf->st_mode = S_IFCHR; - return 0; -} - -int ioctl(int fildes, int request, ...) { - if (!fd_ok(fildes)) { - errno = EBADF; - return -1; - } - switch (request) { - case FIONREAD: { - // There is never anything available to read. - va_list ap; - va_start(ap, request); - int *result = va_arg(ap, int *); - va_end(ap); - *result = 0; - return 0; - } - case FIONBIO: - // There's no difference between "blocking" and "non-blocking" IO for our - // stdio implementation, so this does nothing. - return 0; - default: - errno = EINVAL; - return -1; - } -} - -int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, - const sigset_t *sigmask) { - if (timeout && (timeout->tv_sec < 0 || timeout->tv_nsec < 0 || - timeout->tv_nsec >= NSEC_PER_SEC)) { - errno = EINVAL; - return -1; - } - - int ready = 0; - for (nfds_t i = 0; i < nfds; i++) { - struct pollfd *pollfd = &fds[i]; - pollfd->revents = 0; - if (pollfd->fd < 0) { - continue; - } - if (!fd_ok(pollfd->fd)) { - pollfd->revents = POLLNVAL; - } else if (is_readable(pollfd->fd)) { - // stdin is always readable - pollfd->revents = pollfd->events & POLLRDNORM; - } else { - // stdout and stderr are always writable - pollfd->revents = pollfd->events & POLLWRNORM; - } - if (pollfd->revents != 0) { - ready++; - } - } - - // We cannot actually sit and wait for files to become available, but because - // our stdin, stdout, and stderr are always ready, most user code should - // never hit this. - bool zero_timeout = timeout && timeout->tv_sec == 0 && timeout->tv_nsec == 0; - if (ready == 0 && !zero_timeout) { - WEBP2_UNSUPPORTED("blocking ppoll"); - } - return ready; -} - -ssize_t read(int fildes, void *buf, size_t nbyte) { - if (nbyte > SSIZE_MAX) { - errno = EINVAL; - return -1; - } - if (!is_readable(fildes) || fd_closed[fildes]) { - errno = EBADF; - return -1; - } - return 0; // immediate EOF; -} - -ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { - if (iovcnt < 0 || IOV_MAX < iovcnt) { - errno = EINVAL; - return -1; - } - if (!iovecs_ok(iov, iovcnt, NULL)) { - errno = EINVAL; - return -1; - } - if (!is_readable(fildes) || fd_closed[fildes]) { - errno = EBADF; - return -1; - } - - size_t bytes_read = 0; - for (int i = 0; i < iovcnt; i++) { - const struct iovec *v = &iov[i]; - ssize_t res = read(fildes, v->iov_base, v->iov_len); - if (res < 0) { - // errno already set - return res; - } - bytes_read += res; - if ((size_t)res < v->iov_len) { - break; - } - } - return bytes_read; -} - -ssize_t write(int fildes, const void *buf, size_t nbyte) { - if (nbyte > SSIZE_MAX) { - errno = EINVAL; - return -1; - } - if (!is_writable(fildes) || fd_closed[fildes]) { - errno = EBADF; - return -1; - } - - line_buffer_t *line_buffer = &line_buffers[fildes - 1]; - wasilibc_utf8_writer_t *utf8 = &utf8_writers[fildes - 1]; - for (size_t i = 0; i < nbyte; i++) { - uint8_t b = ((uint8_t *)buf)[i]; - // First write to the buffer, flushing first if there was no room for the - // results (which can occur either because the buffer was completely full - // or because there was no room for a replacement char). - bool already_logged = false; - if (!__wasilibc_utf8_write(utf8, b)) { - line_buffer_out(line_buffer, utf8); - __wasilibc_utf8_reset(utf8); - __wasilibc_utf8_write(utf8, b); // infallible - already_logged = true; - } - // Then, if there was a newline, drop it from the buffer and flush. - if (b == '\n') { - assert(utf8->valid_len == utf8->raw_len); - utf8->raw_len--; - utf8->valid_len--; - bool skip = utf8->valid_len == 0 && already_logged; - if (!skip) { - line_buffer_out(line_buffer, utf8); - } - __wasilibc_utf8_reset(utf8); - } - } - - return nbyte; -} - -ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { - if (iovcnt < 0 || IOV_MAX < iovcnt) { - errno = EINVAL; - return -1; - } - if (!is_writable(fildes) || fd_closed[fildes]) { - errno = EBADF; - return -1; - } - - size_t total_size; - if (!iovecs_ok(iov, iovcnt, &total_size)) { - errno = EINVAL; - return -1; - } - for (int i = 0; i < iovcnt; i++) { - const struct iovec *v = &iov[i]; - int res = write(fildes, v->iov_base, v->iov_len); - if (res < 0) { - // errno already set - return res; - } - } - return total_size; -} - -// ============================================================================ -// Stubs int __dup3(int fd, int newfd, int flags) { WEBP2_UNSUPPORTED("__dup3"); } diff --git a/libc-bottom-half/sources/webp2_stdio.c b/libc-bottom-half/sources/webp2_stdio.c new file mode 100644 index 000000000..31b92e5f5 --- /dev/null +++ b/libc-bottom-half/sources/webp2_stdio.c @@ -0,0 +1,150 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define LINE_BUF_SIZE 4096 + +typedef struct line_buffer_t { + bool warn; + uint8_t buf[LINE_BUF_SIZE]; +} line_buffer_t; + +static line_buffer_t line_buffers[2] = { + (line_buffer_t){}, + (line_buffer_t){.warn = true}, +}; +static wasilibc_utf8_writer_t utf8_writers[2] = { + __wasilibc_make_utf8_writer(line_buffers[0].buf, LINE_BUF_SIZE), + __wasilibc_make_utf8_writer(line_buffers[1].buf, LINE_BUF_SIZE), +}; + +static bool fd_closed[3]; + +static void line_buffer_out(line_buffer_t *lb, wasilibc_utf8_writer_t *utf8) { + webp2_string_t out = + (webp2_string_t){.ptr = utf8->buf, .len = utf8->valid_len}; + if (lb->warn) { + webp2_console_warn(&out); + } else { + webp2_console_log(&out); + } +} + +// Flushes all data in line buffer to the console, replacing any trailing +// multi-byte code points with the replacement character. +static void finish_and_flush(int fd) { + assert(fd == STDOUT_FILENO || fd == STDERR_FILENO); + if (fd_closed[fd]) { + return; + } + wasilibc_utf8_writer_t *utf8 = &utf8_writers[fd - 1]; + if (!__wasilibc_utf8_abort_current(utf8)) { + // Emit what is already valid in the buffer first, and carry over the + // in-progress codepoint + __wasilibc_webp2_fflush(fd); + } + __wasilibc_utf8_abort_current(utf8); + __wasilibc_webp2_fflush(fd); +} + +bool __wasilibc_webp2_fd_ok(int fd) { + return STDIN_FILENO <= fd && fd <= STDERR_FILENO && !fd_closed[fd]; +} + +bool __wasilibc_webp2_is_readable(int fd) { + return fd == STDIN_FILENO && !fd_closed[fd]; +} + +bool __wasilibc_webp2_is_writable(int fd) { + return (fd == STDOUT_FILENO || fd == STDERR_FILENO) && !fd_closed[fd]; +} + +bool __wasilibc_webp2_iovecs_ok(const struct iovec *iov, int iovcnt, + size_t *total_size) { + size_t _total_size = 0; + if (total_size) { + *total_size = 0; + } + for (int i = 0; i < iovcnt; i++) { + const struct iovec *v = &iov[i]; + if (v->iov_len > SSIZE_MAX || _total_size > SSIZE_MAX - v->iov_len) { + return false; + } + _total_size += v->iov_len; + } + if (total_size) { + *total_size = _total_size; + } + return true; +} + +void __wasilibc_webp2_write(int fd, const void *buf, size_t nbyte) { + assert(__wasilibc_webp2_is_writable(fd)); + line_buffer_t *line_buffer = &line_buffers[fd - 1]; + wasilibc_utf8_writer_t *utf8 = &utf8_writers[fd - 1]; + for (size_t i = 0; i < nbyte; i++) { + uint8_t b = ((uint8_t *)buf)[i]; + // First write to the buffer, flushing first if there was no room for the + // results (which can occur either because the buffer was completely full + // or because there was no room for a replacement char). + bool already_logged = false; + if (!__wasilibc_utf8_write(utf8, b)) { + line_buffer_out(line_buffer, utf8); + __wasilibc_utf8_reset(utf8); + __wasilibc_utf8_write(utf8, b); // infallible + already_logged = true; + } + // Then, if there was a newline, drop it from the buffer and flush. + if (b == '\n') { + assert(utf8->valid_len == utf8->raw_len); + utf8->raw_len--; + utf8->valid_len--; + bool skip = utf8->valid_len == 0 && already_logged; + if (!skip) { + line_buffer_out(line_buffer, utf8); + } + __wasilibc_utf8_reset(utf8); + } + } +} + +void __wasilibc_webp2_close(int fd) { + assert(__wasilibc_webp2_fd_ok(fd)); + if (__wasilibc_webp2_is_writable(fd)) { + // Don't flush musl's buffers, but do flush everything we have. + finish_and_flush(fd); + } + fd_closed[fd] = true; +} + +void __wasilibc_webp2_fflush(int fd) { + assert(fd == STDOUT_FILENO || fd == STDERR_FILENO); + if (fd_closed[fd]) { + return; + } + line_buffer_t *lb = &line_buffers[fd - 1]; + wasilibc_utf8_writer_t *utf8 = &utf8_writers[fd - 1]; + if (utf8->valid_len > 0) { + line_buffer_out(lb, utf8); + __wasilibc_utf8_reset(utf8); + } +} + +void __wasilibc_webp2_flush_and_close_stdio(bool also_musl) { + if (also_musl) { + fflush(stdout); + } + finish_and_flush(STDOUT_FILENO); + fd_closed[STDOUT_FILENO] = true; + + if (also_musl) { + fflush(stderr); + } + finish_and_flush(STDERR_FILENO); + fd_closed[STDERR_FILENO] = true; +} From 29c74ce8395294339e004aef2950e12ba28a08a3 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 15 Oct 2026 13:47:25 -0500 Subject: [PATCH 64/96] Add tests for dupery --- test/CMakeLists.txt | 13 +++++++- test/src/stdio-dup-abort.c | 15 +++++++++ test/src/stdio-dup-close.c | 23 ++++++++++++++ test/src/stdio-dup-exit.c | 15 +++++++++ test/src/stdio-dup-shared-buffer.c | 14 ++++++++ test/src/stdio-dup.c | 51 ++++++++++++++++++++++++++++++ test/src/stdio-dup2-fflush.c | 17 ++++++++++ 7 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 test/src/stdio-dup-abort.c create mode 100644 test/src/stdio-dup-close.c create mode 100644 test/src/stdio-dup-exit.c create mode 100644 test/src/stdio-dup-shared-buffer.c create mode 100644 test/src/stdio-dup.c create mode 100644 test/src/stdio-dup2-fflush.c diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9df0b4d2b..0655c1c09 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -724,6 +724,18 @@ add_wasilibc_test(dup2-file.c FS) add_wasilibc_test(dup3-file.c FS) add_wasilibc_test(fcntl-dupfd.c FS) add_wasilibc_test(clear_fds.c FS) +if (NOT (WASI STREQUAL "p1")) + add_wasilibc_test(poll-badfd.c) + add_wasilibc_test(stdio-dup.c) + add_wasilibc_test(stdio-dup-shared-buffer.c PASS_REGULAR_EXPRESSION "abcd\n") + add_wasilibc_test(stdio-dup-close.c PASS_REGULAR_EXPRESSION "hello") + add_wasilibc_test(stdio-dup-exit.c PASS_REGULAR_EXPRESSION "before-exit") + add_wasilibc_test(stdio-dup-abort.c PASS_REGULAR_EXPRESSION "before-abort") +endif() +if (WEB) + # Relies on webp2's line buffering + add_wasilibc_test(stdio-dup2-fflush.c PASS_REGULAR_EXPRESSION "firstline\nsecondline\n") +endif() if (NOT WEB) add_wasilibc_test(pipe.c) add_wasilibc_test(pipe_poll.c) @@ -909,7 +921,6 @@ add_external_entrypoint_test(external_entrypoint_pthread_tls.c) if (NOT (WASI STREQUAL "p1") AND NOT WEB) add_wasilibc_test(poll-connect.c NETWORK) - add_wasilibc_test(poll-badfd.c) add_wasilibc_test(poll-nonblocking-socket.c NETWORK) add_wasilibc_test(setsockopt.c NETWORK) add_wasilibc_test(sockets-ipv6-v6only.c NETWORK) diff --git a/test/src/stdio-dup-abort.c b/test/src/stdio-dup-abort.c new file mode 100644 index 000000000..888cdc6b8 --- /dev/null +++ b/test/src/stdio-dup-abort.c @@ -0,0 +1,15 @@ +#include +#include + +int main(void) { + int d = dup(STDOUT_FILENO); + if (d < 0) { + return 1; + } + + // Even when the original stdout handle is closed, writes to stdout should be + // flushed on abort. + close(STDOUT_FILENO); + write(d, "before-abort", 12); + abort(); +} diff --git a/test/src/stdio-dup-close.c b/test/src/stdio-dup-close.c new file mode 100644 index 000000000..7f4ca6076 --- /dev/null +++ b/test/src/stdio-dup-close.c @@ -0,0 +1,23 @@ +#include +#include + +int main(void) { + int d = dup(STDOUT_FILENO); + if (d < 0) { + return 1; + } + + // Should print "hello" to stdout via the dup despite the original stdout + // handle being closed. + write(d, "hel", 3); + if (close(STDOUT_FILENO) != 0) { + write(d, "FAIL", 4); + } + if (write(STDOUT_FILENO, "X", 1) != -1 || errno != EBADF) { + write(d, "FAIL", 4); + } + write(d, "lo", 2); + close(d); + + return 0; +} diff --git a/test/src/stdio-dup-exit.c b/test/src/stdio-dup-exit.c new file mode 100644 index 000000000..2a41edccc --- /dev/null +++ b/test/src/stdio-dup-exit.c @@ -0,0 +1,15 @@ +#include +#include + +int main(void) { + int d = dup(STDOUT_FILENO); + if (d < 0) { + return 1; + } + + // Even when the original stdout handle is closed, writes to stdout should be + // flushed on exit. + close(STDOUT_FILENO); + write(d, "before-exit", 11); + exit(0); +} diff --git a/test/src/stdio-dup-shared-buffer.c b/test/src/stdio-dup-shared-buffer.c new file mode 100644 index 000000000..33458f97b --- /dev/null +++ b/test/src/stdio-dup-shared-buffer.c @@ -0,0 +1,14 @@ +#include + +int main(void) { + // Write "abcd" to stdout using both the original handle and a dup. + int d = dup(STDOUT_FILENO); + if (d < 0) { + return 1; + } + write(STDOUT_FILENO, "ab", 2); + write(d, "cd", 2); + write(STDOUT_FILENO, "\n", 1); + close(d); + return 0; +} diff --git a/test/src/stdio-dup.c b/test/src/stdio-dup.c new file mode 100644 index 000000000..0cea04d7d --- /dev/null +++ b/test/src/stdio-dup.c @@ -0,0 +1,51 @@ +#include "test.h" +#include +#include +#include +#include + +#define TEST(c) \ + do { \ + errno = 0; \ + if (!(c)) \ + t_error("%s failed (errno = %d)\n", #c, errno); \ + } while (0) + +int main(void) { + TEST(dup(-1) == -1 && errno == EBADF); + TEST(dup(1234) == -1 && errno == EBADF); + + // A dup of stdout behaves like stdout. + int d = dup(1); + TEST(d > 2); + TEST(write(d, "", 0) == 0); + TEST((fcntl(d, F_GETFL) & O_ACCMODE) == O_WRONLY); + struct stat st; + TEST(fstat(d, &st) == 0); + + // A dup of stdin behaves like stdin. + int r = dup(0); + TEST(r > 2 && r != d); + TEST((fcntl(r, F_GETFL) & O_ACCMODE) == O_RDONLY); + TEST(close(r) == 0); + + TEST(dup2(1, 1) == 1); + TEST(dup3(1, 1, 0) == -1 && errno == EINVAL); + + TEST(dup2(1, 40) == 40); + TEST(write(40, "", 0) == 0); + TEST(close(40) == 0); + TEST(write(40, "", 0) == -1 && errno == EBADF); + + int f = fcntl(1, F_DUPFD, 20); + TEST(f >= 20); + TEST(write(f, "", 0) == 0); + TEST(close(f) == 0); + + // Closing the dup leaves the original intact. + TEST(close(d) == 0); + TEST(write(d, "", 0) == -1 && errno == EBADF); + TEST(write(1, "", 0) == 0); + + return t_status; +} diff --git a/test/src/stdio-dup2-fflush.c b/test/src/stdio-dup2-fflush.c new file mode 100644 index 000000000..fa8a3dc64 --- /dev/null +++ b/test/src/stdio-dup2-fflush.c @@ -0,0 +1,17 @@ +#include +#include + +int main(void) { + // Write "firstline" to stderr using both stderr and a dup of stderr + write(STDERR_FILENO, "first", 5); + if (dup2(STDERR_FILENO, STDOUT_FILENO) != 1) { + return 1; + } + printf("line"); + fflush(stdout); // Should flush stderr + + // Write "secondline" using just the original stderr handle + write(STDERR_FILENO, "secondline\n", 11); + + return 0; +} From e1478ee07235cb4caa74f42f1c67253bafc9f296 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 15 Oct 2026 14:03:28 -0500 Subject: [PATCH 65/96] Redo stdio implementation to use the descriptor table --- libc-bottom-half/CMakeLists.txt | 8 +- .../cloudlibc/src/libc/fcntl/fcntl.c | 24 +-- .../cloudlibc/src/libc/stdlib/_Exit.c | 4 + .../cloudlibc/src/libc/sys/ioctl/ioctl.c | 29 +-- .../cloudlibc/src/libc/sys/stat/fstat.c | 17 +- .../cloudlibc/src/libc/sys/uio/readv.c | 34 +--- .../cloudlibc/src/libc/sys/uio/writev.c | 33 +--- .../cloudlibc/src/libc/unistd/lseek.c | 15 +- .../cloudlibc/src/libc/unistd/read.c | 24 +-- .../cloudlibc/src/libc/unistd/write.c | 24 +-- .../headers/private/wasi/descriptor_table.h | 18 +- .../headers/private/wasi/file_utils.h | 10 + .../headers/private/wasi/webp2_stdio.h | 37 ---- .../sources/__wasilibc_fd_renumber.c | 12 +- .../sources/__wasilibc_report_error.c | 4 + libc-bottom-half/sources/__wasilibc_tell.c | 13 +- libc-bottom-half/sources/abort.c | 5 +- libc-bottom-half/sources/file_utils.c | 34 +++- libc-bottom-half/sources/isatty.c | 13 +- libc-bottom-half/sources/ppoll.c | 22 ++- libc-bottom-half/sources/webp2_stdio.c | 187 +++++++++++------- .../headers/private/wasi/report-error.h | 10 +- .../headers/private/wasi/webp2_stdio.h | 23 +++ libc-top-half/musl/src/stdio/fflush.c | 8 +- 24 files changed, 277 insertions(+), 331 deletions(-) delete mode 100644 libc-bottom-half/headers/private/wasi/webp2_stdio.h create mode 100644 libc-top-half/headers/private/wasi/webp2_stdio.h diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index da8ff8efc..9a3396501 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -99,8 +99,10 @@ set(bottom_half_sources sources/__wasilibc_tell.c sources/abort.c sources/complex-builtins.c + sources/descriptor_table.c sources/environ.c sources/errno.c + sources/file_utils.c sources/getentropy.c sources/isatty.c sources/math/fmin-fmax.c @@ -118,12 +120,6 @@ if (NOT WEB STREQUAL "p2") sources/dup.c sources/pipe.c ) - if (WASI STREQUAL "p2" OR WASI STREQUAL "p3") - list(APPEND bottom_half_sources - sources/descriptor_table.c - sources/file_utils.c - ) - endif() endif() # Filesystem diff --git a/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c b/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c index 1eb52f1c8..97f70333a 100644 --- a/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c +++ b/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c @@ -7,25 +7,19 @@ #include #include -#if defined(__webp2__) -#include -#elif !defined(__wasip1__) +#if !defined(__wasip1__) #include +#include #include #include #endif int fcntl(int fildes, int cmd, ...) { -#if defined(__wasip2__) || defined(__wasip3__) +#if defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(fildes, &entry) < 0) return -1; defer descriptor_table_entry_dec(entry); -#elif defined(__webp2__) - if (!__wasilibc_webp2_fd_ok(fildes)) { - errno = EBADF; - return -1; - } #endif switch (cmd) { @@ -60,14 +54,12 @@ int fcntl(int fildes, int cmd, ...) { oflags |= O_SEARCH; } return oflags; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) if (!entry.vtable->fcntl_getfl) { errno = EINVAL; return -1; } return entry.vtable->fcntl_getfl(entry.data); -#elif defined(__webp2__) - return __wasilibc_webp2_is_readable(fildes) ? O_RDONLY : O_WRONLY; #else # error "Unknown WASI version" #endif @@ -87,22 +79,18 @@ int fcntl(int fildes, int cmd, ...) { errno = error; return -1; } -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) if (!entry.vtable->fcntl_setfl) { errno = EINVAL; return -1; } return entry.vtable->fcntl_setfl(entry.data, flags); -#elif defined(__webp2__) - (void)flags; - errno = EINVAL; - return -1; #else # error "Unknown WASI version" #endif return 0; } -#if !defined(__wasip1__) && !defined(__webp2__) +#if !defined(__wasip1__) case F_DUPFD: case F_DUPFD_CLOEXEC: { va_list ap; diff --git a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c index 11ee66b5e..0cf6209d0 100644 --- a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c +++ b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c @@ -8,6 +8,10 @@ #include #include +#ifdef __webp2__ +#include +#endif + noreturn void _Exit(int status) { #if defined(__wasip1__) __wasi_proc_exit(status); 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 4b895fe4e..c82945db9 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c @@ -9,20 +9,11 @@ #include #include -#if defined(__webp2__) -#include -#elif !defined(__wasip1__) +#if !defined(__wasip1__) #include #endif int ioctl(int fildes, int request, ...) { -#if defined(__webp2__) - if (!__wasilibc_webp2_fd_ok(fildes)) { - errno = EBADF; - return -1; - } -#endif - switch (request) { case FIONREAD: { #if defined(__wasip1__) @@ -68,18 +59,10 @@ int ioctl(int fildes, int request, ...) { // No data available for reading. *result = 0; return 0; -#elif defined(__wasip2__) || defined(__wasip3__) - // wasip{2,3} doesn't support this operation +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) + // wasip{2,3} and webp2 don't support this operation errno = ENOTSUP; return -1; -#elif defined(__webp2__) - // There is never anything available to read. - va_list ap; - va_start(ap, request); - int *result = va_arg(ap, int *); - va_end(ap); - *result = 0; - return 0; #else # error "Unknown WASI version" #endif @@ -110,7 +93,7 @@ int ioctl(int fildes, int request, ...) { return -1; } return 0; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(fildes, &entry) < 0) return -1; @@ -125,10 +108,6 @@ int ioctl(int fildes, int request, ...) { return -1; } return entry.vtable->set_blocking(entry.data, blocking); -#elif defined(__webp2__) - // There's no difference between "blocking" and "non-blocking" IO for our - // stdio implementation, so this does nothing. - return 0; #else # error "Unknown WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstat.c b/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstat.c index c8d7c7997..3d2d8c721 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstat.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstat.c @@ -7,14 +7,11 @@ #include #include -#if defined(__webp2__) -#include -#include -#else +#ifndef __webp2__ #include "stat_impl.h" #endif -#if !defined(__wasip1__) && !defined(__webp2__) +#if !defined(__wasip1__) #include #endif @@ -28,21 +25,13 @@ int fstat(int fildes, struct stat *buf) { } to_public_stat(&internal_stat, buf); return 0; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) // Translate the file descriptor to an internal handle descriptor_table_entry_t entry; if (descriptor_table_get(fildes, &entry) < 0) return -1; defer descriptor_table_entry_dec(entry); return entry.vtable->fstat(entry.data, buf); -#elif defined(__webp2__) - if (!__wasilibc_webp2_fd_ok(fildes)) { - errno = EBADF; - return -1; - } - memset(buf, 0, sizeof(*buf)); - buf->st_mode = S_IFCHR; - return 0; #else # error "Unsupported WASI version" #endif 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 74e625ee0..d4593c640 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c @@ -5,15 +5,12 @@ #include #include -#include #include +#include #include #include - -#ifdef __webp2__ -#include -#include -#endif +#include +#include #ifdef __wasip1__ static_assert(offsetof(struct iovec, iov_base) == @@ -33,7 +30,7 @@ static_assert(sizeof(struct iovec) == sizeof(__wasi_iovec_t), #endif ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { - if (iovcnt < 0) { + if (iovcnt < 0 || IOV_MAX < iovcnt) { errno = EINVAL; return -1; } @@ -46,30 +43,11 @@ ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { return -1; } return bytes_read; -#elif defined(__wasip2__) || defined(__wasip3__) - // Skip empty iovecs and then delegate to `read` with the first non-empty - // iovec. - while (iovcnt) { - if (iov->iov_len != 0) { - return read(fildes, iov->iov_base, iov->iov_len); - } - iovcnt--; - iov++; - } - return read(fildes, NULL, 0); -#elif defined(__webp2__) - if (IOV_MAX < iovcnt) { - errno = EINVAL; - return -1; - } - if (!__wasilibc_webp2_iovecs_ok(iov, iovcnt, NULL)) { +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) + if (!__wasilibc_iovecs_ok(iov, iovcnt, NULL)) { errno = EINVAL; return -1; } - if (!__wasilibc_webp2_is_readable(fildes)) { - errno = EBADF; - return -1; - } size_t bytes_read = 0; for (int i = 0; i < iovcnt; i++) { diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/uio/writev.c b/libc-bottom-half/cloudlibc/src/libc/sys/uio/writev.c index 0c458e261..70d62b307 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/writev.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/writev.c @@ -6,13 +6,12 @@ #include #include #include +#include #include #include -#if defined(__webp2__) -#include -#include -#elif !defined(__wasip1__) +#if !defined(__wasip1__) +#include #include #include #endif @@ -35,7 +34,7 @@ static_assert(sizeof(struct iovec) == sizeof(__wasi_ciovec_t), #endif ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { - if (iovcnt < 0) { + if (iovcnt < 0 || IOV_MAX < iovcnt) { errno = EINVAL; return -1; } @@ -49,29 +48,9 @@ ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { return -1; } return bytes_written; -#elif defined(__wasip2__) || defined(__wasip3__) - // Skip empty iovecs and then delegate to `read` with the first non-empty - // iovec. - while (iovcnt) { - if (iov->iov_len != 0) { - return write(fildes, iov->iov_base, iov->iov_len); - } - iovcnt--; - iov++; - } - return write(fildes, NULL, 0); -#elif defined(__webp2__) - if (IOV_MAX < iovcnt) { - errno = EINVAL; - return -1; - } - if (!__wasilibc_webp2_is_writable(fildes)) { - errno = EBADF; - return -1; - } - +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) size_t total_size; - if (!__wasilibc_webp2_iovecs_ok(iov, iovcnt, &total_size)) { + if (!__wasilibc_iovecs_ok(iov, iovcnt, &total_size)) { errno = EINVAL; return -1; } diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c b/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c index aa45ca345..8d85ba1ec 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c @@ -7,9 +7,7 @@ #include #include -#if defined(__webp2__) -#include -#elif !defined(__wasip1__) +#if !defined(__wasip1__) #include #include #include @@ -32,7 +30,7 @@ off_t __lseek(int fildes, off_t offset, int whence) { return -1; } return new_offset; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) // Look up a stream for fildes descriptor_table_entry_t entry; if (descriptor_table_get(fildes, &entry) < 0) @@ -43,15 +41,6 @@ off_t __lseek(int fildes, off_t offset, int whence) { return -1; } return entry.vtable->seek(entry.data, offset, whence); -#elif defined(__webp2__) - (void)offset; - (void)whence; - if (!__wasilibc_webp2_fd_ok(fildes)) { - errno = EBADF; - return -1; - } - errno = ESPIPE; - return -1; #else # error "Unknown WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/read.c b/libc-bottom-half/cloudlibc/src/libc/unistd/read.c index c27095190..95b2baebe 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/read.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/read.c @@ -6,10 +6,7 @@ #include #include -#if defined(__webp2__) -#include -#include -#elif !defined(__wasip1__) +#if !defined(__wasip1__) #include #include #include @@ -28,22 +25,12 @@ ssize_t read(int fildes, void *buf, size_t nbyte) { return -1; } return bytes_read; -#elif defined(__webp2__) - (void)buf; - if (nbyte > SSIZE_MAX) { - errno = EINVAL; - return -1; - } - if (!__wasilibc_webp2_is_readable(fildes)) { - errno = EBADF; - return -1; - } - return 0; // immediate EOF #else descriptor_table_entry_t entry; if (descriptor_table_get(fildes, &entry) < 0) return -1; defer descriptor_table_entry_dec(entry); +#if defined(__wasip2__) || defined(__wasip3__) if (entry.vtable->get_read_stream) { wasi_read_t read; if (entry.vtable->get_read_stream(entry.data, &read) < 0) @@ -51,6 +38,13 @@ ssize_t read(int fildes, void *buf, size_t nbyte) { defer STRONG_UNLOCK(*read.state->lock); return __wasilibc_read(&read, buf, nbyte); } +#elif defined(__webp2__) + if (entry.vtable->read) { + return entry.vtable->read(entry.data, buf, nbyte); + } +#else +#error "Unknown WASI version" +#endif if (entry.vtable->recvfrom) return entry.vtable->recvfrom(entry.data, buf, nbyte, 0, NULL, NULL); errno = EOPNOTSUPP; diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/write.c b/libc-bottom-half/cloudlibc/src/libc/unistd/write.c index 959ec1762..153a9e4a0 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/write.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/write.c @@ -6,10 +6,7 @@ #include #include -#if defined(__webp2__) -#include -#include -#elif !defined(__wasip1__) +#if !defined(__wasip1__) #include #include #include @@ -29,22 +26,12 @@ ssize_t write(int fildes, const void *buf, size_t nbyte) { return -1; } return bytes_written; -#elif defined(__webp2__) - if (nbyte > SSIZE_MAX) { - errno = EINVAL; - return -1; - } - if (!__wasilibc_webp2_is_writable(fildes)) { - errno = EBADF; - return -1; - } - __wasilibc_webp2_write(fildes, buf, nbyte); - return nbyte; #else descriptor_table_entry_t entry; if (descriptor_table_get(fildes, &entry) < 0) return -1; defer descriptor_table_entry_dec(entry); +#if defined(__wasip2__) || defined(__wasip3__) if (entry.vtable->get_write_stream) { wasi_write_t write; if (entry.vtable->get_write_stream(entry.data, &write) < 0) @@ -52,6 +39,13 @@ ssize_t write(int fildes, const void *buf, size_t nbyte) { defer STRONG_UNLOCK(*write.state->lock); return __wasilibc_write(&write, buf, nbyte); } +#elif defined(__webp2__) + if (entry.vtable->write) { + return entry.vtable->write(entry.data, buf, nbyte); + } +#else +#error "Unknown WASI version" +#endif if (entry.vtable->sendto) return entry.vtable->sendto(entry.data, buf, nbyte, 0, NULL, 0); errno = EOPNOTSUPP; diff --git a/libc-bottom-half/headers/private/wasi/descriptor_table.h b/libc-bottom-half/headers/private/wasi/descriptor_table.h index f0bbe5bc2..6be533f82 100644 --- a/libc-bottom-half/headers/private/wasi/descriptor_table.h +++ b/libc-bottom-half/headers/private/wasi/descriptor_table.h @@ -4,6 +4,7 @@ #include #ifndef __wasip1__ + #include #include #include @@ -11,7 +12,6 @@ #include #include -#ifndef __webp2__ #ifdef __wasip3__ #include "lock.h" @@ -126,8 +126,9 @@ static inline bool wasip3_io_state_blocked(wasip3_io_state_t *state) { return false; #endif } -#endif +#endif // __wasip3__ +#if defined(__wasip2__) || defined(__wasip3__) // Metadata for WASI reads which is used to delegate to `__wasilibc_read(...)` // to perform the actual read of a stream. typedef struct wasi_read_t { @@ -171,6 +172,7 @@ typedef struct wasi_write_t { void *eof_data; #endif } wasi_write_t; +#endif // __wasip2__ || __wasip3__ /** * Operations that are required of all descriptors registered as file @@ -191,6 +193,7 @@ typedef struct descriptor_vtable_t { // ===================================================================== // Generic I/O +#if defined(__wasip2__) || defined(__wasip3__) /// Looks up metadata to perform a read operation for this stream. This is /// used to implement the `read` syscall, for example, and is also used with /// `poll` when waiting for readability. @@ -202,6 +205,12 @@ typedef struct descriptor_vtable_t { int (*get_read_stream)(void *, wasi_read_t *); /// Same as `get_read_stream`, but for output streams. int (*get_write_stream)(void *, wasi_write_t *); +#elif defined(__webp2__) + /// Implementation of `read`-the-function. + ssize_t (*read)(void *, void *buf, size_t nbyte); + /// Implementation of `write`-the-function. + ssize_t (*write)(void *, const void *buf, size_t nbyte); +#endif /// Sets the nonblocking flag for this object to the specified value. int (*set_blocking)(void *, bool); @@ -213,9 +222,11 @@ typedef struct descriptor_vtable_t { // ===================================================================== // File-related APIs +#if defined(__wasip2__) || defined(__wasip3__) /// Looks up a `wasi:filesystem/types.descriptor`, if present, from this /// object. int (*get_file)(void *, filesystem_borrow_descriptor_t *); +#endif /// Implementation of `lseek`-the-function. off_t (*seek)(void *, off_t, int); /// Implementation of `fnctl(fd, F_GETFL)`. @@ -364,7 +375,6 @@ static inline void __wasilibc_assert_no_descriptor_leaks(void) {} void __wasilibc_assert_no_descriptor_leaks(void); #endif -#endif // __wasip1__ -#endif // __webp2__ +#endif // !__wasip1__ #endif // DESCRIPTOR_TABLE_H diff --git a/libc-bottom-half/headers/private/wasi/file_utils.h b/libc-bottom-half/headers/private/wasi/file_utils.h index e8314bf0f..abd572ff9 100644 --- a/libc-bottom-half/headers/private/wasi/file_utils.h +++ b/libc-bottom-half/headers/private/wasi/file_utils.h @@ -1,7 +1,17 @@ #ifndef __wasi_file_utils_h #define __wasi_file_utils_h +#include +#include +#include +#include #include + +// Check if the total size of all iovecs would overflow an ssize_t. +// `total_size` can be NULL. +hidden bool __wasilibc_iovecs_ok(const struct iovec *iov, int iovcnt, + size_t *total_size); + #if defined(__wasip2__) || defined(__wasip3__) #include diff --git a/libc-bottom-half/headers/private/wasi/webp2_stdio.h b/libc-bottom-half/headers/private/wasi/webp2_stdio.h deleted file mode 100644 index bc6c97968..000000000 --- a/libc-bottom-half/headers/private/wasi/webp2_stdio.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef WASI_WEBP2_STDIO_H -#define WASI_WEBP2_STDIO_H - -#include - -#ifdef __webp2__ - -#include -#include -#include -#include - -// File descriptors are mostly stubbed out on the web, since there is no -// comparable model of files and streams. However, stdout and stderr are -// supported by routing to console.log, and stdin is supported by immediately -// returning EOF. - -hidden bool __wasilibc_webp2_fd_ok(int fd); -hidden bool __wasilibc_webp2_is_readable(int fd); -hidden bool __wasilibc_webp2_is_writable(int fd); - -// Check if the total size of all iovecs would overflow an ssize_t. -// `total_size` can be NULL. -hidden bool __wasilibc_webp2_iovecs_ok(const struct iovec *iov, int iovcnt, - size_t *total_size); - -// Implementation for the `write` function, writing valid UTF-8 to the stdio -// line buffers. Aborts if `fd` is not writable. -hidden void __wasilibc_webp2_write(int fd, const void *buf, size_t nbyte); - -// Implementation for the `close` function, i.e. finish and flush any line -// buffers and close the fd. Aborts if `fd` is not open. -hidden void __wasilibc_webp2_close(int fd); - -#endif // __webp2__ - -#endif // WASI_WEBP2_STDIO_H diff --git a/libc-bottom-half/sources/__wasilibc_fd_renumber.c b/libc-bottom-half/sources/__wasilibc_fd_renumber.c index 98c334a54..0c085debe 100644 --- a/libc-bottom-half/sources/__wasilibc_fd_renumber.c +++ b/libc-bottom-half/sources/__wasilibc_fd_renumber.c @@ -3,9 +3,7 @@ #include #include -#if defined(__webp2__) -#include -#elif !defined(__wasip1__) +#if !defined(__wasip1__) #include #include #endif @@ -36,15 +34,9 @@ int close(int fd) { errno = error; return -1; } -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) if (descriptor_table_remove(fd) < 0) return -1; -#elif defined(__webp2__) - if (!__wasilibc_webp2_fd_ok(fd)) { - errno = EBADF; - return -1; - } - __wasilibc_webp2_close(fd); #else #error "Unsupported WASI version" #endif // __wasip2__ diff --git a/libc-bottom-half/sources/__wasilibc_report_error.c b/libc-bottom-half/sources/__wasilibc_report_error.c index 4f7fc4f99..4da475331 100644 --- a/libc-bottom-half/sources/__wasilibc_report_error.c +++ b/libc-bottom-half/sources/__wasilibc_report_error.c @@ -2,6 +2,10 @@ #include #include +#ifdef __webp2__ +#include +#endif + void __wasilibc_report_error(const char *message) { #if defined(__wasip1__) || defined(__wasip2__) || defined(__wasip3__) fprintf(stderr, "%s\n", message); diff --git a/libc-bottom-half/sources/__wasilibc_tell.c b/libc-bottom-half/sources/__wasilibc_tell.c index 1ede3b348..a65c96d18 100644 --- a/libc-bottom-half/sources/__wasilibc_tell.c +++ b/libc-bottom-half/sources/__wasilibc_tell.c @@ -2,9 +2,7 @@ #include #include -#if defined(__webp2__) -#include -#elif !defined(__wasip1__) +#if !defined(__wasip1__) #include #include #include @@ -22,7 +20,7 @@ off_t __wasilibc_tell(int fildes) { return -1; } return offset; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) // Look up a stream for fildes descriptor_table_entry_t entry; if (descriptor_table_get(fildes, &entry) < 0) @@ -33,13 +31,6 @@ off_t __wasilibc_tell(int fildes) { return -1; } return entry.vtable->seek(entry.data, 0, SEEK_CUR); -#elif defined(__webp2__) - if (!__wasilibc_webp2_fd_ok(fildes)) { - errno = EBADF; - return -1; - } - errno = ESPIPE; - return -1; #else #error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/sources/abort.c b/libc-bottom-half/sources/abort.c index 1eed0c15a..aa1315abe 100644 --- a/libc-bottom-half/sources/abort.c +++ b/libc-bottom-half/sources/abort.c @@ -1,6 +1,9 @@ -#include "wasi/report-error.h" #include +#ifdef __webp2__ +#include +#endif + void abort(void) { // wasm doesn't support signals, so just trap to halt the program. #ifdef __webp2__ diff --git a/libc-bottom-half/sources/file_utils.c b/libc-bottom-half/sources/file_utils.c index e7f923561..2e84b4b0d 100644 --- a/libc-bottom-half/sources/file_utils.c +++ b/libc-bottom-half/sources/file_utils.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -72,6 +73,7 @@ static int validate_utf8(const char *ptr_signed) { return i; } +#if defined(__wasip2__) || defined(__wasip3__) int wasi_string_from_c(const char *s, wasi_string_t *out) { int len = validate_utf8(s); if (len < 0) { @@ -82,6 +84,26 @@ int wasi_string_from_c(const char *s, wasi_string_t *out) { out->len = len; return 0; } +#endif // __wasip2__ || __wasip3__ + +bool __wasilibc_iovecs_ok(const struct iovec *iov, int iovcnt, + size_t *total_size) { + size_t _total_size = 0; + if (total_size) { + *total_size = 0; + } + for (int i = 0; i < iovcnt; i++) { + const struct iovec *v = &iov[i]; + if (v->iov_len > SSIZE_MAX || _total_size > SSIZE_MAX - v->iov_len) { + return false; + } + _total_size += v->iov_len; + } + if (total_size) { + *total_size = _total_size; + } + return true; +} #ifdef __wasip2__ int __wasilibc_pollable_block_on(poll_borrow_pollable_t pollable, @@ -121,9 +143,9 @@ int __wasilibc_pollable_block_on(poll_borrow_pollable_t pollable, errno = EWOULDBLOCK; return -1; } -#endif +#endif // __wasip2__ -#ifndef __wasip2__ +#ifdef __wasip3__ /// Update `state` with the result of `code` that happened. static size_t wasip3_io_update_code(wasip3_io_state_t *state, wasip3_waitable_status_t code) { @@ -466,8 +488,9 @@ void wasip3_write_state_close(wasip3_io_state_t *state) { filesystem_stream_u8_drop_writable(state->stream); memset(state, 0, sizeof(*state)); } -#endif +#endif // __wasip3__ +#if defined(__wasip2__) || defined(__wasip3__) /// Internal implementation of `__wasilibc_write` except that this doesn't /// update `write->offset`. That's done in the wrapper around this. static ssize_t __wasilibc_write_without_offset_update(wasi_write_t *write, @@ -854,8 +877,9 @@ ssize_t __wasilibc_read(wasi_read_t *read, void *buffer, size_t length) { #endif return result; } +#endif // __wasip2__ || __wasip3__ -#ifndef __wasip2__ +#ifdef __wasip3__ static void wasip3_poll_read_ready(void *data, poll_state_t *state, wasip3_event_t *event) { wasip3_io_state_t *iostate = (wasip3_io_state_t *)data; @@ -1000,4 +1024,4 @@ int __wasilibc_write_poll(wasip3_io_state_t *iostate, poll_state_t *state) { return wasip3_stream_poll(iostate, state, POLLWRNORM, wasip3_poll_write_ready); } -#endif // !__wasip2__ +#endif // __wasip3__ diff --git a/libc-bottom-half/sources/isatty.c b/libc-bottom-half/sources/isatty.c index ad0d1f9ab..bd907aa5e 100644 --- a/libc-bottom-half/sources/isatty.c +++ b/libc-bottom-half/sources/isatty.c @@ -4,9 +4,7 @@ #include #include -#if defined(__webp2__) -#include -#elif !defined(__wasip1__) +#if !defined(__wasip1__) #include #include #include @@ -30,7 +28,7 @@ int __isatty(int fd) { } return 1; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) // Translate the file descriptor into an internal handle descriptor_table_entry_t entry; if (descriptor_table_get(fd, &entry) < 0) @@ -42,13 +40,6 @@ int __isatty(int fd) { } return entry.vtable->isatty(entry.data); -#elif defined(__webp2__) - if (!__wasilibc_webp2_fd_ok(fd)) { - errno = EBADF; - return 0; - } - errno = ENOTTY; - return 0; #else #error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/sources/ppoll.c b/libc-bottom-half/sources/ppoll.c index 9f6352beb..8ec6a0843 100644 --- a/libc-bottom-half/sources/ppoll.c +++ b/libc-bottom-half/sources/ppoll.c @@ -6,11 +6,12 @@ #include #include +#if !defined(__wasip1__) +#include +#endif + #if defined(__webp2__) #include -#include -#elif !defined(__wasip1__) -#include #endif #if defined(__wasip1__) @@ -584,14 +585,17 @@ static int ppoll_impl(struct pollfd *fds, size_t nfds, if (pollfd->fd < 0) { continue; } - if (!__wasilibc_webp2_fd_ok(pollfd->fd)) { + + descriptor_table_entry_t entry; + if (descriptor_table_get(pollfd->fd, &entry)) { pollfd->revents = POLLNVAL; - } else if (__wasilibc_webp2_is_readable(pollfd->fd)) { - // stdin is always readable - pollfd->revents = pollfd->events & POLLRDNORM; } else { - // stdout and stderr are always writable - pollfd->revents = pollfd->events & POLLWRNORM; + if (entry.vtable->read) { + pollfd->revents = pollfd->events & POLLRDNORM; + } + if (entry.vtable->write) { + pollfd->revents = pollfd->events & POLLWRNORM; + } } if (pollfd->revents != 0) { ready++; diff --git a/libc-bottom-half/sources/webp2_stdio.c b/libc-bottom-half/sources/webp2_stdio.c index 31b92e5f5..c3c1370a9 100644 --- a/libc-bottom-half/sources/webp2_stdio.c +++ b/libc-bottom-half/sources/webp2_stdio.c @@ -1,92 +1,109 @@ #include +#include +#include #include #include #include #include +#include #include #include #include #define LINE_BUF_SIZE 4096 -typedef struct line_buffer_t { +typedef struct { + // Must be the first field so that descriptor table `data` pointers can have + // type `descriptor_refcnt_t *` but be cast to `stdio_t *`. + descriptor_refcnt_t refcnt; bool warn; + bool closed; + wasilibc_utf8_writer_t utf8; uint8_t buf[LINE_BUF_SIZE]; -} line_buffer_t; +} stdio_t; -static line_buffer_t line_buffers[2] = { - (line_buffer_t){}, - (line_buffer_t){.warn = true}, -}; -static wasilibc_utf8_writer_t utf8_writers[2] = { - __wasilibc_make_utf8_writer(line_buffers[0].buf, LINE_BUF_SIZE), - __wasilibc_make_utf8_writer(line_buffers[1].buf, LINE_BUF_SIZE), -}; +static stdio_t __stdin; +static stdio_t __stdout; +static stdio_t __stderr; -static bool fd_closed[3]; - -static void line_buffer_out(line_buffer_t *lb, wasilibc_utf8_writer_t *utf8) { +static void stdio_out(stdio_t *stdio) { webp2_string_t out = - (webp2_string_t){.ptr = utf8->buf, .len = utf8->valid_len}; - if (lb->warn) { + (webp2_string_t){.ptr = stdio->utf8.buf, .len = stdio->utf8.valid_len}; + if (stdio->warn) { webp2_console_warn(&out); } else { webp2_console_log(&out); } } +static void stdio_flush(stdio_t *stdio) { + if (stdio->utf8.valid_len > 0) { + stdio_out(stdio); + __wasilibc_utf8_reset(&stdio->utf8); + } +} + // Flushes all data in line buffer to the console, replacing any trailing // multi-byte code points with the replacement character. -static void finish_and_flush(int fd) { - assert(fd == STDOUT_FILENO || fd == STDERR_FILENO); - if (fd_closed[fd]) { +static void finish_and_flush(stdio_t *stdio) { + if (stdio->closed) { return; } - wasilibc_utf8_writer_t *utf8 = &utf8_writers[fd - 1]; - if (!__wasilibc_utf8_abort_current(utf8)) { + if (!__wasilibc_utf8_abort_current(&stdio->utf8)) { // Emit what is already valid in the buffer first, and carry over the // in-progress codepoint - __wasilibc_webp2_fflush(fd); + stdio_flush(stdio); } - __wasilibc_utf8_abort_current(utf8); - __wasilibc_webp2_fflush(fd); + __wasilibc_utf8_abort_current(&stdio->utf8); + stdio_flush(stdio); } -bool __wasilibc_webp2_fd_ok(int fd) { - return STDIN_FILENO <= fd && fd <= STDERR_FILENO && !fd_closed[fd]; +static void stdio_free(void *data) { + stdio_t *stdio = (stdio_t *)data; + if (stdio == &__stdout || stdio == &__stderr) { + // Don't flush musl's buffers, but do flush everything we have. + finish_and_flush(stdio); + } + stdio->closed = true; } -bool __wasilibc_webp2_is_readable(int fd) { - return fd == STDIN_FILENO && !fd_closed[fd]; +static int stdio_fstat(void *data, struct stat *buf) { + (void)data; + memset(buf, 0, sizeof(*buf)); + buf->st_mode = S_IFCHR; + return 0; } -bool __wasilibc_webp2_is_writable(int fd) { - return (fd == STDOUT_FILENO || fd == STDERR_FILENO) && !fd_closed[fd]; +static int stdio_fcntl_getfl(void *data) { + stdio_t *stdio = (stdio_t *)data; + if (stdio == &__stdin) { + return O_RDONLY; + } else { + return O_WRONLY; + } } -bool __wasilibc_webp2_iovecs_ok(const struct iovec *iov, int iovcnt, - size_t *total_size) { - size_t _total_size = 0; - if (total_size) { - *total_size = 0; +static ssize_t stdio_read(void *data, void *buf, size_t nbyte) { + (void)data; + (void)buf; + if (nbyte > SSIZE_MAX) { + errno = EINVAL; + return -1; } - for (int i = 0; i < iovcnt; i++) { - const struct iovec *v = &iov[i]; - if (v->iov_len > SSIZE_MAX || _total_size > SSIZE_MAX - v->iov_len) { - return false; - } - _total_size += v->iov_len; - } - if (total_size) { - *total_size = _total_size; - } - return true; + return 0; // immediate EOF } -void __wasilibc_webp2_write(int fd, const void *buf, size_t nbyte) { - assert(__wasilibc_webp2_is_writable(fd)); - line_buffer_t *line_buffer = &line_buffers[fd - 1]; - wasilibc_utf8_writer_t *utf8 = &utf8_writers[fd - 1]; +static int stdio_set_blocking(void *data, bool blocking) { + // There's no difference between "blocking" and "non-blocking" IO for our + // stdio implementation, so this does nothing. + (void)data; + (void)blocking; + return 0; +} + +static ssize_t stdio_write(void *data, const void *buf, size_t nbyte) { + stdio_t *stdio = (stdio_t *)data; + wasilibc_utf8_writer_t *utf8 = &stdio->utf8; for (size_t i = 0; i < nbyte; i++) { uint8_t b = ((uint8_t *)buf)[i]; // First write to the buffer, flushing first if there was no room for the @@ -94,7 +111,7 @@ void __wasilibc_webp2_write(int fd, const void *buf, size_t nbyte) { // or because there was no room for a replacement char). bool already_logged = false; if (!__wasilibc_utf8_write(utf8, b)) { - line_buffer_out(line_buffer, utf8); + stdio_out(stdio); __wasilibc_utf8_reset(utf8); __wasilibc_utf8_write(utf8, b); // infallible already_logged = true; @@ -106,45 +123,75 @@ void __wasilibc_webp2_write(int fd, const void *buf, size_t nbyte) { utf8->valid_len--; bool skip = utf8->valid_len == 0 && already_logged; if (!skip) { - line_buffer_out(line_buffer, utf8); + stdio_out(stdio); } __wasilibc_utf8_reset(utf8); } } + return nbyte; } -void __wasilibc_webp2_close(int fd) { - assert(__wasilibc_webp2_fd_ok(fd)); - if (__wasilibc_webp2_is_writable(fd)) { - // Don't flush musl's buffers, but do flush everything we have. - finish_and_flush(fd); +static descriptor_vtable_t stdin_vtable = { + .read = stdio_read, + .fstat = stdio_fstat, + .fcntl_getfl = stdio_fcntl_getfl, + .set_blocking = stdio_set_blocking, +}; + +static descriptor_vtable_t stdouterr_vtable = { + .free = stdio_free, + .write = stdio_write, + .fstat = stdio_fstat, + .fcntl_getfl = stdio_fcntl_getfl, + .set_blocking = stdio_set_blocking, +}; + +static int stdio_add(stdio_t *stdio, descriptor_vtable_t *vtable) { + descriptor_table_entry_t entry = (descriptor_table_entry_t) { + .data = (descriptor_refcnt_t *)stdio, + .vtable = vtable, + }; + return descriptor_table_insert(entry); +} + +int __wasilibc_init_stdio() { + if (stdio_add(&__stdin, &stdin_vtable) < 0) { + return -1; } - fd_closed[fd] = true; + + __stdout.utf8 = __wasilibc_make_utf8_writer(__stdout.buf, LINE_BUF_SIZE); + if (stdio_add(&__stdout, &stdouterr_vtable) < 0) { + return -1; + } + + __stderr.utf8 = __wasilibc_make_utf8_writer(__stderr.buf, LINE_BUF_SIZE); + __stderr.warn = true; + if (stdio_add(&__stderr, &stdouterr_vtable) < 0) { + return -1; + } + + return 0; } void __wasilibc_webp2_fflush(int fd) { - assert(fd == STDOUT_FILENO || fd == STDERR_FILENO); - if (fd_closed[fd]) { + descriptor_table_entry_t entry; + if (descriptor_table_get(fd, &entry) < 0) { return; } - line_buffer_t *lb = &line_buffers[fd - 1]; - wasilibc_utf8_writer_t *utf8 = &utf8_writers[fd - 1]; - if (utf8->valid_len > 0) { - line_buffer_out(lb, utf8); - __wasilibc_utf8_reset(utf8); - } + stdio_t *stdio = (stdio_t *)entry.data; + stdio_flush(stdio); } void __wasilibc_webp2_flush_and_close_stdio(bool also_musl) { if (also_musl) { fflush(stdout); } - finish_and_flush(STDOUT_FILENO); - fd_closed[STDOUT_FILENO] = true; - + finish_and_flush(&__stdout); + __stdout.closed = true; + if (also_musl) { fflush(stderr); } - finish_and_flush(STDERR_FILENO); - fd_closed[STDERR_FILENO] = true; + finish_and_flush(&__stderr); + __stderr.closed = true; } diff --git a/libc-top-half/headers/private/wasi/report-error.h b/libc-top-half/headers/private/wasi/report-error.h index 82d89ddc2..16fed81c8 100644 --- a/libc-top-half/headers/private/wasi/report-error.h +++ b/libc-top-half/headers/private/wasi/report-error.h @@ -9,14 +9,6 @@ hidden void __wasilibc_report_error(const char* message); #ifdef __webp2__ -// Flush all valid content in one of our linebuffers (leaving trailing -// multibyte content alone to be completed later). -hidden void __wasilibc_webp2_fflush(int fd); - -// Finalizes and flushes the contents of stdout & stderr in preparation for app -// exit. Optionally also runs `fflush` on musl's stdio streams first. -hidden void __wasilibc_webp2_flush_and_close_stdio(bool also_musl); - // Reports an error to the console using webp2 bindings. void __wasilibc_webp2_report_error(const char* message); @@ -27,4 +19,4 @@ void __wasilibc_webp2_report_error(const char* message); } while (0) #endif -#endif +#endif // __webp2__ diff --git a/libc-top-half/headers/private/wasi/webp2_stdio.h b/libc-top-half/headers/private/wasi/webp2_stdio.h new file mode 100644 index 000000000..95bd2e046 --- /dev/null +++ b/libc-top-half/headers/private/wasi/webp2_stdio.h @@ -0,0 +1,23 @@ +#ifndef WASI_WEBP2_STDIO_H +#define WASI_WEBP2_STDIO_H + +#include + +#ifdef __webp2__ + +#include +#include +#include +#include + +// Flush all valid content in one of our linebuffers (leaving trailing +// multibyte content alone to be completed later). +hidden void __wasilibc_webp2_fflush(int fd); + +// Finalizes and flushes the contents of stdout & stderr in preparation for app +// exit. Optionally also runs `fflush` on musl's stdio streams first. +hidden void __wasilibc_webp2_flush_and_close_stdio(bool also_musl); + +#endif // __webp2__ + +#endif // WASI_WEBP2_STDIO_H diff --git a/libc-top-half/musl/src/stdio/fflush.c b/libc-top-half/musl/src/stdio/fflush.c index 6f83332ff..b1e77c2b7 100644 --- a/libc-top-half/musl/src/stdio/fflush.c +++ b/libc-top-half/musl/src/stdio/fflush.c @@ -2,6 +2,10 @@ #include "wasi/report-error.h" #include +#ifdef __webp2__ +#include +#endif + /* stdout.c will override this if linked */ static FILE *volatile dummy = 0; weak_alias(dummy, __stdout_used); @@ -43,9 +47,7 @@ int fflush(FILE *f) f->rpos = f->rend = 0; #ifdef __webp2__ - if (f->fd == STDOUT_FILENO || f->fd == STDERR_FILENO) { - __wasilibc_webp2_fflush(f->fd); - } + __wasilibc_webp2_fflush(f->fd); #endif FUNLOCK(f); From 0ef91863a23cf4d7ccba9796164c20fa2ac2739b Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 15 Oct 2026 14:14:43 -0500 Subject: [PATCH 66/96] Actually enable dup --- libc-bottom-half/CMakeLists.txt | 2 +- libc-bottom-half/sources/webp2_file_descriptors.c | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index 9a3396501..de6b1ff37 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -100,6 +100,7 @@ set(bottom_half_sources sources/abort.c sources/complex-builtins.c sources/descriptor_table.c + sources/dup.c sources/environ.c sources/errno.c sources/file_utils.c @@ -117,7 +118,6 @@ set(bottom_half_sources # File descriptors if (NOT WEB STREQUAL "p2") list(APPEND bottom_half_sources - sources/dup.c sources/pipe.c ) endif() diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c index 6709ce715..259ae841a 100644 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ b/libc-bottom-half/sources/webp2_file_descriptors.c @@ -3,14 +3,6 @@ #include "wasi/report-error.h" #include -int __dup3(int fd, int newfd, int flags) { WEBP2_UNSUPPORTED("__dup3"); } - -int dup(int fd) { WEBP2_UNSUPPORTED("dup"); } - -int dup2(int fd, int newfd) { WEBP2_UNSUPPORTED("dup2"); } - -int dup3(int fd, int newfd, int flags) { WEBP2_UNSUPPORTED("dup3"); } - int pipe(int fd[2]) { WEBP2_UNSUPPORTED("pipe"); } int pipe2(int fd[2], int flags) { WEBP2_UNSUPPORTED("pipe2"); } From 69ff2f29b1d3ed9ac19f4395ac02d3a1dad98cd4 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 15 Oct 2026 14:16:17 -0500 Subject: [PATCH 67/96] Shut up unused function warning --- libc-bottom-half/sources/file_utils.c | 40 +++++++++++++-------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/libc-bottom-half/sources/file_utils.c b/libc-bottom-half/sources/file_utils.c index 2e84b4b0d..a6a2427f2 100644 --- a/libc-bottom-half/sources/file_utils.c +++ b/libc-bottom-half/sources/file_utils.c @@ -8,6 +8,26 @@ #include #include +bool __wasilibc_iovecs_ok(const struct iovec *iov, int iovcnt, + size_t *total_size) { + size_t _total_size = 0; + if (total_size) { + *total_size = 0; + } + for (int i = 0; i < iovcnt; i++) { + const struct iovec *v = &iov[i]; + if (v->iov_len > SSIZE_MAX || _total_size > SSIZE_MAX - v->iov_len) { + return false; + } + _total_size += v->iov_len; + } + if (total_size) { + *total_size = _total_size; + } + return true; +} + +#if defined(__wasip2__) || defined(__wasip3__) /** * Validates that `ptr_signed` is a valid utf-8 string. * @@ -73,7 +93,6 @@ static int validate_utf8(const char *ptr_signed) { return i; } -#if defined(__wasip2__) || defined(__wasip3__) int wasi_string_from_c(const char *s, wasi_string_t *out) { int len = validate_utf8(s); if (len < 0) { @@ -86,25 +105,6 @@ int wasi_string_from_c(const char *s, wasi_string_t *out) { } #endif // __wasip2__ || __wasip3__ -bool __wasilibc_iovecs_ok(const struct iovec *iov, int iovcnt, - size_t *total_size) { - size_t _total_size = 0; - if (total_size) { - *total_size = 0; - } - for (int i = 0; i < iovcnt; i++) { - const struct iovec *v = &iov[i]; - if (v->iov_len > SSIZE_MAX || _total_size > SSIZE_MAX - v->iov_len) { - return false; - } - _total_size += v->iov_len; - } - if (total_size) { - *total_size = _total_size; - } - return true; -} - #ifdef __wasip2__ int __wasilibc_pollable_block_on(poll_borrow_pollable_t pollable, monotonic_clock_duration_t timeout) { From 66cedbf5af7500f3da82dfdbab3d9ceba0718a46 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 15 Oct 2026 14:17:36 -0500 Subject: [PATCH 68/96] Update expected symbols --- expected/wasm32-webp2/defined-symbols.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/expected/wasm32-webp2/defined-symbols.txt b/expected/wasm32-webp2/defined-symbols.txt index aae4a124e..588e04cc3 100644 --- a/expected/wasm32-webp2/defined-symbols.txt +++ b/expected/wasm32-webp2/defined-symbols.txt @@ -306,6 +306,7 @@ __wait __wake __wasilibc_access __wasilibc_deinitialize_environ +__wasilibc_descriptor_deallocate __wasilibc_dttoif __wasilibc_ensure_environ __wasilibc_environ @@ -313,7 +314,9 @@ __wasilibc_futex_wait __wasilibc_futex_wake __wasilibc_get_environ __wasilibc_iftodt +__wasilibc_init_stdio __wasilibc_initialize_environ +__wasilibc_iovecs_ok __wasilibc_link __wasilibc_link_newat __wasilibc_link_oldat @@ -511,6 +514,11 @@ ctanhl ctanl ctime ctime_r +descriptor_table_clear +descriptor_table_dup +descriptor_table_get +descriptor_table_insert +descriptor_table_remove difftime dirfd dirname From 3eb59465a1fc9ffdc4b9f2c668f7a4c452fb4b36 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 15 Oct 2026 14:25:06 -0500 Subject: [PATCH 69/96] Update clock_getres.c --- .../cloudlibc/src/libc/time/clock_getres.c | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/libc-bottom-half/cloudlibc/src/libc/time/clock_getres.c b/libc-bottom-half/cloudlibc/src/libc/time/clock_getres.c index 69a44e538..0dda30119 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/clock_getres.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/clock_getres.c @@ -17,41 +17,35 @@ int clock_getres(clockid_t clock_id, struct timespec *res) { return -1; } *res = timestamp_to_timespec(ts); -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) if (res != NULL) { if (clock_id == CLOCK_REALTIME) { -#ifdef __wasip2__ +#if defined(__wasip2__) wall_clock_datetime_t time_result; wall_clock_resolution(&time_result); *res = timestamp_to_timespec(&time_result); -#else // __wasip3__ +#elif defined(__wasip3__) *res = instant_to_timespec(system_clock_get_resolution()); +#else // __webp2__ + // Date.now() is always limited to 1ms resolution. + *res = (struct timespec){ .tv_nsec = NSEC_PER_MSEC }; #endif } else if (clock_id == CLOCK_MONOTONIC) { -#ifdef __wasip2__ +#if defined(__wasip2__) monotonic_clock_duration_t time_result = monotonic_clock_resolution(); -#else // __wasip3__ + *res = instant_to_timespec(time_result); +#elif defined(__wasip3__) monotonic_clock_duration_t time_result = monotonic_clock_get_resolution(); -#endif *res = instant_to_timespec(time_result); +#else // __webp2__ + // Performance.now() has up to five-microsecond precision. + *res = (struct timespec){ .tv_nsec = 5 * NSEC_PER_USEC }; +#endif } else { errno = EINVAL; // wasip2/3 only supports wall and monotonic clocks return -1; } } -#elif defined(__webp2__) - if (res != NULL) { - if (clock_id == CLOCK_REALTIME) { - // Date.now() is always limited to 1ms resolution. - *res = (struct timespec){ .tv_nsec = NSEC_PER_MSEC }; - } else if (clock_id == CLOCK_MONOTONIC) { - // Performance.now() has up to five-microsecond precision. - *res = (struct timespec){ .tv_nsec = 5 * NSEC_PER_USEC }; - } else { - errno = EINVAL; // webp2 only supports wall and monotonic clocks - return -1; - } - } #else # error "Unsupported WASI version" #endif From 435da9b69cc6ae1a8b38749d9aa6b3f987219795 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 15 Oct 2026 15:26:54 -0500 Subject: [PATCH 70/96] Spread stubs out across the whole program --- libc-bottom-half/CMakeLists.txt | 106 ++++---- .../cloudlibc/src/libc/dirent/closedir.c | 8 + .../cloudlibc/src/libc/dirent/dirent_impl.h | 4 +- .../cloudlibc/src/libc/dirent/dirfd.c | 8 + .../cloudlibc/src/libc/dirent/fdclosedir.c | 8 + .../cloudlibc/src/libc/dirent/fdopendir.c | 6 + .../cloudlibc/src/libc/dirent/readdir.c | 10 + .../cloudlibc/src/libc/dirent/rewinddir.c | 6 + .../cloudlibc/src/libc/dirent/seekdir.c | 6 + .../cloudlibc/src/libc/dirent/telldir.c | 6 + .../cloudlibc/src/libc/fcntl/posix_fadvise.c | 6 + .../src/libc/fcntl/posix_fallocate.c | 6 + .../cloudlibc/src/libc/sys/stat/futimens.c | 9 + .../cloudlibc/src/libc/sys/uio/preadv.c | 6 + .../cloudlibc/src/libc/sys/uio/pwritev.c | 6 + .../cloudlibc/src/libc/time/clock_nanosleep.c | 6 + .../cloudlibc/src/libc/time/nanosleep.c | 8 + .../cloudlibc/src/libc/unistd/fdatasync.c | 6 + .../cloudlibc/src/libc/unistd/fsync.c | 6 + .../cloudlibc/src/libc/unistd/ftruncate.c | 6 + .../cloudlibc/src/libc/unistd/pread.c | 6 + .../cloudlibc/src/libc/unistd/pwrite.c | 6 + .../cloudlibc/src/libc/unistd/sleep.c | 8 + .../cloudlibc/src/libc/unistd/unlinkat.c | 8 + .../cloudlibc/src/libc/unistd/usleep.c | 8 + libc-bottom-half/sources/accept.c | 12 + libc-bottom-half/sources/at_fdcwd.c | 59 ++++- libc-bottom-half/sources/bind.c | 10 +- libc-bottom-half/sources/chdir.c | 15 +- libc-bottom-half/sources/connect.c | 8 + libc-bottom-half/sources/getcwd.c | 10 + libc-bottom-half/sources/getsockpeername.c | 12 + libc-bottom-half/sources/listen.c | 8 + libc-bottom-half/sources/pipe.c | 18 +- libc-bottom-half/sources/posix.c | 129 +++++++++- libc-bottom-half/sources/preopens.c | 19 +- libc-bottom-half/sources/recv.c | 12 + libc-bottom-half/sources/send.c | 12 + libc-bottom-half/sources/shutdown.c | 8 + libc-bottom-half/sources/socket.c | 11 + libc-bottom-half/sources/sockopt.c | 12 + libc-bottom-half/sources/truncate.c | 8 + .../sources/webp2_file_descriptors.c | 8 - libc-bottom-half/sources/webp2_filesystem.c | 242 ------------------ libc-bottom-half/sources/webp2_sleep.c | 21 -- libc-bottom-half/sources/webp2_socket.c | 67 ----- 46 files changed, 559 insertions(+), 411 deletions(-) delete mode 100644 libc-bottom-half/sources/webp2_file_descriptors.c delete mode 100644 libc-bottom-half/sources/webp2_filesystem.c delete mode 100644 libc-bottom-half/sources/webp2_sleep.c delete mode 100644 libc-bottom-half/sources/webp2_socket.c diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index de6b1ff37..9986c4387 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -64,10 +64,19 @@ endforeach() # building bottom-half sources # -# All targets set(bottom_half_sources + cloudlibc/src/libc/dirent/closedir.c + cloudlibc/src/libc/dirent/dirfd.c + cloudlibc/src/libc/dirent/fdclosedir.c + cloudlibc/src/libc/dirent/fdopendir.c + cloudlibc/src/libc/dirent/readdir.c + cloudlibc/src/libc/dirent/rewinddir.c + cloudlibc/src/libc/dirent/seekdir.c + cloudlibc/src/libc/dirent/telldir.c cloudlibc/src/libc/errno/errno.c cloudlibc/src/libc/fcntl/fcntl.c + cloudlibc/src/libc/fcntl/posix_fadvise.c + cloudlibc/src/libc/fcntl/posix_fallocate.c cloudlibc/src/libc/poll/poll.c cloudlibc/src/libc/sched/sched_yield.c cloudlibc/src/libc/stdlib/_Exit.c @@ -75,16 +84,29 @@ set(bottom_half_sources cloudlibc/src/libc/sys/select/pselect.c cloudlibc/src/libc/sys/select/select.c cloudlibc/src/libc/sys/stat/fstat.c + cloudlibc/src/libc/sys/stat/futimens.c cloudlibc/src/libc/sys/time/gettimeofday.c + cloudlibc/src/libc/sys/uio/preadv.c + cloudlibc/src/libc/sys/uio/pwritev.c cloudlibc/src/libc/sys/uio/readv.c cloudlibc/src/libc/sys/uio/writev.c cloudlibc/src/libc/time/CLOCK_MONOTONIC.c cloudlibc/src/libc/time/CLOCK_REALTIME.c cloudlibc/src/libc/time/clock_getres.c cloudlibc/src/libc/time/clock_gettime.c + cloudlibc/src/libc/time/clock_nanosleep.c + cloudlibc/src/libc/time/nanosleep.c cloudlibc/src/libc/time/time.c + cloudlibc/src/libc/unistd/fdatasync.c + cloudlibc/src/libc/unistd/fsync.c + cloudlibc/src/libc/unistd/ftruncate.c cloudlibc/src/libc/unistd/lseek.c + cloudlibc/src/libc/unistd/pread.c + cloudlibc/src/libc/unistd/pwrite.c cloudlibc/src/libc/unistd/read.c + cloudlibc/src/libc/unistd/sleep.c + cloudlibc/src/libc/unistd/unlinkat.c + cloudlibc/src/libc/unistd/usleep.c cloudlibc/src/libc/unistd/write.c sources/__errno_location.c sources/__main_void.c @@ -98,80 +120,49 @@ set(bottom_half_sources sources/__wasilibc_report_errorf.c sources/__wasilibc_tell.c sources/abort.c + sources/at_fdcwd.c + sources/chdir.c sources/complex-builtins.c sources/descriptor_table.c sources/dup.c sources/environ.c sources/errno.c sources/file_utils.c + sources/getcwd.c sources/getentropy.c sources/isatty.c sources/math/fmin-fmax.c sources/math/math-builtins.c + sources/pipe.c + sources/posix.c sources/ppoll.c + sources/preopens.c sources/reallocarray.c sources/sbrk.c + sources/truncate.c sources/utf8.c sources/__cabi_realloc_wrapper.S ) -# File descriptors +# On webp2, all of the functions that would call these files will trap. if (NOT WEB STREQUAL "p2") list(APPEND bottom_half_sources - sources/pipe.c - ) -endif() - -# Filesystem -if (NOT WEB STREQUAL "p2") - list(APPEND bottom_half_sources - cloudlibc/src/libc/dirent/closedir.c - cloudlibc/src/libc/dirent/dirfd.c - cloudlibc/src/libc/dirent/fdclosedir.c - cloudlibc/src/libc/dirent/fdopendir.c cloudlibc/src/libc/dirent/opendirat.c - cloudlibc/src/libc/dirent/readdir.c - cloudlibc/src/libc/dirent/rewinddir.c cloudlibc/src/libc/dirent/scandirat.c - cloudlibc/src/libc/dirent/seekdir.c - cloudlibc/src/libc/dirent/telldir.c cloudlibc/src/libc/fcntl/openat.c - cloudlibc/src/libc/fcntl/posix_fadvise.c - cloudlibc/src/libc/fcntl/posix_fallocate.c cloudlibc/src/libc/stdio/renameat.c cloudlibc/src/libc/sys/stat/fstatat.c - cloudlibc/src/libc/sys/stat/futimens.c cloudlibc/src/libc/sys/stat/mkdirat.c cloudlibc/src/libc/sys/stat/utimensat.c - cloudlibc/src/libc/sys/uio/preadv.c - cloudlibc/src/libc/sys/uio/pwritev.c cloudlibc/src/libc/unistd/faccessat.c - cloudlibc/src/libc/unistd/fdatasync.c - cloudlibc/src/libc/unistd/fsync.c - cloudlibc/src/libc/unistd/ftruncate.c cloudlibc/src/libc/unistd/linkat.c - cloudlibc/src/libc/unistd/pread.c - cloudlibc/src/libc/unistd/pwrite.c cloudlibc/src/libc/unistd/readlinkat.c cloudlibc/src/libc/unistd/symlinkat.c - cloudlibc/src/libc/unistd/unlinkat.c sources/__wasilibc_rmdirat.c sources/__wasilibc_unlinkat.c - sources/at_fdcwd.c - sources/chdir.c - sources/getcwd.c - sources/posix.c - sources/preopens.c - sources/truncate.c ) - if (WASI STREQUAL "p2" OR WASI STREQUAL "p3") - list(APPEND bottom_half_sources - sources/file.c - ) - endif() endif() -# Sockets if (WASI STREQUAL "p1") list(APPEND bottom_half_sources cloudlibc/src/libc/sys/socket/getsockopt.c @@ -180,32 +171,28 @@ if (WASI STREQUAL "p1") cloudlibc/src/libc/sys/socket/shutdown.c sources/accept-wasip1.c ) -elseif(WASI STREQUAL "p2" OR WASI STREQUAL "p3") +else() list(APPEND bottom_half_sources sources/accept.c sources/bind.c sources/connect.c sources/getsockpeername.c sources/listen.c - sources/netdb.c sources/recv.c sources/send.c sources/shutdown.c sources/socket.c - sources/sockets_utils.c sources/sockopt.c - sources/tcp.c - sources/udp.c ) endif() -# Sleep -if (NOT (WEB STREQUAL "p2")) +if (WASI STREQUAL "p2" OR WASI STREQUAL "p3") list(APPEND bottom_half_sources - cloudlibc/src/libc/time/clock_nanosleep.c - cloudlibc/src/libc/time/nanosleep.c - cloudlibc/src/libc/unistd/sleep.c - cloudlibc/src/libc/unistd/usleep.c + sources/file.c + sources/netdb.c + sources/sockets_utils.c + sources/tcp.c + sources/udp.c ) endif() @@ -231,22 +218,10 @@ if (WASI STREQUAL "p3") endif() endif() - # webp2-specific if (WEB STREQUAL "p2") - set_source_files_properties( - sources/webp2_file_descriptors.c - sources/webp2_filesystem.c - sources/webp2_sleep.c - sources/webp2_socket.c - PROPERTIES COMPILE_OPTIONS -Wno-unused-parameter - ) list(APPEND bottom_half_sources sources/webp2.c - sources/webp2_file_descriptors.c - sources/webp2_filesystem.c - sources/webp2_sleep.c - sources/webp2_socket.c sources/webp2_stdio.c ) endif() @@ -268,6 +243,11 @@ foreach(obj bottom-half-shared bottom-half-static) cloudlibc/src headers/private ) + if (WEB STREQUAL "p2") + # Many functions are unsupported on webp2 and trap without using their + # parameters. + target_compile_options(${obj} PRIVATE -Wno-unused-parameter) + endif() endforeach() diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c index 358573750..51b1ca987 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c @@ -5,6 +5,14 @@ #include #include +#if defined(__webp2__) +#include +#endif + int closedir(DIR *dirp) { +#if !defined(__webp2__) return close(fdclosedir(dirp)); +#else + WEBP2_UNSUPPORTED("closedir"); +#endif } diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/dirent_impl.h b/libc-bottom-half/cloudlibc/src/libc/dirent/dirent_impl.h index c125dfd08..818cb225c 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/dirent_impl.h +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/dirent_impl.h @@ -32,6 +32,8 @@ struct _DIR { bool stream_done; size_t skip; size_t offset; +#elif defined(__webp2__) + // Directories are not supported on webp2. #else # error "Unknown WASI version" #endif @@ -42,7 +44,7 @@ struct _DIR { }; static inline void dirent_close_streams(DIR *dirp) { -#if defined(__wasip1__) +#if defined(__wasip1__) || defined(__webp2__) (void) dirp; // nothing to close ... #elif defined(__wasip2__) diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/dirfd.c b/libc-bottom-half/cloudlibc/src/libc/dirent/dirfd.c index 033157a4e..77c80a53a 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/dirfd.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/dirfd.c @@ -6,6 +6,14 @@ #include "dirent_impl.h" +#if defined(__webp2__) +#include +#endif + int dirfd(DIR *dirp) { +#if !defined(__webp2__) return dirp->fd; +#else + WEBP2_UNSUPPORTED("dirfd"); +#endif } diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c index 03765388f..541941b44 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c @@ -7,7 +7,12 @@ #include "dirent_impl.h" +#if defined(__webp2__) +#include +#endif + int fdclosedir(DIR *dirp) { +#if !defined(__webp2__) int fd = dirp->fd; #ifdef __wasip1__ free(dirp->buffer); @@ -16,4 +21,7 @@ int fdclosedir(DIR *dirp) { free(dirp->dirent); free(dirp); return fd; +#else + WEBP2_UNSUPPORTED("fdclosedir"); +#endif // !webp2 } diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c index 89f61ba65..915788b14 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c @@ -14,6 +14,10 @@ #else #endif +#if defined(__webp2__) +#include +#endif + #include "dirent_impl.h" DIR *fdopendir(int fd) { @@ -85,6 +89,8 @@ DIR *fdopendir(int fd) { DIR *ret = dirp; dirp = NULL; return ret; +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("fdopendir"); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c index 277e9e313..fd2851073 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c @@ -36,6 +36,11 @@ static_assert(DT_UNKNOWN == __WASI_FILETYPE_UNKNOWN, "Value mismatch"); #include #endif +#if defined(__webp2__) +#include +#endif + +#ifndef __webp2__ // Grows a buffer to be large enough to hold a certain amount of data. static struct dirent* grow(struct dirent **buffer, size_t *buffer_size, size_t target_size) { if (*buffer_size < target_size) { @@ -52,6 +57,7 @@ static struct dirent* grow(struct dirent **buffer, size_t *buffer_size, size_t t } return *buffer; } +#endif #if defined(__wasip1__) struct dirent *readdir(DIR *dirp) { @@ -326,6 +332,10 @@ struct dirent *readdir(DIR *dirp) { } return result; } +#elif defined(__webp2__) +struct dirent *readdir(DIR *dirp) { + WEBP2_UNSUPPORTED("readdir"); +} #else # error "Unknown WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/rewinddir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/rewinddir.c index 97d6dc427..e73237d84 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/rewinddir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/rewinddir.c @@ -7,6 +7,10 @@ #include "dirent_impl.h" +#if defined(__webp2__) +#include +#endif + void rewinddir(DIR *dirp) { #if defined(__wasip1__) // Update cookie. @@ -17,6 +21,8 @@ void rewinddir(DIR *dirp) { dirent_close_streams(dirp); dirp->skip = 0; dirp->offset = 0; +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("rewinddir"); #else # error "Unknown WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/seekdir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/seekdir.c index b87d079b0..741771459 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/seekdir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/seekdir.c @@ -6,6 +6,10 @@ #include "dirent_impl.h" +#if defined(__webp2__) +#include +#endif + void seekdir(DIR *dirp, long loc) { #if defined(__wasip1__) // Update cookie. @@ -17,6 +21,8 @@ void seekdir(DIR *dirp, long loc) { dirent_close_streams(dirp); dirp->skip = loc; dirp->offset = 0; +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("seekdir"); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/telldir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/telldir.c index 66e3d9f3c..b2b109bd7 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/telldir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/telldir.c @@ -5,11 +5,17 @@ #include #include "dirent_impl.h" +#if defined(__webp2__) +#include +#endif + long telldir(DIR *dirp) { #if defined(__wasip1__) return dirp->cookie; #elif defined(__wasip2__) || defined(__wasip3__) return dirp->offset + dirp->skip; +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("telldir"); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fadvise.c b/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fadvise.c index 0e9ae05e8..99fbd43bf 100644 --- a/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fadvise.c +++ b/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fadvise.c @@ -25,6 +25,10 @@ static_assert(POSIX_FADV_WILLNEED == __WASI_ADVICE_WILLNEED, "Value mismatch"); #endif +#if defined(__webp2__) +#include +#endif + int posix_fadvise(int fd, off_t offset, off_t len, int advice) { if (offset < 0 || len < 0) return EINVAL; @@ -74,6 +78,8 @@ int posix_fadvise(int fd, off_t offset, off_t len, int advice) { return errno; } return 0; +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("posix_fadvise"); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fallocate.c b/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fallocate.c index cdef58c1d..17e95cec6 100644 --- a/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fallocate.c +++ b/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fallocate.c @@ -6,6 +6,10 @@ #include #include +#if defined(__webp2__) +#include +#endif + int posix_fallocate(int fd, off_t offset, off_t len) { if (offset < 0 || len < 0) return EINVAL; @@ -15,6 +19,8 @@ int posix_fallocate(int fd, off_t offset, off_t len) { (void) fd; // Note: this operation isn't supported in wasip{2,3} return ENOTSUP; +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("posix_fallocate"); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/stat/futimens.c b/libc-bottom-half/cloudlibc/src/libc/sys/stat/futimens.c index 07249bc4b..a35764d4a 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/stat/futimens.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/stat/futimens.c @@ -5,7 +5,10 @@ #include #include #include + +#ifndef __webp2__ #include "stat_impl.h" +#endif #ifndef __wasip1__ #include @@ -13,6 +16,10 @@ #include #endif +#if defined(__webp2__) +#include +#endif + int futimens(int fd, const struct timespec *times) { #if defined(__wasip1__) // Convert timestamps and extract NOW/OMIT flags. @@ -55,6 +62,8 @@ int futimens(int fd, const struct timespec *times) { translate_error(&error); return -1; } +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("futimens"); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/uio/preadv.c b/libc-bottom-half/cloudlibc/src/libc/sys/uio/preadv.c index c33eb3fd2..f756d36a4 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/preadv.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/preadv.c @@ -13,6 +13,10 @@ #include #endif +#if defined(__webp2__) +#include +#endif + ssize_t preadv(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { if (iovcnt < 0 || offset < 0) { errno = EINVAL; @@ -37,6 +41,8 @@ ssize_t preadv(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { iov++; } return pread(fildes, NULL, 0, offset); +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("preadv"); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/uio/pwritev.c b/libc-bottom-half/cloudlibc/src/libc/sys/uio/pwritev.c index 37042502c..3f0bc4b37 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/pwritev.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/pwritev.c @@ -13,6 +13,10 @@ #include #endif +#if defined(__webp2__) +#include +#endif + ssize_t pwritev(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { if (iovcnt < 0 || offset < 0) { errno = EINVAL; @@ -38,6 +42,8 @@ ssize_t pwritev(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { iov++; } return pwrite(fildes, NULL, 0, offset); +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("pwritev"); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c b/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c index f6e757926..5cea4c4d8 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c @@ -15,6 +15,10 @@ static_assert(TIMER_ABSTIME == __WASI_SUBCLOCKFLAGS_SUBSCRIPTION_CLOCK_ABSTIME, "Value mismatch"); #endif +#if defined(__webp2__) +#include +#endif + int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, struct timespec *rmtp) { (void) rmtp; @@ -66,6 +70,8 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, } #endif return 0; +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("clock_nanosleep"); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c b/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c index 31ba4faa9..3f42ee24b 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c @@ -6,7 +6,12 @@ #include #include +#if defined(__webp2__) +#include +#endif + int nanosleep(const struct timespec *rqtp, struct timespec *rem) { +#if !defined(__webp2__) #if defined(__wasip2__) || defined(__wasip3__) // FIXME(WebAssembly/WASI#857): wasip2/p3 only supports the monotonic clock for // sleeping. @@ -20,4 +25,7 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rem) { return -1; } return 0; +#else + WEBP2_UNSUPPORTED("nanosleep"); +#endif // !webp2 } diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/fdatasync.c b/libc-bottom-half/cloudlibc/src/libc/unistd/fdatasync.c index 7a2a7dfff..f02d223a2 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/fdatasync.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/fdatasync.c @@ -13,6 +13,10 @@ #include #endif +#if defined(__webp2__) +#include +#endif + int fdatasync(int fildes) { #if defined(__wasip1__) __wasi_errno_t error = __wasi_fd_datasync(fildes); @@ -34,6 +38,8 @@ int fdatasync(int fildes) { translate_error(&error_code); return -1; } +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("fdatasync"); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/fsync.c b/libc-bottom-half/cloudlibc/src/libc/unistd/fsync.c index d22dc744d..6f5047604 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/fsync.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/fsync.c @@ -12,6 +12,10 @@ #include #endif +#if defined(__webp2__) +#include +#endif + int fsync(int fildes) { #if defined(__wasip1__) __wasi_errno_t error = __wasi_fd_sync(fildes); @@ -33,6 +37,8 @@ int fsync(int fildes) { translate_error(&error_code); return -1; } +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("fsync"); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/ftruncate.c b/libc-bottom-half/cloudlibc/src/libc/unistd/ftruncate.c index 963c70415..e49d1ba49 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/ftruncate.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/ftruncate.c @@ -12,6 +12,10 @@ #include #endif +#if defined(__webp2__) +#include +#endif + int ftruncate(int fildes, off_t length) { if (length < 0) { errno = EINVAL; @@ -38,6 +42,8 @@ int ftruncate(int fildes, off_t length) { translate_error(&error_code); return -1; } +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("ftruncate"); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c b/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c index 2132b1193..df27eb385 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c @@ -17,6 +17,10 @@ #include #endif +#if defined(__webp2__) +#include +#endif + ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset) { if (offset < 0) { errno = EINVAL; @@ -107,6 +111,8 @@ ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset) { filesystem_future_result_void_error_code_drop_readable(io.f1); } return ret; +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("pread"); #else # error "Unknown WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/pwrite.c b/libc-bottom-half/cloudlibc/src/libc/unistd/pwrite.c index a20b6e963..ae7680a6f 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/pwrite.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/pwrite.c @@ -16,6 +16,10 @@ #include #endif +#if defined(__webp2__) +#include +#endif + ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) { if (offset < 0) { errno = EINVAL; @@ -101,6 +105,8 @@ ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) { return -1; } return ret; +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("pwrite"); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c b/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c index 4ab07cbb8..fdf7984ef 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c @@ -6,7 +6,12 @@ #include #include +#if defined(__webp2__) +#include +#endif + unsigned int sleep(unsigned int seconds) { +#if !defined(__webp2__) struct timespec ts = {.tv_sec = seconds, .tv_nsec = 0}; // FIXME(WebAssembly/WASI#857): wasip2/p3 only supports the monotonic clock for // sleeping. @@ -20,4 +25,7 @@ unsigned int sleep(unsigned int seconds) { if (clock_nanosleep(clock_id, 0, &ts, NULL) != 0) return seconds; return 0; +#else + WEBP2_UNSUPPORTED("sleep"); +#endif // !webp2 } diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c b/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c index 351bf9264..e8848471f 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c @@ -9,9 +9,17 @@ #include #include +#if defined(__webp2__) +#include +#endif + int unlinkat(int fd, const char *path, int flag) { +#if !defined(__webp2__) if ((flag & AT_REMOVEDIR) != 0) { return __wasilibc_rmdirat(fd, path); } return __wasilibc_unlinkat(fd, path); +#else + WEBP2_UNSUPPORTED("unlinkat"); +#endif } diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c b/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c index 6af5e80da..aa168092b 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c @@ -7,7 +7,12 @@ #include #include +#if defined(__webp2__) +#include +#endif + int usleep(useconds_t useconds) { +#if !defined(__webp2__) struct timespec ts = {.tv_sec = useconds / 1000000, .tv_nsec = useconds % 1000000 * 1000}; #if defined(__wasip1__) @@ -25,4 +30,7 @@ int usleep(useconds_t useconds) { return -1; } return 0; +#else + WEBP2_UNSUPPORTED("usleep"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/accept.c b/libc-bottom-half/sources/accept.c index ce70b4176..b152d005e 100644 --- a/libc-bottom-half/sources/accept.c +++ b/libc-bottom-half/sources/accept.c @@ -3,13 +3,22 @@ #include #include +#if defined(__webp2__) +#include +#endif + int accept(int socket, struct sockaddr *restrict addr, socklen_t *restrict addrlen) { +#if !defined(__webp2__) return accept4(socket, addr, addrlen, 0); +#else + WEBP2_UNSUPPORTED("accept"); +#endif } int accept4(int socket, struct sockaddr *restrict addr, socklen_t *restrict addrlen, int flags) { +#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -19,4 +28,7 @@ int accept4(int socket, struct sockaddr *restrict addr, return -1; } return entry.vtable->accept4(entry.data, addr, addrlen, flags); +#else + WEBP2_UNSUPPORTED("accept4"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/at_fdcwd.c b/libc-bottom-half/sources/at_fdcwd.c index 3e9bac034..257d695e4 100644 --- a/libc-bottom-half/sources/at_fdcwd.c +++ b/libc-bottom-half/sources/at_fdcwd.c @@ -12,93 +12,135 @@ #include #include #include -#include #include +#if !defined(__webp2__) +#include +#else +#include +#endif + // If the platform doesn't define O_TMPFILE, we don't need to worry about it. #ifndef O_TMPFILE #define O_TMPFILE 0 #endif int openat(int dirfd, const char *pathname, int flags, ...) { +#if !defined(__webp2__) if (dirfd == AT_FDCWD || pathname[0] == '/') { return open(pathname, flags); } return __wasilibc_nocwd_openat_nomode(dirfd, pathname, flags); +#else + WEBP2_UNSUPPORTED("openat"); +#endif } int symlinkat(const char *target, int dirfd, const char *linkpath) { +#if !defined(__webp2__) if (dirfd == AT_FDCWD || linkpath[0] == '/') { return symlink(target, linkpath); } return __wasilibc_nocwd_symlinkat(target, dirfd, linkpath); +#else + WEBP2_UNSUPPORTED("symlinkat"); +#endif } ssize_t readlinkat(int dirfd, const char *__restrict pathname, char *__restrict buf, size_t bufsiz) { +#if !defined(__webp2__) if (dirfd == AT_FDCWD || pathname[0] == '/') { return readlink(pathname, buf, bufsiz); } return __wasilibc_nocwd_readlinkat(dirfd, pathname, buf, bufsiz); +#else + WEBP2_UNSUPPORTED("readlinkat"); +#endif } int mkdirat(int dirfd, const char *pathname, mode_t mode) { +#if !defined(__webp2__) if (dirfd == AT_FDCWD || pathname[0] == '/') { return mkdir(pathname, mode); } return __wasilibc_nocwd_mkdirat_nomode(dirfd, pathname); +#else + WEBP2_UNSUPPORTED("mkdirat"); +#endif } DIR *opendirat(int dirfd, const char *path) { +#if !defined(__webp2__) if (dirfd == AT_FDCWD || path[0] == '/') { return opendir(path); } return __wasilibc_nocwd_opendirat(dirfd, path); +#else + WEBP2_UNSUPPORTED("opendirat"); +#endif } int scandirat(int dirfd, const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) { +#if !defined(__webp2__) if (dirfd == AT_FDCWD || dirp[0] == '/') { return scandir(dirp, namelist, filter, compar); } return __wasilibc_nocwd_scandirat(dirfd, dirp, namelist, filter, compar); +#else + WEBP2_UNSUPPORTED("scandirat"); +#endif } int faccessat(int dirfd, const char *pathname, int mode, int flags) { +#if !defined(__webp2__) if (dirfd == AT_FDCWD || pathname[0] == '/') { return __wasilibc_access(pathname, mode, flags); } return __wasilibc_nocwd_faccessat(dirfd, pathname, mode, flags); +#else + WEBP2_UNSUPPORTED("faccessat"); +#endif } int fstatat(int dirfd, const char *__restrict pathname, struct stat *__restrict statbuf, int flags) { +#if !defined(__webp2__) if (dirfd == AT_FDCWD || pathname[0] == '/') { return __wasilibc_stat(pathname, statbuf, flags); } return __wasilibc_nocwd_fstatat(dirfd, pathname, statbuf, flags); +#else + WEBP2_UNSUPPORTED("fstatat"); +#endif } int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) { +#if !defined(__webp2__) if (dirfd == AT_FDCWD || pathname[0] == '/') { return __wasilibc_utimens(pathname, times, flags); } return __wasilibc_nocwd_utimensat(dirfd, pathname, times, flags); +#else + WEBP2_UNSUPPORTED("utimensat"); +#endif } int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) { +#if !defined(__webp2__) if ((olddirfd == AT_FDCWD || oldpath[0] == '/') && (newdirfd == AT_FDCWD || newpath[0] == '/')) { return __wasilibc_link(oldpath, newpath, flags); @@ -111,10 +153,14 @@ int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, } return __wasilibc_nocwd_linkat(olddirfd, oldpath, newdirfd, newpath, flags); +#else + WEBP2_UNSUPPORTED("linkat"); +#endif // !webp2 } int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) { +#if !defined(__webp2__) if ((olddirfd == AT_FDCWD || oldpath[0] == '/') && (newdirfd == AT_FDCWD || newpath[0] == '/')) { return rename(oldpath, newpath); @@ -127,20 +173,31 @@ int renameat(int olddirfd, const char *oldpath, int newdirfd, } return __wasilibc_nocwd_renameat(olddirfd, oldpath, newdirfd, newpath); +#else + WEBP2_UNSUPPORTED("renameat"); +#endif // !webp2 } int __wasilibc_unlinkat(int dirfd, const char *path) { +#if !defined(__webp2__) if (dirfd == AT_FDCWD || path[0] == '/') { return unlink(path); } return __wasilibc_nocwd___wasilibc_unlinkat(dirfd, path); +#else + WEBP2_UNSUPPORTED("__wasilibc_unlinkat"); +#endif } int __wasilibc_rmdirat(int dirfd, const char *path) { +#if !defined(__webp2__) if (dirfd == AT_FDCWD || path[0] == '/') { return rmdir(path); } return __wasilibc_nocwd___wasilibc_rmdirat(dirfd, path); +#else + WEBP2_UNSUPPORTED("__wasilibc_rmdirat"); +#endif } diff --git a/libc-bottom-half/sources/bind.c b/libc-bottom-half/sources/bind.c index 66f68389d..58b7f49e4 100644 --- a/libc-bottom-half/sources/bind.c +++ b/libc-bottom-half/sources/bind.c @@ -1,9 +1,14 @@ #include #include #include -#include +#include + +#if defined(__webp2__) +#include +#endif int bind(int socket, const struct sockaddr *addr, socklen_t addrlen) { +#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -13,4 +18,7 @@ int bind(int socket, const struct sockaddr *addr, socklen_t addrlen) { return -1; } return entry.vtable->bind(entry.data, addr, addrlen); +#else + WEBP2_UNSUPPORTED("bind"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/chdir.c b/libc-bottom-half/sources/chdir.c index 44e0fd90d..e5e8ac5e6 100644 --- a/libc-bottom-half/sources/chdir.c +++ b/libc-bottom-half/sources/chdir.c @@ -7,14 +7,22 @@ #include #include #include -#include #include +#if !defined(__webp2__) +#include +#else +#include +#endif + +#ifndef __webp2__ DECLARE_WEAK_LOCK(__wasilibc_cwd_lock, extern); extern char *__wasilibc_cwd; static int __wasilibc_cwd_mallocd = 0; +#endif int chdir(const char *path) { +#if !defined(__webp2__) static char *relative_buf = NULL; static size_t relative_buf_len = 0; @@ -68,8 +76,12 @@ int chdir(const char *path) { free(prev_cwd); __wasilibc_cwd_mallocd = 1; return 0; +#else + WEBP2_UNSUPPORTED("chdir"); +#endif // !webp2 } +#ifndef __webp2__ // Computes an absolute path for `path` based on the current working directory. // // The caller must already hold `__wasilibc_cwd_lock`: this function reads the @@ -171,3 +183,4 @@ int __wasilibc_find_relpath_alloc(const char *path, const char **abs_prefix, WEAK_UNLOCK(__wasilibc_cwd_lock); return fd; } +#endif // !webp2 diff --git a/libc-bottom-half/sources/connect.c b/libc-bottom-half/sources/connect.c index f4e1dacc5..2c6990671 100644 --- a/libc-bottom-half/sources/connect.c +++ b/libc-bottom-half/sources/connect.c @@ -3,7 +3,12 @@ #include #include +#if defined(__webp2__) +#include +#endif + int connect(int fd, const struct sockaddr *addr, socklen_t addrlen) { +#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(fd, &entry) < 0) return -1; @@ -13,4 +18,7 @@ int connect(int fd, const struct sockaddr *addr, socklen_t addrlen) { return -1; } return entry.vtable->connect(entry.data, addr, addrlen); +#else + WEBP2_UNSUPPORTED("connect"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/getcwd.c b/libc-bottom-half/sources/getcwd.c index 7786bfba2..dc9a7962f 100644 --- a/libc-bottom-half/sources/getcwd.c +++ b/libc-bottom-half/sources/getcwd.c @@ -4,10 +4,17 @@ #include "lock.h" +#if defined(__webp2__) +#include +#endif + +#ifndef __webp2__ char *__wasilibc_cwd = "/"; DECLARE_WEAK_LOCK(__wasilibc_cwd_lock); +#endif char *getcwd(char *buf, size_t size) { +#if !defined(__webp2__) // Critical section contains no yield points, so we can use weak locks. WEAK_LOCK(__wasilibc_cwd_lock); if (!buf) { @@ -28,4 +35,7 @@ char *getcwd(char *buf, size_t size) { } WEAK_UNLOCK(__wasilibc_cwd_lock); return buf; +#else + WEBP2_UNSUPPORTED("getcwd"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/getsockpeername.c b/libc-bottom-half/sources/getsockpeername.c index 46ffa593c..b72b4e392 100644 --- a/libc-bottom-half/sources/getsockpeername.c +++ b/libc-bottom-half/sources/getsockpeername.c @@ -3,8 +3,13 @@ #include #include +#if defined(__webp2__) +#include +#endif + int getsockname(int socket, struct sockaddr *__restrict addr, socklen_t *__restrict addrlen) { +#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -14,10 +19,14 @@ int getsockname(int socket, struct sockaddr *__restrict addr, return -1; } return entry.vtable->getsockname(entry.data, addr, addrlen); +#else + WEBP2_UNSUPPORTED("getsockname"); +#endif // !webp2 } int getpeername(int socket, struct sockaddr *__restrict addr, socklen_t *__restrict addrlen) { +#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -27,4 +36,7 @@ int getpeername(int socket, struct sockaddr *__restrict addr, return -1; } return entry.vtable->getpeername(entry.data, addr, addrlen); +#else + WEBP2_UNSUPPORTED("getpeername"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/listen.c b/libc-bottom-half/sources/listen.c index 14c514a3d..ba12d03c8 100644 --- a/libc-bottom-half/sources/listen.c +++ b/libc-bottom-half/sources/listen.c @@ -3,7 +3,12 @@ #include #include +#if defined(__webp2__) +#include +#endif + int listen(int socket, int backlog) { +#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -21,4 +26,7 @@ int listen(int socket, int backlog) { return -1; } return entry.vtable->listen(entry.data, backlog); +#else + WEBP2_UNSUPPORTED("listen"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/pipe.c b/libc-bottom-half/sources/pipe.c index db75f948f..2e94c9750 100644 --- a/libc-bottom-half/sources/pipe.c +++ b/libc-bottom-half/sources/pipe.c @@ -2,7 +2,11 @@ #include #include -#if !defined(__wasip1__) && !defined(__wasip2__) +#if defined(__webp2__) +#include +#endif + +#if defined(__wasip3__) #include #include @@ -491,9 +495,15 @@ static descriptor_vtable_t pipe_vtable = { .poll_register = pipe_poll_register, }; -#endif // !__wasip1__ && !__wasip2__ +#endif // __wasip3__ -int pipe(int fd[2]) { return pipe2(fd, 0); } +int pipe(int fd[2]) { +#if !defined(__webp2__) + return pipe2(fd, 0); +#else + WEBP2_UNSUPPORTED("pipe"); +#endif +} int pipe2(int fd[2], int flags) { if ((flags & ~(O_CLOEXEC | O_NONBLOCK)) != 0) { @@ -504,6 +514,8 @@ int pipe2(int fd[2], int flags) { (void)fd; errno = ENOTSUP; return -1; +#elif defined(__webp2__) + WEBP2_UNSUPPORTED("pipe2"); #else pipe_t *reader = calloc(1, sizeof(pipe_t)); pipe_t *writer = calloc(1, sizeof(pipe_t)); diff --git a/libc-bottom-half/sources/posix.c b/libc-bottom-half/sources/posix.c index ed1cfadb9..f16b0ec99 100644 --- a/libc-bottom-half/sources/posix.c +++ b/libc-bottom-half/sources/posix.c @@ -9,10 +9,16 @@ #include #include #include +#include + +#if !defined(__webp2__) #include #include -#include +#else +#include +#endif +#ifndef __webp2__ static int find_relpath2(const char *path, char **relative, size_t *relative_len) { // See comments in `preopens.c` for what this trick is doing. @@ -44,15 +50,21 @@ static int find_relpath_alt(const char *path, char **relative) { *relative = relative_buf; return fd; } +#endif // !webp2 int open(const char *path, int oflag, ...) { +#if !defined(__webp2__) // WASI libc's `openat` ignores the mode argument, so call a special // entrypoint which avoids the varargs calling convention. return __wasilibc_open_nomode(path, oflag); +#else + WEBP2_UNSUPPORTED("open"); +#endif } // See the documentation in libc.h int __wasilibc_open_nomode(const char *path, int oflag) { +#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -63,9 +75,13 @@ int __wasilibc_open_nomode(const char *path, int oflag) { } return __wasilibc_nocwd_openat_nomode(dirfd, relative_path, oflag); +#else + WEBP2_UNSUPPORTED("__wasilibc_open_nomode"); +#endif // !webp2 } int access(const char *path, int amode) { +#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -76,10 +92,14 @@ int access(const char *path, int amode) { } return __wasilibc_nocwd_faccessat(dirfd, relative_path, amode, 0); +#else + WEBP2_UNSUPPORTED("access"); +#endif // !webp2 } ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize) { +#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -90,9 +110,13 @@ ssize_t readlink(const char *restrict path, char *restrict buf, } return __wasilibc_nocwd_readlinkat(dirfd, relative_path, buf, bufsize); +#else + WEBP2_UNSUPPORTED("readlink"); +#endif // !webp2 } int stat(const char *restrict path, struct stat *restrict buf) { +#if !defined(__webp2__) if (path[0] == '\0') { errno = ENOENT; return -1; @@ -108,9 +132,13 @@ int stat(const char *restrict path, struct stat *restrict buf) { } return __wasilibc_nocwd_fstatat(dirfd, relative_path, buf, 0); +#else + WEBP2_UNSUPPORTED("stat"); +#endif // !webp2 } int lstat(const char *restrict path, struct stat *restrict buf) { +#if !defined(__webp2__) if (path[0] == '\0') { errno = ENOENT; return -1; @@ -127,9 +155,13 @@ int lstat(const char *restrict path, struct stat *restrict buf) { return __wasilibc_nocwd_fstatat(dirfd, relative_path, buf, AT_SYMLINK_NOFOLLOW); +#else + WEBP2_UNSUPPORTED("lstat"); +#endif // !webp2 } int utime(const char *path, const struct utimbuf *times) { +#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -145,9 +177,13 @@ int utime(const char *path, const struct utimbuf *times) { {.tv_sec = times->modtime}}) : NULL, 0); +#else + WEBP2_UNSUPPORTED("utime"); +#endif // !webp2 } int utimes(const char *path, const struct timeval times[2]) { +#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -166,9 +202,13 @@ int utimes(const char *path, const struct timeval times[2]) { }) : NULL, 0); +#else + WEBP2_UNSUPPORTED("utimes"); +#endif // !webp2 } int unlink(const char *path) { +#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -182,9 +222,13 @@ int unlink(const char *path) { // `AT_REMOVEDIR` isn't passed. Instead, use a specialized function which // just imports `__wasi_path_unlink_file`. return __wasilibc_nocwd___wasilibc_unlinkat(dirfd, relative_path); +#else + WEBP2_UNSUPPORTED("unlink"); +#endif // !webp2 } int rmdir(const char *path) { +#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -195,9 +239,13 @@ int rmdir(const char *path) { } return __wasilibc_nocwd___wasilibc_rmdirat(dirfd, relative_path); +#else + WEBP2_UNSUPPORTED("rmdir"); +#endif // !webp2 } int remove(const char *path) { +#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -219,9 +267,13 @@ int remove(const char *path) { errno = ENOENT; } return r; +#else + WEBP2_UNSUPPORTED("remove"); +#endif // !webp2 } int mkdir(const char *path, mode_t mode) { +#if !defined(__webp2__) (void)mode; char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -233,9 +285,13 @@ int mkdir(const char *path, mode_t mode) { } return __wasilibc_nocwd_mkdirat_nomode(dirfd, relative_path); +#else + WEBP2_UNSUPPORTED("mkdir"); +#endif // !webp2 } DIR *opendir(const char *dirname) { +#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(dirname, &relative_path); @@ -246,11 +302,15 @@ DIR *opendir(const char *dirname) { } return __wasilibc_nocwd_opendirat(dirfd, relative_path); +#else + WEBP2_UNSUPPORTED("opendir"); +#endif // !webp2 } int scandir(const char *restrict dir, struct dirent ***restrict namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) { +#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(dir, &relative_path); @@ -262,9 +322,13 @@ int scandir(const char *restrict dir, struct dirent ***restrict namelist, return __wasilibc_nocwd_scandirat(dirfd, relative_path, namelist, filter, compar); +#else + WEBP2_UNSUPPORTED("scandir"); +#endif // !webp2 } int symlink(const char *target, const char *linkpath) { +#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(linkpath, &relative_path); @@ -275,9 +339,13 @@ int symlink(const char *target, const char *linkpath) { } return __wasilibc_nocwd_symlinkat(target, dirfd, relative_path); +#else + WEBP2_UNSUPPORTED("symlink"); +#endif // !webp2 } int link(const char *old, const char *new) { +#if !defined(__webp2__) char *old_relative_path; int old_dirfd = find_relpath_alt(old, &old_relative_path); @@ -293,9 +361,13 @@ int link(const char *old, const char *new) { // We couldn't find a preopen for it; fail as if we can't find the path. errno = ENOENT; return -1; +#else + WEBP2_UNSUPPORTED("link"); +#endif // !webp2 } int rename(const char *old, const char *new) { +#if !defined(__webp2__) char *old_relative_path; int old_dirfd = find_relpath_alt(old, &old_relative_path); @@ -311,9 +383,13 @@ int rename(const char *old, const char *new) { // We couldn't find a preopen for it; fail as if we can't find the path. errno = ENOENT; return -1; +#else + WEBP2_UNSUPPORTED("rename"); +#endif // !webp2 } int chmod(const char *path, mode_t mode) { +#if !defined(__webp2__) (void)path; (void)mode; // TODO: We plan to support this eventually in WASI, but not yet. @@ -321,9 +397,13 @@ int chmod(const char *path, mode_t mode) { // implementation will build unmodified. errno = ENOSYS; return -1; +#else + WEBP2_UNSUPPORTED("chmod"); +#endif // !webp2 } int fchmod(int fd, mode_t mode) { +#if !defined(__webp2__) (void)fd; (void)mode; // TODO: We plan to support this eventually in WASI, but not yet. @@ -331,9 +411,13 @@ int fchmod(int fd, mode_t mode) { // implementation will build unmodified. errno = ENOSYS; return -1; +#else + WEBP2_UNSUPPORTED("fchmod"); +#endif // !webp2 } int fchmodat(int fd, const char *path, mode_t mode, int flag) { +#if !defined(__webp2__) (void)fd; (void)path; (void)mode; @@ -343,9 +427,13 @@ int fchmodat(int fd, const char *path, mode_t mode, int flag) { // implementation will build unmodified. errno = ENOSYS; return -1; +#else + WEBP2_UNSUPPORTED("fchmodat"); +#endif // !webp2 } int statvfs(const char *__restrict path, struct statvfs *__restrict buf) { +#if !defined(__webp2__) (void)path; (void)buf; // TODO: We plan to support this eventually in WASI, but not yet. @@ -353,9 +441,13 @@ int statvfs(const char *__restrict path, struct statvfs *__restrict buf) { // implementation will build unmodified. errno = ENOSYS; return -1; +#else + WEBP2_UNSUPPORTED("statvfs"); +#endif // !webp2 } int fstatvfs(int fd, struct statvfs *buf) { +#if !defined(__webp2__) (void)fd; (void)buf; // TODO: We plan to support this eventually in WASI, but not yet. @@ -363,10 +455,14 @@ int fstatvfs(int fd, struct statvfs *buf) { // implementation will build unmodified. errno = ENOSYS; return -1; +#else + WEBP2_UNSUPPORTED("fstatvfs"); +#endif // !webp2 } // Like `access`, but with `faccessat`'s flags argument. int __wasilibc_access(const char *path, int mode, int flags) { +#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -377,11 +473,15 @@ int __wasilibc_access(const char *path, int mode, int flags) { } return __wasilibc_nocwd_faccessat(dirfd, relative_path, mode, flags); +#else + WEBP2_UNSUPPORTED("__wasilibc_access"); +#endif // !webp2 } // Like `utimensat`, but without the `at` part. int __wasilibc_utimens(const char *path, const struct timespec times[2], int flags) { +#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -392,11 +492,15 @@ int __wasilibc_utimens(const char *path, const struct timespec times[2], } return __wasilibc_nocwd_utimensat(dirfd, relative_path, times, flags); +#else + WEBP2_UNSUPPORTED("__wasilibc_utimens"); +#endif // !webp2 } // Like `stat`, but with `fstatat`'s flags argument. int __wasilibc_stat(const char *__restrict path, struct stat *__restrict st, int flags) { +#if !defined(__webp2__) if (path[0] == '\0') { errno = ENOENT; return -1; @@ -412,10 +516,14 @@ int __wasilibc_stat(const char *__restrict path, struct stat *__restrict st, } return __wasilibc_nocwd_fstatat(dirfd, relative_path, st, flags); +#else + WEBP2_UNSUPPORTED("__wasilibc_stat"); +#endif // !webp2 } // Like `link`, but with `linkat`'s flags argument. int __wasilibc_link(const char *oldpath, const char *newpath, int flags) { +#if !defined(__webp2__) char *old_relative_path; char *new_relative_path; int old_dirfd = find_relpath(oldpath, &old_relative_path); @@ -429,11 +537,15 @@ int __wasilibc_link(const char *oldpath, const char *newpath, int flags) { return __wasilibc_nocwd_linkat(old_dirfd, old_relative_path, new_dirfd, new_relative_path, flags); +#else + WEBP2_UNSUPPORTED("__wasilibc_link"); +#endif // !webp2 } // Like `__wasilibc_link`, but oldpath is relative to olddirfd. int __wasilibc_link_oldat(int olddirfd, const char *oldpath, const char *newpath, int flags) { +#if !defined(__webp2__) char *new_relative_path; int new_dirfd = find_relpath(newpath, &new_relative_path); @@ -445,11 +557,15 @@ int __wasilibc_link_oldat(int olddirfd, const char *oldpath, return __wasilibc_nocwd_linkat(olddirfd, oldpath, new_dirfd, new_relative_path, flags); +#else + WEBP2_UNSUPPORTED("__wasilibc_link_oldat"); +#endif // !webp2 } // Like `__wasilibc_link`, but newpath is relative to newdirfd. int __wasilibc_link_newat(const char *oldpath, int newdirfd, const char *newpath, int flags) { +#if !defined(__webp2__) char *old_relative_path; int old_dirfd = find_relpath(oldpath, &old_relative_path); @@ -461,10 +577,14 @@ int __wasilibc_link_newat(const char *oldpath, int newdirfd, return __wasilibc_nocwd_linkat(old_dirfd, old_relative_path, newdirfd, newpath, flags); +#else + WEBP2_UNSUPPORTED("__wasilibc_link_newat"); +#endif // !webp2 } // Like `rename`, but from is relative to fromdirfd. int __wasilibc_rename_oldat(int fromdirfd, const char *from, const char *to) { +#if !defined(__webp2__) char *to_relative_path; int to_dirfd = find_relpath(to, &to_relative_path); @@ -475,10 +595,14 @@ int __wasilibc_rename_oldat(int fromdirfd, const char *from, const char *to) { } return __wasilibc_nocwd_renameat(fromdirfd, from, to_dirfd, to_relative_path); +#else + WEBP2_UNSUPPORTED("__wasilibc_rename_oldat"); +#endif // !webp2 } // Like `rename`, but to is relative to todirfd. int __wasilibc_rename_newat(const char *from, int todirfd, const char *to) { +#if !defined(__webp2__) char *from_relative_path; int from_dirfd = find_relpath(from, &from_relative_path); @@ -489,4 +613,7 @@ int __wasilibc_rename_newat(const char *from, int todirfd, const char *to) { } return __wasilibc_nocwd_renameat(from_dirfd, from_relative_path, todirfd, to); +#else + WEBP2_UNSUPPORTED("__wasilibc_rename_newat"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/preopens.c b/libc-bottom-half/sources/preopens.c index b7301bd0b..91268182c 100644 --- a/libc-bottom-half/sources/preopens.c +++ b/libc-bottom-half/sources/preopens.c @@ -12,15 +12,21 @@ #include #include #include -#include #include -#ifndef __wasip1__ +#if defined(__wasip2__) || defined(__wasip3__) #include #include #include #endif +#if !defined(__webp2__) +#include +#else +#include +#endif + +#ifndef __webp2__ /// Access to the the above preopen must be protected in the presence of /// threads. DECLARE_STRONG_LOCK(lock, static); @@ -305,8 +311,10 @@ int __wasilibc_find_abspath(const char *path, const char **abs_prefix, *relative_path = computed; return fd; } +#endif // !webp2 void __wasilibc_populate_preopens(void) { +#if !defined(__webp2__) // Fast path: If the preopens are already initialized, do nothing. if (preopens_populated) { return; @@ -396,9 +404,13 @@ void __wasilibc_populate_preopens(void) { #endif software: _Exit(EX_SOFTWARE); +#else + WEBP2_UNSUPPORTED("__wasilibc_populate_preopens"); +#endif // !webp2 } void __wasilibc_reset_preopens(void) { +#if !defined(__webp2__) STRONG_LOCK(lock); if (num_preopens) { @@ -421,4 +433,7 @@ void __wasilibc_reset_preopens(void) { #endif STRONG_UNLOCK(lock); +#else + WEBP2_UNSUPPORTED("__wasilibc_reset_preopens"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/recv.c b/libc-bottom-half/sources/recv.c index 96951f9a5..f084503b6 100644 --- a/libc-bottom-half/sources/recv.c +++ b/libc-bottom-half/sources/recv.c @@ -3,13 +3,22 @@ #include #include +#if defined(__webp2__) +#include +#endif + ssize_t recv(int socket, void *restrict buffer, size_t length, int flags) { +#if !defined(__webp2__) return recvfrom(socket, buffer, length, flags, NULL, NULL); +#else + WEBP2_UNSUPPORTED("recv"); +#endif } ssize_t recvfrom(int socket, void *__restrict buffer, size_t length, int flags, struct sockaddr *__restrict addr, socklen_t *__restrict addrlen) { +#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -25,4 +34,7 @@ ssize_t recvfrom(int socket, void *__restrict buffer, size_t length, int flags, } return entry.vtable->recvfrom(entry.data, buffer, length, flags, addr, addrlen); +#else + WEBP2_UNSUPPORTED("recvfrom"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/send.c b/libc-bottom-half/sources/send.c index 1f309006c..be2206537 100644 --- a/libc-bottom-half/sources/send.c +++ b/libc-bottom-half/sources/send.c @@ -3,12 +3,21 @@ #include #include +#if defined(__webp2__) +#include +#endif + ssize_t send(int socket, const void *buffer, size_t length, int flags) { +#if !defined(__webp2__) return sendto(socket, buffer, length, flags, NULL, 0); +#else + WEBP2_UNSUPPORTED("send"); +#endif } ssize_t sendto(int socket, const void *buffer, size_t length, int flags, const struct sockaddr *addr, socklen_t addrlen) { +#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -24,4 +33,7 @@ ssize_t sendto(int socket, const void *buffer, size_t length, int flags, return -1; } return entry.vtable->sendto(entry.data, buffer, length, flags, addr, addrlen); +#else + WEBP2_UNSUPPORTED("sendto"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/shutdown.c b/libc-bottom-half/sources/shutdown.c index 233cb7938..5dc3eb32b 100644 --- a/libc-bottom-half/sources/shutdown.c +++ b/libc-bottom-half/sources/shutdown.c @@ -2,7 +2,12 @@ #include #include +#if defined(__webp2__) +#include +#endif + int shutdown(int socket, int how) { +#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -12,4 +17,7 @@ int shutdown(int socket, int how) { return -1; } return entry.vtable->shutdown(entry.data, how); +#else + WEBP2_UNSUPPORTED("shutdown"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/socket.c b/libc-bottom-half/sources/socket.c index 26990196a..072ce44f3 100644 --- a/libc-bottom-half/sources/socket.c +++ b/libc-bottom-half/sources/socket.c @@ -1,9 +1,14 @@ #include #include #include + +#if !defined(__webp2__) #include #include #include +#else +#include +#endif #ifdef __wasip2__ #define sockets_static_tcp_socket_create tcp_create_socket_create_tcp_socket @@ -12,6 +17,7 @@ #define SOCKETS_IP_ADDRESS_FAMILY_IPV6 NETWORK_IP_ADDRESS_FAMILY_IPV6 #endif +#ifndef __webp2__ static int tcp_socket(sockets_ip_address_family_t family, bool blocking) { sockets_error_code_t error; sockets_own_tcp_socket_t socket; @@ -29,8 +35,10 @@ static int udp_socket(sockets_ip_address_family_t family, bool blocking) { return __wasilibc_add_udp_socket(socket, family, blocking); } +#endif // !webp2 int socket(int domain, int type, int protocol) { +#if !defined(__webp2__) sockets_ip_address_family_t family; switch (domain) { case PF_INET: @@ -61,4 +69,7 @@ int socket(int domain, int type, int protocol) { errno = EPROTONOSUPPORT; return -1; } +#else + WEBP2_UNSUPPORTED("socket"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/sockopt.c b/libc-bottom-half/sources/sockopt.c index 82f3992d2..df7816971 100644 --- a/libc-bottom-half/sources/sockopt.c +++ b/libc-bottom-half/sources/sockopt.c @@ -3,8 +3,13 @@ #include #include +#if defined(__webp2__) +#include +#endif + int getsockopt(int sockfd, int level, int optname, void *restrict optval, socklen_t *restrict optlen) { +#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(sockfd, &entry) < 0) return -1; @@ -23,10 +28,14 @@ int getsockopt(int sockfd, int level, int optname, void *restrict optval, } return entry.vtable->getsockopt(entry.data, level, optname, optval, optlen); +#else + WEBP2_UNSUPPORTED("getsockopt"); +#endif // !webp2 } int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen) { +#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(sockfd, &entry) < 0) return -1; @@ -45,4 +54,7 @@ int setsockopt(int sockfd, int level, int optname, const void *optval, } return entry.vtable->setsockopt(entry.data, level, optname, optval, optlen); +#else + WEBP2_UNSUPPORTED("setsockopt"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/truncate.c b/libc-bottom-half/sources/truncate.c index 4fb61bedf..70cc860ed 100644 --- a/libc-bottom-half/sources/truncate.c +++ b/libc-bottom-half/sources/truncate.c @@ -3,7 +3,12 @@ #include #include +#if defined(__webp2__) +#include +#endif + int truncate(const char *path, off_t length) { +#if !defined(__webp2__) int fd = __wasilibc_open_nomode(path, O_WRONLY | O_CLOEXEC | O_NOCTTY); if (fd < 0) return -1; @@ -17,4 +22,7 @@ int truncate(const char *path, off_t length) { } return close(fd); +#else + WEBP2_UNSUPPORTED("truncate"); +#endif // !webp2 } diff --git a/libc-bottom-half/sources/webp2_file_descriptors.c b/libc-bottom-half/sources/webp2_file_descriptors.c deleted file mode 100644 index 259ae841a..000000000 --- a/libc-bottom-half/sources/webp2_file_descriptors.c +++ /dev/null @@ -1,8 +0,0 @@ -// There is no way to create new file descriptors on the web. - -#include "wasi/report-error.h" -#include - -int pipe(int fd[2]) { WEBP2_UNSUPPORTED("pipe"); } - -int pipe2(int fd[2], int flags) { WEBP2_UNSUPPORTED("pipe2"); } diff --git a/libc-bottom-half/sources/webp2_filesystem.c b/libc-bottom-half/sources/webp2_filesystem.c deleted file mode 100644 index c3c68b021..000000000 --- a/libc-bottom-half/sources/webp2_filesystem.c +++ /dev/null @@ -1,242 +0,0 @@ -// There is no filesystem. - -#include "__struct_stat.h" -#include "dirent.h" -#include "wasi/report-error.h" -#include <__typedef_DIR.h> -#include -#include -#include -#include -#include -#include -#include -#include - -int __wasilibc_access(const char *pathname, int mode, int flags) { - WEBP2_UNSUPPORTED("__wasilibc_access"); -} - -int __wasilibc_link(const char *oldpath, const char *newpath, int flags) { - WEBP2_UNSUPPORTED("__wasilibc_link"); -} - -int __wasilibc_link_newat(const char *oldpath, int newdirfd, - const char *newpath, int flags) { - WEBP2_UNSUPPORTED("__wasilibc_link_newat"); -} - -int __wasilibc_link_oldat(int olddirfd, const char *oldpath, - const char *newpath, int flags) { - WEBP2_UNSUPPORTED("__wasilibc_link_oldat"); -} - -int __wasilibc_open_nomode(const char *path, int oflag) { - WEBP2_UNSUPPORTED("__wasilibc_open_nomode"); -} - -void __wasilibc_populate_preopens(void) { - WEBP2_UNSUPPORTED("__wasilibc_populate_preopens"); -} - -int __wasilibc_rename_newat(const char *oldpath, int newdirfd, - const char *newpath) { - WEBP2_UNSUPPORTED("__wasilibc_rename_newat"); -} - -int __wasilibc_rename_oldat(int olddirfd, const char *oldpath, - const char *newpath) { - WEBP2_UNSUPPORTED("__wasilibc_rename_oldat"); -} - -void __wasilibc_reset_preopens(void) { - WEBP2_UNSUPPORTED("__wasilibc_reset_preopens"); -} - -int __wasilibc_rmdirat(int fd, const char *path) { - WEBP2_UNSUPPORTED("__wasilibc_rmdirat"); -} - -int __wasilibc_stat(const char *restrict pathname, - struct stat *restrict statbuf, int flags) { - WEBP2_UNSUPPORTED("__wasilibc_stat"); -} - -int __wasilibc_unlinkat(int fd, const char *path) { - WEBP2_UNSUPPORTED("__wasilibc_unlinkat"); -} - -int __wasilibc_utimens(const char *pathname, const struct timespec times[2], - int flags) { - WEBP2_UNSUPPORTED("__wasilibc_utimens"); -} - -int access(const char *path, int amode) { WEBP2_UNSUPPORTED("access"); } - -int chdir(const char *path) { WEBP2_UNSUPPORTED("chdir"); } - -int chmod(const char *path, mode_t mode) { WEBP2_UNSUPPORTED("chmod"); } - -int closedir(DIR *dirp) { WEBP2_UNSUPPORTED("closedir"); } - -int dirfd(DIR *dirp) { WEBP2_UNSUPPORTED("dirfd"); } - -int faccessat(int fd, const char *path, int amode, int flag) { - WEBP2_UNSUPPORTED("faccessat"); -} - -int fchmod(int fildes, mode_t mode) { WEBP2_UNSUPPORTED("fchmod"); } - -int fchmodat(int fd, const char *path, mode_t mode, int flag) { - WEBP2_UNSUPPORTED("fchmodat"); -} - -int fdatasync(int fildes) { WEBP2_UNSUPPORTED("fdatasync"); } - -int fdclosedir(DIR *dirp) { WEBP2_UNSUPPORTED("fdclosedir"); } - -DIR *fdopendir(int fd) { WEBP2_UNSUPPORTED("fdopendir"); } - -int fstatat(int fd, const char *restrict path, struct stat *restrict buf, - int flag) { - WEBP2_UNSUPPORTED("fstatat"); -} - -int fstatvfs(int fildes, struct statvfs *buf) { WEBP2_UNSUPPORTED("fstatvfs"); } - -int fsync(int fildes) { WEBP2_UNSUPPORTED("fsync"); } - -int ftruncate(int fildes, off_t length) { WEBP2_UNSUPPORTED("ftruncate"); } - -int futimens(int fd, const struct timespec times[2]) { - WEBP2_UNSUPPORTED("futimens"); -} - -char *getcwd(char *buf, size_t size) { WEBP2_UNSUPPORTED("getcwd"); } - -int link(const char *path1, const char *path2) { WEBP2_UNSUPPORTED("link"); } - -int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag) { - WEBP2_UNSUPPORTED("linkat"); -} - -int lstat(const char *restrict path, struct stat *restrict buf) { - WEBP2_UNSUPPORTED("lstat"); -} - -int mkdir(const char *path, mode_t mode) { WEBP2_UNSUPPORTED("mkdir"); } - -int mkdirat(int fd, const char *path, mode_t mode) { - WEBP2_UNSUPPORTED("mkdirat"); -} - -int open(const char *path, int oflag, ...) { WEBP2_UNSUPPORTED("open"); } - -int openat(int fd, const char *path, int oflag, ...) { - WEBP2_UNSUPPORTED("openat"); -} - -DIR *opendir(const char *dirname) { WEBP2_UNSUPPORTED("opendir"); } - -DIR *opendirat(int dir, const char *dirname) { WEBP2_UNSUPPORTED("opendirat"); } - -int posix_fadvise(int fd, off_t offset, off_t len, int advice) { - WEBP2_UNSUPPORTED("posix_fadvise"); -} - -int posix_fallocate(int fd, off_t offset, off_t len) { - WEBP2_UNSUPPORTED("posix_fallocate"); -} - -ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset) { - WEBP2_UNSUPPORTED("pread"); -} - -ssize_t preadv(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { - WEBP2_UNSUPPORTED("preadv"); -} - -ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) { - WEBP2_UNSUPPORTED("pwrite"); -} - -ssize_t pwritev(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { - WEBP2_UNSUPPORTED("pwritev"); -} - -struct dirent *readdir(DIR *dirp) { WEBP2_UNSUPPORTED("readdir"); } - -ssize_t readlink(const char *restrict path, char *restrict buf, - size_t bufsize) { - WEBP2_UNSUPPORTED("readlink"); -} - -ssize_t readlinkat(int fd, const char *restrict path, char *restrict buf, - size_t bufsize) { - WEBP2_UNSUPPORTED("readlinkat"); -} - -int remove(const char *path) { WEBP2_UNSUPPORTED("remove"); } - -int rename(const char *old, const char *new) { WEBP2_UNSUPPORTED("rename"); } - -int renameat(int oldfd, const char *old, int newfd, const char *new) { - WEBP2_UNSUPPORTED("renameat"); -} - -void rewinddir(DIR *dirp) { WEBP2_UNSUPPORTED("rewinddir"); } - -int rmdir(const char *path) { WEBP2_UNSUPPORTED("rmdir"); } - -int scandir(const char *dir, struct dirent ***namelist, - int (*sel)(const struct dirent *), - int (*compar)(const struct dirent **, const struct dirent **)) { - WEBP2_UNSUPPORTED("scandir"); -} - -int scandirat(int dirfd, const char *dir, struct dirent ***namelist, - int (*sel)(const struct dirent *), - int (*compar)(const struct dirent **, const struct dirent **)) { - WEBP2_UNSUPPORTED("scandirat"); -} - -void seekdir(DIR *dirp, long loc) { WEBP2_UNSUPPORTED("seekdir"); } - -int stat(const char *restrict path, struct stat *restrict buf) { - WEBP2_UNSUPPORTED("stat"); -} - -int statvfs(const char *restrict path, struct statvfs *restrict buf) { - WEBP2_UNSUPPORTED("statvfs"); -} - -int symlink(const char *path1, const char *path2) { - WEBP2_UNSUPPORTED("symlink"); -} - -int symlinkat(const char *path1, int fd, const char *path2) { - WEBP2_UNSUPPORTED("symlinkat"); -} - -long telldir(DIR *dirp) { WEBP2_UNSUPPORTED("telldir"); } - -int truncate(const char *path, off_t length) { WEBP2_UNSUPPORTED("truncate"); } - -int unlink(const char *path) { WEBP2_UNSUPPORTED("unlink"); } - -int unlinkat(int fd, const char *path, int flag) { - WEBP2_UNSUPPORTED("unlinkat"); -} - -int utime(const char *path, const struct utimbuf *times) { - WEBP2_UNSUPPORTED("utime"); -} - -int utimensat(int dirfd, const char *pathname, const struct timespec times[2], - int flags) { - WEBP2_UNSUPPORTED("utimensat"); -} - -int utimes(const char *path, const struct timeval times[2]) { - WEBP2_UNSUPPORTED("utimes"); -} diff --git a/libc-bottom-half/sources/webp2_sleep.c b/libc-bottom-half/sources/webp2_sleep.c deleted file mode 100644 index 706705a38..000000000 --- a/libc-bottom-half/sources/webp2_sleep.c +++ /dev/null @@ -1,21 +0,0 @@ -// Sleeping is not a good fit for the web (until we can figure out how to -// integrate JSPI, or something.) - -#include "wasi/report-error.h" -#include -#include - -int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, - struct timespec *rmtp) { - WEBP2_UNSUPPORTED("clock_nanosleep"); -} - -weak_alias(clock_nanosleep, __clock_nanosleep); - -int nanosleep(const struct timespec *rqtp, struct timespec *rem) { - WEBP2_UNSUPPORTED("nanosleep"); -} - -unsigned int sleep(unsigned int seconds) { WEBP2_UNSUPPORTED("sleep"); } - -int usleep(useconds_t useconds) { WEBP2_UNSUPPORTED("usleep"); } diff --git a/libc-bottom-half/sources/webp2_socket.c b/libc-bottom-half/sources/webp2_socket.c deleted file mode 100644 index d7ba48195..000000000 --- a/libc-bottom-half/sources/webp2_socket.c +++ /dev/null @@ -1,67 +0,0 @@ -// There are no sockets. - -#include "wasi/report-error.h" -#include - -int accept(int socket, struct sockaddr *restrict address, - socklen_t *restrict address_len) { - WEBP2_UNSUPPORTED("accept"); -} - -int accept4(int socket, struct sockaddr *restrict address, - socklen_t *restrict address_len, int flags) { - WEBP2_UNSUPPORTED("accept4"); -} - -int bind(int socket, const struct sockaddr *address, socklen_t address_len) { - WEBP2_UNSUPPORTED("bind"); -} - -int connect(int socket, const struct sockaddr *address, socklen_t address_len) { - WEBP2_UNSUPPORTED("connect"); -} - -int getpeername(int socket, struct sockaddr *restrict address, - socklen_t *restrict address_len) { - WEBP2_UNSUPPORTED("getpeername"); -} - -int getsockname(int socket, struct sockaddr *restrict address, - socklen_t *restrict address_len) { - WEBP2_UNSUPPORTED("getsockname"); -} - -int getsockopt(int socket, int level, int option_name, - void *restrict option_value, socklen_t *restrict option_len) { - WEBP2_UNSUPPORTED("getsockopt"); -} - -int listen(int socket, int backlog) { WEBP2_UNSUPPORTED("listen"); } - -ssize_t recv(int socket, void *buffer, size_t length, int flags) { - WEBP2_UNSUPPORTED("recv"); -} - -ssize_t recvfrom(int socket, void *restrict buffer, size_t length, int flags, - struct sockaddr *restrict address, - socklen_t *restrict address_len) { - WEBP2_UNSUPPORTED("recvfrom"); -} - -ssize_t send(int socket, const void *buffer, size_t length, int flags) { - WEBP2_UNSUPPORTED("send"); -} - -ssize_t sendto(int socket, const void *message, size_t length, int flags, - const struct sockaddr *dest_addr, socklen_t dest_len) { - WEBP2_UNSUPPORTED("sendto"); -} - -int setsockopt(int socket, int level, int option_name, const void *option_value, - socklen_t option_len) { - WEBP2_UNSUPPORTED("setsockopt"); -} - -int shutdown(int socket, int how) { WEBP2_UNSUPPORTED("shutdown"); } - -int socket(int domain, int type, int protocol) { WEBP2_UNSUPPORTED("socket"); } From a8909cead7f6e2a73f2e44a24b6901d4f5211c5a Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 15 Oct 2026 15:27:32 -0500 Subject: [PATCH 71/96] Format files --- libc-bottom-half/headers/private/wasi/file_utils.h | 2 +- libc-bottom-half/sources/file_utils.c | 2 +- libc-bottom-half/sources/webp2_stdio.c | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libc-bottom-half/headers/private/wasi/file_utils.h b/libc-bottom-half/headers/private/wasi/file_utils.h index abd572ff9..d4a587e5d 100644 --- a/libc-bottom-half/headers/private/wasi/file_utils.h +++ b/libc-bottom-half/headers/private/wasi/file_utils.h @@ -10,7 +10,7 @@ // Check if the total size of all iovecs would overflow an ssize_t. // `total_size` can be NULL. hidden bool __wasilibc_iovecs_ok(const struct iovec *iov, int iovcnt, - size_t *total_size); + size_t *total_size); #if defined(__wasip2__) || defined(__wasip3__) diff --git a/libc-bottom-half/sources/file_utils.c b/libc-bottom-half/sources/file_utils.c index a6a2427f2..4aba683bb 100644 --- a/libc-bottom-half/sources/file_utils.c +++ b/libc-bottom-half/sources/file_utils.c @@ -9,7 +9,7 @@ #include bool __wasilibc_iovecs_ok(const struct iovec *iov, int iovcnt, - size_t *total_size) { + size_t *total_size) { size_t _total_size = 0; if (total_size) { *total_size = 0; diff --git a/libc-bottom-half/sources/webp2_stdio.c b/libc-bottom-half/sources/webp2_stdio.c index c3c1370a9..46125a00a 100644 --- a/libc-bottom-half/sources/webp2_stdio.c +++ b/libc-bottom-half/sources/webp2_stdio.c @@ -137,7 +137,7 @@ static descriptor_vtable_t stdin_vtable = { .fcntl_getfl = stdio_fcntl_getfl, .set_blocking = stdio_set_blocking, }; - + static descriptor_vtable_t stdouterr_vtable = { .free = stdio_free, .write = stdio_write, @@ -147,9 +147,9 @@ static descriptor_vtable_t stdouterr_vtable = { }; static int stdio_add(stdio_t *stdio, descriptor_vtable_t *vtable) { - descriptor_table_entry_t entry = (descriptor_table_entry_t) { - .data = (descriptor_refcnt_t *)stdio, - .vtable = vtable, + descriptor_table_entry_t entry = (descriptor_table_entry_t){ + .data = (descriptor_refcnt_t *)stdio, + .vtable = vtable, }; return descriptor_table_insert(entry); } @@ -163,7 +163,7 @@ int __wasilibc_init_stdio() { if (stdio_add(&__stdout, &stdouterr_vtable) < 0) { return -1; } - + __stderr.utf8 = __wasilibc_make_utf8_writer(__stderr.buf, LINE_BUF_SIZE); __stderr.warn = true; if (stdio_add(&__stderr, &stdouterr_vtable) < 0) { @@ -188,7 +188,7 @@ void __wasilibc_webp2_flush_and_close_stdio(bool also_musl) { } finish_and_flush(&__stdout); __stdout.closed = true; - + if (also_musl) { fflush(stderr); } From bfa11cb791924cfb12a50798125362aeed8014c5 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 15 Oct 2026 15:37:26 -0500 Subject: [PATCH 72/96] Fix symbols again probably --- expected/wasm32-wasip2/defined-symbols.txt | 1 + expected/wasm32-wasip3-coop/defined-symbols.txt | 1 + expected/wasm32-wasip3/defined-symbols.txt | 1 + libc-bottom-half/CMakeLists.txt | 4 ++-- 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/expected/wasm32-wasip2/defined-symbols.txt b/expected/wasm32-wasip2/defined-symbols.txt index 29670c994..5c4ad5c04 100644 --- a/expected/wasm32-wasip2/defined-symbols.txt +++ b/expected/wasm32-wasip2/defined-symbols.txt @@ -328,6 +328,7 @@ __wasilibc_getsockopt_timeout __wasilibc_iftodt __wasilibc_init_stdio __wasilibc_initialize_environ +__wasilibc_iovecs_ok __wasilibc_link __wasilibc_link_newat __wasilibc_link_oldat diff --git a/expected/wasm32-wasip3-coop/defined-symbols.txt b/expected/wasm32-wasip3-coop/defined-symbols.txt index 1fe928f0e..3253225f2 100644 --- a/expected/wasm32-wasip3-coop/defined-symbols.txt +++ b/expected/wasm32-wasip3-coop/defined-symbols.txt @@ -355,6 +355,7 @@ __wasilibc_getsockopt_timeout __wasilibc_iftodt __wasilibc_init_stdio __wasilibc_initialize_environ +__wasilibc_iovecs_ok __wasilibc_link __wasilibc_link_newat __wasilibc_link_oldat diff --git a/expected/wasm32-wasip3/defined-symbols.txt b/expected/wasm32-wasip3/defined-symbols.txt index ead42bb40..982783c0f 100644 --- a/expected/wasm32-wasip3/defined-symbols.txt +++ b/expected/wasm32-wasip3/defined-symbols.txt @@ -329,6 +329,7 @@ __wasilibc_getsockopt_timeout __wasilibc_iftodt __wasilibc_init_stdio __wasilibc_initialize_environ +__wasilibc_iovecs_ok __wasilibc_link __wasilibc_link_newat __wasilibc_link_oldat diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index 9986c4387..23873637e 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -123,11 +123,9 @@ set(bottom_half_sources sources/at_fdcwd.c sources/chdir.c sources/complex-builtins.c - sources/descriptor_table.c sources/dup.c sources/environ.c sources/errno.c - sources/file_utils.c sources/getcwd.c sources/getentropy.c sources/isatty.c @@ -176,6 +174,8 @@ else() sources/accept.c sources/bind.c sources/connect.c + sources/descriptor_table.c + sources/file_utils.c sources/getsockpeername.c sources/listen.c sources/recv.c From 64fa30f2899a7ba561a264fa63589cf0976c5fc2 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 15 Oct 2026 16:23:24 -0500 Subject: [PATCH 73/96] Move UTF-8 code closer together --- .../wasm32-wasip1-threads/defined-symbols.txt | 1 + expected/wasm32-wasip1/defined-symbols.txt | 1 + expected/wasm32-wasip2/defined-symbols.txt | 1 + .../wasm32-wasip3-coop/defined-symbols.txt | 1 + expected/wasm32-wasip3/defined-symbols.txt | 1 + expected/wasm32-webp2/defined-symbols.txt | 1 + libc-bottom-half/sources/file_utils.c | 70 +------------------ libc-bottom-half/sources/utf8.c | 65 +++++++++++++++++ libc-top-half/headers/private/wasi/utf8.h | 14 ++++ 9 files changed, 88 insertions(+), 67 deletions(-) diff --git a/expected/wasm32-wasip1-threads/defined-symbols.txt b/expected/wasm32-wasip1-threads/defined-symbols.txt index 41fe3355d..4c4bde9c9 100644 --- a/expected/wasm32-wasip1-threads/defined-symbols.txt +++ b/expected/wasm32-wasip1-threads/defined-symbols.txt @@ -409,6 +409,7 @@ __wasilibc_utf8_abort_current __wasilibc_utf8_reset __wasilibc_utf8_write __wasilibc_utimens +__wasilibc_validate_utf8 __wasm_call_dtors __wcscoll_l __wcsftime_l diff --git a/expected/wasm32-wasip1/defined-symbols.txt b/expected/wasm32-wasip1/defined-symbols.txt index b3f4a4fea..e6543cbeb 100644 --- a/expected/wasm32-wasip1/defined-symbols.txt +++ b/expected/wasm32-wasip1/defined-symbols.txt @@ -396,6 +396,7 @@ __wasilibc_utf8_abort_current __wasilibc_utf8_reset __wasilibc_utf8_write __wasilibc_utimens +__wasilibc_validate_utf8 __wasm_call_dtors __wcscoll_l __wcsftime_l diff --git a/expected/wasm32-wasip2/defined-symbols.txt b/expected/wasm32-wasip2/defined-symbols.txt index 5c4ad5c04..a258763a1 100644 --- a/expected/wasm32-wasip2/defined-symbols.txt +++ b/expected/wasm32-wasip2/defined-symbols.txt @@ -373,6 +373,7 @@ __wasilibc_utf8_abort_current __wasilibc_utf8_reset __wasilibc_utf8_write __wasilibc_utimens +__wasilibc_validate_utf8 __wasilibc_wasi_family_to_libc __wasilibc_wasi_to_sockaddr __wasilibc_write diff --git a/expected/wasm32-wasip3-coop/defined-symbols.txt b/expected/wasm32-wasip3-coop/defined-symbols.txt index 3253225f2..56e54fde6 100644 --- a/expected/wasm32-wasip3-coop/defined-symbols.txt +++ b/expected/wasm32-wasip3-coop/defined-symbols.txt @@ -407,6 +407,7 @@ __wasilibc_utf8_abort_current __wasilibc_utf8_reset __wasilibc_utf8_write __wasilibc_utimens +__wasilibc_validate_utf8 __wasilibc_waitable_block_on __wasilibc_wasi_family_to_libc __wasilibc_wasi_to_sockaddr diff --git a/expected/wasm32-wasip3/defined-symbols.txt b/expected/wasm32-wasip3/defined-symbols.txt index 982783c0f..0342f51d7 100644 --- a/expected/wasm32-wasip3/defined-symbols.txt +++ b/expected/wasm32-wasip3/defined-symbols.txt @@ -381,6 +381,7 @@ __wasilibc_utf8_abort_current __wasilibc_utf8_reset __wasilibc_utf8_write __wasilibc_utimens +__wasilibc_validate_utf8 __wasilibc_waitable_block_on __wasilibc_wasi_family_to_libc __wasilibc_wasi_to_sockaddr diff --git a/expected/wasm32-webp2/defined-symbols.txt b/expected/wasm32-webp2/defined-symbols.txt index 588e04cc3..1384d0263 100644 --- a/expected/wasm32-webp2/defined-symbols.txt +++ b/expected/wasm32-webp2/defined-symbols.txt @@ -338,6 +338,7 @@ __wasilibc_utf8_abort_current __wasilibc_utf8_reset __wasilibc_utf8_write __wasilibc_utimens +__wasilibc_validate_utf8 __wasilibc_webp2_fflush __wasilibc_webp2_flush_and_close_stdio __wasilibc_webp2_report_error diff --git a/libc-bottom-half/sources/file_utils.c b/libc-bottom-half/sources/file_utils.c index 4aba683bb..d7c68ff11 100644 --- a/libc-bottom-half/sources/file_utils.c +++ b/libc-bottom-half/sources/file_utils.c @@ -6,6 +6,7 @@ #include #include #include +#include #include bool __wasilibc_iovecs_ok(const struct iovec *iov, int iovcnt, @@ -28,73 +29,8 @@ bool __wasilibc_iovecs_ok(const struct iovec *iov, int iovcnt, } #if defined(__wasip2__) || defined(__wasip3__) -/** - * Validates that `ptr_signed` is a valid utf-8 string. - * - * Returns the byte-length of the string on success, or -1 if it's invalid - * utf-8. - */ -static int validate_utf8(const char *ptr_signed) { - const unsigned char *ptr = (const unsigned char *)ptr_signed; - size_t i = 0; - while (1) { - unsigned char c = ptr[i]; - size_t codepoint_size; - - if (c == 0x00) { - break; - } else if (c <= 0x7F) { - // fast path for ascii-like characters, just turn the loop. - i++; - continue; - } else if ((c & 0xE0) == 0xC0) { - codepoint_size = 2; - // Disallow overlong encodings, which in this case is either 0xc0 or - // 0xc1. All other patterns represent a valid encoding. - if (c <= 0xC1) - return -1; - } else if ((c & 0xF0) == 0xE0) { - codepoint_size = 3; - // Disallow overlong encodings by ensuring that if no bits are set in - // this byte then the highest payload bit in the next byte must be set. - if (c == 0xE0 && (ptr[i + 1] & 0x20) == 0x00) - return -1; - - // Disallow surrogates which start with 0xED and are followed with - // something bigger than 0xA0. - if (c == 0xED && ptr[i + 1] >= 0xA0) - return -1; - } else if ((c & 0xF8) == 0xF0) { - codepoint_size = 4; - - // Disallow overlong encodings by ensuring that if no payload bits are - // set in this byte then one of the upper two payload bits in the next - // byte are set. - if (c == 0xF0 && (ptr[i + 1] & 0x30) == 0x00) - return -1; - - // Disallow codepoints beyond U+10FFFF which start with 0xF4 and are - // followed with something bigger than 0x8F, or if the leading byte is - // larger than 0xF4. - if (c == 0xF4 && ptr[i + 1] >= 0x90) - return -1; - if (c > 0xF4) - return -1; - } else { - return -1; - } - - // Validate all continuation bytes are `0b10......` - for (size_t j = 1; j < codepoint_size; j++) - if ((ptr[i + j] & 0xC0) != 0x80) - return -1; - i += codepoint_size; - } - return i; -} - int wasi_string_from_c(const char *s, wasi_string_t *out) { - int len = validate_utf8(s); + int len = __wasilibc_validate_utf8(s); if (len < 0) { errno = EILSEQ; return -1; @@ -103,7 +39,7 @@ int wasi_string_from_c(const char *s, wasi_string_t *out) { out->len = len; return 0; } -#endif // __wasip2__ || __wasip3__ +#endif #ifdef __wasip2__ int __wasilibc_pollable_block_on(poll_borrow_pollable_t pollable, diff --git a/libc-bottom-half/sources/utf8.c b/libc-bottom-half/sources/utf8.c index a1f9d2948..99b0b06a0 100644 --- a/libc-bottom-half/sources/utf8.c +++ b/libc-bottom-half/sources/utf8.c @@ -3,6 +3,71 @@ #include #include +// ============================================================================ +// Validation + +int __wasilibc_validate_utf8(const char *ptr_signed) { + const unsigned char *ptr = (const unsigned char *)ptr_signed; + size_t i = 0; + while (1) { + unsigned char c = ptr[i]; + size_t codepoint_size; + + if (c == 0x00) { + break; + } else if (c <= 0x7F) { + // fast path for ascii-like characters, just turn the loop. + i++; + continue; + } else if ((c & 0xE0) == 0xC0) { + codepoint_size = 2; + // Disallow overlong encodings, which in this case is either 0xc0 or + // 0xc1. All other patterns represent a valid encoding. + if (c <= 0xC1) + return -1; + } else if ((c & 0xF0) == 0xE0) { + codepoint_size = 3; + // Disallow overlong encodings by ensuring that if no bits are set in + // this byte then the highest payload bit in the next byte must be set. + if (c == 0xE0 && (ptr[i + 1] & 0x20) == 0x00) + return -1; + + // Disallow surrogates which start with 0xED and are followed with + // something bigger than 0xA0. + if (c == 0xED && ptr[i + 1] >= 0xA0) + return -1; + } else if ((c & 0xF8) == 0xF0) { + codepoint_size = 4; + + // Disallow overlong encodings by ensuring that if no payload bits are + // set in this byte then one of the upper two payload bits in the next + // byte are set. + if (c == 0xF0 && (ptr[i + 1] & 0x30) == 0x00) + return -1; + + // Disallow codepoints beyond U+10FFFF which start with 0xF4 and are + // followed with something bigger than 0x8F, or if the leading byte is + // larger than 0xF4. + if (c == 0xF4 && ptr[i + 1] >= 0x90) + return -1; + if (c > 0xF4) + return -1; + } else { + return -1; + } + + // Validate all continuation bytes are `0b10......` + for (size_t j = 1; j < codepoint_size; j++) + if ((ptr[i + j] & 0xC0) != 0x80) + return -1; + i += codepoint_size; + } + return i; +} + +// ============================================================================ +// Writing + static bool utf8_replchar(wasilibc_utf8_writer_t *w) { if (w->raw_len + 3 > w->cap) { return false; diff --git a/libc-top-half/headers/private/wasi/utf8.h b/libc-top-half/headers/private/wasi/utf8.h index 8e00d6ab5..330997b16 100644 --- a/libc-top-half/headers/private/wasi/utf8.h +++ b/libc-top-half/headers/private/wasi/utf8.h @@ -2,6 +2,20 @@ #include #include +// ============================================================================ +// Validation + +/** + * Validates that `ptr_signed` is a valid utf-8 string. + * + * Returns the byte-length of the string on success, or -1 if it's invalid + * utf-8. + */ +hidden int __wasilibc_validate_utf8(const char *ptr_signed); + +// ============================================================================ +// Writing + // Writes valid UTF-8 to a buffer. The range [0, valid_len) is always valid // even when multi-byte code points are being written. // From 9b08f3134a2ba9cae25959352350a6ab6f1fb3a8 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 19 Oct 2026 12:47:36 -0600 Subject: [PATCH 74/96] Just log unsupported functions and return errors We return ENOSYS for many things, ENOENT for filepath operations since we presume you can't load files, and other errors like EACCES for operations like opening files. Additionally, a couple functions will always trigger an application exit because the alternative is to risk an infinite loop. --- CMakeLists.txt | 5 + .../cloudlibc/src/libc/dirent/closedir.c | 5 +- .../cloudlibc/src/libc/dirent/dirfd.c | 5 +- .../cloudlibc/src/libc/dirent/fdclosedir.c | 5 +- .../cloudlibc/src/libc/dirent/fdopendir.c | 4 +- .../cloudlibc/src/libc/dirent/readdir.c | 4 +- .../cloudlibc/src/libc/dirent/rewinddir.c | 4 +- .../cloudlibc/src/libc/dirent/seekdir.c | 2 +- .../cloudlibc/src/libc/dirent/telldir.c | 5 +- .../cloudlibc/src/libc/fcntl/posix_fadvise.c | 2 +- .../src/libc/fcntl/posix_fallocate.c | 3 +- .../cloudlibc/src/libc/sys/stat/futimens.c | 4 +- .../cloudlibc/src/libc/sys/uio/preadv.c | 4 +- .../cloudlibc/src/libc/sys/uio/pwritev.c | 4 +- .../cloudlibc/src/libc/time/clock_nanosleep.c | 3 +- .../cloudlibc/src/libc/time/nanosleep.c | 4 +- .../cloudlibc/src/libc/unistd/fdatasync.c | 4 +- .../cloudlibc/src/libc/unistd/fsync.c | 4 +- .../cloudlibc/src/libc/unistd/ftruncate.c | 4 +- .../cloudlibc/src/libc/unistd/pread.c | 4 +- .../cloudlibc/src/libc/unistd/pwrite.c | 4 +- .../cloudlibc/src/libc/unistd/sleep.c | 11 +- .../cloudlibc/src/libc/unistd/unlinkat.c | 4 +- .../cloudlibc/src/libc/unistd/usleep.c | 4 +- libc-bottom-half/sources/accept.c | 8 +- libc-bottom-half/sources/at_fdcwd.c | 53 ++++-- libc-bottom-half/sources/bind.c | 4 +- libc-bottom-half/sources/chdir.c | 4 +- libc-bottom-half/sources/connect.c | 4 +- libc-bottom-half/sources/getcwd.c | 4 +- libc-bottom-half/sources/listen.c | 4 +- libc-bottom-half/sources/pipe.c | 8 +- libc-bottom-half/sources/posix.c | 159 ++++++++---------- libc-bottom-half/sources/ppoll.c | 5 +- libc-bottom-half/sources/preopens.c | 4 +- libc-bottom-half/sources/recv.c | 8 +- libc-bottom-half/sources/send.c | 8 +- libc-bottom-half/sources/shutdown.c | 4 +- libc-bottom-half/sources/socket.c | 4 +- libc-bottom-half/sources/truncate.c | 4 +- .../headers/private/wasi/report-error.h | 15 +- 41 files changed, 251 insertions(+), 151 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 45c71e01d..8e3102566 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,7 @@ option(CHECK_SYMBOLS "Whether or not to check the exported symbols of libc.a" OF set(WASI_SDK_VERSION "" CACHE STRING "Version information for wasi-sdk to embed in headers") option(ENABLE_WERROR "Whether to compile with `-Werror`" OFF) option(ENABLE_COOP_THREADS "Whether to compile with cooperative threads support" OFF) +option(LOG_UNSUPPORTED_CALLS "Whether to log error messages when calling unsupported APIs" OFF) if(TARGET_TRIPLE MATCHES "-threads$") set(WASI p1) @@ -152,6 +153,10 @@ if(SIMD) -D__wasilibc_simd_string) endif() +if(LOG_UNSUPPORTED_CALLS) + add_compile_definitions(WASILIBC_LOG_UNSUPPORTED_CALLS) +endif() + if(WASI STREQUAL "p1") set(__wasip1__ ON) elseif(WASI STREQUAL "p2") diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c index 51b1ca987..690a9f302 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c @@ -6,6 +6,7 @@ #include #if defined(__webp2__) +#include #include #endif @@ -13,6 +14,8 @@ int closedir(DIR *dirp) { #if !defined(__webp2__) return close(fdclosedir(dirp)); #else - WEBP2_UNSUPPORTED("closedir"); + WASILIBC_LOG_UNSUPPORTED("closedir"); + errno = ENOSYS; + return -1; #endif } diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/dirfd.c b/libc-bottom-half/cloudlibc/src/libc/dirent/dirfd.c index 77c80a53a..e085e0b5e 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/dirfd.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/dirfd.c @@ -7,6 +7,7 @@ #include "dirent_impl.h" #if defined(__webp2__) +#include #include #endif @@ -14,6 +15,8 @@ int dirfd(DIR *dirp) { #if !defined(__webp2__) return dirp->fd; #else - WEBP2_UNSUPPORTED("dirfd"); + WASILIBC_LOG_UNSUPPORTED("dirfd"); + errno = ENOSYS; + return -1; #endif } diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c index 541941b44..8222ae05b 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c @@ -8,6 +8,7 @@ #include "dirent_impl.h" #if defined(__webp2__) +#include #include #endif @@ -22,6 +23,8 @@ int fdclosedir(DIR *dirp) { free(dirp); return fd; #else - WEBP2_UNSUPPORTED("fdclosedir"); + WASILIBC_LOG_UNSUPPORTED("fdclosedir"); + errno = ENOSYS; + return -1; #endif // !webp2 } diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c index 915788b14..3b4ec3e44 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c @@ -90,7 +90,9 @@ DIR *fdopendir(int fd) { dirp = NULL; return ret; #elif defined(__webp2__) - WEBP2_UNSUPPORTED("fdopendir"); + WASILIBC_LOG_UNSUPPORTED("fdopendir"); + errno = ENOSYS; + return NULL; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c index fd2851073..93e7de1a0 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c @@ -334,7 +334,9 @@ struct dirent *readdir(DIR *dirp) { } #elif defined(__webp2__) struct dirent *readdir(DIR *dirp) { - WEBP2_UNSUPPORTED("readdir"); + WASILIBC_LOG_UNSUPPORTED("readdir"); + errno = EBADF; // It is impossible to open a DIR anyway. + return NULL; } #else # error "Unknown WASI version" diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/rewinddir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/rewinddir.c index e73237d84..0bd0e1960 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/rewinddir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/rewinddir.c @@ -8,6 +8,7 @@ #include "dirent_impl.h" #if defined(__webp2__) +#include #include #endif @@ -22,7 +23,8 @@ void rewinddir(DIR *dirp) { dirp->skip = 0; dirp->offset = 0; #elif defined(__webp2__) - WEBP2_UNSUPPORTED("rewinddir"); + WASILIBC_LOG_UNSUPPORTED("rewinddir"); + errno = ENOSYS; #else # error "Unknown WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/seekdir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/seekdir.c index 741771459..76d5bc4df 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/seekdir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/seekdir.c @@ -22,7 +22,7 @@ void seekdir(DIR *dirp, long loc) { dirp->skip = loc; dirp->offset = 0; #elif defined(__webp2__) - WEBP2_UNSUPPORTED("seekdir"); + WASILIBC_LOG_UNSUPPORTED("seekdir"); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/telldir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/telldir.c index b2b109bd7..8079c8590 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/telldir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/telldir.c @@ -6,6 +6,7 @@ #include "dirent_impl.h" #if defined(__webp2__) +#include #include #endif @@ -15,7 +16,9 @@ long telldir(DIR *dirp) { #elif defined(__wasip2__) || defined(__wasip3__) return dirp->offset + dirp->skip; #elif defined(__webp2__) - WEBP2_UNSUPPORTED("telldir"); + WASILIBC_LOG_UNSUPPORTED("telldir"); + errno = ENOSYS; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fadvise.c b/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fadvise.c index 99fbd43bf..bcabcef21 100644 --- a/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fadvise.c +++ b/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fadvise.c @@ -79,7 +79,7 @@ int posix_fadvise(int fd, off_t offset, off_t len, int advice) { } return 0; #elif defined(__webp2__) - WEBP2_UNSUPPORTED("posix_fadvise"); + return 0; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fallocate.c b/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fallocate.c index 17e95cec6..12d198cbf 100644 --- a/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fallocate.c +++ b/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fallocate.c @@ -20,7 +20,8 @@ int posix_fallocate(int fd, off_t offset, off_t len) { // Note: this operation isn't supported in wasip{2,3} return ENOTSUP; #elif defined(__webp2__) - WEBP2_UNSUPPORTED("posix_fallocate"); + WASILIBC_LOG_UNSUPPORTED("posix_fallocate"); + return ENOSYS; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/stat/futimens.c b/libc-bottom-half/cloudlibc/src/libc/sys/stat/futimens.c index a35764d4a..dd712616e 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/stat/futimens.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/stat/futimens.c @@ -63,7 +63,9 @@ int futimens(int fd, const struct timespec *times) { return -1; } #elif defined(__webp2__) - WEBP2_UNSUPPORTED("futimens"); + WASILIBC_LOG_UNSUPPORTED("futimens"); + errno = ENOSYS; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/uio/preadv.c b/libc-bottom-half/cloudlibc/src/libc/sys/uio/preadv.c index f756d36a4..0a5f7c3d4 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/preadv.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/preadv.c @@ -42,7 +42,9 @@ ssize_t preadv(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { } return pread(fildes, NULL, 0, offset); #elif defined(__webp2__) - WEBP2_UNSUPPORTED("preadv"); + WASILIBC_LOG_UNSUPPORTED("preadv"); + errno = ENOSYS; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/uio/pwritev.c b/libc-bottom-half/cloudlibc/src/libc/sys/uio/pwritev.c index 3f0bc4b37..d95475bfb 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/pwritev.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/pwritev.c @@ -43,7 +43,9 @@ ssize_t pwritev(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { } return pwrite(fildes, NULL, 0, offset); #elif defined(__webp2__) - WEBP2_UNSUPPORTED("pwritev"); + WASILIBC_LOG_UNSUPPORTED("pwritev"); + errno = ENOSYS; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c b/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c index 5cea4c4d8..9da66faf5 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c @@ -71,7 +71,8 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, #endif return 0; #elif defined(__webp2__) - WEBP2_UNSUPPORTED("clock_nanosleep"); + WASILIBC_LOG_UNSUPPORTED("clock_nanosleep"); + return ENOTSUP; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c b/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c index 3f42ee24b..bacca9c84 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c @@ -26,6 +26,8 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rem) { } return 0; #else - WEBP2_UNSUPPORTED("nanosleep"); + WASILIBC_LOG_UNSUPPORTED("nanosleep"); + errno = ENOSYS; + return -1; #endif // !webp2 } diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/fdatasync.c b/libc-bottom-half/cloudlibc/src/libc/unistd/fdatasync.c index f02d223a2..52b6c79bd 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/fdatasync.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/fdatasync.c @@ -39,7 +39,9 @@ int fdatasync(int fildes) { return -1; } #elif defined(__webp2__) - WEBP2_UNSUPPORTED("fdatasync"); + WASILIBC_LOG_UNSUPPORTED("fdatasync"); + errno = EINVAL; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/fsync.c b/libc-bottom-half/cloudlibc/src/libc/unistd/fsync.c index 6f5047604..a9def7420 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/fsync.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/fsync.c @@ -38,7 +38,9 @@ int fsync(int fildes) { return -1; } #elif defined(__webp2__) - WEBP2_UNSUPPORTED("fsync"); + WASILIBC_LOG_UNSUPPORTED("fsync"); + errno = EINVAL; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/ftruncate.c b/libc-bottom-half/cloudlibc/src/libc/unistd/ftruncate.c index e49d1ba49..b356821b5 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/ftruncate.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/ftruncate.c @@ -43,7 +43,9 @@ int ftruncate(int fildes, off_t length) { return -1; } #elif defined(__webp2__) - WEBP2_UNSUPPORTED("ftruncate"); + WASILIBC_LOG_UNSUPPORTED("ftruncate"); + errno = ENOSYS; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c b/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c index df27eb385..1d7c5eff0 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c @@ -112,7 +112,9 @@ ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset) { } return ret; #elif defined(__webp2__) - WEBP2_UNSUPPORTED("pread"); + WASILIBC_LOG_UNSUPPORTED("pread"); + errno = ENOSYS; + return -1; #else # error "Unknown WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/pwrite.c b/libc-bottom-half/cloudlibc/src/libc/unistd/pwrite.c index ae7680a6f..f0025c445 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/pwrite.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/pwrite.c @@ -106,7 +106,9 @@ ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) { } return ret; #elif defined(__webp2__) - WEBP2_UNSUPPORTED("pwrite"); + WASILIBC_LOG_UNSUPPORTED("pwrite"); + errno = ENOSYS; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c b/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c index fdf7984ef..d1086576f 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: BSD-2-Clause +#include #include #include #include @@ -26,6 +27,14 @@ unsigned int sleep(unsigned int seconds) { return seconds; return 0; #else - WEBP2_UNSUPPORTED("sleep"); + if (seconds == 0) { + return 0; + } + + // TODO(webp2): If we actually return from this, we risk user code going into + // an infinite loop as it fruitlessly tries to sleep the night away. + // Therefore we noisily exit. + __wasilibc_report_error("sleep is not supported"); + exit(1); #endif // !webp2 } diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c b/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c index e8848471f..b400601c9 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c @@ -20,6 +20,8 @@ int unlinkat(int fd, const char *path, int flag) { } return __wasilibc_unlinkat(fd, path); #else - WEBP2_UNSUPPORTED("unlinkat"); + WASILIBC_LOG_UNSUPPORTED("unlinkat"); + errno = ENOENT; + return -1; #endif } diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c b/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c index aa168092b..9bc623bd3 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c @@ -31,6 +31,8 @@ int usleep(useconds_t useconds) { } return 0; #else - WEBP2_UNSUPPORTED("usleep"); + WASILIBC_LOG_UNSUPPORTED("usleep"); + errno = ENOSYS; + return -1; #endif // !webp2 } diff --git a/libc-bottom-half/sources/accept.c b/libc-bottom-half/sources/accept.c index b152d005e..e1619bc4a 100644 --- a/libc-bottom-half/sources/accept.c +++ b/libc-bottom-half/sources/accept.c @@ -12,7 +12,9 @@ int accept(int socket, struct sockaddr *restrict addr, #if !defined(__webp2__) return accept4(socket, addr, addrlen, 0); #else - WEBP2_UNSUPPORTED("accept"); + WASILIBC_LOG_UNSUPPORTED("accept"); + errno = ENOSYS; + return -1; #endif } @@ -29,6 +31,8 @@ int accept4(int socket, struct sockaddr *restrict addr, } return entry.vtable->accept4(entry.data, addr, addrlen, flags); #else - WEBP2_UNSUPPORTED("accept4"); + WASILIBC_LOG_UNSUPPORTED("accept4"); + errno = ENOSYS; + return -1; #endif // !webp2 } diff --git a/libc-bottom-half/sources/at_fdcwd.c b/libc-bottom-half/sources/at_fdcwd.c index 257d695e4..d244f6792 100644 --- a/libc-bottom-half/sources/at_fdcwd.c +++ b/libc-bottom-half/sources/at_fdcwd.c @@ -17,6 +17,7 @@ #if !defined(__webp2__) #include #else +#include #include #endif @@ -33,7 +34,9 @@ int openat(int dirfd, const char *pathname, int flags, ...) { return __wasilibc_nocwd_openat_nomode(dirfd, pathname, flags); #else - WEBP2_UNSUPPORTED("openat"); + WASILIBC_LOG_UNSUPPORTED("openat"); + errno = (flags & O_CREAT) ? EACCES : ENOENT; + return -1; #endif } @@ -45,7 +48,9 @@ int symlinkat(const char *target, int dirfd, const char *linkpath) { return __wasilibc_nocwd_symlinkat(target, dirfd, linkpath); #else - WEBP2_UNSUPPORTED("symlinkat"); + WASILIBC_LOG_UNSUPPORTED("symlinkat"); + errno = EACCES; + return -1; #endif } @@ -58,7 +63,9 @@ ssize_t readlinkat(int dirfd, const char *__restrict pathname, return __wasilibc_nocwd_readlinkat(dirfd, pathname, buf, bufsiz); #else - WEBP2_UNSUPPORTED("readlinkat"); + WASILIBC_LOG_UNSUPPORTED("readlinkat"); + errno = ENOENT; + return -1; #endif } @@ -70,7 +77,9 @@ int mkdirat(int dirfd, const char *pathname, mode_t mode) { return __wasilibc_nocwd_mkdirat_nomode(dirfd, pathname); #else - WEBP2_UNSUPPORTED("mkdirat"); + WASILIBC_LOG_UNSUPPORTED("mkdirat"); + errno = EACCES; + return -1; #endif } @@ -82,7 +91,9 @@ DIR *opendirat(int dirfd, const char *path) { return __wasilibc_nocwd_opendirat(dirfd, path); #else - WEBP2_UNSUPPORTED("opendirat"); + WASILIBC_LOG_UNSUPPORTED("opendirat"); + errno = ENOENT; + return NULL; #endif } @@ -96,7 +107,9 @@ int scandirat(int dirfd, const char *dirp, struct dirent ***namelist, return __wasilibc_nocwd_scandirat(dirfd, dirp, namelist, filter, compar); #else - WEBP2_UNSUPPORTED("scandirat"); + WASILIBC_LOG_UNSUPPORTED("scandirat"); + errno = ENOENT; + return -1; #endif } @@ -108,7 +121,9 @@ int faccessat(int dirfd, const char *pathname, int mode, int flags) { return __wasilibc_nocwd_faccessat(dirfd, pathname, mode, flags); #else - WEBP2_UNSUPPORTED("faccessat"); + WASILIBC_LOG_UNSUPPORTED("faccessat"); + errno = ENOENT; + return -1; #endif } @@ -121,7 +136,9 @@ int fstatat(int dirfd, const char *__restrict pathname, return __wasilibc_nocwd_fstatat(dirfd, pathname, statbuf, flags); #else - WEBP2_UNSUPPORTED("fstatat"); + WASILIBC_LOG_UNSUPPORTED("fstatat"); + errno = ENOENT; + return -1; #endif } @@ -134,7 +151,9 @@ int utimensat(int dirfd, const char *pathname, const struct timespec times[2], return __wasilibc_nocwd_utimensat(dirfd, pathname, times, flags); #else - WEBP2_UNSUPPORTED("utimensat"); + WASILIBC_LOG_UNSUPPORTED("utimensat"); + errno = ENOENT; + return -1; #endif } @@ -154,7 +173,9 @@ int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, return __wasilibc_nocwd_linkat(olddirfd, oldpath, newdirfd, newpath, flags); #else - WEBP2_UNSUPPORTED("linkat"); + WASILIBC_LOG_UNSUPPORTED("linkat"); + errno = ENOENT; + return -1; #endif // !webp2 } @@ -174,7 +195,9 @@ int renameat(int olddirfd, const char *oldpath, int newdirfd, return __wasilibc_nocwd_renameat(olddirfd, oldpath, newdirfd, newpath); #else - WEBP2_UNSUPPORTED("renameat"); + WASILIBC_LOG_UNSUPPORTED("renameat"); + errno = ENOENT; + return -1; #endif // !webp2 } @@ -186,7 +209,9 @@ int __wasilibc_unlinkat(int dirfd, const char *path) { return __wasilibc_nocwd___wasilibc_unlinkat(dirfd, path); #else - WEBP2_UNSUPPORTED("__wasilibc_unlinkat"); + WASILIBC_LOG_UNSUPPORTED("__wasilibc_unlinkat"); + errno = ENOENT; + return -1; #endif } @@ -198,6 +223,8 @@ int __wasilibc_rmdirat(int dirfd, const char *path) { return __wasilibc_nocwd___wasilibc_rmdirat(dirfd, path); #else - WEBP2_UNSUPPORTED("__wasilibc_rmdirat"); + WASILIBC_LOG_UNSUPPORTED("__wasilibc_rmdirat"); + errno = ENOENT; + return -1; #endif } diff --git a/libc-bottom-half/sources/bind.c b/libc-bottom-half/sources/bind.c index 58b7f49e4..bbfe4936b 100644 --- a/libc-bottom-half/sources/bind.c +++ b/libc-bottom-half/sources/bind.c @@ -19,6 +19,8 @@ int bind(int socket, const struct sockaddr *addr, socklen_t addrlen) { } return entry.vtable->bind(entry.data, addr, addrlen); #else - WEBP2_UNSUPPORTED("bind"); + WASILIBC_LOG_UNSUPPORTED("bind"); + errno = ENOSYS; + return -1; #endif // !webp2 } diff --git a/libc-bottom-half/sources/chdir.c b/libc-bottom-half/sources/chdir.c index e5e8ac5e6..17ec7599b 100644 --- a/libc-bottom-half/sources/chdir.c +++ b/libc-bottom-half/sources/chdir.c @@ -77,7 +77,9 @@ int chdir(const char *path) { __wasilibc_cwd_mallocd = 1; return 0; #else - WEBP2_UNSUPPORTED("chdir"); + WASILIBC_LOG_UNSUPPORTED("chdir"); + errno = ENOENT; + return -1; #endif // !webp2 } diff --git a/libc-bottom-half/sources/connect.c b/libc-bottom-half/sources/connect.c index 2c6990671..381c61364 100644 --- a/libc-bottom-half/sources/connect.c +++ b/libc-bottom-half/sources/connect.c @@ -19,6 +19,8 @@ int connect(int fd, const struct sockaddr *addr, socklen_t addrlen) { } return entry.vtable->connect(entry.data, addr, addrlen); #else - WEBP2_UNSUPPORTED("connect"); + WASILIBC_LOG_UNSUPPORTED("connect"); + errno = ENOSYS; + return -1; #endif // !webp2 } diff --git a/libc-bottom-half/sources/getcwd.c b/libc-bottom-half/sources/getcwd.c index dc9a7962f..147e7665b 100644 --- a/libc-bottom-half/sources/getcwd.c +++ b/libc-bottom-half/sources/getcwd.c @@ -36,6 +36,8 @@ char *getcwd(char *buf, size_t size) { WEAK_UNLOCK(__wasilibc_cwd_lock); return buf; #else - WEBP2_UNSUPPORTED("getcwd"); + WASILIBC_LOG_UNSUPPORTED("getcwd"); + errno = ENOENT; + return NULL; #endif // !webp2 } diff --git a/libc-bottom-half/sources/listen.c b/libc-bottom-half/sources/listen.c index ba12d03c8..9eb688eb3 100644 --- a/libc-bottom-half/sources/listen.c +++ b/libc-bottom-half/sources/listen.c @@ -27,6 +27,8 @@ int listen(int socket, int backlog) { } return entry.vtable->listen(entry.data, backlog); #else - WEBP2_UNSUPPORTED("listen"); + WASILIBC_LOG_UNSUPPORTED("listen"); + errno = ENOSYS; + return -1; #endif // !webp2 } diff --git a/libc-bottom-half/sources/pipe.c b/libc-bottom-half/sources/pipe.c index 2e94c9750..d1bc0bbf6 100644 --- a/libc-bottom-half/sources/pipe.c +++ b/libc-bottom-half/sources/pipe.c @@ -501,7 +501,9 @@ int pipe(int fd[2]) { #if !defined(__webp2__) return pipe2(fd, 0); #else - WEBP2_UNSUPPORTED("pipe"); + WASILIBC_LOG_UNSUPPORTED("pipe"); + errno = ENOTSUP; + return -1; #endif } @@ -515,7 +517,9 @@ int pipe2(int fd[2], int flags) { errno = ENOTSUP; return -1; #elif defined(__webp2__) - WEBP2_UNSUPPORTED("pipe2"); + WASILIBC_LOG_UNSUPPORTED("pipe2"); + errno = ENOTSUP; + return -1; #else pipe_t *reader = calloc(1, sizeof(pipe_t)); pipe_t *writer = calloc(1, sizeof(pipe_t)); diff --git a/libc-bottom-half/sources/posix.c b/libc-bottom-half/sources/posix.c index f16b0ec99..696c3046d 100644 --- a/libc-bottom-half/sources/posix.c +++ b/libc-bottom-half/sources/posix.c @@ -53,13 +53,9 @@ static int find_relpath_alt(const char *path, char **relative) { #endif // !webp2 int open(const char *path, int oflag, ...) { -#if !defined(__webp2__) // WASI libc's `openat` ignores the mode argument, so call a special // entrypoint which avoids the varargs calling convention. return __wasilibc_open_nomode(path, oflag); -#else - WEBP2_UNSUPPORTED("open"); -#endif } // See the documentation in libc.h @@ -76,8 +72,10 @@ int __wasilibc_open_nomode(const char *path, int oflag) { return __wasilibc_nocwd_openat_nomode(dirfd, relative_path, oflag); #else - WEBP2_UNSUPPORTED("__wasilibc_open_nomode"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("open"); + errno = (oflag & O_CREAT) ? EACCES : ENOENT; + return -1; +#endif } int access(const char *path, int amode) { @@ -93,8 +91,10 @@ int access(const char *path, int amode) { return __wasilibc_nocwd_faccessat(dirfd, relative_path, amode, 0); #else - WEBP2_UNSUPPORTED("access"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("access"); + errno = ENOENT; + return -1; +#endif } ssize_t readlink(const char *restrict path, char *restrict buf, @@ -111,8 +111,10 @@ ssize_t readlink(const char *restrict path, char *restrict buf, return __wasilibc_nocwd_readlinkat(dirfd, relative_path, buf, bufsize); #else - WEBP2_UNSUPPORTED("readlink"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("readlink"); + errno = ENOENT; + return -1; +#endif } int stat(const char *restrict path, struct stat *restrict buf) { @@ -133,8 +135,10 @@ int stat(const char *restrict path, struct stat *restrict buf) { return __wasilibc_nocwd_fstatat(dirfd, relative_path, buf, 0); #else - WEBP2_UNSUPPORTED("stat"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("stat"); + errno = ENOENT; + return -1; +#endif } int lstat(const char *restrict path, struct stat *restrict buf) { @@ -156,8 +160,10 @@ int lstat(const char *restrict path, struct stat *restrict buf) { return __wasilibc_nocwd_fstatat(dirfd, relative_path, buf, AT_SYMLINK_NOFOLLOW); #else - WEBP2_UNSUPPORTED("lstat"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("lstat"); + errno = ENOENT; + return -1; +#endif } int utime(const char *path, const struct utimbuf *times) { @@ -178,8 +184,10 @@ int utime(const char *path, const struct utimbuf *times) { : NULL, 0); #else - WEBP2_UNSUPPORTED("utime"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("utime"); + errno = ENOENT; + return -1; +#endif } int utimes(const char *path, const struct timeval times[2]) { @@ -203,8 +211,10 @@ int utimes(const char *path, const struct timeval times[2]) { : NULL, 0); #else - WEBP2_UNSUPPORTED("utimes"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("utimes"); + errno = ENOENT; + return -1; +#endif } int unlink(const char *path) { @@ -223,8 +233,10 @@ int unlink(const char *path) { // just imports `__wasi_path_unlink_file`. return __wasilibc_nocwd___wasilibc_unlinkat(dirfd, relative_path); #else - WEBP2_UNSUPPORTED("unlink"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("unlink"); + errno = ENOENT; + return -1; +#endif } int rmdir(const char *path) { @@ -240,8 +252,10 @@ int rmdir(const char *path) { return __wasilibc_nocwd___wasilibc_rmdirat(dirfd, relative_path); #else - WEBP2_UNSUPPORTED("rmdir"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("rmdir"); + errno = ENOENT; + return -1; +#endif } int remove(const char *path) { @@ -268,8 +282,10 @@ int remove(const char *path) { } return r; #else - WEBP2_UNSUPPORTED("remove"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("remove"); + errno = ENOENT; + return -1; +#endif } int mkdir(const char *path, mode_t mode) { @@ -286,8 +302,10 @@ int mkdir(const char *path, mode_t mode) { return __wasilibc_nocwd_mkdirat_nomode(dirfd, relative_path); #else - WEBP2_UNSUPPORTED("mkdir"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("mkdir"); + errno = EACCES; + return -1; +#endif } DIR *opendir(const char *dirname) { @@ -303,8 +321,10 @@ DIR *opendir(const char *dirname) { return __wasilibc_nocwd_opendirat(dirfd, relative_path); #else - WEBP2_UNSUPPORTED("opendir"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("opendir"); + errno = ENOENT; + return NULL; +#endif } int scandir(const char *restrict dir, struct dirent ***restrict namelist, @@ -323,8 +343,10 @@ int scandir(const char *restrict dir, struct dirent ***restrict namelist, return __wasilibc_nocwd_scandirat(dirfd, relative_path, namelist, filter, compar); #else - WEBP2_UNSUPPORTED("scandir"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("scandir"); + errno = ENOENT; + return -1; +#endif } int symlink(const char *target, const char *linkpath) { @@ -340,8 +362,10 @@ int symlink(const char *target, const char *linkpath) { return __wasilibc_nocwd_symlinkat(target, dirfd, relative_path); #else - WEBP2_UNSUPPORTED("symlink"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("symlink"); + errno = EACCES; + return -1; +#endif } int link(const char *old, const char *new) { @@ -362,8 +386,10 @@ int link(const char *old, const char *new) { errno = ENOENT; return -1; #else - WEBP2_UNSUPPORTED("link"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("link"); + errno = ENOENT; + return -1; +#endif } int rename(const char *old, const char *new) { @@ -384,40 +410,35 @@ int rename(const char *old, const char *new) { errno = ENOENT; return -1; #else - WEBP2_UNSUPPORTED("rename"); -#endif // !webp2 + WASILIBC_LOG_UNSUPPORTED("rename"); + errno = ENOENT; + return -1; +#endif } int chmod(const char *path, mode_t mode) { -#if !defined(__webp2__) (void)path; (void)mode; // TODO: We plan to support this eventually in WASI, but not yet. // Meanwhile, we provide a stub so that libc++'s `` // implementation will build unmodified. + WASILIBC_LOG_UNSUPPORTED("chmod"); errno = ENOSYS; return -1; -#else - WEBP2_UNSUPPORTED("chmod"); -#endif // !webp2 } int fchmod(int fd, mode_t mode) { -#if !defined(__webp2__) (void)fd; (void)mode; // TODO: We plan to support this eventually in WASI, but not yet. // Meanwhile, we provide a stub so that libc++'s `` // implementation will build unmodified. + WASILIBC_LOG_UNSUPPORTED("fchmod"); errno = ENOSYS; return -1; -#else - WEBP2_UNSUPPORTED("fchmod"); -#endif // !webp2 } int fchmodat(int fd, const char *path, mode_t mode, int flag) { -#if !defined(__webp2__) (void)fd; (void)path; (void)mode; @@ -425,44 +446,36 @@ int fchmodat(int fd, const char *path, mode_t mode, int flag) { // TODO: We plan to support this eventually in WASI, but not yet. // Meanwhile, we provide a stub so that libc++'s `` // implementation will build unmodified. + WASILIBC_LOG_UNSUPPORTED("fchmodat"); errno = ENOSYS; return -1; -#else - WEBP2_UNSUPPORTED("fchmodat"); -#endif // !webp2 } int statvfs(const char *__restrict path, struct statvfs *__restrict buf) { -#if !defined(__webp2__) (void)path; (void)buf; // TODO: We plan to support this eventually in WASI, but not yet. // Meanwhile, we provide a stub so that libc++'s `` // implementation will build unmodified. + WASILIBC_LOG_UNSUPPORTED("statvfs"); errno = ENOSYS; return -1; -#else - WEBP2_UNSUPPORTED("statvfs"); -#endif // !webp2 } int fstatvfs(int fd, struct statvfs *buf) { -#if !defined(__webp2__) (void)fd; (void)buf; // TODO: We plan to support this eventually in WASI, but not yet. // Meanwhile, we provide a stub so that libc++'s `` // implementation will build unmodified. + WASILIBC_LOG_UNSUPPORTED("fstatvfs"); errno = ENOSYS; return -1; -#else - WEBP2_UNSUPPORTED("fstatvfs"); -#endif // !webp2 } +#if !defined(__webp2__) // Like `access`, but with `faccessat`'s flags argument. int __wasilibc_access(const char *path, int mode, int flags) { -#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -473,15 +486,11 @@ int __wasilibc_access(const char *path, int mode, int flags) { } return __wasilibc_nocwd_faccessat(dirfd, relative_path, mode, flags); -#else - WEBP2_UNSUPPORTED("__wasilibc_access"); -#endif // !webp2 } // Like `utimensat`, but without the `at` part. int __wasilibc_utimens(const char *path, const struct timespec times[2], int flags) { -#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -492,15 +501,11 @@ int __wasilibc_utimens(const char *path, const struct timespec times[2], } return __wasilibc_nocwd_utimensat(dirfd, relative_path, times, flags); -#else - WEBP2_UNSUPPORTED("__wasilibc_utimens"); -#endif // !webp2 } // Like `stat`, but with `fstatat`'s flags argument. int __wasilibc_stat(const char *__restrict path, struct stat *__restrict st, int flags) { -#if !defined(__webp2__) if (path[0] == '\0') { errno = ENOENT; return -1; @@ -516,14 +521,10 @@ int __wasilibc_stat(const char *__restrict path, struct stat *__restrict st, } return __wasilibc_nocwd_fstatat(dirfd, relative_path, st, flags); -#else - WEBP2_UNSUPPORTED("__wasilibc_stat"); -#endif // !webp2 } // Like `link`, but with `linkat`'s flags argument. int __wasilibc_link(const char *oldpath, const char *newpath, int flags) { -#if !defined(__webp2__) char *old_relative_path; char *new_relative_path; int old_dirfd = find_relpath(oldpath, &old_relative_path); @@ -537,15 +538,11 @@ int __wasilibc_link(const char *oldpath, const char *newpath, int flags) { return __wasilibc_nocwd_linkat(old_dirfd, old_relative_path, new_dirfd, new_relative_path, flags); -#else - WEBP2_UNSUPPORTED("__wasilibc_link"); -#endif // !webp2 } // Like `__wasilibc_link`, but oldpath is relative to olddirfd. int __wasilibc_link_oldat(int olddirfd, const char *oldpath, const char *newpath, int flags) { -#if !defined(__webp2__) char *new_relative_path; int new_dirfd = find_relpath(newpath, &new_relative_path); @@ -557,15 +554,11 @@ int __wasilibc_link_oldat(int olddirfd, const char *oldpath, return __wasilibc_nocwd_linkat(olddirfd, oldpath, new_dirfd, new_relative_path, flags); -#else - WEBP2_UNSUPPORTED("__wasilibc_link_oldat"); -#endif // !webp2 } // Like `__wasilibc_link`, but newpath is relative to newdirfd. int __wasilibc_link_newat(const char *oldpath, int newdirfd, const char *newpath, int flags) { -#if !defined(__webp2__) char *old_relative_path; int old_dirfd = find_relpath(oldpath, &old_relative_path); @@ -577,14 +570,10 @@ int __wasilibc_link_newat(const char *oldpath, int newdirfd, return __wasilibc_nocwd_linkat(old_dirfd, old_relative_path, newdirfd, newpath, flags); -#else - WEBP2_UNSUPPORTED("__wasilibc_link_newat"); -#endif // !webp2 } // Like `rename`, but from is relative to fromdirfd. int __wasilibc_rename_oldat(int fromdirfd, const char *from, const char *to) { -#if !defined(__webp2__) char *to_relative_path; int to_dirfd = find_relpath(to, &to_relative_path); @@ -595,14 +584,10 @@ int __wasilibc_rename_oldat(int fromdirfd, const char *from, const char *to) { } return __wasilibc_nocwd_renameat(fromdirfd, from, to_dirfd, to_relative_path); -#else - WEBP2_UNSUPPORTED("__wasilibc_rename_oldat"); -#endif // !webp2 } // Like `rename`, but to is relative to todirfd. int __wasilibc_rename_newat(const char *from, int todirfd, const char *to) { -#if !defined(__webp2__) char *from_relative_path; int from_dirfd = find_relpath(from, &from_relative_path); @@ -613,7 +598,5 @@ int __wasilibc_rename_newat(const char *from, int todirfd, const char *to) { } return __wasilibc_nocwd_renameat(from_dirfd, from_relative_path, todirfd, to); -#else - WEBP2_UNSUPPORTED("__wasilibc_rename_newat"); -#endif // !webp2 } +#endif // !webp2 diff --git a/libc-bottom-half/sources/ppoll.c b/libc-bottom-half/sources/ppoll.c index 8ec6a0843..c3e75b6a7 100644 --- a/libc-bottom-half/sources/ppoll.c +++ b/libc-bottom-half/sources/ppoll.c @@ -607,7 +607,10 @@ static int ppoll_impl(struct pollfd *fds, size_t nfds, // never hit this. bool zero_timeout = timeout && timeout->tv_sec == 0 && timeout->tv_nsec == 0; if (ready == 0 && !zero_timeout) { - WEBP2_UNSUPPORTED("blocking ppoll"); + // TODO(webp2): If we actually return from this, we risk user code going + // into an infinite loop. Therefore we noisily exit. + __wasilibc_report_error("blocking ppoll is not supported"); + exit(1); } return ready; } diff --git a/libc-bottom-half/sources/preopens.c b/libc-bottom-half/sources/preopens.c index 91268182c..67a3435e7 100644 --- a/libc-bottom-half/sources/preopens.c +++ b/libc-bottom-half/sources/preopens.c @@ -405,7 +405,7 @@ void __wasilibc_populate_preopens(void) { software: _Exit(EX_SOFTWARE); #else - WEBP2_UNSUPPORTED("__wasilibc_populate_preopens"); + WASILIBC_LOG_UNSUPPORTED("__wasilibc_populate_preopens"); #endif // !webp2 } @@ -434,6 +434,6 @@ void __wasilibc_reset_preopens(void) { STRONG_UNLOCK(lock); #else - WEBP2_UNSUPPORTED("__wasilibc_reset_preopens"); + WASILIBC_LOG_UNSUPPORTED("__wasilibc_reset_preopens"); #endif // !webp2 } diff --git a/libc-bottom-half/sources/recv.c b/libc-bottom-half/sources/recv.c index f084503b6..9592aad60 100644 --- a/libc-bottom-half/sources/recv.c +++ b/libc-bottom-half/sources/recv.c @@ -11,7 +11,9 @@ ssize_t recv(int socket, void *restrict buffer, size_t length, int flags) { #if !defined(__webp2__) return recvfrom(socket, buffer, length, flags, NULL, NULL); #else - WEBP2_UNSUPPORTED("recv"); + WASILIBC_LOG_UNSUPPORTED("recv"); + errno = ENOSYS; + return -1; #endif } @@ -35,6 +37,8 @@ ssize_t recvfrom(int socket, void *__restrict buffer, size_t length, int flags, return entry.vtable->recvfrom(entry.data, buffer, length, flags, addr, addrlen); #else - WEBP2_UNSUPPORTED("recvfrom"); + WASILIBC_LOG_UNSUPPORTED("recvfrom"); + errno = ENOSYS; + return -1; #endif // !webp2 } diff --git a/libc-bottom-half/sources/send.c b/libc-bottom-half/sources/send.c index be2206537..d195c0385 100644 --- a/libc-bottom-half/sources/send.c +++ b/libc-bottom-half/sources/send.c @@ -11,7 +11,9 @@ ssize_t send(int socket, const void *buffer, size_t length, int flags) { #if !defined(__webp2__) return sendto(socket, buffer, length, flags, NULL, 0); #else - WEBP2_UNSUPPORTED("send"); + WASILIBC_LOG_UNSUPPORTED("send"); + errno = ENOTSOCK; + return -1; #endif } @@ -34,6 +36,8 @@ ssize_t sendto(int socket, const void *buffer, size_t length, int flags, } return entry.vtable->sendto(entry.data, buffer, length, flags, addr, addrlen); #else - WEBP2_UNSUPPORTED("sendto"); + WASILIBC_LOG_UNSUPPORTED("sendto"); + errno = ENOTSOCK; + return -1; #endif // !webp2 } diff --git a/libc-bottom-half/sources/shutdown.c b/libc-bottom-half/sources/shutdown.c index 5dc3eb32b..afc6ffd54 100644 --- a/libc-bottom-half/sources/shutdown.c +++ b/libc-bottom-half/sources/shutdown.c @@ -18,6 +18,8 @@ int shutdown(int socket, int how) { } return entry.vtable->shutdown(entry.data, how); #else - WEBP2_UNSUPPORTED("shutdown"); + WASILIBC_LOG_UNSUPPORTED("shutdown"); + errno = ENOSYS; + return -1; #endif // !webp2 } diff --git a/libc-bottom-half/sources/socket.c b/libc-bottom-half/sources/socket.c index 072ce44f3..0d2e606c3 100644 --- a/libc-bottom-half/sources/socket.c +++ b/libc-bottom-half/sources/socket.c @@ -70,6 +70,8 @@ int socket(int domain, int type, int protocol) { return -1; } #else - WEBP2_UNSUPPORTED("socket"); + WASILIBC_LOG_UNSUPPORTED("socket"); + errno = ENOSYS; + return -1; #endif // !webp2 } diff --git a/libc-bottom-half/sources/truncate.c b/libc-bottom-half/sources/truncate.c index 70cc860ed..c0f9748ca 100644 --- a/libc-bottom-half/sources/truncate.c +++ b/libc-bottom-half/sources/truncate.c @@ -23,6 +23,8 @@ int truncate(const char *path, off_t length) { return close(fd); #else - WEBP2_UNSUPPORTED("truncate"); + WASILIBC_LOG_UNSUPPORTED("truncate"); + errno = ENOENT; + return -1; #endif // !webp2 } diff --git a/libc-top-half/headers/private/wasi/report-error.h b/libc-top-half/headers/private/wasi/report-error.h index 16fed81c8..bc8cf971f 100644 --- a/libc-top-half/headers/private/wasi/report-error.h +++ b/libc-top-half/headers/private/wasi/report-error.h @@ -7,16 +7,15 @@ // Reports an error using the system's typical error output. hidden void __wasilibc_report_error(const char* message); -#ifdef __webp2__ +#ifdef WASILIBC_LOG_UNSUPPORTED_CALLS +#define WASILIBC_LOG_UNSUPPORTED(name) __wasilibc_report_error(name " is not supported"); +#else +#define WASILIBC_LOG_UNSUPPORTED(name) +#endif +#ifdef __webp2__ // Reports an error to the console using webp2 bindings. void __wasilibc_webp2_report_error(const char* message); - -#define WEBP2_UNSUPPORTED(name) \ - do { \ - __wasilibc_report_error(name " is not supported on the web"); \ - __builtin_trap(); \ - } while (0) #endif -#endif // __webp2__ +#endif // __WASI_REPORT_ERROR_H From 03cf4a8aa6e4afd74274bb3949d9388936ba7fbd Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 19 Oct 2026 12:56:14 -0600 Subject: [PATCH 75/96] Use the descriptor table for socket things --- libc-bottom-half/sources/accept.c | 16 ---------------- libc-bottom-half/sources/bind.c | 10 ---------- libc-bottom-half/sources/connect.c | 10 ---------- libc-bottom-half/sources/getsockpeername.c | 12 ------------ libc-bottom-half/sources/listen.c | 10 ---------- libc-bottom-half/sources/recv.c | 16 ---------------- libc-bottom-half/sources/send.c | 16 ---------------- libc-bottom-half/sources/shutdown.c | 10 ---------- libc-bottom-half/sources/sockopt.c | 12 ------------ 9 files changed, 112 deletions(-) diff --git a/libc-bottom-half/sources/accept.c b/libc-bottom-half/sources/accept.c index e1619bc4a..ce70b4176 100644 --- a/libc-bottom-half/sources/accept.c +++ b/libc-bottom-half/sources/accept.c @@ -3,24 +3,13 @@ #include #include -#if defined(__webp2__) -#include -#endif - int accept(int socket, struct sockaddr *restrict addr, socklen_t *restrict addrlen) { -#if !defined(__webp2__) return accept4(socket, addr, addrlen, 0); -#else - WASILIBC_LOG_UNSUPPORTED("accept"); - errno = ENOSYS; - return -1; -#endif } int accept4(int socket, struct sockaddr *restrict addr, socklen_t *restrict addrlen, int flags) { -#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -30,9 +19,4 @@ int accept4(int socket, struct sockaddr *restrict addr, return -1; } return entry.vtable->accept4(entry.data, addr, addrlen, flags); -#else - WASILIBC_LOG_UNSUPPORTED("accept4"); - errno = ENOSYS; - return -1; -#endif // !webp2 } diff --git a/libc-bottom-half/sources/bind.c b/libc-bottom-half/sources/bind.c index bbfe4936b..59c49f24c 100644 --- a/libc-bottom-half/sources/bind.c +++ b/libc-bottom-half/sources/bind.c @@ -3,12 +3,7 @@ #include #include -#if defined(__webp2__) -#include -#endif - int bind(int socket, const struct sockaddr *addr, socklen_t addrlen) { -#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -18,9 +13,4 @@ int bind(int socket, const struct sockaddr *addr, socklen_t addrlen) { return -1; } return entry.vtable->bind(entry.data, addr, addrlen); -#else - WASILIBC_LOG_UNSUPPORTED("bind"); - errno = ENOSYS; - return -1; -#endif // !webp2 } diff --git a/libc-bottom-half/sources/connect.c b/libc-bottom-half/sources/connect.c index 381c61364..f4e1dacc5 100644 --- a/libc-bottom-half/sources/connect.c +++ b/libc-bottom-half/sources/connect.c @@ -3,12 +3,7 @@ #include #include -#if defined(__webp2__) -#include -#endif - int connect(int fd, const struct sockaddr *addr, socklen_t addrlen) { -#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(fd, &entry) < 0) return -1; @@ -18,9 +13,4 @@ int connect(int fd, const struct sockaddr *addr, socklen_t addrlen) { return -1; } return entry.vtable->connect(entry.data, addr, addrlen); -#else - WASILIBC_LOG_UNSUPPORTED("connect"); - errno = ENOSYS; - return -1; -#endif // !webp2 } diff --git a/libc-bottom-half/sources/getsockpeername.c b/libc-bottom-half/sources/getsockpeername.c index b72b4e392..46ffa593c 100644 --- a/libc-bottom-half/sources/getsockpeername.c +++ b/libc-bottom-half/sources/getsockpeername.c @@ -3,13 +3,8 @@ #include #include -#if defined(__webp2__) -#include -#endif - int getsockname(int socket, struct sockaddr *__restrict addr, socklen_t *__restrict addrlen) { -#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -19,14 +14,10 @@ int getsockname(int socket, struct sockaddr *__restrict addr, return -1; } return entry.vtable->getsockname(entry.data, addr, addrlen); -#else - WEBP2_UNSUPPORTED("getsockname"); -#endif // !webp2 } int getpeername(int socket, struct sockaddr *__restrict addr, socklen_t *__restrict addrlen) { -#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -36,7 +27,4 @@ int getpeername(int socket, struct sockaddr *__restrict addr, return -1; } return entry.vtable->getpeername(entry.data, addr, addrlen); -#else - WEBP2_UNSUPPORTED("getpeername"); -#endif // !webp2 } diff --git a/libc-bottom-half/sources/listen.c b/libc-bottom-half/sources/listen.c index 9eb688eb3..14c514a3d 100644 --- a/libc-bottom-half/sources/listen.c +++ b/libc-bottom-half/sources/listen.c @@ -3,12 +3,7 @@ #include #include -#if defined(__webp2__) -#include -#endif - int listen(int socket, int backlog) { -#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -26,9 +21,4 @@ int listen(int socket, int backlog) { return -1; } return entry.vtable->listen(entry.data, backlog); -#else - WASILIBC_LOG_UNSUPPORTED("listen"); - errno = ENOSYS; - return -1; -#endif // !webp2 } diff --git a/libc-bottom-half/sources/recv.c b/libc-bottom-half/sources/recv.c index 9592aad60..96951f9a5 100644 --- a/libc-bottom-half/sources/recv.c +++ b/libc-bottom-half/sources/recv.c @@ -3,24 +3,13 @@ #include #include -#if defined(__webp2__) -#include -#endif - ssize_t recv(int socket, void *restrict buffer, size_t length, int flags) { -#if !defined(__webp2__) return recvfrom(socket, buffer, length, flags, NULL, NULL); -#else - WASILIBC_LOG_UNSUPPORTED("recv"); - errno = ENOSYS; - return -1; -#endif } ssize_t recvfrom(int socket, void *__restrict buffer, size_t length, int flags, struct sockaddr *__restrict addr, socklen_t *__restrict addrlen) { -#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -36,9 +25,4 @@ ssize_t recvfrom(int socket, void *__restrict buffer, size_t length, int flags, } return entry.vtable->recvfrom(entry.data, buffer, length, flags, addr, addrlen); -#else - WASILIBC_LOG_UNSUPPORTED("recvfrom"); - errno = ENOSYS; - return -1; -#endif // !webp2 } diff --git a/libc-bottom-half/sources/send.c b/libc-bottom-half/sources/send.c index d195c0385..1f309006c 100644 --- a/libc-bottom-half/sources/send.c +++ b/libc-bottom-half/sources/send.c @@ -3,23 +3,12 @@ #include #include -#if defined(__webp2__) -#include -#endif - ssize_t send(int socket, const void *buffer, size_t length, int flags) { -#if !defined(__webp2__) return sendto(socket, buffer, length, flags, NULL, 0); -#else - WASILIBC_LOG_UNSUPPORTED("send"); - errno = ENOTSOCK; - return -1; -#endif } ssize_t sendto(int socket, const void *buffer, size_t length, int flags, const struct sockaddr *addr, socklen_t addrlen) { -#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -35,9 +24,4 @@ ssize_t sendto(int socket, const void *buffer, size_t length, int flags, return -1; } return entry.vtable->sendto(entry.data, buffer, length, flags, addr, addrlen); -#else - WASILIBC_LOG_UNSUPPORTED("sendto"); - errno = ENOTSOCK; - return -1; -#endif // !webp2 } diff --git a/libc-bottom-half/sources/shutdown.c b/libc-bottom-half/sources/shutdown.c index afc6ffd54..233cb7938 100644 --- a/libc-bottom-half/sources/shutdown.c +++ b/libc-bottom-half/sources/shutdown.c @@ -2,12 +2,7 @@ #include #include -#if defined(__webp2__) -#include -#endif - int shutdown(int socket, int how) { -#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(socket, &entry) < 0) return -1; @@ -17,9 +12,4 @@ int shutdown(int socket, int how) { return -1; } return entry.vtable->shutdown(entry.data, how); -#else - WASILIBC_LOG_UNSUPPORTED("shutdown"); - errno = ENOSYS; - return -1; -#endif // !webp2 } diff --git a/libc-bottom-half/sources/sockopt.c b/libc-bottom-half/sources/sockopt.c index df7816971..82f3992d2 100644 --- a/libc-bottom-half/sources/sockopt.c +++ b/libc-bottom-half/sources/sockopt.c @@ -3,13 +3,8 @@ #include #include -#if defined(__webp2__) -#include -#endif - int getsockopt(int sockfd, int level, int optname, void *restrict optval, socklen_t *restrict optlen) { -#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(sockfd, &entry) < 0) return -1; @@ -28,14 +23,10 @@ int getsockopt(int sockfd, int level, int optname, void *restrict optval, } return entry.vtable->getsockopt(entry.data, level, optname, optval, optlen); -#else - WEBP2_UNSUPPORTED("getsockopt"); -#endif // !webp2 } int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen) { -#if !defined(__webp2__) descriptor_table_entry_t entry; if (descriptor_table_get(sockfd, &entry) < 0) return -1; @@ -54,7 +45,4 @@ int setsockopt(int sockfd, int level, int optname, const void *optval, } return entry.vtable->setsockopt(entry.data, level, optname, optval, optlen); -#else - WEBP2_UNSUPPORTED("setsockopt"); -#endif // !webp2 } From e5e55b02d591c04031c9e06bbdf1d3225578e342 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 19 Oct 2026 13:01:52 -0600 Subject: [PATCH 76/96] Fix header error, fix symbol lists --- expected/wasm32-webp2/defined-symbols.txt | 8 -------- libc-bottom-half/sources/posix.c | 3 +-- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/expected/wasm32-webp2/defined-symbols.txt b/expected/wasm32-webp2/defined-symbols.txt index 1384d0263..a9f4f3db5 100644 --- a/expected/wasm32-webp2/defined-symbols.txt +++ b/expected/wasm32-webp2/defined-symbols.txt @@ -304,7 +304,6 @@ __uselocale __utc __wait __wake -__wasilibc_access __wasilibc_deinitialize_environ __wasilibc_descriptor_deallocate __wasilibc_dttoif @@ -317,27 +316,20 @@ __wasilibc_iftodt __wasilibc_init_stdio __wasilibc_initialize_environ __wasilibc_iovecs_ok -__wasilibc_link -__wasilibc_link_newat -__wasilibc_link_oldat __wasilibc_maybe_reinitialize_environ_eagerly __wasilibc_open_nomode __wasilibc_populate_preopens __wasilibc_pthread_self __wasilibc_random -__wasilibc_rename_newat -__wasilibc_rename_oldat __wasilibc_report_error __wasilibc_report_errorf __wasilibc_reset_preopens __wasilibc_rmdirat -__wasilibc_stat __wasilibc_tell __wasilibc_unlinkat __wasilibc_utf8_abort_current __wasilibc_utf8_reset __wasilibc_utf8_write -__wasilibc_utimens __wasilibc_validate_utf8 __wasilibc_webp2_fflush __wasilibc_webp2_flush_and_close_stdio diff --git a/libc-bottom-half/sources/posix.c b/libc-bottom-half/sources/posix.c index 696c3046d..dd5791223 100644 --- a/libc-bottom-half/sources/posix.c +++ b/libc-bottom-half/sources/posix.c @@ -10,12 +10,11 @@ #include #include #include +#include #if !defined(__webp2__) #include #include -#else -#include #endif #ifndef __webp2__ From c1819a3d57fdc191b7c731d46c52ccacc335551c Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 20 Oct 2026 10:37:19 -0600 Subject: [PATCH 77/96] Fold report-errorf.h into report-error.h --- libc-bottom-half/CMakeLists.txt | 1 - .../sources/__wasilibc_report_error.c | 36 ++++++++++++++++++ .../sources/__wasilibc_report_errorf.c | 37 ------------------- .../headers/private/wasi/report-error.h | 2 + .../headers/private/wasi/report-errorf.h | 9 ----- libc-top-half/musl/src/exit/assert.c | 2 +- 6 files changed, 39 insertions(+), 48 deletions(-) delete mode 100644 libc-bottom-half/sources/__wasilibc_report_errorf.c delete mode 100644 libc-top-half/headers/private/wasi/report-errorf.h diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index 23873637e..0cb9fc595 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -117,7 +117,6 @@ set(bottom_half_sources sources/__wasilibc_random.c sources/__wasilibc_real.c sources/__wasilibc_report_error.c - sources/__wasilibc_report_errorf.c sources/__wasilibc_tell.c sources/abort.c sources/at_fdcwd.c diff --git a/libc-bottom-half/sources/__wasilibc_report_error.c b/libc-bottom-half/sources/__wasilibc_report_error.c index 4da475331..0e214b89d 100644 --- a/libc-bottom-half/sources/__wasilibc_report_error.c +++ b/libc-bottom-half/sources/__wasilibc_report_error.c @@ -1,8 +1,10 @@ +#include #include #include #include #ifdef __webp2__ +#include #include #endif @@ -14,6 +16,40 @@ void __wasilibc_report_error(const char *message) { #endif } +void __wasilibc_report_errorf(const char *format, ...) { + va_list ap; + va_start(ap, format); + +#if defined(__wasi__) + vfprintf(stderr, format, ap); + fputc('\n', stderr); +#elif defined(__webp2__) +#define ERROR_MAX_LEN 512 + char format_buf[ERROR_MAX_LEN]; + uint8_t utf8_buf[ERROR_MAX_LEN + 1]; // leave room for nul + + int n = vsnprintf(format_buf, sizeof(format_buf), format, ap); + wasilibc_utf8_writer_t utf8 = + __wasilibc_make_utf8_writer(utf8_buf, ERROR_MAX_LEN); + + // Re-encode as UTF-8 (excluding vnsprintf's nul) + for (int i = 0; i < n && i < (int)sizeof(format_buf) - 1; i++) { + if (!__wasilibc_utf8_write(&utf8, format_buf[i])) { + break; + } + } + __wasilibc_utf8_abort_current(&utf8); + + // Nul-terminate message and pass along + utf8_buf[utf8.valid_len] = 0; + __wasilibc_report_error((char *)utf8_buf); +#else +#error "Unknown WASI version" +#endif + + va_end(ap); +} + #ifdef __webp2__ void __wasilibc_webp2_report_error(const char *message) { __wasilibc_webp2_flush_and_close_stdio(/*also_musl=*/true); diff --git a/libc-bottom-half/sources/__wasilibc_report_errorf.c b/libc-bottom-half/sources/__wasilibc_report_errorf.c deleted file mode 100644 index e6de85177..000000000 --- a/libc-bottom-half/sources/__wasilibc_report_errorf.c +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include -#include -#include - -void __wasilibc_report_errorf(const char *format, ...) { - va_list ap; - va_start(ap, format); - -#if defined(__webp2__) -#define ERROR_MAX_LEN 512 - char format_buf[ERROR_MAX_LEN]; - uint8_t utf8_buf[ERROR_MAX_LEN + 1]; // leave room for nul - - int n = vsnprintf(format_buf, sizeof(format_buf), format, ap); - wasilibc_utf8_writer_t utf8 = - __wasilibc_make_utf8_writer(utf8_buf, ERROR_MAX_LEN); - - // Re-encode as UTF-8 (excluding vnsprintf's nul) - for (int i = 0; i < n && i < (int)sizeof(format_buf) - 1; i++) { - if (!__wasilibc_utf8_write(&utf8, format_buf[i])) { - break; - } - } - __wasilibc_utf8_abort_current(&utf8); - - // Nul-terminate message and pass along - utf8_buf[utf8.valid_len] = 0; - __wasilibc_report_error((char *)utf8_buf); -#else - vfprintf(stderr, format, ap); - fputc('\n', stderr); -#endif - - va_end(ap); -} diff --git a/libc-top-half/headers/private/wasi/report-error.h b/libc-top-half/headers/private/wasi/report-error.h index bc8cf971f..7509af15d 100644 --- a/libc-top-half/headers/private/wasi/report-error.h +++ b/libc-top-half/headers/private/wasi/report-error.h @@ -6,6 +6,8 @@ // Reports an error using the system's typical error output. hidden void __wasilibc_report_error(const char* message); +hidden void __wasilibc_report_errorf(const char *format, ...) + __attribute__((__format__(__printf__, 1, 2))); #ifdef WASILIBC_LOG_UNSUPPORTED_CALLS #define WASILIBC_LOG_UNSUPPORTED(name) __wasilibc_report_error(name " is not supported"); diff --git a/libc-top-half/headers/private/wasi/report-errorf.h b/libc-top-half/headers/private/wasi/report-errorf.h deleted file mode 100644 index a4f03fb18..000000000 --- a/libc-top-half/headers/private/wasi/report-errorf.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef __WASI_REPORT_ERRORF_H -#define __WASI_REPORT_ERRORF_H - -#include - -hidden void __wasilibc_report_errorf(const char *format, ...) - __attribute__((__format__(__printf__, 1, 2))); - -#endif diff --git a/libc-top-half/musl/src/exit/assert.c b/libc-top-half/musl/src/exit/assert.c index 8a7956e71..d917976c2 100644 --- a/libc-top-half/musl/src/exit/assert.c +++ b/libc-top-half/musl/src/exit/assert.c @@ -1,5 +1,5 @@ #include -#include +#include _Noreturn void __assert_fail(const char *expr, const char *file, int line, const char *func) { From e0cc4bff9d67f4b247b938304a70ea027c3c150a Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 22 Oct 2026 09:24:16 -0600 Subject: [PATCH 78/96] Remove lots of webp2-specific code Many logs of unsupported functions have been removed since they will fall back on deeper functions that will also fail. Additionally, there are now fewer webp2-specific exceptions in the build. --- libc-bottom-half/CMakeLists.txt | 50 +++----- .../cloudlibc/src/libc/dirent/closedir.c | 11 +- .../cloudlibc/src/libc/dirent/dirent_impl.h | 3 +- .../cloudlibc/src/libc/dirent/dirfd.c | 11 -- .../cloudlibc/src/libc/dirent/fdclosedir.c | 13 +- .../cloudlibc/src/libc/dirent/fdopendir.c | 7 +- .../cloudlibc/src/libc/dirent/readdir.c | 16 +-- .../cloudlibc/src/libc/dirent/rewinddir.c | 10 +- .../cloudlibc/src/libc/dirent/seekdir.c | 7 +- .../cloudlibc/src/libc/dirent/telldir.c | 7 +- .../cloudlibc/src/libc/fcntl/openat.c | 13 +- .../cloudlibc/src/libc/fcntl/posix_fadvise.c | 6 +- .../src/libc/fcntl/posix_fallocate.c | 12 +- .../cloudlibc/src/libc/stdio/renameat.c | 11 +- .../cloudlibc/src/libc/stdlib/_Exit.c | 7 +- .../cloudlibc/src/libc/sys/stat/fstat.c | 6 +- .../cloudlibc/src/libc/sys/stat/fstatat.c | 5 + .../cloudlibc/src/libc/sys/stat/futimens.c | 14 +- .../cloudlibc/src/libc/sys/stat/mkdirat.c | 7 + .../cloudlibc/src/libc/sys/stat/stat_impl.h | 2 + .../cloudlibc/src/libc/sys/stat/utimensat.c | 16 ++- .../cloudlibc/src/libc/sys/uio/preadv.c | 13 +- .../cloudlibc/src/libc/sys/uio/pwritev.c | 13 +- .../cloudlibc/src/libc/time/clock_nanosleep.c | 24 ++-- .../cloudlibc/src/libc/time/nanosleep.c | 13 +- .../cloudlibc/src/libc/unistd/faccessat.c | 9 +- .../cloudlibc/src/libc/unistd/fdatasync.c | 8 +- .../cloudlibc/src/libc/unistd/fsync.c | 8 +- .../cloudlibc/src/libc/unistd/ftruncate.c | 8 +- .../cloudlibc/src/libc/unistd/linkat.c | 12 +- .../cloudlibc/src/libc/unistd/pread.c | 8 +- .../cloudlibc/src/libc/unistd/pwrite.c | 10 +- .../cloudlibc/src/libc/unistd/readlinkat.c | 9 ++ .../cloudlibc/src/libc/unistd/sleep.c | 31 ++--- .../cloudlibc/src/libc/unistd/symlinkat.c | 8 ++ .../cloudlibc/src/libc/unistd/unlinkat.c | 13 +- .../cloudlibc/src/libc/unistd/usleep.c | 15 +-- .../headers/private/wasi/sockets_utils.h | 4 +- libc-bottom-half/headers/private/wasi/tcp.h | 4 +- libc-bottom-half/headers/private/wasi/udp.h | 4 +- .../headers/public/wasi/libc-find-relpath.h | 4 + .../sources/__wasilibc_report_error.c | 5 +- libc-bottom-half/sources/__wasilibc_rmdirat.c | 7 + .../sources/__wasilibc_unlinkat.c | 7 + libc-bottom-half/sources/abort.c | 3 - libc-bottom-half/sources/at_fdcwd.c | 114 +++-------------- libc-bottom-half/sources/chdir.c | 13 +- libc-bottom-half/sources/getcwd.c | 15 ++- libc-bottom-half/sources/pipe.c | 14 +- libc-bottom-half/sources/posix.c | 121 +++--------------- libc-bottom-half/sources/ppoll.c | 5 +- libc-bottom-half/sources/preopens.c | 28 ++-- libc-bottom-half/sources/socket.c | 17 +-- libc-bottom-half/sources/truncate.c | 10 -- libc-top-half/musl/src/stdio/fflush.c | 3 - 55 files changed, 287 insertions(+), 527 deletions(-) diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index 0cb9fc595..c5c00eefb 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -31,16 +31,7 @@ if (NOT (WASI STREQUAL "p3")) list(REMOVE_ITEM globbed_headers headers/public/wasi/__generated_wasip3.h) endif() -if (WEB STREQUAL "p2") - # Remove some headers that are unnecessary for webp2, either because their - # contents are completely ifdef'd out or because they contain only internal - # utility functions we would otherwise need to stub. - list(REMOVE_ITEM globbed_headers headers/public/wasi/libc-find-relpath.h) - list(REMOVE_ITEM globbed_headers headers/public/wasi/libc-nocwd.h) - list(REMOVE_ITEM globbed_headers headers/public/wasi/wasip1.h) - list(REMOVE_ITEM globbed_headers headers/public/wasi/wasip2.h) - list(REMOVE_ITEM globbed_headers headers/public/wasi/wasip3.h) -else () +if (NOT (WEB STREQUAL "p2")) list(REMOVE_ITEM globbed_headers headers/public/wasi/__generated_webp2.h) endif() @@ -69,22 +60,29 @@ set(bottom_half_sources cloudlibc/src/libc/dirent/dirfd.c cloudlibc/src/libc/dirent/fdclosedir.c cloudlibc/src/libc/dirent/fdopendir.c + cloudlibc/src/libc/dirent/opendirat.c cloudlibc/src/libc/dirent/readdir.c cloudlibc/src/libc/dirent/rewinddir.c + cloudlibc/src/libc/dirent/scandirat.c cloudlibc/src/libc/dirent/seekdir.c cloudlibc/src/libc/dirent/telldir.c cloudlibc/src/libc/errno/errno.c cloudlibc/src/libc/fcntl/fcntl.c + cloudlibc/src/libc/fcntl/openat.c cloudlibc/src/libc/fcntl/posix_fadvise.c cloudlibc/src/libc/fcntl/posix_fallocate.c cloudlibc/src/libc/poll/poll.c cloudlibc/src/libc/sched/sched_yield.c + cloudlibc/src/libc/stdio/renameat.c cloudlibc/src/libc/stdlib/_Exit.c cloudlibc/src/libc/sys/ioctl/ioctl.c cloudlibc/src/libc/sys/select/pselect.c cloudlibc/src/libc/sys/select/select.c cloudlibc/src/libc/sys/stat/fstat.c + cloudlibc/src/libc/sys/stat/fstatat.c cloudlibc/src/libc/sys/stat/futimens.c + cloudlibc/src/libc/sys/stat/mkdirat.c + cloudlibc/src/libc/sys/stat/utimensat.c cloudlibc/src/libc/sys/time/gettimeofday.c cloudlibc/src/libc/sys/uio/preadv.c cloudlibc/src/libc/sys/uio/pwritev.c @@ -97,17 +95,22 @@ set(bottom_half_sources cloudlibc/src/libc/time/clock_nanosleep.c cloudlibc/src/libc/time/nanosleep.c cloudlibc/src/libc/time/time.c + cloudlibc/src/libc/unistd/faccessat.c cloudlibc/src/libc/unistd/fdatasync.c cloudlibc/src/libc/unistd/fsync.c cloudlibc/src/libc/unistd/ftruncate.c + cloudlibc/src/libc/unistd/linkat.c cloudlibc/src/libc/unistd/lseek.c cloudlibc/src/libc/unistd/pread.c cloudlibc/src/libc/unistd/pwrite.c cloudlibc/src/libc/unistd/read.c + cloudlibc/src/libc/unistd/readlinkat.c cloudlibc/src/libc/unistd/sleep.c + cloudlibc/src/libc/unistd/symlinkat.c cloudlibc/src/libc/unistd/unlinkat.c cloudlibc/src/libc/unistd/usleep.c cloudlibc/src/libc/unistd/write.c + sources/__cabi_realloc_wrapper.S sources/__errno_location.c sources/__main_void.c sources/__wasilibc_dt.c @@ -117,7 +120,9 @@ set(bottom_half_sources sources/__wasilibc_random.c sources/__wasilibc_real.c sources/__wasilibc_report_error.c + sources/__wasilibc_rmdirat.c sources/__wasilibc_tell.c + sources/__wasilibc_unlinkat.c sources/abort.c sources/at_fdcwd.c sources/chdir.c @@ -138,28 +143,8 @@ set(bottom_half_sources sources/sbrk.c sources/truncate.c sources/utf8.c - sources/__cabi_realloc_wrapper.S ) -# On webp2, all of the functions that would call these files will trap. -if (NOT WEB STREQUAL "p2") - list(APPEND bottom_half_sources - cloudlibc/src/libc/dirent/opendirat.c - cloudlibc/src/libc/dirent/scandirat.c - cloudlibc/src/libc/fcntl/openat.c - cloudlibc/src/libc/stdio/renameat.c - cloudlibc/src/libc/sys/stat/fstatat.c - cloudlibc/src/libc/sys/stat/mkdirat.c - cloudlibc/src/libc/sys/stat/utimensat.c - cloudlibc/src/libc/unistd/faccessat.c - cloudlibc/src/libc/unistd/linkat.c - cloudlibc/src/libc/unistd/readlinkat.c - cloudlibc/src/libc/unistd/symlinkat.c - sources/__wasilibc_rmdirat.c - sources/__wasilibc_unlinkat.c - ) -endif() - if (WASI STREQUAL "p1") list(APPEND bottom_half_sources cloudlibc/src/libc/sys/socket/getsockopt.c @@ -242,11 +227,6 @@ foreach(obj bottom-half-shared bottom-half-static) cloudlibc/src headers/private ) - if (WEB STREQUAL "p2") - # Many functions are unsupported on webp2 and trap without using their - # parameters. - target_compile_options(${obj} PRIVATE -Wno-unused-parameter) - endif() endforeach() diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c index 690a9f302..5fb5831b7 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c @@ -3,19 +3,10 @@ // SPDX-License-Identifier: BSD-2-Clause #include -#include - -#if defined(__webp2__) #include +#include #include -#endif int closedir(DIR *dirp) { -#if !defined(__webp2__) return close(fdclosedir(dirp)); -#else - WASILIBC_LOG_UNSUPPORTED("closedir"); - errno = ENOSYS; - return -1; -#endif } diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/dirent_impl.h b/libc-bottom-half/cloudlibc/src/libc/dirent/dirent_impl.h index 818cb225c..aa9a16c72 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/dirent_impl.h +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/dirent_impl.h @@ -5,8 +5,9 @@ #ifndef DIRENT_DIRENT_IMPL_H #define DIRENT_DIRENT_IMPL_H -#include +#include <__typedef_DIR.h> #include +#include struct dirent; diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/dirfd.c b/libc-bottom-half/cloudlibc/src/libc/dirent/dirfd.c index e085e0b5e..033157a4e 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/dirfd.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/dirfd.c @@ -6,17 +6,6 @@ #include "dirent_impl.h" -#if defined(__webp2__) -#include -#include -#endif - int dirfd(DIR *dirp) { -#if !defined(__webp2__) return dirp->fd; -#else - WASILIBC_LOG_UNSUPPORTED("dirfd"); - errno = ENOSYS; - return -1; -#endif } diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c index 8222ae05b..785507d25 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c @@ -3,17 +3,13 @@ // SPDX-License-Identifier: BSD-2-Clause #include +#include #include +#include #include "dirent_impl.h" -#if defined(__webp2__) -#include -#include -#endif - int fdclosedir(DIR *dirp) { -#if !defined(__webp2__) int fd = dirp->fd; #ifdef __wasip1__ free(dirp->buffer); @@ -22,9 +18,4 @@ int fdclosedir(DIR *dirp) { free(dirp->dirent); free(dirp); return fd; -#else - WASILIBC_LOG_UNSUPPORTED("fdclosedir"); - errno = ENOSYS; - return -1; -#endif // !webp2 } diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c index 3b4ec3e44..04676ba60 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c @@ -6,16 +6,12 @@ #include #include #include +#include #ifndef __wasip1__ #include #include #include -#else -#endif - -#if defined(__webp2__) -#include #endif #include "dirent_impl.h" @@ -90,6 +86,7 @@ DIR *fdopendir(int fd) { dirp = NULL; return ret; #elif defined(__webp2__) + (void)fd; WASILIBC_LOG_UNSUPPORTED("fdopendir"); errno = ENOSYS; return NULL; diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c index 93e7de1a0..34c262c0b 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c @@ -2,17 +2,18 @@ // // SPDX-License-Identifier: BSD-2-Clause -#include -#include -#include -#include #include -#include #include #include +#include #include #include #include +#include +#include +#include +#include +#include #ifndef __wasip1__ #include @@ -36,10 +37,6 @@ static_assert(DT_UNKNOWN == __WASI_FILETYPE_UNKNOWN, "Value mismatch"); #include #endif -#if defined(__webp2__) -#include -#endif - #ifndef __webp2__ // Grows a buffer to be large enough to hold a certain amount of data. static struct dirent* grow(struct dirent **buffer, size_t *buffer_size, size_t target_size) { @@ -334,6 +331,7 @@ struct dirent *readdir(DIR *dirp) { } #elif defined(__webp2__) struct dirent *readdir(DIR *dirp) { + (void)dirp; WASILIBC_LOG_UNSUPPORTED("readdir"); errno = EBADF; // It is impossible to open a DIR anyway. return NULL; diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/rewinddir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/rewinddir.c index 0bd0e1960..465e403f5 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/rewinddir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/rewinddir.c @@ -2,15 +2,12 @@ // // SPDX-License-Identifier: BSD-2-Clause -#include #include - -#include "dirent_impl.h" - -#if defined(__webp2__) #include +#include #include -#endif + +#include "dirent_impl.h" void rewinddir(DIR *dirp) { #if defined(__wasip1__) @@ -23,6 +20,7 @@ void rewinddir(DIR *dirp) { dirp->skip = 0; dirp->offset = 0; #elif defined(__webp2__) + (void)dirp; WASILIBC_LOG_UNSUPPORTED("rewinddir"); errno = ENOSYS; #else diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/seekdir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/seekdir.c index 76d5bc4df..25ba18248 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/seekdir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/seekdir.c @@ -3,13 +3,10 @@ // SPDX-License-Identifier: BSD-2-Clause #include +#include #include "dirent_impl.h" -#if defined(__webp2__) -#include -#endif - void seekdir(DIR *dirp, long loc) { #if defined(__wasip1__) // Update cookie. @@ -22,6 +19,8 @@ void seekdir(DIR *dirp, long loc) { dirp->skip = loc; dirp->offset = 0; #elif defined(__webp2__) + (void)dirp; + (void)loc; WASILIBC_LOG_UNSUPPORTED("seekdir"); #else # error "Unsupported WASI version" diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/telldir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/telldir.c index 8079c8590..97cc4e6c2 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/telldir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/telldir.c @@ -3,12 +3,10 @@ // SPDX-License-Identifier: BSD-2-Clause #include -#include "dirent_impl.h" - -#if defined(__webp2__) #include #include -#endif + +#include "dirent_impl.h" long telldir(DIR *dirp) { #if defined(__wasip1__) @@ -16,6 +14,7 @@ long telldir(DIR *dirp) { #elif defined(__wasip2__) || defined(__wasip3__) return dirp->offset + dirp->skip; #elif defined(__webp2__) + (void)dirp; WASILIBC_LOG_UNSUPPORTED("telldir"); errno = ENOSYS; return -1; diff --git a/libc-bottom-half/cloudlibc/src/libc/fcntl/openat.c b/libc-bottom-half/cloudlibc/src/libc/fcntl/openat.c index e0627eb01..797478c9a 100644 --- a/libc-bottom-half/cloudlibc/src/libc/fcntl/openat.c +++ b/libc-bottom-half/cloudlibc/src/libc/fcntl/openat.c @@ -3,13 +3,14 @@ // SPDX-License-Identifier: BSD-2-Clause #include -#include -#include #include #include #include +#include +#include +#include -#ifndef __wasip1__ +#if defined(__wasip2__) || defined(__wasip3__) #include #include #include @@ -164,6 +165,12 @@ int __wasilibc_nocwd_openat_nomode(int fd, const char *path, int oflag) { // Update the descriptor table with the new handle return __wasilibc_add_file(new_handle, oflag); +#elif defined(__webp2__) + (void)fd; + (void)path; + WASILIBC_LOG_UNSUPPORTED("__wasilibc_nocwd_openat_nomode"); + errno = (oflag & O_CREAT) ? EACCES : ENOENT; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fadvise.c b/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fadvise.c index bcabcef21..43cef07d0 100644 --- a/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fadvise.c +++ b/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fadvise.c @@ -25,10 +25,6 @@ static_assert(POSIX_FADV_WILLNEED == __WASI_ADVICE_WILLNEED, "Value mismatch"); #endif -#if defined(__webp2__) -#include -#endif - int posix_fadvise(int fd, off_t offset, off_t len, int advice) { if (offset < 0 || len < 0) return EINVAL; @@ -79,6 +75,8 @@ int posix_fadvise(int fd, off_t offset, off_t len, int advice) { } return 0; #elif defined(__webp2__) + (void)fd; + (void)advice; return 0; #else # error "Unsupported WASI version" diff --git a/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fallocate.c b/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fallocate.c index 12d198cbf..f03569b39 100644 --- a/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fallocate.c +++ b/libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fallocate.c @@ -2,26 +2,20 @@ // // SPDX-License-Identifier: BSD-2-Clause -#include #include #include - -#if defined(__webp2__) +#include #include -#endif int posix_fallocate(int fd, off_t offset, off_t len) { if (offset < 0 || len < 0) return EINVAL; #if defined(__wasip1__) return __wasi_fd_allocate(fd, offset, len); -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) (void) fd; - // Note: this operation isn't supported in wasip{2,3} - return ENOTSUP; -#elif defined(__webp2__) WASILIBC_LOG_UNSUPPORTED("posix_fallocate"); - return ENOSYS; + return ENOTSUP; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/stdio/renameat.c b/libc-bottom-half/cloudlibc/src/libc/stdio/renameat.c index 2d39157ed..485b94602 100644 --- a/libc-bottom-half/cloudlibc/src/libc/stdio/renameat.c +++ b/libc-bottom-half/cloudlibc/src/libc/stdio/renameat.c @@ -2,10 +2,11 @@ // // SPDX-License-Identifier: BSD-2-Clause -#include #include #include #include +#include +#include #ifndef __wasip1__ #include @@ -52,6 +53,14 @@ int __wasilibc_nocwd_renameat(int oldfd, const char *old, int newfd, const char translate_error(&error_code); return -1; } +#elif defined(__webp2__) + (void)oldfd; + (void)old; + (void)newfd; + (void)new; + WASILIBC_LOG_UNSUPPORTED("__wasilibc_nocwd_renameat"); + errno = ENOENT; + return -1; #else # error "Unknown WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c index 0cf6209d0..066bd801d 100644 --- a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c +++ b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c @@ -2,15 +2,12 @@ // // SPDX-License-Identifier: BSD-2-Clause -#include -#include #include <_/cdefs.h> #include #include - -#ifdef __webp2__ +#include +#include #include -#endif noreturn void _Exit(int status) { #if defined(__wasip1__) diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstat.c b/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstat.c index 3d2d8c721..8a2fee718 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstat.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstat.c @@ -7,14 +7,12 @@ #include #include -#ifndef __webp2__ -#include "stat_impl.h" -#endif - #if !defined(__wasip1__) #include #endif +#include "stat_impl.h" + int fstat(int fildes, struct stat *buf) { #if defined(__wasip1__) __wasi_filestat_t internal_stat; diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstatat.c b/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstatat.c index 878652a1b..1d618733e 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstatat.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/stat/fstatat.c @@ -9,6 +9,7 @@ #include #include #include +#include #ifndef __wasip1__ #include @@ -92,6 +93,10 @@ int __wasilibc_nocwd_fstatat(int fd, const char *restrict path, struct stat *res // Convert the internal data to an external struct to_public_stat(&metadata, &internal_stat, buf); return 0; +#elif defined(__webp2__) + WASILIBC_LOG_UNSUPPORTED("__wasilibc_nocwd_fstatat"); + errno = ENOENT; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/stat/futimens.c b/libc-bottom-half/cloudlibc/src/libc/sys/stat/futimens.c index dd712616e..b2b9639fd 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/stat/futimens.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/stat/futimens.c @@ -2,13 +2,11 @@ // // SPDX-License-Identifier: BSD-2-Clause +#include #include #include -#include - -#ifndef __webp2__ -#include "stat_impl.h" -#endif +#include +#include #ifndef __wasip1__ #include @@ -16,9 +14,7 @@ #include #endif -#if defined(__webp2__) -#include -#endif +#include "stat_impl.h" int futimens(int fd, const struct timespec *times) { #if defined(__wasip1__) @@ -63,6 +59,8 @@ int futimens(int fd, const struct timespec *times) { return -1; } #elif defined(__webp2__) + (void)fd; + (void)times; WASILIBC_LOG_UNSUPPORTED("futimens"); errno = ENOSYS; return -1; diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/stat/mkdirat.c b/libc-bottom-half/cloudlibc/src/libc/sys/stat/mkdirat.c index 3c35470b4..3f6c87aa5 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/stat/mkdirat.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/stat/mkdirat.c @@ -6,6 +6,7 @@ #include #include #include +#include #ifndef __wasip1__ #include @@ -43,6 +44,12 @@ int __wasilibc_nocwd_mkdirat_nomode(int fd, const char *path) { return -1; } return 0; +#elif defined(__webp2__) + (void)fd; + (void)path; + WASILIBC_LOG_UNSUPPORTED("__wasilibc_nocwd_mkdirat_nomode"); + errno = EACCES; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h b/libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h index c55075fc1..eab8b131f 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h +++ b/libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h @@ -126,6 +126,7 @@ static inline void to_public_stat(const filesystem_metadata_hash_value_t *metada } filesystem_descriptor_stat_free(in); } +#elif defined(__webp2__) #else # error "Unsupported WASI version" #endif @@ -209,6 +210,7 @@ static inline bool utimens_get_timestamps(const struct timespec *times, } return true; } +#elif defined(__webp2__) #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/stat/utimensat.c b/libc-bottom-half/cloudlibc/src/libc/sys/stat/utimensat.c index b113acfba..92c3b7e68 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/stat/utimensat.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/stat/utimensat.c @@ -2,12 +2,12 @@ // // SPDX-License-Identifier: BSD-2-Clause -#include -#include #include #include #include -#include "stat_impl.h" +#include +#include +#include #ifndef __wasip1__ #include @@ -15,6 +15,8 @@ #include #endif +#include "stat_impl.h" + int __wasilibc_nocwd_utimensat(int fd, const char *path, const struct timespec times[2], int flag) { #if defined(__wasip1__) @@ -77,6 +79,14 @@ int __wasilibc_nocwd_utimensat(int fd, const char *path, const struct timespec t translate_error(&error); return -1; } +#elif defined(__webp2__) + (void)fd; + (void)path; + (void)times; + (void)flag; + WASILIBC_LOG_UNSUPPORTED("__wasilibc_nocwd_utimensat"); + errno = ENOENT; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/uio/preadv.c b/libc-bottom-half/cloudlibc/src/libc/sys/uio/preadv.c index 0a5f7c3d4..0ff01f139 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/preadv.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/preadv.c @@ -2,10 +2,11 @@ // // SPDX-License-Identifier: BSD-2-Clause +#include #include #include #include -#include +#include #ifndef __wasip1__ #include @@ -13,10 +14,6 @@ #include #endif -#if defined(__webp2__) -#include -#endif - ssize_t preadv(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { if (iovcnt < 0 || offset < 0) { errno = EINVAL; @@ -30,7 +27,7 @@ ssize_t preadv(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { errno = error; return -1; } -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) // Skip empty iovecs and then delegate to `pread` with the first non-empty // iovec. while (iovcnt) { @@ -41,10 +38,6 @@ ssize_t preadv(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { iov++; } return pread(fildes, NULL, 0, offset); -#elif defined(__webp2__) - WASILIBC_LOG_UNSUPPORTED("preadv"); - errno = ENOSYS; - return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/uio/pwritev.c b/libc-bottom-half/cloudlibc/src/libc/sys/uio/pwritev.c index d95475bfb..4d7d63844 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/pwritev.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/pwritev.c @@ -2,10 +2,11 @@ // // SPDX-License-Identifier: BSD-2-Clause +#include #include #include -#include #include +#include #ifndef __wasip1__ #include @@ -13,10 +14,6 @@ #include #endif -#if defined(__webp2__) -#include -#endif - ssize_t pwritev(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { if (iovcnt < 0 || offset < 0) { errno = EINVAL; @@ -31,7 +28,7 @@ ssize_t pwritev(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { return -1; } return bytes_written; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) // Skip empty iovecs and then delegate to `pwrite` with the first non-empty // iovec. while (iovcnt) { @@ -42,10 +39,6 @@ ssize_t pwritev(int fildes, const struct iovec *iov, int iovcnt, off_t offset) { iov++; } return pwrite(fildes, NULL, 0, offset); -#elif defined(__webp2__) - WASILIBC_LOG_UNSUPPORTED("pwritev"); - errno = ENOSYS; - return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c b/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c index 9da66faf5..a53ff8705 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c @@ -2,12 +2,13 @@ // // SPDX-License-Identifier: BSD-2-Clause +#include #include #include -#include -#include #include #include +#include +#include #include #ifdef __wasip1__ @@ -15,10 +16,6 @@ static_assert(TIMER_ABSTIME == __WASI_SUBCLOCKFLAGS_SUBSCRIPTION_CLOCK_ABSTIME, "Value mismatch"); #endif -#if defined(__webp2__) -#include -#endif - int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, struct timespec *rmtp) { (void) rmtp; @@ -40,10 +37,10 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, __wasi_event_t ev; __wasi_errno_t error = __wasi_poll_oneoff(&sub, &ev, 1, &nevents); return error == 0 && ev.error == 0 ? 0 : ENOTSUP; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) if (clock_id != CLOCK_MONOTONIC) { - // wasip{2,3} only provide a pollable for monotonic clocks + // wasip{2,3} and webp2 only provide a pollable for monotonic clocks return ENOTSUP; } @@ -62,17 +59,20 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, poll_method_pollable_block(poll_borrow_pollable(pollable)); poll_pollable_drop_own(pollable); -#else // defined(__wasip3__) +#elif defined(__wasip3__) if (flags & TIMER_ABSTIME) { __wasilibc_subtask_await(monotonic_clock_wait_until(duration)); } else { __wasilibc_subtask_await(monotonic_clock_wait_for(duration)); } +#else // defined(__webp2__) + // Allow zero-length sleeps but otherwise return an error. + if (duration != 0) { + WASILIBC_LOG_UNSUPPORTED("clock_nanosleep"); + return ENOSYS; + } #endif return 0; -#elif defined(__webp2__) - WASILIBC_LOG_UNSUPPORTED("clock_nanosleep"); - return ENOTSUP; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c b/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c index bacca9c84..14d951e62 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c @@ -4,15 +4,11 @@ #include #include -#include - -#if defined(__webp2__) #include -#endif +#include int nanosleep(const struct timespec *rqtp, struct timespec *rem) { -#if !defined(__webp2__) -#if defined(__wasip2__) || defined(__wasip3__) +#if defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) // FIXME(WebAssembly/WASI#857): wasip2/p3 only supports the monotonic clock for // sleeping. clockid_t clock = CLOCK_MONOTONIC; @@ -25,9 +21,4 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rem) { return -1; } return 0; -#else - WASILIBC_LOG_UNSUPPORTED("nanosleep"); - errno = ENOSYS; - return -1; -#endif // !webp2 } diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/faccessat.c b/libc-bottom-half/cloudlibc/src/libc/unistd/faccessat.c index fe8cfbb2c..f67480cf7 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/faccessat.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/faccessat.c @@ -2,11 +2,12 @@ // // SPDX-License-Identifier: BSD-2-Clause -#include #include #include #include #include +#include +#include #ifndef __wasip1__ #include @@ -112,6 +113,12 @@ int __wasilibc_nocwd_faccessat(int fd, const char *path, int amode, int flag) { return -1; } } +#elif defined(__webp2__) + (void)fd; + (void)path; + WASILIBC_LOG_UNSUPPORTED("__wasilibc_nocwd_faccessat"); + errno = ENOENT; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/fdatasync.c b/libc-bottom-half/cloudlibc/src/libc/unistd/fdatasync.c index 52b6c79bd..c9dfd8024 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/fdatasync.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/fdatasync.c @@ -3,9 +3,10 @@ // SPDX-License-Identifier: BSD-2-Clause #include -#include #include #include +#include +#include #ifndef __wasip1__ #include @@ -13,10 +14,6 @@ #include #endif -#if defined(__webp2__) -#include -#endif - int fdatasync(int fildes) { #if defined(__wasip1__) __wasi_errno_t error = __wasi_fd_datasync(fildes); @@ -39,6 +36,7 @@ int fdatasync(int fildes) { return -1; } #elif defined(__webp2__) + (void)fildes; WASILIBC_LOG_UNSUPPORTED("fdatasync"); errno = EINVAL; return -1; diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/fsync.c b/libc-bottom-half/cloudlibc/src/libc/unistd/fsync.c index a9def7420..298713de8 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/fsync.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/fsync.c @@ -2,9 +2,10 @@ // // SPDX-License-Identifier: BSD-2-Clause -#include #include #include +#include +#include #ifndef __wasip1__ #include @@ -12,10 +13,6 @@ #include #endif -#if defined(__webp2__) -#include -#endif - int fsync(int fildes) { #if defined(__wasip1__) __wasi_errno_t error = __wasi_fd_sync(fildes); @@ -38,6 +35,7 @@ int fsync(int fildes) { return -1; } #elif defined(__webp2__) + (void)fildes; WASILIBC_LOG_UNSUPPORTED("fsync"); errno = EINVAL; return -1; diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/ftruncate.c b/libc-bottom-half/cloudlibc/src/libc/unistd/ftruncate.c index b356821b5..9d5dc744b 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/ftruncate.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/ftruncate.c @@ -2,9 +2,10 @@ // // SPDX-License-Identifier: BSD-2-Clause -#include #include #include +#include +#include #ifndef __wasip1__ #include @@ -12,10 +13,6 @@ #include #endif -#if defined(__webp2__) -#include -#endif - int ftruncate(int fildes, off_t length) { if (length < 0) { errno = EINVAL; @@ -43,6 +40,7 @@ int ftruncate(int fildes, off_t length) { return -1; } #elif defined(__webp2__) + (void)fildes; WASILIBC_LOG_UNSUPPORTED("ftruncate"); errno = ENOSYS; return -1; diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/linkat.c b/libc-bottom-half/cloudlibc/src/libc/unistd/linkat.c index 9c8d5bc48..6b6e5fb68 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/linkat.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/linkat.c @@ -2,11 +2,12 @@ // // SPDX-License-Identifier: BSD-2-Clause -#include #include #include #include #include +#include +#include #ifndef __wasip1__ #include @@ -61,6 +62,15 @@ int __wasilibc_nocwd_linkat(int fd1, const char *path1, int fd2, const char *pat translate_error(&error_code); return -1; } +#elif defined(__webp2__) + (void)fd1; + (void)path1; + (void)fd2; + (void)path2; + (void)flag; + WASILIBC_LOG_UNSUPPORTED("__wasilibc_nocwd_linkat"); + errno = ENOENT; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c b/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c index 1d7c5eff0..508b0f2d2 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c @@ -5,6 +5,7 @@ #include #include #include +#include #ifndef __wasip1__ #include @@ -17,10 +18,6 @@ #include #endif -#if defined(__webp2__) -#include -#endif - ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset) { if (offset < 0) { errno = EINVAL; @@ -112,6 +109,9 @@ ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset) { } return ret; #elif defined(__webp2__) + (void)fildes; + (void)buf; + (void)nbyte; WASILIBC_LOG_UNSUPPORTED("pread"); errno = ENOSYS; return -1; diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/pwrite.c b/libc-bottom-half/cloudlibc/src/libc/unistd/pwrite.c index f0025c445..0845dc8af 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/pwrite.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/pwrite.c @@ -2,9 +2,10 @@ // // SPDX-License-Identifier: BSD-2-Clause -#include #include #include +#include +#include #ifndef __wasip1__ #include @@ -16,10 +17,6 @@ #include #endif -#if defined(__webp2__) -#include -#endif - ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) { if (offset < 0) { errno = EINVAL; @@ -106,6 +103,9 @@ ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) { } return ret; #elif defined(__webp2__) + (void)fildes; + (void)buf; + (void)nbyte; WASILIBC_LOG_UNSUPPORTED("pwrite"); errno = ENOSYS; return -1; diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/readlinkat.c b/libc-bottom-half/cloudlibc/src/libc/unistd/readlinkat.c index ef0f021ef..5f9be590a 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/readlinkat.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/readlinkat.c @@ -6,6 +6,7 @@ #include #include #include +#include #ifndef __wasip1__ #include @@ -56,6 +57,14 @@ ssize_t __wasilibc_nocwd_readlinkat(int fd, const char *restrict path, char *res #else wasip3_string_free(&link_source); #endif +#elif defined(__webp2__) + (void)fd; + (void)path; + (void)buf; + (void)bufsize; + WASILIBC_LOG_UNSUPPORTED("__wasilibc_nocwd_readlinkat"); + errno = ENOENT; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c b/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c index d1086576f..094515979 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/sleep.c @@ -2,39 +2,34 @@ // // SPDX-License-Identifier: BSD-2-Clause +#include <__errno_values.h> #include #include #include -#include - -#if defined(__webp2__) #include -#endif +#include unsigned int sleep(unsigned int seconds) { -#if !defined(__webp2__) struct timespec ts = {.tv_sec = seconds, .tv_nsec = 0}; // FIXME(WebAssembly/WASI#857): wasip2/p3 only supports the monotonic clock for // sleeping. #if defined(__wasip1__) clockid_t clock_id = CLOCK_REALTIME; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) clockid_t clock_id = CLOCK_MONOTONIC; #else # error "Unsupported WASI version" #endif - if (clock_nanosleep(clock_id, 0, &ts, NULL) != 0) + int err = clock_nanosleep(clock_id, 0, &ts, NULL); +#ifdef __webp2__ + if (err == ENOSYS) { + // If we return `seconds` from this, we risk user code going into an + // infinite loop. Therefore we noisily exit. clock_nanosleep has already + // logged. + exit(1); + } +#endif + if (err != 0) return seconds; return 0; -#else - if (seconds == 0) { - return 0; - } - - // TODO(webp2): If we actually return from this, we risk user code going into - // an infinite loop as it fruitlessly tries to sleep the night away. - // Therefore we noisily exit. - __wasilibc_report_error("sleep is not supported"); - exit(1); -#endif // !webp2 } diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/symlinkat.c b/libc-bottom-half/cloudlibc/src/libc/unistd/symlinkat.c index 0dd45201e..8365ecaad 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/symlinkat.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/symlinkat.c @@ -6,6 +6,7 @@ #include #include #include +#include #ifndef __wasip1__ #include @@ -47,6 +48,13 @@ int __wasilibc_nocwd_symlinkat(const char *path1, int fd, const char *path2) { translate_error(&error_code); return -1; } +#elif defined(__webp2__) + (void)path1; + (void)fd; + (void)path2; + WASILIBC_LOG_UNSUPPORTED("__wasilibc_nocwd_symlinkat"); + errno = EACCES; + return -1; #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c b/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c index b400601c9..42668d97b 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c @@ -2,26 +2,17 @@ // // SPDX-License-Identifier: BSD-2-Clause -#include -#include #include #include #include #include - -#if defined(__webp2__) +#include +#include #include -#endif int unlinkat(int fd, const char *path, int flag) { -#if !defined(__webp2__) if ((flag & AT_REMOVEDIR) != 0) { return __wasilibc_rmdirat(fd, path); } return __wasilibc_unlinkat(fd, path); -#else - WASILIBC_LOG_UNSUPPORTED("unlinkat"); - errno = ENOENT; - return -1; -#endif } diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c b/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c index 9bc623bd3..332fbdaed 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c @@ -5,24 +5,18 @@ #include #include #include -#include - -#if defined(__webp2__) #include -#endif +#include int usleep(useconds_t useconds) { -#if !defined(__webp2__) struct timespec ts = {.tv_sec = useconds / 1000000, .tv_nsec = useconds % 1000000 * 1000}; #if defined(__wasip1__) clockid_t clock_id = CLOCK_REALTIME; -#elif defined(__wasip2__) || defined(__wasip3__) +#elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) // FIXME(WebAssembly/WASI#857): wasip2/p3 only supports the monotonic clock // for sleeping. clockid_t clock_id = CLOCK_MONOTONIC; -#else -# error "Unsupported WASI version" #endif int error = clock_nanosleep(clock_id, 0, &ts, NULL); if (error != 0) { @@ -30,9 +24,4 @@ int usleep(useconds_t useconds) { return -1; } return 0; -#else - WASILIBC_LOG_UNSUPPORTED("usleep"); - errno = ENOSYS; - return -1; -#endif // !webp2 } diff --git a/libc-bottom-half/headers/private/wasi/sockets_utils.h b/libc-bottom-half/headers/private/wasi/sockets_utils.h index 2e6726bdb..42b631cf8 100644 --- a/libc-bottom-half/headers/private/wasi/sockets_utils.h +++ b/libc-bottom-half/headers/private/wasi/sockets_utils.h @@ -5,7 +5,7 @@ #include #include -#ifndef __wasip1__ +#if !defined(__wasip1__) && !defined(__webp2__) #if defined(__wasip2__) typedef network_error_code_t sockets_error_code_t; @@ -104,6 +104,6 @@ const service_entry_t *__wasilibc_get_service_entry_by_name(const char *name); const service_entry_t * __wasilibc_get_service_entry_by_port(const uint16_t port); -#endif // not(__wasip1__) +#endif // !__wasip1__ && !__webp2__ #endif // __wasi_sockets_utils_h diff --git a/libc-bottom-half/headers/private/wasi/tcp.h b/libc-bottom-half/headers/private/wasi/tcp.h index b843d194f..4593ee31f 100644 --- a/libc-bottom-half/headers/private/wasi/tcp.h +++ b/libc-bottom-half/headers/private/wasi/tcp.h @@ -3,7 +3,7 @@ #include -#ifndef __wasip1__ +#if !defined(__wasip1__) && !defined(__webp2__) #include "lock.h" #include @@ -141,6 +141,6 @@ int __wasilibc_add_tcp_socket(sockets_own_tcp_socket_t socket, sockets_ip_address_family_t family, bool blocking); -#endif // not(__wasip1__) +#endif // !__wasip1__ && !__webp2__ #endif // WASI_TCP_H diff --git a/libc-bottom-half/headers/private/wasi/udp.h b/libc-bottom-half/headers/private/wasi/udp.h index 07112366a..26df80f86 100644 --- a/libc-bottom-half/headers/private/wasi/udp.h +++ b/libc-bottom-half/headers/private/wasi/udp.h @@ -3,7 +3,7 @@ #include -#ifndef __wasip1__ +#if !defined(__wasip1__) && !defined(__webp2__) #include @@ -21,6 +21,6 @@ int __wasilibc_add_udp_socket(sockets_own_udp_socket_t socket, sockets_ip_address_family_t family, bool blocking); -#endif // not(__wasip1__) +#endif // !__wasip1__ && !__webp2__ #endif // WASI_UDP_H diff --git a/libc-bottom-half/headers/public/wasi/libc-find-relpath.h b/libc-bottom-half/headers/public/wasi/libc-find-relpath.h index b519d4ef5..32bd72e70 100644 --- a/libc-bottom-half/headers/public/wasi/libc-find-relpath.h +++ b/libc-bottom-half/headers/public/wasi/libc-find-relpath.h @@ -5,6 +5,8 @@ extern "C" { #endif +#ifndef __webp2__ + /** * Look up the given `path`, relative to the cwd, in the preopened directory * map. If a suitable entry is found, then the file descriptor for that entry @@ -72,4 +74,6 @@ int __wasilibc_find_relpath_alloc(const char *path, const char **abs, } #endif +#endif // !__webp2__ + #endif diff --git a/libc-bottom-half/sources/__wasilibc_report_error.c b/libc-bottom-half/sources/__wasilibc_report_error.c index 0e214b89d..58a260ae2 100644 --- a/libc-bottom-half/sources/__wasilibc_report_error.c +++ b/libc-bottom-half/sources/__wasilibc_report_error.c @@ -2,11 +2,8 @@ #include #include #include - -#ifdef __webp2__ #include #include -#endif void __wasilibc_report_error(const char *message) { #if defined(__wasip1__) || defined(__wasip2__) || defined(__wasip3__) @@ -20,7 +17,7 @@ void __wasilibc_report_errorf(const char *format, ...) { va_list ap; va_start(ap, format); -#if defined(__wasi__) +#if defined(__wasip1__) || defined(__wasip2__) || defined(__wasip3__) vfprintf(stderr, format, ap); fputc('\n', stderr); #elif defined(__webp2__) diff --git a/libc-bottom-half/sources/__wasilibc_rmdirat.c b/libc-bottom-half/sources/__wasilibc_rmdirat.c index 98dd05d44..b8a37d7ac 100644 --- a/libc-bottom-half/sources/__wasilibc_rmdirat.c +++ b/libc-bottom-half/sources/__wasilibc_rmdirat.c @@ -1,6 +1,7 @@ #include #include #include +#include #ifndef __wasip1__ #include @@ -37,6 +38,12 @@ int __wasilibc_nocwd___wasilibc_rmdirat(int fd, const char *path) { translate_error(&error_code); return -1; } +#elif defined(__webp2__) + (void)fd; + (void)path; + WASILIBC_LOG_UNSUPPORTED("__wasilibc_nocwd___wasilibc_rmdirat"); + errno = ENOENT; + return -1; #else #error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/sources/__wasilibc_unlinkat.c b/libc-bottom-half/sources/__wasilibc_unlinkat.c index 36b1859ae..b9fe2ac10 100644 --- a/libc-bottom-half/sources/__wasilibc_unlinkat.c +++ b/libc-bottom-half/sources/__wasilibc_unlinkat.c @@ -1,6 +1,7 @@ #include #include #include +#include #ifndef __wasip1__ #include @@ -40,6 +41,12 @@ int __wasilibc_nocwd___wasilibc_unlinkat(int fd, const char *path) { } return 0; +#elif defined(__webp2__) + (void)fd; + (void)path; + WASILIBC_LOG_UNSUPPORTED("__wasilibc_nocwd___wasilibc_unlinkat"); + errno = ENOENT; + return -1; #else #error "Unknown WASI version" #endif diff --git a/libc-bottom-half/sources/abort.c b/libc-bottom-half/sources/abort.c index aa1315abe..ca52389b9 100644 --- a/libc-bottom-half/sources/abort.c +++ b/libc-bottom-half/sources/abort.c @@ -1,8 +1,5 @@ #include - -#ifdef __webp2__ #include -#endif void abort(void) { // wasm doesn't support signals, so just trap to halt the program. diff --git a/libc-bottom-half/sources/at_fdcwd.c b/libc-bottom-half/sources/at_fdcwd.c index d244f6792..18592b752 100644 --- a/libc-bottom-half/sources/at_fdcwd.c +++ b/libc-bottom-half/sources/at_fdcwd.c @@ -7,19 +7,15 @@ // function, which then calls the appropriate WASI function. #include +#include #include #include #include #include #include #include - -#if !defined(__webp2__) -#include -#else -#include #include -#endif +#include // If the platform doesn't define O_TMPFILE, we don't need to worry about it. #ifndef O_TMPFILE @@ -27,140 +23,85 @@ #endif int openat(int dirfd, const char *pathname, int flags, ...) { -#if !defined(__webp2__) - if (dirfd == AT_FDCWD || pathname[0] == '/') { + if(dirfd == AT_FDCWD || pathname[0] == '/') { return open(pathname, flags); } - + return __wasilibc_nocwd_openat_nomode(dirfd, pathname, flags); -#else - WASILIBC_LOG_UNSUPPORTED("openat"); - errno = (flags & O_CREAT) ? EACCES : ENOENT; - return -1; -#endif } int symlinkat(const char *target, int dirfd, const char *linkpath) { -#if !defined(__webp2__) - if (dirfd == AT_FDCWD || linkpath[0] == '/') { + if(dirfd == AT_FDCWD || linkpath[0] == '/') { return symlink(target, linkpath); } return __wasilibc_nocwd_symlinkat(target, dirfd, linkpath); -#else - WASILIBC_LOG_UNSUPPORTED("symlinkat"); - errno = EACCES; - return -1; -#endif } ssize_t readlinkat(int dirfd, const char *__restrict pathname, char *__restrict buf, size_t bufsiz) { -#if !defined(__webp2__) - if (dirfd == AT_FDCWD || pathname[0] == '/') { + if(dirfd == AT_FDCWD || pathname[0] == '/') { return readlink(pathname, buf, bufsiz); } return __wasilibc_nocwd_readlinkat(dirfd, pathname, buf, bufsiz); -#else - WASILIBC_LOG_UNSUPPORTED("readlinkat"); - errno = ENOENT; - return -1; -#endif } int mkdirat(int dirfd, const char *pathname, mode_t mode) { -#if !defined(__webp2__) - if (dirfd == AT_FDCWD || pathname[0] == '/') { + if(dirfd == AT_FDCWD || pathname[0] == '/') { return mkdir(pathname, mode); } return __wasilibc_nocwd_mkdirat_nomode(dirfd, pathname); -#else - WASILIBC_LOG_UNSUPPORTED("mkdirat"); - errno = EACCES; - return -1; -#endif } DIR *opendirat(int dirfd, const char *path) { -#if !defined(__webp2__) - if (dirfd == AT_FDCWD || path[0] == '/') { + if(dirfd == AT_FDCWD || path[0] == '/') { return opendir(path); } return __wasilibc_nocwd_opendirat(dirfd, path); -#else - WASILIBC_LOG_UNSUPPORTED("opendirat"); - errno = ENOENT; - return NULL; -#endif } int scandirat(int dirfd, const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) { -#if !defined(__webp2__) - if (dirfd == AT_FDCWD || dirp[0] == '/') { + if(dirfd == AT_FDCWD || dirp[0] == '/') { return scandir(dirp, namelist, filter, compar); } return __wasilibc_nocwd_scandirat(dirfd, dirp, namelist, filter, compar); -#else - WASILIBC_LOG_UNSUPPORTED("scandirat"); - errno = ENOENT; - return -1; -#endif } int faccessat(int dirfd, const char *pathname, int mode, int flags) { -#if !defined(__webp2__) - if (dirfd == AT_FDCWD || pathname[0] == '/') { + if(dirfd == AT_FDCWD || pathname[0] == '/') { return __wasilibc_access(pathname, mode, flags); } return __wasilibc_nocwd_faccessat(dirfd, pathname, mode, flags); -#else - WASILIBC_LOG_UNSUPPORTED("faccessat"); - errno = ENOENT; - return -1; -#endif } int fstatat(int dirfd, const char *__restrict pathname, struct stat *__restrict statbuf, int flags) { -#if !defined(__webp2__) - if (dirfd == AT_FDCWD || pathname[0] == '/') { + if(dirfd == AT_FDCWD || pathname[0] == '/') { return __wasilibc_stat(pathname, statbuf, flags); } return __wasilibc_nocwd_fstatat(dirfd, pathname, statbuf, flags); -#else - WASILIBC_LOG_UNSUPPORTED("fstatat"); - errno = ENOENT; - return -1; -#endif } int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) { -#if !defined(__webp2__) - if (dirfd == AT_FDCWD || pathname[0] == '/') { + if(dirfd == AT_FDCWD || pathname[0] == '/') { return __wasilibc_utimens(pathname, times, flags); } return __wasilibc_nocwd_utimensat(dirfd, pathname, times, flags); -#else - WASILIBC_LOG_UNSUPPORTED("utimensat"); - errno = ENOENT; - return -1; -#endif } int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) { -#if !defined(__webp2__) - if ((olddirfd == AT_FDCWD || oldpath[0] == '/') && + if((olddirfd == AT_FDCWD || oldpath[0] == '/') && (newdirfd == AT_FDCWD || newpath[0] == '/')) { return __wasilibc_link(oldpath, newpath, flags); } @@ -172,17 +113,11 @@ int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, } return __wasilibc_nocwd_linkat(olddirfd, oldpath, newdirfd, newpath, flags); -#else - WASILIBC_LOG_UNSUPPORTED("linkat"); - errno = ENOENT; - return -1; -#endif // !webp2 } int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) { -#if !defined(__webp2__) - if ((olddirfd == AT_FDCWD || oldpath[0] == '/') && + if((olddirfd == AT_FDCWD || oldpath[0] == '/') && (newdirfd == AT_FDCWD || newpath[0] == '/')) { return rename(oldpath, newpath); } @@ -194,37 +129,20 @@ int renameat(int olddirfd, const char *oldpath, int newdirfd, } return __wasilibc_nocwd_renameat(olddirfd, oldpath, newdirfd, newpath); -#else - WASILIBC_LOG_UNSUPPORTED("renameat"); - errno = ENOENT; - return -1; -#endif // !webp2 } int __wasilibc_unlinkat(int dirfd, const char *path) { -#if !defined(__webp2__) - if (dirfd == AT_FDCWD || path[0] == '/') { + if(dirfd == AT_FDCWD || path[0] == '/') { return unlink(path); } return __wasilibc_nocwd___wasilibc_unlinkat(dirfd, path); -#else - WASILIBC_LOG_UNSUPPORTED("__wasilibc_unlinkat"); - errno = ENOENT; - return -1; -#endif } int __wasilibc_rmdirat(int dirfd, const char *path) { -#if !defined(__webp2__) - if (dirfd == AT_FDCWD || path[0] == '/') { + if(dirfd == AT_FDCWD || path[0] == '/') { return rmdir(path); } return __wasilibc_nocwd___wasilibc_rmdirat(dirfd, path); -#else - WASILIBC_LOG_UNSUPPORTED("__wasilibc_rmdirat"); - errno = ENOENT; - return -1; -#endif } diff --git a/libc-bottom-half/sources/chdir.c b/libc-bottom-half/sources/chdir.c index 17ec7599b..dd3a0093b 100644 --- a/libc-bottom-half/sources/chdir.c +++ b/libc-bottom-half/sources/chdir.c @@ -8,12 +8,8 @@ #include #include #include - -#if !defined(__webp2__) #include -#else #include -#endif #ifndef __webp2__ DECLARE_WEAK_LOCK(__wasilibc_cwd_lock, extern); @@ -22,7 +18,7 @@ static int __wasilibc_cwd_mallocd = 0; #endif int chdir(const char *path) { -#if !defined(__webp2__) +#if defined(__wasip1__) || defined(__wasip2__) || defined(__wasip3__) static char *relative_buf = NULL; static size_t relative_buf_len = 0; @@ -76,11 +72,14 @@ int chdir(const char *path) { free(prev_cwd); __wasilibc_cwd_mallocd = 1; return 0; -#else +#elif defined(__webp2__) + (void)path; WASILIBC_LOG_UNSUPPORTED("chdir"); errno = ENOENT; return -1; -#endif // !webp2 +#else +#error "Unknown WASI version" +#endif } #ifndef __webp2__ diff --git a/libc-bottom-half/sources/getcwd.c b/libc-bottom-half/sources/getcwd.c index 147e7665b..48d875427 100644 --- a/libc-bottom-half/sources/getcwd.c +++ b/libc-bottom-half/sources/getcwd.c @@ -1,20 +1,17 @@ #include #include #include +#include #include "lock.h" -#if defined(__webp2__) -#include -#endif - #ifndef __webp2__ char *__wasilibc_cwd = "/"; DECLARE_WEAK_LOCK(__wasilibc_cwd_lock); #endif char *getcwd(char *buf, size_t size) { -#if !defined(__webp2__) +#if defined(__wasip1__) || defined(__wasip2__) || defined(__wasip3__) // Critical section contains no yield points, so we can use weak locks. WEAK_LOCK(__wasilibc_cwd_lock); if (!buf) { @@ -35,9 +32,13 @@ char *getcwd(char *buf, size_t size) { } WEAK_UNLOCK(__wasilibc_cwd_lock); return buf; -#else +#elif defined(__webp2__) + (void)buf; + (void)size; WASILIBC_LOG_UNSUPPORTED("getcwd"); errno = ENOENT; return NULL; -#endif // !webp2 +#else +#error "Unknown WASI version" +#endif } diff --git a/libc-bottom-half/sources/pipe.c b/libc-bottom-half/sources/pipe.c index d1bc0bbf6..ef1d02969 100644 --- a/libc-bottom-half/sources/pipe.c +++ b/libc-bottom-half/sources/pipe.c @@ -1,10 +1,7 @@ #include #include #include - -#if defined(__webp2__) #include -#endif #if defined(__wasip3__) @@ -498,13 +495,7 @@ static descriptor_vtable_t pipe_vtable = { #endif // __wasip3__ int pipe(int fd[2]) { -#if !defined(__webp2__) return pipe2(fd, 0); -#else - WASILIBC_LOG_UNSUPPORTED("pipe"); - errno = ENOTSUP; - return -1; -#endif } int pipe2(int fd[2], int flags) { @@ -512,11 +503,8 @@ int pipe2(int fd[2], int flags) { errno = EINVAL; return -1; } -#if defined(__wasip1__) || defined(__wasip2__) +#if defined(__wasip1__) || defined(__wasip2__) || defined(__webp2__) (void)fd; - errno = ENOTSUP; - return -1; -#elif defined(__webp2__) WASILIBC_LOG_UNSUPPORTED("pipe2"); errno = ENOTSUP; return -1; diff --git a/libc-bottom-half/sources/posix.c b/libc-bottom-half/sources/posix.c index dd5791223..9615f0b7a 100644 --- a/libc-bottom-half/sources/posix.c +++ b/libc-bottom-half/sources/posix.c @@ -10,14 +10,11 @@ #include #include #include -#include - -#if !defined(__webp2__) #include #include -#endif +#include -#ifndef __webp2__ +#if defined(__wasip1__) || defined(__wasip2__) || defined(__wasip3__) static int find_relpath2(const char *path, char **relative, size_t *relative_len) { // See comments in `preopens.c` for what this trick is doing. @@ -49,7 +46,19 @@ static int find_relpath_alt(const char *path, char **relative) { *relative = relative_buf; return fd; } -#endif // !webp2 +#elif defined(__webp2__) +static int find_relpath(const char *path, char **relative) { + (void)path; + (void)relative; + return -1; +} + +static int find_relpath_alt(const char *path, char **relative) { + (void)path; + (void)relative; + return -1; +} +#endif int open(const char *path, int oflag, ...) { // WASI libc's `openat` ignores the mode argument, so call a special @@ -59,10 +68,9 @@ int open(const char *path, int oflag, ...) { // See the documentation in libc.h int __wasilibc_open_nomode(const char *path, int oflag) { -#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); - + // If we can't find a preopen for it, fail as if we can't find the path. if (dirfd == -1) { errno = ENOENT; @@ -70,15 +78,9 @@ int __wasilibc_open_nomode(const char *path, int oflag) { } return __wasilibc_nocwd_openat_nomode(dirfd, relative_path, oflag); -#else - WASILIBC_LOG_UNSUPPORTED("open"); - errno = (oflag & O_CREAT) ? EACCES : ENOENT; - return -1; -#endif } int access(const char *path, int amode) { -#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -89,16 +91,10 @@ int access(const char *path, int amode) { } return __wasilibc_nocwd_faccessat(dirfd, relative_path, amode, 0); -#else - WASILIBC_LOG_UNSUPPORTED("access"); - errno = ENOENT; - return -1; -#endif } ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize) { -#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -109,15 +105,9 @@ ssize_t readlink(const char *restrict path, char *restrict buf, } return __wasilibc_nocwd_readlinkat(dirfd, relative_path, buf, bufsize); -#else - WASILIBC_LOG_UNSUPPORTED("readlink"); - errno = ENOENT; - return -1; -#endif } int stat(const char *restrict path, struct stat *restrict buf) { -#if !defined(__webp2__) if (path[0] == '\0') { errno = ENOENT; return -1; @@ -133,15 +123,9 @@ int stat(const char *restrict path, struct stat *restrict buf) { } return __wasilibc_nocwd_fstatat(dirfd, relative_path, buf, 0); -#else - WASILIBC_LOG_UNSUPPORTED("stat"); - errno = ENOENT; - return -1; -#endif } int lstat(const char *restrict path, struct stat *restrict buf) { -#if !defined(__webp2__) if (path[0] == '\0') { errno = ENOENT; return -1; @@ -158,15 +142,9 @@ int lstat(const char *restrict path, struct stat *restrict buf) { return __wasilibc_nocwd_fstatat(dirfd, relative_path, buf, AT_SYMLINK_NOFOLLOW); -#else - WASILIBC_LOG_UNSUPPORTED("lstat"); - errno = ENOENT; - return -1; -#endif } int utime(const char *path, const struct utimbuf *times) { -#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -182,15 +160,9 @@ int utime(const char *path, const struct utimbuf *times) { {.tv_sec = times->modtime}}) : NULL, 0); -#else - WASILIBC_LOG_UNSUPPORTED("utime"); - errno = ENOENT; - return -1; -#endif } int utimes(const char *path, const struct timeval times[2]) { -#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -209,15 +181,9 @@ int utimes(const char *path, const struct timeval times[2]) { }) : NULL, 0); -#else - WASILIBC_LOG_UNSUPPORTED("utimes"); - errno = ENOENT; - return -1; -#endif } int unlink(const char *path) { -#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -231,15 +197,9 @@ int unlink(const char *path) { // `AT_REMOVEDIR` isn't passed. Instead, use a specialized function which // just imports `__wasi_path_unlink_file`. return __wasilibc_nocwd___wasilibc_unlinkat(dirfd, relative_path); -#else - WASILIBC_LOG_UNSUPPORTED("unlink"); - errno = ENOENT; - return -1; -#endif } int rmdir(const char *path) { -#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -250,15 +210,9 @@ int rmdir(const char *path) { } return __wasilibc_nocwd___wasilibc_rmdirat(dirfd, relative_path); -#else - WASILIBC_LOG_UNSUPPORTED("rmdir"); - errno = ENOENT; - return -1; -#endif } int remove(const char *path) { -#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -280,15 +234,9 @@ int remove(const char *path) { errno = ENOENT; } return r; -#else - WASILIBC_LOG_UNSUPPORTED("remove"); - errno = ENOENT; - return -1; -#endif } int mkdir(const char *path, mode_t mode) { -#if !defined(__webp2__) (void)mode; char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -300,15 +248,9 @@ int mkdir(const char *path, mode_t mode) { } return __wasilibc_nocwd_mkdirat_nomode(dirfd, relative_path); -#else - WASILIBC_LOG_UNSUPPORTED("mkdir"); - errno = EACCES; - return -1; -#endif } DIR *opendir(const char *dirname) { -#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(dirname, &relative_path); @@ -319,17 +261,11 @@ DIR *opendir(const char *dirname) { } return __wasilibc_nocwd_opendirat(dirfd, relative_path); -#else - WASILIBC_LOG_UNSUPPORTED("opendir"); - errno = ENOENT; - return NULL; -#endif } int scandir(const char *restrict dir, struct dirent ***restrict namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) { -#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(dir, &relative_path); @@ -341,15 +277,9 @@ int scandir(const char *restrict dir, struct dirent ***restrict namelist, return __wasilibc_nocwd_scandirat(dirfd, relative_path, namelist, filter, compar); -#else - WASILIBC_LOG_UNSUPPORTED("scandir"); - errno = ENOENT; - return -1; -#endif } int symlink(const char *target, const char *linkpath) { -#if !defined(__webp2__) char *relative_path; int dirfd = find_relpath(linkpath, &relative_path); @@ -360,15 +290,9 @@ int symlink(const char *target, const char *linkpath) { } return __wasilibc_nocwd_symlinkat(target, dirfd, relative_path); -#else - WASILIBC_LOG_UNSUPPORTED("symlink"); - errno = EACCES; - return -1; -#endif } int link(const char *old, const char *new) { -#if !defined(__webp2__) char *old_relative_path; int old_dirfd = find_relpath_alt(old, &old_relative_path); @@ -384,15 +308,9 @@ int link(const char *old, const char *new) { // We couldn't find a preopen for it; fail as if we can't find the path. errno = ENOENT; return -1; -#else - WASILIBC_LOG_UNSUPPORTED("link"); - errno = ENOENT; - return -1; -#endif } int rename(const char *old, const char *new) { -#if !defined(__webp2__) char *old_relative_path; int old_dirfd = find_relpath_alt(old, &old_relative_path); @@ -408,11 +326,6 @@ int rename(const char *old, const char *new) { // We couldn't find a preopen for it; fail as if we can't find the path. errno = ENOENT; return -1; -#else - WASILIBC_LOG_UNSUPPORTED("rename"); - errno = ENOENT; - return -1; -#endif } int chmod(const char *path, mode_t mode) { @@ -472,7 +385,6 @@ int fstatvfs(int fd, struct statvfs *buf) { return -1; } -#if !defined(__webp2__) // Like `access`, but with `faccessat`'s flags argument. int __wasilibc_access(const char *path, int mode, int flags) { char *relative_path; @@ -598,4 +510,3 @@ int __wasilibc_rename_newat(const char *from, int todirfd, const char *to) { return __wasilibc_nocwd_renameat(from_dirfd, from_relative_path, todirfd, to); } -#endif // !webp2 diff --git a/libc-bottom-half/sources/ppoll.c b/libc-bottom-half/sources/ppoll.c index c3e75b6a7..536349334 100644 --- a/libc-bottom-half/sources/ppoll.c +++ b/libc-bottom-half/sources/ppoll.c @@ -5,15 +5,12 @@ #include #include #include +#include #if !defined(__wasip1__) #include #endif -#if defined(__webp2__) -#include -#endif - #if defined(__wasip1__) static int ppoll_impl(struct pollfd *fds, size_t nfds, const struct timespec *timeout) { diff --git a/libc-bottom-half/sources/preopens.c b/libc-bottom-half/sources/preopens.c index 67a3435e7..71cdf7d87 100644 --- a/libc-bottom-half/sources/preopens.c +++ b/libc-bottom-half/sources/preopens.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include #if defined(__wasip2__) || defined(__wasip3__) #include @@ -20,13 +22,7 @@ #include #endif -#if !defined(__webp2__) -#include -#else -#include -#endif - -#ifndef __webp2__ +#if defined(__wasip1__) || defined(__wasip2__) || defined(__wasip3__) /// Access to the the above preopen must be protected in the presence of /// threads. DECLARE_STRONG_LOCK(lock, static); @@ -311,10 +307,8 @@ int __wasilibc_find_abspath(const char *path, const char **abs_prefix, *relative_path = computed; return fd; } -#endif // !webp2 void __wasilibc_populate_preopens(void) { -#if !defined(__webp2__) // Fast path: If the preopens are already initialized, do nothing. if (preopens_populated) { return; @@ -404,13 +398,9 @@ void __wasilibc_populate_preopens(void) { #endif software: _Exit(EX_SOFTWARE); -#else - WASILIBC_LOG_UNSUPPORTED("__wasilibc_populate_preopens"); -#endif // !webp2 } void __wasilibc_reset_preopens(void) { -#if !defined(__webp2__) STRONG_LOCK(lock); if (num_preopens) { @@ -433,7 +423,15 @@ void __wasilibc_reset_preopens(void) { #endif STRONG_UNLOCK(lock); -#else +} +#elif defined(__webp2__) +void __wasilibc_populate_preopens(void) { + WASILIBC_LOG_UNSUPPORTED("__wasilibc_populate_preopens"); +} + +void __wasilibc_reset_preopens(void) { WASILIBC_LOG_UNSUPPORTED("__wasilibc_reset_preopens"); -#endif // !webp2 } +#else +#error "Unknown WASI version" +#endif diff --git a/libc-bottom-half/sources/socket.c b/libc-bottom-half/sources/socket.c index 0d2e606c3..e9f122d92 100644 --- a/libc-bottom-half/sources/socket.c +++ b/libc-bottom-half/sources/socket.c @@ -1,14 +1,10 @@ #include #include #include - -#if !defined(__webp2__) +#include #include #include #include -#else -#include -#endif #ifdef __wasip2__ #define sockets_static_tcp_socket_create tcp_create_socket_create_tcp_socket @@ -38,7 +34,7 @@ static int udp_socket(sockets_ip_address_family_t family, bool blocking) { #endif // !webp2 int socket(int domain, int type, int protocol) { -#if !defined(__webp2__) +#if defined(__wasip1__) || defined(__wasip2__) || defined(__wasip3__) sockets_ip_address_family_t family; switch (domain) { case PF_INET: @@ -69,9 +65,14 @@ int socket(int domain, int type, int protocol) { errno = EPROTONOSUPPORT; return -1; } -#else +#elif defined(__webp2__) + (void)domain; + (void)type; + (void)protocol; WASILIBC_LOG_UNSUPPORTED("socket"); errno = ENOSYS; return -1; -#endif // !webp2 +#else +#error "Unknown WASI version" +#endif } diff --git a/libc-bottom-half/sources/truncate.c b/libc-bottom-half/sources/truncate.c index c0f9748ca..4fb61bedf 100644 --- a/libc-bottom-half/sources/truncate.c +++ b/libc-bottom-half/sources/truncate.c @@ -3,12 +3,7 @@ #include #include -#if defined(__webp2__) -#include -#endif - int truncate(const char *path, off_t length) { -#if !defined(__webp2__) int fd = __wasilibc_open_nomode(path, O_WRONLY | O_CLOEXEC | O_NOCTTY); if (fd < 0) return -1; @@ -22,9 +17,4 @@ int truncate(const char *path, off_t length) { } return close(fd); -#else - WASILIBC_LOG_UNSUPPORTED("truncate"); - errno = ENOENT; - return -1; -#endif // !webp2 } diff --git a/libc-top-half/musl/src/stdio/fflush.c b/libc-top-half/musl/src/stdio/fflush.c index b1e77c2b7..7ca9ebe4b 100644 --- a/libc-top-half/musl/src/stdio/fflush.c +++ b/libc-top-half/musl/src/stdio/fflush.c @@ -1,10 +1,7 @@ #include "stdio_impl.h" #include "wasi/report-error.h" #include - -#ifdef __webp2__ #include -#endif /* stdout.c will override this if linked */ static FILE *volatile dummy = 0; From 2de4febe6f4ee1614ee10d165e677e335c0b8654 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 22 Oct 2026 09:39:26 -0600 Subject: [PATCH 79/96] Add unsupported logs to posix.c functions --- libc-bottom-half/sources/posix.c | 100 ++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/libc-bottom-half/sources/posix.c b/libc-bottom-half/sources/posix.c index 9615f0b7a..f8c99a705 100644 --- a/libc-bottom-half/sources/posix.c +++ b/libc-bottom-half/sources/posix.c @@ -47,12 +47,14 @@ static int find_relpath_alt(const char *path, char **relative) { return fd; } #elif defined(__webp2__) +// For webp2 we always return -1, which will always take these functions down +// the "no preopen" path, which means in just about every case we know we can +// just log that this function is unsupported. static int find_relpath(const char *path, char **relative) { (void)path; (void)relative; return -1; } - static int find_relpath_alt(const char *path, char **relative) { (void)path; (void)relative; @@ -68,6 +70,10 @@ int open(const char *path, int oflag, ...) { // See the documentation in libc.h int __wasilibc_open_nomode(const char *path, int oflag) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("__wasilibc_open_nomode"); +#endif + char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -81,6 +87,10 @@ int __wasilibc_open_nomode(const char *path, int oflag) { } int access(const char *path, int amode) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("access"); +#endif + char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -95,6 +105,10 @@ int access(const char *path, int amode) { ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("readlink"); +#endif + char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -108,6 +122,10 @@ ssize_t readlink(const char *restrict path, char *restrict buf, } int stat(const char *restrict path, struct stat *restrict buf) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("stat"); +#endif + if (path[0] == '\0') { errno = ENOENT; return -1; @@ -126,6 +144,10 @@ int stat(const char *restrict path, struct stat *restrict buf) { } int lstat(const char *restrict path, struct stat *restrict buf) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("lstat"); +#endif + if (path[0] == '\0') { errno = ENOENT; return -1; @@ -145,6 +167,10 @@ int lstat(const char *restrict path, struct stat *restrict buf) { } int utime(const char *path, const struct utimbuf *times) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("utime"); +#endif + char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -163,6 +189,10 @@ int utime(const char *path, const struct utimbuf *times) { } int utimes(const char *path, const struct timeval times[2]) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("utimes"); +#endif + char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -184,6 +214,10 @@ int utimes(const char *path, const struct timeval times[2]) { } int unlink(const char *path) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("unlink"); +#endif + char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -200,6 +234,10 @@ int unlink(const char *path) { } int rmdir(const char *path) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("rmdir"); +#endif + char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -213,6 +251,10 @@ int rmdir(const char *path) { } int remove(const char *path) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("remove"); +#endif + char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -237,6 +279,10 @@ int remove(const char *path) { } int mkdir(const char *path, mode_t mode) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("mkdir"); +#endif + (void)mode; char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -251,6 +297,10 @@ int mkdir(const char *path, mode_t mode) { } DIR *opendir(const char *dirname) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("opendir"); +#endif + char *relative_path; int dirfd = find_relpath(dirname, &relative_path); @@ -266,6 +316,10 @@ DIR *opendir(const char *dirname) { int scandir(const char *restrict dir, struct dirent ***restrict namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("scandir"); +#endif + char *relative_path; int dirfd = find_relpath(dir, &relative_path); @@ -280,6 +334,10 @@ int scandir(const char *restrict dir, struct dirent ***restrict namelist, } int symlink(const char *target, const char *linkpath) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("symlink"); +#endif + char *relative_path; int dirfd = find_relpath(linkpath, &relative_path); @@ -293,6 +351,10 @@ int symlink(const char *target, const char *linkpath) { } int link(const char *old, const char *new) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("link"); +#endif + char *old_relative_path; int old_dirfd = find_relpath_alt(old, &old_relative_path); @@ -311,6 +373,10 @@ int link(const char *old, const char *new) { } int rename(const char *old, const char *new) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("rename"); +#endif + char *old_relative_path; int old_dirfd = find_relpath_alt(old, &old_relative_path); @@ -387,6 +453,10 @@ int fstatvfs(int fd, struct statvfs *buf) { // Like `access`, but with `faccessat`'s flags argument. int __wasilibc_access(const char *path, int mode, int flags) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("__wasilibc_access"); +#endif + char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -402,6 +472,10 @@ int __wasilibc_access(const char *path, int mode, int flags) { // Like `utimensat`, but without the `at` part. int __wasilibc_utimens(const char *path, const struct timespec times[2], int flags) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("__waslibc_utimens"); +#endif + char *relative_path; int dirfd = find_relpath(path, &relative_path); @@ -417,6 +491,10 @@ int __wasilibc_utimens(const char *path, const struct timespec times[2], // Like `stat`, but with `fstatat`'s flags argument. int __wasilibc_stat(const char *__restrict path, struct stat *__restrict st, int flags) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("__wasilibc_stat"); +#endif + if (path[0] == '\0') { errno = ENOENT; return -1; @@ -436,6 +514,10 @@ int __wasilibc_stat(const char *__restrict path, struct stat *__restrict st, // Like `link`, but with `linkat`'s flags argument. int __wasilibc_link(const char *oldpath, const char *newpath, int flags) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("__wasilibc_link"); +#endif + char *old_relative_path; char *new_relative_path; int old_dirfd = find_relpath(oldpath, &old_relative_path); @@ -454,6 +536,10 @@ int __wasilibc_link(const char *oldpath, const char *newpath, int flags) { // Like `__wasilibc_link`, but oldpath is relative to olddirfd. int __wasilibc_link_oldat(int olddirfd, const char *oldpath, const char *newpath, int flags) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("__wasilibc_link_oldat"); +#endif + char *new_relative_path; int new_dirfd = find_relpath(newpath, &new_relative_path); @@ -470,6 +556,10 @@ int __wasilibc_link_oldat(int olddirfd, const char *oldpath, // Like `__wasilibc_link`, but newpath is relative to newdirfd. int __wasilibc_link_newat(const char *oldpath, int newdirfd, const char *newpath, int flags) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("__wasilibc_link_newat"); +#endif + char *old_relative_path; int old_dirfd = find_relpath(oldpath, &old_relative_path); @@ -485,6 +575,10 @@ int __wasilibc_link_newat(const char *oldpath, int newdirfd, // Like `rename`, but from is relative to fromdirfd. int __wasilibc_rename_oldat(int fromdirfd, const char *from, const char *to) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("__wasilibc_rename_oldat"); +#endif + char *to_relative_path; int to_dirfd = find_relpath(to, &to_relative_path); @@ -499,6 +593,10 @@ int __wasilibc_rename_oldat(int fromdirfd, const char *from, const char *to) { // Like `rename`, but to is relative to todirfd. int __wasilibc_rename_newat(const char *from, int todirfd, const char *to) { +#ifdef __webp2__ + WASILIBC_LOG_UNSUPPORTED("__wasilibc_rename_newat"); +#endif + char *from_relative_path; int from_dirfd = find_relpath(from, &from_relative_path); From 7796415d28f5c47bb7de91c6b083c23fc86cd5e4 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 22 Oct 2026 09:46:45 -0600 Subject: [PATCH 80/96] Restore original readv/writev implementations I didn't know they were supposed to be atomic. --- .../cloudlibc/src/libc/sys/uio/readv.c | 26 ++++++------------- .../cloudlibc/src/libc/sys/uio/writev.c | 26 ++++++++----------- .../headers/private/wasi/file_utils.h | 5 ---- libc-bottom-half/sources/file_utils.c | 19 -------------- 4 files changed, 19 insertions(+), 57 deletions(-) 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 d4593c640..13d93a4e0 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/readv.c @@ -10,7 +10,6 @@ #include #include #include -#include #ifdef __wasip1__ static_assert(offsetof(struct iovec, iov_base) == @@ -44,25 +43,16 @@ ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) { } return bytes_read; #elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) - if (!__wasilibc_iovecs_ok(iov, iovcnt, NULL)) { - errno = EINVAL; - return -1; - } - - size_t bytes_read = 0; - for (int i = 0; i < iovcnt; i++) { - const struct iovec *v = &iov[i]; - ssize_t res = read(fildes, v->iov_base, v->iov_len); - if (res < 0) { - // errno already set - return res; - } - bytes_read += res; - if ((size_t)res < v->iov_len) { - break; + // Skip empty iovecs and then delegate to `read` with the first non-empty + // iovec. + while (iovcnt) { + if (iov->iov_len != 0) { + return read(fildes, iov->iov_base, iov->iov_len); } + iovcnt--; + iov++; } - return bytes_read; + return read(fildes, NULL, 0); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/uio/writev.c b/libc-bottom-half/cloudlibc/src/libc/sys/uio/writev.c index 70d62b307..3a0bf1528 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/uio/writev.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/uio/writev.c @@ -3,15 +3,15 @@ // SPDX-License-Identifier: BSD-2-Clause #include + #include -#include #include #include #include #include +#include -#if !defined(__wasip1__) -#include +#ifndef __wasip1__ #include #include #endif @@ -49,20 +49,16 @@ ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) { } return bytes_written; #elif defined(__wasip2__) || defined(__wasip3__) || defined(__webp2__) - size_t total_size; - if (!__wasilibc_iovecs_ok(iov, iovcnt, &total_size)) { - errno = EINVAL; - return -1; - } - for (int i = 0; i < iovcnt; i++) { - const struct iovec *v = &iov[i]; - int res = write(fildes, v->iov_base, v->iov_len); - if (res < 0) { - // errno already set - return res; + // Skip empty iovecs and then delegate to `read` with the first non-empty + // iovec. + while (iovcnt) { + if (iov->iov_len != 0) { + return write(fildes, iov->iov_base, iov->iov_len); } + iovcnt--; + iov++; } - return total_size; + return write(fildes, NULL, 0); #else # error "Unsupported WASI version" #endif diff --git a/libc-bottom-half/headers/private/wasi/file_utils.h b/libc-bottom-half/headers/private/wasi/file_utils.h index d4a587e5d..10a97ed27 100644 --- a/libc-bottom-half/headers/private/wasi/file_utils.h +++ b/libc-bottom-half/headers/private/wasi/file_utils.h @@ -7,11 +7,6 @@ #include #include -// Check if the total size of all iovecs would overflow an ssize_t. -// `total_size` can be NULL. -hidden bool __wasilibc_iovecs_ok(const struct iovec *iov, int iovcnt, - size_t *total_size); - #if defined(__wasip2__) || defined(__wasip3__) #include diff --git a/libc-bottom-half/sources/file_utils.c b/libc-bottom-half/sources/file_utils.c index d7c68ff11..b5c199edc 100644 --- a/libc-bottom-half/sources/file_utils.c +++ b/libc-bottom-half/sources/file_utils.c @@ -9,25 +9,6 @@ #include #include -bool __wasilibc_iovecs_ok(const struct iovec *iov, int iovcnt, - size_t *total_size) { - size_t _total_size = 0; - if (total_size) { - *total_size = 0; - } - for (int i = 0; i < iovcnt; i++) { - const struct iovec *v = &iov[i]; - if (v->iov_len > SSIZE_MAX || _total_size > SSIZE_MAX - v->iov_len) { - return false; - } - _total_size += v->iov_len; - } - if (total_size) { - *total_size = _total_size; - } - return true; -} - #if defined(__wasip2__) || defined(__wasip3__) int wasi_string_from_c(const char *s, wasi_string_t *out) { int len = __wasilibc_validate_utf8(s); From d37b135825bb0638ea423496f5001dd984c335d7 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 22 Oct 2026 09:55:22 -0600 Subject: [PATCH 81/96] Update symbols --- expected/wasm32-wasip2/defined-symbols.txt | 1 - .../wasm32-wasip3-coop/defined-symbols.txt | 1 - expected/wasm32-wasip3/defined-symbols.txt | 1 - expected/wasm32-webp2/defined-symbols.txt | 22 ++++++++++++++++++- expected/wasm32-webp2/include-all.c | 5 +++++ expected/wasm32-webp2/predefined-macros.txt | 5 +++++ 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/expected/wasm32-wasip2/defined-symbols.txt b/expected/wasm32-wasip2/defined-symbols.txt index a258763a1..63e92e5ab 100644 --- a/expected/wasm32-wasip2/defined-symbols.txt +++ b/expected/wasm32-wasip2/defined-symbols.txt @@ -328,7 +328,6 @@ __wasilibc_getsockopt_timeout __wasilibc_iftodt __wasilibc_init_stdio __wasilibc_initialize_environ -__wasilibc_iovecs_ok __wasilibc_link __wasilibc_link_newat __wasilibc_link_oldat diff --git a/expected/wasm32-wasip3-coop/defined-symbols.txt b/expected/wasm32-wasip3-coop/defined-symbols.txt index 56e54fde6..f7974a92f 100644 --- a/expected/wasm32-wasip3-coop/defined-symbols.txt +++ b/expected/wasm32-wasip3-coop/defined-symbols.txt @@ -355,7 +355,6 @@ __wasilibc_getsockopt_timeout __wasilibc_iftodt __wasilibc_init_stdio __wasilibc_initialize_environ -__wasilibc_iovecs_ok __wasilibc_link __wasilibc_link_newat __wasilibc_link_oldat diff --git a/expected/wasm32-wasip3/defined-symbols.txt b/expected/wasm32-wasip3/defined-symbols.txt index 0342f51d7..98c813758 100644 --- a/expected/wasm32-wasip3/defined-symbols.txt +++ b/expected/wasm32-wasip3/defined-symbols.txt @@ -329,7 +329,6 @@ __wasilibc_getsockopt_timeout __wasilibc_iftodt __wasilibc_init_stdio __wasilibc_initialize_environ -__wasilibc_iovecs_ok __wasilibc_link __wasilibc_link_newat __wasilibc_link_oldat diff --git a/expected/wasm32-webp2/defined-symbols.txt b/expected/wasm32-webp2/defined-symbols.txt index a9f4f3db5..933359bc1 100644 --- a/expected/wasm32-webp2/defined-symbols.txt +++ b/expected/wasm32-webp2/defined-symbols.txt @@ -304,6 +304,7 @@ __uselocale __utc __wait __wake +__wasilibc_access __wasilibc_deinitialize_environ __wasilibc_descriptor_deallocate __wasilibc_dttoif @@ -315,21 +316,40 @@ __wasilibc_get_environ __wasilibc_iftodt __wasilibc_init_stdio __wasilibc_initialize_environ -__wasilibc_iovecs_ok +__wasilibc_link +__wasilibc_link_newat +__wasilibc_link_oldat __wasilibc_maybe_reinitialize_environ_eagerly +__wasilibc_nocwd___wasilibc_rmdirat +__wasilibc_nocwd___wasilibc_unlinkat +__wasilibc_nocwd_faccessat +__wasilibc_nocwd_fstatat +__wasilibc_nocwd_linkat +__wasilibc_nocwd_mkdirat_nomode +__wasilibc_nocwd_openat_nomode +__wasilibc_nocwd_opendirat +__wasilibc_nocwd_readlinkat +__wasilibc_nocwd_renameat +__wasilibc_nocwd_scandirat +__wasilibc_nocwd_symlinkat +__wasilibc_nocwd_utimensat __wasilibc_open_nomode __wasilibc_populate_preopens __wasilibc_pthread_self __wasilibc_random +__wasilibc_rename_newat +__wasilibc_rename_oldat __wasilibc_report_error __wasilibc_report_errorf __wasilibc_reset_preopens __wasilibc_rmdirat +__wasilibc_stat __wasilibc_tell __wasilibc_unlinkat __wasilibc_utf8_abort_current __wasilibc_utf8_reset __wasilibc_utf8_write +__wasilibc_utimens __wasilibc_validate_utf8 __wasilibc_webp2_fflush __wasilibc_webp2_flush_and_close_stdio diff --git a/expected/wasm32-webp2/include-all.c b/expected/wasm32-webp2/include-all.c index 339c98a6e..169132198 100644 --- a/expected/wasm32-webp2/include-all.c +++ b/expected/wasm32-webp2/include-all.c @@ -168,8 +168,13 @@ #include #include #include +#include +#include #include #include +#include +#include +#include #include #include #include diff --git a/expected/wasm32-webp2/predefined-macros.txt b/expected/wasm32-webp2/predefined-macros.txt index de4fa2d59..6df75c807 100644 --- a/expected/wasm32-webp2/predefined-macros.txt +++ b/expected/wasm32-webp2/predefined-macros.txt @@ -2924,6 +2924,8 @@ #define __USER_LABEL_PREFIX__ #define __USE_TIME_BITS64 1 #define __WASI_VERSION_H +#define __WASI_WASIP2_H +#define __WASI_WASIP3_H #define __WASI_WEBP2_H #define __WCHAR_TYPE__ int #define __WCHAR_WIDTH__ 32 @@ -2960,7 +2962,10 @@ #define __wasi_api_h #define __wasi_libc_busywait_h #define __wasi_libc_environ_h +#define __wasi_libc_find_relpath_h #define __wasi_libc_h +#define __wasi_libc_nocwd_h +#define __wasi_wasip1_h #define __wasilibc___errno_values_h #define __wasilibc___fd_set_h #define __wasilibc___function___isatty_h From 338a4f3ce4001340e34161d31edde3c2a90f211c Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 22 Oct 2026 10:49:16 -0600 Subject: [PATCH 82/96] Fix formatting / misc. feedback --- libc-bottom-half/CMakeLists.txt | 14 +++++---- .../cloudlibc/src/libc/dirent/closedir.c | 2 -- .../cloudlibc/src/libc/dirent/fdclosedir.c | 2 -- .../cloudlibc/src/libc/time/nanosleep.c | 1 - .../cloudlibc/src/libc/unistd/unlinkat.c | 1 - .../cloudlibc/src/libc/unistd/usleep.c | 1 - libc-bottom-half/crt/crt1-command.c | 2 +- .../headers/private/wasi/file_utils.h | 4 --- libc-bottom-half/sources/at_fdcwd.c | 30 +++++++++---------- libc-bottom-half/sources/chdir.c | 2 +- libc-bottom-half/sources/pipe.c | 4 +-- libc-bottom-half/sources/posix.c | 4 +-- libc-bottom-half/sources/ppoll.c | 4 +-- libc-bottom-half/sources/preopens.c | 2 +- libc-top-half/musl/src/env/__init_tls.c | 3 -- libc-top-half/musl/src/stdio/fflush.c | 2 -- test/CMakeLists.txt | 8 ++--- 17 files changed, 33 insertions(+), 53 deletions(-) diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index c5c00eefb..5a142f015 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -110,7 +110,6 @@ set(bottom_half_sources cloudlibc/src/libc/unistd/unlinkat.c cloudlibc/src/libc/unistd/usleep.c cloudlibc/src/libc/unistd/write.c - sources/__cabi_realloc_wrapper.S sources/__errno_location.c sources/__main_void.c sources/__wasilibc_dt.c @@ -143,6 +142,7 @@ set(bottom_half_sources sources/sbrk.c sources/truncate.c sources/utf8.c + sources/__cabi_realloc_wrapper.S ) if (WASI STREQUAL "p1") @@ -195,11 +195,6 @@ if (WASI STREQUAL "p3") sources/wasip3_block_on.c sources/wasip3_stdio.c ) - if (HAVE_WASM_LIBCALL_THREAD_CONTEXT) - list(APPEND bottom_half_sources sources/__wasm_task_hook.S - sources/wasip3_tls.c - ) - endif() endif() # webp2-specific @@ -210,6 +205,13 @@ if (WEB STREQUAL "p2") ) endif() +if (HAVE_WASM_LIBCALL_THREAD_CONTEXT) + list(APPEND bottom_half_sources + sources/__wasm_task_hook.S + sources/wasip3_tls.c + ) +endif() + # Don't export symbols of generated code in shared libraries, so specifically # pass `-fvisibility=hidden`. set_source_files_properties(sources/wasip2.c PROPERTIES diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c index 5fb5831b7..358573750 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/closedir.c @@ -3,9 +3,7 @@ // SPDX-License-Identifier: BSD-2-Clause #include -#include #include -#include int closedir(DIR *dirp) { return close(fdclosedir(dirp)); diff --git a/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c b/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c index 785507d25..03765388f 100644 --- a/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c +++ b/libc-bottom-half/cloudlibc/src/libc/dirent/fdclosedir.c @@ -3,9 +3,7 @@ // SPDX-License-Identifier: BSD-2-Clause #include -#include #include -#include #include "dirent_impl.h" diff --git a/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c b/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c index 14d951e62..ffdc9d526 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/nanosleep.c @@ -4,7 +4,6 @@ #include #include -#include #include int nanosleep(const struct timespec *rqtp, struct timespec *rem) { diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c b/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c index 42668d97b..7891c1b3c 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/unlinkat.c @@ -8,7 +8,6 @@ #include #include #include -#include int unlinkat(int fd, const char *path, int flag) { if ((flag & AT_REMOVEDIR) != 0) { diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c b/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c index 332fbdaed..fa1bbbf8e 100644 --- a/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/usleep.c @@ -5,7 +5,6 @@ #include #include #include -#include #include int usleep(useconds_t useconds) { diff --git a/libc-bottom-half/crt/crt1-command.c b/libc-bottom-half/crt/crt1-command.c index cb7f84c30..6caac0f16 100644 --- a/libc-bottom-half/crt/crt1-command.c +++ b/libc-bottom-half/crt/crt1-command.c @@ -1,10 +1,10 @@ -#include "wasi/report-error.h" #ifdef _REENTRANT #include #endif #include #include +#include extern void __wasm_call_ctors(void); extern int __main_void(void); diff --git a/libc-bottom-half/headers/private/wasi/file_utils.h b/libc-bottom-half/headers/private/wasi/file_utils.h index 10a97ed27..38624c63e 100644 --- a/libc-bottom-half/headers/private/wasi/file_utils.h +++ b/libc-bottom-half/headers/private/wasi/file_utils.h @@ -1,10 +1,6 @@ #ifndef __wasi_file_utils_h #define __wasi_file_utils_h -#include -#include -#include -#include #include #if defined(__wasip2__) || defined(__wasip3__) diff --git a/libc-bottom-half/sources/at_fdcwd.c b/libc-bottom-half/sources/at_fdcwd.c index 18592b752..f28d209ab 100644 --- a/libc-bottom-half/sources/at_fdcwd.c +++ b/libc-bottom-half/sources/at_fdcwd.c @@ -13,9 +13,9 @@ #include #include #include +#include #include #include -#include // If the platform doesn't define O_TMPFILE, we don't need to worry about it. #ifndef O_TMPFILE @@ -23,15 +23,15 @@ #endif int openat(int dirfd, const char *pathname, int flags, ...) { - if(dirfd == AT_FDCWD || pathname[0] == '/') { + if (dirfd == AT_FDCWD || pathname[0] == '/') { return open(pathname, flags); } - + return __wasilibc_nocwd_openat_nomode(dirfd, pathname, flags); } int symlinkat(const char *target, int dirfd, const char *linkpath) { - if(dirfd == AT_FDCWD || linkpath[0] == '/') { + if (dirfd == AT_FDCWD || linkpath[0] == '/') { return symlink(target, linkpath); } @@ -40,7 +40,7 @@ int symlinkat(const char *target, int dirfd, const char *linkpath) { ssize_t readlinkat(int dirfd, const char *__restrict pathname, char *__restrict buf, size_t bufsiz) { - if(dirfd == AT_FDCWD || pathname[0] == '/') { + if (dirfd == AT_FDCWD || pathname[0] == '/') { return readlink(pathname, buf, bufsiz); } @@ -48,7 +48,7 @@ ssize_t readlinkat(int dirfd, const char *__restrict pathname, } int mkdirat(int dirfd, const char *pathname, mode_t mode) { - if(dirfd == AT_FDCWD || pathname[0] == '/') { + if (dirfd == AT_FDCWD || pathname[0] == '/') { return mkdir(pathname, mode); } @@ -56,7 +56,7 @@ int mkdirat(int dirfd, const char *pathname, mode_t mode) { } DIR *opendirat(int dirfd, const char *path) { - if(dirfd == AT_FDCWD || path[0] == '/') { + if (dirfd == AT_FDCWD || path[0] == '/') { return opendir(path); } @@ -66,7 +66,7 @@ DIR *opendirat(int dirfd, const char *path) { int scandirat(int dirfd, const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) { - if(dirfd == AT_FDCWD || dirp[0] == '/') { + if (dirfd == AT_FDCWD || dirp[0] == '/') { return scandir(dirp, namelist, filter, compar); } @@ -74,7 +74,7 @@ int scandirat(int dirfd, const char *dirp, struct dirent ***namelist, } int faccessat(int dirfd, const char *pathname, int mode, int flags) { - if(dirfd == AT_FDCWD || pathname[0] == '/') { + if (dirfd == AT_FDCWD || pathname[0] == '/') { return __wasilibc_access(pathname, mode, flags); } @@ -83,7 +83,7 @@ int faccessat(int dirfd, const char *pathname, int mode, int flags) { int fstatat(int dirfd, const char *__restrict pathname, struct stat *__restrict statbuf, int flags) { - if(dirfd == AT_FDCWD || pathname[0] == '/') { + if (dirfd == AT_FDCWD || pathname[0] == '/') { return __wasilibc_stat(pathname, statbuf, flags); } @@ -92,7 +92,7 @@ int fstatat(int dirfd, const char *__restrict pathname, int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) { - if(dirfd == AT_FDCWD || pathname[0] == '/') { + if (dirfd == AT_FDCWD || pathname[0] == '/') { return __wasilibc_utimens(pathname, times, flags); } @@ -101,7 +101,7 @@ int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) { - if((olddirfd == AT_FDCWD || oldpath[0] == '/') && + if ((olddirfd == AT_FDCWD || oldpath[0] == '/') && (newdirfd == AT_FDCWD || newpath[0] == '/')) { return __wasilibc_link(oldpath, newpath, flags); } @@ -117,7 +117,7 @@ int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) { - if((olddirfd == AT_FDCWD || oldpath[0] == '/') && + if ((olddirfd == AT_FDCWD || oldpath[0] == '/') && (newdirfd == AT_FDCWD || newpath[0] == '/')) { return rename(oldpath, newpath); } @@ -132,7 +132,7 @@ int renameat(int olddirfd, const char *oldpath, int newdirfd, } int __wasilibc_unlinkat(int dirfd, const char *path) { - if(dirfd == AT_FDCWD || path[0] == '/') { + if (dirfd == AT_FDCWD || path[0] == '/') { return unlink(path); } @@ -140,7 +140,7 @@ int __wasilibc_unlinkat(int dirfd, const char *path) { } int __wasilibc_rmdirat(int dirfd, const char *path) { - if(dirfd == AT_FDCWD || path[0] == '/') { + if (dirfd == AT_FDCWD || path[0] == '/') { return rmdir(path); } diff --git a/libc-bottom-half/sources/chdir.c b/libc-bottom-half/sources/chdir.c index dd3a0093b..21c5ba16a 100644 --- a/libc-bottom-half/sources/chdir.c +++ b/libc-bottom-half/sources/chdir.c @@ -7,8 +7,8 @@ #include #include #include -#include #include +#include #include #ifndef __webp2__ diff --git a/libc-bottom-half/sources/pipe.c b/libc-bottom-half/sources/pipe.c index ef1d02969..b19f41aa3 100644 --- a/libc-bottom-half/sources/pipe.c +++ b/libc-bottom-half/sources/pipe.c @@ -494,9 +494,7 @@ static descriptor_vtable_t pipe_vtable = { #endif // __wasip3__ -int pipe(int fd[2]) { - return pipe2(fd, 0); -} +int pipe(int fd[2]) { return pipe2(fd, 0); } int pipe2(int fd[2], int flags) { if ((flags & ~(O_CLOEXEC | O_NONBLOCK)) != 0) { diff --git a/libc-bottom-half/sources/posix.c b/libc-bottom-half/sources/posix.c index f8c99a705..23d5692ce 100644 --- a/libc-bottom-half/sources/posix.c +++ b/libc-bottom-half/sources/posix.c @@ -9,9 +9,9 @@ #include #include #include -#include #include #include +#include #include #if defined(__wasip1__) || defined(__wasip2__) || defined(__wasip3__) @@ -76,7 +76,7 @@ int __wasilibc_open_nomode(const char *path, int oflag) { char *relative_path; int dirfd = find_relpath(path, &relative_path); - + // If we can't find a preopen for it, fail as if we can't find the path. if (dirfd == -1) { errno = ENOENT; diff --git a/libc-bottom-half/sources/ppoll.c b/libc-bottom-half/sources/ppoll.c index 536349334..e811b0fbf 100644 --- a/libc-bottom-half/sources/ppoll.c +++ b/libc-bottom-half/sources/ppoll.c @@ -604,8 +604,8 @@ static int ppoll_impl(struct pollfd *fds, size_t nfds, // never hit this. bool zero_timeout = timeout && timeout->tv_sec == 0 && timeout->tv_nsec == 0; if (ready == 0 && !zero_timeout) { - // TODO(webp2): If we actually return from this, we risk user code going - // into an infinite loop. Therefore we noisily exit. + // If we actually return from this, we risk user code going into an + // infinite loop. Therefore we noisily exit. __wasilibc_report_error("blocking ppoll is not supported"); exit(1); } diff --git a/libc-bottom-half/sources/preopens.c b/libc-bottom-half/sources/preopens.c index 71cdf7d87..a93e519f9 100644 --- a/libc-bottom-half/sources/preopens.c +++ b/libc-bottom-half/sources/preopens.c @@ -12,8 +12,8 @@ #include #include #include -#include #include +#include #include #if defined(__wasip2__) || defined(__wasip3__) diff --git a/libc-top-half/musl/src/env/__init_tls.c b/libc-top-half/musl/src/env/__init_tls.c index 18ccbb1c7..00d31dfa7 100644 --- a/libc-top-half/musl/src/env/__init_tls.c +++ b/libc-top-half/musl/src/env/__init_tls.c @@ -16,10 +16,7 @@ #include #include "lock.h" #include - -#ifdef __wasip3__ #include -#endif DECLARE_WEAK_LOCK(__thread_list_lock); diff --git a/libc-top-half/musl/src/stdio/fflush.c b/libc-top-half/musl/src/stdio/fflush.c index 7ca9ebe4b..4cee9ecf1 100644 --- a/libc-top-half/musl/src/stdio/fflush.c +++ b/libc-top-half/musl/src/stdio/fflush.c @@ -1,6 +1,4 @@ #include "stdio_impl.h" -#include "wasi/report-error.h" -#include #include /* stdout.c will override this if linked */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0655c1c09..325d2b5df 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -57,12 +57,8 @@ elseif(NOT ENGINE OR ENGINE STREQUAL "") # up-to-date. Note though that `wasm32-wasip1-threads` requires an explicitly # older version of Wasmtime as the latest releases no longer support # wasi-threads. - # - # TODO(webp2): Switch to v50.0.0 once it's released. - set(wasmtime_version v48.0.1) - if (WEB) - set(wasmtime_version v50.0.0-rc.1) - elseif (TARGET_TRIPLE MATCHES "-threads") + set(wasmtime_version v50.0.0) + if (TARGET_TRIPLE MATCHES "-threads") set(wasmtime_version v44.0.0) endif() ba_download( From 51b4cf544693e4f255f66677207fa0edee5241f2 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 22 Oct 2026 11:09:45 -0600 Subject: [PATCH 83/96] Fix mistakes from merge --- libc-bottom-half/sources/file_utils.c | 4 +++- .../sources/wasip3_component_type.o | Bin 8007 -> 8007 bytes 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/libc-bottom-half/sources/file_utils.c b/libc-bottom-half/sources/file_utils.c index f125ce713..2c97d90c4 100644 --- a/libc-bottom-half/sources/file_utils.c +++ b/libc-bottom-half/sources/file_utils.c @@ -9,7 +9,7 @@ #include #include -#if defined(__wasip2__) || defined(__wasip3__) +#ifdef __wasip2__ void __wasilibc_set_stream_errno(streams_own_error_t error) { filesystem_error_code_t code; @@ -21,7 +21,9 @@ void __wasilibc_set_stream_errno(streams_own_error_t error) { io_error_error_drop_own(error); } +#endif +#if defined(__wasip2__) || defined(__wasip3__) int wasi_string_from_c(const char *s, wasi_string_t *out) { int len = __wasilibc_validate_utf8(s); if (len < 0) { diff --git a/libc-bottom-half/sources/wasip3_component_type.o b/libc-bottom-half/sources/wasip3_component_type.o index 706db58f182f2044c7b79a185b3cbdc847fc0743..fb593497963458503e4e34db41e2cfe4532c62a8 100644 GIT binary patch delta 140 zcmX?Zcie8mJw~I6_ahmNCR;N40LeDSEFk%jF%C!uFoDSROy)rHIg=%jRAep!l2e$= zfFw6d5|GSe(F2lmS=IwdU)E+uqsPJ*B`kh8f)hK&&@)+`$fBp=Ea T0?A-GHy}AnP6|kFm74?r&{HXG delta 140 zcmX?Zcie8mJw}6x_ahk%CR;N40LeDSEFk%jF%C!uFoDSROy)rHIg=%jRAep!l2e$= zfFw6d5|GSe(F2lmS=IwdU)E+ugUuIN6}W+%PXf6>PJ*B`kh8f)hK&&@)+`$fBp=Ea T0?A-GHy}AnP6|kFm74?r#78MX From d09715a876ac9b2e7bb36b3be428e0d2442f1261 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 22 Oct 2026 12:04:16 -0600 Subject: [PATCH 84/96] Keep older version of wasmtime by default --- test/CMakeLists.txt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4be6eca3e..46104d39e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -56,9 +56,12 @@ elseif(NOT ENGINE OR ENGINE STREQUAL "") # By default a pinned version of Wasmtime is used that's kept relatively # up-to-date. Note though that `wasm32-wasip1-threads` requires an explicitly # older version of Wasmtime as the latest releases no longer support - # wasi-threads. - set(wasmtime_version v50.0.0) - if (TARGET_TRIPLE MATCHES "-threads") + # wasi-threads, and web targets require sufficiently new versions of wasmtime + # that support new component model features. + set(wasmtime_version v48.0.1) + if (WEB) + set(wasmtime_version v50.0.0) + elseif (TARGET_TRIPLE MATCHES "-threads") set(wasmtime_version v44.0.0) endif() ba_download( From 0f4c179eff083d948e69483c7364db6522c5d950 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Fri, 23 Oct 2026 13:53:45 -0600 Subject: [PATCH 85/96] Add missing include guard --- libc-top-half/headers/private/wasi/utf8.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libc-top-half/headers/private/wasi/utf8.h b/libc-top-half/headers/private/wasi/utf8.h index 330997b16..4e02b9721 100644 --- a/libc-top-half/headers/private/wasi/utf8.h +++ b/libc-top-half/headers/private/wasi/utf8.h @@ -1,3 +1,6 @@ +#ifndef __WASI_UTF8_H +#define __WASI_UTF8_H + #include #include #include @@ -48,3 +51,5 @@ hidden bool __wasilibc_utf8_abort_current(wasilibc_utf8_writer_t *w); // Resets the writer back to the start of its buffer. If there is a multi-byte // code point in progress, it will be copied to the start of the buffer. hidden void __wasilibc_utf8_reset(wasilibc_utf8_writer_t *w); + +#endif // __WASI_UTF8_H From b1322760ceee6096875be0aeaa71892a20bc9b85 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Fri, 23 Oct 2026 13:57:07 -0600 Subject: [PATCH 86/96] Don't use generated header directly --- libc-bottom-half/sources/webp2_stdio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libc-bottom-half/sources/webp2_stdio.c b/libc-bottom-half/sources/webp2_stdio.c index 46125a00a..a2bd98336 100644 --- a/libc-bottom-half/sources/webp2_stdio.c +++ b/libc-bottom-half/sources/webp2_stdio.c @@ -4,10 +4,10 @@ #include #include #include -#include #include #include #include +#include #include #define LINE_BUF_SIZE 4096 From 6b41b44510b638bb524bb02ab23e5a427013cf53 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Fri, 23 Oct 2026 14:16:13 -0600 Subject: [PATCH 87/96] Dispatch fflush through the descriptor vtable This avoids assuming that all fd's we fflush are stdio streams. That's true for now but very likely won't be in the future. --- .../headers/private/wasi/descriptor_table.h | 3 +++ libc-bottom-half/sources/webp2_stdio.c | 23 +++++++++++-------- .../headers/private/wasi/webp2_stdio.h | 4 ++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/libc-bottom-half/headers/private/wasi/descriptor_table.h b/libc-bottom-half/headers/private/wasi/descriptor_table.h index 6be533f82..68634af7b 100644 --- a/libc-bottom-half/headers/private/wasi/descriptor_table.h +++ b/libc-bottom-half/headers/private/wasi/descriptor_table.h @@ -210,6 +210,9 @@ typedef struct descriptor_vtable_t { ssize_t (*read)(void *, void *buf, size_t nbyte); /// Implementation of `write`-the-function. ssize_t (*write)(void *, const void *buf, size_t nbyte); + /// The bottom half of the `fflush` function, flushing content to the output + /// device (i.e. the console). + void (*fflush)(void *); #endif /// Sets the nonblocking flag for this object to the specified value. diff --git a/libc-bottom-half/sources/webp2_stdio.c b/libc-bottom-half/sources/webp2_stdio.c index a2bd98336..05dac3cf3 100644 --- a/libc-bottom-half/sources/webp2_stdio.c +++ b/libc-bottom-half/sources/webp2_stdio.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -26,7 +27,7 @@ static stdio_t __stdin; static stdio_t __stdout; static stdio_t __stderr; -static void stdio_out(stdio_t *stdio) { +static void stdio_console_out(stdio_t *stdio) { webp2_string_t out = (webp2_string_t){.ptr = stdio->utf8.buf, .len = stdio->utf8.valid_len}; if (stdio->warn) { @@ -36,9 +37,10 @@ static void stdio_out(stdio_t *stdio) { } } -static void stdio_flush(stdio_t *stdio) { +static void stdio_flush_to_console(void *data) { + stdio_t *stdio = (stdio_t *)data; if (stdio->utf8.valid_len > 0) { - stdio_out(stdio); + stdio_console_out(stdio); __wasilibc_utf8_reset(&stdio->utf8); } } @@ -52,10 +54,10 @@ static void finish_and_flush(stdio_t *stdio) { if (!__wasilibc_utf8_abort_current(&stdio->utf8)) { // Emit what is already valid in the buffer first, and carry over the // in-progress codepoint - stdio_flush(stdio); + stdio_flush_to_console(stdio); } __wasilibc_utf8_abort_current(&stdio->utf8); - stdio_flush(stdio); + stdio_flush_to_console(stdio); } static void stdio_free(void *data) { @@ -111,7 +113,7 @@ static ssize_t stdio_write(void *data, const void *buf, size_t nbyte) { // or because there was no room for a replacement char). bool already_logged = false; if (!__wasilibc_utf8_write(utf8, b)) { - stdio_out(stdio); + stdio_console_out(stdio); __wasilibc_utf8_reset(utf8); __wasilibc_utf8_write(utf8, b); // infallible already_logged = true; @@ -123,7 +125,7 @@ static ssize_t stdio_write(void *data, const void *buf, size_t nbyte) { utf8->valid_len--; bool skip = utf8->valid_len == 0 && already_logged; if (!skip) { - stdio_out(stdio); + stdio_console_out(stdio); } __wasilibc_utf8_reset(utf8); } @@ -141,6 +143,7 @@ static descriptor_vtable_t stdin_vtable = { static descriptor_vtable_t stdouterr_vtable = { .free = stdio_free, .write = stdio_write, + .fflush = stdio_flush_to_console, .fstat = stdio_fstat, .fcntl_getfl = stdio_fcntl_getfl, .set_blocking = stdio_set_blocking, @@ -178,8 +181,10 @@ void __wasilibc_webp2_fflush(int fd) { if (descriptor_table_get(fd, &entry) < 0) { return; } - stdio_t *stdio = (stdio_t *)entry.data; - stdio_flush(stdio); + defer descriptor_table_entry_dec(entry); + if (entry.vtable->fflush) { + entry.vtable->fflush(entry.data); + } } void __wasilibc_webp2_flush_and_close_stdio(bool also_musl) { diff --git a/libc-top-half/headers/private/wasi/webp2_stdio.h b/libc-top-half/headers/private/wasi/webp2_stdio.h index 95bd2e046..a8990ecab 100644 --- a/libc-top-half/headers/private/wasi/webp2_stdio.h +++ b/libc-top-half/headers/private/wasi/webp2_stdio.h @@ -10,8 +10,8 @@ #include #include -// Flush all valid content in one of our linebuffers (leaving trailing -// multibyte content alone to be completed later). +// Flush content in our output buffers to the output device (i.e. the console). +// Trailing multi-byte UTF-8 content will remain in the buffer for later. hidden void __wasilibc_webp2_fflush(int fd); // Finalizes and flushes the contents of stdout & stderr in preparation for app From f3247afe6162a63f2c8bfc51ad411c945f26a508 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Fri, 23 Oct 2026 14:21:53 -0600 Subject: [PATCH 88/96] Clean up crt1-command.c --- libc-bottom-half/crt/crt1-command.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libc-bottom-half/crt/crt1-command.c b/libc-bottom-half/crt/crt1-command.c index 01c425788..867111d13 100644 --- a/libc-bottom-half/crt/crt1-command.c +++ b/libc-bottom-half/crt/crt1-command.c @@ -30,7 +30,6 @@ __attribute__((export_name("_initialize"))) void _start(void) #error "Unsupported WASI version" #endif { -#ifndef __webp2__ // Commands should only be called once per instance. This simple check // ensures that the `_start` function isn't started more than once. // @@ -50,7 +49,6 @@ __attribute__((export_name("_initialize"))) void _start(void) __builtin_trap(); } started = 1; -#endif #endif // The linker synthesizes this to call constructors. @@ -61,6 +59,9 @@ __attribute__((export_name("_initialize"))) void _start(void) // arguments and calls `__main_argv_argc`. int r = __main_void(); + // On webp2 we don't run destructors because main is used as a start function + // rather than the entire program. Everything about the application state + // must therefore be ok after main exits. #ifndef __webp2__ // Call atexit functions, destructors, stdio cleanup, etc. __wasm_call_dtors(); From e9786577d0cf47860d5f5110651a3095f347c133 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Fri, 23 Oct 2026 14:24:22 -0600 Subject: [PATCH 89/96] Add extra comments for report_errorf --- libc-bottom-half/sources/__wasilibc_report_error.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libc-bottom-half/sources/__wasilibc_report_error.c b/libc-bottom-half/sources/__wasilibc_report_error.c index 58a260ae2..17f67493d 100644 --- a/libc-bottom-half/sources/__wasilibc_report_error.c +++ b/libc-bottom-half/sources/__wasilibc_report_error.c @@ -29,7 +29,8 @@ void __wasilibc_report_errorf(const char *format, ...) { wasilibc_utf8_writer_t utf8 = __wasilibc_make_utf8_writer(utf8_buf, ERROR_MAX_LEN); - // Re-encode as UTF-8 (excluding vnsprintf's nul) + // Re-encode as UTF-8 (excluding vnsprintf's nul). Non-UTF-8 data can slip in + // via __FILE__ or function names at a minimum. for (int i = 0; i < n && i < (int)sizeof(format_buf) - 1; i++) { if (!__wasilibc_utf8_write(&utf8, format_buf[i])) { break; From 20a2b9e3482d059e840d17630a988021a26d22d9 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Fri, 23 Oct 2026 14:34:12 -0600 Subject: [PATCH 90/96] Use size_t in utf-8 writer --- libc-bottom-half/sources/utf8.c | 4 ++-- libc-top-half/headers/private/wasi/utf8.h | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/libc-bottom-half/sources/utf8.c b/libc-bottom-half/sources/utf8.c index 99b0b06a0..cdb5b4143 100644 --- a/libc-bottom-half/sources/utf8.c +++ b/libc-bottom-half/sources/utf8.c @@ -103,7 +103,7 @@ bool __wasilibc_utf8_write(wasilibc_utf8_writer_t *w, uint8_t b) { w->buf[w->raw_len++] = b; w->valid_len = w->raw_len; } else if (is_2byte || is_3byte || is_4byte) { - int32_t codepoint_length = is_2byte ? 2 : (is_3byte ? 3 : 4); + size_t codepoint_length = is_2byte ? 2 : (is_3byte ? 3 : 4); if (w->raw_len + codepoint_length > w->cap) { return false; } @@ -120,7 +120,7 @@ bool __wasilibc_utf8_write(wasilibc_utf8_writer_t *w, uint8_t b) { w->codepoint_remaining -= 1; // If done, validate overlong / out-of-range - if (w->codepoint_remaining <= 0) { + if (w->codepoint_remaining == 0) { uint8_t first = w->buf[w->valid_len]; uint8_t second = w->buf[w->valid_len + 1]; bool overlong = first == 0xC0 || first == 0xC1 || diff --git a/libc-top-half/headers/private/wasi/utf8.h b/libc-top-half/headers/private/wasi/utf8.h index 4e02b9721..0d8351da7 100644 --- a/libc-top-half/headers/private/wasi/utf8.h +++ b/libc-top-half/headers/private/wasi/utf8.h @@ -3,6 +3,7 @@ #include #include +#include #include // ============================================================================ @@ -25,9 +26,9 @@ hidden int __wasilibc_validate_utf8(const char *ptr_signed); // `cap` must be at least 8. typedef struct wasilibc_utf8_writer_t { uint8_t *buf; - int32_t cap; - int32_t raw_len, valid_len; - int32_t codepoint_remaining; + size_t cap; + size_t raw_len, valid_len; + size_t codepoint_remaining; } wasilibc_utf8_writer_t; #define __wasilibc_make_utf8_writer(_buf, _cap) \ From fe6dfe21a260f61ed75ef9ea1de165b391cad206 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 9 Nov 2026 12:14:54 -0600 Subject: [PATCH 91/96] Report precise nonzero status code on exit --- libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c index 066bd801d..40172bb08 100644 --- a/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c +++ b/libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c @@ -30,7 +30,7 @@ noreturn void _Exit(int status) { webp2_string_set(&msg, "exit with status 0 (reported as trap)"); webp2_console_log(&msg); } else { - __wasilibc_report_error("exit with nonzero status"); + __wasilibc_report_errorf("exit with status %d", status); } __builtin_trap(); #else From 7d703f63ee298553afcd99f74ea678fc2ee0eec5 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 9 Nov 2026 12:26:05 -0600 Subject: [PATCH 92/96] Assume wasi-sdk C compiler for tests --- .github/workflows/main.yml | 3 +- test/CMakeLists.txt | 69 ++------------------------------------ 2 files changed, 4 insertions(+), 68 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 16fece472..9aeb5e804 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -150,7 +150,8 @@ jobs: - name: Test wasm32-webp2 os: ubuntu-24.04 - clang_version: 22 + # wasi-sdk is required to build the wasip2 web adapter for tests. + clang_version: wasi-sdk-34.0-rc.3 upload: wasm32-webp2 test: true args: -DTARGET_TRIPLE=wasm32-webp2 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 46104d39e..34075b6fa 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -482,75 +482,13 @@ if (WEB) --rename wasm:js/global=global --rename wasm:js/console=console) - # The adapter is a `wasm32-wasip2` program, not linked against the webp2 libc - # under test, so it's built against a sysroot and compiler-rt downloaded from - # wasi-sdk. This way the compiler doesn't need to have its own wasip2 sysroot. - set(web_adapter_wasi_sdk_version 34) - set(web_adapter_wasi_sdk_url - "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${web_adapter_wasi_sdk_version}") - ExternalProject_Add( - web-adapter-sysroot - EXCLUDE_FROM_ALL ON - URL "${web_adapter_wasi_sdk_url}/wasi-sysroot-${web_adapter_wasi_sdk_version}.0.tar.gz" - DOWNLOAD_EXTRACT_TIMESTAMP ON - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - ) - ExternalProject_Get_Property(web-adapter-sysroot SOURCE_DIR) - set(web_adapter_sysroot ${SOURCE_DIR}) - ExternalProject_Add( - web-adapter-builtins - EXCLUDE_FROM_ALL ON - URL "${web_adapter_wasi_sdk_url}/libclang_rt-${web_adapter_wasi_sdk_version}.0.tar.gz" - DOWNLOAD_EXTRACT_TIMESTAMP ON - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - ) - ExternalProject_Get_Property(web-adapter-builtins SOURCE_DIR) - set(web_adapter_builtins_src ${SOURCE_DIR}/wasm32-unknown-wasip2/libclang_rt.builtins.a) - - # Place compiler-rt wherever the compiler expects to find it for wasip2 - # within a custom resource dir, the same as `cmake/builtins.cmake` does. - execute_process( - COMMAND ${CMAKE_C_COMPILER} --target=wasm32-wasip2 --print-libgcc-file-name - OUTPUT_VARIABLE web_adapter_system_builtins - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - cmake_path( - RELATIVE_PATH web_adapter_system_builtins - BASE_DIRECTORY ${system_resource_dir} - OUTPUT_VARIABLE web_adapter_builtins_rel - ) - set(web_adapter_resource_dir ${web_adapter_dir}/resource-dir) - set(web_adapter_builtins ${web_adapter_resource_dir}/${web_adapter_builtins_rel}) - add_custom_command( - OUTPUT ${web_adapter_builtins} - COMMAND ${CMAKE_COMMAND} -E copy ${web_adapter_builtins_src} ${web_adapter_builtins} - COMMAND ${CMAKE_COMMAND} -E create_symlink ${system_resource_dir}/include ${web_adapter_resource_dir}/include - DEPENDS web-adapter-builtins - ) - - set(web_adapter_link_flags) - set(web_adapter_link_deps) - if (WASM_COMPONENT_LD_EXECUTABLE) - list(APPEND web_adapter_link_flags -fuse-ld=${WASM_COMPONENT_LD_EXECUTABLE}) - if (TARGET wasm-component-ld) - list(APPEND web_adapter_link_deps wasm-component-ld) - endif() - endif() - - # The adapter itself. The compiler is invoked directly to avoid all the flags - # configured above for tests. + # The actual adapter, which is a wasm32-wasip2 program to be run against + # wasmtime. CMAKE_C_COMPILER must have component support. set(WEB_ADAPTER ${web_adapter_dir}/web-adapter.wasm) add_custom_command( OUTPUT ${WEB_ADAPTER} COMMAND ${CMAKE_C_COMPILER} --target=wasm32-wasip2 -mexec-model=reactor - --sysroot=${web_adapter_sysroot} - -resource-dir ${web_adapter_resource_dir} - ${web_adapter_link_flags} -Wall -Wextra -Werror -g -I${web_adapter_dir} ${web_adapter_src}/adapter.c ${web_adapter_dir}/adapter.c @@ -561,9 +499,6 @@ if (WEB) ${web_adapter_dir}/adapter.c ${web_adapter_dir}/adapter.h ${web_adapter_dir}/adapter_component_type.o - ${web_adapter_builtins} - web-adapter-sysroot - ${web_adapter_link_deps} ) add_custom_target(web-adapter DEPENDS ${WEB_ADAPTER}) endif() From 35f42b97d4cef2ef3b3e2fe2fa66f4aa75a7e55f Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 9 Nov 2026 13:37:30 -0600 Subject: [PATCH 93/96] Include pipe tests in build but suppress at runtime --- test/CMakeLists.txt | 16 +++++++--------- test/src/pipe.c | 2 +- test/src/pipe_atomic_write.c | 2 +- test/src/pipe_fstat.c | 2 +- test/src/pipe_poll.c | 2 +- test/src/pipe_setfl_independent.c | 2 +- test/src/pipe_zero_len.c | 2 +- 7 files changed, 13 insertions(+), 15 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f91370cd9..e62b59747 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -622,6 +622,12 @@ add_wasilibc_test(memchr.c LDFLAGS -Wl,--stack-first -Wl,--initial-memory=327680 add_wasilibc_test(memcmp.c LDFLAGS -Wl,--stack-first -Wl,--initial-memory=327680) add_wasilibc_test(opendir.c FS ARGV /) add_wasilibc_test(open_relative_path.c FS ARGV /) +add_wasilibc_test(pipe.c) +add_wasilibc_test(pipe_poll.c) +add_wasilibc_test(pipe_fstat.c) +add_wasilibc_test(pipe_setfl_independent.c) +add_wasilibc_test(pipe_atomic_write.c) +add_wasilibc_test(pipe_zero_len.c) add_wasilibc_test(poll.c FS) add_wasilibc_test(ppoll.c FS) add_wasilibc_test(pselect.c FS) @@ -636,6 +642,7 @@ add_wasilibc_test(stat.c FS) add_wasilibc_test(stdio.c FS) add_wasilibc_test(stdio_nonblocking.c) add_wasilibc_test(strchrnul.c LDFLAGS -Wl,--stack-first -Wl,--initial-memory=327680) +add_wasilibc_test(stream-error.c FS) add_wasilibc_test(strlen.c LDFLAGS -Wl,--stack-first -Wl,--initial-memory=327680) add_wasilibc_test(strptime.c) add_wasilibc_test(strrchr.c LDFLAGS -Wl,--stack-first -Wl,--initial-memory=327680) @@ -671,15 +678,6 @@ if (WEB) # Relies on webp2's line buffering add_wasilibc_test(stdio-dup2-fflush.c PASS_REGULAR_EXPRESSION "firstline\nsecondline\n") endif() -if (NOT WEB) - add_wasilibc_test(pipe.c) - add_wasilibc_test(pipe_poll.c) - add_wasilibc_test(pipe_fstat.c) - add_wasilibc_test(pipe_setfl_independent.c) - add_wasilibc_test(pipe_atomic_write.c) - add_wasilibc_test(pipe_zero_len.c) - add_wasilibc_test(stream-error.c FS) -endif() if (NOT TEST_WITH_V8) add_wasilibc_test(pipe_espipe.c NETWORK) add_wasilibc_test(task_tls_basic.c) diff --git a/test/src/pipe.c b/test/src/pipe.c index 3748d37da..eccfe21d5 100644 --- a/test/src/pipe.c +++ b/test/src/pipe.c @@ -179,7 +179,7 @@ static void test_stdio(void) { } int main(void) { -#if defined(__wasip1__) || defined(__wasip2__) +#if defined(__wasip1__) || defined(__wasip2__) || defined(__webp2__) if (1) return t_status; #endif diff --git a/test/src/pipe_atomic_write.c b/test/src/pipe_atomic_write.c index 30245f326..9d5190f5f 100644 --- a/test/src/pipe_atomic_write.c +++ b/test/src/pipe_atomic_write.c @@ -13,7 +13,7 @@ } while (0) int main(void) { -#if defined(__wasip1__) || defined(__wasip2__) +#if defined(__wasip1__) || defined(__wasip2__) || defined(__webp2__) if (1) return t_status; #endif diff --git a/test/src/pipe_fstat.c b/test/src/pipe_fstat.c index 783e31441..897eb5d96 100644 --- a/test/src/pipe_fstat.c +++ b/test/src/pipe_fstat.c @@ -11,7 +11,7 @@ } while (0) int main(void) { -#if defined(__wasip1__) || defined(__wasip2__) +#if defined(__wasip1__) || defined(__wasip2__) || defined(__webp2__) if (1) return t_status; #endif diff --git a/test/src/pipe_poll.c b/test/src/pipe_poll.c index 611aa925b..dd9c4ae1e 100644 --- a/test/src/pipe_poll.c +++ b/test/src/pipe_poll.c @@ -30,7 +30,7 @@ static size_t fill_pipe(int wr) { } int main(void) { -#if defined(__wasip1__) || defined(__wasip2__) +#if defined(__wasip1__) || defined(__wasip2__) || defined(__webp2__) if (1) return t_status; #endif diff --git a/test/src/pipe_setfl_independent.c b/test/src/pipe_setfl_independent.c index 18c78ff66..6e3312402 100644 --- a/test/src/pipe_setfl_independent.c +++ b/test/src/pipe_setfl_independent.c @@ -11,7 +11,7 @@ } while (0) int main(void) { -#if defined(__wasip1__) || defined(__wasip2__) +#if defined(__wasip1__) || defined(__wasip2__) || defined(__webp2__) if (1) return t_status; #endif diff --git a/test/src/pipe_zero_len.c b/test/src/pipe_zero_len.c index 29bb9791d..3b9c26936 100644 --- a/test/src/pipe_zero_len.c +++ b/test/src/pipe_zero_len.c @@ -11,7 +11,7 @@ } while (0) int main(void) { -#if defined(__wasip1__) || defined(__wasip2__) +#if defined(__wasip1__) || defined(__wasip2__) || defined(__webp2__) if (1) return t_status; #endif From 9938744d0d31e5dace8489b09da9217cafb0aef4 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 9 Nov 2026 13:44:31 -0600 Subject: [PATCH 94/96] Change which wasm-component-ld is used for the web adapter --- test/CMakeLists.txt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e62b59747..7552118cd 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -482,6 +482,17 @@ if (WEB) --rename wasm:js/global=global --rename wasm:js/console=console) + # Link with the same `wasm-component-ld` as everything else, since the one + # bundled with the compiler may be too old for the adapter's WIT. + set(web_adapter_link_flags) + set(web_adapter_link_deps) + if (WASM_COMPONENT_LD_EXECUTABLE) + list(APPEND web_adapter_link_flags -fuse-ld=${WASM_COMPONENT_LD_EXECUTABLE}) + if (TARGET wasm-component-ld) + list(APPEND web_adapter_link_deps wasm-component-ld) + endif() + endif() + # The actual adapter, which is a wasm32-wasip2 program to be run against # wasmtime. CMAKE_C_COMPILER must have component support. set(WEB_ADAPTER ${web_adapter_dir}/web-adapter.wasm) @@ -489,6 +500,7 @@ if (WEB) OUTPUT ${WEB_ADAPTER} COMMAND ${CMAKE_C_COMPILER} --target=wasm32-wasip2 -mexec-model=reactor + ${web_adapter_link_flags} -Wall -Wextra -Werror -g -I${web_adapter_dir} ${web_adapter_src}/adapter.c ${web_adapter_dir}/adapter.c @@ -499,6 +511,7 @@ if (WEB) ${web_adapter_dir}/adapter.c ${web_adapter_dir}/adapter.h ${web_adapter_dir}/adapter_component_type.o + ${web_adapter_link_deps} ) add_custom_target(web-adapter DEPENDS ${WEB_ADAPTER}) endif() From 47f9a947f9ad59d460af65948ca444d8ee98bdb9 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 9 Nov 2026 13:48:39 -0600 Subject: [PATCH 95/96] Fix symbols --- expected/wasm32-webp2/defined-symbols.txt | 1 + expected/wasm32-webp2/predefined-macros.txt | 2 ++ 2 files changed, 3 insertions(+) diff --git a/expected/wasm32-webp2/defined-symbols.txt b/expected/wasm32-webp2/defined-symbols.txt index 933359bc1..dfc2fe7a4 100644 --- a/expected/wasm32-webp2/defined-symbols.txt +++ b/expected/wasm32-webp2/defined-symbols.txt @@ -296,6 +296,7 @@ __tre_mem_alloc_impl __tre_mem_destroy __tre_mem_new_impl __tsearch_balance +__tzname_to_isdst __uflow __unlist_locked_file __unlock diff --git a/expected/wasm32-webp2/predefined-macros.txt b/expected/wasm32-webp2/predefined-macros.txt index 6df75c807..c16a6975e 100644 --- a/expected/wasm32-webp2/predefined-macros.txt +++ b/expected/wasm32-webp2/predefined-macros.txt @@ -2588,6 +2588,7 @@ #define __DEFINED_pthread_rwlockattr_t #define __DEFINED_pthread_spinlock_t #define __DEFINED_pthread_t +#define __DEFINED_reclen_t #define __DEFINED_register_t #define __DEFINED_regoff_t #define __DEFINED_sa_family_t @@ -2752,6 +2753,7 @@ #define __NEED_pthread_rwlockattr_t #define __NEED_pthread_spinlock_t #define __NEED_pthread_t +#define __NEED_reclen_t #define __NEED_register_t #define __NEED_regoff_t #define __NEED_sa_family_t From bc9886e5210153b8fcad493f0a31bae4385f4129 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 9 Nov 2026 14:09:02 -0600 Subject: [PATCH 96/96] Make stdout and stderr ttys --- libc-bottom-half/sources/webp2_stdio.c | 8 ++++++++ libc-top-half/musl/src/stdio/__stdout_write.c | 5 ++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/libc-bottom-half/sources/webp2_stdio.c b/libc-bottom-half/sources/webp2_stdio.c index 2bab31ed2..d17b34891 100644 --- a/libc-bottom-half/sources/webp2_stdio.c +++ b/libc-bottom-half/sources/webp2_stdio.c @@ -95,6 +95,13 @@ static int stdio_fcntl_setfl(void *data, int flags) { return 0; } +static int stdio_isatty(void *data) { + // This function is only used on stdout and stderr, both of which are + // considered to be ttys because of musl's buffering behavior. + (void)data; + return 1; +} + static ssize_t stdio_read(void *data, void *buf, size_t nbyte) { (void)data; (void)buf; @@ -156,6 +163,7 @@ static descriptor_vtable_t stdouterr_vtable = { .fstat = stdio_fstat, .fcntl_getfl = stdio_fcntl_getfl, .fcntl_setfl = stdio_fcntl_setfl, + .isatty = stdio_isatty, .set_blocking = stdio_set_blocking, }; diff --git a/libc-top-half/musl/src/stdio/__stdout_write.c b/libc-top-half/musl/src/stdio/__stdout_write.c index e806ded5c..8190ec5b4 100644 --- a/libc-top-half/musl/src/stdio/__stdout_write.c +++ b/libc-top-half/musl/src/stdio/__stdout_write.c @@ -13,10 +13,9 @@ size_t __stdout_write(FILE *f, const unsigned char *buf, size_t len) f->write = __stdio_write; #ifdef __wasilibc_unmodified_upstream // use isatty rather than manual ioctl if (!(f->flags & F_SVB) && __syscall(SYS_ioctl, f->fd, TIOCGWINSZ, &wsz)) - f->lbf = -1; -#elif !defined(__webp2__) +#else if (!(f->flags & F_SVB) && !__isatty(f->fd)) - f->lbf = -1; #endif + f->lbf = -1; return __stdio_write(f, buf, len); }