Skip to content

Commit 93d0e7d

Browse files
committed
udf: move Wasmtime runtime implementation to Rust
Instead of binding to the Wasmtime C API library, the support is now moved entirely to Rust, with only the minimal set of C-compatible functions exported to be callable from libSQL main code. The new code produces a libwblibsql.so dynamic library which contains the implementation of all functions required by our ext/udf/wasm_bindings.h header. The stripped library weighs 6.4MiB, which is quite heavy, but already much better than Wasmtime's default C API library, which weighted ~17MiB.
1 parent 79ec00b commit 93d0e7d

10 files changed

Lines changed: 1812 additions & 332 deletions

File tree

.github/workflows/maketestwasm.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ jobs:
1717
- name: get TCL
1818
run: sudo apt-get install -y tcl8.6-dev
1919

20-
- name: get Wasmtime
21-
run: wget -q -O - https://github.com/bytecodealliance/wasmtime/releases/download/v1.0.1/wasmtime-v1.0.1-x86_64-linux-c-api.tar.xz | tar -xvJ
22-
&& sudo cp -vr wasmtime*/include /usr/ && sudo cp -vr wasmtime*/lib /usr/
23-
2420
- name: configure with Wasm
2521
run: ./configure --enable-wasm-runtime
22+
23+
- name: make the library generally available
24+
run: sudo ln -s $(pwd)/.libs/libwblibsql.so /usr/lib/libwblibsql.so && ls -lsh /usr/lib
2625

2726
- name: Run tests
2827
run: make test

Makefile.in

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,13 @@ LIBREADLINE = @TARGET_READLINE_LIBS@
6666
#
6767
TCC += -DSQLITE_THREADSAFE=@SQLITE_THREADSAFE@
6868

69+
# Optional WebAssembly runtime library
70+
#
71+
TARGET_OPT_WASM_RUNTIME_LINK = @TARGET_OPT_WASM_RUNTIME_LINK@
72+
6973
# Any target libraries which libsqlite must be linked against
7074
#
71-
TLIBS = @LIBS@ $(LIBS)
75+
TLIBS = @LIBS@ $(LIBS) $(TARGET_OPT_WASM_RUNTIME_LINK)
7276

7377
# Flags controlling use of the in memory btree implementation
7478
#
@@ -369,8 +373,6 @@ SRC += \
369373
$(TOP)/ext/rbu/sqlite3rbu.c
370374
SRC += \
371375
$(TOP)/ext/misc/stmt.c
372-
SRC += \
373-
$(TOP)/ext/udf/wasmtime_bindings.c
374376

375377
# Generated source code files
376378
#
@@ -775,7 +777,10 @@ mptest: mptester$(TEXE)
775777
cp fts5.c fts5.h tsrc
776778
touch .target_source
777779

778-
sqlite3.c: .target_source $(TOP)/tool/mksqlite3c.tcl
780+
libwblibsql.so: $(TOP)/src/rust/wasmtime-bindings/src/lib.rs
781+
( mkdir -p .libs && cd $(TOP)/src/rust/wasmtime-bindings/ && cargo build --release && cp target/release/libwblibsql.so $(TOP)/.libs/ )
782+
783+
sqlite3.c: .target_source $(TOP)/tool/mksqlite3c.tcl @OPT_WASM_RUNTIME_LIB@
779784
$(TCLSH_CMD) $(TOP)/tool/mksqlite3c.tcl $(AMALGAMATION_LINE_MACROS)
780785
cp tsrc/sqlite3ext.h .
781786
cp $(TOP)/ext/session/sqlite3session.h .

configure

Lines changed: 55 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,9 @@ BUILD_CFLAGS
800800
AMALGAMATION_LINE_MACROS
801801
USE_GCOV
802802
OPT_FEATURE_FLAGS
803+
OPT_WASM_RUNTIME_LIB
804+
TARGET_OPT_WASM_RUNTIME_LINK
805+
CARGO_BIN
803806
HAVE_ZLIB
804807
USE_AMALGAMATION
805808
TARGET_DEBUG
@@ -4460,13 +4463,13 @@ then :
44604463
else $as_nop
44614464
lt_cv_nm_interface="BSD nm"
44624465
echo "int some_variable = 0;" > conftest.$ac_ext
4463-
(eval echo "\"\$as_me:4463: $ac_compile\"" >&5)
4466+
(eval echo "\"\$as_me:4466: $ac_compile\"" >&5)
44644467
(eval "$ac_compile" 2>conftest.err)
44654468
cat conftest.err >&5
4466-
(eval echo "\"\$as_me:4466: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
4469+
(eval echo "\"\$as_me:4469: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
44674470
(eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
44684471
cat conftest.err >&5
4469-
(eval echo "\"\$as_me:4469: output\"" >&5)
4472+
(eval echo "\"\$as_me:4472: output\"" >&5)
44704473
cat conftest.out >&5
44714474
if $GREP 'External.*some_variable' conftest.out > /dev/null; then
44724475
lt_cv_nm_interface="MS dumpbin"
@@ -5717,7 +5720,7 @@ ia64-*-hpux*)
57175720
;;
57185721
*-*-irix6*)
57195722
# Find out which ABI we are using.
5720-
echo '#line 5720 "configure"' > conftest.$ac_ext
5723+
echo '#line 5723 "configure"' > conftest.$ac_ext
57215724
if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5
57225725
(eval $ac_compile) 2>&5
57235726
ac_status=$?
@@ -7060,11 +7063,11 @@ else $as_nop
70607063
-e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
70617064
-e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
70627065
-e 's:$: $lt_compiler_flag:'`
7063-
(eval echo "\"\$as_me:7063: $lt_compile\"" >&5)
7066+
(eval echo "\"\$as_me:7066: $lt_compile\"" >&5)
70647067
(eval "$lt_compile" 2>conftest.err)
70657068
ac_status=$?
70667069
cat conftest.err >&5
7067-
echo "$as_me:7067: \$? = $ac_status" >&5
7070+
echo "$as_me:7070: \$? = $ac_status" >&5
70687071
if (exit $ac_status) && test -s "$ac_outfile"; then
70697072
# The compiler can only warn and ignore the option if not recognized
70707073
# So say no if there are warnings other than the usual output.
@@ -7400,11 +7403,11 @@ else $as_nop
74007403
-e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
74017404
-e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
74027405
-e 's:$: $lt_compiler_flag:'`
7403-
(eval echo "\"\$as_me:7403: $lt_compile\"" >&5)
7406+
(eval echo "\"\$as_me:7406: $lt_compile\"" >&5)
74047407
(eval "$lt_compile" 2>conftest.err)
74057408
ac_status=$?
74067409
cat conftest.err >&5
7407-
echo "$as_me:7407: \$? = $ac_status" >&5
7410+
echo "$as_me:7410: \$? = $ac_status" >&5
74087411
if (exit $ac_status) && test -s "$ac_outfile"; then
74097412
# The compiler can only warn and ignore the option if not recognized
74107413
# So say no if there are warnings other than the usual output.
@@ -7507,11 +7510,11 @@ else $as_nop
75077510
-e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
75087511
-e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
75097512
-e 's:$: $lt_compiler_flag:'`
7510-
(eval echo "\"\$as_me:7510: $lt_compile\"" >&5)
7513+
(eval echo "\"\$as_me:7513: $lt_compile\"" >&5)
75117514
(eval "$lt_compile" 2>out/conftest.err)
75127515
ac_status=$?
75137516
cat out/conftest.err >&5
7514-
echo "$as_me:7514: \$? = $ac_status" >&5
7517+
echo "$as_me:7517: \$? = $ac_status" >&5
75157518
if (exit $ac_status) && test -s out/conftest2.$ac_objext
75167519
then
75177520
# The compiler can only warn and ignore the option if not recognized
@@ -7563,11 +7566,11 @@ else $as_nop
75637566
-e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
75647567
-e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
75657568
-e 's:$: $lt_compiler_flag:'`
7566-
(eval echo "\"\$as_me:7566: $lt_compile\"" >&5)
7569+
(eval echo "\"\$as_me:7569: $lt_compile\"" >&5)
75677570
(eval "$lt_compile" 2>out/conftest.err)
75687571
ac_status=$?
75697572
cat out/conftest.err >&5
7570-
echo "$as_me:7570: \$? = $ac_status" >&5
7573+
echo "$as_me:7573: \$? = $ac_status" >&5
75717574
if (exit $ac_status) && test -s out/conftest2.$ac_objext
75727575
then
75737576
# The compiler can only warn and ignore the option if not recognized
@@ -9951,7 +9954,7 @@ else
99519954
lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
99529955
lt_status=$lt_dlunknown
99539956
cat > conftest.$ac_ext <<_LT_EOF
9954-
#line 9954 "configure"
9957+
#line 9957 "configure"
99559958
#include "confdefs.h"
99569959
99579960
#if HAVE_DLFCN_H
@@ -10048,7 +10051,7 @@ else
1004810051
lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
1004910052
lt_status=$lt_dlunknown
1005010053
cat > conftest.$ac_ext <<_LT_EOF
10051-
#line 10051 "configure"
10054+
#line 10054 "configure"
1005210055
#include "confdefs.h"
1005310056
1005410057
#if HAVE_DLFCN_H
@@ -11689,7 +11692,7 @@ fi
1168911692

1169011693
if test x"$found" = xno; then
1169111694
if test x"$enable_readline" = xyes; then
11692-
as_fn_error 1 "No suitable readline library found"
11695+
as_fn_error $? "\"No suitable readline library found\"" "$LINENO" 5
1169311696
fi
1169411697
TARGET_READLINE_LIBS=""
1169511698
TARGET_READLINE_INC=""
@@ -12063,70 +12066,59 @@ if test "$enable_wasm_runtime" = "yes"; then
1206312066
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
1206412067
printf "%s\n" "yes" >&6; }
1206512068
OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DLIBSQL_ENABLE_WASM_RUNTIME"
12066-
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for library containing wasmtime_store_new" >&5
12067-
printf %s "checking for library containing wasmtime_store_new... " >&6; }
12068-
if test ${ac_cv_search_wasmtime_store_new+y}
12069+
# Extract the first word of "cargo", so it can be a program name with args.
12070+
set dummy cargo; ac_word=$2
12071+
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
12072+
printf %s "checking for $ac_word... " >&6; }
12073+
if test ${ac_cv_prog_CARGO_BIN+y}
1206912074
then :
1207012075
printf %s "(cached) " >&6
1207112076
else $as_nop
12072-
ac_func_search_save_LIBS=$LIBS
12073-
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
12074-
/* end confdefs.h. */
12075-
12076-
/* Override any GCC internal prototype to avoid an error.
12077-
Use char because int might match the return type of a GCC
12078-
builtin and then its argument prototype would still apply. */
12079-
char wasmtime_store_new ();
12080-
int
12081-
main (void)
12082-
{
12083-
return wasmtime_store_new ();
12084-
;
12085-
return 0;
12086-
}
12087-
_ACEOF
12088-
for ac_lib in '' wasmtime
12077+
if test -n "$CARGO_BIN"; then
12078+
ac_cv_prog_CARGO_BIN="$CARGO_BIN" # Let the user override the test.
12079+
else
12080+
as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
12081+
for as_dir in $PATH
1208912082
do
12090-
if test -z "$ac_lib"; then
12091-
ac_res="none required"
12092-
else
12093-
ac_res=-l$ac_lib
12094-
LIBS="-l$ac_lib $ac_func_search_save_LIBS"
12083+
IFS=$as_save_IFS
12084+
case $as_dir in #(((
12085+
'') as_dir=./ ;;
12086+
*/) ;;
12087+
*) as_dir=$as_dir/ ;;
12088+
esac
12089+
for ac_exec_ext in '' $ac_executable_extensions; do
12090+
if as_fn_executable_p "$as_dir$ac_word$ac_exec_ext"; then
12091+
ac_cv_prog_CARGO_BIN=""
12092+
printf "%s\n" "$as_me:${as_lineno-$LINENO}: found $as_dir$ac_word$ac_exec_ext" >&5
12093+
break 2
1209512094
fi
12096-
if ac_fn_c_try_link "$LINENO"
12097-
then :
12098-
ac_cv_search_wasmtime_store_new=$ac_res
12099-
fi
12100-
rm -f core conftest.err conftest.$ac_objext conftest.beam \
12101-
conftest$ac_exeext
12102-
if test ${ac_cv_search_wasmtime_store_new+y}
12103-
then :
12104-
break
12105-
fi
1210612095
done
12107-
if test ${ac_cv_search_wasmtime_store_new+y}
12108-
then :
12096+
done
12097+
IFS=$as_save_IFS
1210912098

12110-
else $as_nop
12111-
ac_cv_search_wasmtime_store_new=no
1211212099
fi
12113-
rm conftest.$ac_ext
12114-
LIBS=$ac_func_search_save_LIBS
1211512100
fi
12116-
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_wasmtime_store_new" >&5
12117-
printf "%s\n" "$ac_cv_search_wasmtime_store_new" >&6; }
12118-
ac_res=$ac_cv_search_wasmtime_store_new
12119-
if test "$ac_res" != no
12120-
then :
12121-
test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
12122-
12101+
CARGO_BIN=$ac_cv_prog_CARGO_BIN
12102+
if test -n "$CARGO_BIN"; then
12103+
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $CARGO_BIN" >&5
12104+
printf "%s\n" "$CARGO_BIN" >&6; }
12105+
else
12106+
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
12107+
printf "%s\n" "no" >&6; }
1212312108
fi
1212412109

12110+
12111+
TARGET_OPT_WASM_RUNTIME_LINK='-L$(TOP)/.libs -lwblibsql'
12112+
OPT_WASM_RUNTIME_LIB="libwblibsql.so"
1212512113
else
12114+
TARGET_OPT_WASM_RUNTIME_LINK=""
12115+
OPT_WASM_RUNTIME_LIB=""
1212612116
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
1212712117
printf "%s\n" "no" >&6; }
1212812118
fi
1212912119

12120+
12121+
1213012122
########
1213112123
# The --enable-all argument is short-hand to enable
1213212124
# multiple extensions.

configure.ac

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,10 +636,16 @@ AC_MSG_CHECKING([whether to support WebAssembly integration])
636636
if test "$enable_wasm_runtime" = "yes"; then
637637
AC_MSG_RESULT([yes])
638638
OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DLIBSQL_ENABLE_WASM_RUNTIME"
639-
AC_SEARCH_LIBS(wasmtime_store_new, wasmtime)
639+
AC_CHECK_PROG(CARGO_BIN, cargo)
640+
TARGET_OPT_WASM_RUNTIME_LINK='-L$(TOP)/.libs -lwblibsql'
641+
OPT_WASM_RUNTIME_LIB="libwblibsql.so"
640642
else
643+
TARGET_OPT_WASM_RUNTIME_LINK=""
644+
OPT_WASM_RUNTIME_LIB=""
641645
AC_MSG_RESULT([no])
642646
fi
647+
AC_SUBST(TARGET_OPT_WASM_RUNTIME_LINK)
648+
AC_SUBST(OPT_WASM_RUNTIME_LIB)
643649

644650
########
645651
# The --enable-all argument is short-hand to enable

0 commit comments

Comments
 (0)