Skip to content

Commit 47b9db6

Browse files
authored
add WASI Preview 2 bindings (#460)
* add WASI Preview 2 bindings This adds C bindings generated from the `wasi:cli/imports@0.2.0-rc-2023-12-05` world, plus a makefile target to regenerate them from the WIT source files. We'll use these bindings to call Preview 2 host functions when building for the `wasm32-wasi-preview2` target. Signed-off-by: Joel Dice <joel.dice@fermyon.com> * update to pre-release of `wit-bindgen` 0.17.0 This includes bytecodealliance/wit-bindgen#804 (fix broken indentation in generated code) and bytecodealliance/wit-bindgen#805 (support overriding world name and adding a suffix to the component type custom section). Signed-off-by: Joel Dice <joel.dice@fermyon.com> * test all targets; update preview2 expected output files Signed-off-by: Joel Dice <joel.dice@fermyon.com> * build for `wasm32-wasi-threads` before testing it Signed-off-by: Joel Dice <joel.dice@fermyon.com> * move generated bindings per review feedback Since these files aren't part of cloudlibc, no reason to put them under the cloudlibc directory. Signed-off-by: Joel Dice <joel.dice@fermyon.com> * move preview2.h to wasi directory Signed-off-by: Joel Dice <joel.dice@fermyon.com> --------- Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent 03b228e commit 47b9db6

10 files changed

Lines changed: 7502 additions & 25 deletions

File tree

.github/workflows/main.yml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ jobs:
8888
make -j4
8989
WASI_SNAPSHOT=preview2 make -j4
9090
91+
- name: Build libc + threads
92+
# Only build the thread-capable wasi-libc in the latest supported Clang
93+
# version; the earliest version does not have all necessary builtins
94+
# (e.g., `__builtin_wasm_memory_atomic_notify`).
95+
if: matrix.clang_version != '10.0.0'
96+
shell: bash
97+
run: make -j4 THREAD_MODEL=posix
98+
9199
- name: Test
92100
shell: bash
93101
# For Clang linking to work correctly, we need to place Clang's runtime
@@ -99,23 +107,17 @@ jobs:
99107
export WASI_DIR=$(realpath $($CLANG_DIR/clang -print-resource-dir)/lib/wasi/)
100108
mkdir -p $WASI_DIR
101109
cp download/lib/wasi/libclang_rt.builtins-wasm32.a $WASI_DIR
102-
make test
110+
TARGET_TRIPLE=wasm32-wasi make test
111+
rm -r build
112+
TARGET_TRIPLE=wasm32-wasi-preview2 make test
103113
rm -r build
104-
WASI_SNAPSHOT=preview2 make test
114+
TARGET_TRIPLE=wasm32-wasi-threads make test
105115
# The older version of Clang does not provide the expected symbol for the
106116
# test entrypoints: `undefined symbol: __main_argc_argv`.
107117
# The older (<15.0.7) version of wasm-ld does not provide `__heap_end`,
108118
# which is required by our malloc implementation.
109119
if: matrix.os == 'ubuntu-latest' && matrix.clang_version != '10.0.0'
110120

111-
- name: Build libc + threads
112-
# Only build the thread-capable wasi-libc in the latest supported Clang
113-
# version; the earliest version does not have all necessary builtins
114-
# (e.g., `__builtin_wasm_memory_atomic_notify`).
115-
if: matrix.clang_version != '10.0.0'
116-
shell: bash
117-
run: make -j4 THREAD_MODEL=posix
118-
119121
- uses: actions/upload-artifact@v1
120122
with:
121123
# Upload the sysroot folder. Give it a name according to the OS it was built for.

Makefile

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ MALLOC_IMPL ?= dlmalloc
2323
BUILD_LIBC_TOP_HALF ?= yes
2424
# The directory where we will store intermediate artifacts.
2525
OBJDIR ?= build/$(TARGET_TRIPLE)
26+
# The directory where we store files and tools for generating WASI Preview 2 bindings
27+
BINDING_WORK_DIR ?= build/bindings
28+
# URL from which to retrieve the WIT files used to generate the WASI Preview 2 bindings
29+
WASI_CLI_URL ?= https://github.com/WebAssembly/wasi-cli/archive/refs/tags/v0.2.0-rc-2023-12-05.tar.gz
30+
# URL from which to retrieve the `wit-bindgen` command used to generate the WASI
31+
# Preview 2 bindings.
32+
#
33+
# TODO: Switch to bytecodealliance/wit-bindgen 0.17.0 once it's released (which
34+
# will include https://github.com/bytecodealliance/wit-bindgen/pull/804 and
35+
# https://github.com/bytecodealliance/wit-bindgen/pull/805, which we rely on)
36+
WIT_BINDGEN_URL ?= https://github.com/dicej/wit-bindgen/releases/download/wit-bindgen-cli-0.17.0-dicej-pre0/wit-bindgen-v0.17.0-dicej-pre0-x86_64-linux.tar.gz
2637

2738
# When the length is no larger than this threshold, we consider the
2839
# overhead of bulk memory opcodes to outweigh the performance benefit,
@@ -37,7 +48,7 @@ BULK_MEMORY_THRESHOLD ?= 32
3748
# Set the default WASI target triple.
3849
TARGET_TRIPLE = wasm32-wasi
3950

40-
# Threaded version necessitates a different traget, as objects from different
51+
# Threaded version necessitates a different target, as objects from different
4152
# targets can't be mixed together while linking.
4253
ifeq ($(THREAD_MODEL), posix)
4354
TARGET_TRIPLE = wasm32-wasi-threads
@@ -68,6 +79,16 @@ LIBC_BOTTOM_HALF_ALL_SOURCES = \
6879
$(shell find $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC) -name \*.c) \
6980
$(shell find $(LIBC_BOTTOM_HALF_SOURCES) -name \*.c))
7081

82+
ifeq ($(WASI_SNAPSHOT), preview1)
83+
# Omit source files not relevant to WASI Preview 1. As we introduce files
84+
# supporting `wasi-sockets` for `wasm32-wasi-preview2`, we'll add those files to
85+
# this list.
86+
LIBC_BOTTOM_HALF_OMIT_SOURCES := $(LIBC_BOTTOM_HALF_SOURCES)/preview2.c
87+
LIBC_BOTTOM_HALF_ALL_SOURCES := $(filter-out $(LIBC_BOTTOM_HALF_OMIT_SOURCES),$(LIBC_BOTTOM_HALF_ALL_SOURCES))
88+
# Omit preview2.h from include-all.c test.
89+
INCLUDE_ALL_CLAUSES := -not -name preview2.h
90+
endif
91+
7192
# FIXME(https://reviews.llvm.org/D85567) - due to a bug in LLD the weak
7293
# references to a function defined in `chdir.c` only work if `chdir.c` is at the
7394
# end of the archive, but once that LLD review lands and propagates into LLVM
@@ -741,7 +762,7 @@ check-symbols: startup_files libc
741762
# Generate a test file that includes all public C header files.
742763
#
743764
cd "$(SYSROOT_INC)" && \
744-
for header in $$(find . -type f -not -name mman.h -not -name signal.h -not -name times.h -not -name resource.h |grep -v /bits/ |grep -v /c++/); do \
765+
for header in $$(find . -type f -not -name mman.h -not -name signal.h -not -name times.h -not -name resource.h $(INCLUDE_ALL_CLAUSES) |grep -v /bits/ |grep -v /c++/); do \
745766
echo '#include <'$$header'>' | sed 's/\.\///' ; \
746767
done |LC_ALL=C sort >$(SYSROOT_SHARE)/include-all.c ; \
747768
cd - >/dev/null
@@ -816,8 +837,65 @@ install: finish
816837
mkdir -p "$(INSTALL_DIR)"
817838
cp -r "$(SYSROOT)/lib" "$(SYSROOT)/share" "$(SYSROOT)/include" "$(INSTALL_DIR)"
818839

840+
$(BINDING_WORK_DIR)/wasi-cli:
841+
mkdir -p "$(BINDING_WORK_DIR)"
842+
cd "$(BINDING_WORK_DIR)" && \
843+
curl -L "$(WASI_CLI_URL)" -o wasi-cli.tar.gz && \
844+
tar xf wasi-cli.tar.gz && \
845+
mv wasi-cli-* wasi-cli
846+
847+
$(BINDING_WORK_DIR)/wit-bindgen:
848+
mkdir -p "$(BINDING_WORK_DIR)"
849+
cd "$(BINDING_WORK_DIR)" && \
850+
curl -L "$(WIT_BINDGEN_URL)" -o wit-bindgen.tar.gz && \
851+
tar xf wit-bindgen.tar.gz && \
852+
mv wit-bindgen-* wit-bindgen
853+
854+
bindings: $(BINDING_WORK_DIR)/wasi-cli $(BINDING_WORK_DIR)/wit-bindgen
855+
cd "$(BINDING_WORK_DIR)" && \
856+
./wit-bindgen/wit-bindgen c \
857+
--rename-world preview2 \
858+
--type-section-suffix __wasi_libc \
859+
--world wasi:cli/imports@0.2.0-rc-2023-12-05 \
860+
--rename wasi:clocks/monotonic-clock@0.2.0-rc-2023-11-10=monotonic_clock \
861+
--rename wasi:clocks/wall-clock@0.2.0-rc-2023-11-10=wall_clock \
862+
--rename wasi:filesystem/preopens@0.2.0-rc-2023-11-10=filesystem_preopens \
863+
--rename wasi:filesystem/types@0.2.0-rc-2023-11-10=filesystem \
864+
--rename wasi:io/error@0.2.0-rc-2023-11-10=io_error \
865+
--rename wasi:io/poll@0.2.0-rc-2023-11-10=poll \
866+
--rename wasi:io/streams@0.2.0-rc-2023-11-10=streams \
867+
--rename wasi:random/insecure-seed@0.2.0-rc-2023-11-10=random_insecure_seed \
868+
--rename wasi:random/insecure@0.2.0-rc-2023-11-10=random_insecure \
869+
--rename wasi:random/random@0.2.0-rc-2023-11-10=random \
870+
--rename wasi:sockets/instance-network@0.2.0-rc-2023-11-10=instance_network \
871+
--rename wasi:sockets/ip-name-lookup@0.2.0-rc-2023-11-10=ip_name_lookup \
872+
--rename wasi:sockets/network@0.2.0-rc-2023-11-10=network \
873+
--rename wasi:sockets/tcp-create-socket@0.2.0-rc-2023-11-10=tcp_create_socket \
874+
--rename wasi:sockets/tcp@0.2.0-rc-2023-11-10=tcp \
875+
--rename wasi:sockets/udp-create-socket@0.2.0-rc-2023-11-10=udp_create_socket \
876+
--rename wasi:sockets/udp@0.2.0-rc-2023-11-10=udp \
877+
--rename wasi:cli/environment@0.2.0-rc-2023-12-05=environment \
878+
--rename wasi:cli/exit@0.2.0-rc-2023-12-05=exit \
879+
--rename wasi:cli/stdin@0.2.0-rc-2023-12-05=stdin \
880+
--rename wasi:cli/stdout@0.2.0-rc-2023-12-05=stdout \
881+
--rename wasi:cli/stderr@0.2.0-rc-2023-12-05=stderr \
882+
--rename wasi:cli/terminal-input@0.2.0-rc-2023-12-05=terminal_input \
883+
--rename wasi:cli/terminal-output@0.2.0-rc-2023-12-05=terminal_output \
884+
--rename wasi:cli/terminal-stdin@0.2.0-rc-2023-12-05=terminal_stdin \
885+
--rename wasi:cli/terminal-stdout@0.2.0-rc-2023-12-05=terminal_stdout \
886+
--rename wasi:cli/terminal-stderr@0.2.0-rc-2023-12-05=terminal_stderr \
887+
./wasi-cli/wit && \
888+
mv preview2.h ../../libc-bottom-half/headers/public/wasi/ && \
889+
mv preview2_component_type.o ../../libc-bottom-half/sources && \
890+
sed 's_#include "preview2.h"_#include "wasi/preview2.h"_' \
891+
< preview2.c \
892+
> ../../libc-bottom-half/sources/preview2.c && \
893+
rm preview2.c
894+
895+
819896
clean:
897+
$(RM) -r "$(BINDING_WORK_DIR)"
820898
$(RM) -r "$(OBJDIR)"
821899
$(RM) -r "$(SYSROOT)"
822900

823-
.PHONY: default startup_files libc libc_so finish install include_dirs clean check-symbols
901+
.PHONY: default startup_files libc libc_so finish install include_dirs clean check-symbols bindings

0 commit comments

Comments
 (0)