Skip to content

Commit

Permalink
implement getsockname, getpeername, and getaddrinfo (#488)
Browse files Browse the repository at this point in the history
This also includes stubs for `gethostbyname`, `gethostbyaddr`, etc. which were
necessary to get CPython to build.  I believe it will be possible to implement
them all properly at some point, but don't have the bandwidth at the moment.

Finally, this includes a few fixes for issues I missed in earlier PRs that
surfaced when running the CPython `asyncio` test suite.

Signed-off-by: Joel Dice <[email protected]>
Co-authored-by: Dave Bakker <[email protected]>
  • Loading branch information
dicej and badeend committed Apr 2, 2024
1 parent 1ab654e commit d038294
Show file tree
Hide file tree
Showing 8 changed files with 555 additions and 5 deletions.
20 changes: 18 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ LIBC_BOTTOM_HALF_OMIT_SOURCES := \
$(LIBC_BOTTOM_HALF_SOURCES)/accept-wasip2.c \
$(LIBC_BOTTOM_HALF_SOURCES)/shutdown.c \
$(LIBC_BOTTOM_HALF_SOURCES)/sockopt.c \
$(LIBC_BOTTOM_HALF_SOURCES)/poll-wasip2.c
$(LIBC_BOTTOM_HALF_SOURCES)/poll-wasip2.c \
$(LIBC_BOTTOM_HALF_SOURCES)/getsockpeername.c \
$(LIBC_BOTTOM_HALF_SOURCES)/netdb.c
LIBC_BOTTOM_HALF_ALL_SOURCES := $(filter-out $(LIBC_BOTTOM_HALF_OMIT_SOURCES),$(LIBC_BOTTOM_HALF_ALL_SOURCES))
# Omit p2-specific headers from include-all.c test.
# for exception-handling.
Expand Down Expand Up @@ -249,6 +251,13 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/complex/*.c)) \
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/crypt/*.c)

ifeq ($(WASI_SNAPSHOT), p2)
LIBC_TOP_HALF_MUSL_SOURCES += \
$(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \
network/gai_strerror.c \
)
endif

ifeq ($(THREAD_MODEL), posix)
LIBC_TOP_HALF_MUSL_SOURCES += \
$(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \
Expand Down Expand Up @@ -387,6 +396,10 @@ ASMFLAGS += -matomics
CFLAGS += -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
endif

ifeq ($(WASI_SNAPSHOT), p2)
EXTRA_CFLAGS += -D__wasilibc_use_wasip2
endif

# Expose the public headers to the implementation. We use `-isystem` for
# purpose for two reasons:
#
Expand Down Expand Up @@ -500,7 +513,6 @@ MUSL_OMIT_HEADERS += \
"sys/auxv.h" \
"pwd.h" "shadow.h" "grp.h" \
"mntent.h" \
"netdb.h" \
"resolv.h" \
"pty.h" \
"ulimit.h" \
Expand All @@ -523,6 +535,10 @@ MUSL_OMIT_HEADERS += \
"sys/sysmacros.h" \
"aio.h"

ifeq ($(WASI_SNAPSHOT), p1)
MUSL_OMIT_HEADERS += "netdb.h"
endif

ifeq ($(THREAD_MODEL), single)
# Remove headers not supported in single-threaded mode.
MUSL_OMIT_HEADERS += "pthread.h"
Expand Down
20 changes: 20 additions & 0 deletions expected/wasm32-wasip2/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,9 @@ dprintf
drand48
drem
dremf
drop_tcp_socket
drop_udp_socket
drop_udp_socket_streams
duplocale
ecvt
encrypt
Expand Down Expand Up @@ -709,6 +712,7 @@ fputws_unlocked
fread
fread_unlocked
free
freeaddrinfo
freelocale
freopen
freopen64
Expand Down Expand Up @@ -737,11 +741,13 @@ fwprintf
fwrite
fwrite_unlocked
fwscanf
gai_strerror
gcvt
get_avphys_pages
get_nprocs
get_nprocs_conf
get_phys_pages
getaddrinfo
getc
getc_unlocked
getchar
Expand All @@ -753,14 +759,22 @@ getdelim
getdomainname
getentropy
getenv
gethostbyaddr
gethostbyname
gethostid
getline
getnameinfo
getopt
getopt_long
getopt_long_only
getpagesize
getpeername
getpid
getprotobyname
getrusage
getservbyname
getservbyport
getsockname
getsockopt
getsubopt
gettimeofday
Expand All @@ -775,12 +789,14 @@ globfree
globfree64
gmtime
gmtime_r
h_errno
hcreate
hcreate_r
hdestroy
hdestroy_r
hsearch
hsearch_r
hstrerror
htonl
htons
hypot
Expand Down Expand Up @@ -1246,6 +1262,8 @@ tcp_bind
tcp_borrow_tcp_socket
tcp_create_socket_create_tcp_socket
tcp_create_socket_result_own_tcp_socket_error_code_free
tcp_getpeername
tcp_getsockname
tcp_getsockopt
tcp_ip_socket_address_free
tcp_listen
Expand Down Expand Up @@ -1338,6 +1356,8 @@ udp_borrow_outgoing_datagram_stream
udp_borrow_udp_socket
udp_create_socket_create_udp_socket
udp_create_socket_result_own_udp_socket_error_code_free
udp_getpeername
udp_getsockname
udp_getsockopt
udp_incoming_datagram_free
udp_incoming_datagram_stream_drop_borrow
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasip2/include-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
#include <memory.h>
#include <monetary.h>
#include <mqueue.h>
#include <netdb.h>
#include <netinet/icmp6.h>
#include <netinet/igmp.h>
#include <netinet/in.h>
Expand Down
43 changes: 42 additions & 1 deletion expected/wasm32-wasip2/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
#define AF_INET6 PF_INET6
#define AF_UNIX 3
#define AF_UNSPEC PF_UNSPEC
#define AI_ADDRCONFIG 0x20
#define AI_ALL 0x10
#define AI_CANONNAME 0x02
#define AI_NUMERICHOST 0x04
#define AI_NUMERICSERV 0x400
#define AI_PASSIVE 0x01
#define AI_V4MAPPED 0x08
#define ALT_DIGITS 0x2002F
#define AM_STR 0x20026
#define ANYMARK 0x01
Expand Down Expand Up @@ -192,6 +199,24 @@
#define EADDRNOTAVAIL __WASI_ERRNO_ADDRNOTAVAIL
#define EAFNOSUPPORT __WASI_ERRNO_AFNOSUPPORT
#define EAGAIN __WASI_ERRNO_AGAIN
#define EAI_ADDRFAMILY -9
#define EAI_AGAIN -3
#define EAI_ALLDONE -103
#define EAI_BADFLAGS -1
#define EAI_CANCELED -101
#define EAI_FAIL -4
#define EAI_FAMILY -6
#define EAI_IDN_ENCODE -105
#define EAI_INPROGRESS -100
#define EAI_INTR -104
#define EAI_MEMORY -10
#define EAI_NODATA -5
#define EAI_NONAME -2
#define EAI_NOTCANCELED -102
#define EAI_OVERFLOW -12
#define EAI_SERVICE -8
#define EAI_SOCKTYPE -7
#define EAI_SYSTEM -11
#define EALREADY __WASI_ERRNO_ALREADY
#define EBADF __WASI_ERRNO_BADF
#define EBADID 5
Expand Down Expand Up @@ -489,6 +514,7 @@
#define HIBITL MINLONG
#define HIBITS MINSHORT
#define HOST_NAME_MAX 255
#define HOST_NOT_FOUND 1
#define HUGE 3.40282346638528859812e+38F
#define HUGE_VAL ((double)INFINITY)
#define HUGE_VALF INFINITY
Expand Down Expand Up @@ -1239,6 +1265,14 @@
#define NEW_ENV_VAR 0
#define NGROUPS 32
#define NGROUPS_MAX 32
#define NI_DGRAM 0x10
#define NI_MAXHOST 255
#define NI_MAXSERV 32
#define NI_NAMEREQD 0x08
#define NI_NOFQDN 0x04
#define NI_NUMERICHOST 0x01
#define NI_NUMERICSCOPE 0x100
#define NI_NUMERICSERV 0x02
#define NL_ARGMAX 9
#define NL_CAT_LOCALE 1
#define NL_LANGMAX 32
Expand All @@ -1257,6 +1291,9 @@
#define NOTAUTH ns_r_notauth
#define NOTIMP ns_r_notimpl
#define NOTZONE ns_r_notzone
#define NO_ADDRESS NO_DATA
#define NO_DATA 4
#define NO_RECOVERY 3
#define NR_ICMP_TYPES 18
#define NR_ICMP_UNREACH 15
#define NSLC 18
Expand Down Expand Up @@ -1968,6 +2005,7 @@
#define TOREAD 00004
#define TOWRITE 00002
#define TRANSIENT 4
#define TRY_AGAIN 2
#define TSGID 02000
#define TSS_DTOR_ITERATIONS 4
#define TSUID 04000
Expand Down Expand Up @@ -2173,6 +2211,7 @@
#define _MATH_H
#define _MONETARY_H
#define _MQUEUE_H
#define _NETDB_H
#define _NETINET_ICMP6_H
#define _NETINET_IGMP_H
#define _NETINET_IN_H
Expand Down Expand Up @@ -3194,7 +3233,7 @@
#define __wasilibc___typedef_suseconds_t_h
#define __wasilibc___typedef_time_t_h
#define __wasilibc___typedef_uid_t_h
#define __wasilibc_use_wasip2
#define __wasilibc_use_wasip2 1
#define __wasm 1
#define __wasm32 1
#define __wasm32__ 1
Expand Down Expand Up @@ -3281,6 +3320,8 @@
#define glob64 glob
#define glob64_t glob_t
#define globfree64 globfree
#define h_addr h_addr_list[0]
#define h_errno h_errno
#define howmany(n,d) (((n)+((d)-1))/(d))
#define htobe16(x) __bswap16(x)
#define htobe32(x) __bswap32(x)
Expand Down
Loading

0 comments on commit d038294

Please sign in to comment.