Skip to content
Draft
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
3 changes: 3 additions & 0 deletions expected/wasm32-wasip3/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ __wasilibc_open_nomode
__wasilibc_populate_preopens
__wasilibc_pthread_self
__wasilibc_random
__wasilibc_read3
__wasilibc_rename_newat
__wasilibc_rename_oldat
__wasilibc_reset_preopens
Expand All @@ -344,6 +345,7 @@ __wasilibc_stat
__wasilibc_tell
__wasilibc_unlinkat
__wasilibc_utimens
__wasilibc_write3
__wasm_call_dtors
__wcscoll_l
__wcsftime_l
Expand Down Expand Up @@ -1472,6 +1474,7 @@ wasip3_list_u8_free
wasip3_option_string_free
wasip3_string_dup
wasip3_string_free
wasip3_string_from_c
wasip3_string_set
wasip3_subtask_block_on
wasip3_subtask_cancel
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 @@ -164,6 +164,7 @@ if (WASI STREQUAL "p3")
list(APPEND bottom_half_sources
sources/wasip3.c
sources/wasip3_file.c
sources/wasip3_file_utils.c
sources/wasip3_stdio.c
sources/wasip3_subtask.c
sources/wasip3_tcp.c
Expand Down
4 changes: 3 additions & 1 deletion libc-bottom-half/cloudlibc/src/common/errors.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <wasi/api.h>

#ifdef __wasip2__
#if defined(__wasip2__) || defined(__wasip3__)
#include <errno.h>
#include <stdlib.h>

Expand All @@ -9,9 +9,11 @@ static void translate_error(filesystem_error_code_t error) {
case FILESYSTEM_ERROR_CODE_ACCESS:
errno = EACCES;
break;
#ifdef __wasip2__
case FILESYSTEM_ERROR_CODE_WOULD_BLOCK:
errno = EAGAIN;
break;
#endif
case FILESYSTEM_ERROR_CODE_ALREADY:
errno = EALREADY;
break;
Expand Down
32 changes: 28 additions & 4 deletions libc-bottom-half/cloudlibc/src/libc/fcntl/openat.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include <common/errors.h>
#endif

#ifdef __wasip3__
#include <wasi/wasip3_block.h>
#endif

#ifdef __wasip1__
static_assert(O_APPEND == __WASI_FDFLAGS_APPEND, "Value mismatch");
static_assert(O_DSYNC == __WASI_FDFLAGS_DSYNC, "Value mismatch");
Expand Down Expand Up @@ -87,7 +91,7 @@ int __wasilibc_nocwd_openat_nomode(int fd, const char *path, int oflag) {
return -1;
}
return newfd;
#elif defined(__wasip2__)
#elif defined(__wasip2__) || defined(__wasip3__)
// Set up path flags
filesystem_path_flags_t lookup_flags = 0;
if ((oflag & O_NOFOLLOW) == 0)
Expand Down Expand Up @@ -140,6 +144,7 @@ int __wasilibc_nocwd_openat_nomode(int fd, const char *path, int oflag) {
return -1;

// Construct a WASI string for the path
#ifdef __wasip2__
wasip2_string_t path2;
if (wasip2_string_from_c(path, &path2) < 0)
return -1;
Expand All @@ -162,9 +167,28 @@ 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(__wasip3__)
// TODO(wasip3)
errno = ENOTSUP;
return -1;
wasip3_string_t path3;
if (wasip3_string_from_c(path, &path3) < 0)
return -1;

// Open the file, yielding a new handle
filesystem_method_descriptor_open_at_args_t args;
args.self = file_handle;
args.flags = lookup_flags;
args.path = path3;
args.open_flags = open_flags;
args.path_flags = fs_flags;
filesystem_result_own_descriptor_error_code_t result;
wasip3_subtask_status_t status = filesystem_method_descriptor_open_at(&args, &result);
wasip3_subtask_block_on(status);
if (result.is_err) {
translate_error(result.val.err);
return -1;
}

// Update the descriptor table with the new handle
return __wasilibc_add_file(result.val.ok, oflag);
#endif
#else
# error "Unsupported WASI version"
#endif
Expand Down
6 changes: 1 addition & 5 deletions libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ off_t __lseek(int fildes, off_t offset, int whence) {
return -1;
}
return new_offset;
#elif defined(__wasip2__)
#elif defined(__wasip2__) || defined(__wasip3__)
// Look up a stream for fildes
descriptor_table_entry_t *entry = descriptor_table_get_ref(fildes);
if (!entry)
Expand All @@ -39,10 +39,6 @@ off_t __lseek(int fildes, off_t offset, int whence) {
return -1;
}
return entry->vtable->seek(entry->data, offset, whence);
#elif defined(__wasip3__)
// TODO(wasip3)
errno = ENOTSUP;
return -1;
#else
# error "Unknown WASI version"
#endif
Expand Down
4 changes: 1 addition & 3 deletions libc-bottom-half/cloudlibc/src/libc/unistd/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ ssize_t read(int fildes, void *buf, size_t nbyte) {
*off += contents.len;
return contents.len;
#elif defined(__wasip3__)
// TODO(wasip3)
errno = ENOTSUP;
return -1;
return __wasilibc_read3(fildes, buf, nbyte);
#else
# error "Unsupported WASI version"
#endif
Expand Down
4 changes: 1 addition & 3 deletions libc-bottom-half/cloudlibc/src/libc/unistd/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ ssize_t write(int fildes, const void *buf, size_t nbyte) {
*off += contents.len;
return contents.len;
#elif defined(__wasip3__)
// TODO(wasip3)
errno = ENOTSUP;
return -1;
return __wasilibc_write3(fildes, buf, nbyte);
#else
# error "Unknown WASI version"
#endif
Expand Down
11 changes: 11 additions & 0 deletions libc-bottom-half/headers/private/wasi/descriptor_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include <sys/stat.h>
#include <netinet/in.h>

#ifdef __wasip3__
// create an alias to distinguish the handle type in the API
typedef uint32_t waitable_t;
#endif

/**
* Operations that are required of all descriptors registered as file
* descriptors.
Expand Down Expand Up @@ -37,6 +42,12 @@ typedef struct descriptor_vtable_t {
/// Same as `get_read_stream`, but for output streams.
int (*get_write_stream)(void*, streams_borrow_output_stream_t*, off_t**, poll_own_pollable_t**);
#endif
#ifdef __wasip3__
/// Start an asynchronous read or write, returns zero on success.
/// Stores the waitable, status and offset location.
int (*read3)(void*, void *buf, size_t nbyte, waitable_t *waitable, wasip3_waitable_status_t *out, off_t**);
int (*write3)(void*, void const *buf, size_t nbyte, waitable_t *waitable, wasip3_waitable_status_t *out, off_t**);
#endif

/// Sets the nonblocking flag for this object to the specified value.
int (*set_blocking)(void*, bool);
Expand Down
17 changes: 16 additions & 1 deletion libc-bottom-half/headers/private/wasi/file_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

#include <wasi/version.h>

#ifdef __wasip2__
#if defined(__wasip2__) || defined(__wasip3__)
#include <assert.h>
#include <wasi/wasip2.h>
#include <wasi/descriptor_table.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#endif

#ifdef __wasip2__
/// Handles a `wasi:io/streams.stream-error` for a `read`-style operation.
///
/// If the error indicates "closed" then 0 is returned to mean EOF. Otherwise
Expand Down Expand Up @@ -46,7 +48,9 @@ static int wasip2_handle_write_error(streams_stream_error_t error) {
// Returns 0 if `s` is valid utf-8.
// Returns -1 and sets errno to `ENOENT` if `s` is not valid utf-8.
int wasip2_string_from_c(const char *s, wasip2_string_t* out);
#endif

#if defined(__wasip2__) || defined(__wasip3__)
// Succeed only if fd is bound to a file handle in the descriptor table
static int fd_to_file_handle(int fd, filesystem_borrow_descriptor_t* result) {
descriptor_table_entry_t* entry = descriptor_table_get_ref(fd);
Expand All @@ -58,7 +62,9 @@ static int fd_to_file_handle(int fd, filesystem_borrow_descriptor_t* result) {
}
return entry->vtable->get_file(entry->data, result);
}
#endif

#ifdef __wasip2__
// Gets an `output-stream` borrow from the `fd` provided.
int __wasilibc_write_stream(int fd,
streams_borrow_output_stream_t *out,
Expand Down Expand Up @@ -96,4 +102,13 @@ static unsigned dir_entry_type_to_d_type(filesystem_descriptor_type_t ty) {

#endif

#ifdef __wasip3__
#include <wasi/descriptor_table.h>

int wasip3_string_from_c(const char *s, wasip3_string_t* out);

ssize_t __wasilibc_write3(int fildes, void const *buf, size_t nbyte);
ssize_t __wasilibc_read3(int fildes, void *buf, size_t nbyte);
#endif

#endif
Loading
Loading