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

Draft of WASI Preview 2 support, featuring wasi-sockets #449

Closed
wants to merge 68 commits into from
Closed
Show file tree
Hide file tree
Changes from 53 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
6da9ef8
first draft of socket support
dicej Nov 22, 2023
95454e8
support non-blocking sockets
dicej Nov 29, 2023
e961d20
close Preview 2 handles in close(2)
dicej Nov 30, 2023
9d98fe3
add getaddrinfo and setsockopt, etc.
dicej Dec 1, 2023
1937e94
Implement TCP based on preview2 wasi-sockets
badeend Dec 3, 2023
c7eda81
Ignore .vscode
badeend Dec 3, 2023
a14b30d
Merge branch 'sockets' of https://github.com/dicej/wasi-libc into tcp…
badeend Dec 3, 2023
f3a1106
Fix errors after merge
badeend Dec 3, 2023
ae41e77
Perform implicit bind in Listen
badeend Dec 3, 2023
a00da21
Implement MSG_DONTWAIT flag
badeend Dec 3, 2023
b120ad4
"Implement" MSG_NOSIGNAL
badeend Dec 3, 2023
ee04f9e
No need for these constant to match their preview1 counterparts anymo…
badeend Dec 3, 2023
ea2fcf0
Make my own TODOs easier to find
badeend Dec 4, 2023
6cf2c3b
Decode IP addresses
badeend Dec 4, 2023
76160c3
Replace assert with abort
badeend Dec 4, 2023
6bb179a
Update libc-bottom-half/cloudlibc/src/libc/sys/socket/recv.c
badeend Dec 4, 2023
5af19d5
Merge pull request #1 from badeend/tcp-sockets
dicej Dec 5, 2023
74b703e
add `TCP_NODELAY` support
dicej Dec 5, 2023
0762651
Update to preview2 version rc-2023-11-10. And use the new --rename op…
badeend Dec 5, 2023
7112e67
Merge branch 'sockets' of https://github.com/dicej/wasi-libc into upd…
badeend Dec 5, 2023
879d533
TCP_NODELAY was removed in rc-2023-11-10
badeend Dec 5, 2023
9ab49f3
Update to rc-2023-12-05
badeend Dec 6, 2023
eb3602d
Implement remaining socket options introduced in rc-2023-11-10
badeend Dec 6, 2023
91b0a92
Represent tagged unions as a single type instead of three.
badeend Dec 6, 2023
1e97c2e
Open & close UDP sockets
badeend Dec 6, 2023
e0f4314
UDP socket options
badeend Dec 6, 2023
c11533a
Keep track of the address family ourselves, because we'll need it qui…
badeend Dec 6, 2023
4fc628c
Refactor sockaddr validation:
badeend Dec 6, 2023
ebb3e3f
add basic `TCP_NODELAY` support to `setsockopt`
dicej Dec 7, 2023
253f895
add netdb.h-related stubs
dicej Dec 7, 2023
e09d83b
Implement recv&send using recvfrom&sendto
badeend Dec 7, 2023
8f7dd8c
UDP bind
badeend Dec 7, 2023
f780765
Split up Bound state
badeend Dec 7, 2023
743718d
Reimplement TCP_NODELAY. (#3)
badeend Dec 7, 2023
f60a655
UDP connect
badeend Dec 7, 2023
954bae8
Fix trap/abort when calling getsockname or getpeername too soon
badeend Dec 7, 2023
fac12b2
UDP getsockname & getpeername
badeend Dec 7, 2023
9eb88b1
add `wasi-sockets` support for `poll` and `ioctl`
dicej Dec 7, 2023
2680196
Refactor `stream` state transition
badeend Dec 8, 2023
9e9171f
UDP send and recv
badeend Dec 8, 2023
eb50594
Merge branch 'sockets' of https://github.com/dicej/wasi-libc into udp
badeend Dec 8, 2023
02285af
Fix build errors after pull
badeend Dec 8, 2023
b984f46
Fix incompatible signature of poll
badeend Dec 8, 2023
2a24635
free ready list after polling
dicej Dec 9, 2023
a81b074
Merge remote-tracking branch 'badeend/udp' into sockets
dicej Dec 11, 2023
955e4d7
use a single assignment when updating TCP state
dicej Dec 11, 2023
6be1e1a
build for Preview 1 and 2 separately
dicej Dec 12, 2023
24c7bb7
use separate include dirs for each target
dicej Dec 12, 2023
e8925b3
add (slightly modified) socket.c to test list
dicej Dec 12, 2023
5112461
re-enable `check-symbols`
dicej Dec 12, 2023
fc38c7a
add instructions to `make-bindings.sh`
dicej Dec 12, 2023
14757da
revert spurious whitespace changes
dicej Dec 12, 2023
f0c29d4
fix out-of-date comment in descriptor_table.c
dicej Dec 12, 2023
bc1b85a
use AF_INET6 when creating a sockaddr_in6
dicej Dec 15, 2023
2a61396
implement `getaddrinfo`
dicej Dec 15, 2023
b46fcaf
fix backwards logic in `ioctl`
dicej Dec 16, 2023
ef83aa6
allow TCP_SOCKET_STATE_CONNECT_FAILED sockets to be polled
dicej Dec 16, 2023
0b50784
support TCP_SOCKET_STATE_LISTENING sockets in poll
dicej Dec 19, 2023
1db6e1a
return `getaddrinfo` results in order
dicej Dec 19, 2023
5b1ae5e
honor ai_family hint in getaddrinfo
dicej Dec 20, 2023
9c4d23a
add UDP support to ioctl
dicej Dec 20, 2023
7c28169
add fake SO_REUSEADDR support
dicej Dec 20, 2023
69842d6
add UDP support to poll
dicej Dec 20, 2023
c7eeb21
re-implement pselect using poll
dicej Dec 20, 2023
32f3a06
convert nanoseconds to milliseconds in pselect
dicej Dec 20, 2023
f3adffe
return zero from recv on STREAMS_STREAM_ERROR_CLOSED
dicej Dec 20, 2023
a3fb845
update bindings to WASI 0.2.0
dicej Jan 26, 2024
60e3428
update tests to use Wasmtime 17
dicej Jan 26, 2024
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
6 changes: 5 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ jobs:

- name: Build libc
shell: bash
run: make -j4
run: |
make -j4
WASI_SNAPSHOT=preview2 make -j4

- name: Test
shell: bash
Expand All @@ -98,6 +100,8 @@ jobs:
mkdir -p $WASI_DIR
cp download/lib/wasi/libclang_rt.builtins-wasm32.a $WASI_DIR
make test
rm -r build
WASI_SNAPSHOT=preview2 make test
# The older version of Clang does not provide the expected symbol for the
# test entrypoints: `undefined symbol: __main_argc_argv`.
# The older (<15.0.7) version of wasm-ld does not provide `__heap_end`,
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
sysroot
build
.vscode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put editor-specific paths in your own global git ignore file, rather than in repository .gitignore files.

55 changes: 52 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ SYSROOT ?= $(CURDIR)/sysroot
INSTALL_DIR ?= /usr/local
# single or posix; note that pthread support is still a work-in-progress.
THREAD_MODEL ?= single
# preview1 or preview2; the latter is not (yet) compatible with multithreading
WASI_SNAPSHOT ?= preview1
# dlmalloc or none
MALLOC_IMPL ?= dlmalloc
# yes or no
Expand All @@ -41,6 +43,10 @@ ifeq ($(THREAD_MODEL), posix)
TARGET_TRIPLE = wasm32-wasi-threads
endif

ifeq ($(WASI_SNAPSHOT), preview2)
TARGET_TRIPLE = wasm32-wasi-preview2
endif

BUILTINS_LIB ?= $(shell ${CC} --print-libgcc-file-name)

# These variables describe the locations of various files and directories in
Expand All @@ -62,6 +68,23 @@ LIBC_BOTTOM_HALF_ALL_SOURCES = \
$(shell find $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC) -name \*.c) \
$(shell find $(LIBC_BOTTOM_HALF_SOURCES) -name \*.c))

ifeq ($(WASI_SNAPSHOT), preview1)
# WASI Preview 1 has minimal socket support, so the following files are not used:
LIBC_BOTTOM_HALF_OMIT_SOURCES := \
$(addprefix $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)/libc/sys/, \
socket/__utils.c \
socket/bind.c \
socket/connect.c \
socket/getsockpeername.c \
socket/listen.c \
socket/netdb.c \
socket/socket.c \
wasi_preview2/descriptor_table.c \
wasi_preview2/preview2.c \
)
LIBC_BOTTOM_HALF_ALL_SOURCES := $(filter-out $(LIBC_BOTTOM_HALF_OMIT_SOURCES),$(LIBC_BOTTOM_HALF_ALL_SOURCES))
endif

# FIXME(https://reviews.llvm.org/D85567) - due to a bug in LLD the weak
# references to a function defined in `chdir.c` only work if `chdir.c` is at the
# end of the archive, but once that LLD review lands and propagates into LLVM
Expand Down Expand Up @@ -197,6 +220,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), preview2)
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 @@ -334,6 +364,10 @@ ASMFLAGS += -matomics
CFLAGS += -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
endif

ifeq ($(WASI_SNAPSHOT), preview2)
EXTRA_CFLAGS += -D__wasilibc_use_preview2
endif

# Expose the public headers to the implementation. We use `-isystem` for
# purpose for two reasons:
#
Expand All @@ -356,6 +390,9 @@ DLMALLOC_OBJS = $(call objs,$(DLMALLOC_SOURCES))
EMMALLOC_OBJS = $(call objs,$(EMMALLOC_SOURCES))
LIBC_BOTTOM_HALF_ALL_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_ALL_SOURCES))
LIBC_TOP_HALF_ALL_OBJS = $(call asmobjs,$(call objs,$(LIBC_TOP_HALF_ALL_SOURCES)))
ifeq ($(WASI_SNAPSHOT), preview2)
LIBC_OBJS += $(OBJDIR)/preview2_component_type.o
endif
ifeq ($(MALLOC_IMPL),dlmalloc)
LIBC_OBJS += $(DLMALLOC_OBJS)
else ifeq ($(MALLOC_IMPL),emmalloc)
Expand Down Expand Up @@ -386,7 +423,7 @@ LIBC_BOTTOM_HALF_CRT_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_CRT_SOURCES))
# These variables describe the locations of various files and
# directories in the generated sysroot tree.
SYSROOT_LIB := $(SYSROOT)/lib/$(TARGET_TRIPLE)
SYSROOT_INC = $(SYSROOT)/include
SYSROOT_INC = $(SYSROOT)/include/$(TARGET_TRIPLE)
SYSROOT_SHARE = $(SYSROOT)/share/$(TARGET_TRIPLE)

# Files from musl's include directory that we don't want to install in the
Expand Down Expand Up @@ -444,7 +481,6 @@ MUSL_OMIT_HEADERS += \
"sys/auxv.h" \
"pwd.h" "shadow.h" "grp.h" \
"mntent.h" \
"netdb.h" \
"resolv.h" \
"pty.h" \
"setjmp.h" \
Expand All @@ -468,6 +504,11 @@ MUSL_OMIT_HEADERS += \
"sys/sysmacros.h" \
"aio.h"

ifeq ($(WASI_SNAPSHOT), preview1)
# Remove headers not supported in WASI Preview 1.
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 Expand Up @@ -510,7 +551,7 @@ PIC_OBJS = \
# to CC. This is a workaround for a Windows command line size limitation. See
# the `%.a` rule below for details.
$(SYSROOT_LIB)/%.so: $(OBJDIR)/%.so.a $(BUILTINS_LIB)
$(CC) -nodefaultlibs -shared --sysroot=$(SYSROOT) \
$(CC) --target=$(TARGET_TRIPLE) -nodefaultlibs -shared --sysroot=$(SYSROOT) \
-o $@ -Wl,--whole-archive $< -Wl,--no-whole-archive $(BUILTINS_LIB)

$(OBJDIR)/libc.so.a: $(LIBC_SO_OBJS) $(MUSL_PRINTSCAN_LONG_DOUBLE_SO_OBJS)
Expand Down Expand Up @@ -577,6 +618,10 @@ $(OBJDIR)/%.long-double.pic.o: %.c include_dirs
@mkdir -p "$(@D)"
$(CC) $(CFLAGS) -MD -MP -o $@ -c $<

$(OBJDIR)/preview2_component_type.pic.o $(OBJDIR)/preview2_component_type.o: $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)/libc/sys/wasi_preview2/preview2_component_type.o
@mkdir -p "$(@D)"
cp $< $@

$(OBJDIR)/%.pic.o: %.c include_dirs
@mkdir -p "$(@D)"
$(CC) $(CFLAGS) -MD -MP -o $@ -c $<
Expand Down Expand Up @@ -653,6 +698,10 @@ include_dirs:

# Remove selected header files.
$(RM) $(patsubst %,$(SYSROOT_INC)/%,$(MUSL_OMIT_HEADERS))
ifeq ($(WASI_SNAPSHOT), preview2)
printf '#ifndef __wasilibc_use_preview2\n#define __wasilibc_use_preview2\n#endif\n' \
> "$(SYSROOT_INC)/__wasi_snapshot.h"
endif

startup_files: include_dirs $(LIBC_BOTTOM_HALF_CRT_OBJS)
#
Expand Down
Loading