Skip to content

Add support for building to the wasm32-wasi target #2

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,9 @@ static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{
#endif
executor_globals->saved_fpu_cw_ptr = NULL;
executor_globals->active = 0;
#ifndef __wasi__
executor_globals->bailout = NULL;
#endif // __wasi__
executor_globals->error_handling = EH_NORMAL;
executor_globals->exception_class = NULL;
executor_globals->exception = NULL;
Expand Down Expand Up @@ -1223,6 +1225,7 @@ ZEND_COLD void zenderror(const char *error) /* {{{ */
ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32_t lineno) /* {{{ */
{

#ifndef __wasi__
if (!EG(bailout)) {
zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno);
exit(-1);
Expand All @@ -1233,6 +1236,7 @@ ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32
CG(in_compilation) = 0;
EG(current_execute_data) = NULL;
LONGJMP(*EG(bailout), FAILURE);
#endif // __wasi__
}
/* }}} */

Expand Down
14 changes: 14 additions & 0 deletions Zend/zend.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ typedef size_t (*zend_write_func_t)(const char *str, size_t str_length);

#define zend_bailout() _zend_bailout(__FILE__, __LINE__)

#ifndef __wasi__
#define zend_try \
{ \
JMP_BUF *__orig_bailout = EG(bailout); \
Expand All @@ -272,6 +273,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 // __wasi__
#define zend_try \
{ \
if (1) {
#define zend_catch \
} else {
#define zend_end_try() \
} \
}
#define zend_first_try zend_try
#endif // __wasi__


BEGIN_EXTERN_C()
void zend_startup(zend_utility_functions *utility_functions);
void zend_shutdown(void);
Expand Down
18 changes: 16 additions & 2 deletions Zend/zend_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
#include <fcntl.h>
#include <errno.h>

#ifndef _WIN32
#if !defined(_WIN32) && HAVE_MMAP
# include <sys/mman.h>
# ifndef MAP_ANON
# ifdef MAP_ANONYMOUS
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 18 additions & 4 deletions Zend/zend_fibers.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# include <ucontext.h>
#endif

#ifndef ZEND_WIN32
#if !defined(ZEND_WIN32) && HAVE_MMAP
# include <unistd.h>
# include <sys/mman.h>
# include <limits.h>
Expand Down Expand Up @@ -108,7 +108,9 @@ typedef struct _zend_fiber_vm_state {
zend_execute_data *current_execute_data;
int error_reporting;
uint32_t jit_trace_num;
#ifndef __wasi__
JMP_BUF *bailout;
#endif // __wasi__
zend_fiber *active_fiber;
#ifdef ZEND_CHECK_STACK_LIMIT
void *stack_base;
Expand All @@ -125,7 +127,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 __wasi__
state->bailout = EG(bailout);
#endif // __wasi__
state->active_fiber = EG(active_fiber);
#ifdef ZEND_CHECK_STACK_LIMIT
state->stack_base = EG(stack_base);
Expand All @@ -142,7 +146,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 __wasi__
EG(bailout) = state->bailout;
#endif // __wasi__
EG(active_fiber) = state->active_fiber;
#ifdef ZEND_CHECK_STACK_LIMIT
EG(stack_base) = state->stack_base;
Expand Down Expand Up @@ -236,6 +242,8 @@ static zend_fiber_stack *zend_fiber_stack_allocate(size_t size)
return NULL;
}
# endif
#elif defined __wasi__
pointer = malloc(alloc_size);
#else
pointer = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);

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

#ifdef ZEND_WIN32
VirtualFree(pointer, 0, MEM_RELEASE);
#elif defined __wasi__
free(pointer);
#else
munmap(pointer, stack->size + ZEND_FIBER_GUARD_PAGES * page_size);
#endif
Expand Down Expand Up @@ -415,7 +425,7 @@ 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);

Expand All @@ -428,6 +438,8 @@ ZEND_API bool zend_fiber_init_context(zend_fiber_context *context, void *kind, z

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;
Expand Down Expand Up @@ -502,16 +514,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
Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#define ZEND_GLOBALS_H


#ifndef __wasi__
#include <setjmp.h>
#endif

#include "zend_globals_macros.h"

Expand Down Expand Up @@ -162,7 +164,9 @@ struct _zend_executor_globals {

HashTable included_files; /* files already included */

#ifndef __wasi__
JMP_BUF *bailout;
#endif

int error_reporting;
int exit_status;
Expand Down
10 changes: 10 additions & 0 deletions Zend/zend_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 __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 // __wasi__

#endif /* ZEND_TYPES_H */
11 changes: 10 additions & 1 deletion Zend/zend_virtual_cwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 __wasi__
ret = 0;
#else
ret = chmod(new_state.cwd, mode);
#endif
Expand Down Expand Up @@ -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 __wasi__
ret = chown(new_state.cwd, owner, group);
#else
ret = 0;
#endif // __wasi__
}

CWD_STATE_FREE_ERR(&new_state);
Expand Down Expand Up @@ -1706,8 +1712,11 @@ CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
*ptr++ = ' ';

memcpy(ptr, command, command_length+1);
#ifndef __wasi__
retval = popen(command_line, type);

#else
retval = 0;
#endif // __wasi__
efree(command_line);
return retval;
}
Expand Down
15 changes: 10 additions & 5 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,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 ----------------------------------------------------------------------------

Expand Down Expand Up @@ -1334,11 +1337,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
Expand Down
2 changes: 2 additions & 0 deletions ext/fileinfo/libmagic/fsmagic.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ file_fsmagic(struct magic_set *ms, const char *fn, zend_stat_t *sb)
# endif
#endif

#ifndef __wasi__
#ifdef S_IFIFO
case S_IFIFO:
if((ms->flags & MAGIC_DEVICES) != 0)
Expand All @@ -179,6 +180,7 @@ file_fsmagic(struct magic_set *ms, const char *fn, zend_stat_t *sb)
return -1;
break;
#endif
#endif // __wasi__
#ifdef S_IFDOOR
case S_IFDOOR:
if (mime) {
Expand Down
2 changes: 2 additions & 0 deletions ext/pdo_sqlite/sqlite_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 __wasi__
sqlite3_finalize(S->stmt);
#endif
S->stmt = NULL;
}
efree(S);
Expand Down
Loading