Skip to content

Commit 4eae4c7

Browse files
ereslibreassambar
andcommitted
Add support for building to the wasm32-wasi target
This change performs initial adaptations allowing the PHP interpreter to be compiled for the wasm32-wasi target. Co-Authored-By: Asen Alexandrov <[email protected]>
1 parent f90962d commit 4eae4c7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+883
-94
lines changed

Zend/Optimizer/zend_func_infos.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,13 +512,19 @@ static const func_info_t func_infos[] = {
512512
#if defined(HAVE_STRPTIME)
513513
F1("strptime", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE),
514514
#endif
515-
#if defined(HAVE_GETHOSTNAME)
515+
#if !defined(PHP_WASI) && defined(HAVE_GETHOSTNAME)
516516
F1("gethostname", MAY_BE_STRING|MAY_BE_FALSE),
517517
#endif
518+
#if !defined(PHP_WASI)
518519
F1("gethostbyaddr", MAY_BE_STRING|MAY_BE_FALSE),
520+
#endif
521+
#if !defined(PHP_WASI)
519522
F1("gethostbyname", MAY_BE_STRING),
523+
#endif
524+
#if !defined(PHP_WASI)
520525
F1("gethostbynamel", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE),
521-
#if (defined(PHP_WIN32) || defined(HAVE_DNS_SEARCH_FUNC))
526+
#endif
527+
#if !defined(PHP_WASI) && (defined(PHP_WIN32) || defined(HAVE_DNS_SEARCH_FUNC))
522528
F1("dns_get_record", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_FALSE),
523529
#endif
524530
F1("md5", MAY_BE_STRING),

Zend/zend.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,9 @@ static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{
788788
#endif
789789
executor_globals->saved_fpu_cw_ptr = NULL;
790790
executor_globals->active = 0;
791+
#ifndef PHP_WASI
791792
executor_globals->bailout = NULL;
793+
#endif // PHP_WASI
792794
executor_globals->error_handling = EH_NORMAL;
793795
executor_globals->exception_class = NULL;
794796
executor_globals->exception = NULL;
@@ -1179,6 +1181,7 @@ ZEND_COLD void zenderror(const char *error) /* {{{ */
11791181
}
11801182
/* }}} */
11811183

1184+
#ifndef PHP_WASI
11821185
ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32_t lineno) /* {{{ */
11831186
{
11841187

@@ -1195,6 +1198,7 @@ ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32
11951198
LONGJMP(*EG(bailout), FAILURE);
11961199
}
11971200
/* }}} */
1201+
#endif // PHP_WASI
11981202

11991203
ZEND_API size_t zend_get_page_size(void)
12001204
{

Zend/zend.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ TSRMLS_MAIN_CACHE_EXTERN()
7676
ZEND_TSRMLS_CACHE_EXTERN()
7777
#endif
7878

79+
#undef PHP_WASI
80+
#ifdef __wasi__
81+
#define PHP_WASI __wasi__
82+
#endif
83+
7984
struct _zend_serialize_data;
8085
struct _zend_unserialize_data;
8186

@@ -257,6 +262,7 @@ typedef size_t (*zend_write_func_t)(const char *str, size_t str_length);
257262

258263
#define zend_bailout() _zend_bailout(__FILE__, __LINE__)
259264

265+
#ifndef PHP_WASI
260266
#define zend_try \
261267
{ \
262268
JMP_BUF *__orig_bailout = EG(bailout); \
@@ -273,6 +279,19 @@ typedef size_t (*zend_write_func_t)(const char *str, size_t str_length);
273279
}
274280
#define zend_first_try EG(bailout)=NULL; zend_try
275281

282+
#else // PHP_WASI
283+
#define zend_try \
284+
{ \
285+
if (1) {
286+
#define zend_catch \
287+
} else {
288+
#define zend_end_try() \
289+
} \
290+
}
291+
#define zend_first_try zend_try
292+
#endif // PHP_WASI
293+
294+
276295
BEGIN_EXTERN_C()
277296
void zend_startup(zend_utility_functions *utility_functions);
278297
void zend_shutdown(void);

Zend/zend_alloc.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
#include <fcntl.h>
8181
#include <errno.h>
8282

83-
#ifndef _WIN32
83+
#if !defined(_WIN32) && defined(HAVE_MMAP)
8484
# include <sys/mman.h>
8585
# ifndef MAP_ANON
8686
# ifdef MAP_ANONYMOUS
@@ -444,6 +444,8 @@ static void zend_mm_munmap(void *addr, size_t size)
444444
#endif
445445
}
446446
}
447+
#elif !defined(_WIN32) && !defined(HAVE_MMAP)
448+
free(addr);
447449
#else
448450
if (munmap(addr, size) != 0) {
449451
#if ZEND_MM_ERROR
@@ -471,6 +473,8 @@ static void *zend_mm_mmap_fixed(void *addr, size_t size)
471473
}
472474
ZEND_ASSERT(ptr == addr);
473475
return ptr;
476+
#elif !defined(HAVE_MMAP)
477+
return NULL;
474478
#else
475479
int flags = MAP_PRIVATE | MAP_ANON;
476480
#if defined(MAP_EXCL)
@@ -507,6 +511,10 @@ static void *zend_mm_mmap(size_t size)
507511
return NULL;
508512
}
509513
return ptr;
514+
#elif !defined(HAVE_MMAP)
515+
void* ptr = malloc(size);
516+
memset(ptr, 0, size);
517+
return ptr;
510518
#else
511519
void *ptr;
512520

@@ -716,6 +724,11 @@ static zend_always_inline void zend_mm_hugepage(void* ptr, size_t size)
716724

717725
static void *zend_mm_chunk_alloc_int(size_t size, size_t alignment)
718726
{
727+
#if !defined(_WIN32) && !defined(HAVE_MMAP)
728+
void* ptr = aligned_alloc(alignment, size);
729+
memset(ptr, 0, size);
730+
return ptr;
731+
#else
719732
void *ptr = zend_mm_mmap(size);
720733

721734
if (ptr == NULL) {
@@ -766,6 +779,7 @@ static void *zend_mm_chunk_alloc_int(size_t size, size_t alignment)
766779
#endif
767780
return ptr;
768781
}
782+
#endif
769783
}
770784

771785
static void *zend_mm_chunk_alloc(zend_mm_heap *heap, size_t size, size_t alignment)
@@ -2934,7 +2948,7 @@ ZEND_API void start_memory_manager(void)
29342948
#else
29352949
alloc_globals_ctor(&alloc_globals);
29362950
#endif
2937-
#ifndef _WIN32
2951+
#if !defined(_WIN32) && defined(HAVE_MMAP)
29382952
# if defined(_SC_PAGESIZE)
29392953
REAL_PAGE_SIZE = sysconf(_SC_PAGESIZE);
29402954
# elif defined(_SC_PAGE_SIZE)

Zend/zend_fibers.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
# include <ucontext.h>
4040
#endif
4141

42-
#ifndef ZEND_WIN32
42+
#if !defined(ZEND_WIN32) && defined(HAVE_MMAP)
4343
# include <unistd.h>
4444
# include <sys/mman.h>
4545
# include <limits.h>
@@ -108,7 +108,9 @@ typedef struct _zend_fiber_vm_state {
108108
zend_execute_data *current_execute_data;
109109
int error_reporting;
110110
uint32_t jit_trace_num;
111+
#ifndef PHP_WASI
111112
JMP_BUF *bailout;
113+
#endif // PHP_WASI
112114
zend_fiber *active_fiber;
113115
#ifdef ZEND_CHECK_STACK_LIMIT
114116
void *stack_base;
@@ -125,7 +127,9 @@ static zend_always_inline void zend_fiber_capture_vm_state(zend_fiber_vm_state *
125127
state->current_execute_data = EG(current_execute_data);
126128
state->error_reporting = EG(error_reporting);
127129
state->jit_trace_num = EG(jit_trace_num);
130+
#ifndef PHP_WASI
128131
state->bailout = EG(bailout);
132+
#endif // PHP_WASI
129133
state->active_fiber = EG(active_fiber);
130134
#ifdef ZEND_CHECK_STACK_LIMIT
131135
state->stack_base = EG(stack_base);
@@ -142,7 +146,9 @@ static zend_always_inline void zend_fiber_restore_vm_state(zend_fiber_vm_state *
142146
EG(current_execute_data) = state->current_execute_data;
143147
EG(error_reporting) = state->error_reporting;
144148
EG(jit_trace_num) = state->jit_trace_num;
149+
#ifndef PHP_WASI
145150
EG(bailout) = state->bailout;
151+
#endif // PHP_WASI
146152
EG(active_fiber) = state->active_fiber;
147153
#ifdef ZEND_CHECK_STACK_LIMIT
148154
EG(stack_base) = state->stack_base;
@@ -236,6 +242,8 @@ static zend_fiber_stack *zend_fiber_stack_allocate(size_t size)
236242
return NULL;
237243
}
238244
# endif
245+
#elif defined(PHP_WASI)
246+
pointer = malloc(alloc_size);
239247
#else
240248
pointer = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
241249

@@ -302,6 +310,8 @@ static void zend_fiber_stack_free(zend_fiber_stack *stack)
302310

303311
#ifdef ZEND_WIN32
304312
VirtualFree(pointer, 0, MEM_RELEASE);
313+
#elif defined(PHP_WASI)
314+
free(pointer);
305315
#else
306316
munmap(pointer, stack->size + ZEND_FIBER_GUARD_PAGES * page_size);
307317
#endif
@@ -394,6 +404,7 @@ ZEND_API bool zend_fiber_switch_blocked(void)
394404
return zend_fiber_switch_blocking;
395405
}
396406

407+
#ifndef PHP_WASI
397408
ZEND_API zend_result zend_fiber_init_context(zend_fiber_context *context, void *kind, zend_fiber_coroutine coroutine, size_t stack_size)
398409
{
399410
context->stack = zend_fiber_stack_allocate(stack_size);
@@ -440,6 +451,7 @@ ZEND_API zend_result zend_fiber_init_context(zend_fiber_context *context, void *
440451

441452
return SUCCESS;
442453
}
454+
#endif // PHP_WASI
443455

444456
ZEND_API void zend_fiber_destroy_context(zend_fiber_context *context)
445457
{
@@ -502,16 +514,18 @@ ZEND_API void zend_fiber_switch_context(zend_fiber_transfer *transfer)
502514

503515
/* Copy transfer struct because it might live on the other fiber's stack that will eventually be destroyed. */
504516
*transfer = *transfer_data;
505-
#else
517+
#elif !defined(PHP_WASI)
506518
boost_context_data data = jump_fcontext(to->handle, transfer);
507519

508520
/* Copy transfer struct because it might live on the other fiber's stack that will eventually be destroyed. */
509521
*transfer = *data.transfer;
522+
#else
523+
return;
510524
#endif
511525

512526
to = transfer->context;
513527

514-
#ifndef ZEND_FIBER_UCONTEXT
528+
#if !defined(ZEND_FIBER_UCONTEXT) && !defined(PHP_WASI)
515529
/* Get the context that resumed us and update its handle to allow for symmetric coroutines. */
516530
to->handle = data.handle;
517531
#endif

Zend/zend_fibers_wasi.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Zend Engine |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 2.00 of the Zend license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.zend.com/license/2_00.txt. |
11+
| If you did not receive a copy of the Zend license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#include "zend.h"
18+
#include "zend_fibers.h"
19+
20+
ZEND_API zend_result zend_fiber_init_context(zend_fiber_context *context, void *kind, zend_fiber_coroutine coroutine, size_t stack_size)
21+
{
22+
}

Zend/zend_globals.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
#define ZEND_GLOBALS_H
2222

2323

24+
#ifndef PHP_WASI
2425
#include <setjmp.h>
26+
#endif
27+
2528
#include <stdint.h>
2629
#include <sys/types.h>
2730

@@ -174,7 +177,9 @@ struct _zend_executor_globals {
174177

175178
HashTable included_files; /* files already included */
176179

180+
#ifndef PHP_WASI
177181
JMP_BUF *bailout;
182+
#endif
178183

179184
int error_reporting;
180185
int exit_status;

Zend/zend_virtual_cwd.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,7 @@ CWD_API int virtual_utime(const char *filename, struct utimbuf *buf) /* {{{ */
13781378
/* }}} */
13791379
#endif
13801380

1381+
#ifndef PHP_WASI
13811382
CWD_API int virtual_chmod(const char *filename, mode_t mode) /* {{{ */
13821383
{
13831384
cwd_state new_state;
@@ -1411,8 +1412,9 @@ CWD_API int virtual_chmod(const char *filename, mode_t mode) /* {{{ */
14111412
return ret;
14121413
}
14131414
/* }}} */
1415+
#endif
14141416

1415-
#if !defined(ZEND_WIN32)
1417+
#if !defined(ZEND_WIN32) && !defined(PHP_WASI)
14161418
CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link) /* {{{ */
14171419
{
14181420
cwd_state new_state;
@@ -1659,7 +1661,7 @@ CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
16591661
return popen_ex(command, type, CWDG(cwd).cwd, NULL);
16601662
}
16611663
/* }}} */
1662-
#else /* Unix */
1664+
#elif !defined(PHP_WASI) /* Unix */
16631665
CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
16641666
{
16651667
size_t command_length;

Zend/zend_virtual_cwd_wasi.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
*/
14+
15+
#include "zend.h"
16+
#include "zend_virtual_cwd.h"
17+
18+
CWD_API int virtual_chmod(const char *filename, mode_t mode) /* {{{ */
19+
{
20+
return -1;
21+
}
22+
/* }}} */
23+
24+
CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link) /* {{{ */
25+
{
26+
return -1;
27+
}
28+
/* }}} */
29+
30+
CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
31+
{
32+
return NULL;
33+
}
34+
/* }}} */

Zend/zend_wasi.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Zend Engine |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 2.00 of the Zend license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.zend.com/license/2_00.txt. |
11+
| If you did not receive a copy of the Zend license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#include "zend.h"
18+
19+
ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename,
20+
uint32_t lineno) /* {{{ */
21+
{
22+
// Not implemented yet, as WebAssembly has no stack switching
23+
// feature implementation at this point.
24+
}
25+
/* }}} */

0 commit comments

Comments
 (0)