Skip to content
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

Try to repro memory out of bounds errors in CI #1199

Closed
wants to merge 7 commits 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
44 changes: 2 additions & 42 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,17 @@ on:
pull_request:

jobs:
# This step:
# * Warms up the node_modules cache
# * Performs linting and typechecking
#
# The linting tasks take ~5s to complete and it doesn't
# make sense to separate them into separate steps that would
# take ~25s just to run git clone and restore node_modules.
lint-and-typecheck:
name: 'Lint and typecheck'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/prepare-playground
- run: npx nx affected --target=lint
- run: npx nx affected --target=typecheck
test-unit:
runs-on: ubuntu-latest
needs: [lint-and-typecheck]
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/prepare-playground
- run: node --expose-gc node_modules/nx/bin/nx affected --target=test --configuration=ci
test-e2e:
runs-on: ubuntu-latest
needs: [lint-and-typecheck]
# Run as root to allow node to bind to port 80
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/prepare-playground
- run: sudo ./node_modules/.bin/cypress install --force
- run: sudo CYPRESS_CI=1 npx nx e2e playground-website --configuration=ci --verbose
# Upload the Cypress screenshots as artifacts if the job fails
- uses: actions/upload-artifact@v2
if: ${{ failure() }}
with:
name: cypress-screenshots
path: dist/cypress/packages/playground/website/screenshots

build:
runs-on: ubuntu-latest
needs: [lint-and-typecheck]
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/prepare-playground
- run: npx nx affected --target=build --parallel=3 --verbose
- run: node --expose-gc node_modules/nx/bin/nx run playground-blueprints:"test:vite" --configuration=ci

# Deploy documentation job
deploy_docs:
if: github.ref == 'refs/heads/trunk' && github.event_name == 'push'
# Add a dependency to the build job
needs: [test-unit, test-e2e, build]
needs: [test-unit]
name: 'Deploy doc site'

# Grant GITHUB_TOKEN the permissions required to make a Pages deployment
Expand Down
4 changes: 2 additions & 2 deletions packages/php-wasm/compile/base-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ RUN set -euxo pipefail;\
# the build take forever on Apple Silicon.
RUN ln -s /usr/bin/python3 /usr/bin/python
RUN git clone https://github.com/emscripten-core/emsdk.git && \
./emsdk/emsdk install 3.1.43 && \
/root/emsdk/emsdk activate 3.1.43
./emsdk/emsdk install 3.1.56 && \
/root/emsdk/emsdk activate 3.1.56

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

Expand Down
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,135 @@
/**
* wasm_memory_storage extension for PHP.
*
* The purpose of this extension is to work around a memory leak caused by
* failing attempts to partially unmap memory allocated with mmap().
* By providing custom memory storage, we can avoid mmap()/munmap() calls and
* use posix_memalign()/free() instead.
*
* Background:
* Issue: https://github.com/WordPress/wordpress-playground/issues/1128
* PR: https://github.com/WordPress/wordpress-playground/pull/1189
*/

#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"

/**
* Allocate a chunk of memory.
*
* This function implements the PHP Zend custom memory storage operation "chunk_free()".
*
* Here is an example offered in the PHP source code:
* https://github.com/php/php-src/blob/dbaeb62ab1e34067057170ab50cf39d1bde584d8/Zend/zend_alloc.h#L325
*
* @param storage The zend_mm_storage struct for this allocation.
* @param size The number of bytes to allocate.
* @param alignment The byte alignment of the memory allocation.
* @returns A pointer to allocated memory or NULL on failure.
*/
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)
{
return ptr;
} else {
return NULL;
}
}

/**
* Free a chunk of memory.
*
* This function implements the PHP Zend custom memory storage operation "chunk_free()".
*
* Here is an example offered in the PHP source code:
* https://github.com/php/php-src/blob/dbaeb62ab1e34067057170ab50cf39d1bde584d8/Zend/zend_alloc.h#L352
*
* @param storage The zend_mm_storage struct for this memory.
* @param ptr The pointer to the memory to free.
* @param size The size of the memory to free. Ignored.
* @returns void
*/
void wasm_memory_storage_chunk_free(zend_mm_storage* storage, void* ptr, size_t 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,
};

// These definitions are mirrored from zend_alloc.c:
// https://github.com/php/php-src/blob/dbaeb62ab1e34067057170ab50cf39d1bde584d8/Zend/zend_alloc.c#L131-L137
#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

// This struct mirrors the heap structure, which is declared privately
// in zend_alloc.c. We use this to more easily and clearly assign our custom memory storage handler.
// https://github.com/php/php-src/blob/dbaeb62ab1e34067057170ab50cf39d1bde584d8/Zend/zend_alloc.c#L234-L240
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_MINIT_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_MSHUTDOWN_FUNCTION(wasm_memory_storage)
{
wasm_memory_storage_heap* heap = (wasm_memory_storage_heap*) zend_mm_get_heap();
heap->storage = NULL;

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 */
PHP_MINIT(wasm_memory_storage), /* PHP_MINIT - Module initialization */
PHP_MSHUTDOWN(wasm_memory_storage), /* PHP_MSHUTDOWN - Module shutdown */
NULL, /* 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
};
/* }}} */
Loading
Loading