From 1c407571d2d856b04710b0cf22e149e935f31bb7 Mon Sep 17 00:00:00 2001 From: Simon Shillaker Date: Fri, 27 Mar 2020 11:11:40 +0000 Subject: [PATCH 1/4] faasm: one-commit-diff on top of upstream wasi-libc faasm: remove unnecessary things from diff milestone: inv llvm.libc --clean running faasm: disable bulk-memory faasm: milestone, all functions cross-compiling faasm: clapack compiling python: changes to cross-compile cpython remove duplicated declaration in header sbrk: disable faasm-managed sbrk and add patch to re-enable it simplify diff more simplify and fixes for tensorflow more simplifying libc: use wasi-libc mmap/mmunmap implementation patch: remove trailing whitespaces from patch faasm: update patch to switch on sbrk to actually work patch: rename patch patch: fix inception issue simd: wasm_simd128 header is distributed as part of llvm now makefile: cleanup bulk-memory: make into a patch cleanup patches: remove faasm-patches --- Makefile | 25 +- .../cloudlibc/src/libc/fcntl/fcntl.c | 10 + .../headers/public/__header_fcntl.h | 5 + .../headers/public/__header_poll.h | 1 + .../headers/public/__header_sys_resource.h | 20 + .../headers/public/__struct_rlimit.h | 13 + libc-bottom-half/headers/public/wasi/api.h | 9 +- libc-top-half/musl/arch/wasm32/bits/fenv.h | 22 + libc-top-half/musl/arch/wasm32/bits/setjmp.h | 1 + .../musl/arch/wasm32/bits/syscall.h.in | 385 ++++++++++++++++++ libc-top-half/musl/arch/wasm32/syscall_arch.h | 35 ++ libc-top-half/musl/include/arpa/inet.h | 2 +- libc-top-half/musl/include/pthread.h | 5 +- libc-top-half/musl/include/setjmp.h | 2 +- libc-top-half/musl/include/signal.h | 11 +- libc-top-half/musl/include/stdio.h | 6 +- libc-top-half/musl/include/stdlib.h | 4 +- libc-top-half/musl/include/sys/socket.h | 12 +- libc-top-half/musl/include/sys/stat.h | 4 +- libc-top-half/musl/include/sys/syscall.h | 2 +- libc-top-half/musl/include/unistd.h | 25 +- libc-top-half/musl/src/internal/syscall.h | 2 +- libc-top-half/musl/src/thread/pthread_join.c | 7 +- 23 files changed, 568 insertions(+), 40 deletions(-) create mode 100644 libc-bottom-half/headers/public/__struct_rlimit.h create mode 100644 libc-top-half/musl/arch/wasm32/bits/fenv.h create mode 100644 libc-top-half/musl/arch/wasm32/bits/setjmp.h create mode 100644 libc-top-half/musl/arch/wasm32/bits/syscall.h.in diff --git a/Makefile b/Makefile index 8084ea8fb..47a4adadd 100644 --- a/Makefile +++ b/Makefile @@ -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 \ @@ -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: @@ -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. @@ -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" \ @@ -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" \ @@ -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. @@ -573,6 +586,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. @@ -617,8 +635,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 diff --git a/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c b/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c index 5d4055dd5..f55357058 100644 --- a/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c +++ b/libc-bottom-half/cloudlibc/src/libc/fcntl/fcntl.c @@ -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; diff --git a/libc-bottom-half/headers/public/__header_fcntl.h b/libc-bottom-half/headers/public/__header_fcntl.h index 04252acc4..bbe65855c 100644 --- a/libc-bottom-half/headers/public/__header_fcntl.h +++ b/libc-bottom-half/headers/public/__header_fcntl.h @@ -4,6 +4,7 @@ #include #include <__seek.h> #include <__mode_t.h> +#include <__typedef_off_t.h> #define O_APPEND __WASI_FDFLAGS_APPEND #define O_DSYNC __WASI_FDFLAGS_DSYNC @@ -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) @@ -57,5 +59,8 @@ #define AT_REMOVEDIR (0x4) #define AT_FDCWD (-2) +#ifdef __faasm +int lockf(int, int, off_t); +#endif #endif diff --git a/libc-bottom-half/headers/public/__header_poll.h b/libc-bottom-half/headers/public/__header_poll.h index 23b36f2f2..69b071edf 100644 --- a/libc-bottom-half/headers/public/__header_poll.h +++ b/libc-bottom-half/headers/public/__header_poll.h @@ -9,6 +9,7 @@ #define POLLIN POLLRDNORM #define POLLOUT POLLWRNORM +#define POLLPRI 0x3 #define POLLERR 0x1000 #define POLLHUP 0x2000 diff --git a/libc-bottom-half/headers/public/__header_sys_resource.h b/libc-bottom-half/headers/public/__header_sys_resource.h index bae9d5aac..b96e48b28 100644 --- a/libc-bottom-half/headers/public/__header_sys_resource.h +++ b/libc-bottom-half/headers/public/__header_sys_resource.h @@ -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 diff --git a/libc-bottom-half/headers/public/__struct_rlimit.h b/libc-bottom-half/headers/public/__struct_rlimit.h new file mode 100644 index 000000000..15963f56c --- /dev/null +++ b/libc-bottom-half/headers/public/__struct_rlimit.h @@ -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 diff --git a/libc-bottom-half/headers/public/wasi/api.h b/libc-bottom-half/headers/public/wasi/api.h index 45a6506e2..19f9b1eb3 100644 --- a/libc-bottom-half/headers/public/wasi/api.h +++ b/libc-bottom-half/headers/public/wasi/api.h @@ -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; @@ -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. diff --git a/libc-top-half/musl/arch/wasm32/bits/fenv.h b/libc-top-half/musl/arch/wasm32/bits/fenv.h new file mode 100644 index 000000000..31320c39a --- /dev/null +++ b/libc-top-half/musl/arch/wasm32/bits/fenv.h @@ -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) + + diff --git a/libc-top-half/musl/arch/wasm32/bits/setjmp.h b/libc-top-half/musl/arch/wasm32/bits/setjmp.h new file mode 100644 index 000000000..decd26dca --- /dev/null +++ b/libc-top-half/musl/arch/wasm32/bits/setjmp.h @@ -0,0 +1 @@ +typedef unsigned long __jmp_buf[6]; diff --git a/libc-top-half/musl/arch/wasm32/bits/syscall.h.in b/libc-top-half/musl/arch/wasm32/bits/syscall.h.in new file mode 100644 index 000000000..119bfe901 --- /dev/null +++ b/libc-top-half/musl/arch/wasm32/bits/syscall.h.in @@ -0,0 +1,385 @@ +#define __NR_restart_syscall 0 +#define __NR_exit 1 +#define __NR_fork 2 +#define __NR_read 3 +#define __NR_write 4 +#define __NR_open 5 +#define __NR_close 6 +#define __NR_waitpid 7 +#define __NR_creat 8 +#define __NR_link 9 +#define __NR_unlink 10 +#define __NR_execve 11 +#define __NR_chdir 12 +#define __NR_time 13 +#define __NR_mknod 14 +#define __NR_chmod 15 +#define __NR_lchown 16 +#define __NR_break 17 +#define __NR_oldstat 18 +#define __NR_lseek 19 +#define __NR_getpid 20 +#define __NR_mount 21 +#define __NR_umount 22 +#define __NR_setuid 23 +#define __NR_getuid 24 +#define __NR_stime 25 +#define __NR_ptrace 26 +#define __NR_alarm 27 +#define __NR_oldfstat 28 +#define __NR_pause 29 +#define __NR_utime 30 +#define __NR_stty 31 +#define __NR_gtty 32 +#define __NR_access 33 +#define __NR_nice 34 +#define __NR_ftime 35 +#define __NR_sync 36 +#define __NR_kill 37 +#define __NR_rename 38 +#define __NR_mkdir 39 +#define __NR_rmdir 40 +#define __NR_dup 41 +#define __NR_pipe 42 +#define __NR_times 43 +#define __NR_prof 44 +#define __NR_brk 45 +#define __NR_setgid 46 +#define __NR_getgid 47 +#define __NR_signal 48 +#define __NR_geteuid 49 +#define __NR_getegid 50 +#define __NR_acct 51 +#define __NR_umount2 52 +#define __NR_lock 53 +#define __NR_ioctl 54 +#define __NR_fcntl 55 +#define __NR_mpx 56 +#define __NR_setpgid 57 +#define __NR_ulimit 58 +#define __NR_oldolduname 59 +#define __NR_umask 60 +#define __NR_chroot 61 +#define __NR_ustat 62 +#define __NR_dup2 63 +#define __NR_getppid 64 +#define __NR_getpgrp 65 +#define __NR_setsid 66 +#define __NR_sigaction 67 +#define __NR_sgetmask 68 +#define __NR_ssetmask 69 +#define __NR_setreuid 70 +#define __NR_setregid 71 +#define __NR_sigsuspend 72 +#define __NR_sigpending 73 +#define __NR_sethostname 74 +#define __NR_setrlimit 75 +#define __NR_getrlimit 76 /* Back compatible 2Gig limited rlimit */ +#define __NR_getrusage 77 +#define __NR_gettimeofday 78 +#define __NR_settimeofday 79 +#define __NR_getgroups 80 +#define __NR_setgroups 81 +#define __NR_select 82 +#define __NR_symlink 83 +#define __NR_oldlstat 84 +#define __NR_readlink 85 +#define __NR_uselib 86 +#define __NR_swapon 87 +#define __NR_reboot 88 +#define __NR_readdir 89 +#define __NR_mmap 90 +#define __NR_munmap 91 +#define __NR_truncate 92 +#define __NR_ftruncate 93 +#define __NR_fchmod 94 +#define __NR_fchown 95 +#define __NR_getpriority 96 +#define __NR_setpriority 97 +#define __NR_profil 98 +#define __NR_statfs 99 +#define __NR_fstatfs 100 +#define __NR_ioperm 101 +#define __NR_socketcall 102 +#define __NR_syslog 103 +#define __NR_setitimer 104 +#define __NR_getitimer 105 +#define __NR_stat 106 +#define __NR_lstat 107 +#define __NR_fstat 108 +#define __NR_olduname 109 +#define __NR_iopl 110 +#define __NR_vhangup 111 +#define __NR_idle 112 +#define __NR_vm86old 113 +#define __NR_wait4 114 +#define __NR_swapoff 115 +#define __NR_sysinfo 116 +#define __NR_ipc 117 +#define __NR_fsync 118 +#define __NR_sigreturn 119 +#define __NR_clone 120 +#define __NR_setdomainname 121 +#define __NR_uname 122 +#define __NR_modify_ldt 123 +#define __NR_adjtimex 124 +#define __NR_mprotect 125 +#define __NR_sigprocmask 126 +#define __NR_create_module 127 +#define __NR_init_module 128 +#define __NR_delete_module 129 +#define __NR_get_kernel_syms 130 +#define __NR_quotactl 131 +#define __NR_getpgid 132 +#define __NR_fchdir 133 +#define __NR_bdflush 134 +#define __NR_sysfs 135 +#define __NR_personality 136 +#define __NR_afs_syscall 137 +#define __NR_setfsuid 138 +#define __NR_setfsgid 139 +#define __NR__llseek 140 +#define __NR_getdents 141 +#define __NR__newselect 142 +#define __NR_flock 143 +#define __NR_msync 144 +#define __NR_readv 145 +#define __NR_writev 146 +#define __NR_getsid 147 +#define __NR_fdatasync 148 +#define __NR__sysctl 149 +#define __NR_mlock 150 +#define __NR_munlock 151 +#define __NR_mlockall 152 +#define __NR_munlockall 153 +#define __NR_sched_setparam 154 +#define __NR_sched_getparam 155 +#define __NR_sched_setscheduler 156 +#define __NR_sched_getscheduler 157 +#define __NR_sched_yield 158 +#define __NR_sched_get_priority_max 159 +#define __NR_sched_get_priority_min 160 +#define __NR_sched_rr_get_interval 161 +#define __NR_nanosleep 162 +#define __NR_mremap 163 +#define __NR_setresuid 164 +#define __NR_getresuid 165 +#define __NR_vm86 166 +#define __NR_query_module 167 +#define __NR_poll 168 +#define __NR_nfsservctl 169 +#define __NR_setresgid 170 +#define __NR_getresgid 171 +#define __NR_prctl 172 +#define __NR_rt_sigreturn 173 +#define __NR_rt_sigaction 174 +#define __NR_rt_sigprocmask 175 +#define __NR_rt_sigpending 176 +#define __NR_rt_sigtimedwait 177 +#define __NR_rt_sigqueueinfo 178 +#define __NR_rt_sigsuspend 179 +#define __NR_pread64 180 +#define __NR_pwrite64 181 +#define __NR_chown 182 +#define __NR_getcwd 183 +#define __NR_capget 184 +#define __NR_capset 185 +#define __NR_sigaltstack 186 +#define __NR_sendfile 187 +#define __NR_getpmsg 188 +#define __NR_putpmsg 189 +#define __NR_vfork 190 +#define __NR_ugetrlimit 191 +#define __NR_mmap2 192 +#define __NR_truncate64 193 +#define __NR_ftruncate64 194 +#define __NR_stat64 195 +#define __NR_lstat64 196 +#define __NR_fstat64 197 +#define __NR_lchown32 198 +#define __NR_getuid32 199 +#define __NR_getgid32 200 +#define __NR_geteuid32 201 +#define __NR_getegid32 202 +#define __NR_setreuid32 203 +#define __NR_setregid32 204 +#define __NR_getgroups32 205 +#define __NR_setgroups32 206 +#define __NR_fchown32 207 +#define __NR_setresuid32 208 +#define __NR_getresuid32 209 +#define __NR_setresgid32 210 +#define __NR_getresgid32 211 +#define __NR_chown32 212 +#define __NR_setuid32 213 +#define __NR_setgid32 214 +#define __NR_setfsuid32 215 +#define __NR_setfsgid32 216 +#define __NR_pivot_root 217 +#define __NR_mincore 218 +#define __NR_madvise 219 +#define __NR_getdents64 220 +#define __NR_fcntl64 221 +#define __NR_sbrk 223 +#define __NR_gettid 224 +#define __NR_readahead 225 +#define __NR_setxattr 226 +#define __NR_lsetxattr 227 +#define __NR_fsetxattr 228 +#define __NR_getxattr 229 +#define __NR_lgetxattr 230 +#define __NR_fgetxattr 231 +#define __NR_listxattr 232 +#define __NR_llistxattr 233 +#define __NR_flistxattr 234 +#define __NR_removexattr 235 +#define __NR_lremovexattr 236 +#define __NR_fremovexattr 237 +#define __NR_tkill 238 +#define __NR_sendfile64 239 +#define __NR_futex 240 +#define __NR_sched_setaffinity 241 +#define __NR_sched_getaffinity 242 +#define __NR_set_thread_area 243 +#define __NR_get_thread_area 244 +#define __NR_io_setup 245 +#define __NR_io_destroy 246 +#define __NR_io_getevents 247 +#define __NR_io_submit 248 +#define __NR_io_cancel 249 +#define __NR_fadvise64 250 +/* 251 is available for reuse (was briefly sys_set_zone_reclaim) */ +#define __NR_exit_group 252 +#define __NR_lookup_dcookie 253 +#define __NR_epoll_create 254 +#define __NR_epoll_ctl 255 +#define __NR_epoll_wait 256 +#define __NR_remap_file_pages 257 +#define __NR_set_tid_address 258 +#define __NR_timer_create 259 +#define __NR_timer_settime 260 +#define __NR_timer_gettime 261 +#define __NR_timer_getoverrun 262 +#define __NR_timer_delete 263 +#define __NR_clock_settime 264 +#define __NR_clock_gettime 265 +#define __NR_clock_getres 266 +#define __NR_clock_nanosleep 267 +#define __NR_statfs64 268 +#define __NR_fstatfs64 269 +#define __NR_tgkill 270 +#define __NR_utimes 271 +#define __NR_fadvise64_64 272 +#define __NR_vserver 273 +#define __NR_mbind 274 +#define __NR_get_mempolicy 275 +#define __NR_set_mempolicy 276 +#define __NR_mq_open 277 +#define __NR_mq_unlink 278 +#define __NR_mq_timedsend 279 +#define __NR_mq_timedreceive 280 +#define __NR_mq_notify 281 +#define __NR_mq_getsetattr 282 +#define __NR_kexec_load 283 +#define __NR_waitid 284 +/* #define __NR_sys_setaltroot 285 */ +#define __NR_add_key 286 +#define __NR_request_key 287 +#define __NR_keyctl 288 +#define __NR_ioprio_set 289 +#define __NR_ioprio_get 290 +#define __NR_inotify_init 291 +#define __NR_inotify_add_watch 292 +#define __NR_inotify_rm_watch 293 +#define __NR_migrate_pages 294 +#define __NR_openat 295 +#define __NR_mkdirat 296 +#define __NR_mknodat 297 +#define __NR_fchownat 298 +#define __NR_futimesat 299 +#define __NR_fstatat64 300 +#define __NR_unlinkat 301 +#define __NR_renameat 302 +#define __NR_linkat 303 +#define __NR_symlinkat 304 +#define __NR_readlinkat 305 +#define __NR_fchmodat 306 +#define __NR_faccessat 307 +#define __NR_pselect6 308 +#define __NR_ppoll 309 +#define __NR_unshare 310 +#define __NR_set_robust_list 311 +#define __NR_get_robust_list 312 +#define __NR_splice 313 +#define __NR_sync_file_range 314 +#define __NR_tee 315 +#define __NR_vmsplice 316 +#define __NR_move_pages 317 +#define __NR_getcpu 318 +#define __NR_epoll_pwait 319 +#define __NR_utimensat 320 +#define __NR_signalfd 321 +#define __NR_timerfd_create 322 +#define __NR_eventfd 323 +#define __NR_fallocate 324 +#define __NR_timerfd_settime 325 +#define __NR_timerfd_gettime 326 +#define __NR_signalfd4 327 +#define __NR_eventfd2 328 +#define __NR_epoll_create1 329 +#define __NR_dup3 330 +#define __NR_pipe2 331 +#define __NR_inotify_init1 332 +#define __NR_preadv 333 +#define __NR_pwritev 334 +#define __NR_rt_tgsigqueueinfo 335 +#define __NR_perf_event_open 336 +#define __NR_recvmmsg 337 +#define __NR_fanotify_init 338 +#define __NR_fanotify_mark 339 +#define __NR_prlimit64 340 +#define __NR_name_to_handle_at 341 +#define __NR_open_by_handle_at 342 +#define __NR_clock_adjtime 343 +#define __NR_syncfs 344 +#define __NR_sendmmsg 345 +#define __NR_setns 346 +#define __NR_process_vm_readv 347 +#define __NR_process_vm_writev 348 +#define __NR_kcmp 349 +#define __NR_finit_module 350 +#define __NR_sched_setattr 351 +#define __NR_sched_getattr 352 +#define __NR_renameat2 353 +#define __NR_seccomp 354 +#define __NR_getrandom 355 +#define __NR_memfd_create 356 +#define __NR_bpf 357 +#define __NR_execveat 358 +#define __NR_socket 359 +#define __NR_socketpair 360 +#define __NR_bind 361 +#define __NR_connect 362 +#define __NR_listen 363 +#define __NR_accept4 364 +#define __NR_getsockopt 365 +#define __NR_setsockopt 366 +#define __NR_getsockname 367 +#define __NR_getpeername 368 +#define __NR_sendto 369 +#define __NR_sendmsg 370 +#define __NR_recvfrom 371 +#define __NR_recvmsg 372 +#define __NR_shutdown 373 +#define __NR_userfaultfd 374 +#define __NR_membarrier 375 +#define __NR_mlock2 376 +#define __NR_copy_file_range 377 +#define __NR_preadv2 378 +#define __NR_pwritev2 379 +#define __NR_pkey_mprotect 380 +#define __NR_pkey_alloc 381 +#define __NR_pkey_free 382 +#define __NR_statx 383 +#define __NR_arch_prctl 384 + diff --git a/libc-top-half/musl/arch/wasm32/syscall_arch.h b/libc-top-half/musl/arch/wasm32/syscall_arch.h index e69de29bb..6f55abb35 100644 --- a/libc-top-half/musl/arch/wasm32/syscall_arch.h +++ b/libc-top-half/musl/arch/wasm32/syscall_arch.h @@ -0,0 +1,35 @@ +#define __SYSCALL_LL_E(x) \ +((union { long long ll; long l[2]; }){ .ll = x }).l[0], \ +((union { long long ll; long l[2]; }){ .ll = x }).l[1] +#define __SYSCALL_LL_O(x) 0, __SYSCALL_LL_E((x)) + +#define __SC_socket 1 +#define __SC_bind 2 +#define __SC_connect 3 +#define __SC_listen 4 +#define __SC_accept 5 +#define __SC_getsockname 6 +#define __SC_getpeername 7 +#define __SC_socketpair 8 +#define __SC_send 9 +#define __SC_recv 10 +#define __SC_sendto 11 +#define __SC_recvfrom 12 +#define __SC_shutdown 13 +#define __SC_setsockopt 14 +#define __SC_getsockopt 15 +#define __SC_sendmsg 16 +#define __SC_recvmsg 17 +#define __SC_accept4 18 +#define __SC_recvmmsg 19 +#define __SC_sendmmsg 20 + +long __syscall0(long n); +long __syscall1(long n, long a); +long __syscall2(long n, long a, long b); +long __syscall3(long n, long a, long b, long c); +long __syscall4(long n, long a, long b, long c, long d); +long __syscall5(long n, long a, long b, long c, long d, long e); +long __syscall6(long n, long a, long b, long c, long d, long e, long f); + +#define SYSCALL_USE_SOCKETCALL diff --git a/libc-top-half/musl/include/arpa/inet.h b/libc-top-half/musl/include/arpa/inet.h index f657c30ec..9f95d1a0f 100644 --- a/libc-top-half/musl/include/arpa/inet.h +++ b/libc-top-half/musl/include/arpa/inet.h @@ -13,7 +13,7 @@ uint16_t htons(uint16_t); uint32_t ntohl(uint32_t); uint16_t ntohs(uint16_t); -#ifdef __wasilibc_unmodified_upstream /* WASI has no inet_addr */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no inet_addr */ in_addr_t inet_addr (const char *); in_addr_t inet_network (const char *); char *inet_ntoa (struct in_addr); diff --git a/libc-top-half/musl/include/pthread.h b/libc-top-half/musl/include/pthread.h index b14fe826d..f4d3d7f40 100644 --- a/libc-top-half/musl/include/pthread.h +++ b/libc-top-half/musl/include/pthread.h @@ -55,7 +55,7 @@ extern "C" { #define PTHREAD_PROCESS_SHARED 1 -#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT) +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT) #define PTHREAD_MUTEX_INITIALIZER {{{0}}} #define PTHREAD_RWLOCK_INITIALIZER {{{0}}} #define PTHREAD_COND_INITIALIZER {{{0}}} @@ -85,7 +85,8 @@ extern "C" { int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict); int pthread_detach(pthread_t); -#ifdef __wasilibc_unmodified_upstream +// Faasm: we need the pthread_exit symbol to cross-compile CPython +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) _Noreturn void pthread_exit(void *); #endif int pthread_join(pthread_t, void **); diff --git a/libc-top-half/musl/include/setjmp.h b/libc-top-half/musl/include/setjmp.h index f505f8e4f..6aa74a23c 100644 --- a/libc-top-half/musl/include/setjmp.h +++ b/libc-top-half/musl/include/setjmp.h @@ -7,7 +7,7 @@ extern "C" { #include -#ifdef __wasilibc_unmodified_upstream /* WASI has no setjmp */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no setjmp */ #include typedef struct __jmp_buf_tag { diff --git a/libc-top-half/musl/include/signal.h b/libc-top-half/musl/include/signal.h index 75b5e5528..624a199c3 100644 --- a/libc-top-half/musl/include/signal.h +++ b/libc-top-half/musl/include/signal.h @@ -15,7 +15,7 @@ extern "C" { || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ || defined(_BSD_SOURCE) -#ifdef __wasilibc_unmodified_upstream /* WASI has no ucontext support */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no ucontext support */ #ifdef _GNU_SOURCE #define __ucontext ucontext #endif @@ -57,7 +57,7 @@ typedef struct sigaltstack stack_t; || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ || defined(_BSD_SOURCE) -#ifdef __wasilibc_unmodified_upstream /* WASI has no sigaction */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no ucontext support */ #define SIG_HOLD ((void (*)(int)) 2) #define FPE_INTDIV 1 @@ -220,7 +220,7 @@ int __libc_current_sigrtmax(void); #define SIGRTMAX (__libc_current_sigrtmax()) #endif -#ifdef __wasilibc_unmodified_upstream /* WASI has no signals */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no signals */ int kill(pid_t, int); int sigemptyset(sigset_t *); @@ -239,7 +239,8 @@ int sigtimedwait(const sigset_t *__restrict, siginfo_t *__restrict, const struct int sigqueue(pid_t, int, union sigval); #endif -#ifdef __wasilibc_unmodified_upstream /* WASI has no threads yet */ +// Faasm: we need pthread_sigmask to cross-compile TensorFlow +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no threads yet */ int pthread_sigmask(int, const sigset_t *__restrict, sigset_t *__restrict); int pthread_kill(pthread_t, int); #endif @@ -300,7 +301,7 @@ int sigandset(sigset_t *, const sigset_t *, const sigset_t *); #endif #endif -#ifdef __wasilibc_unmodified_upstream /* 1 is a valid function pointer on wasm */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* 1 is a valid function pointer on wasm */ #define SIG_ERR ((void (*)(int))-1) #define SIG_DFL ((void (*)(int)) 0) #define SIG_IGN ((void (*)(int)) 1) diff --git a/libc-top-half/musl/include/stdio.h b/libc-top-half/musl/include/stdio.h index d63d739f0..69f23ba9d 100644 --- a/libc-top-half/musl/include/stdio.h +++ b/libc-top-half/musl/include/stdio.h @@ -145,7 +145,7 @@ void perror(const char *); int setvbuf(FILE *__restrict, char *__restrict, int, size_t); void setbuf(FILE *__restrict, char *__restrict); -#ifdef __wasilibc_unmodified_upstream /* WASI has no temp directories */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no temp directories */ char *tmpnam(char *); FILE *tmpfile(void); #else @@ -159,7 +159,7 @@ FILE *tmpfile(void) __attribute__((__deprecated__("tmpfile is not defined on WAS FILE *fmemopen(void *__restrict, size_t, const char *__restrict); FILE *open_memstream(char **, size_t *); FILE *fdopen(int, const char *); -#ifdef __wasilibc_unmodified_upstream /* WASI has no popen */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no popen */ FILE *popen(const char *, const char *); int pclose(FILE *); #endif @@ -185,7 +185,7 @@ char *ctermid(char *); #endif -#ifdef __wasilibc_unmodified_upstream /* WASI has no temp directories */ +#if defined(__faasm) || (__wasilibc_unmodified_upstream) /* WASI has no temp directories */ #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ || defined(_BSD_SOURCE) #define P_tmpdir "/tmp" diff --git a/libc-top-half/musl/include/stdlib.h b/libc-top-half/musl/include/stdlib.h index 1bcb9ab0a..c93c2cd34 100644 --- a/libc-top-half/musl/include/stdlib.h +++ b/libc-top-half/musl/include/stdlib.h @@ -115,7 +115,7 @@ size_t __ctype_get_mb_cur_max(void); int posix_memalign (void **, size_t, size_t); int setenv (const char *, const char *, int); int unsetenv (const char *); -#ifdef __wasilibc_unmodified_upstream /* WASI has no temp directories */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no temp directories */ int mkstemp (char *); int mkostemp (char *, int); char *mkdtemp (char *); @@ -128,7 +128,7 @@ int rand_r (unsigned *); #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ || defined(_BSD_SOURCE) -#ifdef __wasilibc_unmodified_upstream /* WASI has no absolute paths */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no absolute paths */ char *realpath (const char *__restrict, char *__restrict); #endif long int random (void); diff --git a/libc-top-half/musl/include/sys/socket.h b/libc-top-half/musl/include/sys/socket.h index 4d574c662..c08d8f649 100644 --- a/libc-top-half/musl/include/sys/socket.h +++ b/libc-top-half/musl/include/sys/socket.h @@ -395,14 +395,14 @@ struct sockaddr_storage { #include <__struct_sockaddr_storage.h> #endif -#ifdef __wasilibc_unmodified_upstream /* WASI has no socket/socketpair */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no socket/socketpair */ int socket (int, int, int); int socketpair (int, int, int, int [2]); #endif int shutdown (int, int); -#ifdef __wasilibc_unmodified_upstream /* WASI has no bind/connect/listen/accept */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no bind/connect/listen/accept */ int bind (int, const struct sockaddr *, socklen_t); int connect (int, const struct sockaddr *, socklen_t); int listen (int, int); @@ -411,24 +411,24 @@ int listen (int, int); int accept (int, struct sockaddr *__restrict, socklen_t *__restrict); int accept4(int, struct sockaddr *__restrict, socklen_t *__restrict, int); -#ifdef __wasilibc_unmodified_upstream /* WASI has no getsockname/getpeername */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no getsockname/getpeername */ int getsockname (int, struct sockaddr *__restrict, socklen_t *__restrict); int getpeername (int, struct sockaddr *__restrict, socklen_t *__restrict); #endif ssize_t send (int, const void *, size_t, int); ssize_t recv (int, void *, size_t, int); -#ifdef __wasilibc_unmodified_upstream /* WASI has no sendto/recvfrom */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no sendto/recvfrom */ ssize_t sendto (int, const void *, size_t, int, const struct sockaddr *, socklen_t); ssize_t recvfrom (int, void *__restrict, size_t, int, struct sockaddr *__restrict, socklen_t *__restrict); #endif -#ifdef __wasilibc_unmodified_upstream /* WASI has no sendmsg/recvmsg */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no sendmsg/recvmsg */ ssize_t sendmsg (int, const struct msghdr *, int); ssize_t recvmsg (int, struct msghdr *, int); #endif int getsockopt (int, int, int, void *__restrict, socklen_t *__restrict); -#ifdef __wasilibc_unmodified_upstream /* WASI has no setsockopt */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no setsockopt */ int setsockopt (int, int, int, const void *, socklen_t); #endif diff --git a/libc-top-half/musl/include/sys/stat.h b/libc-top-half/musl/include/sys/stat.h index 72e162615..c271f9bbf 100644 --- a/libc-top-half/musl/include/sys/stat.h +++ b/libc-top-half/musl/include/sys/stat.h @@ -78,12 +78,12 @@ int stat(const char *__restrict, struct stat *__restrict); int fstat(int, struct stat *); int lstat(const char *__restrict, struct stat *__restrict); int fstatat(int, const char *__restrict, struct stat *__restrict, int); -#ifdef __wasilibc_unmodified_upstream /* WASI has no chmod */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no chmod */ int chmod(const char *, mode_t); int fchmod(int, mode_t); int fchmodat(int, const char *, mode_t, int); #endif -#ifdef __wasilibc_unmodified_upstream /* WASI has no umask */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no umask */ mode_t umask(mode_t); #endif int mkdir(const char *, mode_t); diff --git a/libc-top-half/musl/include/sys/syscall.h b/libc-top-half/musl/include/sys/syscall.h index ceed6b59c..a25b5ec44 100644 --- a/libc-top-half/musl/include/sys/syscall.h +++ b/libc-top-half/musl/include/sys/syscall.h @@ -1,7 +1,7 @@ #ifndef _SYS_SYSCALL_H #define _SYS_SYSCALL_H -#ifdef __wasilibc_unmodified_upstream /* WASI has no syscall */ +#if defined(__wasilibc_unmodified_upstream) || defined(__faasm) #include #else /* The generic syscall funtion is not yet implemented. */ diff --git a/libc-top-half/musl/include/unistd.h b/libc-top-half/musl/include/unistd.h index 0be83e368..e5b81452d 100644 --- a/libc-top-half/musl/include/unistd.h +++ b/libc-top-half/musl/include/unistd.h @@ -45,13 +45,13 @@ extern "C" { #include -#ifdef __wasilibc_unmodified_upstream /* WASI has no pipe */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no pipe */ int pipe(int [2]); int pipe2(int [2], int); #endif int close(int); int posix_close(int, int); -#ifdef __wasilibc_unmodified_upstream /* WASI has no dup */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no dup */ int dup(int); int dup2(int, int); int dup3(int, int, int); @@ -122,7 +122,8 @@ int ftruncate(int, off_t); int access(const char *, int); int faccessat(int, const char *, int, int); -#ifdef __wasilibc_unmodified_upstream /* WASI has no fchdir */ +// Faasm: we emulate chdir in Faasm +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no cwd */ int fchdir(int); #endif int chdir(const char *); @@ -136,7 +137,7 @@ unsigned sleep(unsigned); int pause(void); #endif -#ifdef __wasilibc_unmodified_upstream /* WASI has no fork/exec */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no fork/exec */ pid_t fork(void); pid_t _Fork(void); int execve(const char *, char *const [], char *const []); @@ -159,7 +160,8 @@ __attribute__((__deprecated__( ))) pid_t getpid(void); #endif -#ifdef __wasilibc_unmodified_upstream /* WASI has no getpid etc. */ +// Faasm: we need getppid +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no getpid etc. */ pid_t getppid(void); pid_t getpgrp(void); pid_t getpgid(pid_t); @@ -167,7 +169,7 @@ int setpgid(pid_t, pid_t); pid_t setsid(void); pid_t getsid(pid_t); #endif -#ifdef __wasilibc_unmodified_upstream /* WASI has no ttyname */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no ttyname */ char *ttyname(int); int ttyname_r(int, char *, size_t); #endif @@ -177,11 +179,12 @@ pid_t tcgetpgrp(int); int tcsetpgrp(int, pid_t); #endif -#ifdef __wasilibc_unmodified_upstream /* WASI has no getuid etc. */ uid_t getuid(void); uid_t geteuid(void); gid_t getgid(void); gid_t getegid(void); + +#ifdef __wasilibc_unmodified_upstream /* WASI has no getuid etc. */ int getgroups(int, gid_t []); int setuid(uid_t); int seteuid(uid_t); @@ -236,10 +239,12 @@ unsigned ualarm(unsigned, unsigned); #define L_SET 0 #define L_INCR 1 #define L_XTND 2 -#ifdef __wasilibc_unmodified_upstream /* WASI has no brk */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no brk */ int brk(void *); #endif void *sbrk(intptr_t); + +int getpagesize(void); #ifdef __wasilibc_unmodified_upstream /* WASI has no processes */ pid_t vfork(void); int vhangup(void); @@ -258,17 +263,17 @@ void setusershell(void); void endusershell(void); char *getusershell(void); int acct(const char *); -long syscall(long, ...); int execvpe(const char *, char *const [], char *const []); int issetugid(void); #endif +long syscall(long, ...); int getentropy(void *, size_t); extern int optreset; #endif #ifdef _GNU_SOURCE extern char **environ; -#ifdef __wasilibc_unmodified_upstream /* WASI has no get/setresuid */ +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) /* WASI has no get/setresuid */ int setresuid(uid_t, uid_t, uid_t); int setresgid(gid_t, gid_t, gid_t); int getresuid(uid_t *, uid_t *, uid_t *); diff --git a/libc-top-half/musl/src/internal/syscall.h b/libc-top-half/musl/src/internal/syscall.h index 32e0e8ac3..fe0215404 100644 --- a/libc-top-half/musl/src/internal/syscall.h +++ b/libc-top-half/musl/src/internal/syscall.h @@ -1,4 +1,4 @@ -#ifdef __wasilibc_unmodified_upstream +#if defined(__faasm) || defined(__wasilibc_unmodified_upstream) #ifndef _INTERNAL_SYSCALL_H #define _INTERNAL_SYSCALL_H diff --git a/libc-top-half/musl/src/thread/pthread_join.c b/libc-top-half/musl/src/thread/pthread_join.c index b06e7e75e..b57476eee 100644 --- a/libc-top-half/musl/src/thread/pthread_join.c +++ b/libc-top-half/musl/src/thread/pthread_join.c @@ -41,6 +41,7 @@ static int __pthread_tryjoin_np(pthread_t t, void **res) return t->detach_state==DT_JOINABLE ? EBUSY : __pthread_join(t, res); } -weak_alias(__pthread_tryjoin_np, pthread_tryjoin_np); -weak_alias(__pthread_timedjoin_np, pthread_timedjoin_np); -weak_alias(__pthread_join, pthread_join); +// Faasm intercepts +//weak_alias(__pthread_tryjoin_np, pthread_tryjoin_np); +//weak_alias(__pthread_timedjoin_np, pthread_timedjoin_np); +//weak_alias(__pthread_join, pthread_join); From c4f536100cde28d7fa079d3183f095308e5d9673 Mon Sep 17 00:00:00 2001 From: Carlos Segarra Date: Thu, 23 Feb 2023 10:02:08 +0000 Subject: [PATCH 2/4] faasm: disable bulk-memory --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index 47a4adadd..cbbe6ff1c 100644 --- a/Makefile +++ b/Makefile @@ -516,6 +516,9 @@ $(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 += \ @@ -523,6 +526,7 @@ $(BULK_MEMORY_OBJS): CFLAGS += \ $(BULK_MEMORY_OBJS): CFLAGS += \ -DBULK_MEMORY_THRESHOLD=$(BULK_MEMORY_THRESHOLD) +endif $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS): CFLAGS += \ -D_WASI_EMULATED_SIGNAL From e35515bc5878a9262d25b0698a36d77abe54a6fd Mon Sep 17 00:00:00 2001 From: Carlos Segarra Date: Thu, 23 Feb 2023 10:31:10 +0000 Subject: [PATCH 3/4] faasm: switch on faasm-managed mmap and sbrk --- dlmalloc/src/dlmalloc.c | 14 ++++++++++++++ libc-bottom-half/sources/sbrk.c | 9 +++++++++ libc-top-half/musl/include/sys/mman.h | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/dlmalloc/src/dlmalloc.c b/dlmalloc/src/dlmalloc.c index 331536b44..d1b29cef8 100644 --- a/dlmalloc/src/dlmalloc.c +++ b/dlmalloc/src/dlmalloc.c @@ -6,11 +6,25 @@ // Define configuration macros for dlmalloc. +#ifdef __faasm +// Faasm supports mmap/ munmap +#define HAVE_MMAP 1 + +// Faasm supports normal wasm memory growth, but we need to be able to reclaim +// memory, so force everything through mmap/ munmap +#define HAVE_MORECORE 1 +#define MORECORE_CONTIGUOUS 1 + +// Faasm supports shrinking linear memory using offsets +#define MORECORE_CANNOT_TRIM 0 + +#else // WebAssembly doesn't have mmap-style memory allocation. #define HAVE_MMAP 0 // WebAssembly doesn't support shrinking linear memory. #define MORECORE_CANNOT_TRIM 1 +#endif // Disable sanity checks to reduce code size. #define ABORT __builtin_unreachable() diff --git a/libc-bottom-half/sources/sbrk.c b/libc-bottom-half/sources/sbrk.c index a26b75e9e..ce0c1bb80 100644 --- a/libc-bottom-half/sources/sbrk.c +++ b/libc-bottom-half/sources/sbrk.c @@ -3,8 +3,16 @@ #include #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. @@ -29,4 +37,5 @@ void *sbrk(intptr_t increment) { } return (void *)(old * PAGESIZE); +#endif } diff --git a/libc-top-half/musl/include/sys/mman.h b/libc-top-half/musl/include/sys/mman.h index 335ba2d71..f2f24bb98 100644 --- a/libc-top-half/musl/include/sys/mman.h +++ b/libc-top-half/musl/include/sys/mman.h @@ -1,4 +1,4 @@ -#ifndef _WASI_EMULATED_MMAN +#if !defined(__faasm) && !defined(_WASI_EMULATED_MMAN) #error "WASI lacks a true mmap; to enable minimal mmap emulation, \ compile with -D_WASI_EMULATED_MMAN and link with -lwasi-emulated-mman" #else From 16fde82fe008aa2995cec65685a217cf469e4837 Mon Sep 17 00:00:00 2001 From: Carlos Segarra Date: Mon, 3 Apr 2023 13:37:36 +0000 Subject: [PATCH 4/4] faasm: force all heap allocations to use __sbrk --- dlmalloc/src/dlmalloc.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/dlmalloc/src/dlmalloc.c b/dlmalloc/src/dlmalloc.c index d1b29cef8..cdf81d4c0 100644 --- a/dlmalloc/src/dlmalloc.c +++ b/dlmalloc/src/dlmalloc.c @@ -6,22 +6,23 @@ // Define configuration macros for dlmalloc. -#ifdef __faasm -// Faasm supports mmap/ munmap -#define HAVE_MMAP 1 +// WebAssembly doesn't have mmap-style memory allocation. +#define HAVE_MMAP 0 -// Faasm supports normal wasm memory growth, but we need to be able to reclaim -// memory, so force everything through mmap/ munmap +#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 -// Faasm supports shrinking linear memory using offsets +// 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 have mmap-style memory allocation. -#define HAVE_MMAP 0 - // WebAssembly doesn't support shrinking linear memory. #define MORECORE_CANNOT_TRIM 1 #endif