Skip to content

Commit

Permalink
Merge pull request WebAssembly#1 from waynr/cpython-compatibility-fixes
Browse files Browse the repository at this point in the history
CPython compatibility updates
  • Loading branch information
john-sharratt authored May 25, 2023
2 parents 577ac36 + 2362aca commit 35bc1bd
Show file tree
Hide file tree
Showing 41 changed files with 5,705 additions and 68 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.ccls*
sysroot
sysroot32
sysroot64
Expand Down
39 changes: 36 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
misc/nftw.c \
misc/syslog.c \
errno/strerror.c \
\
network/services.c \
network/getaddrinfo.c \
network/getnameinfo.c \
network/gethostbyname.c \
network/gethostbyname2.c \
network/gethostbyname2_r.c \
network/getservbyname.c \
network/getservbyname_r.c \
network/gethostbyaddr.c \
network/gethostbyaddr_r.c \
network/lookup_ipliteral.c \
network/lookup_name.c \
network/lookup_serv.c \
network/freeaddrinfo.c \
network/resolvconf.c \
\
network/gethostbyaddr.c \
network/gethostbyname.c \
network/getservbyname.c \
network/getservbyport.c \
network/h_errno.c \
network/hstrerror.c \
network/htonl.c \
network/htons.c \
network/ntohl.c \
Expand All @@ -108,6 +131,7 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
network/inet_aton.c \
network/in6addr_any.c \
network/in6addr_loopback.c \
network/proto.c \
fenv/fenv.c \
fenv/fesetround.c \
fenv/feupdateenv.c \
Expand Down Expand Up @@ -169,6 +193,7 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
linux/wait3.c \
linux/wait4.c \
linux/eventfd.c \
linux/setgroups.c \
stat/futimesat.c \
legacy/getpagesize.c \
thread/thrd_sleep.c \
Expand Down Expand Up @@ -445,7 +470,7 @@ MUSL_OMIT_HEADERS += \
"sys/acct.h" \
"sys/cachectl.h" \
"sys/epoll.h" "sys/reboot.h" "sys/swap.h" \
"sys/sendfile.h" "sys/inotify.h" \
"sys/inotify.h" \
"sys/quota.h" \
"sys/klog.h" \
"sys/fsuid.h" \
Expand All @@ -468,7 +493,6 @@ MUSL_OMIT_HEADERS += \
"sys/membarrier.h" \
"sys/signalfd.h" \
"sys/termios.h" \
"net/if.h" \
"net/if_arp.h" \
"net/ethernet.h" \
"net/route.h" \
Expand All @@ -483,7 +507,16 @@ ifeq ($(THREAD_MODEL), single)
MUSL_OMIT_HEADERS += "pthread.h"
endif

default: finish
default: finish post-finish

wasix-headers:
git submodule init
git submodule update
cargo run --manifest-path tools/wasix-headers/Cargo.toml generate-libc

post-finish: finish
rm -f sysroot/lib/wasm32-wasi/libc-printscan-log-double.a
rsync -rtvu --delete ./sysroot/ ./sysroot32/

$(SYSROOT_LIB)/libc.a: $(LIBC_OBJS)

Expand Down
11 changes: 6 additions & 5 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
CC=clang
CC ?= "clang"
SYSROOT ?= "../sysroot32"
LLD_PATH=/prog/rust/build/x86_64-unknown-linux-gnu/lld/bin

CFLAGS = --target=wasm32-wasmer-wasi \
-O2 \
--sysroot ../../wasix-libc/sysroot32 \
--sysroot ${SYSROOT} \
-matomics \
-mbulk-memory \
-mmutable-globals \
Expand Down Expand Up @@ -54,7 +55,7 @@ SRC=$(wildcard *.c)

export PATH := $(LLD_PATH):$(PATH)

all: longjmp file-copy signal fork vfork fork-longjmp execve spawn pipe epoll
all: longjmp file-copy signal fork vfork fork-longjmp execve spawn pipe epoll getrandom readdir_tree
cp -f ../dist/wasix/longjmp.wasm ../../wasmer/tests/integration/cli/tests/wasm/example-longjmp.wasm
cp -f ../dist/wasix/fork.wasm ../../wasmer/tests/integration/cli/tests/wasm/example-fork.wasm
cp -f ../dist/wasix/fork.wasm ../../packages/fork-test/test.wasm
Expand All @@ -69,9 +70,9 @@ all: longjmp file-copy signal fork vfork fork-longjmp execve spawn pipe epoll

%: %.c
mkdir -p ../dist/host
clang $@.c -o ../dist/host/$@
${CC} $(CFLAGS) $@.c -o ../dist/host/$@
mkdir -p ../dist/wasix
clang $(CFLAGS) $(CLFLAGS) \
${CC} $(CFLAGS) $(CLFLAGS) \
$@.c \
-o ../dist/wasix/$@.rustc.wasm
wasm-opt -O2 --asyncify ../dist/wasix/$@.rustc.wasm -o ../dist/wasix/$@.wasm
Expand Down
32 changes: 32 additions & 0 deletions examples/getrandom.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <sys/random.h>
#include <unistd.h>

int print_random_int_array() {
ssize_t retval;
int buf[100];
retval = getrandom(buf, sizeof(int) * 100, 0);
if (retval == -1) {
return retval;
}
for (int i = 0; i < 100; ++i) {
printf("%d ", buf[i]);
}
printf("\n");
return 0;
}

int main() {
int i = 0, retval = 0;
while (i < 5) {
printf("trial %d:\n ", i);
retval = print_random_int_array();
printf("\n");
if (retval == -1) {
printf("failed!\n");
return retval;
}
i++;
}
return 0;
}
64 changes: 64 additions & 0 deletions examples/readdir_tree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <stdarg.h>
#include <stdio.h>
#include <sys/random.h>
#include <unistd.h>
#include <string.h>

#include <dirent.h>

void print(int lvl, const char *format, ...) {
for (int i = 0; i < lvl; i++) {
printf(" ");
}
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
fflush(stdout);
}

int handle_dir(int lvl, DIR *d) {
DIR *new;
int ret = 0;
for (;;) {
struct dirent *ent = readdir(d);
if (ent == NULL) {
return 0;
}

if (strcmp(ent->d_name, "..") == 0 || strcmp(ent->d_name, ".") == 0) {
continue;
}

switch (ent->d_type) {
case DT_DIR:
print(lvl, "%s:\n", ent->d_name);
new = opendir(ent->d_name);
ret = handle_dir(lvl + 1, new);
if (ret < 0) {
return ret;
}
continue;
default:
print(lvl, "%s\n", ent->d_name);
}
}

return ret;
}

int main(int argc, char *argv[]) {
if (argc < 1) {
fprintf(stderr, "not enough args!");
}
for (int i = 1; i < argc; i++) {
print(0, "%s\n", argv[i]);

DIR *d = opendir(argv[i]);
int ret = handle_dir(1, d);
if (ret < 0) {
return ret;
}
}
return 0;
}
29 changes: 29 additions & 0 deletions expected/wasm32-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ __fwritex
__fwriting
__get_handler_set
__get_locale
__get_resolv_conf
__getdelim
__getentropy
__getgr_a
Expand All @@ -113,6 +114,7 @@ __getpwent_a
__gettext_lockptr
__gettextdomain
__gmtime_r
__h_errno_location
__hwcap
__inet_aton
__inhibit_ptc
Expand Down Expand Up @@ -184,6 +186,10 @@ __log2_data
__log2f_data
__log_data
__logf_data
__lookup_ipliteral
__lookup_name
__lookup_serv
__lookup_serv_wasi
__lseek
__main_void
__malloc_atfork
Expand Down Expand Up @@ -560,6 +566,7 @@ acosh
acoshf
acoshl
acosl
add_entry
alarm
aligned_alloc
alphasort
Expand Down Expand Up @@ -742,6 +749,7 @@ duplocale
ecvt
encrypt
endgrent
endprotoent
endpwent
endspent
environ
Expand Down Expand Up @@ -869,6 +877,7 @@ fputws_unlocked
fread
fread_unlocked
free
freeaddrinfo
freeif_addrs
freelocale
freopen
Expand Down Expand Up @@ -901,9 +910,11 @@ fwrite_unlocked
fwscanf
gcvt
get_avphys_pages
get_entry
get_nprocs
get_nprocs_conf
get_phys_pages
getaddrinfo
getc
getc_unlocked
getchar
Expand All @@ -925,11 +936,17 @@ getgrgid_r
getgrnam
getgrnam_r
getgrouplist
gethostbyaddr
gethostbyaddr_r
gethostbyname
gethostbyname2
gethostbyname2_r
gethostid
gethostname
getif_addrs
getitimer
getline
getnameinfo
getopt
getopt_long
getopt_long_only
Expand All @@ -939,6 +956,9 @@ getpgid
getpgrp
getpid
getppid
getprotobyname
getprotobynumber
getprotoent
getpwent
getpwnam
getpwnam_r
Expand All @@ -948,6 +968,9 @@ getrandom
getrlimit
getrlimit64
getrusage
getservbyname
getservbyname_r
getservbyport
getsid
getsockname
getsockopt
Expand All @@ -969,12 +992,14 @@ globfree
globfree64
gmtime
gmtime_r
h_errno
hcreate
hcreate_r
hdestroy
hdestroy_r
hsearch
hsearch_r
hstrerror
htonl
htons
hypot
Expand All @@ -994,6 +1019,7 @@ index
inet_aton
inet_ntop
inet_pton
init_services
initstate
insque
ioctl
Expand Down Expand Up @@ -1428,13 +1454,15 @@ send
sendfile
sendmsg
sendto
services
setbuf
setbuffer
setegid
setenv
seteuid
setgid
setgrent
setgroups
setitimer
setjmp
setkey
Expand All @@ -1443,6 +1471,7 @@ setlocale
setlogmask
setpgid
setpgrp
setprotoent
setpwent
setrlimit
setrlimit64
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm32-wasi/include-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
#include <memory.h>
#include <monetary.h>
#include <mqueue.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/icmp6.h>
#include <netinet/igmp.h>
Expand Down Expand Up @@ -161,6 +162,7 @@
#include <sys/random.h>
#include <sys/reg.h>
#include <sys/select.h>
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/stropts.h>
Expand Down
Loading

0 comments on commit 35bc1bd

Please sign in to comment.