Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
upload: windows-clang-20
- name: Build on WASIp2 Windows x86_64
os: windows-2025
clang_version: 20.1.8
clang_version: 22.1.8
args: -DTARGET_TRIPLE=wasm32-wasip2

# Other versions of LLVM
Expand Down Expand Up @@ -74,7 +74,7 @@ jobs:

- name: Test wasm32-wasip2
os: ubuntu-24.04
clang_version: 20
clang_version: 22
test: true
upload: wasm32-wasip2
args: -DTARGET_TRIPLE=wasm32-wasip2
Expand Down Expand Up @@ -113,7 +113,7 @@ jobs:

- name: Test wasm32-wasip2 (debug)
os: ubuntu-24.04
clang_version: 20
clang_version: 22
test: true
args: -DCMAKE_BUILD_TYPE=Debug -DTARGET_TRIPLE=wasm32-wasip2

Expand All @@ -125,14 +125,14 @@ jobs:

- name: Test wasm32-wasip3
os: ubuntu-24.04
clang_version: 20
clang_version: 22
test: true
upload: wasm32-wasip3
args: -DTARGET_TRIPLE=wasm32-wasip3

- name: Test wasm32-wasip3 (debug)
os: ubuntu-24.04
clang_version: 20
clang_version: 22
test: true
args: -DCMAKE_BUILD_TYPE=Debug -DTARGET_TRIPLE=wasm32-wasip3

Expand All @@ -156,19 +156,19 @@ jobs:

- name: Test LTO
os: ubuntu-24.04
clang_version: 20
clang_version: 22
test: true
args: -DLTO=full -DTARGET_TRIPLE=wasm32-wasip2 -DCHECK_SYMBOLS=OFF

- name: Test wasip2 on macOS
os: macos-15
clang_version: wasi-sdk-30
clang_version: wasi-sdk-33
test: true
args: -DTARGET_TRIPLE=wasm32-wasip2 -DCMAKE_C_COMPILER_WORKS=ON

- name: Test wasip2 on Windows
os: windows-2025
clang_version: 20.1.8
clang_version: 22.1.8
test: true
args: -DTARGET_TRIPLE=wasm32-wasip2

Expand Down Expand Up @@ -248,11 +248,16 @@ jobs:
- uses: actions/checkout@v6
with:
submodules: true
- run: cmake -S . -B build -DBUILD_TESTS=ON -G Ninja -DCMAKE_C_COMPILER=clang
- uses: ./.github/actions/setup
with:
clang_version: 22
- run: cmake -S . -B build -DBUILD_TESTS=ON -G Ninja -DCMAKE_C_COMPILER=clang-22
- run: ninja -C build format-check
- run: cmake -S . -B build -DTARGET_TRIPLE=wasm32-wasip1-threads
- run: ninja -C build format-check
- run: cmake -S . -B build -DTARGET_TRIPLE=wasm32-wasip2
- run: ninja -C build format-check
- run: cmake -S . -B build -DTARGET_TRIPLE=wasm32-wasip1-threads
- run: cmake -S . -B build -DTARGET_TRIPLE=wasm32-wasip3
- run: ninja -C build format-check

rustfmt:
Expand Down
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ if(ENABLE_COOP_THREADS)
else()
endif()

# In wasip2/wasip3 code we're using `defer { ... }`, so make sure that exists.
include(CheckCCompilerFlag)
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`")
endif()

# =============================================================================
# Helper functions for adding libraries.

Expand Down
8 changes: 8 additions & 0 deletions cmake/scripts/run-check-symbols.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ endforeach()
list(FILTER defined_symbols INCLUDE REGEX " [A-Z] ")
foreach(line IN LISTS defined_symbols)
string(REGEX REPLACE ".* [A-Z] " "" symbol "${line}")
# Skip internal debug-only asserts
if (symbol MATCHES "__wasilibc_assert_.*")
continue()
endif()
list(APPEND final_defined_symbols ${symbol})
endforeach()
list(SORT final_defined_symbols)
Expand All @@ -55,6 +59,10 @@ list(REMOVE_DUPLICATES final_defined_symbols)
list(FILTER undefined_symbols INCLUDE REGEX " U ")
foreach(line IN LISTS undefined_symbols)
string(REGEX REPLACE ".* U " "" symbol "${line}")
# Skip internal debug-only asserts
if (symbol MATCHES "__wasilibc_assert_.*")
continue()
endif()
list(APPEND final_undefined_symbols ${symbol})
endforeach()
list(SORT final_undefined_symbols)
Expand Down
3 changes: 2 additions & 1 deletion expected/wasm32-wasip2/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ __wasilibc_add_tcp_socket
__wasilibc_add_udp_socket
__wasilibc_cwd
__wasilibc_deinitialize_environ
__wasilibc_descriptor_deallocate
__wasilibc_dttoif
__wasilibc_ensure_environ
__wasilibc_environ
Expand Down Expand Up @@ -522,7 +523,7 @@ ctanl
ctime
ctime_r
descriptor_table_clear
descriptor_table_get_ref
descriptor_table_get
descriptor_table_insert
descriptor_table_remove
descriptor_table_renumber
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasip2/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2978,6 +2978,7 @@
#define __SIZE_TYPE__ long unsigned int
#define __SIZE_WIDTH__ 32
#define __STDARG_H
#define __STDC_DEFER_TS25755__ 1
#define __STDC_HOSTED__ 1
#define __STDC_IEC_559__ 1
#define __STDC_ISO_10646__ 201206L
Expand Down
3 changes: 2 additions & 1 deletion expected/wasm32-wasip3-coop/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ __wasilibc_add_tcp_socket
__wasilibc_add_udp_socket
__wasilibc_cwd
__wasilibc_deinitialize_environ
__wasilibc_descriptor_deallocate
__wasilibc_dttoif
__wasilibc_ensure_environ
__wasilibc_environ
Expand Down Expand Up @@ -552,7 +553,7 @@ ctanl
ctime
ctime_r
descriptor_table_clear
descriptor_table_get_ref
descriptor_table_get
descriptor_table_insert
descriptor_table_remove
descriptor_table_renumber
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasip3-coop/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2989,6 +2989,7 @@
#define __SIZE_TYPE__ long unsigned int
#define __SIZE_WIDTH__ 32
#define __STDARG_H
#define __STDC_DEFER_TS25755__ 1
#define __STDC_HOSTED__ 1
#define __STDC_IEC_559__ 1
#define __STDC_ISO_10646__ 201206L
Expand Down
3 changes: 2 additions & 1 deletion expected/wasm32-wasip3/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ __wasilibc_add_tcp_socket
__wasilibc_add_udp_socket
__wasilibc_cwd
__wasilibc_deinitialize_environ
__wasilibc_descriptor_deallocate
__wasilibc_dttoif
__wasilibc_ensure_environ
__wasilibc_environ
Expand Down Expand Up @@ -529,7 +530,7 @@ ctanl
ctime
ctime_r
descriptor_table_clear
descriptor_table_get_ref
descriptor_table_get
descriptor_table_insert
descriptor_table_remove
descriptor_table_renumber
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasip3/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2984,6 +2984,7 @@
#define __SIZE_TYPE__ long unsigned int
#define __SIZE_WIDTH__ 32
#define __STDARG_H
#define __STDC_DEFER_TS25755__ 1
#define __STDC_HOSTED__ 1
#define __STDC_IEC_559__ 1
#define __STDC_ISO_10646__ 201206L
Expand Down
1 change: 1 addition & 0 deletions libc-bottom-half/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ foreach(file crt/crt1-command.c
target_link_libraries(${stem} PRIVATE musl-top-half-interface)
set_pic(${stem})
target_compile_options(${stem} PRIVATE -fvisibility=default -fno-lto)
target_include_directories(${stem} PRIVATE headers/private)
endforeach()

set(crt_sysroot ${SYSROOT}/lib/${TARGET_TRIPLE})
Expand Down
14 changes: 9 additions & 5 deletions libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <wasi/api.h>

#ifndef __wasip1__
#include <stddefer.h>
#include <wasi/file_utils.h>
#include <common/errors.h>
#else
Expand Down Expand Up @@ -50,12 +51,14 @@ DIR *fdopendir(int fd) {
dirp->dirent_size = 1;
return dirp;
#elif defined(__wasip2__) || defined(__wasip3__)
defer free(dirp);

// Translate the file descriptor to an internal handle
filesystem_borrow_descriptor_t file_handle;
if (fd_to_file_handle(fd, &file_handle) < 0) {
free(dirp);
descriptor_table_entry_t entry;
if (fd_to_file_handle(fd, &entry, &file_handle) < 0)
return NULL;
}
defer descriptor_table_entry_dec(entry);

// Read the directory
#if defined(__wasip2__)
Expand All @@ -65,7 +68,6 @@ DIR *fdopendir(int fd) {
&result,
&error_code);
if (!ok) {
free(dirp);
translate_error(&error_code);
return NULL;
}
Expand All @@ -80,7 +82,9 @@ DIR *fdopendir(int fd) {
dirp->offset = 0;
dirp->dirent = NULL;
dirp->dirent_size = 1;
return dirp;
DIR *ret = dirp;
dirp = NULL;
return ret;
#else
# error "Unsupported WASI version"
#endif
Expand Down
22 changes: 18 additions & 4 deletions libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <string.h>

#ifndef __wasip1__
#include <stddefer.h>
#include <wasi/file_utils.h>
#include <common/errors.h>
#endif
Expand Down Expand Up @@ -147,9 +148,19 @@ struct dirent *readdir(DIR *dirp) {
}

#elif defined(__wasip2__) || defined(__wasip3__)

static int ensure_has_directory_stream(DIR *dirp, filesystem_borrow_descriptor_t *handle) {
if (fd_to_file_handle(dirp->fd, handle) < 0)
// Ensures that `dirp` has the necessary streams prepared to start reading
// directory entries.
//
// On success returns `entry`, the owned entry for this operation that must be
// deallocated with `descriptor_table_entry_dec` when done, and `handle`, the
// WASI file that's being used. Additionally `dirp`'s streams are filled and
// ready for use.
//
// On failure returns -1 and `entry` need not be deallocated.
static int ensure_has_directory_stream(DIR *dirp,
descriptor_table_entry_t *entry,
filesystem_borrow_descriptor_t *handle) {
if (fd_to_file_handle(dirp->fd, entry, handle) < 0)
return -1;

#ifdef __wasip2__
Expand All @@ -162,6 +173,7 @@ static int ensure_has_directory_stream(DIR *dirp, filesystem_borrow_descriptor_t
&error_code);
if (!ok) {
translate_error(&error_code);
descriptor_table_entry_dec(*entry);
return -1;
}
#elif defined(__wasip3__)
Expand All @@ -176,9 +188,11 @@ static struct dirent *readdir_next(DIR *dirp) {
filesystem_metadata_hash_value_t metadata;
filesystem_error_code_t error_code;
filesystem_borrow_descriptor_t dir_handle;
descriptor_table_entry_t entry;

if (ensure_has_directory_stream(dirp, &dir_handle) < 0)
if (ensure_has_directory_stream(dirp, &entry, &dir_handle) < 0)
return NULL;
defer descriptor_table_entry_dec(entry);

// Yield '.' first if the offset is 0. Note that `d_ino` is from the metadata
// hash of the directory itself.
Expand Down
14 changes: 8 additions & 6 deletions libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
#include <stdarg.h>

#ifndef __wasip1__
#include <stddefer.h>
#include <wasi/file_utils.h>
#include <common/errors.h>
#endif

int fcntl(int fildes, int cmd, ...) {
#if defined(__wasip2__) || defined(__wasip3__)
descriptor_table_entry_t *entry = descriptor_table_get_ref(fildes);
if (entry == NULL)
descriptor_table_entry_t entry;
if (descriptor_table_get(fildes, &entry) < 0)
return -1;
defer descriptor_table_entry_dec(entry);
#endif

switch (cmd) {
Expand Down Expand Up @@ -52,11 +54,11 @@ int fcntl(int fildes, int cmd, ...) {
}
return oflags;
#elif defined(__wasip2__) || defined(__wasip3__)
if (!entry->vtable->fcntl_getfl) {
if (!entry.vtable->fcntl_getfl) {
errno = EINVAL;
return -1;
}
return entry->vtable->fcntl_getfl(entry->data);
return entry.vtable->fcntl_getfl(entry.data);
#else
# error "Unknown WASI version"
#endif
Expand All @@ -77,11 +79,11 @@ int fcntl(int fildes, int cmd, ...) {
return -1;
}
#elif defined(__wasip2__) || defined(__wasip3__)
if (!entry->vtable->fcntl_setfl) {
if (!entry.vtable->fcntl_setfl) {
errno = EINVAL;
return -1;
}
return entry->vtable->fcntl_setfl(entry->data, flags);
return entry.vtable->fcntl_setfl(entry.data, flags);
#else
# error "Unknown WASI version"
#endif
Expand Down
5 changes: 4 additions & 1 deletion libc-bottom-half/cloudlibc/src/libc/fcntl/openat.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string.h>

#ifndef __wasip1__
#include <stddefer.h>
#include <wasi/file.h>
#include <wasi/descriptor_table.h>
#include <wasi/file_utils.h>
Expand Down Expand Up @@ -136,8 +137,10 @@ int __wasilibc_nocwd_openat_nomode(int fd, const char *path, int oflag) {

// Translate the file descriptor to an internal handle
filesystem_borrow_descriptor_t file_handle;
if (fd_to_file_handle(fd, &file_handle) < 0)
descriptor_table_entry_t entry;
if (fd_to_file_handle(fd, &entry, &file_handle) < 0)
return -1;
defer descriptor_table_entry_dec(entry);

// Construct a WASI string for the path
wasi_string_t wasi_path;
Expand Down
5 changes: 4 additions & 1 deletion libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fadvise.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <wasi/api.h>

#ifndef __wasip1__
#include <stddefer.h>
#include <wasi/file_utils.h>
#include <common/errors.h>
#endif
Expand All @@ -32,8 +33,10 @@ int posix_fadvise(int fd, off_t offset, off_t len, int advice) {
return __wasi_fd_advise(fd, offset, len, advice);
#elif defined(__wasip2__) || defined(__wasip3__)
filesystem_borrow_descriptor_t file_handle;
if (fd_to_file_handle(fd, &file_handle) < 0)
descriptor_table_entry_t entry;
if (fd_to_file_handle(fd, &entry, &file_handle) < 0)
return EBADF;
defer descriptor_table_entry_dec(entry);
if (file_handle.__handle == 0) {
errno = EBADF;
return EBADF;
Expand Down
Loading