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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ jobs:
submodules: true
- run: cmake -S . -B build -DBUILD_TESTS=ON -G Ninja -DCMAKE_C_COMPILER=clang
- 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: ninja -C build format-check

rustfmt:
name: Rustfmt
Expand Down
3 changes: 3 additions & 0 deletions cmake/clang-format.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ add_custom_target(format-check)
set(formatted_sources)

function(clang_format_file file)
if (file MATCHES "wasip2\..$") # Skip auto-generated files
return()
endif()
cmake_path(ABSOLUTE_PATH file BASE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} OUTPUT_VARIABLE src)

# Only format sources primarily authored in this repository itself. This
Expand Down
19 changes: 9 additions & 10 deletions libc-bottom-half/sources/bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
#include <netinet/in.h>
#include <wasi/sockets_utils.h>

int bind(int socket, const struct sockaddr *addr, socklen_t addrlen)
{
descriptor_table_entry_t *entry = descriptor_table_get_ref(socket);
if (!entry)
return -1;
if (!entry->vtable->bind) {
errno = EOPNOTSUPP;
return -1;
}
return entry->vtable->bind(entry->data, addr, addrlen);
int bind(int socket, const struct sockaddr *addr, socklen_t addrlen) {
descriptor_table_entry_t *entry = descriptor_table_get_ref(socket);
if (!entry)
return -1;
if (!entry->vtable->bind) {
errno = EOPNOTSUPP;
return -1;
}
return entry->vtable->bind(entry->data, addr, addrlen);
}
19 changes: 9 additions & 10 deletions libc-bottom-half/sources/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
#include <netinet/in.h>
#include <wasi/descriptor_table.h>

int connect(int fd, const struct sockaddr *addr, socklen_t addrlen)
{
descriptor_table_entry_t *entry = descriptor_table_get_ref(fd);
if (!entry)
return -1;
if (!entry->vtable->connect) {
errno = EOPNOTSUPP;
return -1;
}
return entry->vtable->connect(entry->data, addr, addrlen);
int connect(int fd, const struct sockaddr *addr, socklen_t addrlen) {
descriptor_table_entry_t *entry = descriptor_table_get_ref(fd);
if (!entry)
return -1;
if (!entry->vtable->connect) {
errno = EOPNOTSUPP;
return -1;
}
return entry->vtable->connect(entry->data, addr, addrlen);
}
111 changes: 53 additions & 58 deletions libc-bottom-half/sources/descriptor_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,29 @@
#include <wasi/stdio.h>

#define MINSIZE 8
#define MAXSIZE ((size_t)-1 / 2 + 1)
#define MAXSIZE ((size_t) - 1 / 2 + 1)

typedef struct {
bool occupied;
union {
int next;
descriptor_table_entry_t entry;
};
bool occupied;
union {
int next;
descriptor_table_entry_t entry;
};
} descriptor_table_item_t;

typedef struct {
// Dynamically allocated array of `cap` entries.
descriptor_table_item_t *entries;
// Next free entry.
int next;
// Number of `entries` that are initialized.
size_t len;
// Dynamic length of `entries`.
size_t cap;
// Dynamically allocated array of `cap` entries.
descriptor_table_item_t *entries;
// Next free entry.
int next;
// Number of `entries` that are initialized.
size_t len;
// Dynamic length of `entries`.
size_t cap;
} descriptor_table_t;

static descriptor_table_t global_table = { .entries = NULL,
.next = 0,
.len = 0,
.cap = 0 };
static descriptor_table_t global_table = {
.entries = NULL, .next = 0, .len = 0, .cap = 0};
static int global_table_stdio_initialized = 0;

/**
Expand Down Expand Up @@ -106,7 +104,8 @@ static descriptor_table_entry_t *lookup(descriptor_table_t *table, int fd) {
*
* Returns -1 on failure and sets `errno`.
*/
static int remove(descriptor_table_t *table, int fd, descriptor_table_entry_t *ret) {
static int remove(descriptor_table_t *table, int fd,
descriptor_table_entry_t *ret) {
if (fd < 0 || (size_t)fd >= table->len) {
errno = EBADF;
return -1;
Expand All @@ -133,51 +132,47 @@ static int init_stdio() {
return __wasilibc_init_stdio();
}

int descriptor_table_insert(descriptor_table_entry_t entry)
{
if (!stdio_initialized && init_stdio() < 0)
goto error;
int fd = allocate(&global_table, entry);
if (fd < 0)
goto error;
return fd;
int descriptor_table_insert(descriptor_table_entry_t entry) {
if (!stdio_initialized && init_stdio() < 0)
goto error;
int fd = allocate(&global_table, entry);
if (fd < 0)
goto error;
return fd;
error:
entry.vtable->free(entry.data);
return -1;
entry.vtable->free(entry.data);
return -1;
}

descriptor_table_entry_t *descriptor_table_get_ref(int fd)
{
if (!stdio_initialized && init_stdio() < 0)
return NULL;
return lookup(&global_table, fd);
descriptor_table_entry_t *descriptor_table_get_ref(int fd) {
if (!stdio_initialized && init_stdio() < 0)
return NULL;
return lookup(&global_table, fd);
}

int descriptor_table_renumber(int fd, int newfd)
{
descriptor_table_entry_t* fdentry = descriptor_table_get_ref(fd);
if (!fdentry)
return -1;
descriptor_table_entry_t* newfdentry = descriptor_table_get_ref(newfd);
if (!newfdentry)
return -1;
int descriptor_table_renumber(int fd, int newfd) {
descriptor_table_entry_t *fdentry = descriptor_table_get_ref(fd);
if (!fdentry)
return -1;
descriptor_table_entry_t *newfdentry = descriptor_table_get_ref(newfd);
if (!newfdentry)
return -1;

descriptor_table_entry_t temp = *fdentry;
*fdentry = *newfdentry;
*newfdentry = temp;
if (remove(&global_table, fd, &temp) < 0)
return -1;
temp.vtable->free(temp.data);
return 0;
descriptor_table_entry_t temp = *fdentry;
*fdentry = *newfdentry;
*newfdentry = temp;
if (remove(&global_table, fd, &temp) < 0)
return -1;
temp.vtable->free(temp.data);
return 0;
}

int descriptor_table_remove(int fd)
{
if (!stdio_initialized && init_stdio() < 0)
return -1;
descriptor_table_entry_t entry;
if (remove(&global_table, fd, &entry) < 0)
return -1;
entry.vtable->free(entry.data);
return 0;
int descriptor_table_remove(int fd) {
if (!stdio_initialized && init_stdio() < 0)
return -1;
descriptor_table_entry_t entry;
if (remove(&global_table, fd, &entry) < 0)
return -1;
entry.vtable->free(entry.data);
return 0;
}
38 changes: 18 additions & 20 deletions libc-bottom-half/sources/getsockpeername.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@
#include <wasi/descriptor_table.h>

int getsockname(int socket, struct sockaddr *__restrict addr,
socklen_t *__restrict addrlen)
{
descriptor_table_entry_t *entry = descriptor_table_get_ref(socket);
if (!entry)
return -1;
if (!entry->vtable->getsockname) {
errno = ENOTSOCK;
return -1;
}
return entry->vtable->getsockname(entry->data, addr, addrlen);
socklen_t *__restrict addrlen) {
descriptor_table_entry_t *entry = descriptor_table_get_ref(socket);
if (!entry)
return -1;
if (!entry->vtable->getsockname) {
errno = ENOTSOCK;
return -1;
}
return entry->vtable->getsockname(entry->data, addr, addrlen);
}

int getpeername(int socket, struct sockaddr *__restrict addr,
socklen_t *__restrict addrlen)
{
descriptor_table_entry_t *entry = descriptor_table_get_ref(socket);
if (!entry)
return -1;
if (!entry->vtable->getpeername) {
errno = ENOTSOCK;
return -1;
}
return entry->vtable->getpeername(entry->data, addr, addrlen);
socklen_t *__restrict addrlen) {
descriptor_table_entry_t *entry = descriptor_table_get_ref(socket);
if (!entry)
return -1;
if (!entry->vtable->getpeername) {
errno = ENOTSOCK;
return -1;
}
return entry->vtable->getpeername(entry->data, addr, addrlen);
}
33 changes: 16 additions & 17 deletions libc-bottom-half/sources/listen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
#include <netinet/in.h>
#include <wasi/descriptor_table.h>

int listen(int socket, int backlog)
{
descriptor_table_entry_t *entry = descriptor_table_get_ref(socket);
if (!entry)
return -1;
int listen(int socket, int backlog) {
descriptor_table_entry_t *entry = descriptor_table_get_ref(socket);
if (!entry)
return -1;

if (backlog < 0) {
// POSIX:
// > If listen() is called with a backlog argument value that is
// > less than 0, the function behaves as if it had been called
// > with a backlog argument value of 0.
backlog = 0;
}
if (!entry->vtable->listen) {
errno = EOPNOTSUPP;
return -1;
}
return entry->vtable->listen(entry->data, backlog);
if (backlog < 0) {
// POSIX:
// > If listen() is called with a backlog argument value that is
// > less than 0, the function behaves as if it had been called
// > with a backlog argument value of 0.
backlog = 0;
}
if (!entry->vtable->listen) {
errno = EOPNOTSUPP;
return -1;
}
return entry->vtable->listen(entry->data, backlog);
}
Loading