Skip to content

Stop crash with PHP extension update #2165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"prepare": "husky install",
"reset": "nx reset",
"recompile:php": "npm run recompile:php:web && npm run recompile:php:node",
"recompile:php:web": "nx recompile-php:all php-wasm-web ",
"recompile:php:web:jspi:all": "nx recompile-php:jspi:all php-wasm-web",
"recompile:php:web:jspi:8.4": "nx recompile-php:jspi php-wasm-web -- --PHP_VERSION=8.4 ",
"recompile:php:web:jspi:8.3": "nx recompile-php:jspi php-wasm-web -- --PHP_VERSION=8.3 ",
Expand Down
13 changes: 7 additions & 6 deletions packages/php-wasm/compile/base-image/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Originally forked from https://github.com/seanmorris/php-wasm
# ubuntu:lunar supports amd64 and arm64 (Apple Silicon) while
# emscripten/emsdk:3.1.24 supports amd64 only.
FROM ubuntu:lunar as emscripten
# ubuntu:noble supports amd64 and arm64 (Apple Silicon)
FROM ubuntu:noble as emscripten

SHELL ["/bin/bash", "-c"]

Expand Down Expand Up @@ -50,8 +49,8 @@ RUN set -euxo pipefail;\
# https://github.com/WordPress/wordpress-playground/tree/67d916b5eccfe78e26e9c953598cc1a81f316931/packages/php-wasm/compile
RUN ln -s /usr/bin/python3 /usr/bin/python
RUN git clone https://github.com/emscripten-core/emsdk.git && \
./emsdk/emsdk install 3.1.61 && \
/root/emsdk/emsdk activate 3.1.61
./emsdk/emsdk install 3.1.74 && \
/root/emsdk/emsdk activate 3.1.74

RUN mkdir -p /root/lib/lib /root/lib/include /root/lib/share /root/lib/bin

Expand All @@ -62,9 +61,11 @@ for dir in $(ls $1); do \n\
cp -r /root/$1/$dir/* /root/lib/$dir/ 2>/dev/null || :; \n\
done ' > /root/copy-lib.sh
RUN chmod a+x /root/copy-lib.sh
# Script to replace strings in files and exit with a non-zero status if the string is not found.
# Scripts to replace strings in files and exit with a non-zero status if the string is not found.
COPY ./replace.sh /root/replace.sh
RUN chmod a+x /root/replace.sh
COPY ./replace-across-lines.sh /root/replace-across-lines.sh
RUN chmod a+x /root/replace-across-lines.sh

# Patch emcc to allow skipping flags and passing additional flags using environment variables.
#
Expand Down
14 changes: 14 additions & 0 deletions packages/php-wasm/compile/base-image/replace-across-lines.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# This script is used to replace strings in files and exit with a non-zero status if the string is not found.
# This version of the script can do replacements across multiple lines.

perl -0 -pi.bak -e "$1" "$2"
if [ $? -ne 0 ]; then
echo "Failed to replace $1 in file $2 - exiting with non-zero status"
exit 1
fi
if cmp --silent -- "$2" "$2.bak"; then
echo "No matches found for $1 in file $2 - exiting with non-zero status"
exit 1
fi
8 changes: 8 additions & 0 deletions packages/php-wasm/compile/php-wasm-dns-polyfill/config.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dnl config.m4 for extension dns_polyfill

PHP_ARG_ENABLE(dns_polyfill, whether to enable dns_polyfill support,
[ --enable-dns_polyfill Enable dns_polyfill support])

if test "$PHP_WASM_DNS_POLYFILL" != "no"; then
PHP_NEW_EXTENSION(dns_polyfill, dns_polyfill.c, $ext_shared)
fi
5 changes: 5 additions & 0 deletions packages/php-wasm/compile/php-wasm-dns-polyfill/config.w32
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ARG_ENABLE("dns_polyfill", "Enable dns_polyfill support", "no");

if (PHP_WASM_DNS_POLYFILL == "yes") {
EXTENSION("dns_polyfill", "dns_polyfill.c");
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "php_network.h"
#include "zend_API.h"
#include "dns_polyfill.h"
#include "ext/standard/info.h"

#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
Expand Down Expand Up @@ -108,61 +109,11 @@ extern void __res_ndestroy(res_state statp);
#define MAXRESOURCERECORDS 64
#endif /* MAXRESOURCERECORDS */

#ifndef PHP_DNS_A
#define PHP_DNS_A 0x00000001
#endif
#ifndef PHP_DNS_NS
#define PHP_DNS_NS 0x00000002
#endif
#ifndef PHP_DNS_CNAME
#define PHP_DNS_CNAME 0x00000010
#endif
#ifndef PHP_DNS_SOA
#define PHP_DNS_SOA 0x00000020
#endif
#ifndef PHP_DNS_PTR
#define PHP_DNS_PTR 0x00000800
#endif
#ifndef PHP_DNS_HINFO
#define PHP_DNS_HINFO 0x00001000
#endif
#if !defined(PHP_WIN32) && !defined(PHP_DNS_CAA)
#define PHP_DNS_CAA 0x00002000
#endif
#ifndef PHP_DNS_MX
#define PHP_DNS_MX 0x00004000
#endif
#ifndef PHP_DNS_TXT
#define PHP_DNS_TXT 0x00008000
#endif
#ifndef PHP_DNS_A6
#define PHP_DNS_A6 0x01000000
#endif
#ifndef PHP_DNS_SRV
#define PHP_DNS_SRV 0x02000000
#endif
#ifndef PHP_DNS_NAPTR
#define PHP_DNS_NAPTR 0x04000000
#endif
#ifndef PHP_DNS_AAAA
#define PHP_DNS_AAAA 0x08000000
#endif
#ifndef PHP_DNS_ANY
#define PHP_DNS_ANY 0x10000000
#endif
#ifndef PHP_DNS_NUM_TYPES
#define PHP_DNS_NUM_TYPES 13 /* Number of DNS Types Supported by PHP currently */
#endif
#ifndef PHP_DNS_ALL
#define PHP_DNS_ALL (PHP_DNS_A|PHP_DNS_NS|PHP_DNS_CNAME|PHP_DNS_SOA|PHP_DNS_PTR|PHP_DNS_HINFO|PHP_DNS_CAA|PHP_DNS_MX|PHP_DNS_TXT|PHP_DNS_A6|PHP_DNS_SRV|PHP_DNS_NAPTR|PHP_DNS_AAAA)
#endif
typedef union {
HEADER qb1;
uint8_t qb2[65536];
} querybuf;

#define arginfo_checkdnsrr arginfo_dns_check_record

PHP_FUNCTION(dns_check_record)
{
HEADER *hp;
Expand Down Expand Up @@ -232,8 +183,6 @@ PHP_FUNCTION(dns_get_record)

/* {{{ Get MX records corresponding to a given Internet host name */

#define arginfo_getmxrr arginfo_dns_get_mx

PHP_FUNCTION(dns_get_mx)
{
char *hostname;
Expand Down Expand Up @@ -271,3 +220,62 @@ PHP_FUNCTION(dns_get_mx)
RETURN_FALSE;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(dns_polyfill)
{
php_info_print_table_start();
php_info_print_table_row(2, "dns_polyfill support", "enabled");
php_info_print_table_end();
}
/* }}} */

PHP_MINIT_FUNCTION(dns_polyfill)
{
REGISTER_LONG_CONSTANT("DNS_A", PHP_DNS_A, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("DNS_NS", PHP_DNS_NS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("DNS_CNAME", PHP_DNS_CNAME, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("DNS_SOA", PHP_DNS_SOA, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("DNS_PTR", PHP_DNS_PTR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("DNS_HINFO", PHP_DNS_HINFO, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("DNS_CAA", PHP_DNS_CAA, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("DNS_MX", PHP_DNS_MX, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("DNS_TXT", PHP_DNS_TXT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("DNS_SRV", PHP_DNS_SRV, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("DNS_NAPTR", PHP_DNS_NAPTR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("DNS_AAAA", PHP_DNS_AAAA, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("DNS_A6", PHP_DNS_A6, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("DNS_ANY", PHP_DNS_ANY, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("DNS_ALL", PHP_DNS_ALL, CONST_CS | CONST_PERSISTENT);

return SUCCESS;
}

PHP_MSHUTDOWN_FUNCTION(dns_polyfill)
{
return SUCCESS;
}

/* {{{ dns_polyfill_functions[] */
const zend_function_entry dns_polyfill_functions[] = {
ZEND_FE(dns_get_mx, arginfo_dns_get_mx)
ZEND_FALIAS(getmxrr, dns_get_mx, arginfo_getmxrr)
ZEND_FE(dns_check_record, arginfo_dns_check_record)
ZEND_FALIAS(checkdnsrr, dns_check_record, arginfo_checkdnsrr)
ZEND_FE(dns_get_record, arginfo_dns_get_record)
ZEND_FE_END};
/* }}} */

/* {{{ dns_polyfill_module_entry */
zend_module_entry dns_polyfill_module_entry = {
STANDARD_MODULE_HEADER,
"dns_polyfill", /* Extension name */
dns_polyfill_functions, /* zend_function_entry */
PHP_MINIT(dns_polyfill), /* PHP_MINIT - Module initialization */
PHP_MSHUTDOWN(dns_polyfill), /* PHP_MSHUTDOWN - Module shutdown */
NULL, /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(dns_polyfill), /* PHP_MINFO - Module info */
PHP_DNS_POLYFILL_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES};
/* }}} */
87 changes: 87 additions & 0 deletions packages/php-wasm/compile/php-wasm-dns-polyfill/dns_polyfill.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#ifndef PHP_WASM_DNS_POLYFILL_H
#define PHP_WASM_DNS_POLYFILL_H

#ifndef PHP_DNS_A
#define PHP_DNS_A 0x00000001
#endif
#ifndef PHP_DNS_NS
#define PHP_DNS_NS 0x00000002
#endif
#ifndef PHP_DNS_CNAME
#define PHP_DNS_CNAME 0x00000010
#endif
#ifndef PHP_DNS_SOA
#define PHP_DNS_SOA 0x00000020
#endif
#ifndef PHP_DNS_PTR
#define PHP_DNS_PTR 0x00000800
#endif
#ifndef PHP_DNS_HINFO
#define PHP_DNS_HINFO 0x00001000
#endif
#if !defined(PHP_WIN32) && !defined(PHP_DNS_CAA)
#define PHP_DNS_CAA 0x00002000
#endif
#ifndef PHP_DNS_MX
#define PHP_DNS_MX 0x00004000
#endif
#ifndef PHP_DNS_TXT
#define PHP_DNS_TXT 0x00008000
#endif
#ifndef PHP_DNS_A6
#define PHP_DNS_A6 0x01000000
#endif
#ifndef PHP_DNS_SRV
#define PHP_DNS_SRV 0x02000000
#endif
#ifndef PHP_DNS_NAPTR
#define PHP_DNS_NAPTR 0x04000000
#endif
#ifndef PHP_DNS_AAAA
#define PHP_DNS_AAAA 0x08000000
#endif
#ifndef PHP_DNS_ANY
#define PHP_DNS_ANY 0x10000000
#endif
#ifndef PHP_DNS_NUM_TYPES
#define PHP_DNS_NUM_TYPES 13 /* Number of DNS Types Supported by PHP currently */
#endif
#ifndef PHP_DNS_ALL
#define PHP_DNS_ALL (PHP_DNS_A | PHP_DNS_NS | PHP_DNS_CNAME | PHP_DNS_SOA | PHP_DNS_PTR | PHP_DNS_HINFO | PHP_DNS_CAA | PHP_DNS_MX | PHP_DNS_TXT | PHP_DNS_A6 | PHP_DNS_SRV | PHP_DNS_NAPTR | PHP_DNS_AAAA)
#endif

extern zend_module_entry dns_polyfill_module_entry;
#define phpext_dns_polyfill_ptr &dns_polyfill_module_entry

ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_check_record, 0, 0, 1)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, type)
ZEND_END_ARG_INFO()

#define arginfo_checkdnsrr arginfo_dns_check_record

PHP_FUNCTION(dns_check_record);

ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_get_record, 0, 0, 1)
ZEND_ARG_INFO(0, hostname)
ZEND_ARG_INFO(0, type)
ZEND_ARG_ARRAY_INFO(1, authns, 1)
ZEND_ARG_ARRAY_INFO(1, addtl, 1)
ZEND_ARG_INFO(0, raw)
ZEND_END_ARG_INFO()

PHP_FUNCTION(dns_get_record);

ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_get_mx, 0, 0, 2)
ZEND_ARG_INFO(0, hostname)
ZEND_ARG_INFO(1, mxhosts) /* ARRAY_INFO(1, mxhosts, 1) */
ZEND_ARG_INFO(1, weight) /* ARRAY_INFO(1, weight, 1) */
ZEND_END_ARG_INFO()

#define arginfo_getmxrr arginfo_dns_get_mx

PHP_FUNCTION(dns_get_mx);

#define PHP_DNS_POLYFILL_VERSION "1.0.0"

#endif // PHP_WASM_DNS_POLYFILL_H
Loading
Loading