diff --git a/.github/workflows/build-php.yaml b/.github/workflows/build-php.yaml index 5375b24..628a568 100644 --- a/.github/workflows/build-php.yaml +++ b/.github/workflows/build-php.yaml @@ -38,6 +38,16 @@ jobs: flavor: -slim build-php-cli: false version: 8.2.0 + - name: php- + suffix: "" + flavor: "" + build-php-cli: false + version: 8.2.6 + - name: wasmedge-php- + suffix: "-wasmedge" + flavor: "" + build-php-cli: false + version: 8.2.6 runs-on: ubuntu-latest steps: - name: Checkout repository diff --git a/.github/workflows/build-python.yaml b/.github/workflows/build-python.yaml index 9a3eb1e..e49ec02 100644 --- a/.github/workflows/build-python.yaml +++ b/.github/workflows/build-python.yaml @@ -15,16 +15,16 @@ jobs: include: - prefix: "" suffix: "" - version: "3.11.3" + version: "3.11.4" - prefix: "wasmedge-" suffix: "-wasmedge" - version: "3.11.3" + version: "3.11.4" - prefix: "aio-" suffix: "-aio" - version: "3.11.3" + version: "3.11.4" - prefix: "aio-wasmedge-" suffix: "-aio-wasmedge" - version: "3.11.3" + version: "3.11.4" runs-on: ubuntu-latest steps: - name: Checkout repository diff --git a/.github/workflows/build-ruby.yaml b/.github/workflows/build-ruby.yaml index 6998014..d43a431 100644 --- a/.github/workflows/build-ruby.yaml +++ b/.github/workflows/build-ruby.yaml @@ -16,6 +16,11 @@ jobs: include: - version: 3.2.0 target_version: 3_2_0 + - version: 3.2.2 + target_version: 3_2_2 + exclude: + - flavor: "-slim" + version: 3.2.2 runs-on: ubuntu-latest steps: - name: Checkout repository diff --git a/.github/workflows/release-php.yaml b/.github/workflows/release-php.yaml index 2185893..2e99465 100644 --- a/.github/workflows/release-php.yaml +++ b/.github/workflows/release-php.yaml @@ -39,6 +39,16 @@ jobs: flavor: -slim build-php-cli: false version: 8.2.0 + - name: php- + suffix: "" + flavor: "" + build-php-cli: false + version: 8.2.6 + - name: wasmedge-php- + suffix: "-wasmedge" + flavor: "" + build-php-cli: true + version: 8.2.6 runs-on: ubuntu-latest steps: - name: Checkout repository diff --git a/.github/workflows/release-ruby.yaml b/.github/workflows/release-ruby.yaml index 020fd97..b4d17ba 100644 --- a/.github/workflows/release-ruby.yaml +++ b/.github/workflows/release-ruby.yaml @@ -17,6 +17,11 @@ jobs: include: - version: 3.2.0 target_version: 3_2_0 + - version: 3.2.2 + target_version: 3_2_2 + exclude: + - flavor: "-slim" + version: 3.2.2 runs-on: ubuntu-latest steps: - name: Checkout repository diff --git a/Dockerfile.wasi-builder b/Dockerfile.wasi-builder index 99c5b18..00c2cb1 100644 --- a/Dockerfile.wasi-builder +++ b/Dockerfile.wasi-builder @@ -28,6 +28,7 @@ RUN apt update && \ clang \ curl \ git \ + jq \ libtool \ libtool-bin \ pkg-config \ diff --git a/Makefile b/Makefile index f018351..8420a0d 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,11 @@ php/wasmedge-php-8.2.0: WLR_BUILD_FLAVOR=wasmedge \ make -C php php-8.2.0 +.PHONY: php/wasmedge-php-8.2.6 +php/wasmedge-php-8.2.6: + WLR_BUILD_FLAVOR=wasmedge \ + make -C php php-8.2.6 + .PHONY: php/master php/master: make -C php master @@ -62,42 +67,42 @@ oci-python-3.11.1-wasmedge: python/wasmedge-v3.11.1 -f images/python/Dockerfile \ build-output -.PHONY: python/wasmedge-v3.11.3 -python/wasmedge-v3.11.3: +.PHONY: python/wasmedge-v3.11.4 +python/wasmedge-v3.11.4: WLR_BUILD_FLAVOR=wasmedge \ make -C python $(subst python/wasmedge-,,$@) -.PHONY: python/aio-v3.11.3 -python/aio-v3.11.3: +.PHONY: python/aio-v3.11.4 +python/aio-v3.11.4: WLR_BUILD_FLAVOR=aio \ make -C python $(subst python/aio-,,$@) -.PHONY: python/aio-wasmedge-v3.11.3 -python/aio-wasmedge-v3.11.3: +.PHONY: python/aio-wasmedge-v3.11.4 +python/aio-wasmedge-v3.11.4: WLR_BUILD_FLAVOR=aio-wasmedge \ make -C python $(subst python/aio-wasmedge-,,$@) -.PHONY: oci-python-3.11.3 -oci-python-3.11.3: python/v3.11.3 +.PHONY: oci-python-3.11.4 +oci-python-3.11.4: python/v3.11.4 docker build \ --platform wasi/wasm32 \ --build-arg NAME=python-wasm \ --build-arg SUMMARY="CPython built for WASI, by Wasm Labs" \ - --build-arg ARTIFACTS_BASE_DIR=python/v3.11.3 \ + --build-arg ARTIFACTS_BASE_DIR=python/v3.11.4 \ --build-arg PYTHON_BINARY=python.wasm \ - -t ghcr.io/vmware-labs/python-wasm:3.11.3 \ + -t ghcr.io/vmware-labs/python-wasm:3.11.4 \ -f images/python/Dockerfile \ build-output -.PHONY: oci-python-3.11.3-wasmedge -oci-python-3.11.3-wasmedge: python/wasmedge-v3.11.3 +.PHONY: oci-python-3.11.4-wasmedge +oci-python-3.11.3-wasmedge: python/wasmedge-v3.11.4 docker build \ --platform wasi/wasm32 \ --build-arg NAME=python-wasm \ --build-arg SUMMARY="CPython built for WASI+WasmEdge, by Wasm Labs" \ - --build-arg ARTIFACTS_BASE_DIR=python/v3.11.3-wasmedge \ + --build-arg ARTIFACTS_BASE_DIR=python/v3.11.4-wasmedge \ --build-arg PYTHON_BINARY=python.wasm \ - -t ghcr.io/vmware-labs/python-wasm:3.11.3-wasmedge \ + -t ghcr.io/vmware-labs/python-wasm:3.11.4-wasmedge \ -f images/python/Dockerfile \ build-output diff --git a/libs/bundle_wlr/README.md b/libs/bundle_wlr/README.md index bc7a8eb..19bebcd 100644 --- a/libs/bundle_wlr/README.md +++ b/libs/bundle_wlr/README.md @@ -2,4 +2,4 @@ This folder builds assets of the bundle_wlr project. -The list of bundled libraries can be deduced from [wlr-build-deps.sh](./wlr-build-deps.sh) in main project page is at [https://www.zlib.net/](https://www.zlib.net/). +The list of bundled libraries can be deduced from the `"deps"` section in [wlr-info.json](./wlr-info.sh). The main project page is at [https://www.zlib.net/](https://www.zlib.net/). diff --git a/libs/bundle_wlr/wlr-build-deps.sh b/libs/bundle_wlr/wlr-build-deps.sh deleted file mode 100644 index 6770e85..0000000 --- a/libs/bundle_wlr/wlr-build-deps.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bash - -if [ "${BASH_SOURCE-}" = "$0" ]; then - echo "You must source this script to add to CFLAGS and LDFLAGS: \$ source $0" >&2 - return -fi - -source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_dependencies.sh - -wlr_dependencies_add "zlib" "libs/zlib/v1.2.13" "lib/wasm32-wasi/libz.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Fzlib%2F1.2.13%2B20230310-c46e363/libz-1.2.13-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "libpng" "libs/libpng/v1.6.39" "lib/wasm32-wasi/libpng16.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Flibpng%2F1.6.39%2B20230310-13a5f2e/libpng-1.6.39-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "libjpeg" "libs/libjpeg/v2.1.5.1" "lib/wasm32-wasi/libjpeg.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Flibjpeg%2F2.1.5.1%2B20230310-c46e363/libjpeg-2.1.5.1-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "libxml2" "libs/libxml2/v2.10.3" "lib/wasm32-wasi/libxml2.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Flibxml2%2F2.10.3%2B20230310-c46e363/libxml2-2.10.3-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "oniguruma" "libs/oniguruma/v6.9.8" "lib/wasm32-wasi/libonig.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Foniguruma%2F6.9.8%2B20230310-c46e363/libonig-6.9.8-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "SQLite" "libs/sqlite/version-3.41.2" "lib/wasm32-wasi/libsqlite3.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Fsqlite%2F3.41.2%2B20230329-43f9aea/libsqlite-3.41.2-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "uuid" "libs/uuid/libuuid-1.0.3" "lib/wasm32-wasi/libuuid.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Flibuuid%2F1.0.3%2B20230310-c46e363/libuuid-1.0.3-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "bzip2" "libs/bzip2/v1.0.8" "lib/wasm32-wasi/libbz2.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Fbzip2%2F1.0.8%2B20230425-e1a7579/libbzip2-1.0.8-wasi-sdk-19.0.tar.gz" diff --git a/libs/bundle_wlr/wlr-info.json b/libs/bundle_wlr/wlr-info.json new file mode 100644 index 0000000..88c6050 --- /dev/null +++ b/libs/bundle_wlr/wlr-info.json @@ -0,0 +1,36 @@ +{ + "deps": { + "zlib": { + "build_target": "libs/zlib/v1.2.13", + "required_file": "lib/wasm32-wasi/libz.a" + }, + "libpng": { + "build_target": "libs/libpng/v1.6.39", + "required_file": "lib/wasm32-wasi/libpng16.a" + }, + "libjpeg": { + "build_target": "libs/libjpeg/v2.1.5.1", + "required_file": "lib/wasm32-wasi/libjpeg.a" + }, + "libxml2": { + "build_target": "libs/libxml2/v2.11.4", + "required_file": "lib/wasm32-wasi/libxml2.a" + }, + "oniguruma": { + "build_target": "libs/oniguruma/v6.9.8", + "required_file": "lib/wasm32-wasi/libonig.a" + }, + "SQLite": { + "build_target": "libs/sqlite/v3.42.0", + "required_file": "lib/wasm32-wasi/libsqlite3.a" + }, + "libuuid": { + "build_target": "libs/libuuid/v1.0.3", + "required_file": "lib/wasm32-wasi/libuuid.a" + }, + "bzip2": { + "build_target": "libs/bzip2/v1.0.8", + "required_file": "lib/wasm32-wasi/libbz2.a" + } + } +} \ No newline at end of file diff --git a/libs/libpng/v1.6.39/wlr-build-deps.sh b/libs/libpng/v1.6.39/wlr-build-deps.sh deleted file mode 100644 index 0893188..0000000 --- a/libs/libpng/v1.6.39/wlr-build-deps.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash - -if [ "${BASH_SOURCE-}" = "$0" ]; then - echo "You must source this script to add to CFLAGS and LDFLAGS: \$ source $0" >&2 - return -fi - -source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_dependencies.sh - -wlr_dependencies_add "zlib" "libs/zlib/v1.2.13" "lib/wasm32-wasi/libz.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Fzlib%2F1.2.13%2B20230310-c46e363/libz-1.2.13-wasi-sdk-19.0.tar.gz" - diff --git a/libs/libpng/v1.6.39/wlr-info.json b/libs/libpng/v1.6.39/wlr-info.json new file mode 100644 index 0000000..13ebcec --- /dev/null +++ b/libs/libpng/v1.6.39/wlr-info.json @@ -0,0 +1,8 @@ +{ + "deps": { + "zlib": { + "build_target": "libs/zlib/v1.2.13", + "required_file": "lib/wasm32-wasi/libz.a" + } + } +} \ No newline at end of file diff --git a/libs/libuuid/v1.0.3/wlr-env-repo.sh b/libs/libuuid/v1.0.3/wlr-env-repo.sh index 0e2e04d..07c9472 100644 --- a/libs/libuuid/v1.0.3/wlr-env-repo.sh +++ b/libs/libuuid/v1.0.3/wlr-env-repo.sh @@ -12,6 +12,6 @@ fi export WLR_REPO=https://git.code.sf.net/p/libuuid/code export WLR_REPO_BRANCH=libuuid-1.0.3 -export WLR_ENV_NAME=uuid/libuuid-1.0.3 +export WLR_ENV_NAME=libuuid/libuuid-1.0.3 export WLR_PACKAGE_VERSION=1.0.3 export WLR_PACKAGE_NAME=uuid diff --git a/libs/libxml2/v2.11.4/wlr-build.sh b/libs/libxml2/v2.11.4/wlr-build.sh new file mode 100755 index 0000000..8840b11 --- /dev/null +++ b/libs/libxml2/v2.11.4/wlr-build.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash + +if [[ ! -v WLR_ENV ]] +then + echo "Wasmlabs environment is not set" + exit 1 +fi + +export CFLAGS_CONFIG="-O0" + +export CFLAGS_WASI="--sysroot=${WASI_SYSROOT}" +export LDFLAGS_WASI="--sysroot=${WASI_SYSROOT}" + +export CFLAGS_LIBXML2='' + +logStatus "Using LIBXML2 DEFINES: ${CFLAGS_LIBXML2}" + +export CFLAGS_BUILD='' + +# We need to add LDFLAGS ot CFLAGS because autoconf compiles(+links) to binary when checking stuff +export CFLAGS="${CFLAGS_CONFIG} ${CFLAGS_WASI} ${CFLAGS_LIBXML2} ${CFLAGS_BUILD} ${LDFLAGS_WASI}" +export LDFLAGS="${LDFLAGS_WASI}" + +cd "${WLR_SOURCE_PATH}" + +source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_pkg_config.sh + +if [[ -z "$WLR_SKIP_CONFIGURE" ]]; then + ./autogen.sh + export LIBXML2_CONFIGURE="${WLR_CONFIGURE_PREFIXES} --enable-static --disable-shared --with-minimum=yes --with-output=yes --with-schemas=yes --with-tree=yes --with-valid=yes --with-html=yes --with-xpath=yes --with-reader=yes --with-writer=yes --with-xinclude=yes --with-c14n=yes --with-sax1=yes" + logStatus "Configuring build with '${LIBXML2_CONFIGURE}'..." + ./configure --config-cache --host=wasm32-wasi host_alias=wasm32-musl-wasi --target=wasm32-wasi target_alias=wasm32-musl-wasi ${LIBXML2_CONFIGURE} || exit 1 +else + logStatus "Skipping configure..." +fi + +logStatus "Building..." +make -j || exit 1 + +logStatus "Preparing artifacts..." +make install ${WLR_INSTALL_PREFIXES} || exit 1 + +wlr_package_lib + +logStatus "DONE. Artifacts in ${WLR_OUTPUT}" diff --git a/libs/libxml2/v2.11.4/wlr-env-repo.sh b/libs/libxml2/v2.11.4/wlr-env-repo.sh new file mode 100644 index 0000000..c0029be --- /dev/null +++ b/libs/libxml2/v2.11.4/wlr-env-repo.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +if [[ $1 == "--unset" ]]; then + unset WLR_REPO + unset WLR_REPO_BRANCH + unset WLR_ENV_NAME + unset WLR_PACKAGE_VERSION + unset WLR_PACKAGE_NAME + return +fi + +export WLR_REPO=https://github.com/GNOME/libxml2.git +export WLR_REPO_BRANCH=v2.11.4 +export WLR_ENV_NAME=libxml2/v2.11.4 +export WLR_PACKAGE_VERSION=2.11.4 +export WLR_PACKAGE_NAME=xml2 diff --git a/libs/libxml2/v2.11.4/wlr-tag.sh b/libs/libxml2/v2.11.4/wlr-tag.sh new file mode 100644 index 0000000..f85f194 --- /dev/null +++ b/libs/libxml2/v2.11.4/wlr-tag.sh @@ -0,0 +1 @@ +export WLR_TAG="libs/libxml2/2.11.4" diff --git a/libs/sqlite/v3.42.0/patches/0001-Allow-installation-of-headers-and-pkg-config-files-w.patch b/libs/sqlite/v3.42.0/patches/0001-Allow-installation-of-headers-and-pkg-config-files-w.patch new file mode 100644 index 0000000..5e2872b --- /dev/null +++ b/libs/sqlite/v3.42.0/patches/0001-Allow-installation-of-headers-and-pkg-config-files-w.patch @@ -0,0 +1,39 @@ +From 50027d770993b4bf5875e6cebf608151062765a9 Mon Sep 17 00:00:00 2001 +From: "no-reply@wasmlabs.dev" +Date: Mon, 19 Jun 2023 14:58:12 +0300 +Subject: [PATCH] Allow installation of headers and pkg-config files with the + library + + +diff --git a/Makefile.in b/Makefile.in +index 61cb3ef..68b0252 100644 +--- a/Makefile.in ++++ b/Makefile.in +@@ -1471,19 +1471,19 @@ threadtest5: sqlite3.c $(TOP)/test/threadtest5.c + + # Standard install and cleanup targets + # +-lib_install: libsqlite3.la ++lib_install: libsqlite3.la sqlite3.h sqlite3.pc + $(INSTALL) -d $(DESTDIR)$(libdir) + $(LTINSTALL) libsqlite3.la $(DESTDIR)$(libdir) +- +-install: sqlite3$(TEXE) lib_install sqlite3.h sqlite3.pc ${HAVE_TCL:1=tcl_install} +- $(INSTALL) -d $(DESTDIR)$(bindir) +- $(LTINSTALL) sqlite3$(TEXE) $(DESTDIR)$(bindir) + $(INSTALL) -d $(DESTDIR)$(includedir) + $(INSTALL) -m 0644 sqlite3.h $(DESTDIR)$(includedir) + $(INSTALL) -m 0644 $(TOP)/src/sqlite3ext.h $(DESTDIR)$(includedir) + $(INSTALL) -d $(DESTDIR)$(pkgconfigdir) + $(INSTALL) -m 0644 sqlite3.pc $(DESTDIR)$(pkgconfigdir) + ++install: sqlite3$(TEXE) lib_install ${HAVE_TCL:1=tcl_install} ++ $(INSTALL) -d $(DESTDIR)$(bindir) ++ $(LTINSTALL) sqlite3$(TEXE) $(DESTDIR)$(bindir) ++ + pkgIndex.tcl: + echo 'package ifneeded sqlite3 $(RELEASE) [list load [file join $$dir libtclsqlite3[info sharedlibextension]] sqlite3]' > $@ + tcl_install: lib_install libtclsqlite3.la pkgIndex.tcl +-- +2.38.1 + diff --git a/libs/sqlite/v3.42.0/wlr-build.sh b/libs/sqlite/v3.42.0/wlr-build.sh new file mode 100755 index 0000000..6953bba --- /dev/null +++ b/libs/sqlite/v3.42.0/wlr-build.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +if [[ ! -v WLR_ENV ]] +then + echo "WLR build environment is not set" + exit 1 +fi + +export CFLAGS_CONFIG="-O0" + + +# We need to add LDFLAGS ot CFLAGS because autoconf compiles(+links) to binary when checking stuff +export CFLAGS="${CFLAGS_CONFIG}" + +cd "${WLR_SOURCE_PATH}" + +source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_pkg_config.sh + +unset WASI_SYSROOT +unset CC +unset LD +unset CXX +unset NM +unset AR +unset RANLIB + +if [[ -z "$WLR_SKIP_CONFIGURE" ]]; then + export SQLITE_CONFIGURE="${WLR_CONFIGURE_PREFIXES} --enable-all --with-wasi-sdk=${WASI_SDK_PATH}" + logStatus "Configuring build with '${SQLITE_CONFIGURE}'..." + ./configure --config-cache ${SQLITE_CONFIGURE} || exit 1 +else + logStatus "Skipping configure..." +fi + +logStatus "Building... " +make -j || exit 1 + +logStatus "Preparing artifacts... " +make lib_install ${WLR_INSTALL_PREFIXES} || exit 1 + +wlr_package_lib + +logStatus "DONE. Artifacts in ${WLR_OUTPUT}" diff --git a/libs/sqlite/v3.42.0/wlr-env-repo.sh b/libs/sqlite/v3.42.0/wlr-env-repo.sh new file mode 100644 index 0000000..e24c5b4 --- /dev/null +++ b/libs/sqlite/v3.42.0/wlr-env-repo.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +if [[ $1 == "--unset" ]] +then + unset WLR_REPO + unset WLR_REPO_BRANCH + unset WLR_ENV_NAME + unset WLR_PACKAGE_VERSION + unset WLR_PACKAGE_NAME + return +fi + +export WLR_REPO=https://github.com/sqlite/sqlite.git +export WLR_REPO_BRANCH=version-3.42.0 +export WLR_ENV_NAME=sqlite/v3.42.0 +export WLR_PACKAGE_VERSION=3.42.0 +export WLR_PACKAGE_NAME=sqlite diff --git a/libs/sqlite/v3.42.0/wlr-tag.sh b/libs/sqlite/v3.42.0/wlr-tag.sh new file mode 100644 index 0000000..34b8f86 --- /dev/null +++ b/libs/sqlite/v3.42.0/wlr-tag.sh @@ -0,0 +1 @@ +export WLR_TAG="libs/sqlite/3.42.0" diff --git a/php/master/wlr-build-deps.sh b/php/master/wlr-build-deps.sh deleted file mode 100644 index c764efe..0000000 --- a/php/master/wlr-build-deps.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env bash - -if [ "${BASH_SOURCE-}" = "$0" ]; then - echo "You must source this script to add to CFLAGS and LDFLAGS: \$ source $0" >&2 - return -fi - -source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_dependencies.sh - -wlr_dependencies_add "zlib" "libs/zlib/v1.2.13" "lib/wasm32-wasi/libz.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Fzlib%2F1.2.13%2B20230310-c46e363/libz-1.2.13-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "libpng" "libs/libpng/v1.6.39" "lib/wasm32-wasi/libpng16.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Flibpng%2F1.6.39%2B20230310-13a5f2e/libpng-1.6.39-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "libxml2" "libs/libxml2/v2.10.3" "lib/wasm32-wasi/libxml2.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Flibxml2%2F2.10.3%2B20230310-c46e363/libxml2-2.10.3-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "oniguruma" "libs/oniguruma/v6.9.8" "lib/wasm32-wasi/libonig.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Foniguruma%2F6.9.8%2B20230310-c46e363/libonig-6.9.8-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "SQLite" "libs/sqlite/version-3.41.2" "lib/wasm32-wasi/libsqlite3.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Fsqlite%2F3.41.2%2B20230329-43f9aea/libsqlite-3.41.2-wasi-sdk-19.0.tar.gz" - -logStatus "Completed building dependencies for PHP master!" -logStatus " -> Exported PKG_CONFIG_PATH=${PKG_CONFIG_PATH}" diff --git a/php/master/wlr-info.json b/php/master/wlr-info.json new file mode 100644 index 0000000..3942442 --- /dev/null +++ b/php/master/wlr-info.json @@ -0,0 +1,24 @@ +{ + "deps": { + "zlib": { + "build_target": "libs/zlib/v1.2.13", + "required_file": "lib/wasm32-wasi/libz.a" + }, + "libpng": { + "build_target": "libs/libpng/v1.6.39", + "required_file": "lib/wasm32-wasi/libpng16.a" + }, + "libxml2": { + "build_target": "libs/libxml2/v2.11.4", + "required_file": "lib/wasm32-wasi/libxml2.a" + }, + "oniguruma": { + "build_target": "libs/oniguruma/v6.9.8", + "required_file": "lib/wasm32-wasi/libonig.a" + }, + "SQLite": { + "build_target": "libs/sqlite/v3.42.0", + "required_file": "lib/wasm32-wasi/libsqlite3.a" + } + } +} \ No newline at end of file diff --git a/php/php-8.1.11/wlr-build-deps.sh b/php/php-8.1.11/wlr-build-deps.sh deleted file mode 100644 index 897179a..0000000 --- a/php/php-8.1.11/wlr-build-deps.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash - -if [ "${BASH_SOURCE-}" = "$0" ]; then - echo "You must source this script to add to CFLAGS and LDFLAGS: \$ source $0" >&2 - return -fi - -source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_dependencies.sh - -wlr_dependencies_add "SQLite" "libs/sqlite/version-3.41.2" "lib/wasm32-wasi/libsqlite3.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Fsqlite%2F3.41.2%2B20230329-43f9aea/libsqlite-3.41.2-wasi-sdk-19.0.tar.gz" diff --git a/php/php-8.1.11/wlr-info.json b/php/php-8.1.11/wlr-info.json new file mode 100644 index 0000000..de9d8fa --- /dev/null +++ b/php/php-8.1.11/wlr-info.json @@ -0,0 +1,8 @@ +{ + "deps": { + "SQLite": { + "build_target": "libs/sqlite/v3.42.0", + "required_file": "lib/wasm32-wasi/libsqlite3.a" + } + } +} \ No newline at end of file diff --git a/php/php-8.2.0-slim/wlr-build-deps.sh b/php/php-8.2.0-slim/wlr-build-deps.sh deleted file mode 100644 index 2deec66..0000000 --- a/php/php-8.2.0-slim/wlr-build-deps.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env bash - -logStatus "Building dependencies..." - -if [[ ! -v WLR_ENV ]] -then - echo "WLR build environment is not set" - exit 1 -fi - -if [ "${BASH_SOURCE-}" = "$0" ]; then - echo "You must source this script to add to CFLAGS and LDFLAGS: \$ source $0" >&2 - return -fi - diff --git a/php/php-8.2.0/wlr-build-deps.sh b/php/php-8.2.0/wlr-build-deps.sh deleted file mode 100644 index a488c71..0000000 --- a/php/php-8.2.0/wlr-build-deps.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env bash - -if [ "${BASH_SOURCE-}" = "$0" ]; then - echo "You must source this script to add to CFLAGS and LDFLAGS: \$ source $0" >&2 - return -fi - -source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_dependencies.sh - -wlr_dependencies_add "zlib" "libs/zlib/v1.2.13" "lib/wasm32-wasi/libz.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Fzlib%2F1.2.13%2B20230310-c46e363/libz-1.2.13-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "libpng" "libs/libpng/v1.6.39" "lib/wasm32-wasi/libpng16.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Flibpng%2F1.6.39%2B20230310-13a5f2e/libpng-1.6.39-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "libjpeg" "libs/libjpeg/v2.1.5.1" "lib/wasm32-wasi/libjpeg.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Flibjpeg%2F2.1.5.1%2B20230310-c46e363/libjpeg-2.1.5.1-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "libxml2" "libs/libxml2/v2.10.3" "lib/wasm32-wasi/libxml2.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Flibxml2%2F2.10.3%2B20230310-c46e363/libxml2-2.10.3-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "oniguruma" "libs/oniguruma/v6.9.8" "lib/wasm32-wasi/libonig.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Foniguruma%2F6.9.8%2B20230310-c46e363/libonig-6.9.8-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "SQLite" "libs/sqlite/version-3.41.2" "lib/wasm32-wasi/libsqlite3.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Fsqlite%2F3.41.2%2B20230329-43f9aea/libsqlite-3.41.2-wasi-sdk-19.0.tar.gz" - -logStatus "Completed building dependencies for PHP 8.2.0!" -logStatus " -> Exported PKG_CONFIG_PATH=${PKG_CONFIG_PATH}" diff --git a/php/php-8.2.0/wlr-info.json b/php/php-8.2.0/wlr-info.json new file mode 100644 index 0000000..7a109e2 --- /dev/null +++ b/php/php-8.2.0/wlr-info.json @@ -0,0 +1,28 @@ +{ + "deps": { + "zlib": { + "build_target": "libs/zlib/v1.2.13", + "required_file": "lib/wasm32-wasi/libz.a" + }, + "libpng": { + "build_target": "libs/libpng/v1.6.39", + "required_file": "lib/wasm32-wasi/libpng16.a" + }, + "libjpeg": { + "build_target": "libs/libjpeg/v2.1.5.1", + "required_file": "lib/wasm32-wasi/libjpeg.a" + }, + "libxml2": { + "build_target": "libs/libxml2/v2.11.4", + "required_file": "lib/wasm32-wasi/libxml2.a" + }, + "oniguruma": { + "build_target": "libs/oniguruma/v6.9.8", + "required_file": "lib/wasm32-wasi/libonig.a" + }, + "SQLite": { + "build_target": "libs/sqlite/v3.42.0", + "required_file": "lib/wasm32-wasi/libsqlite3.a" + } + } +} \ No newline at end of file diff --git a/php/php-8.2.6/patches/0001-Initial-port-of-the-php-7.4.32-patch-for-php-8.2.6.patch b/php/php-8.2.6/patches/0001-Initial-port-of-the-php-7.4.32-patch-for-php-8.2.6.patch new file mode 100644 index 0000000..89d20dc --- /dev/null +++ b/php/php-8.2.6/patches/0001-Initial-port-of-the-php-7.4.32-patch-for-php-8.2.6.patch @@ -0,0 +1,2059 @@ +From 5a17d4d8a7075f1c0a09cd64c9427ee58bef93f4 Mon Sep 17 00:00:00 2001 +From: Asen Alexandrov +Date: Tue, 8 Nov 2022 16:07:48 +0200 +Subject: [PATCH 01/18] Initial port of the php 7.4.32 patch for php 8.2.6 + + 19.8% Zend/ + 16.0% ext/posix/ + 24.1% ext/standard/ + 14.4% main/streams/ + 17.2% main/ + 5.5% sapi/cgi/ + + 19.4% Zend/ + 15.7% ext/posix/ + 25.4% ext/standard/ + 14.1% main/streams/ + 16.9% main/ + 5.4% sapi/cgi/ + + 19.4% Zend/ + 15.7% ext/posix/ + 25.4% ext/standard/ + 14.1% main/streams/ + 16.9% main/ + 5.4% sapi/cgi/ + + 19.3% Zend/ + 16.2% ext/posix/ + 25.3% ext/standard/ + 14.0% main/streams/ + 16.8% main/ + 5.4% sapi/cgi/ +diff --git a/Zend/zend.c b/Zend/zend.c +index e0abbcd2c37..84ab3b6ac13 100644 +--- a/Zend/zend.c ++++ b/Zend/zend.c +@@ -781,7 +781,9 @@ static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{ + #endif + executor_globals->saved_fpu_cw_ptr = NULL; + executor_globals->active = 0; ++#ifndef WASM_WASI + executor_globals->bailout = NULL; ++#endif // WASM_WASI + executor_globals->error_handling = EH_NORMAL; + executor_globals->exception_class = NULL; + executor_globals->exception = NULL; +@@ -1164,6 +1166,7 @@ ZEND_COLD void zenderror(const char *error) /* {{{ */ + ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32_t lineno) /* {{{ */ + { + ++#ifndef WASM_WASI + if (!EG(bailout)) { + zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno); + exit(-1); +@@ -1175,6 +1178,7 @@ ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32 + CG(memoize_mode) = 0; + EG(current_execute_data) = NULL; + LONGJMP(*EG(bailout), FAILURE); ++#endif // WASM_WASI + } + /* }}} */ + +diff --git a/Zend/zend.h b/Zend/zend.h +index e5f913a9f90..9ab26a9a925 100644 +--- a/Zend/zend.h ++++ b/Zend/zend.h +@@ -255,6 +255,7 @@ typedef size_t (*zend_write_func_t)(const char *str, size_t str_length); + + #define zend_bailout() _zend_bailout(__FILE__, __LINE__) + ++#ifndef WASM_WASI + #define zend_try \ + { \ + JMP_BUF *__orig_bailout = EG(bailout); \ +@@ -271,6 +272,19 @@ typedef size_t (*zend_write_func_t)(const char *str, size_t str_length); + } + #define zend_first_try EG(bailout)=NULL; zend_try + ++#else // WASM_WASI ++#define zend_try \ ++ { \ ++ if (1) { ++#define zend_catch \ ++ } else { ++#define zend_end_try() \ ++ } \ ++ } ++#define zend_first_try zend_try ++#endif // WASM_WASI ++ ++ + BEGIN_EXTERN_C() + void zend_startup(zend_utility_functions *utility_functions); + void zend_shutdown(void); +diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c +index 5f11f49b071..3f71eb22a32 100644 +--- a/Zend/zend_alloc.c ++++ b/Zend/zend_alloc.c +@@ -80,7 +80,7 @@ + #include + #include + +-#ifndef _WIN32 ++#if !defined(_WIN32) && HAVE_MMAP + # include + # ifndef MAP_ANON + # ifdef MAP_ANONYMOUS +@@ -445,6 +445,8 @@ static void zend_mm_munmap(void *addr, size_t size) + #endif + } + } ++#elif ! HAVE_MMAP ++ free(addr); + #else + if (munmap(addr, size) != 0) { + #if ZEND_MM_ERROR +@@ -472,6 +474,8 @@ static void *zend_mm_mmap_fixed(void *addr, size_t size) + } + ZEND_ASSERT(ptr == addr); + return ptr; ++#elif ! HAVE_MMAP ++ return NULL; + #else + int flags = MAP_PRIVATE | MAP_ANON; + #if defined(MAP_EXCL) +@@ -508,6 +512,10 @@ static void *zend_mm_mmap(size_t size) + return NULL; + } + return ptr; ++#elif ! HAVE_MMAP ++ void* ptr = malloc(size); ++ memset(ptr, 0, size); ++ return ptr; + #else + void *ptr; + +@@ -717,6 +725,11 @@ static zend_always_inline void zend_mm_hugepage(void* ptr, size_t size) + + static void *zend_mm_chunk_alloc_int(size_t size, size_t alignment) + { ++#if ! HAVE_MMAP ++ void* ptr = aligned_alloc(alignment, size); ++ memset(ptr, 0, size); ++ return ptr; ++#else + void *ptr = zend_mm_mmap(size); + + if (ptr == NULL) { +@@ -767,6 +780,7 @@ static void *zend_mm_chunk_alloc_int(size_t size, size_t alignment) + #endif + return ptr; + } ++#endif + } + + static void *zend_mm_chunk_alloc(zend_mm_heap *heap, size_t size, size_t alignment) +@@ -2932,7 +2946,7 @@ ZEND_API void start_memory_manager(void) + #else + alloc_globals_ctor(&alloc_globals); + #endif +-#ifndef _WIN32 ++#if !defined(_WIN32) && HAVE_MMAP + # if defined(_SC_PAGESIZE) + REAL_PAGE_SIZE = sysconf(_SC_PAGESIZE); + # elif defined(_SC_PAGE_SIZE) +diff --git a/Zend/zend_fibers.c b/Zend/zend_fibers.c +index 53f78417f5d..b9f9a26c6e4 100644 +--- a/Zend/zend_fibers.c ++++ b/Zend/zend_fibers.c +@@ -39,7 +39,7 @@ + # include + #endif + +-#ifndef ZEND_WIN32 ++#if !defined(ZEND_WIN32) && HAVE_MMAP + # include + # include + # include +@@ -94,7 +94,9 @@ typedef struct _zend_fiber_vm_state { + zend_execute_data *current_execute_data; + int error_reporting; + uint32_t jit_trace_num; ++#ifndef WASM_WASI + JMP_BUF *bailout; ++#endif // WASM_WASI + zend_fiber *active_fiber; + } zend_fiber_vm_state; + +@@ -107,7 +109,9 @@ static zend_always_inline void zend_fiber_capture_vm_state(zend_fiber_vm_state * + state->current_execute_data = EG(current_execute_data); + state->error_reporting = EG(error_reporting); + state->jit_trace_num = EG(jit_trace_num); ++#ifndef WASM_WASI + state->bailout = EG(bailout); ++#endif // WASM_WASI + state->active_fiber = EG(active_fiber); + } + +@@ -120,7 +124,9 @@ static zend_always_inline void zend_fiber_restore_vm_state(zend_fiber_vm_state * + EG(current_execute_data) = state->current_execute_data; + EG(error_reporting) = state->error_reporting; + EG(jit_trace_num) = state->jit_trace_num; ++#ifndef WASM_WASI + EG(bailout) = state->bailout; ++#endif // WASM_WASI + EG(active_fiber) = state->active_fiber; + } + +@@ -210,6 +216,8 @@ static zend_fiber_stack *zend_fiber_stack_allocate(size_t size) + return NULL; + } + # endif ++#elif defined(WASM_WASI) ++ pointer = malloc(alloc_size); + #else + pointer = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0); + +@@ -259,6 +267,8 @@ static void zend_fiber_stack_free(zend_fiber_stack *stack) + + #ifdef ZEND_WIN32 + VirtualFree(pointer, 0, MEM_RELEASE); ++#elif defined(WASM_WASI) ++ free(pointer); + #else + munmap(pointer, stack->size + ZEND_FIBER_GUARD_PAGES * page_size); + #endif +diff --git a/Zend/zend_globals.h b/Zend/zend_globals.h +index 368127b10a6..637039dc8ba 100644 +--- a/Zend/zend_globals.h ++++ b/Zend/zend_globals.h +@@ -21,7 +21,9 @@ + #define ZEND_GLOBALS_H + + ++#ifndef WASM_WASI + #include ++#endif + #include + + #include "zend_globals_macros.h" +@@ -159,7 +161,9 @@ struct _zend_executor_globals { + + HashTable included_files; /* files already included */ + ++#ifndef WASM_WASI + JMP_BUF *bailout; ++#endif + + int error_reporting; + int exit_status; +diff --git a/Zend/zend_types.h b/Zend/zend_types.h +index e6505d8b62a..a8b5d12c916 100644 +--- a/Zend/zend_types.h ++++ b/Zend/zend_types.h +@@ -1453,5 +1453,15 @@ static zend_always_inline uint32_t zval_delref_p(zval* pz) { + #define ZVAL_COPY_OR_DUP_PROP(z, v) \ + do { ZVAL_COPY_OR_DUP(z, v); Z_PROP_FLAG_P(z) = Z_PROP_FLAG_P(v); } while (0) + ++#ifdef WASM_WASI ++#define LOG_EMERG 0 /* system is unusable */ ++#define LOG_ALERT 1 /* action must be taken immediately */ ++#define LOG_CRIT 2 /* critical conditions */ ++#define LOG_ERR 3 /* error conditions */ ++#define LOG_WARNING 4 /* warning conditions */ ++#define LOG_NOTICE 5 /* normal but significant condition */ ++#define LOG_INFO 6 /* informational */ ++#define LOG_DEBUG 7 /* debug-level messages */ ++#endif // WASM_WASI + + #endif /* ZEND_TYPES_H */ +diff --git a/Zend/zend_virtual_cwd.c b/Zend/zend_virtual_cwd.c +index 64fc1651740..2a3c5c7d427 100644 +--- a/Zend/zend_virtual_cwd.c ++++ b/Zend/zend_virtual_cwd.c +@@ -1400,6 +1400,8 @@ CWD_API int virtual_chmod(const char *filename, mode_t mode) /* {{{ */ + } + ret = php_win32_ioutil_chmod(new_state.cwd, mode); + } ++#elif defined WASM_WASI ++ ret = 0; + #else + ret = chmod(new_state.cwd, mode); + #endif +@@ -1428,7 +1430,11 @@ CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int li + ret = -1; + #endif + } else { ++#ifndef WASM_WASI + ret = chown(new_state.cwd, owner, group); ++#else ++ ret = 0; ++#endif // WASM_WASI + } + + CWD_STATE_FREE_ERR(&new_state); +@@ -1706,8 +1712,11 @@ CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */ + *ptr++ = ' '; + + memcpy(ptr, command, command_length+1); ++#ifndef WASM_WASI + retval = popen(command_line, type); +- ++#else ++ retval = 0; ++#endif // WASM_WASI + efree(command_line); + return retval; + } +diff --git a/ext/fileinfo/libmagic/compress.c b/ext/fileinfo/libmagic/compress.c +index 85d0b2938e4..0129c203418 100644 +--- a/ext/fileinfo/libmagic/compress.c ++++ b/ext/fileinfo/libmagic/compress.c +@@ -453,7 +453,7 @@ file_pipe2file(struct magic_set *ms, int fd, const void *startbuf, + int tfd; + + (void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf); +-#ifndef HAVE_MKSTEMP ++#if !defined(HAVE_MKSTEMP) && !defined(WASM_WASI) + { + char *ptr = mktemp(buf); + tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600); +diff --git a/ext/fileinfo/libmagic/fsmagic.c b/ext/fileinfo/libmagic/fsmagic.c +index a1b18d479f3..ff43d7bebc9 100644 +--- a/ext/fileinfo/libmagic/fsmagic.c ++++ b/ext/fileinfo/libmagic/fsmagic.c +@@ -167,6 +167,7 @@ file_fsmagic(struct magic_set *ms, const char *fn, zend_stat_t *sb) + # endif + #endif + ++#ifndef WASM_WASI + #ifdef S_IFIFO + case S_IFIFO: + if((ms->flags & MAGIC_DEVICES) != 0) +@@ -179,6 +180,7 @@ file_fsmagic(struct magic_set *ms, const char *fn, zend_stat_t *sb) + return -1; + break; + #endif ++#endif // WASM_WASI + #ifdef S_IFDOOR + case S_IFDOOR: + if (mime) { +diff --git a/ext/pdo_sqlite/sqlite_statement.c b/ext/pdo_sqlite/sqlite_statement.c +index 90de059a3b7..2e83f01af63 100644 +--- a/ext/pdo_sqlite/sqlite_statement.c ++++ b/ext/pdo_sqlite/sqlite_statement.c +@@ -32,7 +32,9 @@ static int pdo_sqlite_stmt_dtor(pdo_stmt_t *stmt) + pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data; + + if (S->stmt) { ++#ifndef WASM_WASI + sqlite3_finalize(S->stmt); ++#endif + S->stmt = NULL; + } + efree(S); +diff --git a/ext/posix/posix.c b/ext/posix/posix.c +index 788fca0b287..408c03a5a6b 100644 +--- a/ext/posix/posix.c ++++ b/ext/posix/posix.c +@@ -37,8 +37,10 @@ + #include + #include + #include ++#ifndef WASM_WASI + #include + #include ++#endif // WASM_WASI + #ifdef HAVE_SYS_MKDEV_H + # include + #endif +@@ -123,6 +125,7 @@ ZEND_GET_MODULE(posix) + + PHP_FUNCTION(posix_kill) + { ++#ifndef WASM_WASI + zend_long pid, sig; + + ZEND_PARSE_PARAMETERS_START(2, 2) +@@ -134,7 +137,7 @@ PHP_FUNCTION(posix_kill) + POSIX_G(last_error) = errno; + RETURN_FALSE; + } +- ++#endif // WASM_WASI + RETURN_TRUE; + } + /* }}} */ +@@ -142,56 +145,88 @@ PHP_FUNCTION(posix_kill) + /* {{{ Get the current process id (POSIX.1, 4.1.1) */ + PHP_FUNCTION(posix_getpid) + { ++#ifndef WASM_WASI + PHP_POSIX_RETURN_LONG_FUNC(getpid); ++#else ++ RETURN_LONG(0); ++#endif // WASM_WASI + } + /* }}} */ + + /* {{{ Get the parent process id (POSIX.1, 4.1.1) */ + PHP_FUNCTION(posix_getppid) + { ++#ifndef WASM_WASI + PHP_POSIX_RETURN_LONG_FUNC(getppid); ++#else ++ RETURN_LONG(0); ++#endif // WASM_WASI + } + /* }}} */ + + /* {{{ Get the current user id (POSIX.1, 4.2.1) */ + PHP_FUNCTION(posix_getuid) + { ++#ifndef WASM_WASI + PHP_POSIX_RETURN_LONG_FUNC(getuid); ++#else ++ RETURN_LONG(0); ++#endif // WASM_WASI + } + /* }}} */ + + /* {{{ Get the current group id (POSIX.1, 4.2.1) */ + PHP_FUNCTION(posix_getgid) + { ++#ifndef WASM_WASI + PHP_POSIX_RETURN_LONG_FUNC(getgid); ++#else ++ RETURN_LONG(0); ++#endif // WASM_WASI + } + /* }}} */ + + /* {{{ Get the current effective user id (POSIX.1, 4.2.1) */ + PHP_FUNCTION(posix_geteuid) + { ++#ifndef WASM_WASI + PHP_POSIX_RETURN_LONG_FUNC(geteuid); ++#else ++ RETURN_LONG(0); ++#endif // WASM_WASI + } + /* }}} */ + + /* {{{ Get the current effective group id (POSIX.1, 4.2.1) */ + PHP_FUNCTION(posix_getegid) + { ++#ifndef WASM_WASI + PHP_POSIX_RETURN_LONG_FUNC(getegid); ++#else ++ RETURN_LONG(0); ++#endif // WASM_WASI + } + /* }}} */ + + /* {{{ Set user id (POSIX.1, 4.2.2) */ + PHP_FUNCTION(posix_setuid) + { ++#ifndef WASM_WASI + PHP_POSIX_SINGLE_ARG_FUNC(setuid); ++#else ++ RETURN_LONG(0); ++#endif // WASM_WASI + } + /* }}} */ + + /* {{{ Set group id (POSIX.1, 4.2.2) */ + PHP_FUNCTION(posix_setgid) + { ++#ifndef WASM_WASI + PHP_POSIX_SINGLE_ARG_FUNC(setgid); ++#else ++ RETURN_LONG(0); ++#endif // WASM_WASI + } + /* }}} */ + +@@ -199,7 +234,11 @@ PHP_FUNCTION(posix_setgid) + #ifdef HAVE_SETEUID + PHP_FUNCTION(posix_seteuid) + { ++#ifndef WASM_WASI + PHP_POSIX_SINGLE_ARG_FUNC(seteuid); ++#else ++ RETURN_LONG(0); ++#endif // WASM_WASI + } + #endif + /* }}} */ +@@ -208,7 +247,11 @@ PHP_FUNCTION(posix_seteuid) + #ifdef HAVE_SETEGID + PHP_FUNCTION(posix_setegid) + { ++#ifndef WASM_WASI + PHP_POSIX_SINGLE_ARG_FUNC(setegid); ++#else ++ RETURN_LONG(0); ++#endif // WASM_WASI + } + #endif + /* }}} */ +@@ -268,7 +311,11 @@ PHP_FUNCTION(posix_getlogin) + /* {{{ Get current process group id (POSIX.1, 4.3.1) */ + PHP_FUNCTION(posix_getpgrp) + { ++#ifndef WASM_WASI + PHP_POSIX_RETURN_LONG_FUNC(getpgrp); ++#else ++ RETURN_LONG(0); ++#endif // WASM_WASI + } + /* }}} */ + +@@ -276,7 +323,11 @@ PHP_FUNCTION(posix_getpgrp) + #ifdef HAVE_SETSID + PHP_FUNCTION(posix_setsid) + { ++#ifndef WASM_WASI + PHP_POSIX_RETURN_LONG_FUNC(setsid); ++#else ++ RETURN_LONG(0); ++#endif // WASM_WASI + } + #endif + /* }}} */ +@@ -284,6 +335,7 @@ PHP_FUNCTION(posix_setsid) + /* {{{ Set process group id for job control (POSIX.1, 4.3.3) */ + PHP_FUNCTION(posix_setpgid) + { ++#ifndef WASM_WASI + zend_long pid, pgid; + + ZEND_PARSE_PARAMETERS_START(2, 2) +@@ -295,6 +347,7 @@ PHP_FUNCTION(posix_setpgid) + POSIX_G(last_error) = errno; + RETURN_FALSE; + } ++#endif // WASM_WASI + + RETURN_TRUE; + } +@@ -462,6 +515,7 @@ PHP_FUNCTION(posix_ttyname) + default: + fd = zval_get_long(z_fd); + } ++#ifndef WASM_WASI + #if defined(ZTS) && defined(HAVE_TTYNAME_R) && defined(_SC_TTY_NAME_MAX) + buflen = sysconf(_SC_TTY_NAME_MAX); + if (buflen < 1) { +@@ -481,8 +535,11 @@ PHP_FUNCTION(posix_ttyname) + POSIX_G(last_error) = errno; + RETURN_FALSE; + } +- RETURN_STRING(p); ++ RETURN_STRING(""); + #endif ++#else // WASM_WASI ++ RETURN_STRING(""); ++#endif // WASM_WASI + } + /* }}} */ + +@@ -628,6 +685,7 @@ int php_posix_group_to_array(struct group *g, zval *array_group) /* {{{ */ + zval array_members; + int count; + ++#ifndef WASM_WASI + if (NULL == g) + return 0; + +@@ -655,6 +713,9 @@ int php_posix_group_to_array(struct group *g, zval *array_group) /* {{{ */ + zend_hash_str_update(Z_ARRVAL_P(array_group), "members", sizeof("members")-1, &array_members); + add_assoc_long(array_group, "gid", g->gr_gid); + return 1; ++#else ++ return 0; ++#endif // WASM_WASI + } + /* }}} */ + +@@ -712,6 +773,7 @@ PHP_FUNCTION(posix_access) + /* {{{ Group database access (POSIX.1, 9.2.1) */ + PHP_FUNCTION(posix_getgrnam) + { ++#ifndef WASM_WASI + char *name; + struct group *g; + size_t name_len; +@@ -760,12 +822,16 @@ PHP_FUNCTION(posix_getgrnam) + #if defined(ZTS) && defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX) + efree(buf); + #endif ++#else ++ RETURN_FALSE; ++#endif // WASM_WASI + } + /* }}} */ + + /* {{{ Group database access (POSIX.1, 9.2.1) */ + PHP_FUNCTION(posix_getgrgid) + { ++#ifndef WASM_WASI + zend_long gid; + #if defined(ZTS) && defined(HAVE_GETGRGID_R) && defined(_SC_GETGR_R_SIZE_MAX) + int ret; +@@ -818,11 +884,15 @@ PHP_FUNCTION(posix_getgrgid) + #if defined(ZTS) && defined(HAVE_GETGRGID_R) && defined(_SC_GETGR_R_SIZE_MAX) + efree(grbuf); + #endif ++#else ++ RETURN_FALSE; ++#endif // WASM_WASI + } + /* }}} */ + + int php_posix_passwd_to_array(struct passwd *pw, zval *return_value) /* {{{ */ + { ++#ifndef WASM_WASI + if (NULL == pw) + return 0; + if (NULL == return_value || Z_TYPE_P(return_value) != IS_ARRAY) +@@ -836,12 +906,16 @@ int php_posix_passwd_to_array(struct passwd *pw, zval *return_value) /* {{{ */ + add_assoc_string(return_value, "dir", pw->pw_dir); + add_assoc_string(return_value, "shell", pw->pw_shell); + return 1; ++#else ++ return 0; ++#endif // WASM_WASI + } + /* }}} */ + + /* {{{ User database access (POSIX.1, 9.2.2) */ + PHP_FUNCTION(posix_getpwnam) + { ++#ifndef WASM_WASI + struct passwd *pw; + char *name; + size_t name_len; +@@ -890,12 +964,16 @@ PHP_FUNCTION(posix_getpwnam) + #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWNAM_R) + efree(buf); + #endif ++#else ++ RETURN_FALSE; ++#endif // WASM_WASI + } + /* }}} */ + + /* {{{ User database access (POSIX.1, 9.2.2) */ + PHP_FUNCTION(posix_getpwuid) + { ++#ifndef WASM_WASI + zend_long uid; + #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWUID_R) + struct passwd _pw; +@@ -946,6 +1024,9 @@ PHP_FUNCTION(posix_getpwuid) + #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWUID_R) + efree(pwbuf); + #endif ++#else ++ RETURN_FALSE; ++#endif // WASM_WASI + } + /* }}} */ + +diff --git a/ext/session/mod_files.c b/ext/session/mod_files.c +index e640f96f932..b93c47c57e9 100644 +--- a/ext/session/mod_files.c ++++ b/ext/session/mod_files.c +@@ -189,6 +189,7 @@ static void ps_files_open(ps_files *data, /* const */ zend_string *key) + #endif + + if (data->fd != -1) { ++#ifndef WASM_WASI + #ifndef PHP_WIN32 + /* check that this session file was created by us or root – we + don't want to end up accepting the sessions of another webapp +@@ -208,6 +209,7 @@ static void ps_files_open(ps_files *data, /* const */ zend_string *key) + do { + ret = flock(data->fd, LOCK_EX); + } while (ret == -1 && errno == EINTR); ++#endif // WASM_WASI + + #ifdef F_SETFD + # ifndef FD_CLOEXEC +diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c +index 6967307df45..ad88b7b3a2e 100755 +--- a/ext/standard/basic_functions.c ++++ b/ext/standard/basic_functions.c +@@ -64,11 +64,13 @@ typedef struct yy_buffer_state *YY_BUFFER_STATE; + #include + #endif + ++#ifndef WASM_WASI + #ifndef PHP_WIN32 + # include + #else + #include "win32/inet.h" + #endif ++#endif // WASM_WASI + + #ifdef HAVE_ARPA_INET_H + # include +@@ -456,9 +458,11 @@ PHP_RSHUTDOWN_FUNCTION(basic) /* {{{ */ + tsrm_env_unlock(); + #endif + ++#ifndef WASM_WASI + if (BG(umask) != -1) { + umask(BG(umask)); + } ++#endif // WASM_WASI + + /* Check if locale was changed and change it back + * to the value in startup environment */ +@@ -2411,6 +2415,7 @@ PHP_FUNCTION(move_uploaded_file) + + if (VCWD_RENAME(path, new_path) == 0) { + successful = 1; ++#ifndef WASM_WASI + #ifndef PHP_WIN32 + oldmask = umask(077); + umask(oldmask); +@@ -2421,6 +2426,7 @@ PHP_FUNCTION(move_uploaded_file) + php_error_docref(NULL, E_WARNING, "%s", strerror(errno)); + } + #endif ++#endif // WASM_WASI + } else if (php_copy_file_ex(path, new_path, STREAM_DISABLE_OPEN_BASEDIR) == SUCCESS) { + VCWD_UNLINK(path); + successful = 1; +diff --git a/ext/standard/dns.c b/ext/standard/dns.c +index d229b998a4b..104f847ab40 100644 +--- a/ext/standard/dns.c ++++ b/ext/standard/dns.c +@@ -34,7 +34,9 @@ + #ifdef HAVE_ARPA_INET_H + #include + #endif ++#ifndef WASM_WASI + #include ++#endif // WASM_WASI + #ifdef _OSD_POSIX + #undef STATUS + #undef T_UNSPEC +@@ -172,6 +174,7 @@ PHP_FUNCTION(gethostbyaddr) + /* {{{ php_gethostbyaddr */ + static zend_string *php_gethostbyaddr(char *ip) + { ++#ifndef WASM_WASI + #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON) + struct sockaddr_in sa4; + struct sockaddr_in6 sa6; +@@ -213,6 +216,9 @@ static zend_string *php_gethostbyaddr(char *ip) + + return zend_string_init(hp->h_name, strlen(hp->h_name), 0); + #endif ++#else ++ return NULL; ++#endif // WASM_WASI + } + /* }}} */ + +@@ -239,6 +245,7 @@ PHP_FUNCTION(gethostbyname) + /* {{{ Return a list of IP addresses that a given hostname resolves to. */ + PHP_FUNCTION(gethostbynamel) + { ++#ifndef WASM_WASI + char *hostname; + size_t hostname_len; + struct hostent *hp; +@@ -280,12 +287,16 @@ PHP_FUNCTION(gethostbynamel) + add_next_index_string(return_value, inet_ntoa(in)); + #endif + } ++#else ++ RETURN_FALSE; ++#endif // WASM_WASI + } + /* }}} */ + + /* {{{ php_gethostbyname */ + static zend_string *php_gethostbyname(char *name) + { ++#ifndef WASM_WASI + struct hostent *hp; + struct in_addr *h_addr_0; /* Don't call this h_addr, it's a macro! */ + struct in_addr in; +@@ -313,6 +324,9 @@ static zend_string *php_gethostbyname(char *name) + address = inet_ntoa(in); + #endif + return zend_string_init(address, strlen(address), 0); ++#else ++ return NULL; ++#endif // WASM_WASI + } + /* }}} */ + +diff --git a/ext/standard/exec.c b/ext/standard/exec.c +index 1b1b0ab9e9c..83d26d93edb 100644 +--- a/ext/standard/exec.c ++++ b/ext/standard/exec.c +@@ -113,6 +113,7 @@ static size_t handle_line(int type, zval *array, char *buf, size_t bufl) { + */ + PHPAPI int php_exec(int type, const char *cmd, zval *array, zval *return_value) + { ++#ifndef WASM_WASI + FILE *fp; + char *buf; + int pclose_return; +@@ -200,7 +201,10 @@ PHPAPI int php_exec(int type, const char *cmd, zval *array, zval *return_value) + pclose_return = -1; + RETVAL_FALSE; + goto done; +-} ++#else ++ return 0; ++#endif // WASM_WASI ++ } + /* }}} */ + + static void php_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */ +@@ -512,6 +516,7 @@ PHP_FUNCTION(escapeshellarg) + /* {{{ Execute command via shell and return complete output as string */ + PHP_FUNCTION(shell_exec) + { ++#ifndef WASM_WASI + FILE *in; + char *command; + size_t command_len; +@@ -547,7 +552,10 @@ PHP_FUNCTION(shell_exec) + if (ret && ZSTR_LEN(ret) > 0) { + RETVAL_STR(ret); + } +-} ++#else ++ RETURN_FALSE; ++#endif ++ } + /* }}} */ + + #ifdef HAVE_NICE +diff --git a/ext/standard/file.c b/ext/standard/file.c +index f40579dde59..27d212d4a45 100644 +--- a/ext/standard/file.c ++++ b/ext/standard/file.c +@@ -55,7 +55,9 @@ + # endif + # include + # include +-# include ++# ifndef WASM_WASI ++# include ++# endif // WASM_WASI + # if HAVE_ARPA_INET_H + # include + # endif +@@ -793,6 +795,7 @@ PHPAPI PHP_FUNCTION(fclose) + /* {{{ Execute a command and open either a read or a write pipe to it */ + PHP_FUNCTION(popen) + { ++#ifndef WASM_WASI + char *command, *mode; + size_t command_len, mode_len; + FILE *fp; +@@ -842,6 +845,9 @@ PHP_FUNCTION(popen) + } + + efree(posix_mode); ++#else ++ RETURN_FALSE; ++#endif // WASM_WASI + } + /* }}} */ + +@@ -1223,6 +1229,7 @@ PHP_FUNCTION(readfile) + /* {{{ Return or change the umask */ + PHP_FUNCTION(umask) + { ++#ifndef WASM_WASI + zend_long mask = 0; + bool mask_is_null = 1; + int oldumask; +@@ -1245,6 +1252,9 @@ PHP_FUNCTION(umask) + } + + RETURN_LONG(oldumask); ++#else ++ RETURN_LONG(0); ++#endif // WASM_WASI + } + /* }}} */ + +diff --git a/ext/standard/filestat.c b/ext/standard/filestat.c +index aaf7e730d0a..2fdb9d765c6 100644 +--- a/ext/standard/filestat.c ++++ b/ext/standard/filestat.c +@@ -326,12 +326,16 @@ PHPAPI int php_get_gid_by_name(const char *name, gid_t *gid) + efree(grbuf); + *gid = gr.gr_gid; + #else ++#ifndef WASM_WASI + struct group *gr = getgrnam(name); + + if (!gr) { + return FAILURE; + } + *gid = gr->gr_gid; ++#else ++ *gid = 0; ++#endif // WASM_WASI + #endif + return SUCCESS; + } +@@ -399,6 +403,7 @@ static void php_do_chgrp(INTERNAL_FUNCTION_PARAMETERS, int do_lchgrp) /* {{{ */ + RETURN_FALSE; + } + ++#ifndef WASM_WASI + if (do_lchgrp) { + #ifdef HAVE_LCHOWN + ret = VCWD_LCHOWN(filename, -1, gid); +@@ -410,6 +415,7 @@ static void php_do_chgrp(INTERNAL_FUNCTION_PARAMETERS, int do_lchgrp) /* {{{ */ + php_error_docref(NULL, E_WARNING, "%s", strerror(errno)); + RETURN_FALSE; + } ++#endif // WASM_WASI + RETURN_TRUE; + #endif + } +@@ -452,12 +458,16 @@ PHPAPI uid_t php_get_uid_by_name(const char *name, uid_t *uid) + efree(pwbuf); + *uid = pw.pw_uid; + #else ++#ifndef WASM_WASI + struct passwd *pw = getpwnam(name); + + if (!pw) { + return FAILURE; + } + *uid = pw->pw_uid; ++#else ++ *uid = 0; ++#endif // WASM_WASI + #endif + return SUCCESS; + } +@@ -465,6 +475,7 @@ PHPAPI uid_t php_get_uid_by_name(const char *name, uid_t *uid) + + static void php_do_chown(INTERNAL_FUNCTION_PARAMETERS, int do_lchown) /* {{{ */ + { ++#ifndef WASM_WASI + char *filename; + size_t filename_len; + zend_string *user_str; +@@ -539,6 +550,9 @@ static void php_do_chown(INTERNAL_FUNCTION_PARAMETERS, int do_lchown) /* {{{ */ + } + RETURN_TRUE; + #endif ++#else ++ RETURN_TRUE; ++#endif // WASM_WASI + } + /* }}} */ + +@@ -563,6 +577,7 @@ PHP_FUNCTION(lchown) + /* {{{ Change file mode */ + PHP_FUNCTION(chmod) + { ++#ifndef WASM_WASI + char *filename; + size_t filename_len; + zend_long mode; +@@ -601,6 +616,7 @@ PHP_FUNCTION(chmod) + php_error_docref(NULL, E_WARNING, "%s", strerror(errno)); + RETURN_FALSE; + } ++#endif // WASM_WASI + RETURN_TRUE; + } + /* }}} */ +@@ -855,6 +871,7 @@ PHPAPI void php_stat(zend_string *filename, int type, zval *return_value) + + stat_sb = &ssb.sb; + ++#ifndef WASM_WASI + if (type >= FS_IS_W && type <= FS_IS_X) { + if(ssb.sb.st_uid==getuid()) { + rmask=S_IRUSR; +@@ -895,6 +912,7 @@ PHPAPI void php_stat(zend_string *filename, int type, zval *return_value) + } + } + } ++#endif // WASM_WASI + + switch (type) { + case FS_PERMS: +@@ -919,7 +937,9 @@ PHPAPI void php_stat(zend_string *filename, int type, zval *return_value) + RETURN_STRING("link"); + } + switch(ssb.sb.st_mode & S_IFMT) { ++#ifndef WASM_WASI + case S_IFIFO: RETURN_STRING("fifo"); ++#endif // WASM_WASI + case S_IFCHR: RETURN_STRING("char"); + case S_IFDIR: RETURN_STRING("dir"); + case S_IFBLK: RETURN_STRING("block"); +diff --git a/ext/standard/flock_compat.c b/ext/standard/flock_compat.c +index 8622ec1aeca..7aae46c3922 100644 +--- a/ext/standard/flock_compat.c ++++ b/ext/standard/flock_compat.c +@@ -26,7 +26,12 @@ PHPAPI int flock(int fd, int operation) + #endif /* !defined(HAVE_FLOCK) */ + + PHPAPI int php_flock(int fd, int operation) +-#ifdef HAVE_STRUCT_FLOCK /* {{{ */ ++#if defined(WASM_WASI) ++{ ++ errno = 0; ++ return 0; ++} ++#elif defined(HAVE_STRUCT_FLOCK) /* {{{ */ + { + struct flock flck; + int ret; +diff --git a/ext/standard/ftp_fopen_wrapper.c b/ext/standard/ftp_fopen_wrapper.c +index 0be729a59fd..31180f329cb 100644 +--- a/ext/standard/ftp_fopen_wrapper.c ++++ b/ext/standard/ftp_fopen_wrapper.c +@@ -48,8 +48,10 @@ + #include + #else + #include ++#ifndef WASM_WASI + #include +-#ifdef HAVE_ARPA_INET_H ++#endif // WASM_WASI ++#if HAVE_ARPA_INET_H + #include + #endif + #endif +diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c +index be0ee30b7d8..2432220bcc7 100644 +--- a/ext/standard/http_fopen_wrapper.c ++++ b/ext/standard/http_fopen_wrapper.c +@@ -51,8 +51,10 @@ + #include + #else + #include ++#ifndef WASM_WASI + #include +-#ifdef HAVE_ARPA_INET_H ++#endif // WASM_WASI ++#if HAVE_ARPA_INET_H + #include + #endif + #endif +diff --git a/ext/standard/mail.c b/ext/standard/mail.c +index 0323341c3d6..fb43c8d5463 100644 +--- a/ext/standard/mail.c ++++ b/ext/standard/mail.c +@@ -374,6 +374,7 @@ static int php_mail_detect_multiple_crlf(const char *hdr) { + /* {{{ php_mail */ + PHPAPI int php_mail(const char *to, const char *subject, const char *message, const char *headers, const char *extra_cmd) + { ++#ifndef WASM_WASI + #ifdef PHP_WIN32 + int tsm_err; + char *tsm_errmsg = NULL; +@@ -555,6 +556,9 @@ PHPAPI int php_mail(const char *to, const char *subject, const char *message, co + } + + MAIL_RET(1); /* never reached */ ++#else ++ return 0; ++#endif // WASM_WASI + } + /* }}} */ + +diff --git a/ext/standard/microtime.c b/ext/standard/microtime.c +index ca8643eb519..f39e90fdc03 100644 +--- a/ext/standard/microtime.c ++++ b/ext/standard/microtime.c +@@ -126,6 +126,7 @@ PHP_FUNCTION(getrusage) + PHP_RUSAGE_PARA(ru_majflt); + PHP_RUSAGE_PARA(ru_maxrss); + #elif !defined(_OSD_POSIX) && !defined(__HAIKU__) ++#ifndef WASM_WASI + PHP_RUSAGE_PARA(ru_oublock); + PHP_RUSAGE_PARA(ru_inblock); + PHP_RUSAGE_PARA(ru_msgsnd); +@@ -139,6 +140,7 @@ PHP_FUNCTION(getrusage) + PHP_RUSAGE_PARA(ru_nvcsw); + PHP_RUSAGE_PARA(ru_nivcsw); + PHP_RUSAGE_PARA(ru_nswap); ++#endif // WASM_WASI + #endif /*_OSD_POSIX*/ + PHP_RUSAGE_PARA(ru_utime.tv_usec); + PHP_RUSAGE_PARA(ru_utime.tv_sec); +diff --git a/ext/standard/net.c b/ext/standard/net.c +index b22f304c8eb..e385b5de6b5 100644 +--- a/ext/standard/net.c ++++ b/ext/standard/net.c +@@ -43,7 +43,7 @@ + # include + # include + # include +-#else ++#elif ! defined(WASM_WASI) + # include + #endif + +@@ -86,6 +86,7 @@ PHPAPI zend_string* php_inet_ntop(const struct sockaddr *addr) { + ZEND_FALLTHROUGH; + #endif + case AF_INET: { ++#ifndef WASM_WASI + zend_string *ret = zend_string_alloc(NI_MAXHOST, 0); + if (getnameinfo(addr, addrlen, ZSTR_VAL(ret), NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == SUCCESS) { + /* Also demangle numeric host with %name suffix */ +@@ -96,6 +97,9 @@ PHPAPI zend_string* php_inet_ntop(const struct sockaddr *addr) { + } + zend_string_efree(ret); + break; ++#else ++ return NULL; ++#endif // WASM_WASI + } + } + +diff --git a/ext/standard/pageinfo.c b/ext/standard/pageinfo.c +index f24659a95e0..472f7e4a560 100644 +--- a/ext/standard/pageinfo.c ++++ b/ext/standard/pageinfo.c +@@ -61,8 +61,10 @@ PHPAPI void php_statpage(void) + BG(page_inode) = pstat->st_ino; + BG(page_mtime) = pstat->st_mtime; + } else { /* handler for situations where there is no source file, ex. php -r */ ++#ifndef WASM_WASI + BG(page_uid) = getuid(); + BG(page_gid) = getgid(); ++#endif // WASM_WASI + } + } + } +@@ -71,20 +73,29 @@ PHPAPI void php_statpage(void) + /* {{{ php_getuid */ + zend_long php_getuid(void) + { ++#ifndef WASM_WASI + php_statpage(); + return (BG(page_uid)); ++#else ++ return 0; ++#endif //WASM_WASI + } + /* }}} */ + + zend_long php_getgid(void) + { ++#ifndef WASM_WASI + php_statpage(); + return (BG(page_gid)); ++#else ++ return 0; ++#endif //WASM_WASI + } + + /* {{{ Get PHP script owner's UID */ + PHP_FUNCTION(getmyuid) + { ++#ifndef WASM_WASI + zend_long uid; + + ZEND_PARSE_PARAMETERS_NONE(); +@@ -95,6 +106,9 @@ PHP_FUNCTION(getmyuid) + } else { + RETURN_LONG(uid); + } ++#else ++ RETURN_LONG(0); ++#endif //WASM_WASI + } + /* }}} */ + +diff --git a/ext/standard/php_fopen_wrapper.c b/ext/standard/php_fopen_wrapper.c +index 8926485025a..be3fc6fd7bb 100644 +--- a/ext/standard/php_fopen_wrapper.c ++++ b/ext/standard/php_fopen_wrapper.c +@@ -246,13 +246,17 @@ php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, const char *pa + static int cli_in = 0; + fd = STDIN_FILENO; + if (cli_in) { +- fd = dup(fd); ++#ifndef WASM_WASI ++ fd = dup(fd); ++#endif // WASM_WASI + } else { + cli_in = 1; + file = stdin; + } + } else { ++#ifndef WASM_WASI + fd = dup(STDIN_FILENO); ++#endif // WASM_WASI + } + #ifdef PHP_WIN32 + pipe_requested = 1; +@@ -262,13 +266,17 @@ php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, const char *pa + static int cli_out = 0; + fd = STDOUT_FILENO; + if (cli_out++) { +- fd = dup(fd); ++#ifndef WASM_WASI ++ fd = dup(fd); ++#endif // WASM_WASI + } else { + cli_out = 1; + file = stdout; + } + } else { ++#ifndef WASM_WASI + fd = dup(STDOUT_FILENO); ++#endif // WASM_WASI + } + #ifdef PHP_WIN32 + pipe_requested = 1; +@@ -278,13 +286,17 @@ php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, const char *pa + static int cli_err = 0; + fd = STDERR_FILENO; + if (cli_err++) { ++#ifndef WASM_WASI + fd = dup(fd); ++#endif // WASM_WASI + } else { + cli_err = 1; + file = stderr; + } + } else { ++#ifndef WASM_WASI + fd = dup(STDERR_FILENO); ++#endif // WASM_WASI + } + #ifdef PHP_WIN32 + pipe_requested = 1; +@@ -317,7 +329,7 @@ php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, const char *pa + return NULL; + } + +-#ifdef HAVE_UNISTD_H ++#if defined(HAVE_UNISTD_H) && !defined(WASM_WASI) + dtablesize = getdtablesize(); + #else + dtablesize = INT_MAX; +@@ -329,6 +341,7 @@ php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, const char *pa + return NULL; + } + ++#ifndef WASM_WASI + fd = dup((int)fildes_ori); + if (fd == -1) { + php_stream_wrapper_log_error(wrapper, options, +@@ -336,6 +349,7 @@ php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, const char *pa + "[%d]: %s", fildes_ori, errno, strerror(errno)); + return NULL; + } ++#endif // WASM_WASI + } else if (!strncasecmp(path, "filter/", 7)) { + /* Save time/memory when chain isn't specified */ + if (strchr(mode, 'r') || strchr(mode, '+')) { +diff --git a/main/fastcgi.c b/main/fastcgi.c +index a77491f1bf2..ec29595e703 100644 +--- a/main/fastcgi.c ++++ b/main/fastcgi.c +@@ -69,7 +69,9 @@ static int is_impersonate = 0; + # include + # include + # include ++# ifndef WASM_WASI + # include ++# endif // WASM_WASI + # include + + # if defined(HAVE_POLL_H) && defined(HAVE_POLL) +@@ -431,6 +433,7 @@ static void fcgi_signal_handler(int signo) + + static void fcgi_setup_signals(void) + { ++#ifndef WASM_WASI + struct sigaction new_sa, old_sa; + + sigemptyset(&new_sa.sa_mask); +@@ -442,6 +445,7 @@ static void fcgi_setup_signals(void) + if (old_sa.sa_handler == SIG_DFL) { + sigaction(SIGPIPE, &new_sa, NULL); + } ++#endif // WASM_WASI + } + #endif + +@@ -526,6 +530,8 @@ int fcgi_init(void) + } else { + return is_fastcgi = 0; + } ++#elif defined(WASM_WASI) ++ return is_fastcgi = 0; + #else + errno = 0; + if (getpeername(0, (struct sockaddr *)&sa, &len) != 0 && errno == ENOTCONN) { +@@ -675,6 +681,7 @@ int fcgi_listen(const char *path, int backlog) + + /* Prepare socket address */ + if (tcp) { ++#ifndef WASM_WASI + memset(&sa.sa_inet, 0, sizeof(sa.sa_inet)); + sa.sa_inet.sin_family = AF_INET; + sa.sa_inet.sin_port = htons(port); +@@ -767,6 +774,7 @@ int fcgi_listen(const char *path, int backlog) + if (!tcp) { + chmod(path, 0777); + } else { ++#endif // WASM_WASI + char *ip = getenv("FCGI_WEB_SERVER_ADDRS"); + char *cur, *end; + int n; +@@ -1099,7 +1107,9 @@ static int fcgi_read_request(fcgi_request *req) + int on = 1; + # endif + ++# ifndef WASM_WASI + setsockopt(req->fd, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on)); ++# endif // WASM_WASI + req->nodelay = 1; + } + #endif +@@ -1409,7 +1419,9 @@ int fcgi_accept_request(fcgi_request *req) + client_sa = sa; + if (req->fd >= 0 && !fcgi_is_allowed()) { + fcgi_log(FCGI_ERROR, "Connection disallowed: IP address '%s' has been dropped.", fcgi_get_last_client_ip()); ++#ifndef WASM_WASI + closesocket(req->fd); ++#endif // WASM_WASI + req->fd = -1; + continue; + } +diff --git a/main/fopen_wrappers.c b/main/fopen_wrappers.c +index efb110171b1..fd95f5cd1a5 100644 +--- a/main/fopen_wrappers.c ++++ b/main/fopen_wrappers.c +@@ -52,7 +52,9 @@ + #include + #else + #include ++#ifndef WASM_WASI + #include ++#endif // WASM_WASI + #if HAVE_ARPA_INET_H + #include + #endif +diff --git a/main/main.c b/main/main.c +index 970d5f96c4c..7e2931dad6d 100644 +--- a/main/main.c ++++ b/main/main.c +@@ -1411,6 +1411,7 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c + /* {{{ php_get_current_user */ + PHPAPI char *php_get_current_user(void) + { ++#ifndef WASM_WASI + zend_stat_t *pstat = NULL; + + if (SG(request_info).current_user) { +@@ -1473,6 +1474,9 @@ PHPAPI char *php_get_current_user(void) + return SG(request_info).current_user; + #endif + } ++#else ++ return ""; ++#endif // WASM_WASI + } + /* }}} */ + +diff --git a/main/network.c b/main/network.c +index a189714aafb..6fb97865cb7 100644 +--- a/main/network.c ++++ b/main/network.c +@@ -54,7 +54,9 @@ + + #ifndef PHP_WIN32 + #include ++#ifndef WASM_WASI + #include ++#endif // WASM_WASI + #if HAVE_ARPA_INET_H + #include + #endif +@@ -153,6 +155,7 @@ PHPAPI void php_network_freeaddresses(struct sockaddr **sal) + */ + PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, zend_string **error_string) + { ++#ifndef WASM_WASI + struct sockaddr **sap; + int n; + #if HAVE_GETADDRINFO +@@ -275,6 +278,9 @@ PHPAPI int php_network_getaddresses(const char *host, int socktype, struct socka + + *sap = NULL; + return n; ++#else ++ return 0; ++#endif // WASM_WASI + } + /* }}} */ + +@@ -310,6 +316,7 @@ PHPAPI int php_network_connect_socket(php_socket_t sockfd, + zend_string **error_string, + int *error_code) + { ++#ifndef WASM_WASI + php_non_blocking_flags_t orig_flags; + int n; + int error = 0; +@@ -403,6 +410,9 @@ static inline void sub_times(struct timeval a, struct timeval b, struct timeval + result->tv_sec++; + result->tv_usec -= 1000000L; + } ++#else ++ return 0; ++#endif // WASM_WASI + } + /* }}} */ + +@@ -447,7 +457,11 @@ php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned po + } + + /* create a socket for this address */ ++#ifndef WASM_WASI + sock = socket(sa->sa_family, socktype, 0); ++#else ++ sock = SOCK_ERR; ++#endif // WASM_WASI + + if (sock == SOCK_ERR) { + continue; +@@ -456,31 +470,45 @@ php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned po + /* attempt to bind */ + + #ifdef SO_REUSEADDR ++#ifndef WASM_WASI + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&sockoptval, sizeof(sockoptval)); ++#endif // WASM_WASI + #endif + #ifdef IPV6_V6ONLY + if (sockopts & STREAM_SOCKOP_IPV6_V6ONLY) { + int ipv6_val = !!(sockopts & STREAM_SOCKOP_IPV6_V6ONLY_ENABLED); ++#ifndef WASM_WASI + setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&ipv6_val, sizeof(sockoptval)); ++#endif // WASM_WASI + } + #endif + #ifdef SO_REUSEPORT + if (sockopts & STREAM_SOCKOP_SO_REUSEPORT) { ++#ifndef WASM_WASI + setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (char*)&sockoptval, sizeof(sockoptval)); ++#endif // WASM_WASI + } + #endif + #ifdef SO_BROADCAST + if (sockopts & STREAM_SOCKOP_SO_BROADCAST) { ++#ifndef WASM_WASI + setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&sockoptval, sizeof(sockoptval)); ++#endif // WASM_WASI + } + #endif + #ifdef TCP_NODELAY + if (sockopts & STREAM_SOCKOP_TCP_NODELAY) { ++#ifndef WASM_WASI + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char*)&sockoptval, sizeof(sockoptval)); ++#endif // WASM_WASI + } + #endif + ++#ifndef WASM_WASI + n = bind(sock, sa, socklen); ++#else ++ n = SOCK_CONN_ERR; ++#endif // WASM_WASI + + if (n != SOCK_CONN_ERR) { + goto bound; +@@ -613,6 +641,7 @@ PHPAPI void php_network_populate_name_from_sockaddr( + socklen_t *addrlen + ) + { ++#ifndef WASM_WASI + if (addr) { + *addr = emalloc(sl); + memcpy(*addr, sa, sl); +@@ -670,6 +699,7 @@ PHPAPI void php_network_populate_name_from_sockaddr( + } + + } ++#endif // WASM_WASI + } + + PHPAPI int php_network_get_peer_name(php_socket_t sock, +@@ -678,6 +708,7 @@ PHPAPI int php_network_get_peer_name(php_socket_t sock, + socklen_t *addrlen + ) + { ++#ifndef WASM_WASI + php_sockaddr_storage sa; + socklen_t sl = sizeof(sa); + memset(&sa, 0, sizeof(sa)); +@@ -690,6 +721,9 @@ PHPAPI int php_network_get_peer_name(php_socket_t sock, + return 0; + } + return -1; ++#else ++ return 0; ++#endif // WASM_WASI + } + + PHPAPI int php_network_get_sock_name(php_socket_t sock, +@@ -698,6 +732,7 @@ PHPAPI int php_network_get_sock_name(php_socket_t sock, + socklen_t *addrlen + ) + { ++#ifndef WASM_WASI + php_sockaddr_storage sa; + socklen_t sl = sizeof(sa); + memset(&sa, 0, sizeof(sa)); +@@ -710,7 +745,9 @@ PHPAPI int php_network_get_sock_name(php_socket_t sock, + return 0; + } + return -1; +- ++#else ++ return 0; ++#endif // WASM_WASI + } + + +@@ -756,7 +793,9 @@ PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock, + ); + if (tcp_nodelay) { + #ifdef TCP_NODELAY ++#ifndef WASM_WASI + setsockopt(clisock, IPPROTO_TCP, TCP_NODELAY, (char*)&tcp_nodelay, sizeof(tcp_nodelay)); ++#endif // WASM_WASI + #endif + } + } else { +@@ -873,7 +912,9 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short + local_address_len = sizeof(struct sockaddr_in); + local_address.in4.sin_family = sa->sa_family; + local_address.in4.sin_port = htons(bindport); ++#ifndef WASM_WASI + memset(&(local_address.in4.sin_zero), 0, sizeof(local_address.in4.sin_zero)); ++#endif // WASM_WASI + } + } + #if HAVE_IPV6 && HAVE_INET_PTON +@@ -907,7 +948,9 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short + { + int val = 1; + if (sockopts & STREAM_SOCKOP_SO_BROADCAST) { ++#ifndef WASM_WASI + setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&val, sizeof(val)); ++#endif // WASM_WASI + } + } + #endif +@@ -916,7 +959,9 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short + { + int val = 1; + if (sockopts & STREAM_SOCKOP_TCP_NODELAY) { ++#ifndef WASM_WASI + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char*)&val, sizeof(val)); ++#endif // WASM_WASI + } + } + #endif +@@ -1329,6 +1374,7 @@ struct hostent * gethostname_re (const char *host,struct hostent *hostbuf,char * + #endif + + PHPAPI struct hostent* php_network_gethostbyname(const char *name) { ++#ifndef WASM_WASI + #if !defined(HAVE_GETHOSTBYNAME_R) + return gethostbyname(name); + #else +@@ -1343,4 +1389,7 @@ PHPAPI struct hostent* php_network_gethostbyname(const char *name) { + + return gethostname_re(name, &FG(tmp_host_info), &FG(tmp_host_buf), &FG(tmp_host_buf_len)); + #endif ++#else ++ return NULL; ++#endif // WASM_WASI + } +diff --git a/main/php_open_temporary_file.c b/main/php_open_temporary_file.c +index dcea7835848..57bd337835b 100644 +--- a/main/php_open_temporary_file.c ++++ b/main/php_open_temporary_file.c +@@ -30,7 +30,9 @@ + #include + #include + #include ++#ifndef WASM_WASI + #include ++#endif + #if HAVE_ARPA_INET_H + #include + #endif +@@ -97,7 +99,7 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, zend_st + char cwd[MAXPATHLEN]; + cwd_state new_state; + int fd = -1; +-#ifndef HAVE_MKSTEMP ++#if !defined(HAVE_MKSTEMP) || defined(WASM_WASI) + int open_flags = O_CREAT | O_TRUNC | O_RDWR + #ifdef PHP_WIN32 + | _O_BINARY +@@ -177,6 +179,8 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, zend_st + free(pfxw); + #elif defined(HAVE_MKSTEMP) + fd = mkstemp(opened_path); ++#elif defined(WASM_WASI) ++ fd = VCWD_OPEN(opened_path, open_flags); + #else + if (mktemp(opened_path)) { + fd = VCWD_OPEN(opened_path, open_flags); +diff --git a/main/php_syslog.c b/main/php_syslog.c +index fa71a863134..68c7aac7072 100644 +--- a/main/php_syslog.c ++++ b/main/php_syslog.c +@@ -105,6 +105,7 @@ PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */ + #else + PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */ + { ++#ifndef WASM_WASI + zend_string *fbuf = NULL; + va_list args; + +@@ -124,6 +125,7 @@ PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */ + php_syslog_str(priority, fbuf); + + zend_string_release(fbuf); ++#endif // WASM_WASI + } + /* }}} */ + #endif +diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c +index e9a30f33340..f72825e1530 100644 +--- a/main/streams/plain_wrapper.c ++++ b/main/streams/plain_wrapper.c +@@ -493,7 +493,9 @@ static int php_stdiop_close(php_stream *stream, int close_handle) + if (data->file) { + if (data->is_process_pipe) { + errno = 0; ++#ifndef WASM_WASI + ret = pclose(data->file); ++#endif + + #ifdef HAVE_SYS_WAIT_H + if (WIFEXITED(ret)) { +@@ -1304,13 +1306,18 @@ static int php_plain_files_rename(php_stream_wrapper *wrapper, const char *url_f + zend_stat_t sb; + # if !defined(ZTS) && !defined(TSRM_WIN32) + /* not sure what to do in ZTS case, umask is not thread-safe */ ++# ifndef WASM_WASI + int oldmask = umask(077); ++# else ++ int oldmask = 077; ++# endif // WASM_WASI + # endif + int success = 0; + if (php_copy_file(url_from, url_to) == SUCCESS) { + if (VCWD_STAT(url_from, &sb) == 0) { + success = 1; +-# ifndef TSRM_WIN32 ++# ifndef WASM_WASI ++# ifndef TSRM_WIN32 + /* + * Try to set user and permission info on the target. + * If we're not root, then some of these may fail. +@@ -1333,7 +1340,8 @@ static int php_plain_files_rename(php_stream_wrapper *wrapper, const char *url_f + } + } + } +-# endif ++# endif ++# endif // WASM_WASI + if (success) { + VCWD_UNLINK(url_from); + } +@@ -1344,7 +1352,9 @@ static int php_plain_files_rename(php_stream_wrapper *wrapper, const char *url_f + php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno)); + } + # if !defined(ZTS) && !defined(TSRM_WIN32) ++# ifndef WASM_WASI + umask(oldmask); ++# endif // WASM_WASI + # endif + return success; + } +@@ -1535,7 +1545,11 @@ static int php_plain_files_metadata(php_stream_wrapper *wrapper, const char *url + } else { + uid = (uid_t)*(long *)value; + } ++#ifndef WASM_WASI + ret = VCWD_CHOWN(url, uid, -1); ++#else ++ ret = 0; ++#endif // WASM_WASI + break; + case PHP_STREAM_META_GROUP: + case PHP_STREAM_META_GROUP_NAME: +@@ -1547,12 +1561,20 @@ static int php_plain_files_metadata(php_stream_wrapper *wrapper, const char *url + } else { + gid = (gid_t)*(long *)value; + } +- ret = VCWD_CHOWN(url, -1, gid); ++#ifndef WASM_WASI ++ ret = VCWD_CHvOWN(url, -1, gid); ++#else ++ ret = 0; ++#endif // WASM_WASI + break; + #endif + case PHP_STREAM_META_ACCESS: + mode = (mode_t)*(zend_long *)value; ++#ifndef WASM_WASI + ret = VCWD_CHMOD(url, mode); ++#else ++ ret = 0; ++#endif // WASM_WASI + break; + default: + zend_value_error("Unknown option %d for stream_metadata", option); +diff --git a/main/streams/xp_socket.c b/main/streams/xp_socket.c +index 3ba79df6349..5d1039b0c51 100644 +--- a/main/streams/xp_socket.c ++++ b/main/streams/xp_socket.c +@@ -219,7 +219,9 @@ static int php_sockop_close(php_stream *stream, int close_handle) + n = php_pollfd_for_ms(sock->socket, POLLOUT, 500); + } while (n == -1 && php_socket_errno() == EINTR); + #endif ++#ifndef WASM_WASI + closesocket(sock->socket); ++#endif // WASM_WASI + sock->socket = SOCK_ERR; + } + +@@ -256,8 +258,11 @@ static inline int sock_sendto(php_netstream_data_t *sock, const char *buf, size_ + { + int ret; + if (addr) { ++#ifndef WASM_WASI + ret = sendto(sock->socket, buf, XP_SOCK_BUF_SIZE(buflen), flags, addr, XP_SOCK_BUF_SIZE(addrlen)); +- ++#else ++ ret = 0; ++#endif // WASM_WASI + return (ret == SOCK_CONN_ERR) ? -1 : ret; + } + #ifdef PHP_WIN32 +@@ -278,7 +283,11 @@ static inline int sock_recvfrom(php_netstream_data_t *sock, char *buf, size_t bu + if (want_addr) { + php_sockaddr_storage sa; + socklen_t sl = sizeof(sa); ++#ifndef WASM_WASI + ret = recvfrom(sock->socket, buf, XP_SOCK_BUF_SIZE(buflen), flags, (struct sockaddr*)&sa, &sl); ++#else ++ ret = 0; ++#endif // WASM_WASI + ret = (ret == SOCK_CONN_ERR) ? -1 : ret; + #ifdef PHP_WIN32 + /* POSIX discards excess bytes without signalling failure; emulate this on Windows */ +@@ -337,6 +346,7 @@ static int php_sockop_set_option(php_stream *stream, int option, int value, void + + if (sock->socket == -1) { + alive = 0; ++#ifndef WASM_WASI + } else if ( + ( + value == 0 && +@@ -359,6 +369,7 @@ static int php_sockop_set_option(php_stream *stream, int option, int value, void + (0 > ret && err != EWOULDBLOCK && err != EAGAIN && err != EMSGSIZE)) { /* there was an unrecoverable error */ + alive = 0; + } ++#endif // WASM_WASI + } + return alive ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR; + } +@@ -387,7 +398,11 @@ static int php_sockop_set_option(php_stream *stream, int option, int value, void + + switch (xparam->op) { + case STREAM_XPORT_OP_LISTEN: ++#ifndef WASM_WASI + xparam->outputs.returncode = (listen(sock->socket, xparam->inputs.backlog) == 0) ? 0: -1; ++#else ++ xparam->outputs.returncode = 0; ++#endif // WASM_WASI + return PHP_STREAM_OPTION_RETURN_OK; + + case STREAM_XPORT_OP_GET_NAME: +@@ -409,7 +424,9 @@ static int php_sockop_set_option(php_stream *stream, int option, int value, void + case STREAM_XPORT_OP_SEND: + flags = 0; + if ((xparam->inputs.flags & STREAM_OOB) == STREAM_OOB) { ++#ifndef WASM_WASI + flags |= MSG_OOB; ++#endif // WASM_WASI + } + xparam->outputs.returncode = sock_sendto(sock, + xparam->inputs.buf, xparam->inputs.buflen, +@@ -427,10 +444,14 @@ static int php_sockop_set_option(php_stream *stream, int option, int value, void + case STREAM_XPORT_OP_RECV: + flags = 0; + if ((xparam->inputs.flags & STREAM_OOB) == STREAM_OOB) { ++#ifndef WASM_WASI + flags |= MSG_OOB; ++#endif // WASM_WASI + } + if ((xparam->inputs.flags & STREAM_PEEK) == STREAM_PEEK) { ++#ifndef WASM_WASI + flags |= MSG_PEEK; ++#endif // WASM_WASI + } + xparam->outputs.returncode = sock_recvfrom(sock, + xparam->inputs.buf, xparam->inputs.buflen, +@@ -564,6 +585,7 @@ static inline int parse_unix_address(php_stream_xport_param *xparam, struct sock + memset(unix_addr, 0, sizeof(*unix_addr)); + unix_addr->sun_family = AF_UNIX; + ++#ifndef WASM_WASI + /* we need to be binary safe on systems that support an abstract + * namespace */ + if (xparam->inputs.namelen >= sizeof(unix_addr->sun_path)) { +@@ -579,6 +601,7 @@ static inline int parse_unix_address(php_stream_xport_param *xparam, struct sock + } + + memcpy(unix_addr->sun_path, xparam->inputs.name, xparam->inputs.namelen); ++#endif // WASM_WASI + + return 1; + } +@@ -640,7 +663,9 @@ static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t * + if (stream->ops == &php_stream_unix_socket_ops || stream->ops == &php_stream_unixdg_socket_ops) { + struct sockaddr_un unix_addr; + ++#ifndef WASM_WASI + sock->socket = socket(PF_UNIX, stream->ops == &php_stream_unix_socket_ops ? SOCK_STREAM : SOCK_DGRAM, 0); ++#endif // WASM_WASI + + if (sock->socket == SOCK_ERR) { + if (xparam->want_errortext) { +@@ -653,8 +678,12 @@ static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t * + + parse_unix_address(xparam, &unix_addr); + ++#ifndef WASM_WASI + return bind(sock->socket, (const struct sockaddr *)&unix_addr, + (socklen_t) XtOffsetOf(struct sockaddr_un, sun_path) + xparam->inputs.namelen); ++#else ++ return 0; ++#endif // WASM_WASI + } + #endif + +@@ -721,7 +750,9 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_ + if (stream->ops == &php_stream_unix_socket_ops || stream->ops == &php_stream_unixdg_socket_ops) { + struct sockaddr_un unix_addr; + ++#ifndef WASM_WASI + sock->socket = socket(PF_UNIX, stream->ops == &php_stream_unix_socket_ops ? SOCK_STREAM : SOCK_DGRAM, 0); ++#endif // WASM_WASI + + if (sock->socket == SOCK_ERR) { + if (xparam->want_errortext) { +@@ -733,7 +764,11 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_ + parse_unix_address(xparam, &unix_addr); + + ret = php_network_connect_socket(sock->socket, ++#ifndef WASM_WASI + (const struct sockaddr *)&unix_addr, (socklen_t) XtOffsetOf(struct sockaddr_un, sun_path) + xparam->inputs.namelen, ++#else ++ (const struct sockaddr *)&unix_addr, xparam->inputs.namelen, ++#endif // WASM_WASI + xparam->op == STREAM_XPORT_OP_CONNECT_ASYNC, xparam->inputs.timeout, + xparam->want_errortext ? &xparam->outputs.error_text : NULL, + &err); +diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c +index b45468031fc..7d5ddf08808 100644 +--- a/sapi/cgi/cgi_main.c ++++ b/sapi/cgi/cgi_main.c +@@ -44,7 +44,9 @@ + # include + #endif + ++#ifndef WASM_WASI + #include ++#endif // WASM_WASI + + #include + +@@ -95,10 +97,12 @@ int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS; + # include "valgrind/callgrind.h" + #endif + ++#ifndef WASM_WASI + #ifndef PHP_WIN32 + /* XXX this will need to change later when threaded fastcgi is implemented. shane */ + struct sigaction act, old_term, old_quit, old_int; + #endif ++#endif // WASM_WASI + + static void (*php_php_import_environment_variables)(zval *array_ptr); + +@@ -1456,6 +1460,7 @@ static void init_request_info(fcgi_request *request) + */ + void fastcgi_cleanup(int signal) + { ++#ifndef WASM_WASI + #ifdef DEBUG_FASTCGI + fprintf(stderr, "FastCGI shutdown, pid %d\n", getpid()); + #endif +@@ -1470,6 +1475,9 @@ void fastcgi_cleanup(int signal) + } else { + exit(0); + } ++#else ++ exit(0); ++#endif // WASM_WASI + } + #else + BOOL WINAPI fastcgi_cleanup(DWORD sig) +@@ -1765,7 +1773,9 @@ int main(int argc, char *argv[]) + # endif + #endif + ++#ifndef WASM_WASI + zend_signal_startup(); ++#endif // WASM_WASI + + #ifdef ZTS + ts_allocate_id(&php_cgi_globals_id, sizeof(php_cgi_globals_struct), (ts_allocate_ctor) php_cgi_globals_ctor, NULL); +@@ -1872,6 +1882,7 @@ int main(int argc, char *argv[]) + return FAILURE; + } + ++#ifndef WASM_WASI + /* check force_cgi after startup, so we have proper output */ + if (cgi && CGIG(force_redirect)) { + /* Apache will generate REDIRECT_STATUS, +@@ -1912,6 +1923,7 @@ consult the installation file that came with this distribution, or visit \n\ + return FAILURE; + } + } ++#endif // WASM_WASI + + #ifndef HAVE_ATTRIBUTE_WEAK + fcgi_set_logger(fcgi_log); +@@ -1988,12 +2000,17 @@ consult the installation file that came with this distribution, or visit \n\ + pid_t pid; + + /* Create a process group for us & children */ ++#ifndef WASM_WASI + setsid(); + pgroup = getpgrp(); ++#else ++ pgroup = 0; ++#endif // WASM_WASI + #ifdef DEBUG_FASTCGI + fprintf(stderr, "Process group %d\n", pgroup); + #endif + ++#ifndef WASM_WASI + /* Set up handler to kill children upon exit */ + act.sa_flags = 0; + act.sa_handler = fastcgi_cleanup; +@@ -2004,6 +2021,7 @@ consult the installation file that came with this distribution, or visit \n\ + perror("Can't set signals"); + exit(1); + } ++#endif // WASM_WASI + + if (fcgi_in_shutdown()) { + goto parent_out; +@@ -2014,7 +2032,11 @@ consult the installation file that came with this distribution, or visit \n\ + #ifdef DEBUG_FASTCGI + fprintf(stderr, "Forking, %d running\n", running); + #endif ++#ifndef WASM_WASI + pid = fork(); ++#else ++ pid = 1; ++#endif // WASM_WASI + switch (pid) { + case 0: + /* One of the children. +@@ -2023,11 +2045,13 @@ consult the installation file that came with this distribution, or visit \n\ + */ + parent = 0; + ++#ifndef WASM_WASI + /* don't catch our signals */ + sigaction(SIGTERM, &old_term, 0); + sigaction(SIGQUIT, &old_quit, 0); + sigaction(SIGINT, &old_int, 0); + zend_signal_init(); ++#endif // WASM_WASI + break; + case -1: + perror("php (pre-forking)"); +@@ -2046,12 +2070,14 @@ consult the installation file that came with this distribution, or visit \n\ + #endif + parent_waiting = 1; + while (1) { ++#ifndef WASM_WASI + if (wait(&status) >= 0) { + running--; + break; + } else if (exit_signal) { + break; + } ++#endif // WASM_WASI + } + if (exit_signal) { + #if 0 +diff --git a/sapi/fpm/fpm/fpm_unix.c b/sapi/fpm/fpm/fpm_unix.c +index d10a6f3254b..bfc0b888d1c 100644 +--- a/sapi/fpm/fpm/fpm_unix.c ++++ b/sapi/fpm/fpm/fpm_unix.c +@@ -9,7 +9,9 @@ + #include + #include + #include ++#ifdef HAVE_GRP_H + #include ++#endif + + #ifdef HAVE_PRCTL + #include +diff --git a/sapi/litespeed/lsapilib.c b/sapi/litespeed/lsapilib.c +index 055677c7b85..e353ff3c4b3 100644 +--- a/sapi/litespeed/lsapilib.c ++++ b/sapi/litespeed/lsapilib.c +@@ -65,7 +65,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + #include + #include + #include ++#ifdef HAVE_GRP_H + #include ++#endif + #include + #include + #include +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0002-feat-Incapacitate-fibers-when-compiling-for-WASI.patch b/php/php-8.2.6/patches/0002-feat-Incapacitate-fibers-when-compiling-for-WASI.patch new file mode 100644 index 0000000..b3b2735 --- /dev/null +++ b/php/php-8.2.6/patches/0002-feat-Incapacitate-fibers-when-compiling-for-WASI.patch @@ -0,0 +1,89 @@ +From a467d2ed9e1c6a8092a4dab1e51f696e0de0f46a Mon Sep 17 00:00:00 2001 +From: Asen Alexandrov +Date: Tue, 3 Jan 2023 17:02:38 +0200 +Subject: [PATCH 02/18] feat: Incapacitate fibers when compiling for WASI + + 26.2% Zend/ + + 26.2% Zend/ + + 26.2% Zend/ + + 26.2% Zend/ +diff --git a/Zend/zend_fibers.c b/Zend/zend_fibers.c +index b9f9a26c6e4..a7c2dd8b45d 100644 +--- a/Zend/zend_fibers.c ++++ b/Zend/zend_fibers.c +@@ -353,12 +353,14 @@ ZEND_API bool zend_fiber_init_context(zend_fiber_context *context, void *kind, z + makecontext(handle, (void (*)(void)) zend_fiber_trampoline, 0); + + context->handle = handle; +-#else ++#elif !defined(__wasi__) + // Stack grows down, calculate the top of the stack. make_fcontext then shifts pointer to lower 16-byte boundary. + void *stack = (void *) ((uintptr_t) context->stack->pointer + context->stack->size); + + context->handle = make_fcontext(stack, context->stack->size, zend_fiber_trampoline); + ZEND_ASSERT(context->handle != NULL && "make_fcontext() never returns NULL"); ++#else ++ return false; + #endif + + context->kind = kind; +@@ -433,16 +435,18 @@ ZEND_API void zend_fiber_switch_context(zend_fiber_transfer *transfer) + + /* Copy transfer struct because it might live on the other fiber's stack that will eventually be destroyed. */ + *transfer = *transfer_data; +-#else ++#elif !defined(__wasi__) + boost_context_data data = jump_fcontext(to->handle, transfer); + + /* Copy transfer struct because it might live on the other fiber's stack that will eventually be destroyed. */ + *transfer = *data.transfer; ++#else ++ return; + #endif + + to = transfer->context; + +-#ifndef ZEND_FIBER_UCONTEXT ++#if !defined(ZEND_FIBER_UCONTEXT) && !defined(__wasi__) + /* Get the context that resumed us and update its handle to allow for symmetric coroutines. */ + to->handle = data.handle; + #endif +diff --git a/configure.ac b/configure.ac +index d0e2f225b28..c8b824c0f8b 100644 +--- a/configure.ac ++++ b/configure.ac +@@ -197,6 +197,9 @@ label2: + fi + PHP_SUBST(RE2C_FLAGS) + ++dnl Check if __wasi__ is defined by the compiler ++AC_CHECK_DECLS([__wasi__]) ++ + dnl Platform-specific compile settings. + dnl ---------------------------------------------------------------------------- + +@@ -1301,11 +1304,13 @@ else + if test "$fiber_os" = 'mac'; then + AC_DEFINE([_XOPEN_SOURCE], 1, [ ]) + fi +- AC_CHECK_HEADER(ucontext.h, [ +- AC_DEFINE([ZEND_FIBER_UCONTEXT], 1, [ ]) +- ], [ +- AC_MSG_ERROR([fibers not available on this platform]) +- ]) ++ if test "$ac_cv_have_decl___wasi__" != "yes"; then ++ AC_CHECK_HEADER(ucontext.h, [ ++ AC_DEFINE([ZEND_FIBER_UCONTEXT], 1, [ ]) ++ ], [ ++ AC_MSG_ERROR([fibers not available on this platform]) ++ ]) ++ fi + fi + + LIBZEND_BASIC_CHECKS +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0003-fix-Add-more-ifdefs-for-php-8.2.0.patch b/php/php-8.2.6/patches/0003-fix-Add-more-ifdefs-for-php-8.2.0.patch new file mode 100644 index 0000000..c84ea15 --- /dev/null +++ b/php/php-8.2.6/patches/0003-fix-Add-more-ifdefs-for-php-8.2.0.patch @@ -0,0 +1,50 @@ +From a6741ecf983131d31c3f70d5c96c3127c5af1aef Mon Sep 17 00:00:00 2001 +From: "no-reply@wasmlabs.dev" +Date: Fri, 6 Jan 2023 11:04:53 +0200 +Subject: [PATCH 03/18] fix: Add more ifdefs for php-8.2.0 + + 43.3% ext/standard/ + 56.6% main/ + + 43.3% ext/standard/ + 56.6% main/ + + 43.3% ext/standard/ + 56.6% main/ +diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h +index d7eb83b5569..ffe3f0b1ece 100644 +--- a/ext/standard/basic_functions_arginfo.h ++++ b/ext/standard/basic_functions_arginfo.h +@@ -3634,6 +3634,7 @@ static void register_basic_functions_symbols(int module_number) + REGISTER_LONG_CONSTANT("IMAGETYPE_AVIF", IMAGE_FILETYPE_AVIF, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("IMAGETYPE_UNKNOWN", IMAGE_FILETYPE_UNKNOWN, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("IMAGETYPE_COUNT", IMAGE_FILETYPE_COUNT, CONST_PERSISTENT); ++#ifndef __wasi__ + REGISTER_LONG_CONSTANT("LOG_EMERG", LOG_EMERG, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("LOG_ALERT", LOG_ALERT, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("LOG_CRIT", LOG_CRIT, CONST_PERSISTENT); +@@ -3695,6 +3696,7 @@ static void register_basic_functions_symbols(int module_number) + #if defined(LOG_PERROR) + REGISTER_LONG_CONSTANT("LOG_PERROR", LOG_PERROR, CONST_PERSISTENT); + #endif ++#endif // __wasi__ + REGISTER_LONG_CONSTANT("STR_PAD_LEFT", PHP_STR_PAD_LEFT, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("STR_PAD_RIGHT", PHP_STR_PAD_RIGHT, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("STR_PAD_BOTH", PHP_STR_PAD_BOTH, CONST_PERSISTENT); +diff --git a/main/network.c b/main/network.c +index 6fb97865cb7..683e11d03d6 100644 +--- a/main/network.c ++++ b/main/network.c +@@ -929,7 +929,9 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short + #ifdef IP_BIND_ADDRESS_NO_PORT + { + int val = 1; ++#ifndef __wasi__ + (void) setsockopt(sock, SOL_IP, IP_BIND_ADDRESS_NO_PORT, &val, sizeof(val)); ++#endif // !defined(__wasi__)o + } + #endif + if (local_address_len == 0) { +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0004-fix-Fix-memory-issues-and-introduce-WASM_WASMEDGE.patch b/php/php-8.2.6/patches/0004-fix-Fix-memory-issues-and-introduce-WASM_WASMEDGE.patch new file mode 100644 index 0000000..e456376 --- /dev/null +++ b/php/php-8.2.6/patches/0004-fix-Fix-memory-issues-and-introduce-WASM_WASMEDGE.patch @@ -0,0 +1,57 @@ +From bf357d349d3c9075c4c38f7146298cf77e91ce26 Mon Sep 17 00:00:00 2001 +From: "no-reply@wasmlabs.dev" +Date: Fri, 6 Jan 2023 11:21:03 +0200 +Subject: [PATCH 04/18] fix: Fix memory issues and introduce WASM_WASMEDGE. + + 76.4% Zend/ + 11.4% ext/posix/ + 12.1% ext/standard/ + + 76.4% Zend/ + 11.4% ext/posix/ + 12.1% ext/standard/ + + 86.2% Zend/ + 13.7% ext/standard/ +diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c +index 3f71eb22a32..c0184439fa4 100644 +--- a/Zend/zend_alloc.c ++++ b/Zend/zend_alloc.c +@@ -513,8 +513,8 @@ static void *zend_mm_mmap(size_t size) + } + return ptr; + #elif ! HAVE_MMAP +- void* ptr = malloc(size); +- memset(ptr, 0, size); ++ void* ptr = malloc(size + ZEND_MMAP_AHEAD); ++ memset(ptr, 0, size + ZEND_MMAP_AHEAD); + return ptr; + #else + void *ptr; +@@ -726,8 +726,8 @@ static zend_always_inline void zend_mm_hugepage(void* ptr, size_t size) + static void *zend_mm_chunk_alloc_int(size_t size, size_t alignment) + { + #if ! HAVE_MMAP +- void* ptr = aligned_alloc(alignment, size); +- memset(ptr, 0, size); ++ void* ptr = aligned_alloc(alignment, size + ZEND_MMAP_AHEAD); ++ memset(ptr, 0, size + ZEND_MMAP_AHEAD); + return ptr; + #else + void *ptr = zend_mm_mmap(size); +diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c +index ad88b7b3a2e..82409e2377a 100755 +--- a/ext/standard/basic_functions.c ++++ b/ext/standard/basic_functions.c +@@ -70,6 +70,8 @@ typedef struct yy_buffer_state *YY_BUFFER_STATE; + #else + #include "win32/inet.h" + #endif ++#elif defined(WASM_WASMEDGE) ++# include + #endif // WASM_WASI + + #ifdef HAVE_ARPA_INET_H +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0005-feat-Server-sockets-via-with-wasm-runtime-wasmedge.patch b/php/php-8.2.6/patches/0005-feat-Server-sockets-via-with-wasm-runtime-wasmedge.patch new file mode 100644 index 0000000..5f64345 --- /dev/null +++ b/php/php-8.2.6/patches/0005-feat-Server-sockets-via-with-wasm-runtime-wasmedge.patch @@ -0,0 +1,1096 @@ +From a0fd4d629e2b572cddb63016b9065dd25f0247ae Mon Sep 17 00:00:00 2001 +From: "no-reply@wasmlabs.dev" +Date: Fri, 6 Jan 2023 12:38:11 +0200 +Subject: [PATCH 05/18] feat: Server sockets via --with-wasm-runtime=wasmedge + + 8.3% sapi/cli/ + 88.4% wasmedge_stubs/ + + 8.3% sapi/cli/ + 88.4% wasmedge_stubs/ + + 8.3% sapi/cli/ + 88.4% wasmedge_stubs/ +diff --git a/configure.ac b/configure.ac +index c8b824c0f8b..8df252fe620 100644 +--- a/configure.ac ++++ b/configure.ac +@@ -788,6 +788,28 @@ fi + dnl Check for openpty. It may require linking against libutil or libbsd. + PHP_CHECK_FUNC(openpty, util, bsd) + ++dnl Check wasm runtime. ++PHP_ARG_WITH([wasm-runtime], ++ [whether to build for a specific wasm runtime], ++ [AS_HELP_STRING([--with-wasm-runtime=RUNTIME], ++ [Build with support for specific runtime. Can be either 'default' or 'wasmedge' [default]])], ++ [default], ++ [no]) ++ ++if test "$PHP_WASM_RUNTIME" != "default"; then ++ case $host_alias in ++ *wasm*) ++ if test "$PHP_WASM_RUNTIME" = "wasmedge"; then ++ PHP_ADD_SOURCES(wasmedge_stubs, wasi_socket_ext.c, -DWASM_RUNTIME_WASMEDGE) ++ CFLAGS="$CFLAGS -DWASM_RUNTIME_WASMEDGE" ++ fi ++ ;; ++ *) ++ AC_MSG_ERROR([--with-wasm-runtime has meaning only for wasm builds!]) ++ ;; ++ esac ++fi ++ + dnl General settings. + dnl ---------------------------------------------------------------------------- + PHP_CONFIGURE_PART(General settings) +diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c +index 82409e2377a..40ab5286b47 100755 +--- a/ext/standard/basic_functions.c ++++ b/ext/standard/basic_functions.c +@@ -70,8 +70,8 @@ typedef struct yy_buffer_state *YY_BUFFER_STATE; + #else + #include "win32/inet.h" + #endif +-#elif defined(WASM_WASMEDGE) +-# include ++#elif defined(WASM_RUNTIME_WASMEDGE) ++# include "wasmedge_stubs/netdb.h" + #endif // WASM_WASI + + #ifdef HAVE_ARPA_INET_H +diff --git a/main/php_network.h b/main/php_network.h +index a3b7ba7ab31..8ca945f9783 100644 +--- a/main/php_network.h ++++ b/main/php_network.h +@@ -230,7 +230,7 @@ static inline bool _php_check_fd_setsize(php_socket_t *max_fd, int setsize) + return true; + } + +-#ifdef PHP_WIN32 ++#if defined(PHP_WIN32) || defined(__wasi__) + /* it is safe to FD_SET too many fd's under win32; the macro will simply ignore + * descriptors that go beyond the default FD_SETSIZE */ + # define PHP_SAFE_FD_SET(fd, set) FD_SET(fd, set) +diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c +index ce3ff5de6cb..ffbe2f71854 100644 +--- a/sapi/cli/php_cli_server.c ++++ b/sapi/cli/php_cli_server.c +@@ -97,6 +97,19 @@ + #include "php_cli_process_title.h" + #include "php_cli_process_title_arginfo.h" + ++#ifdef WASM_RUNTIME_WASMEDGE ++#include "wasmedge_stubs/netdb.h" ++#include "wasmedge_stubs/wasi_socket_ext.h" ++#endif ++ ++// #define WASMEDGE_SOCKET_DEBUG ++ ++#ifdef WASMEDGE_SOCKET_DEBUG ++#define WSEDEBUG(fmt, ...) fprintf(stderr, fmt __VA_OPT__(,) __VA_ARGS__) ++#else ++#define WSEDEBUG(fmt, ...) ++#endif ++ + #define OUTPUT_NOT_CHECKED -1 + #define OUTPUT_IS_TTY 1 + #define OUTPUT_NOT_TTY 0 +@@ -759,7 +772,7 @@ static void sapi_cli_server_log_write(int type, const char *msg) /* {{{ */ + memmove(buf, "unknown", sizeof("unknown")); + } + } +-#ifdef HAVE_FORK ++#if HAVE_FORK + if (php_cli_server_workers_max > 1) { + fprintf(stderr, "[%ld] [%s] %s\n", (long) getpid(), buf, msg); + } else { +@@ -838,6 +851,24 @@ static void php_cli_server_poller_remove(php_cli_server_poller *poller, int mode + } + #ifndef PHP_WIN32 + if (fd == poller->max_fd) { ++#ifdef WASM_RUNTIME_WASMEDGE ++ php_socket_t new_max_fd = 0; ++ ++ for (int i=0; i< poller->rfds.__nfds; ++i) ++ { ++ int candidate = poller->rfds.__fds[i]; ++ if(candidate > new_max_fd) ++ new_max_fd = candidate; ++ } ++ ++ for (int i=0; i< poller->wfds.__nfds; ++i) ++ { ++ int candidate = poller->wfds.__fds[i]; ++ if(candidate > new_max_fd) ++ new_max_fd = candidate; ++ } ++ poller->max_fd = new_max_fd; ++#else + while (fd > 0) { + fd--; + if (PHP_SAFE_FD_ISSET(fd, &poller->rfds) || PHP_SAFE_FD_ISSET(fd, &poller->wfds)) { +@@ -845,6 +876,7 @@ static void php_cli_server_poller_remove(php_cli_server_poller *poller, int mode + } + } + poller->max_fd = fd; ++#endif + } + #endif + } /* }}} */ +@@ -904,6 +936,27 @@ static zend_result php_cli_server_poller_iter_on_active(php_cli_server_poller *p + php_socket_t fd; + const php_socket_t max_fd = poller->max_fd; + ++#ifdef WASM_RUNTIME_WASMEDGE ++ // Note this does not keep the order in terms of fd number, when there are both r and w ++ ++ for (int i = 0; i < poller->active.rfds.__nfds; ++i) ++ { ++ fd = poller->active.rfds.__fds[i]; ++ if (SUCCESS != callback(opaque, fd, POLLIN)) ++ { ++ retval = FAILURE; ++ } ++ } ++ ++ for (int i = 0; i < poller->active.wfds.__nfds; ++i) ++ { ++ fd = poller->active.wfds.__fds[i]; ++ if (SUCCESS != callback(opaque, fd, POLLOUT)) ++ { ++ retval = FAILURE; ++ } ++ } ++#else + for (fd=0 ; fd<=max_fd ; fd++) { + if (PHP_SAFE_FD_ISSET(fd, &poller->active.rfds)) { + if (SUCCESS != callback(opaque, fd, POLLIN)) { +@@ -916,6 +969,7 @@ static zend_result php_cli_server_poller_iter_on_active(php_cli_server_poller *p + } + } + } ++#endif // WASM_RUNTIME_WASMEDGE + #endif + return retval; + } /* }}} */ +@@ -1265,7 +1319,21 @@ static php_socket_t php_network_listen_socket(const char *host, int *port, int s + + int num_addrs = php_network_getaddresses(host, socktype, &sal, errstr); + if (num_addrs == 0) { ++#ifdef WASM_RUNTIME_WASMEDGE ++ // This is attempt to copy-paste and initialize the addresses based on the code in php_network_getaddresses, ++ // but I failed to get it right, so it is abandoned (see the commented out free calls at the end of the method) ++ *sal = safe_emalloc(2, sizeof(*sal), 0); ++ struct sockaddr **sap; ++ sap = *sal; ++ *sap = emalloc(sizeof(struct sockaddr_in)); ++ (*sap)->sa_family = AF_INET; ++ ((struct sockaddr_in *)*sap)->sin_addr.s_addr = INADDR_ANY; ++ ((struct sockaddr_in *)*sap)->sin_port = *port; ++ ++sap; ++ *sap = NULL; ++#else + return -1; ++#endif + } + for (p = sal; *p; p++) { + if (sa) { +@@ -1273,11 +1341,19 @@ static php_socket_t php_network_listen_socket(const char *host, int *port, int s + sa = NULL; + } + ++#ifdef WASM_RUNTIME_WASMEDGE ++ (*p)->sa_family = AF_INET; ++#endif ++ + retval = socket((*p)->sa_family, socktype, 0); + if (retval == SOCK_ERR) { + continue; + } + ++#ifdef WASM_RUNTIME_WASMEDGE ++ (*p)->sa_family = AF_INET; ++#endif ++ + switch ((*p)->sa_family) { + #if HAVE_GETADDRINFO && HAVE_IPV6 + case AF_INET6: +@@ -1289,8 +1365,15 @@ static php_socket_t php_network_listen_socket(const char *host, int *port, int s + #endif + case AF_INET: + sa = pemalloc(sizeof(struct sockaddr_in), 1); ++#ifdef WASM_RUNTIME_WASMEDGE ++ memset(sa, 0, sizeof(struct sockaddr_in)); ++ ((struct sockaddr_in *)sa)->sin_family = AF_INET; ++ ((struct sockaddr_in *)sa)->sin_addr.s_addr = INADDR_ANY; ++ ((struct sockaddr_in *)sa)->sin_port = *port; ++#else + *(struct sockaddr_in *)sa = *(struct sockaddr_in *)*p; + ((struct sockaddr_in *)sa)->sin_port = htons(*port); ++#endif + *socklen = sizeof(struct sockaddr_in); + break; + default: +@@ -1320,10 +1403,12 @@ static php_socket_t php_network_listen_socket(const char *host, int *port, int s + + *af = sa->sa_family; + if (*port == 0) { ++#ifndef WASM_RUNTIME_WASMEDGE + if (getsockname(retval, sa, socklen)) { + err = php_socket_errno(); + goto out; + } ++#endif + switch (sa->sa_family) { + #if HAVE_GETADDRINFO && HAVE_IPV6 + case AF_INET6: +@@ -1349,12 +1434,14 @@ static php_socket_t php_network_listen_socket(const char *host, int *port, int s + } + + out: ++#ifndef WASM_RUNTIME_WASMEDGE + if (sa) { + pefree(sa, 1); + } + if (sal) { + php_network_freeaddresses(sal); + } ++#endif + if (err) { + if (ZEND_VALID_SOCKET(retval)) { + closesocket(retval); +diff --git a/wasmedge_stubs/netdb.h b/wasmedge_stubs/netdb.h +new file mode 100644 +index 00000000000..c1136150b7b +--- /dev/null ++++ b/wasmedge_stubs/netdb.h +@@ -0,0 +1,73 @@ ++#pragma once ++// Based on https://github.com/hangedfish/wasmedge_wasi_socket_c ++ ++struct addrinfo { ++ int ai_flags; ++ int ai_family; ++ int ai_socktype; ++ int ai_protocol; ++ socklen_t ai_addrlen; ++ struct sockaddr *ai_addr; ++ char *ai_canonname; ++ int ai_canonnamelen; ++ struct addrinfo *ai_next; ++}; ++ ++#define AI_PASSIVE 0x00 ++#define AI_CANONNAME 0x01 ++#define AI_NUMERICHOST 0x02 ++#define AI_NUMERICSERV 0x03 ++#define AI_V4MAPPED 0x04 ++#define AI_ALL 0x05 ++#define AI_ADDRCONFIG 0x06 ++ ++ ++#define NI_NUMERICHOST 0x01 ++#define NI_NUMERICSERV 0x02 ++#define NI_NOFQDN 0x04 ++#define NI_NAMEREQD 0x08 ++#define NI_DGRAM 0x10 ++#define NI_NUMERICSCOPE 0x100 ++ ++#define EAI_BADFLAGS -1 ++#define EAI_NONAME -2 ++#define EAI_AGAIN -3 ++#define EAI_FAIL -4 ++#define EAI_FAMILY -6 ++#define EAI_SOCKTYPE -7 ++#define EAI_SERVICE -8 ++#define EAI_MEMORY -10 ++#define EAI_SYSTEM -11 ++#define EAI_OVERFLOW -12 ++ ++#define EAI_NODATA -5 ++#define EAI_ADDRFAMILY -9 ++#define EAI_INPROGRESS -100 ++#define EAI_CANCELED -101 ++#define EAI_NOTCANCELED -102 ++#define EAI_ALLDONE -103 ++#define EAI_INTR -104 ++#define EAI_IDN_ENCODE -105 ++#define NI_MAXHOST 255 ++#define NI_MAXSERV 32 ++ ++struct servent { ++ char *s_name; ++ char **s_aliases; ++ int s_port; ++ char *s_proto; ++}; ++ ++#ifdef __cplusplus ++extern "C" { ++#endif ++ ++struct servent *getservbyname (const char *, const char *); ++ ++int getaddrinfo (const char *__restrict, const char *__restrict, const struct addrinfo *__restrict, struct addrinfo **__restrict); ++void freeaddrinfo (struct addrinfo *); ++// int getnameinfo (const struct sockaddr *__restrict, socklen_t, char *__restrict, socklen_t, char *__restrict, socklen_t, int); ++ ++#ifdef __cplusplus ++} ++#endif +\ No newline at end of file +diff --git a/wasmedge_stubs/wasi_socket_ext.c b/wasmedge_stubs/wasi_socket_ext.c +new file mode 100644 +index 00000000000..0c74c75a5ec +--- /dev/null ++++ b/wasmedge_stubs/wasi_socket_ext.c +@@ -0,0 +1,406 @@ ++// Based on https://github.com/hangedfish/wasmedge_wasi_socket_c ++ ++#include "wasi_socket_ext.h" ++#include "netdb.h" ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++// #define WASMEDGE_SOCKET_DEBUG ++ ++#ifdef WASMEDGE_SOCKET_DEBUG ++#define WSEDEBUG(fmt, ...) fprintf(stderr, fmt __VA_OPT__(,) __VA_ARGS__) ++#else ++#define WSEDEBUG(fmt, ...) ++#endif ++ ++// WasmEdge Socket API ++ ++#define kUnspec 0 ++#define kInet4 1 ++#define kInet6 2 ++ ++typedef uint8_t address_family_t; ++ ++#define kAny 0 ++#define kDatagram 1 ++#define kStream 2 ++ ++typedef uint8_t socket_type_t; ++ ++#define kIPProtoIP 0 ++#define kIPProtoTCP 1 ++#define kIPProtoUDP 2 ++ ++typedef uint32_t ai_protocol_t; ++ ++#define kAiPassive 0 ++#define kAiCanonname 1 ++#define kAiNumericHost 2 ++#define kAiNumericServ = 4 ++#define kAiV4Mapped = 8 ++#define kAiAll = 16 ++#define kAiAddrConfig = 32 ++ ++typedef uint16_t ai_flags_t; ++ ++typedef struct wasi_address { ++ uint8_t *buf; ++ uint32_t size; ++} wasi_address_t; ++ ++typedef struct iovec_read { ++ uint8_t *buf; ++ uint32_t size; ++} iovec_read_t; ++ ++typedef struct iovec_write { ++ uint8_t *buf; ++ uint32_t size; ++} iovec_write_t; ++ ++typedef struct wasi_sockaddr { ++ address_family_t family; ++ uint32_t sa_data_len; ++ uint8_t *sa_data; ++} wasi_sockaddr_t; ++ ++typedef struct wasi_canonname_buff { ++ char name[30]; ++} wasi_canonname_buff_t; ++ ++#pragma pack(push, 1) ++typedef struct wasi_addrinfo { ++ ai_flags_t ai_flags; ++ address_family_t ai_family; ++ socket_type_t ai_socktype; ++ ai_protocol_t ai_protocol; ++ uint32_t ai_addrlen; ++ wasi_sockaddr_t *ai_addr; ++ char *ai_canonname; ++ uint32_t ai_canonnamelen; ++ struct wasi_addrinfo *ai_next; ++} wasi_addrinfo_t; ++#pragma pack(pop) ++ ++typedef struct sockaddr_generic { ++ union sa { ++ struct sockaddr_in sin; ++ struct sockaddr_in6 sin6; ++ } sa; ++ ++} sa_t; ++ ++#define MAX_ADDRINFO_RES_LEN 10 ++ ++int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_open( ++ uint8_t addr_family, uint8_t sock_type, int32_t *fd) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_open"))); ++ ++int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_bind( ++ uint32_t fd, wasi_address_t *addr, uint32_t port) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_bind"))); ++ ++uint32_t ++__imported_wasmedge_wasi_snapshot_preview1_sock_listen(uint32_t fd, ++ uint32_t backlog) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_listen"))); ++ ++int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_accept(uint32_t fd, ++ uint32_t *fd2) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_accept"))); ++ ++int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_connect( ++ uint32_t fd, wasi_address_t *addr, uint32_t port) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_connect"))); ++ ++int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_recv( ++ uint32_t fd, iovec_read_t *buf, uint32_t buf_len, uint16_t flags, ++ uint32_t *recv_len, uint32_t *oflags) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_recv"))); ++ ++int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_recv_from( ++ uint32_t fd, iovec_read_t *buf, uint32_t buf_len, uint8_t *addr, ++ uint32_t *addr_len, uint16_t flags) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_recv_from"))); ++ ++int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_send(uint32_t fd, ++ iovec_write_t buf, ++ uint32_t buf_len, ++ uint16_t flags, ++ uint32_t *send_len) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_send"))); ++ ++int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_send_to( ++ uint32_t fd, uint8_t *buf, uint32_t buf_len, uint8_t *addr, ++ uint32_t addr_len, uint16_t flags) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_send_to"))); ++ ++int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_shutdown(uint32_t fd, ++ uint8_t flags) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_shutdown"))); ++ ++int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_getaddrinfo( ++ uint8_t *node, uint32_t node_len, uint8_t *server, uint32_t server_len, ++ wasi_addrinfo_t *hint, uint32_t *res, uint32_t max_len, uint32_t *res_len) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_getaddrinfo"))); ++ ++int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_getpeeraddr( ++ uint32_t fd, wasi_address_t *addr, uint32_t *addr_type, uint32_t *port) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_getpeeraddr"))); ++ ++int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_getlocaladdr( ++ uint32_t fd, wasi_address_t *addr, uint32_t *addr_type, uint32_t *port) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_getlocaladdr"))); ++ ++int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_getsockopt( ++ uint32_t fd, int32_t level, int32_t name, int32_t *flag, ++ uint32_t *flag_size) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_getsockopt"))); ++ ++int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_setsockopt( ++ uint32_t fd, int32_t level, int32_t name, int32_t *flag, ++ uint32_t *flag_size) ++ __attribute__((__import_module__("wasi_snapshot_preview1"), ++ __import_name__("sock_getsockopt"))); ++ ++int socket(int domain, int type, int protocol) ++{ ++ int fd; ++ address_family_t af = (domain == AF_INET ? kInet4 : kInet6); ++ socket_type_t st = (type == SOCK_STREAM ? kStream : kDatagram); ++ int res = __imported_wasmedge_wasi_snapshot_preview1_sock_open((int8_t)af, (int8_t)st, &fd); ++ if (0 != res) { ++ errno = res; ++ printf("socket err: %s \n", strerror(errno)); ++ return -1; ++ } ++ WSEDEBUG("WWSock| socket returning: %d \n", fd); ++ return fd; ++} ++ ++int bind(int fd, const struct sockaddr *addr, socklen_t len) ++{ ++ WSEDEBUG("WWSock| bind[%d]: calling bind on sa_data=[", __LINE__); ++ for (int i = 0; i < len; ++i) ++ WSEDEBUG("'%d', ", (short)addr->sa_data[i]); ++ WSEDEBUG("]\n"); ++ ++ wasi_address_t wasi_addr; ++ memset(&wasi_addr, 0, sizeof(wasi_address_t)); ++ uint32_t port; ++ if (AF_INET == addr->sa_family) { ++ struct sockaddr_in *sin = (struct sockaddr_in *)addr; ++ wasi_addr.buf = (uint8_t *)&sin->sin_addr; ++ wasi_addr.size = 4; ++ port = sin->sin_port; ++ } else if (AF_INET6 == addr->sa_family) { ++ struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr; ++ wasi_addr.buf = (uint8_t *)&sin->sin6_addr; ++ wasi_addr.size = 16; ++ port = sin->sin6_port; ++ } ++ ++ WSEDEBUG("WWSock| bind[%d]: __imported_wasmedge_wasi_snapshot_preview1_sock_bind\n", __LINE__); ++ int res = ++ __imported_wasmedge_wasi_snapshot_preview1_sock_bind(fd, &wasi_addr, port); ++ WSEDEBUG("WWSock| bind[%d]: res=%d\n", __LINE__, res); ++ if (res != 0) ++ { ++ errno = res; ++ return -1; ++ } ++ return res; ++} ++ ++int connect(int fd, const struct sockaddr *addr, socklen_t len) { ++ WSEDEBUG("WWSock| connect[%d]: fd=%d, addr=%d, port=%d \n", __LINE__, ++ fd, ((struct sockaddr_in *)addr)->sin_addr.s_addr, ((struct sockaddr_in *)addr)->sin_port); ++ wasi_address_t wasi_addr; ++ memset(&wasi_addr, 0, sizeof(wasi_address_t)); ++ uint32_t port; ++ if (AF_INET == addr->sa_family) { ++ struct sockaddr_in *sin = (struct sockaddr_in *)addr; ++ wasi_addr.buf = (uint8_t *)&sin->sin_addr; ++ wasi_addr.size = 4; ++ port = ntohs(sin->sin_port); ++ } else if (AF_INET6 == addr->sa_family) { ++ struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr; ++ wasi_addr.buf = (uint8_t *)&sin->sin6_addr; ++ wasi_addr.size = 16; ++ port = ntohs(sin->sin6_port); ++ } ++ int res = __imported_wasmedge_wasi_snapshot_preview1_sock_connect( ++ fd, &wasi_addr, port); ++ if (res != 0) { ++ errno = res; ++ return -1; ++ } ++ return res; ++} ++ ++int listen(int fd, int backlog) { ++ WSEDEBUG("WWSock| __imported_wasmedge_wasi_snapshot_preview1_sock_listen[%d]: \n", __LINE__); ++ int res = __imported_wasmedge_wasi_snapshot_preview1_sock_listen(fd, backlog); ++ WSEDEBUG("WWSock| listen[%d]: res=%d\n", __LINE__, res); ++ return res; ++} ++ ++int accept(int fd, struct sockaddr *restrict addr, socklen_t *restrict len) { ++ WSEDEBUG("WWSock| accept[%d]: fd=%d\n", __LINE__, fd); ++ int new_sockfd; ++ int res = __imported_wasmedge_wasi_snapshot_preview1_sock_accept( ++ fd, (uint32_t *)&new_sockfd); ++ if (res != 0) { ++ errno = res; ++ return -1; ++ } ++ return new_sockfd; ++} ++ ++int setsockopt(int fd, int level, int optname, const void *optval, ++ socklen_t optlen) { ++ WSEDEBUG("WWSock| setsockopt[%d]: fd=%d\n", __LINE__, fd); ++ int res = __imported_wasmedge_wasi_snapshot_preview1_sock_setsockopt( ++ fd, level, optname, (int32_t *)optval, (uint32_t *)&optlen); ++ if (res != 0) { ++ errno = res; ++ return -1; ++ } ++ return 0; ++} ++ ++struct servent *getservbyname(const char *name, const char *prots) ++{ ++ WSEDEBUG("WWSock| getservbyname[%d]: name=%s\n", __LINE__, name); ++ return NULL; ++} ++ ++static struct addrinfo * ++convert_wasi_addrinfo_to_addrinfo(wasi_addrinfo_t *wasi_addrinfo, ++ uint32_t size) { ++ WSEDEBUG("WWSock| convert_wasi_addrinfo_to_addrinfo[%d]: \n", __LINE__); ++ ++ struct addrinfo *addrinfo_arr = (struct addrinfo *)calloc( ++ (sizeof(struct addrinfo) + sizeof(struct sockaddr_generic)) * size + 30, ++ 1); ++ struct sockaddr_generic *sockaddr_generic_arr = ++ (struct sockaddr_generic *)&addrinfo_arr[size]; ++ char *ai_canonname = (char *)&sockaddr_generic_arr[size]; ++ int ai_canonnamelen = addrinfo_arr[0].ai_canonnamelen; ++ memcpy(ai_canonname, addrinfo_arr[0].ai_canonname, ai_canonnamelen); ++ ++ for (size_t i = 0; i < size; i++) { ++ addrinfo_arr[i] = (struct addrinfo){ ++ .ai_flags = (int)wasi_addrinfo[i].ai_flags, ++ .ai_family = wasi_addrinfo[i].ai_family == kInet4 ? AF_INET : AF_INET6, ++ .ai_socktype = ++ wasi_addrinfo[i].ai_socktype == kStream ? SOCK_STREAM : SOCK_DGRAM, ++ .ai_protocol = wasi_addrinfo[i].ai_protocol == kIPProtoTCP ++ ? IPPROTO_TCP ++ : IPPROTO_UDP, ++ .ai_addrlen = 0, ++ .ai_addr = (struct sockaddr *)&sockaddr_generic_arr[i], ++ .ai_canonname = ai_canonname, ++ .ai_canonnamelen = ai_canonnamelen, ++ .ai_next = NULL, ++ }; ++ if (wasi_addrinfo[i].ai_addr != NULL) { ++ if (wasi_addrinfo[i].ai_addr->family == kInet4) { ++ // IPv4 ++ wasi_addrinfo[i].ai_addrlen = sizeof(struct sockaddr_in); ++ sockaddr_generic_arr[i].sa.sin.sin_family = AF_INET; ++ sockaddr_generic_arr[i].sa.sin.sin_port = ++ *(uint16_t *)&wasi_addrinfo[i].ai_addr->sa_data[0]; ++ sockaddr_generic_arr[i].sa.sin.sin_addr.s_addr = ++ *(in_addr_t *)&wasi_addrinfo[i].ai_addr->sa_data[2]; ++ } else { ++ // IPv6 ++ wasi_addrinfo[i].ai_addrlen = sizeof(struct sockaddr_in6); ++ sockaddr_generic_arr[i].sa.sin6.sin6_family = AF_INET6; ++ sockaddr_generic_arr[i].sa.sin6.sin6_port = ++ *(uint16_t *)&wasi_addrinfo[i].ai_addr->sa_data[0]; ++ // WasmEdge rust socket api not support IPv6 addrinfo. ++ WSEDEBUG("Not support IPv6 addrinfo."); ++ abort(); ++ } ++ } ++ if (i > 0) { ++ addrinfo_arr[i - 1].ai_next = &addrinfo_arr[i]; ++ } ++ } ++ return addrinfo_arr; ++} ++ ++int getaddrinfo(const char *restrict host, const char *restrict serv, ++ const struct addrinfo *restrict hint, ++ struct addrinfo **restrict res) { ++ WSEDEBUG("WWSock| getaddrinfo[%d]: \n", __LINE__); ++ uint32_t res_len = 0; ++ uint8_t *sockbuff = (uint8_t *)calloc(26 * MAX_ADDRINFO_RES_LEN, 1); ++ wasi_sockaddr_t *sockaddr_arr = ++ (wasi_sockaddr_t *)calloc(sizeof(wasi_sockaddr_t) * MAX_ADDRINFO_RES_LEN + ++ sizeof(wasi_canonname_buff_t), ++ 1); ++ wasi_addrinfo_t *addrinfo_arr = (wasi_addrinfo_t *)calloc( ++ sizeof(wasi_addrinfo_t) * MAX_ADDRINFO_RES_LEN, 1); ++ for (size_t i = 0; i < MAX_ADDRINFO_RES_LEN; i++) { ++ sockaddr_arr[i].sa_data = &sockbuff[i]; ++ addrinfo_arr[i].ai_addr = &sockaddr_arr[i]; ++ addrinfo_arr[i].ai_canonname = (char *)&addrinfo_arr[MAX_ADDRINFO_RES_LEN]; ++ if (i > 0) { ++ addrinfo_arr[i - 1].ai_next = &addrinfo_arr[i]; ++ } ++ } ++ wasi_addrinfo_t wasi_hint = (wasi_addrinfo_t){ ++ .ai_flags = (ai_flags_t)hint->ai_flags, ++ .ai_family = hint->ai_family == AF_INET6 ? kInet6 : kInet4, ++ .ai_socktype = hint->ai_socktype == SOCK_DGRAM ? kDatagram : kStream, ++ .ai_protocol = ++ hint->ai_protocol == IPPROTO_UDP ? kIPProtoUDP : kIPProtoTCP, ++ .ai_addrlen = 0, ++ .ai_addr = NULL, ++ .ai_canonname = NULL, ++ .ai_canonnamelen = 0, ++ .ai_next = NULL, ++ }; ++ ++ int rc = __imported_wasmedge_wasi_snapshot_preview1_sock_getaddrinfo( ++ (uint8_t *)host, strlen(host), (uint8_t *)serv, strlen(serv), &wasi_hint, ++ (uint32_t *)&addrinfo_arr, MAX_ADDRINFO_RES_LEN, &res_len); ++ if (0 != rc) { ++ errno = rc; ++ free((void *)addrinfo_arr); ++ free((void *)sockaddr_arr); ++ free((void *)sockbuff); ++ return -1; ++ } ++ *res = convert_wasi_addrinfo_to_addrinfo(addrinfo_arr, res_len); ++ free(addrinfo_arr); ++ free(sockaddr_arr); ++ free(sockbuff); ++ return 0; ++} ++ ++void freeaddrinfo(struct addrinfo *p) { ++ WSEDEBUG("WWSock| freeaddrinfo[%d]: \n", __LINE__); ++ free(p); ++} +\ No newline at end of file +diff --git a/wasmedge_stubs/wasi_socket_ext.h b/wasmedge_stubs/wasi_socket_ext.h +new file mode 100644 +index 00000000000..ee4dc75b430 +--- /dev/null ++++ b/wasmedge_stubs/wasi_socket_ext.h +@@ -0,0 +1,331 @@ ++#pragma once ++// Based on https://github.com/hangedfish/wasmedge_wasi_socket_c ++ ++#include ++ ++// struct sockaddr_in { ++// sa_family_t sin_family; ++// in_port_t sin_port; ++// struct in_addr sin_addr; ++// uint8_t sin_zero[8]; ++// }; ++ ++// struct sockaddr_in6 { ++// sa_family_t sin6_family; ++// in_port_t sin6_port; ++// uint32_t sin6_flowinfo; ++// struct in6_addr sin6_addr; ++// uint32_t sin6_scope_id; ++// }; ++ ++#ifndef SHUT_RD ++#define SHUT_RD 0 ++#define SHUT_WR 1 ++#define SHUT_RDWR 2 ++#endif ++ ++// #ifndef SOCK_STREAM ++// #define SOCK_STREAM 1 ++// #define SOCK_DGRAM 2 ++// #endif ++ ++#define SOCK_RAW 3 ++#define SOCK_RDM 4 ++#define SOCK_SEQPACKET 5 ++#define SOCK_DCCP 6 ++#define SOCK_PACKET 10 ++ ++#ifndef SOCK_CLOEXEC ++#define SOCK_CLOEXEC 02000000 ++#define SOCK_NONBLOCK 04000 ++#endif ++ ++#define PF_UNSPEC 0 ++#define PF_LOCAL 1 ++#define PF_UNIX PF_LOCAL ++#define PF_FILE PF_LOCAL ++#define PF_INET 2 ++#define PF_AX25 3 ++#define PF_IPX 4 ++#define PF_APPLETALK 5 ++#define PF_NETROM 6 ++#define PF_BRIDGE 7 ++#define PF_ATMPVC 8 ++#define PF_X25 9 ++#define PF_INET6 10 ++#define PF_ROSE 11 ++#define PF_DECnet 12 ++#define PF_NETBEUI 13 ++#define PF_SECURITY 14 ++#define PF_KEY 15 ++#define PF_NETLINK 16 ++#define PF_ROUTE PF_NETLINK ++#define PF_PACKET 17 ++#define PF_ASH 18 ++#define PF_ECONET 19 ++#define PF_ATMSVC 20 ++#define PF_RDS 21 ++#define PF_SNA 22 ++#define PF_IRDA 23 ++#define PF_PPPOX 24 ++#define PF_WANPIPE 25 ++#define PF_LLC 26 ++#define PF_IB 27 ++#define PF_MPLS 28 ++#define PF_CAN 29 ++#define PF_TIPC 30 ++#define PF_BLUETOOTH 31 ++#define PF_IUCV 32 ++#define PF_RXRPC 33 ++#define PF_ISDN 34 ++#define PF_PHONET 35 ++#define PF_IEEE802154 36 ++#define PF_CAIF 37 ++#define PF_ALG 38 ++#define PF_NFC 39 ++#define PF_VSOCK 40 ++#define PF_KCM 41 ++#define PF_QIPCRTR 42 ++#define PF_SMC 43 ++#define PF_XDP 44 ++#define PF_MAX 45 ++ ++// #define AF_UNSPEC PF_UNSPEC ++// #define AF_LOCAL PF_LOCAL ++// #define AF_UNIX AF_LOCAL ++// #define AF_FILE AF_LOCAL ++// #define AF_INET PF_INET ++// #define AF_AX25 PF_AX25 ++// #define AF_IPX PF_IPX ++// #define AF_APPLETALK PF_APPLETALK ++// #define AF_NETROM PF_NETROM ++// #define AF_BRIDGE PF_BRIDGE ++// #define AF_ATMPVC PF_ATMPVC ++// #define AF_X25 PF_X25 ++// #define AF_INET6 PF_INET6 ++// #define AF_ROSE PF_ROSE ++// #define AF_DECnet PF_DECnet ++// #define AF_NETBEUI PF_NETBEUI ++// #define AF_SECURITY PF_SECURITY ++// #define AF_KEY PF_KEY ++// #define AF_NETLINK PF_NETLINK ++// #define AF_ROUTE PF_ROUTE ++// #define AF_PACKET PF_PACKET ++// #define AF_ASH PF_ASH ++// #define AF_ECONET PF_ECONET ++// #define AF_ATMSVC PF_ATMSVC ++// #define AF_RDS PF_RDS ++// #define AF_SNA PF_SNA ++// #define AF_IRDA PF_IRDA ++// #define AF_PPPOX PF_PPPOX ++// #define AF_WANPIPE PF_WANPIPE ++// #define AF_LLC PF_LLC ++// #define AF_IB PF_IB ++// #define AF_MPLS PF_MPLS ++// #define AF_CAN PF_CAN ++// #define AF_TIPC PF_TIPC ++// #define AF_BLUETOOTH PF_BLUETOOTH ++// #define AF_IUCV PF_IUCV ++// #define AF_RXRPC PF_RXRPC ++// #define AF_ISDN PF_ISDN ++// #define AF_PHONET PF_PHONET ++// #define AF_IEEE802154 PF_IEEE802154 ++// #define AF_CAIF PF_CAIF ++// #define AF_ALG PF_ALG ++// #define AF_NFC PF_NFC ++// #define AF_VSOCK PF_VSOCK ++// #define AF_KCM PF_KCM ++// #define AF_QIPCRTR PF_QIPCRTR ++// #define AF_SMC PF_SMC ++// #define AF_XDP PF_XDP ++// #define AF_MAX PF_MAX ++ ++// WasmEdge wasi_sock_opt_so ++#define SO_REUSEADDR 0 ++#ifdef SO_TYPE ++#undef SO_TYPE ++#define SO_TYPE 1 ++#endif ++#define SO_ERROR 2 ++#define SO_DONTROUTE 3 ++#define SO_BROADCAST 4 ++#ifdef SO_SNDBUF ++#undef SO_SNBBUF ++#define SO_SNDBUF 5 ++#endif ++#define SO_RCVBUF 6 ++#define SO_KEEPALIVE 7 ++#define SO_OOBINLINE 8 ++#define SO_LINGER 9 ++#define SO_RCVLOWAT 10 ++#define SO_RCVTIMEO 11 ++#define SO_SNDTIME0 12 ++#define SO_ACCEPTCONN 13 ++ ++// #ifndef SO_DEBUG ++// #define SO_DEBUG 1 ++// #define SO_REUSEADDR 2 ++// #define SO_TYPE 3 ++// #define SO_ERROR 4 ++// #define SO_DONTROUTE 5 ++// #define SO_BROADCAST 6 ++// #define SO_SNDBUF 7 ++// #define SO_RCVBUF 8 ++// #define SO_KEEPALIVE 9 ++// #define SO_OOBINLINE 10 ++// #define SO_NO_CHECK 11 ++// #define SO_PRIORITY 12 ++// #define SO_LINGER 13 ++// #define SO_BSDCOMPAT 14 ++// #define SO_REUSEPORT 15 ++// #define SO_PASSCRED 16 ++// #define SO_PEERCRED 17 ++// #define SO_RCVLOWAT 18 ++// #define SO_SNDLOWAT 19 ++// #define SO_ACCEPTCONN 30 ++// #define SO_PEERSEC 31 ++// #define SO_SNDBUFFORCE 32 ++// #define SO_RCVBUFFORCE 33 ++// #define SO_PROTOCOL 38 ++// #define SO_DOMAIN 39 ++// #endif ++ ++#ifndef SO_RCVTIMEO ++#if __LONG_MAX == 0x7fffffff ++#define SO_RCVTIMEO 66 ++#define SO_SNDTIMEO 67 ++#else ++#define SO_RCVTIMEO 20 ++#define SO_SNDTIMEO 21 ++#endif ++#endif ++ ++#ifndef SO_TIMESTAMP ++#if __LONG_MAX == 0x7fffffff ++#define SO_TIMESTAMP 63 ++#define SO_TIMESTAMPNS 64 ++#define SO_TIMESTAMPING 65 ++#else ++#define SO_TIMESTAMP 29 ++#define SO_TIMESTAMPNS 35 ++#define SO_TIMESTAMPING 37 ++#endif ++#endif ++ ++#define SO_SECURITY_AUTHENTICATION 22 ++#define SO_SECURITY_ENCRYPTION_TRANSPORT 23 ++#define SO_SECURITY_ENCRYPTION_NETWORK 24 ++ ++#define SO_BINDTODEVICE 25 ++ ++#define SO_ATTACH_FILTER 26 ++#define SO_DETACH_FILTER 27 ++#define SO_GET_FILTER SO_ATTACH_FILTER ++ ++#define SO_PEERNAME 28 ++#define SCM_TIMESTAMP SO_TIMESTAMP ++#define SO_PASSSEC 34 ++#define SCM_TIMESTAMPNS SO_TIMESTAMPNS ++#define SO_MARK 36 ++#define SCM_TIMESTAMPING SO_TIMESTAMPING ++#define SO_RXQ_OVFL 40 ++#define SO_WIFI_STATUS 41 ++#define SCM_WIFI_STATUS SO_WIFI_STATUS ++#define SO_PEEK_OFF 42 ++#define SO_NOFCS 43 ++#define SO_LOCK_FILTER 44 ++#define SO_SELECT_ERR_QUEUE 45 ++#define SO_BUSY_POLL 46 ++#define SO_MAX_PACING_RATE 47 ++#define SO_BPF_EXTENSIONS 48 ++#define SO_INCOMING_CPU 49 ++#define SO_ATTACH_BPF 50 ++#define SO_DETACH_BPF SO_DETACH_FILTER ++#define SO_ATTACH_REUSEPORT_CBPF 51 ++#define SO_ATTACH_REUSEPORT_EBPF 52 ++#define SO_CNX_ADVICE 53 ++#define SCM_TIMESTAMPING_OPT_STATS 54 ++#define SO_MEMINFO 55 ++#define SO_INCOMING_NAPI_ID 56 ++#define SO_COOKIE 57 ++#define SCM_TIMESTAMPING_PKTINFO 58 ++#define SO_PEERGROUPS 59 ++#define SO_ZEROCOPY 60 ++#define SO_TXTIME 61 ++#define SCM_TXTIME SO_TXTIME ++#define SO_BINDTOIFINDEX 62 ++#define SO_DETACH_REUSEPORT_BPF 68 ++ ++#ifndef SOL_SOCKET ++#define SOL_SOCKET 1 ++#endif ++ ++#define SOL_IP 0 ++#define SOL_IPV6 41 ++#define SOL_ICMPV6 58 ++ ++#define SOL_RAW 255 ++#define SOL_DECNET 261 ++#define SOL_X25 262 ++#define SOL_PACKET 263 ++#define SOL_ATM 264 ++#define SOL_AAL 265 ++#define SOL_IRDA 266 ++#define SOL_NETBEUI 267 ++#define SOL_LLC 268 ++#define SOL_DCCP 269 ++#define SOL_NETLINK 270 ++#define SOL_TIPC 271 ++#define SOL_RXRPC 272 ++#define SOL_PPPOL2TP 273 ++#define SOL_BLUETOOTH 274 ++#define SOL_PNPIPE 275 ++#define SOL_RDS 276 ++#define SOL_IUCV 277 ++#define SOL_CAIF 278 ++#define SOL_ALG 279 ++#define SOL_NFC 280 ++#define SOL_KCM 281 ++#define SOL_TLS 282 ++#define SOL_XDP 283 ++ ++#define SOMAXCONN 128 ++ ++// #define MSG_OOB 0x0001 ++// #define MSG_PEEK 0x0002 ++// #define MSG_DONTROUTE 0x0004 ++// #define MSG_CTRUNC 0x0008 ++// #define MSG_PROXY 0x0010 ++// #define MSG_TRUNC 0x0020 ++// #define MSG_DONTWAIT 0x0040 ++// #define MSG_EOR 0x0080 ++// #define MSG_WAITALL 0x0100 ++// #define MSG_FIN 0x0200 ++// #define MSG_SYN 0x0400 ++// #define MSG_CONFIRM 0x0800 ++// #define MSG_RST 0x1000 ++// #define MSG_ERRQUEUE 0x2000 ++// #define MSG_NOSIGNAL 0x4000 ++// #define MSG_MORE 0x8000 ++// #define MSG_WAITFORONE 0x10000 ++// #define MSG_BATCH 0x40000 ++// #define MSG_ZEROCOPY 0x4000000 ++// #define MSG_FASTOPEN 0x20000000 ++// #define MSG_CMSG_CLOEXEC 0x40000000 ++ ++#ifdef __cplusplus ++extern "C" { ++#endif ++ ++int socket(int, int, int); ++ ++int bind(int, const struct sockaddr *, socklen_t); ++int connect(int, const struct sockaddr *, socklen_t); ++int listen(int, int); ++int accept(int, struct sockaddr *__restrict, socklen_t *__restrict); ++ ++int setsockopt(int, int, int, const void *, socklen_t); ++ ++#ifdef __cplusplus ++} ++#endif +\ No newline at end of file +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0006-fix-Avoid-include-syslog.h-since-it-is-not-part-of-w.patch b/php/php-8.2.6/patches/0006-fix-Avoid-include-syslog.h-since-it-is-not-part-of-w.patch new file mode 100644 index 0000000..1db508a --- /dev/null +++ b/php/php-8.2.6/patches/0006-fix-Avoid-include-syslog.h-since-it-is-not-part-of-w.patch @@ -0,0 +1,35 @@ +From 547ab191f123a4a7cf157bf0e20a5eb4ae4222b9 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Jesu=CC=81s=20Gonza=CC=81lez?= +Date: Tue, 14 Feb 2023 19:09:34 +0100 +Subject: [PATCH 06/18] fix: Avoid `#include ` since it is not part + of wasi-libc + + 100.0% ext/gd/libgd/ + + 100.0% ext/gd/libgd/ +diff --git a/ext/gd/libgd/gd_errors.h b/ext/gd/libgd/gd_errors.h +index 8a28bf60519..e84cef4578b 100644 +--- a/ext/gd/libgd/gd_errors.h ++++ b/ext/gd/libgd/gd_errors.h +@@ -1,11 +1,13 @@ + #ifndef GD_ERRORS_H + #define GD_ERRORS_H + +-#ifndef _WIN32 +-# include +-#else +-# include "win32/syslog.h" +-#endif ++#ifndef __wasi__ ++ #ifndef _WIN32 ++ # include ++ #else ++ # include "win32/syslog.h" ++ #endif ++#endif /* __wasi__ */ + + /* + LOG_EMERG system is unusable +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0007-fix-Avoid-setjmp-longjmp-in-gd_jpeg.patch b/php/php-8.2.6/patches/0007-fix-Avoid-setjmp-longjmp-in-gd_jpeg.patch new file mode 100644 index 0000000..f281dd0 --- /dev/null +++ b/php/php-8.2.6/patches/0007-fix-Avoid-setjmp-longjmp-in-gd_jpeg.patch @@ -0,0 +1,89 @@ +From 32d0a8f0495eb676f92888cd052bb3a50d514df6 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Jesu=CC=81s=20Gonza=CC=81lez?= +Date: Tue, 14 Feb 2023 19:10:15 +0100 +Subject: [PATCH 07/18] fix: Avoid setjmp/longjmp in `gd_jpeg` + + 100.0% ext/gd/libgd/ + + 100.0% ext/gd/libgd/ +diff --git a/ext/gd/libgd/gd_jpeg.c b/ext/gd/libgd/gd_jpeg.c +index f0974c2a99d..f3aa695967c 100644 +--- a/ext/gd/libgd/gd_jpeg.c ++++ b/ext/gd/libgd/gd_jpeg.c +@@ -23,7 +23,9 @@ + + #include + #include +-#include ++#ifndef __wasi__ ++ #include ++#endif + #include + #include + +@@ -97,8 +99,10 @@ static void fatal_jpeg_error (j_common_ptr cinfo) + jpeg_destroy (cinfo); + + if (jmpbufw != 0) { +- longjmp (jmpbufw->jmpbuf, 1); +- gd_error_ex(GD_ERROR, "gd-jpeg: EXTREMELY fatal error: longjmp returned control; terminating"); ++ #ifndef __wasi__ ++ longjmp (jmpbufw->jmpbuf, 1); ++ gd_error_ex(GD_ERROR, "gd-jpeg: EXTREMELY fatal error: longjmp returned control; terminating"); ++ #endif + } else { + gd_error_ex(GD_ERROR, "gd-jpeg: EXTREMELY fatal error: jmpbuf unrecoverable; terminating"); + } +@@ -187,13 +191,15 @@ static int _gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality) + + cinfo.err = jpeg_std_error (&jerr); + cinfo.client_data = &jmpbufw; +- if (setjmp (jmpbufw.jmpbuf) != 0) { +- /* we're here courtesy of longjmp */ +- if (row) { +- gdFree (row); ++ #ifndef __wasi__ ++ if (setjmp (jmpbufw.jmpbuf) != 0) { ++ /* we're here courtesy of longjmp */ ++ if (row) { ++ gdFree (row); ++ } ++ return 1; + } +- return 1; +- } ++ #endif + + cinfo.err->error_exit = fatal_jpeg_error; + +@@ -356,16 +362,18 @@ gdImagePtr gdImageCreateFromJpegCtxEx (gdIOCtx * infile, int ignore_warning) + cinfo.client_data = &jmpbufw; + cinfo.err->emit_message = php_jpeg_emit_message; + +- if (setjmp (jmpbufw.jmpbuf) != 0) { +- /* we're here courtesy of longjmp */ +- if (row) { +- gdFree (row); +- } +- if (im) { +- gdImageDestroy (im); ++ #ifndef __wasi__ ++ if (setjmp (jmpbufw.jmpbuf) != 0) { ++ /* we're here courtesy of longjmp */ ++ if (row) { ++ gdFree (row); ++ } ++ if (im) { ++ gdImageDestroy (im); ++ } ++ return 0; + } +- return 0; +- } ++ #endif + + cinfo.err->error_exit = fatal_jpeg_error; + +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0008-fix-Avoid-tmpfile-in-gd_bmp.patch b/php/php-8.2.6/patches/0008-fix-Avoid-tmpfile-in-gd_bmp.patch new file mode 100644 index 0000000..77c6f2f --- /dev/null +++ b/php/php-8.2.6/patches/0008-fix-Avoid-tmpfile-in-gd_bmp.patch @@ -0,0 +1,33 @@ +From bb7f3d60d80d938869d0e3a9c8ab5d58b8ff9f6f Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Jesu=CC=81s=20Gonza=CC=81lez?= +Date: Tue, 14 Feb 2023 22:01:33 +0100 +Subject: [PATCH 08/18] fix: Avoid tmpfile() in `gd_bmp` + + 100.0% ext/gd/libgd/ + + 100.0% ext/gd/libgd/ +diff --git a/ext/gd/libgd/gd_bmp.c b/ext/gd/libgd/gd_bmp.c +index 00903d5ff8d..b7a9121d15a 100644 +--- a/ext/gd/libgd/gd_bmp.c ++++ b/ext/gd/libgd/gd_bmp.c +@@ -102,9 +102,15 @@ void gdImageBmpCtx(gdImagePtr im, gdIOCtxPtr out, int compression) + compression = 0; + } + +- if (compression == 1 && !out->seek) { ++ if (compression == 1 && !out->seek) { + /* Try to create a temp file where we can seek */ +- if ((tmpfile_for_compression = tmpfile()) == NULL) { ++ if ((tmpfile_for_compression = ++#ifdef __wasi__ ++ NULL ++#else ++ tmpfile() ++#endif /* __wasi__ */ ++ ) == NULL) { + compression = 0; + } else { + out_original = out; +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0009-fix-Avoid-jmp_buf-in-gd_jpeg.patch b/php/php-8.2.6/patches/0009-fix-Avoid-jmp_buf-in-gd_jpeg.patch new file mode 100644 index 0000000..5b7e6b1 --- /dev/null +++ b/php/php-8.2.6/patches/0009-fix-Avoid-jmp_buf-in-gd_jpeg.patch @@ -0,0 +1,25 @@ +From b1d7bc69013a079a2df2d1c774df11d9cdbbd1a0 Mon Sep 17 00:00:00 2001 +From: "no-reply@wasmlabs.dev" +Date: Wed, 8 Mar 2023 10:45:26 +0200 +Subject: [PATCH 09/18] fix: Avoid 'jmp_buf' in 'gd_jpeg' + + 100.0% ext/gd/libgd/ + + 100.0% ext/gd/libgd/ +diff --git a/ext/gd/libgd/gd_jpeg.c b/ext/gd/libgd/gd_jpeg.c +index f3aa695967c..d9d613a3bc8 100644 +--- a/ext/gd/libgd/gd_jpeg.c ++++ b/ext/gd/libgd/gd_jpeg.c +@@ -45,7 +45,9 @@ static const char *const GD_JPEG_VERSION = "1.0"; + + typedef struct _jmpbuf_wrapper + { ++#ifndef __wasi__ + jmp_buf jmpbuf; ++#endif // __wasi__ + int ignore_warning; + } jmpbuf_wrapper; + +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0010-chore-Add-more-logs-in-wasi_socket_ext.patch b/php/php-8.2.6/patches/0010-chore-Add-more-logs-in-wasi_socket_ext.patch new file mode 100644 index 0000000..58b50b4 --- /dev/null +++ b/php/php-8.2.6/patches/0010-chore-Add-more-logs-in-wasi_socket_ext.patch @@ -0,0 +1,44 @@ +From d1708155a319ff39788a7b9fbe6b84618b1c059a Mon Sep 17 00:00:00 2001 +From: Asen Alexandrov +Date: Thu, 30 Mar 2023 09:29:55 +0300 +Subject: [PATCH 10/18] chore: Add more logs in wasi_socket_ext + + 100.0% wasmedge_stubs/ + + 100.0% wasmedge_stubs/ + + 100.0% wasmedge_stubs/ +diff --git a/wasmedge_stubs/wasi_socket_ext.c b/wasmedge_stubs/wasi_socket_ext.c +index 0c74c75a5ec..5a2a3d26573 100644 +--- a/wasmedge_stubs/wasi_socket_ext.c ++++ b/wasmedge_stubs/wasi_socket_ext.c +@@ -184,6 +184,7 @@ int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_setsockopt( + + int socket(int domain, int type, int protocol) + { ++ WSEDEBUG("WWSock| socket called: %d, %d, %d \n", domain, type, protocol); + int fd; + address_family_t af = (domain == AF_INET ? kInet4 : kInet6); + socket_type_t st = (type == SOCK_STREAM ? kStream : kDatagram); +@@ -191,6 +192,7 @@ int socket(int domain, int type, int protocol) + if (0 != res) { + errno = res; + printf("socket err: %s \n", strerror(errno)); ++ WSEDEBUG("WWSock| socket failed with error: %s \n", strerror(errno)); + return -1; + } + WSEDEBUG("WWSock| socket returning: %d \n", fd); +@@ -271,8 +273,10 @@ int accept(int fd, struct sockaddr *restrict addr, socklen_t *restrict len) { + fd, (uint32_t *)&new_sockfd); + if (res != 0) { + errno = res; ++ WSEDEBUG("WWSock| accept[%d]: failed=%d\n", __LINE__, errno); + return -1; + } ++ WSEDEBUG("WWSock| accept[%d]: client_fd=%d\n", __LINE__, new_sockfd); + return new_sockfd; + } + +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0011-fix-fastcgi-server-in-php-cgi-for-wasmedge.patch b/php/php-8.2.6/patches/0011-fix-fastcgi-server-in-php-cgi-for-wasmedge.patch new file mode 100644 index 0000000..79e4ec6 --- /dev/null +++ b/php/php-8.2.6/patches/0011-fix-fastcgi-server-in-php-cgi-for-wasmedge.patch @@ -0,0 +1,167 @@ +From 2285abd8f2a50da92b54e66739267b05236a2a83 Mon Sep 17 00:00:00 2001 +From: Asen Alexandrov +Date: Thu, 30 Mar 2023 16:23:01 +0300 +Subject: [PATCH 11/18] fix: fastcgi server in php-cgi for wasmedge + + 100.0% main/ + + 100.0% main/ + + 100.0% main/ +diff --git a/main/fastcgi.c b/main/fastcgi.c +index ec29595e703..742a44c8432 100644 +--- a/main/fastcgi.c ++++ b/main/fastcgi.c +@@ -69,11 +69,16 @@ static int is_impersonate = 0; + # include + # include + # include +-# ifndef WASM_WASI ++# ifndef __wasi__ + # include +-# endif // WASM_WASI ++# endif // __wasi__ + # include + ++#ifdef WASM_RUNTIME_WASMEDGE ++#include "wasmedge_stubs/netdb.h" ++#include "wasmedge_stubs/wasi_socket_ext.h" ++#endif ++ + # if defined(HAVE_POLL_H) && defined(HAVE_POLL) + # include + # elif defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL) +@@ -433,7 +438,7 @@ static void fcgi_signal_handler(int signo) + + static void fcgi_setup_signals(void) + { +-#ifndef WASM_WASI ++#ifndef __wasi__ + struct sigaction new_sa, old_sa; + + sigemptyset(&new_sa.sa_mask); +@@ -445,7 +450,7 @@ static void fcgi_setup_signals(void) + if (old_sa.sa_handler == SIG_DFL) { + sigaction(SIGPIPE, &new_sa, NULL); + } +-#endif // WASM_WASI ++#endif // __wasi__ + } + #endif + +@@ -530,7 +535,9 @@ int fcgi_init(void) + } else { + return is_fastcgi = 0; + } +-#elif defined(WASM_WASI) ++#elif defined(__wasi__) ++ // This should be 1 if we were spawned as a fastcgi process by an external manager. ++ // Otherwise, if listening as a fastcgi server - the only thing supported by Wasi, we should return 0 here. + return is_fastcgi = 0; + #else + errno = 0; +@@ -681,7 +688,14 @@ int fcgi_listen(const char *path, int backlog) + + /* Prepare socket address */ + if (tcp) { +-#ifndef WASM_WASI ++#ifdef WASM_RUNTIME_WASMEDGE ++ memset(&sa.sa_inet, 0, sizeof(sa.sa_inet)); ++ sa.sa_inet.sin_family = AF_INET; ++ sa.sa_inet.sin_addr.s_addr = INADDR_ANY; ++ sa.sa_inet.sin_port = port; ++ sock_len = sizeof(sa.sa_inet); ++#endif ++#ifndef __wasi__ + memset(&sa.sa_inet, 0, sizeof(sa.sa_inet)); + sa.sa_inet.sin_family = AF_INET; + sa.sa_inet.sin_port = htons(port); +@@ -713,6 +727,7 @@ int fcgi_listen(const char *path, int backlog) + sa.sa_inet.sin_addr.s_addr = ((struct in_addr*)hep->h_addr_list[0])->s_addr; + } + } ++#endif // __wasi__ + } else { + #ifdef _WIN32 + SECURITY_DESCRIPTOR sd; +@@ -740,7 +755,7 @@ int fcgi_listen(const char *path, int backlog) + is_fastcgi = 1; + return listen_socket; + +-#else ++#elif !defined(__wasi__) + size_t path_len = strlen(path); + + if (path_len >= sizeof(sa.sa_unix.sun_path)) { +@@ -759,9 +774,10 @@ int fcgi_listen(const char *path, int backlog) + #endif + } + ++#if !defined(__wasi__) || defined(WASM_RUNTIME_WASMEDGE) + /* Create, bind socket and start listen on it */ + if ((listen_socket = socket(sa.sa.sa_family, SOCK_STREAM, 0)) < 0 || +-#ifdef SO_REUSEADDR ++#if defined(SO_REUSEADDR) && !defined(__wasi__) + setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse)) < 0 || + #endif + bind(listen_socket, (struct sockaddr *) &sa, sock_len) < 0 || +@@ -770,11 +786,13 @@ int fcgi_listen(const char *path, int backlog) + fcgi_log(FCGI_ERROR, "Cannot bind/listen socket - [%d] %s.\n",errno, strerror(errno)); + return -1; + } ++#endif + + if (!tcp) { ++#ifndef __wasi__ + chmod(path, 0777); ++#endif // __wasi__ + } else { +-#endif // WASM_WASI + char *ip = getenv("FCGI_WEB_SERVER_ADDRS"); + char *cur, *end; + int n; +@@ -947,6 +965,8 @@ static inline ssize_t safe_write(fcgi_request *req, const void *buf, size_t coun + errno = WSAGetLastError(); + } + } ++#elif defined(__wasi__) ++ ret = send(req->fd, ((char*)buf)+n, count-n, 0); + #else + ret = write(req->fd, ((char*)buf)+n, count-n); + #endif +@@ -984,6 +1004,8 @@ static inline ssize_t safe_read(fcgi_request *req, const void *buf, size_t count + errno = WSAGetLastError(); + } + } ++#elif defined(__wasi__) ++ ret = recv(req->fd, ((char*)buf)+n, count-n, 0); + #else + ret = read(req->fd, ((char*)buf)+n, count-n); + #endif +@@ -1107,9 +1129,9 @@ static int fcgi_read_request(fcgi_request *req) + int on = 1; + # endif + +-# ifndef WASM_WASI ++# ifndef __wasi__ + setsockopt(req->fd, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on)); +-# endif // WASM_WASI ++# endif // __wasi__ + req->nodelay = 1; + } + #endif +@@ -1419,9 +1441,9 @@ int fcgi_accept_request(fcgi_request *req) + client_sa = sa; + if (req->fd >= 0 && !fcgi_is_allowed()) { + fcgi_log(FCGI_ERROR, "Connection disallowed: IP address '%s' has been dropped.", fcgi_get_last_client_ip()); +-#ifndef WASM_WASI ++#ifndef __wasi__ + closesocket(req->fd); +-#endif // WASM_WASI ++#endif // __wasi__ + req->fd = -1; + continue; + } +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0012-chore-Add-debug-logging-in-fastcgi.patch b/php/php-8.2.6/patches/0012-chore-Add-debug-logging-in-fastcgi.patch new file mode 100644 index 0000000..aeef14e --- /dev/null +++ b/php/php-8.2.6/patches/0012-chore-Add-debug-logging-in-fastcgi.patch @@ -0,0 +1,277 @@ +From 2773d5476500ae6bd9a11586b95b983c86bc923d Mon Sep 17 00:00:00 2001 +From: Asen Alexandrov +Date: Thu, 30 Mar 2023 18:17:16 +0300 +Subject: [PATCH 12/18] chore: Add debug logging in fastcgi + + 100.0% main/ + + 100.0% main/ + + 100.0% main/ +diff --git a/main/fastcgi.c b/main/fastcgi.c +index 742a44c8432..ab4b4b1286d 100644 +--- a/main/fastcgi.c ++++ b/main/fastcgi.c +@@ -137,6 +137,14 @@ static int is_impersonate = 0; + + #include "fastcgi.h" + ++// #define FASTCGI_DEBUG ++ ++#ifdef FASTCGI_DEBUG ++#define FCGIDEBUG(fmt, ...) fprintf(stderr, fmt __VA_OPT__(,) __VA_ARGS__) ++#else ++#define FCGIDEBUG(fmt, ...) ++#endif ++ + typedef struct _fcgi_header { + unsigned char version; + unsigned char type; +@@ -490,6 +498,7 @@ void __attribute__((weak)) fcgi_log(int type, const char *format, ...) { + + int fcgi_init(void) + { ++ FCGIDEBUG("(%s)[%d] fcgi_init\n",__FILE__, __LINE__); + if (!is_initialized) { + #ifndef _WIN32 + sa_t sa; +@@ -498,6 +507,7 @@ int fcgi_init(void) + zend_hash_init(&fcgi_mgmt_vars, 8, NULL, fcgi_free_mgmt_var_cb, 1); + fcgi_set_mgmt_var("FCGI_MPXS_CONNS", sizeof("FCGI_MPXS_CONNS")-1, "0", sizeof("0")-1); + ++ FCGIDEBUG("(%s)[%d] is_initialized=1\n",__FILE__, __LINE__); + is_initialized = 1; + #ifdef _WIN32 + # if 0 +@@ -556,6 +566,7 @@ int fcgi_init(void) + int fcgi_is_fastcgi(void) + { + if (!is_initialized) { ++ FCGIDEBUG("(%s)[%d] before fcgi_init\n",__FILE__, __LINE__); + return fcgi_init(); + } else { + return is_fastcgi; +@@ -656,6 +667,7 @@ static int is_port_number(const char *bindpath) + + int fcgi_listen(const char *path, int backlog) + { ++ FCGIDEBUG("(%s)[%d] fcgi_listen(%s, %d)\n",__FILE__, __LINE__, path, backlog); + char *s; + int tcp = 0; + char host[MAXPATHLEN]; +@@ -729,6 +741,7 @@ int fcgi_listen(const char *path, int backlog) + } + #endif // __wasi__ + } else { ++ FCGIDEBUG("(%s)[%d] else, for if(tcp)\n",__FILE__, __LINE__); + #ifdef _WIN32 + SECURITY_DESCRIPTOR sd; + SECURITY_ATTRIBUTES saw; +@@ -750,6 +763,7 @@ int fcgi_listen(const char *path, int backlog) + } + listen_socket = _open_osfhandle((intptr_t)namedPipe, 0); + if (!is_initialized) { ++ FCGIDEBUG("(%s)[%d] before fcgi_init\n",__FILE__, __LINE__); + fcgi_init(); + } + is_fastcgi = 1; +@@ -783,6 +797,7 @@ int fcgi_listen(const char *path, int backlog) + bind(listen_socket, (struct sockaddr *) &sa, sock_len) < 0 || + listen(listen_socket, backlog) < 0) { + close(listen_socket); ++ FCGIDEBUG("(%s)[%d] Cannot bind/listen socket\n",__FILE__, __LINE__); + fcgi_log(FCGI_ERROR, "Cannot bind/listen socket - [%d] %s.\n",errno, strerror(errno)); + return -1; + } +@@ -798,6 +813,7 @@ int fcgi_listen(const char *path, int backlog) + int n; + + if (ip) { ++ FCGIDEBUG("(%s)[%d] FCGI_WEB_SERVER_ADDRS ip=%s\n",__FILE__, __LINE__, ip); + ip = strdup(ip); + cur = ip; + n = 0; +@@ -823,6 +839,7 @@ int fcgi_listen(const char *path, int backlog) + n++; + #endif + } else { ++ FCGIDEBUG("(%s)[%d] Wrong IP address '%s' in listen.allowed_clients\n",__FILE__, __LINE__, cur); + fcgi_log(FCGI_ERROR, "Wrong IP address '%s' in listen.allowed_clients", cur); + } + cur = end; +@@ -830,6 +847,7 @@ int fcgi_listen(const char *path, int backlog) + allowed_clients[n].sa.sa_family = 0; + free(ip); + if (!n) { ++ FCGIDEBUG("(%s)[%d] NO allowed addresses\n",__FILE__, __LINE__); + fcgi_log(FCGI_ERROR, "There are no allowed addresses"); + /* don't clear allowed_clients as it will create an "open for all" security issue */ + } +@@ -837,8 +855,10 @@ int fcgi_listen(const char *path, int backlog) + } + + if (!is_initialized) { ++ FCGIDEBUG("(%s)[%d] before fcgi_init\n",__FILE__, __LINE__); + fcgi_init(); + } ++ FCGIDEBUG("(%s)[%d] is_fastcgi = 1;\n",__FILE__, __LINE__); + is_fastcgi = 1; + + #ifdef _WIN32 +@@ -846,13 +866,16 @@ int fcgi_listen(const char *path, int backlog) + listen_socket = _open_osfhandle((intptr_t)listen_socket, 0); + } + #else ++ FCGIDEBUG("(%s)[%d] before fcgi_setup_signals;\n",__FILE__, __LINE__); + fcgi_setup_signals(); + #endif ++ FCGIDEBUG("(%s)[%d] returning listen_socket=%d;\n",__FILE__, __LINE__, listen_socket); + return listen_socket; + } + + void fcgi_set_allowed_clients(char *ip) + { ++ FCGIDEBUG("(%s)[%d] fcgi_set_allowed_clients ip=%s\n",__FILE__, __LINE__, ip); + char *cur, *end; + int n; + +@@ -883,6 +906,7 @@ void fcgi_set_allowed_clients(char *ip) + n++; + #endif + } else { ++ FCGIDEBUG("(%s)[%d] Wrong IP address '%s' in listen.allowed_clients\n",__FILE__, __LINE__, cur); + fcgi_log(FCGI_ERROR, "Wrong IP address '%s' in listen.allowed_clients", cur); + } + cur = end; +@@ -890,6 +914,7 @@ void fcgi_set_allowed_clients(char *ip) + allowed_clients[n].sa.sa_family = 0; + free(ip); + if (!n) { ++ FCGIDEBUG("(%s)[%d] No allowed addresses\n",__FILE__, __LINE__); + fcgi_log(FCGI_ERROR, "There are no allowed addresses"); + /* don't clear allowed_clients as it will create an "open for all" security issue */ + } +@@ -1072,6 +1097,7 @@ static int fcgi_get_params(fcgi_request *req, unsigned char *p, unsigned char *e + + static int fcgi_read_request(fcgi_request *req) + { ++ FCGIDEBUG("(%s)[%d] fcgi_read_request\n",__FILE__, __LINE__); + fcgi_header hdr; + int len, padding; + unsigned char buf[FCGI_MAX_LENGTH+8]; +@@ -1090,12 +1116,14 @@ static int fcgi_read_request(fcgi_request *req) + + if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) || + hdr.version < FCGI_VERSION_1) { ++ FCGIDEBUG("(%s)[%d] failed to read header=%d\n",__FILE__, __LINE__, hdr.version); + return 0; + } + + len = (hdr.contentLengthB1 << 8) | hdr.contentLengthB0; + padding = hdr.paddingLength; + ++ FCGIDEBUG("(%s)[%d] len=%d, padding=%d\n",__FILE__, __LINE__, len, padding); + while (hdr.type == FCGI_STDIN && len == 0) { + if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) || + hdr.version < FCGI_VERSION_1) { +@@ -1106,6 +1134,7 @@ static int fcgi_read_request(fcgi_request *req) + padding = hdr.paddingLength; + } + ++ FCGIDEBUG("(%s)[%d] len + padding=%d\n",__FILE__, __LINE__, len, padding); + if (len + padding > FCGI_MAX_LENGTH) { + return 0; + } +@@ -1352,12 +1381,15 @@ int fcgi_is_closed(fcgi_request *req) + } + + static int fcgi_is_allowed(void) { ++ FCGIDEBUG("(%s)[%d] fcgi_is_allowed\n",__FILE__, __LINE__); + int i; + + if (client_sa.sa.sa_family == AF_UNIX) { ++ FCGIDEBUG("(%s)[%d] client_sa.sa.sa_family == AF_UNIX\n",__FILE__, __LINE__); + return 1; + } + if (!allowed_clients) { ++ FCGIDEBUG("(%s)[%d] !allowed_clients\n",__FILE__, __LINE__); + return 1; + } + if (client_sa.sa.sa_family == AF_INET) { +@@ -1386,6 +1418,7 @@ static int fcgi_is_allowed(void) { + } + #endif + ++ FCGIDEBUG("(%s)[%d] fcgi_is_allowed return 0\n",__FILE__, __LINE__); + return 0; + } + +@@ -1403,6 +1436,7 @@ int fcgi_accept_request(fcgi_request *req) + return -1; + } + ++ FCGIDEBUG("(%s)[%d] before req->hook.on_accept\n",__FILE__, __LINE__); + req->hook.on_accept(); + #ifdef _WIN32 + if (!req->tcp) { +@@ -1435,9 +1469,12 @@ int fcgi_accept_request(fcgi_request *req) + socklen_t len = sizeof(sa); + + FCGI_LOCK(req->listen_socket); ++ FCGIDEBUG("(%s)[%d] before accept(%d, %p, %u)\n",__FILE__, __LINE__, listen_socket, (struct sockaddr *)&sa, &len); + req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len); + FCGI_UNLOCK(req->listen_socket); + ++ FCGIDEBUG("(%s)[%d] accepted fd=%d\n",__FILE__, __LINE__, req->fd); ++ + client_sa = sa; + if (req->fd >= 0 && !fcgi_is_allowed()) { + fcgi_log(FCGI_ERROR, "Connection disallowed: IP address '%s' has been dropped.", fcgi_get_last_client_ip()); +@@ -1449,6 +1486,7 @@ int fcgi_accept_request(fcgi_request *req) + } + } + ++ FCGIDEBUG("(%s)[%d] before shutdown check\n",__FILE__, __LINE__); + #ifdef _WIN32 + if (req->fd < 0 && (in_shutdown || errno != EINTR)) { + #else +@@ -1462,6 +1500,7 @@ int fcgi_accept_request(fcgi_request *req) + #else + if (req->fd >= 0) { + #if defined(HAVE_POLL) ++ FCGIDEBUG("(%s)[%d] HAVE_POLL\n",__FILE__, __LINE__); + struct pollfd fds; + int ret; + +@@ -1470,13 +1509,18 @@ int fcgi_accept_request(fcgi_request *req) + fds.revents = 0; + do { + errno = 0; ++ FCGIDEBUG("(%s)[%d] before poll for fd=%d \n",__FILE__, __LINE__, req->fd); + ret = poll(&fds, 1, 5000); ++ FCGIDEBUG("(%s)[%d] poll returned %d, fds.revents=%d\n",__FILE__, __LINE__, ret, fds.revents); + } while (ret < 0 && errno == EINTR); + if (ret > 0 && (fds.revents & POLLIN)) { ++ FCGIDEBUG("(%s)[%d] before break\n",__FILE__, __LINE__); + break; + } ++ FCGIDEBUG("(%s)[%d] before fcgi_close\n",__FILE__, __LINE__); + fcgi_close(req, 1, 0); + #else ++ FCGIDEBUG("(%s)[%d] ! HAVE_POLL\n",__FILE__, __LINE__); + if (req->fd < FD_SETSIZE) { + struct timeval tv = {5,0}; + fd_set set; +@@ -1503,7 +1547,9 @@ int fcgi_accept_request(fcgi_request *req) + } else if (in_shutdown) { + return -1; + } ++ FCGIDEBUG("(%s)[%d] before hook.on_read\n",__FILE__, __LINE__); + req->hook.on_read(); ++ FCGIDEBUG("(%s)[%d] before fcgi_read_request\n",__FILE__, __LINE__); + if (fcgi_read_request(req)) { + #ifdef _WIN32 + if (is_impersonate && !req->tcp) { +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0013-chore-Fixes-logs-getnameinfo-in-wasi-sock-sdk.patch b/php/php-8.2.6/patches/0013-chore-Fixes-logs-getnameinfo-in-wasi-sock-sdk.patch new file mode 100644 index 0000000..9bba478 --- /dev/null +++ b/php/php-8.2.6/patches/0013-chore-Fixes-logs-getnameinfo-in-wasi-sock-sdk.patch @@ -0,0 +1,133 @@ +From a5f23f61ebf0f49d42f34fc9195883b68024d1d8 Mon Sep 17 00:00:00 2001 +From: Asen Alexandrov +Date: Wed, 5 Apr 2023 11:47:39 +0300 +Subject: [PATCH 13/18] chore: Fixes, logs, getnameinfo in wasi sock sdk + + 100.0% wasmedge_stubs/ + + 100.0% wasmedge_stubs/ +diff --git a/wasmedge_stubs/netdb.h b/wasmedge_stubs/netdb.h +index c1136150b7b..9faf3ced9f0 100644 +--- a/wasmedge_stubs/netdb.h ++++ b/wasmedge_stubs/netdb.h +@@ -1,6 +1,8 @@ + #pragma once + // Based on https://github.com/hangedfish/wasmedge_wasi_socket_c + ++#include ++ + struct addrinfo { + int ai_flags; + int ai_family; +@@ -51,6 +53,12 @@ struct addrinfo { + #define NI_MAXHOST 255 + #define NI_MAXSERV 32 + ++ ++ ++#ifdef __cplusplus ++extern "C" { ++#endif ++ + struct servent { + char *s_name; + char **s_aliases; +@@ -58,15 +66,20 @@ struct servent { + char *s_proto; + }; + +-#ifdef __cplusplus +-extern "C" { +-#endif ++struct hostent { ++ char *h_name; ++ char **h_aliases; ++ int h_addrtype; ++ int h_length; ++ char **h_addr_list; ++}; ++#define h_addr h_addr_list[0] + + struct servent *getservbyname (const char *, const char *); + + int getaddrinfo (const char *__restrict, const char *__restrict, const struct addrinfo *__restrict, struct addrinfo **__restrict); + void freeaddrinfo (struct addrinfo *); +-// int getnameinfo (const struct sockaddr *__restrict, socklen_t, char *__restrict, socklen_t, char *__restrict, socklen_t, int); ++int getnameinfo (const struct sockaddr *__restrict addr, socklen_t addrlen, char *__restrict host, socklen_t hostlen, char *__restrict serv, socklen_t servlen, int flags); + + #ifdef __cplusplus + } +diff --git a/wasmedge_stubs/wasi_socket_ext.c b/wasmedge_stubs/wasi_socket_ext.c +index 5a2a3d26573..acbebba855b 100644 +--- a/wasmedge_stubs/wasi_socket_ext.c ++++ b/wasmedge_stubs/wasi_socket_ext.c +@@ -180,7 +180,7 @@ int32_t __imported_wasmedge_wasi_snapshot_preview1_sock_setsockopt( + uint32_t fd, int32_t level, int32_t name, int32_t *flag, + uint32_t *flag_size) + __attribute__((__import_module__("wasi_snapshot_preview1"), +- __import_name__("sock_getsockopt"))); ++ __import_name__("sock_setsockopt"))); + + int socket(int domain, int type, int protocol) + { +@@ -208,7 +208,7 @@ int bind(int fd, const struct sockaddr *addr, socklen_t len) + + wasi_address_t wasi_addr; + memset(&wasi_addr, 0, sizeof(wasi_address_t)); +- uint32_t port; ++ uint32_t port = 0; + if (AF_INET == addr->sa_family) { + struct sockaddr_in *sin = (struct sockaddr_in *)addr; + wasi_addr.buf = (uint8_t *)&sin->sin_addr; +@@ -219,6 +219,9 @@ int bind(int fd, const struct sockaddr *addr, socklen_t len) + wasi_addr.buf = (uint8_t *)&sin->sin6_addr; + wasi_addr.size = 16; + port = sin->sin6_port; ++ } else { ++ errno = EAFNOSUPPORT; ++ return -1; + } + + WSEDEBUG("WWSock| bind[%d]: __imported_wasmedge_wasi_snapshot_preview1_sock_bind\n", __LINE__); +@@ -235,7 +238,7 @@ int bind(int fd, const struct sockaddr *addr, socklen_t len) + + int connect(int fd, const struct sockaddr *addr, socklen_t len) { + WSEDEBUG("WWSock| connect[%d]: fd=%d, addr=%d, port=%d \n", __LINE__, +- fd, ((struct sockaddr_in *)addr)->sin_addr.s_addr, ((struct sockaddr_in *)addr)->sin_port); ++ fd, ((struct sockaddr_in *)addr)->sin_addr.s_addr, ((struct sockaddr_in *)addr)->sin_port); + wasi_address_t wasi_addr; + memset(&wasi_addr, 0, sizeof(wasi_address_t)); + uint32_t port; +@@ -249,11 +252,15 @@ int connect(int fd, const struct sockaddr *addr, socklen_t len) { + wasi_addr.buf = (uint8_t *)&sin->sin6_addr; + wasi_addr.size = 16; + port = ntohs(sin->sin6_port); ++ } else { ++ errno = EAFNOSUPPORT; ++ return -1; + } + int res = __imported_wasmedge_wasi_snapshot_preview1_sock_connect( + fd, &wasi_addr, port); + if (res != 0) { + errno = res; ++ WSEDEBUG("WWSock| connect[%d]: fd=%d failed: wasi_error=%d, errno=%d \n", __LINE__, fd, res, errno); + return -1; + } + return res; +@@ -407,4 +414,11 @@ int getaddrinfo(const char *restrict host, const char *restrict serv, + void freeaddrinfo(struct addrinfo *p) { + WSEDEBUG("WWSock| freeaddrinfo[%d]: \n", __LINE__); + free(p); +-} +\ No newline at end of file ++} ++ ++int getnameinfo(const struct sockaddr *__restrict addr, socklen_t addrlen, char *__restrict host, socklen_t hostlen, char *__restrict serv, socklen_t servlen, int flags) ++{ ++ WSEDEBUG("WWSock| getnameinfo[%d]: \n", __LINE__); ++ // When lookup fails, software should use the IP address string as hostname ++ return EAI_FAIL; ++} +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0014-fix-implicit-function-declaration-in-syslog.patch b/php/php-8.2.6/patches/0014-fix-implicit-function-declaration-in-syslog.patch new file mode 100644 index 0000000..443bcfc --- /dev/null +++ b/php/php-8.2.6/patches/0014-fix-implicit-function-declaration-in-syslog.patch @@ -0,0 +1,28 @@ +From 5addc7b01c06ea6d0af0ac4e6a7dfa50ade903b8 Mon Sep 17 00:00:00 2001 +From: Asen Alexandrov +Date: Wed, 5 Apr 2023 11:55:31 +0300 +Subject: [PATCH 14/18] fix: implicit-function-declaration in syslog + + 100.0% main/ + + 100.0% main/ +diff --git a/main/php_syslog.c b/main/php_syslog.c +index 68c7aac7072..a1d6a23c92c 100644 +--- a/main/php_syslog.c ++++ b/main/php_syslog.c +@@ -32,6 +32,12 @@ + #define syslog std_syslog + #endif + ++#ifdef __wasi__ ++#define syslog(...) ++#define openlog(...) ++#define closelog() ++#endif ++ + PHPAPI void php_syslog_str(int priority, const zend_string* message) + { + smart_string sbuf = {0}; +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0015-feat-Enable-mysqlnd-and-more-network-functions-with-.patch b/php/php-8.2.6/patches/0015-feat-Enable-mysqlnd-and-more-network-functions-with-.patch new file mode 100644 index 0000000..79a1166 --- /dev/null +++ b/php/php-8.2.6/patches/0015-feat-Enable-mysqlnd-and-more-network-functions-with-.patch @@ -0,0 +1,248 @@ +From a85084989c64e839f94d1e1efe85e4d177fb4d4b Mon Sep 17 00:00:00 2001 +From: Asen Alexandrov +Date: Wed, 5 Apr 2023 12:11:28 +0300 +Subject: [PATCH 15/18] feat: Enable mysqlnd and more network functions with + WasmEdge + + 8.6% ext/mysqlnd/ + 27.8% ext/standard/ + 6.5% main/streams/ + 56.9% main/ + + 8.6% ext/mysqlnd/ + 27.8% ext/standard/ + 6.5% main/streams/ + 56.9% main/ +diff --git a/ext/mysqlnd/mysqlnd_vio.c b/ext/mysqlnd/mysqlnd_vio.c +index 79fc92d500a..e94854919ba 100644 +--- a/ext/mysqlnd/mysqlnd_vio.c ++++ b/ext/mysqlnd/mysqlnd_vio.c +@@ -23,7 +23,10 @@ + #include "mysqlnd_ext_plugin.h" + #include "php_network.h" + +-#ifndef PHP_WIN32 ++#ifdef WASM_RUNTIME_WASMEDGE ++#include "wasmedge_stubs/netdb.h" ++#include "wasmedge_stubs/wasi_socket_ext.h" ++#elif !defined(PHP_WIN32) + #include + #else + #include +diff --git a/ext/standard/dns.c b/ext/standard/dns.c +index 104f847ab40..79ea4f6382e 100644 +--- a/ext/standard/dns.c ++++ b/ext/standard/dns.c +@@ -34,9 +34,11 @@ + #ifdef HAVE_ARPA_INET_H + #include + #endif +-#ifndef WASM_WASI ++#ifndef __wasi__ + #include +-#endif // WASM_WASI ++#elif defined(WASM_RUNTIME_WASMEDGE) ++#include ++#endif // __wasi__ + #ifdef _OSD_POSIX + #undef STATUS + #undef T_UNSPEC +@@ -174,7 +176,7 @@ PHP_FUNCTION(gethostbyaddr) + /* {{{ php_gethostbyaddr */ + static zend_string *php_gethostbyaddr(char *ip) + { +-#ifndef WASM_WASI ++#if !defined(__wasi__) || defined(WASM_RUNTIME_WASMEDGE) + #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON) + struct sockaddr_in sa4; + struct sockaddr_in6 sa6; +@@ -218,7 +220,7 @@ static zend_string *php_gethostbyaddr(char *ip) + #endif + #else + return NULL; +-#endif // WASM_WASI ++#endif // __wasi__ + } + /* }}} */ + +@@ -245,7 +247,7 @@ PHP_FUNCTION(gethostbyname) + /* {{{ Return a list of IP addresses that a given hostname resolves to. */ + PHP_FUNCTION(gethostbynamel) + { +-#ifndef WASM_WASI ++#if !defined(__wasi__) || defined(WASM_RUNTIME_WASMEDGE) + char *hostname; + size_t hostname_len; + struct hostent *hp; +@@ -289,14 +291,14 @@ PHP_FUNCTION(gethostbynamel) + } + #else + RETURN_FALSE; +-#endif // WASM_WASI ++#endif // __wasi__ + } + /* }}} */ + + /* {{{ php_gethostbyname */ + static zend_string *php_gethostbyname(char *name) + { +-#ifndef WASM_WASI ++#if !defined(__wasi__) || defined(WASM_RUNTIME_WASMEDGE) + struct hostent *hp; + struct in_addr *h_addr_0; /* Don't call this h_addr, it's a macro! */ + struct in_addr in; +@@ -326,7 +328,7 @@ static zend_string *php_gethostbyname(char *name) + return zend_string_init(address, strlen(address), 0); + #else + return NULL; +-#endif // WASM_WASI ++#endif // __wasi__ + } + /* }}} */ + +diff --git a/main/network.c b/main/network.c +index 683e11d03d6..bb39f27e04b 100644 +--- a/main/network.c ++++ b/main/network.c +@@ -54,9 +54,12 @@ + + #ifndef PHP_WIN32 + #include +-#ifndef WASM_WASI ++#if !defined(__wasi__) + #include +-#endif // WASM_WASI ++#elif defined(WASM_RUNTIME_WASMEDGE) ++#include ++#include ++#endif // __wasi__ + #if HAVE_ARPA_INET_H + #include + #endif +@@ -155,7 +158,9 @@ PHPAPI void php_network_freeaddresses(struct sockaddr **sal) + */ + PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, zend_string **error_string) + { +-#ifndef WASM_WASI ++#if defined(__wasi__) && !defined(WASM_RUNTIME_WASMEDGE) ++ return 0; ++#else + struct sockaddr **sap; + int n; + #if HAVE_GETADDRINFO +@@ -278,9 +283,8 @@ PHPAPI int php_network_getaddresses(const char *host, int socktype, struct socka + + *sap = NULL; + return n; +-#else +- return 0; +-#endif // WASM_WASI ++#endif // !__wasi__ ++ + } + /* }}} */ + +@@ -316,14 +320,21 @@ PHPAPI int php_network_connect_socket(php_socket_t sockfd, + zend_string **error_string, + int *error_code) + { +-#ifndef WASM_WASI ++#if defined(__wasi__) && !defined(WASM_RUNTIME_WASMEDGE) ++ return 0; ++#else + php_non_blocking_flags_t orig_flags; + int n; + int error = 0; + socklen_t len; + int ret = 0; + ++#if !defined(WASM_RUNTIME_WASMEDGE) ++ // This always sets the O_NONBLOCK flag to the fd, even if asynchronous == false. ++ // connect will return EINPROGRESS, but we don't expect that when asynchronous == false ++ // Not sure how this works on Linux, but fails 1/1 on WasmEdge + SET_SOCKET_BLOCKING_MODE(sockfd, orig_flags); ++#endif + + if ((n = connect(sockfd, addr, addrlen)) != 0) { + error = php_socket_errno(); +@@ -394,6 +405,7 @@ PHPAPI int php_network_connect_socket(php_socket_t sockfd, + } + } + return ret; ++#endif + } + /* }}} */ + +@@ -410,9 +422,6 @@ static inline void sub_times(struct timeval a, struct timeval b, struct timeval + result->tv_sec++; + result->tv_usec -= 1000000L; + } +-#else +- return 0; +-#endif // WASM_WASI + } + /* }}} */ + +@@ -457,11 +466,11 @@ php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned po + } + + /* create a socket for this address */ +-#ifndef WASM_WASI ++#if !defined(__wasi__) || defined(WASM_RUNTIME_WASMEDGE) + sock = socket(sa->sa_family, socktype, 0); + #else + sock = SOCK_ERR; +-#endif // WASM_WASI ++#endif // __wasi__ + + if (sock == SOCK_ERR) { + continue; +@@ -504,11 +513,11 @@ php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned po + } + #endif + +-#ifndef WASM_WASI ++#if !defined(__wasi__) || defined(WASM_RUNTIME_WASMEDGE) + n = bind(sock, sa, socklen); + #else + n = SOCK_CONN_ERR; +-#endif // WASM_WASI ++#endif // __wasi__ + + if (n != SOCK_CONN_ERR) { + goto bound; +@@ -1376,7 +1385,7 @@ struct hostent * gethostname_re (const char *host,struct hostent *hostbuf,char * + #endif + + PHPAPI struct hostent* php_network_gethostbyname(const char *name) { +-#ifndef WASM_WASI ++#ifndef __wasi__ + #if !defined(HAVE_GETHOSTBYNAME_R) + return gethostbyname(name); + #else +@@ -1393,5 +1402,5 @@ PHPAPI struct hostent* php_network_gethostbyname(const char *name) { + #endif + #else + return NULL; +-#endif // WASM_WASI ++#endif // __wasi__ + } +diff --git a/main/streams/xp_socket.c b/main/streams/xp_socket.c +index 5d1039b0c51..4755b1aaab4 100644 +--- a/main/streams/xp_socket.c ++++ b/main/streams/xp_socket.c +@@ -219,9 +219,9 @@ static int php_sockop_close(php_stream *stream, int close_handle) + n = php_pollfd_for_ms(sock->socket, POLLOUT, 500); + } while (n == -1 && php_socket_errno() == EINTR); + #endif +-#ifndef WASM_WASI ++#if !defined(__wasi__) || defined(WASM_RUNTIME_WASMEDGE) + closesocket(sock->socket); +-#endif // WASM_WASI ++#endif // __wasi__ + sock->socket = SOCK_ERR; + } + +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0016-fix-implicit-function-declaration-for-bind-when-not-.patch b/php/php-8.2.6/patches/0016-fix-implicit-function-declaration-for-bind-when-not-.patch new file mode 100644 index 0000000..cfcfdc0 --- /dev/null +++ b/php/php-8.2.6/patches/0016-fix-implicit-function-declaration-for-bind-when-not-.patch @@ -0,0 +1,43 @@ +From e4dc97b18b02098b21883132c3d07c883ad11c59 Mon Sep 17 00:00:00 2001 +From: "no-reply@wasmlabs.dev" +Date: Fri, 7 Apr 2023 08:51:02 +0300 +Subject: [PATCH 16/18] fix: implicit-function-declaration for bind when not on + a wasmedge build + + 100.0% main/ + + 100.0% main/ +diff --git a/main/network.c b/main/network.c +index bb39f27e04b..3c7b9be3e3e 100644 +--- a/main/network.c ++++ b/main/network.c +@@ -836,6 +836,9 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short + int *error_code, const char *bindto, unsigned short bindport, long sockopts + ) + { ++#if defined(__wasi__) && !defined(WASM_RUNTIME_WASMEDGE) ++ return -1; ++#else + int num_addrs, n, fatal = 0; + php_socket_t sock; + struct sockaddr **sal, **psal, *sa; +@@ -940,7 +943,7 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short + int val = 1; + #ifndef __wasi__ + (void) setsockopt(sock, SOL_IP, IP_BIND_ADDRESS_NO_PORT, &val, sizeof(val)); +-#endif // !defined(__wasi__)o ++#endif // __wasi__ + } + #endif + if (local_address_len == 0) { +@@ -1021,6 +1024,7 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short + php_network_freeaddresses(psal); + + return sock; ++#endif // __wasi__ + } + /* }}} */ + +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0017-fix-Patch-for-is_readable-and-is_writable-to-bypass-.patch b/php/php-8.2.6/patches/0017-fix-Patch-for-is_readable-and-is_writable-to-bypass-.patch new file mode 100644 index 0000000..df6a0a3 --- /dev/null +++ b/php/php-8.2.6/patches/0017-fix-Patch-for-is_readable-and-is_writable-to-bypass-.patch @@ -0,0 +1,37 @@ +From b87bd9c6bc1ee596da66eb1757bc3d3c8a2d371c Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Jesu=CC=81s=20Gonza=CC=81lez?= +Date: Wed, 12 Apr 2023 18:01:06 +0200 +Subject: [PATCH 17/18] fix: Patch for `is_readable()` and `is_writable()` to + bypass WASI preview1 limitations with stating files + + + 100.0% ext/standard/ +diff --git a/ext/standard/filestat.c b/ext/standard/filestat.c +index 2fdb9d765c6..6eddeb15a74 100644 +--- a/ext/standard/filestat.c ++++ b/ext/standard/filestat.c +@@ -950,12 +950,21 @@ PHPAPI void php_stat(zend_string *filename, int type, zval *return_value) + } + php_error_docref(NULL, E_NOTICE, "Unknown file type (%d)", ssb.sb.st_mode&S_IFMT); + RETURN_STRING("unknown"); ++#ifndef __wasi__ + case FS_IS_W: + RETURN_BOOL((ssb.sb.st_mode & wmask) != 0); + case FS_IS_R: + RETURN_BOOL((ssb.sb.st_mode&rmask)!=0); + case FS_IS_X: + RETURN_BOOL((ssb.sb.st_mode&xmask)!=0); ++#else ++ case FS_IS_W: ++ RETURN_TRUE; ++ case FS_IS_R: ++ RETURN_TRUE; ++ case FS_IS_X: ++ RETURN_TRUE; ++#endif /* __wasi__ */ + case FS_IS_FILE: + RETURN_BOOL(S_ISREG(ssb.sb.st_mode)); + case FS_IS_DIR: +-- +2.38.1 + diff --git a/php/php-8.2.6/patches/0018-fix-random_bytes-failing-on-Windows.patch b/php/php-8.2.6/patches/0018-fix-random_bytes-failing-on-Windows.patch new file mode 100644 index 0000000..92522ac --- /dev/null +++ b/php/php-8.2.6/patches/0018-fix-random_bytes-failing-on-Windows.patch @@ -0,0 +1,48 @@ +From 5ac48070ba9f27c2f3f02a39788406d13b0ec954 Mon Sep 17 00:00:00 2001 +From: "no-reply@wasmlabs.dev" +Date: Thu, 13 Apr 2023 10:21:04 +0200 +Subject: [PATCH 18/18] fix random_bytes failing on Windows + + 100.0% ext/random/ + + 100.0% ext/random/ +diff --git a/ext/random/random.c b/ext/random/random.c +index dda57f0fe7d..687ab43a34c 100644 +--- a/ext/random/random.c ++++ b/ext/random/random.c +@@ -510,7 +510,7 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) + #else + size_t read_bytes = 0; + ssize_t n; +-# if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || (defined(__DragonFly__) && __DragonFly_version >= 500700) || defined(__sun) ++# if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || (defined(__DragonFly__) && __DragonFly_version >= 500700) || defined(__sun) || defined(__wasi__) + /* Linux getrandom(2) syscall or FreeBSD/DragonFlyBSD getrandom(2) function*/ + /* Keep reading until we get enough entropy */ + while (read_bytes < size) { +@@ -527,6 +527,14 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) + size_t amount_to_read = size - read_bytes; + # if defined(__linux__) + n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0); ++# elif defined(__wasi__) ++ // getentropy always reads the amount requested on success (0) ++ // and returns -1 otherwise ++ if (getentropy(bytes + read_bytes, amount_to_read) == 0) { ++ n = amount_to_read; ++ } else { ++ n = -1; ++ } + # else + n = getrandom(bytes + read_bytes, amount_to_read, 0); + # endif +@@ -560,7 +568,7 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) + + if (fd < 0) { + errno = 0; +-# if HAVE_DEV_URANDOM ++# if defined(HAVE_DEV_URANDOM) && !defined(__wasi__) + fd = open("/dev/urandom", O_RDONLY); + # endif + if (fd < 0) { +-- +2.38.1 + diff --git a/php/php-8.2.6/wlr-build.sh b/php/php-8.2.6/wlr-build.sh new file mode 100644 index 0000000..f39bac0 --- /dev/null +++ b/php/php-8.2.6/wlr-build.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env bash +logStatus "Building 'php/php-8.2.6'" + +if [[ ! -v WLR_ENV ]] +then + echo "WLR build environment is not set" + exit 1 +fi + +# Optimization is disabled during build as we might do some instrumentation at the end +export CFLAGS_CONFIG="-O0" + +########## Setup the wasi related flags ############# +export CFLAGS_WASI="--sysroot=${WASI_SYSROOT} -D_WASI_EMULATED_GETPID -D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_PROCESS_CLOCKS" +export LDFLAGS_WASI="--sysroot=${WASI_SYSROOT} -lwasi-emulated-getpid -lwasi-emulated-signal -lwasi-emulated-process-clocks" + +########## Setup the flags for php ############# +export CFLAGS_PHP='-D_POSIX_SOURCE=1 -D_GNU_SOURCE=1 -DHAVE_FORK=0 -DPNG_USER_CONFIG -DWASM_WASI' + +export LDFLAGS_WARNINGS='-Wno-unused-command-line-argument -Werror=implicit-function-declaration -Wno-incompatible-function-pointer-types' + +# We need to add LDFLAGS ot CFLAGS because autoconf compiles(+links) to binary when checking stuff +export LDFLAGS="${LDFLAGS_WASI} ${LDFLAGS_DEPENDENCIES} ${LDFLAGS_WARNINGS}" +export CFLAGS="${CFLAGS_CONFIG} ${CFLAGS_WASI} ${CFLAGS_DEPENDENCIES} ${CFLAGS_PHP} ${LDFLAGS}" + +logStatus "CFLAGS="${CFLAGS} +logStatus "LDFLAGS="${LDFLAGS} + + +cd "${WLR_SOURCE_PATH}" + +if [[ -z "$WLR_SKIP_CONFIGURE" ]]; then + logStatus "Generating configure script..." + ./buildconf --force || exit 1 + + export PHP_CONFIGURE='--without-iconv --without-openssl --without-pear --disable-phar --disable-opcache --disable-zend-signals --without-pcre-jit --with-sqlite3 --enable-pdo --with-pdo-sqlite --enable-mbstring --enable-gd --disable-fiber-asm --with-jpeg' + + if [[ "${WLR_BUILD_FLAVOR}" == *"wasmedge"* ]] + then + export PHP_CONFIGURE="${PHP_CONFIGURE} --enable-mysqlnd --with-pdo-mysql --with-mysqli" + export PHP_CONFIGURE="--with-wasm-runtime=wasmedge ${PHP_CONFIGURE}" + fi + + logStatus "Configuring build with '${PHP_CONFIGURE}'... " + ./configure --config-cache --host=wasm32-wasi host_alias=wasm32-musl-wasi --target=wasm32-wasi target_alias=wasm32-musl-wasi ${PHP_CONFIGURE} || exit 1 +else + logStatus "Skipping configure..." +fi + +export MAKE_TARGETS='cgi' +if [[ "${WLR_BUILD_FLAVOR}" == *"wasmedge"* ]] +then + export MAKE_TARGETS="${MAKE_TARGETS} cli" +fi + +logStatus "Building '${MAKE_TARGETS}'..." +# By exporting WLR_SKIP_WASM_OPT envvar during the build, the +# wasm-opt wrapper in the wasm-base image will be a dummy wrapper that +# is effectively a NOP. +# +# This is due to https://github.com/llvm/llvm-project/issues/55781, so +# that we get to choose which optimization passes are executed after +# the artifacts have been built. +export WLR_SKIP_WASM_OPT=1 +make -j ${MAKE_TARGETS} || exit 1 +unset WLR_SKIP_WASM_OPT + +logStatus "Preparing artifacts..." +mkdir -p ${WLR_OUTPUT}/bin 2>/dev/null || exit 1 + +WASM_OPT_ARGS=-O3 +# WASM_OPT_ARGS="${WASM_OPT_ARGS} --asyncify --pass-arg=asyncify-ignore-imports" + +PHP_CGI_TARGET="${WLR_OUTPUT}/bin/php-cgi${WLR_BUILD_FLAVOR:+-$WLR_BUILD_FLAVOR}.wasm" +logStatus "Running wasm-opt with '${WASM_OPT_ARGS}' on php-cgi..." +wasm-opt ${WASM_OPT_ARGS} -o "${PHP_CGI_TARGET}" sapi/cgi/php-cgi || exit 1 + +if [[ "${WLR_BUILD_FLAVOR}" == *"wasmedge"* ]] +then + PHP_CLI_TARGET="${WLR_OUTPUT}/bin/php${WLR_BUILD_FLAVOR:+-$WLR_BUILD_FLAVOR}.wasm" + logStatus "Running wasm-opt with '${WASM_OPT_ARGS}' for ${PHP_CLI_TARGET}..." + wasm-opt ${WASM_OPT_ARGS} -o ${PHP_CLI_TARGET} sapi/cli/php || exit 1 +fi + +logStatus "DONE. Artifacts in ${WLR_OUTPUT}" diff --git a/php/php-8.2.6/wlr-env-repo.sh b/php/php-8.2.6/wlr-env-repo.sh new file mode 100644 index 0000000..442b50e --- /dev/null +++ b/php/php-8.2.6/wlr-env-repo.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +if [[ $1 == "--unset" ]] +then + unset WLR_REPO + unset WLR_REPO_BRANCH + unset WLR_ENV_NAME + unset WLR_PACKAGE_VERSION + unset WLR_PACKAGE_NAME + return +fi + +export WLR_REPO=https://github.com/php/php-src.git +export WLR_REPO_BRANCH=php-8.2.6 +export WLR_ENV_NAME=php/php-8.2.6 +export WLR_PACKAGE_VERSION=8.2.6 +export WLR_PACKAGE_NAME=php diff --git a/php/php-8.2.6/wlr-info.json b/php/php-8.2.6/wlr-info.json new file mode 100644 index 0000000..7c27e31 --- /dev/null +++ b/php/php-8.2.6/wlr-info.json @@ -0,0 +1,36 @@ +{ + "deps": { + "libuuid": { + "build_target": "libs/libuuid/v1.0.3", + "required_file": "lib/wasm32-wasi/libuuid.a" + }, + "zlib": { + "build_target": "libs/zlib/v1.2.13", + "required_file": "lib/wasm32-wasi/libz.a" + }, + "libpng": { + "build_target": "libs/libpng/v1.6.39", + "required_file": "lib/wasm32-wasi/libpng16.a" + }, + "libjpeg": { + "build_target": "libs/libjpeg/v2.1.5.1", + "required_file": "lib/wasm32-wasi/libjpeg.a" + }, + "libxml2": { + "build_target": "libs/libxml2/v2.11.4", + "required_file": "lib/wasm32-wasi/libxml2.a" + }, + "oniguruma": { + "build_target": "libs/oniguruma/v6.9.8", + "required_file": "lib/wasm32-wasi/libonig.a" + }, + "SQLite": { + "build_target": "libs/sqlite/v3.42.0", + "required_file": "lib/wasm32-wasi/libsqlite3.a" + }, + "bzip2": { + "build_target": "libs/bzip2/v1.0.8", + "required_file": "lib/wasm32-wasi/libbz2.a" + } + } +} \ No newline at end of file diff --git a/php/php-8.2.6/wlr-tag.sh b/php/php-8.2.6/wlr-tag.sh new file mode 100644 index 0000000..8d0073c --- /dev/null +++ b/php/php-8.2.6/wlr-tag.sh @@ -0,0 +1 @@ +export WLR_TAG="php/8.2.6" diff --git a/php/php-8.2.6/wlr-test.sh b/php/php-8.2.6/wlr-test.sh new file mode 100644 index 0000000..55b17ec --- /dev/null +++ b/php/php-8.2.6/wlr-test.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +if [[ ! -v WLR_ENV ]] +then + echo "WLR build environment is not set" + exit 1 +fi + +if ! [ -x "$(command -v php)" ] +then + echo "Native php is required in PATH on the host to act as orchestrator for php.wasm tests!" + exit 1 +fi + +if [[ "${WLR_BUILD_FLAVOR}" == *"wasmedge"* ]] +then + WLR_BINARY_WUFFIX=-wasmedge +fi + +if [ -f "${WLR_OUTPUT}/bin/php${WLR_BINARY_WUFFIX}" ] +then + export WLR_TESTED_MODULE="${WLR_OUTPUT}/bin/php${WLR_BINARY_WUFFIX}.wasm" +else + export WLR_TESTED_MODULE="${WLR_OUTPUT}/bin/php-cgi${WLR_BINARY_WUFFIX}.wasm" +fi + +if ! [ -f "${WLR_TESTED_MODULE}" ] +then + echo "WASM module not found at '${WLR_TESTED_MODULE}'" + exit 1 +fi + +cd "${WLR_SOURCE_PATH}" +echo "Calling 'WLR_TESTED_MODULE=${WLR_TESTED_MODULE} php -f run-tests.php -- -p ${WLR_TEST_RUNTIME_WRAPPER} -j6' to run tests..." +php -f run-tests.php -- -p ${WLR_TEST_RUNTIME_WRAPPER} -j6 \ + tests/lang \ + tests/output \ + tests/strings \ + Zend/tests/fibers diff --git a/python/test/run_me.sh b/python/test/run_me.sh new file mode 100755 index 0000000..6e3591b --- /dev/null +++ b/python/test/run_me.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +if [[ "$(realpath $PWD)" != "$(realpath $(dirname $BASH_SOURCE))" ]] +then + echo "This script works only if called from its location as PWD" + exit 1 +fi + +TESTS=$(for t in */test.py; do echo $(dirname $t); done) + +status=0 + +for test_dir in "$TESTS"; do + echo "Running test in '${test_dir}'..." + if diff ${test_dir}/test.stdout <($WLR_TEST_RUNTIME \ + --mapdir /usr::${WLR_OUTPUT}/usr \ + --mapdir /test::./${test_dir} \ + --mapdir .::./${test_dir} \ + ${WLR_TESTED_MODULE} \ + /test/test.py); then + echo "${test_dir}/test.py passed!" + else + echo "${test_dir}/test.py failed!" + status=$(expr ${status} + 1) + fi +done + +exit $status diff --git a/python/test/sqlite/.gitignore b/python/test/sqlite/.gitignore new file mode 100644 index 0000000..3997bea --- /dev/null +++ b/python/test/sqlite/.gitignore @@ -0,0 +1 @@ +*.db \ No newline at end of file diff --git a/python/test/sqlite/test.py b/python/test/sqlite/test.py new file mode 100755 index 0000000..ce2d845 --- /dev/null +++ b/python/test/sqlite/test.py @@ -0,0 +1,74 @@ +import traceback + +import sqlite3 +import sys + +DB_FILE_NAME = 'test.db' + +def create_table(con, name, schema): + cursorObj = con.cursor() + cursorObj.execute(f"CREATE TABLE {name}{schema}") + con.commit() + +def populate_table(con, name): + cursorObj = con.cursor() + cursorObj.execute(f"INSERT INTO {name} VALUES(1, 'John', 700, 'TheDarkMaster')") + cursorObj.execute(f"INSERT INTO {name} VALUES(2, 'Jane', 710, 'AngelOfBenevolence')") + cursorObj.execute(f"INSERT INTO {name} VALUES(3, 'George', 623, 'MagiFromTheDeep')") + con.commit() + +def select_table(con, name): + cursorObj = con.cursor() + cursorObj.execute(f"SELECT * FROM {name}") + rows = cursorObj.fetchall() + for row in rows: + print(row) + +def sql_version(con): + cursor = con.cursor() + + sqlite_select_Query = "select sqlite_version();" + cursor.execute(sqlite_select_Query) + record = cursor.fetchall() + print("SQLite Database Version is: ", record) + cursor.close() + +connection = None +try: + connection = sqlite3.connect(DB_FILE_NAME) + print("Database created and Successfully Connected to SQLite") + + sql_version(connection) + + try: + create_table(connection, "players", "(id integer PRIMARY KEY, name text, score real, nickname text)") + except sqlite3.Error as e: + print("Error while trying to create table: ", e) + + try: + populate_table(connection, "players") + except sqlite3.Error as e: + print("Error while trying to create table: ", e) + + + try: + select_table(connection, "players") + + except sqlite3.Error as e: + print("Error while trying to fetch table: ", e) + + +except sqlite3.Error as error: + print("Error while connecting to sqlite", error) + + print(traceback.format_exc()) + +finally: + if connection: + connection.close() + print("The SQLite connection is closed") + import os + if os.path.isfile(DB_FILE_NAME): + os.remove(DB_FILE_NAME) + print(f"File '{DB_FILE_NAME}' deleted!") + diff --git a/python/test/sqlite/test.stdout b/python/test/sqlite/test.stdout new file mode 100644 index 0000000..da77e46 --- /dev/null +++ b/python/test/sqlite/test.stdout @@ -0,0 +1,7 @@ +Database created and Successfully Connected to SQLite +SQLite Database Version is: [('3.42.0',)] +(1, 'John', 700.0, 'TheDarkMaster') +(2, 'Jane', 710.0, 'AngelOfBenevolence') +(3, 'George', 623.0, 'MagiFromTheDeep') +The SQLite connection is closed +File 'test.db' deleted! diff --git a/python/v3.11.1/wlr-build-deps.sh b/python/v3.11.1/wlr-build-deps.sh deleted file mode 100644 index 07873b5..0000000 --- a/python/v3.11.1/wlr-build-deps.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env bash - -if [ "${BASH_SOURCE-}" = "$0" ]; then - echo "You must source this script to add to CFLAGS and LDFLAGS: \$ source $0" >&2 - return -fi - -source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_dependencies.sh - -wlr_dependencies_add "uuid" "libs/uuid/libuuid-1.0.3" "lib/wasm32-wasi/libuuid.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Flibuuid%2F1.0.3%2B20230310-c46e363/libuuid-1.0.3-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "zlib" "libs/zlib/v1.2.13" "lib/wasm32-wasi/libz.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Fzlib%2F1.2.13%2B20230310-c46e363/libz-1.2.13-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "SQLite" "libs/sqlite/version-3.41.2" "lib/wasm32-wasi/libsqlite3.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Fsqlite%2F3.41.2%2B20230329-43f9aea/libsqlite-3.41.2-wasi-sdk-19.0.tar.gz" diff --git a/python/v3.11.1/wlr-info.json b/python/v3.11.1/wlr-info.json new file mode 100644 index 0000000..89fbd15 --- /dev/null +++ b/python/v3.11.1/wlr-info.json @@ -0,0 +1,16 @@ +{ + "deps": { + "zlib": { + "build_target": "libs/zlib/v1.2.13", + "required_file": "lib/wasm32-wasi/libz.a" + }, + "libuuid": { + "build_target": "libs/libuuid/v1.0.3", + "required_file": "lib/wasm32-wasi/libuuid.a" + }, + "bzip2": { + "build_target": "libs/bzip2/v1.0.8", + "required_file": "lib/wasm32-wasi/libbz2.a" + } + } +} \ No newline at end of file diff --git a/python/v3.11.3/wlr-build-deps.sh b/python/v3.11.3/wlr-build-deps.sh deleted file mode 100644 index 7747de2..0000000 --- a/python/v3.11.3/wlr-build-deps.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env bash - -if [ "${BASH_SOURCE-}" = "$0" ]; then - echo "You must source this script to add to CFLAGS and LDFLAGS: \$ source $0" >&2 - return -fi - -source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_dependencies.sh - -wlr_dependencies_add "uuid" "libs/uuid/libuuid-1.0.3" "lib/wasm32-wasi/libuuid.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Flibuuid%2F1.0.3%2B20230310-c46e363/libuuid-1.0.3-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "zlib" "libs/zlib/v1.2.13" "lib/wasm32-wasi/libz.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Fzlib%2F1.2.13%2B20230310-c46e363/libz-1.2.13-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "SQLite" "libs/sqlite/version-3.41.2" "lib/wasm32-wasi/libsqlite3.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Fsqlite%2F3.41.2%2B20230329-43f9aea/libsqlite-3.41.2-wasi-sdk-19.0.tar.gz" - -wlr_dependencies_add "bzip2" "libs/bzip2/v1.0.8" "lib/wasm32-wasi/libbz2.a" \ - "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/libs%2Fbzip2%2F1.0.8%2B20230425-e1a7579/libbzip2-1.0.8-wasi-sdk-19.0.tar.gz" diff --git a/python/v3.11.3/wlr-info.json b/python/v3.11.3/wlr-info.json new file mode 100644 index 0000000..0864b71 --- /dev/null +++ b/python/v3.11.3/wlr-info.json @@ -0,0 +1,20 @@ +{ + "deps": { + "zlib": { + "build_target": "libs/zlib/v1.2.13", + "required_file": "lib/wasm32-wasi/libz.a" + }, + "SQLite": { + "build_target": "libs/sqlite/v3.42.0", + "required_file": "lib/wasm32-wasi/libsqlite3.a" + }, + "libuuid": { + "build_target": "libs/libuuid/v1.0.3", + "required_file": "lib/wasm32-wasi/libuuid.a" + }, + "bzip2": { + "build_target": "libs/bzip2/v1.0.8", + "required_file": "lib/wasm32-wasi/libbz2.a" + } + } +} \ No newline at end of file diff --git a/python/v3.11.4/wlr-build.sh b/python/v3.11.4/wlr-build.sh new file mode 100644 index 0000000..5ddb46a --- /dev/null +++ b/python/v3.11.4/wlr-build.sh @@ -0,0 +1,132 @@ +#!/usr/bin/env bash + +if [[ ! -v WLR_ENV ]] +then + echo "WLR build environment is not set" + exit 1 +fi + +cd "${WLR_SOURCE_PATH}" + +if [[ "${WLR_BUILD_FLAVOR}" == *"aio"* ]] +then + source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_wasi_vfs.sh + wlr_wasi_vfs_setup_dependencies || exit 1 +fi + +source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_pkg_config.sh + +export CFLAGS_CONFIG="-O0" + + +# This fails with upgraded clang for wasi-sdk19 and later. Disabled on cpython main. +# +# PyModule_AddIntMacro(module, CLOCK_MONOTONIC) and the like cause this. +# In all POSIX variants CLOCK_MONOTONIC is a numeric constant, so python imports it as int macro +# However, in wasi-libc clockid_t is defined as a pointer to struct __clockid. + +export CFLAGS_CONFIG="${CFLAGS_CONFIG} -Wno-int-conversion" + +export CFLAGS="${CFLAGS_CONFIG} ${CFLAGS_DEPENDENCIES} ${CFLAGS}" +export LDFLAGS="${LDFLAGS_DEPENDENCIES} ${LDFLAGS}" + +export PYTHON_WASM_CONFIGURE="--with-build-python=python3" + +if [[ "${WLR_BUILD_FLAVOR}" == *"wasmedge"* ]] +then + if [[ ! -v WABT_ROOT ]] + then + echo "WABT_ROOT is needed to patch imports for wasmedge" + exit 1 + fi +fi + +# By exporting WLR_SKIP_WASM_OPT envvar during the build, the +# wasm-opt wrapper in the wasm-base image will be a dummy wrapper that +# is effectively a NOP. +# +# This is due to https://github.com/llvm/llvm-project/issues/55781, so +# that we get to choose which optimization passes are executed after +# the artifacts have been built. +export WLR_SKIP_WASM_OPT=1 + +if [[ -z "$WLR_SKIP_CONFIGURE" ]]; then + logStatus "Configuring build with '${PYTHON_WASM_CONFIGURE}'... " + CONFIG_SITE=./Tools/wasm/config.site-wasm32-wasi ./configure -C --host=wasm32-wasi --build=$(./config.guess) ${PYTHON_WASM_CONFIGURE} || exit 1 +else + logStatus "Skipping configure..." +fi + +export MAKE_TARGETS='python.wasm wasm_stdlib' + +logStatus "Building '${MAKE_TARGETS}'... " +make -j ${MAKE_TARGETS} || exit 1 + +unset WLR_SKIP_WASM_OPT + +if [[ "${WLR_BUILD_FLAVOR}" == *"aio"* ]] +then + logStatus "Packing with wasi-vfs" + wlr_wasi_vfs_cli pack python.wasm --mapdir /usr::$PWD/usr -o python.wasm || exit 1 +fi + +logStatus "Optimizing python binary..." +wasm-opt -O2 -o python-optimized.wasm python.wasm || exit 1 + +if [[ "${WLR_BUILD_FLAVOR}" == *"wasmedge"* ]] +then + logStatus "Patching python binary for wasmedge..." + ${WLR_REPO_ROOT}/scripts/build-helpers/patch_wasmedge_wat_sock_accept.sh python-optimized.wasm || exit 1 +fi + +logStatus "Preparing artifacts... " +TARGET_PYTHON_BINARY=${WLR_OUTPUT}/bin/python.wasm + +mkdir -p ${WLR_OUTPUT}/bin 2>/dev/null || exit 1 + +if [[ "${WLR_BUILD_FLAVOR}" == *"aio"* ]] +then + cp -v python-optimized.wasm ${TARGET_PYTHON_BINARY} || exit 1 +else + mkdir -p ${WLR_OUTPUT}/usr 2>/dev/null || exit 1 + cp -v python-optimized.wasm ${TARGET_PYTHON_BINARY} || exit 1 + cp -TRv usr ${WLR_OUTPUT}/usr || exit 1 +fi + +if [[ "${WLR_BUILD_FLAVOR}" != *"aio"* && "${WLR_BUILD_FLAVOR}" != *"wasmedge"* ]] +then + + logStatus "Install includes..." + make inclinstall \ + prefix=${WLR_OUTPUT} \ + libdir=${WLR_OUTPUT}/lib/wasm32-wasi \ + pkgconfigdir=${WLR_OUTPUT}/lib/wasm32-wasi/pkgconfig || exit 1 + + logStatus "Create libpython3.11-aio.a" +(${AR} -M </dev/null || exit 1 + cp -v libpython3.11-aio.a ${WLR_OUTPUT}/lib/wasm32-wasi/libpython3.11.a || exit 1 + + logStatus "Generating pkg-config file for libpython3.11.a" + DESCRIPTION="libpython3.11 allows embedding the CPython interpreter" + EXTRA_LINK_FLAGS="-lpython3.11 -Wl,-z,stack-size=524288 -Wl,--stack-first -Wl,--initial-memory=10485760 -lwasi-emulated-getpid -lwasi-emulated-signal -lwasi-emulated-process-clocks" + + PC_INCLUDE_SUBDIR=python3.11 wlr_pkg_config_create_pc_file "libpython3.11" "${WLR_PACKAGE_VERSION}" "${DESCRIPTION}" "${EXTRA_LINK_FLAGS}" || exit 1 + + WLR_PACKAGE_LIB_EXTRA_DIRS=usr wlr_package_lib || exit 1 +fi + +logStatus "DONE. Artifacts in ${WLR_OUTPUT}" diff --git a/python/v3.11.4/wlr-env-repo.sh b/python/v3.11.4/wlr-env-repo.sh new file mode 100644 index 0000000..4d4946b --- /dev/null +++ b/python/v3.11.4/wlr-env-repo.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +if [[ $1 == "--unset" ]] +then + unset WLR_REPO + unset WLR_REPO_BRANCH + unset WLR_ENV_NAME + unset WLR_PACKAGE_VERSION + unset WLR_PACKAGE_NAME + return +fi + +export WLR_REPO=https://github.com/python/cpython +export WLR_REPO_BRANCH=v3.11.4 +export WLR_ENV_NAME=python/v3.11.4 +export WLR_PACKAGE_VERSION=3.11.4 +export WLR_PACKAGE_NAME=python diff --git a/python/v3.11.4/wlr-info.json b/python/v3.11.4/wlr-info.json new file mode 100644 index 0000000..f1dbf90 --- /dev/null +++ b/python/v3.11.4/wlr-info.json @@ -0,0 +1,20 @@ +{ + "deps": { + "libuuid": { + "build_target": "libs/libuuid/v1.0.3", + "required_file": "lib/wasm32-wasi/libuuid.a" + }, + "zlib": { + "build_target": "libs/zlib/v1.2.13", + "required_file": "lib/wasm32-wasi/libz.a" + }, + "SQLite": { + "build_target": "libs/sqlite/v3.42.0", + "required_file": "lib/wasm32-wasi/libsqlite3.a" + }, + "bzip2": { + "build_target": "libs/bzip2/v1.0.8", + "required_file": "lib/wasm32-wasi/libbz2.a" + } + } +} \ No newline at end of file diff --git a/python/v3.11.4/wlr-tag.sh b/python/v3.11.4/wlr-tag.sh new file mode 100644 index 0000000..dcee87a --- /dev/null +++ b/python/v3.11.4/wlr-tag.sh @@ -0,0 +1 @@ +export WLR_TAG="python/3.11.4" diff --git a/python/v3.11.4/wlr-test.sh b/python/v3.11.4/wlr-test.sh new file mode 100644 index 0000000..1fe937c --- /dev/null +++ b/python/v3.11.4/wlr-test.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash + +if [[ ! -v WLR_ENV ]] +then + echo "WLR build environment is not set" + exit 1 +fi + +if [[ "${WLR_BUILD_FLAVOR}" == *"wasmedge"* ]] +then + WLR_BINARY_WUFFIX=-wasmedge +fi + +export WLR_TESTED_MODULE="${WLR_OUTPUT}/bin/python${WLR_BINARY_WUFFIX}.wasm" +export TEST_ROOT=${WLR_ENV}/../test + +(cd ${TEST_ROOT}; ./run_me.sh) + + +# if ! [ -x "$(command -v php)" ] +# then +# echo "Native php is required in PATH on the host to act as orchestrator for php.wasm tests!" +# exit 1 +# fi + + + +# if [ -f "${WLR_OUTPUT}/bin/php${WLR_BINARY_WUFFIX}" ] +# then +# export WLR_TESTED_MODULE="${WLR_OUTPUT}/bin/php${WLR_BINARY_WUFFIX}.wasm" +# else +# export WLR_TESTED_MODULE="${WLR_OUTPUT}/bin/php-cgi${WLR_BINARY_WUFFIX}.wasm" +# fi + +# if ! [ -f "${WLR_TESTED_MODULE}" ] +# then +# echo "WASM module not found at '${WLR_TESTED_MODULE}'" +# exit 1 +# fi + +# cd "${WLR_SOURCE_PATH}" +# echo "Calling 'WLR_TESTED_MODULE=${WLR_TESTED_MODULE} php -f run-tests.php -- -p ${WLR_TEST_RUNTIME_WRAPPER} -j6' to run tests..." +# php -f run-tests.php -- -p ${WLR_TEST_RUNTIME_WRAPPER} -j6 \ +# tests/lang \ +# tests/output \ +# tests/strings \ +# Zend/tests/fibers diff --git a/ruby/v3_2_2/wlr-build.sh b/ruby/v3_2_2/wlr-build.sh new file mode 100644 index 0000000..51475fb --- /dev/null +++ b/ruby/v3_2_2/wlr-build.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +if [[ ! -v WLR_ENV ]] +then + echo "WLR build environment is not set" + exit 1 +fi + +cd "${WLR_SOURCE_PATH}" + +export PREFIX=/wlr-rubies +export XLDFLAGS="/wasi-vfs/lib/libwasi_vfs.a $XLDFLAGS" + +if [[ -z "$WLR_SKIP_CONFIGURE" ]]; then + logStatus "Downloading autotools data... " + ruby tool/downloader.rb -d tool -e gnu config.guess config.sub + + logStatus "Generating configure script... " + ./autogen.sh + + logStatus "Configuring ruby..." + ./configure \ + --host wasm32-unknown-wasi \ + --prefix=$PREFIX \ + --with-ext=bigdecimal,cgi/escape,continuation,coverage,date,dbm,digest/bubblebabble,digest,digest/md5,digest/rmd160,digest/sha1,digest/sha2,etc,fcntl,fiber,gdbm,json,json/generator,json/parser,nkf,objspace,pathname,racc/cparse,rbconfig/sizeof,ripper,stringio,strscan,monitor \ + --with-static-linked-ext \ + --disable-install-doc \ + LDFLAGS=" \ + -Xlinker --stack-first \ + -Xlinker -z -Xlinker stack-size=16777216 \ + " \ + optflags="-O2" \ + debugflags="" \ + wasmoptflags="-O2" +else + logStatus "Skipping configure..." +fi + +logStatus "Building ruby..." +make install + +logStatus "Preparing artifacts... " +mkdir -p ${WLR_OUTPUT}/bin 2>/dev/null || exit 1 +mv $PREFIX/bin/ruby ruby +rm -rf $PREFIX/bin +wasi-vfs pack ruby --mapdir $PREFIX::$PREFIX -o ${WLR_OUTPUT}/bin/ruby.wasm || exit 1 + +logStatus "DONE. Artifacts in ${WLR_OUTPUT}" diff --git a/ruby/v3_2_2/wlr-env-repo.sh b/ruby/v3_2_2/wlr-env-repo.sh new file mode 100644 index 0000000..067ff7b --- /dev/null +++ b/ruby/v3_2_2/wlr-env-repo.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +if [[ $1 == "--unset" ]] +then + unset WLR_REPO + unset WLR_REPO_BRANCH + unset WLR_ENV_NAME + unset WLR_PACKAGE_VERSION + unset WLR_PACKAGE_NAME + return +fi + +export WLR_REPO=https://github.com/ruby/ruby.git +export WLR_REPO_BRANCH=v3_2_2 +export WLR_ENV_NAME=ruby/v3_2_2 +export WLR_PACKAGE_VERSION=3.2.2 +export WLR_PACKAGE_NAME=ruby diff --git a/ruby/v3_2_2/wlr-tag.sh b/ruby/v3_2_2/wlr-tag.sh new file mode 100644 index 0000000..041351b --- /dev/null +++ b/ruby/v3_2_2/wlr-tag.sh @@ -0,0 +1 @@ +export WLR_TAG="ruby/3.2.2" diff --git a/scripts/build-helpers/wlr_dependencies.sh b/scripts/build-helpers/wlr_dependencies.sh index d02b77a..b218d76 100644 --- a/scripts/build-helpers/wlr_dependencies.sh +++ b/scripts/build-helpers/wlr_dependencies.sh @@ -39,6 +39,10 @@ function wlr_dependencies_add { fi if [[ -v _DEP_URL && "${WLR_DEPS_FORCE_LOCAL}" != *"${_NAME}"* ]]; then + if [[ "${_DEP_URL}" != *"${WASI_SDK_ASSET_NAME}"* ]]; then + logStatus "Trying to get ${_NAME} from asset which is not built with '${WASI_SDK_ASSET_NAME}': '${_DEP_URL}'" + exit 1 + fi logStatus "Downloading ${_NAME} dependency from ${_DEP_URL}..." echo "curl -sL \"${_DEP_URL}\" | tar xzv -C \"${WLR_DEPS_ROOT}/build-output/\"" curl -sL "${_DEP_URL}" | tar xzv -C "${WLR_DEPS_ROOT}/build-output/" @@ -53,3 +57,25 @@ function wlr_dependencies_add { $WLR_MAKE "${WLR_REPO_ROOT}/${_BUILD_COMMAND}" || exit 1 fi } + +function wlr_dependencies_import { + local _DEPS_FILE=$1 + + if [ ! -f "${_DEPS_FILE}" ]; then + echo "Missing dependencies file '${_DEPS_FILE}'" + exit 1 + fi + + for dependency in $(jq '.deps | keys | join(" ")' -r $_DEPS_FILE); do + local _NAME=$dependency + local _BUILD_TARGET=$(jq ".deps.${_NAME}.build_target" -r $_DEPS_FILE) + local _REQUIRED_FILE=$(jq ".deps.${_NAME}.required_file" -r $_DEPS_FILE) + local _URL=$(jq ".deps.${_NAME}.url" -r $_DEPS_FILE) + + if [ "${_URL}" = "null" ]; then + unset _URL + fi + + wlr_dependencies_add "${_NAME}" "${_BUILD_TARGET}" "${_REQUIRED_FILE}" "${_URL}" + done +} diff --git a/scripts/wlr-build.sh b/scripts/wlr-build.sh index 91d2eaa..493b1d6 100755 --- a/scripts/wlr-build.sh +++ b/scripts/wlr-build.sh @@ -51,6 +51,7 @@ for line in $(env | grep -E "WLR_\w+="); do done logStatus WASI_SDK_PATH=${WASI_SDK_PATH} +logStatus WASI_SDK_ASSET_NAME=${WASI_SDK_ASSET_NAME} logStatus BINARYEN_PATH=${BINARYEN_PATH} logStatus WABT_ROOT=${WABT_ROOT} logStatus WASI_VFS_ROOT=${WASI_VFS_ROOT} @@ -63,6 +64,13 @@ export NM=${WASI_SDK_PATH}/bin/llvm-nm export AR=${WASI_SDK_PATH}/bin/llvm-ar export RANLIB=${WASI_SDK_PATH}/bin/llvm-ranlib +logStatus CC+${CC} +logStatus LD+${LD} +logStatus CXX+${CXX} +logStatus NM+${NM} +logStatus AR+${AR} +logStatus RANLIB+${RANLIB} + if ! builtin type -P wasm-opt then logStatus "Using wasm-opt wrapper from ${WLR_REPO_ROOT}/scripts/wrappers" @@ -70,9 +78,10 @@ then fi logStatus "Checking dependencies..." -if [[ -f ${WLR_ENV}/wlr-build-deps.sh ]] +if [[ -f ${WLR_ENV}/wlr-info.json ]] then - source ${WLR_ENV}/wlr-build-deps.sh + source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_dependencies.sh + wlr_dependencies_import ${WLR_ENV}/wlr-info.json fi source ${WLR_REPO_ROOT}/scripts/build-helpers/wlr_package.sh diff --git a/scripts/wlr-tag.sh b/scripts/wlr-tag.sh index 3bdab6f..cf85083 100755 --- a/scripts/wlr-tag.sh +++ b/scripts/wlr-tag.sh @@ -23,6 +23,11 @@ if [ ! -f $1/wlr-tag.sh ]; then exit 1 fi +if [ "$(git branch --show-current)" != "main" ] || [ "${WLR_TAG_ANY_BRANCH}" != "1" ]; then + echo "can only tag/release from 'main' branch. To force run with '${WLR_TAG_ANY_BRANCH}=1 $0'" + exit 1 +fi + source $1/wlr-tag.sh WLR_FINAL_TAG="${WLR_TAG}+${TAG_DATE}-${SHORT_SHA}"