Skip to content

Commit

Permalink
Add patch and extension to avoid munmap mem leak
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonpayton committed Apr 2, 2024
1 parent 06f27eb commit 569a437
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 0 deletions.
41 changes: 41 additions & 0 deletions packages/php-wasm/compile/php-wasm-memory-storage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
*.lo
*.la
.libs
acinclude.m4
aclocal.m4
autom4te.cache
build
config.guess
config.h
config.h.in
config.log
config.nice
config.status
config.sub
configure
configure.ac
configure.in
include
install-sh
libtool
ltmain.sh
Makefile
Makefile.fragments
Makefile.global
Makefile.objects
missing
mkinstalldirs
modules
php_test_results_*.txt
phpt.*
run-test-info.php
run-tests.php
tests/**/*.diff
tests/**/*.out
tests/**/*.php
tests/**/*.exp
tests/**/*.log
tests/**/*.sh
tests/**/*.db
tests/**/*.mem
tmp-php.ini
94 changes: 94 additions & 0 deletions packages/php-wasm/compile/php-wasm-memory-storage/config.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
dnl config.m4 for extension wasm_memory_storage

dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary.

dnl If your extension references something external, use 'with':

dnl PHP_ARG_WITH([wasm_memory_storage],
dnl [for wasm_memory_storage support],
dnl [AS_HELP_STRING([--with-wasm_memory_storage],
dnl [Include wasm_memory_storage support])])

dnl Otherwise use 'enable':

PHP_ARG_ENABLE([wasm_memory_storage],
[whether to enable wasm_memory_storage support],
[AS_HELP_STRING([--enable-wasm_memory_storage],
[Enable wasm_memory_storage support])],
[no])

if test "$PHP_WASM_MEMORY_STORAGE" != "no"; then
dnl Write more examples of tests here...

dnl Remove this code block if the library does not support pkg-config.
dnl PKG_CHECK_MODULES([LIBFOO], [foo])
dnl PHP_EVAL_INCLINE($LIBFOO_CFLAGS)
dnl PHP_EVAL_LIBLINE($LIBFOO_LIBS, WASM_MEMORY_STORAGE_SHARED_LIBADD)

dnl If you need to check for a particular library version using PKG_CHECK_MODULES,
dnl you can use comparison operators. For example:
dnl PKG_CHECK_MODULES([LIBFOO], [foo >= 1.2.3])
dnl PKG_CHECK_MODULES([LIBFOO], [foo < 3.4])
dnl PKG_CHECK_MODULES([LIBFOO], [foo = 1.2.3])

dnl Remove this code block if the library supports pkg-config.
dnl --with-wasm_memory_storage -> check with-path
dnl SEARCH_PATH="/usr/local /usr" # you might want to change this
dnl SEARCH_FOR="/include/wasm_memory_storage.h" # you most likely want to change this
dnl if test -r $PHP_WASM_MEMORY_STORAGE/$SEARCH_FOR; then # path given as parameter
dnl WASM_MEMORY_STORAGE_DIR=$PHP_WASM_MEMORY_STORAGE
dnl else # search default path list
dnl AC_MSG_CHECKING([for wasm_memory_storage files in default path])
dnl for i in $SEARCH_PATH ; do
dnl if test -r $i/$SEARCH_FOR; then
dnl WASM_MEMORY_STORAGE_DIR=$i
dnl AC_MSG_RESULT(found in $i)
dnl fi
dnl done
dnl fi
dnl
dnl if test -z "$WASM_MEMORY_STORAGE_DIR"; then
dnl AC_MSG_RESULT([not found])
dnl AC_MSG_ERROR([Please reinstall the wasm_memory_storage distribution])
dnl fi

dnl Remove this code block if the library supports pkg-config.
dnl --with-wasm_memory_storage -> add include path
dnl PHP_ADD_INCLUDE($WASM_MEMORY_STORAGE_DIR/include)

dnl Remove this code block if the library supports pkg-config.
dnl --with-wasm_memory_storage -> check for lib and symbol presence
dnl LIBNAME=WASM_MEMORY_STORAGE # you may want to change this
dnl LIBSYMBOL=WASM_MEMORY_STORAGE # you most likely want to change this

dnl If you need to check for a particular library function (e.g. a conditional
dnl or version-dependent feature) and you are using pkg-config:
dnl PHP_CHECK_LIBRARY($LIBNAME, $LIBSYMBOL,
dnl [
dnl AC_DEFINE(HAVE_WASM_MEMORY_STORAGE_FEATURE, 1, [ ])
dnl ],[
dnl AC_MSG_ERROR([FEATURE not supported by your wasm_memory_storage library.])
dnl ], [
dnl $LIBFOO_LIBS
dnl ])

dnl If you need to check for a particular library function (e.g. a conditional
dnl or version-dependent feature) and you are not using pkg-config:
dnl PHP_CHECK_LIBRARY($LIBNAME, $LIBSYMBOL,
dnl [
dnl PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $WASM_MEMORY_STORAGE_DIR/$PHP_LIBDIR, WASM_MEMORY_STORAGE_SHARED_LIBADD)
dnl AC_DEFINE(HAVE_WASM_MEMORY_STORAGE_FEATURE, 1, [ ])
dnl ],[
dnl AC_MSG_ERROR([FEATURE not supported by your wasm_memory_storage library.])
dnl ],[
dnl -L$WASM_MEMORY_STORAGE_DIR/$PHP_LIBDIR -lm
dnl ])
dnl
dnl PHP_SUBST(WASM_MEMORY_STORAGE_SHARED_LIBADD)

dnl In case of no dependencies
AC_DEFINE(HAVE_WASM_MEMORY_STORAGE, 1, [ Have wasm_memory_storage support ])

PHP_NEW_EXTENSION(wasm_memory_storage, wasm_memory_storage.c, $ext_shared)
fi
7 changes: 7 additions & 0 deletions packages/php-wasm/compile/php-wasm-memory-storage/config.w32
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ARG_ENABLE('wasm_memory_storage', 'wasm_memory_storage support', 'no');

if (PHP_WASM_MEMORY_STORAGE != 'no') {
AC_DEFINE('HAVE_WASM_MEMORY_STORAGE', 1, 'wasm_memory_storage support enabled');

EXTENSION('wasm_memory_storage', 'wasm_memory_storage.c', null, '/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* wasm_memory_storage extension for PHP */

#ifndef PHP_WASM_MEMORY_STORAGE_H
# define PHP_WASM_MEMORY_STORAGE_H

extern zend_module_entry wasm_memory_storage_module_entry;
# define phpext_wasm_memory_storage_ptr &wasm_memory_storage_module_entry

# define PHP_WASM_MEMORY_STORAGE_VERSION "0.0.1"

#endif /* PHP_WASM_MEMORY_STORAGE_H */
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* wasm_memory_storage extension for PHP */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include "php.h"
#include "ext/standard/info.h"
#include "Zend/zend_alloc.h"
#include "php_wasm_memory_storage.h"

void* wasm_memory_storage_chunk_alloc(zend_mm_storage* storage, size_t size, size_t alignment)
{
void* ptr = NULL;
if (posix_memalign(&ptr, alignment, size) == 0)
{
//fprintf(stdout, "* chunk_alloc, %p, %#zx, %#zx\n", ptr, size, alignment);
return ptr;
} else {
return NULL;
}
}

void wasm_memory_storage_chunk_free(zend_mm_storage* storage, void* ptr, size_t size)
{
//fprintf(stdout, "* free, %p, %#zx\n", ptr, size);
free(ptr);
}

zend_mm_storage wasm_memory_storage_struct = {
.handlers = {
.chunk_alloc = &wasm_memory_storage_chunk_alloc,
.chunk_free = &wasm_memory_storage_chunk_free,
.chunk_truncate = NULL,
.chunk_extend = NULL,
},
.data = NULL,
};

// TODO: Show where this was derived from. Defaults in zend_alloc.c
#ifndef ZEND_MM_CUSTOM
# define ZEND_MM_CUSTOM 1 /* support for custom memory allocator */
#endif
#ifndef ZEND_MM_STORAGE
# define ZEND_MM_STORAGE 1 /* support for custom memory storage */
#endif

// TODO: Link to struct decl that is being mirrored
typedef struct _wasm_memory_storage_heap {
#if ZEND_MM_CUSTOM
int custom;
#endif
#if ZEND_MM_STORAGE
zend_mm_storage *storage;
#endif
} wasm_memory_storage_heap;

PHP_RINIT_FUNCTION(wasm_memory_storage)
{
wasm_memory_storage_heap* heap = (wasm_memory_storage_heap*) zend_mm_get_heap();
heap->storage = &wasm_memory_storage_struct;

return SUCCESS;
}

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

/* {{{ wasm_memory_storage_module_entry */
zend_module_entry wasm_memory_storage_module_entry = {
STANDARD_MODULE_HEADER,
"wasm_memory_storage", /* Extension name */
NULL, /* zend_function_entry */
NULL, /* PHP_MINIT - Module initialization */
NULL, /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(wasm_memory_storage), /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(wasm_memory_storage), /* PHP_MINFO - Module info */
PHP_WASM_MEMORY_STORAGE_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES
};
/* }}} */
5 changes: 5 additions & 0 deletions packages/php-wasm/compile/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ RUN if [ "$WITH_MBREGEX" = "yes" && "${PHP_VERSION}" -ne "7.0" ]; \
COPY ./php/php*.patch /root/
RUN cd /root && \
git apply --no-index /root/php${PHP_VERSION:0:3}*.patch -v && \
( [[ "${PHP_VERSION:0:3}" == '8.3' ]] && \
git apply --no-index /root/php-chunk-alloc-zend-assert-8.3.patch -v || \
git apply --no-index /root/php-chunk-alloc-zend-assert.patch -v \
) && \
touch php-src/patched

# Add VRZNO if needed
Expand Down Expand Up @@ -255,6 +259,7 @@ RUN source /root/emsdk/emsdk_env.sh && \
--enable-bcmath \
--enable-ctype \
--enable-tokenizer \
--enable-wasm_memory_storage \
$(cat /root/.php-configure-flags)

# Silence the errors "munmap() failed: [28] Invalid argument"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/php-src/Zend/zend_alloc.c b/php-src/Zend/zend_alloc.c
index bf2116fc91..bec65453d8 100644
--- a/php-src/Zend/zend_alloc.c
+++ b/php-src/Zend/zend_alloc.c
@@ -773,7 +773,7 @@ static void *zend_mm_chunk_alloc(zend_mm_heap *heap, size_t size, size_t alignme
#if ZEND_MM_STORAGE
if (UNEXPECTED(heap->storage)) {
void *ptr = heap->storage->handlers.chunk_alloc(heap->storage, size, alignment);
- ZEND_ASSERT(((uintptr_t)((char*)ptr + (alignment-1)) & (alignment-1)) == (uintptr_t)ptr);
+ ZEND_ASSERT(((uintptr_t)((char*)ptr + (alignment-1)) & ~(alignment-1)) == (uintptr_t)ptr);
return ptr;
}
#endif
13 changes: 13 additions & 0 deletions packages/php-wasm/compile/php/php-chunk-alloc-zend-assert.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/php-src/Zend/zend_alloc.c b/php-src/Zend/zend_alloc.c
index bf2116fc91..bec65453d8 100644
--- a/php-src/Zend/zend_alloc.c
+++ b/php-src/Zend/zend_alloc.c
@@ -773,7 +773,7 @@ static void *zend_mm_chunk_alloc(zend_mm_heap *heap, size_t size, size_t alignme
#if ZEND_MM_STORAGE
if (UNEXPECTED(heap->storage)) {
void *ptr = heap->storage->handlers.chunk_alloc(heap->storage, size, alignment);
- ZEND_ASSERT(((zend_uintptr_t)((char*)ptr + (alignment-1)) & (alignment-1)) == (zend_uintptr_t)ptr);
+ ZEND_ASSERT(((zend_uintptr_t)((char*)ptr + (alignment-1)) & ~(alignment-1)) == (zend_uintptr_t)ptr);
return ptr;
}
#endif

0 comments on commit 569a437

Please sign in to comment.