Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faasm vs wasi-libc:main #5

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
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
29 changes: 27 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
legacy/getpagesize.c \
thread/thrd_sleep.c \
) \
$(filter-out %/procfdname.c %/syscall.c %/syscall_ret.c %/vdso.c %/version.c, \
$(filter-out %/procfdname.c %/vdso.c %/version.c, \
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal/*.c)) \
$(filter-out %/flockfile.c %/funlockfile.c %/__lockfile.c %/ftrylockfile.c \
%/rename.c \
Expand Down Expand Up @@ -329,6 +329,13 @@ CFLAGS += -mthread-model posix -pthread -ftls-model=local-exec
# Include cloudlib's directory to access the structure definition of clockid_t
CFLAGS += -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
endif
# Faasm: We **must** avoid introducing atomics, but still want to include the
# ability to do threading. See:
# https://reviews.llvm.org/D59281
CFLAGS += -mno-atomics -mthread-model posix -ftls-model=local-exec

# Faasm: dd Faasm def for conditional compilation of some headers
CFLAGS += -D__faasm

# Expose the public headers to the implementation. We use `-isystem` for
# purpose for two reasons:
Expand All @@ -343,6 +350,8 @@ endif
# and/or `#include_next` the public headers. `-isystem` (like
# `-idirafter`) comes later in the search path than `-I`.
CFLAGS += -isystem "$(SYSROOT_INC)"
# Set the sysroot.
CFLAGS += --sysroot="$(SYSROOT)"

# These variables describe the locations of various files and directories in
# the build tree.
Expand Down Expand Up @@ -387,7 +396,7 @@ SYSROOT_SHARE = $(SYSROOT)/share/$(TARGET_TRIPLE)
# sysroot's include directory.
MUSL_OMIT_HEADERS :=

# Remove files which aren't headers (we generate alltypes.h below).
# Remove files which aren't headers (we generate alltypes.h and syscall.h below).
MUSL_OMIT_HEADERS += \
"bits/syscall.h.in" \
"bits/alltypes.h.in" \
Expand All @@ -402,6 +411,9 @@ MUSL_OMIT_HEADERS += \
MUSL_OMIT_HEADERS += \
"bits/errno.h"

ifeq ($(THREAD_MODEL), faasm)
# Include all other headers in Faasm
else
# Remove headers that aren't supported yet or that aren't relevant for WASI.
MUSL_OMIT_HEADERS += \
"sys/procfs.h" \
Expand Down Expand Up @@ -462,6 +474,7 @@ MUSL_OMIT_HEADERS += \
"libintl.h" \
"sys/sysmacros.h" \
"aio.h"
endif

ifeq ($(THREAD_MODEL), single)
# Remove headers not supported in single-threaded mode.
Expand Down Expand Up @@ -503,13 +516,17 @@ $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS): CFLAGS += \
-D__wasilibc_printscan_no_floating_point \
-D__wasilibc_printscan_floating_point_support_option="\"remove -lc-printscan-no-floating-point from the link command\""

# Faasm: we disable bulk-memory in Faasm as it breaks our page-granular memory
# management model
ifneq ($(THREAD_MODEL), faasm)
# TODO: apply -mbulk-memory globally, once
# https://github.com/llvm/llvm-project/issues/52618 is resolved
$(BULK_MEMORY_OBJS): CFLAGS += \
-mbulk-memory

$(BULK_MEMORY_OBJS): CFLAGS += \
-DBULK_MEMORY_THRESHOLD=$(BULK_MEMORY_THRESHOLD)
endif

$(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS): CFLAGS += \
-D_WASI_EMULATED_SIGNAL
Expand Down Expand Up @@ -573,6 +590,11 @@ include_dirs:
$(LIBC_TOP_HALF_MUSL_DIR)/include/alltypes.h.in \
> "$(SYSROOT_INC)/bits/alltypes.h"

# Generate musl's syscall header.
sed -n -e s/__NR_/SYS_/p < \
$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32/bits/syscall.h.in \
> "$(SYSROOT_INC)/bits/syscall.h"

# Copy in the bulk of musl's public header files.
cp -r "$(LIBC_TOP_HALF_MUSL_INC)"/* "$(SYSROOT_INC)"
# Copy in the musl's "bits" header files.
Expand Down Expand Up @@ -617,8 +639,11 @@ finish: startup_files libc
# alloctor (providing malloc, calloc, free, etc). Skip this step if the build
# is done without a malloc implementation.
ifneq ($(MALLOC_IMPL),none)
# Faasm: we don't do the symbol check
ifneq ($(THREAD_MODEL), faasm)
finish: check-symbols
endif
endif

DEFINED_SYMBOLS = $(SYSROOT_SHARE)/defined-symbols.txt
UNDEFINED_SYMBOLS = $(SYSROOT_SHARE)/undefined-symbols.txt
Expand Down
15 changes: 15 additions & 0 deletions dlmalloc/src/dlmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,23 @@
// WebAssembly doesn't have mmap-style memory allocation.
#define HAVE_MMAP 0

#ifdef __faasm
// Faasm supports memory growth and shrinkage with sbrk-style positive and
// negative allocation. We need to be explicit about sbrk usage (HAVE_MORECORE
// set to 1) and not mmap/munmap usage (HAVE_MMAP set to 0, default in WASM).
// Note that this does not mean that we don't support application code using
// mmap/munmap, it means that heap is only managed with sbrk
#define HAVE_MORECORE 1
#define MORECORE_CONTIGUOUS 1

// We don't shrink the linear memory, but claim the pages for the runtime, and
// move the offset down, so that subsequent allocations can re-use the free-ed
// memory
#define MORECORE_CANNOT_TRIM 0
#else
// WebAssembly doesn't support shrinking linear memory.
#define MORECORE_CANNOT_TRIM 1
#endif

// Disable sanity checks to reduce code size.
#define ABORT __builtin_unreachable()
Expand Down
10 changes: 10 additions & 0 deletions libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ int fcntl(int fildes, int cmd, ...) {
}
return 0;
}
case F_DUPFD: {
int new_fd = -1;
__wasi_errno_t error = __wasi_fd_dup(fildes, &new_fd);

if(error != 0) {
errno = error;
return -1;
}
return new_fd;
}
default:
errno = EINVAL;
return -1;
Expand Down
5 changes: 5 additions & 0 deletions libc-bottom-half/headers/public/__header_fcntl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <wasi/api.h>
#include <__seek.h>
#include <__mode_t.h>
#include <__typedef_off_t.h>

#define O_APPEND __WASI_FDFLAGS_APPEND
#define O_DSYNC __WASI_FDFLAGS_DSYNC
Expand Down Expand Up @@ -44,6 +45,7 @@
#define POSIX_FADV_SEQUENTIAL __WASI_ADVICE_SEQUENTIAL
#define POSIX_FADV_WILLNEED __WASI_ADVICE_WILLNEED

#define F_DUPFD (0)
#define F_GETFD (1)
#define F_SETFD (2)
#define F_GETFL (3)
Expand All @@ -57,5 +59,8 @@
#define AT_REMOVEDIR (0x4)

#define AT_FDCWD (-2)
#ifdef __faasm
int lockf(int, int, off_t);
#endif

#endif
1 change: 1 addition & 0 deletions libc-bottom-half/headers/public/__header_poll.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#define POLLIN POLLRDNORM
#define POLLOUT POLLWRNORM
#define POLLPRI 0x3

#define POLLERR 0x1000
#define POLLHUP 0x2000
Expand Down
20 changes: 20 additions & 0 deletions libc-bottom-half/headers/public/__header_sys_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,36 @@
#define __wasilibc___header_sys_resource_h

#include <__struct_rusage.h>
#include <__struct_rlimit.h>

#define RUSAGE_SELF 1
#define RUSAGE_CHILDREN 2

#define RLIMIT_CPU 0
#define RLIMIT_FSIZE 1
#define RLIMIT_DATA 2
#define RLIMIT_STACK 3
#define RLIMIT_CORE 4
#ifndef RLIMIT_RSS
#define RLIMIT_RSS 5
#define RLIMIT_NPROC 6
#define RLIMIT_NOFILE 7
#define RLIMIT_MEMLOCK 8
#define RLIMIT_AS 9
#endif

#ifdef __cplusplus
extern "C" {
#endif

int getrlimit (int resource, struct rlimit *);
int setrlimit (int resource, const struct rlimit *);

int getrusage(int who, struct rusage *usage);

int getpriority (int which, id_t who);
int setpriority (int which, id_t who, int prio);

#ifdef __cplusplus
}
#endif
Expand Down
13 changes: 13 additions & 0 deletions libc-bottom-half/headers/public/__struct_rlimit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef __wasilibc___struct_rlimit_h
#define __wasilibc___struct_rlimit_h

#include <__struct_timeval.h>

typedef unsigned long long rlim_t;

struct rlimit {
rlim_t rlim_cur;
rlim_t rlim_max;
};

#endif
9 changes: 8 additions & 1 deletion libc-bottom-half/headers/public/wasi/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ _Static_assert(_Alignof(__wasi_whence_t) == 1, "witx calculated align");

/**
* A reference to the offset of a directory entry.
*
*
* The value 0 signifies the start of the directory.
*/
typedef uint64_t __wasi_dircookie_t;
Expand Down Expand Up @@ -1737,6 +1737,13 @@ __wasi_errno_t __wasi_fd_seek(
__wasi_errno_t __wasi_fd_sync(
__wasi_fd_t fd
) __attribute__((__warn_unused_result__));
/**
* Faasm: add header for wasi_fd_dup
*/
__wasi_errno_t __wasi_fd_dup(
int fildes,
__wasi_fd_t *new_fd
) __attribute__((__warn_unused_result__));
/**
* Return the current offset of a file descriptor.
* Note: This is similar to `lseek(fd, 0, SEEK_CUR)` in POSIX.
Expand Down
9 changes: 9 additions & 0 deletions libc-bottom-half/sources/sbrk.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
#include <errno.h>
#include <__macro_PAGESIZE.h>

#ifdef __faasm
extern void* __sbrk(intptr_t increment);
#endif

// Bare-bones implementation of sbrk.
void *sbrk(intptr_t increment) {
#ifdef __faasm
// In Faasm we defer to our custom sbrk implementation
return __sbrk(increment);
#else
// sbrk(0) returns the current memory size.
if (increment == 0) {
// The wasm spec doesn't guarantee that memory.grow of 0 always succeeds.
Expand All @@ -29,4 +37,5 @@ void *sbrk(intptr_t increment) {
}

return (void *)(old * PAGESIZE);
#endif
}
22 changes: 22 additions & 0 deletions libc-top-half/musl/arch/wasm32/bits/fenv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#define FE_ALL_EXCEPT 0

#define FE_INVALID 1
#define FE_DIVBYZERO 4
#define FE_OVERFLOW 8
#define FE_UNDERFLOW 16
#define FE_INEXACT 32
#define FE_TONEAREST 0
#define FE_DOWNWARD 0x400
#define FE_UPWARD 0x800
#define FE_TOWARDZERO 0xc00


typedef unsigned long fexcept_t;

typedef struct {
unsigned long __cw;
} fenv_t;

#define FE_DFL_ENV ((const fenv_t *) -1)


1 change: 1 addition & 0 deletions libc-top-half/musl/arch/wasm32/bits/setjmp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typedef unsigned long __jmp_buf[6];
Loading