Skip to content

Commit 4dafa96

Browse files
committed
feat(wasm): add comprehensive WASI build with multiple extension dependencies
- Integrate bzip2, libsodium, OpenSSL, libxml2, SQLite, and libzip as build dependencies - Implement caching mechanism for WASI dependencies with version checking and rebuild logic - Add support for sodium, openssl, libxml, dom, SimpleXML, xml, xmlreader, xmlwriter extensions - Enable PDO, pdo_sqlite, zip, bz2, and exif extensions in WASI build configuration - Create download, extract, and build functions for external library dependencies - Add WASI-specific patches for OpenSSL and libxml2 compatibility - Update build script to merge multiple static libraries into libphp.a - Modify configure flags to include new extension dependencies and headers - Add comprehensive test cases for all newly supported extensions - Update ABI version from v6 to v7 to reflect expanded extension support - Add wasm/patches directory to gitignore for patch files
1 parent 9874924 commit 4dafa96

13 files changed

Lines changed: 474 additions & 85 deletions

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,5 @@ tmp-php.ini
323323
!/ext/fileinfo/magicdata.patch
324324
!/ext/lexbor/patches/*.patch
325325
!/ext/pcre/pcre2lib/config.h
326+
!/wasm/patches/*.patch
326327
!/win32/build/Makefile

‎ext/bz2/bz2.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ PHP_FUNCTION(bzdecompress)
549549
if (error == BZ_STREAM_END || error == BZ_OK) {
550550
size = (((uint64_t) bzs.total_out_hi32) << 32U) + bzs.total_out_lo32;
551551
if (UNEXPECTED(size > SIZE_MAX)) {
552-
php_error_docref(NULL, E_WARNING, "Decompressed size too big, max is %zd", SIZE_MAX);
552+
php_error_docref(NULL, E_WARNING, "Decompressed size too big, max is %zu", SIZE_MAX);
553553
zend_string_efree(dest);
554554
RETVAL_LONG(BZ_MEM_ERROR);
555555
} else {

‎ext/openssl/config0.m4‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ PHP_ARG_WITH([openssl],
33
[AS_HELP_STRING([--with-openssl],
44
[Include OpenSSL support (requires OpenSSL >= 1.1.1)])])
55

6+
PHP_ARG_ENABLE([openssl-network],
7+
[whether to enable OpenSSL stream transports],
8+
[AS_HELP_STRING([--disable-openssl-network],
9+
[OPENSSL: Build local cryptography APIs without TLS/SSL stream transports])],
10+
[yes],
11+
[no])
12+
613
PHP_ARG_WITH([system-ciphers],
714
[whether to use system default cipher list instead of hardcoded value],
815
[AS_HELP_STRING([--with-system-ciphers],
@@ -25,13 +32,26 @@ PHP_ARG_WITH([openssl-argon2],
2532
[no])
2633

2734
if test "$PHP_OPENSSL" != "no"; then
28-
PHP_NEW_EXTENSION([openssl],
29-
[openssl.c openssl_pwhash.c openssl_backend_common.c openssl_backend_v1.c openssl_backend_v3.c xp_ssl.c],
30-
[$ext_shared])
35+
openssl_sources="openssl.c openssl_pwhash.c openssl_backend_common.c openssl_backend_v1.c openssl_backend_v3.c"
36+
AS_VAR_IF([PHP_OPENSSL_NETWORK], [no], [
37+
AC_DEFINE([PHP_OPENSSL_CRYPTO_ONLY], [1],
38+
[Define to 1 to build OpenSSL without network stream transports.])
39+
], [
40+
openssl_sources="$openssl_sources xp_ssl.c"
41+
])
42+
PHP_NEW_EXTENSION([openssl], [$openssl_sources], [$ext_shared])
3143
PHP_SUBST([OPENSSL_SHARED_LIBADD])
32-
PHP_SETUP_OPENSSL([OPENSSL_SHARED_LIBADD],
33-
[AC_DEFINE([HAVE_OPENSSL_EXT], [1],
34-
[Define to 1 if the PHP extension 'openssl' is available.])])
44+
AS_VAR_IF([PHP_OPENSSL_NETWORK], [no], [
45+
PKG_CHECK_MODULES([OPENSSL], [libcrypto >= 1.1.1])
46+
PHP_EVAL_INCLINE([$OPENSSL_CFLAGS])
47+
PHP_EVAL_LIBLINE([$OPENSSL_LIBS], [OPENSSL_SHARED_LIBADD])
48+
AC_DEFINE([HAVE_OPENSSL_EXT], [1],
49+
[Define to 1 if the PHP extension 'openssl' is available.])
50+
], [
51+
PHP_SETUP_OPENSSL([OPENSSL_SHARED_LIBADD],
52+
[AC_DEFINE([HAVE_OPENSSL_EXT], [1],
53+
[Define to 1 if the PHP extension 'openssl' is available.])])
54+
])
3555

3656
PHP_CHECK_LIBRARY([crypto], [RAND_egd],
3757
[AC_DEFINE([HAVE_RAND_EGD], [1],

‎ext/openssl/openssl.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ bool php_openssl_check_path_ex(
334334
return false;
335335
}
336336

337+
#ifndef PHP_OPENSSL_CRYPTO_ONLY
337338
static int ssl_stream_data_index;
338339

339340
php_stream* php_openssl_get_stream_from_ssl_handle(const SSL *ssl)
@@ -345,6 +346,7 @@ int php_openssl_get_ssl_stream_data_index(void)
345346
{
346347
return ssl_stream_data_index;
347348
}
349+
#endif
348350

349351
static PHP_INI_MH(OnUpdateLibCtx)
350352
{
@@ -420,6 +422,7 @@ PHP_MINIT_FUNCTION(openssl)
420422

421423
php_openssl_backend_init();
422424

425+
#ifndef PHP_OPENSSL_CRYPTO_ONLY
423426
/* register a resource id number with OpenSSL so that we can map SSL -> stream structures in
424427
* OpenSSL callbacks */
425428
ssl_stream_data_index = SSL_get_ex_new_index(0, "PHP stream index", NULL, NULL, NULL);
@@ -439,6 +442,7 @@ PHP_MINIT_FUNCTION(openssl)
439442

440443
php_register_url_stream_wrapper("https", &php_stream_http_wrapper);
441444
php_register_url_stream_wrapper("ftps", &php_stream_ftp_wrapper);
445+
#endif
442446

443447
REGISTER_INI_ENTRIES();
444448

@@ -495,6 +499,7 @@ PHP_MSHUTDOWN_FUNCTION(openssl)
495499
{
496500
php_openssl_backend_shutdown();
497501

502+
#ifndef PHP_OPENSSL_CRYPTO_ONLY
498503
php_unregister_url_stream_wrapper("https");
499504
php_unregister_url_stream_wrapper("ftps");
500505

@@ -510,6 +515,7 @@ PHP_MSHUTDOWN_FUNCTION(openssl)
510515

511516
/* reinstate the default tcp handler */
512517
php_stream_xport_register("tcp", php_stream_generic_socket_factory);
518+
#endif
513519

514520
UNREGISTER_INI_ENTRIES();
515521

‎ext/openssl/openssl_backend_v1.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ void php_openssl_backend_init(void)
3636
OpenSSL_add_all_digests();
3737
OpenSSL_add_all_algorithms();
3838
SSL_load_error_strings();
39+
#else
40+
#ifdef PHP_OPENSSL_CRYPTO_ONLY
41+
OPENSSL_init_crypto(OPENSSL_INIT_NO_LOAD_CONFIG, NULL);
3942
#else
4043
OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL);
44+
#endif
4145
#endif
4246

4347
php_openssl_backend_init_common();

‎ext/openssl/openssl_backend_v3.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ void php_openssl_backend_init(void)
3131
OSSL_PROVIDER_load(NULL, "default");
3232
#endif
3333

34+
#ifdef PHP_OPENSSL_CRYPTO_ONLY
35+
OPENSSL_init_crypto(OPENSSL_INIT_NO_LOAD_CONFIG, NULL);
36+
#else
3437
OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL);
38+
#endif
3539

3640
php_openssl_backend_init_common();
3741
}
@@ -50,8 +54,10 @@ void php_openssl_backend_init_libctx(struct php_openssl_libctx *ctx)
5054
ctx->custom_libctx = OSSL_LIB_CTX_new();
5155
if (ctx->custom_libctx != NULL) {
5256
/* This is not being checked because there is not much that can be done. */
57+
#ifndef PHP_OPENSSL_CRYPTO_ONLY
5358
CONF_modules_load_file_ex(ctx->custom_libctx, NULL, NULL,
5459
PHP_OPENSSL_DEFAULT_CONF_MFLAGS);
60+
#endif
5561
#ifdef LOAD_OPENSSL_LEGACY_PROVIDER
5662
OSSL_PROVIDER_load(ctx->custom_libctx, "legacy");
5763
OSSL_PROVIDER_load(ctx->custom_libctx, "default");

‎ext/openssl/php_openssl.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ ZEND_END_MODULE_GLOBALS(openssl)
9696
ZEND_TSRMLS_CACHE_EXTERN();
9797
#endif
9898

99+
#ifndef PHP_OPENSSL_CRYPTO_ONLY
99100
php_stream_transport_factory_func php_openssl_ssl_socket_factory;
101+
#endif
100102

101103
void php_openssl_store_errors(void);
102104
void php_openssl_errors_set_mark(void);

‎wasm/README.md‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,33 @@
22

33
本目录只负责将裁剪后的 PHP 及其静态扩展编译为 `wasm32-wasip2`,并安装
44
PHP 头文件和 `libphp.a`。PHP 不依赖 GMP、MPFR、mpdecimal 或 PHPX;这些
5-
依赖由 PHPX 的 WASI 构建负责。PHP 自身使用的 zlib 则由本构建流程下载、
6-
交叉编译并合并进 `libphp.a`,安装结果不需要额外的 `libz.a`。
5+
依赖由 PHPX 的 WASI 构建负责。PHP 扩展所需的第三方库由本构建流程下载、
6+
交叉编译并合并进 `libphp.a`,安装结果不需要额外分发依赖库的 `.a` 文件。
77

88
下载的第三方源码和编译结果默认持久化到
99
`wasm/dependencies/wasm32-wasip2/`。该目录被 Git 忽略,不受 `/tmp` 清理或
10-
系统重启影响。修改 PHP 源码后再次构建会直接复用其中的 zlib;只有 zlib
11-
版本、校验值、目标参数或 WASI 编译器版本发生变化时才会重新编译。
10+
系统重启影响。修改 PHP 源码后再次构建会直接复用依赖;只有版本、校验值、
11+
补丁、目标参数或 WASI 编译器版本发生变化时才会重新编译。
1212

13-
默认扩展包含 `mbstring`(不含 `mbregex`)、`zlib` 和 `fileinfo`。WASI 不支持
14-
创建子进程,因此 `fileinfo` 保留文件及内存数据的 MIME 检测,但不会调用
15-
外部 gzip、bzip2、xz 等命令检查压缩文件内部内容。
13+
默认扩展在原有核心扩展之外包含:
14+
15+
- `mbstring`(不含 `mbregex`)、`zlib`、`fileinfo`;
16+
- `sodium`;
17+
- `openssl` crypto-only 模式;
18+
- `libxml`、`dom`、`SimpleXML`、`xml`、`xmlreader`、`xmlwriter`;
19+
- `PDO`、`pdo_sqlite`;
20+
- `zip`、`bz2`、`exif`。
21+
22+
OpenSSL 只保留本地加密、摘要、签名、密钥与证书处理能力,不编译
23+
`xp_ssl.c`,也不注册 `ssl`/`tls` transports。WASI HTTP Component 提供的
24+
HTTP/HTTPS 能力与 OpenSSL 扩展相互独立。SQLite 禁止运行时加载本地动态扩展,
25+
但内存数据库与预开放目录中的数据库文件可正常使用。WASI 不支持创建子进程,
26+
因此 `fileinfo` 保留文件及内存数据的 MIME 检测,但不会调用外部 gzip、bzip2、
27+
xz 等命令检查压缩文件内部内容。
28+
29+
依赖版本固定并进行 SHA-256 校验:zlib 1.3.1、bzip2 1.0.8、libsodium
30+
1.0.22、OpenSSL 3.5.7、libxml2 2.15.3、SQLite 3.53.4 和 libzip 1.11.4。
31+
OpenSSL 只构建 `libcrypto.a`,不构建或安装 `libssl.a`。
1632

1733
## 单独构建 PHP
1834

@@ -67,8 +83,9 @@ PHP、执行命令行指定的 PHP 文件并完成关闭流程。具体特性测
6783
```
6884

6985
`run-cases.sh` 使用 `--dir <host-cases>::/cases`,只把宿主机的 `cases/`
70-
映射为 WASI guest 中的 `/cases`,然后分别执行 `/cases/*.php`,不会向 WASM
71-
开放整个 php-src 工作区。等价的单个 case 命令为:
86+
映射为 WASI guest 中的 `/cases`,并将一个自动清理的临时目录映射到 `/tmp`,
87+
然后分别执行 `/cases/*.php`,不会向 WASM 开放整个 php-src 工作区。等价的
88+
单个 case 命令为:
7289

7390
```bash
7491
wasmtime -S http \

0 commit comments

Comments
 (0)