Re: com php-src: override malloc/realloc/free and block signals: Zend/zend_signal.c Zend/zend_signal.h sapi/cli/php_cli.c sapi/fpm/fpm/fpm_main.c
| From: | Antony Dovgal | Date: | Mon, 31 Mar 2014 14:34:57 +0000 |
| Subject: | Re: com php-src: override malloc/realloc/free and block signals: Zend/zend_signal.c Zend/zend_signal.h sapi/cli/php_cli.c sapi/fpm/fpm/fpm_main.c | ||
| References: | 1 | Groups: | php.cvs |
| Request: | Send a blank email to php-cvs+get-76656@lists.php.net to get a copy of this message | ||
Oh, sorry, didn't want to push that here.
On 03/31/2014 05:04 PM, Antony Dovgal wrote:
-- Wbr, Antony Dovgal --- http://pinba.org - realtime profiling for PHPCommit: c5e63fb51bf141afc41b21b9f10cbeac41f4f34d Author: Antony Dovgal <tony2001@php.net> Mon, 31 Mar 2014 17:04:52 +0400Parents: 3d69168d3002684468f3b2889efd884004920ac0 Branches: badoo-5.5_mysqlndLink: http://git.php.net/?p=php-src.git;a=commitdiff;h=c5e63fb51bf141afc41b21b9f10cbeac41f4f34dLog: override malloc/realloc/free and block signals Changed paths: M Zend/zend_signal.c M Zend/zend_signal.h M sapi/cli/php_cli.c M sapi/fpm/fpm/fpm_main.c Diff: diff --git a/Zend/zend_signal.c b/Zend/zend_signal.c index 6105fb1..215a665 100644 --- a/Zend/zend_signal.c +++ b/Zend/zend_signal.c @@ -41,6 +41,8 @@ #include <unistd.h> #endif +#include <malloc.h> + #ifdef ZEND_SIGNALS #include "zend_signal.h" @@ -60,7 +62,7 @@ static int zend_signal_register(int signo, void (*handler)(int, siginfo_t*, void #define TIMEOUT_SIG SIGPROF #endif -static int zend_sigs[] = { TIMEOUT_SIG, SIGHUP, SIGINT, SIGQUIT, SIGTERM, SIGUSR1, SIGUSR2 }; +static int zend_sigs[] = { TIMEOUT_SIG, SIGHUP, SIGINT, SIGUSR1, SIGUSR2 }; #define SA_FLAGS_MASK ~(SA_NODEFER | SA_RESETHAND) @@ -68,6 +70,62 @@ static int zend_sigs[] = { TIMEOUT_SIG, SIGHUP, SIGINT, SIGQUIT, SIGTERM, SIGUSR static zend_signal_entry_t global_orig_handlers[NSIG];static sigset_t global_sigmask;+#include <dlfcn.h> + +void *(*orig_malloc)(int); +void *(*orig_realloc)(void *, int); +void (*orig_free)(void *); + +void *malloc(size_t size) /* {{{ */ +{ + void *ptr; + HANDLE_BLOCK_INTERRUPTIONS(); + ptr = orig_malloc(size); + HANDLE_UNBLOCK_INTERRUPTIONS(); + return ptr; +} +/* }}} */ + +void *realloc(void *ptr, size_t size) /* {{{ */ +{ + HANDLE_BLOCK_INTERRUPTIONS(); + ptr = orig_realloc(ptr, size); + HANDLE_UNBLOCK_INTERRUPTIONS(); + return ptr; +} +/* }}} */ + +void free(void *ptr) /* {{{ */ +{ + HANDLE_BLOCK_INTERRUPTIONS(); + orig_free(ptr); + HANDLE_UNBLOCK_INTERRUPTIONS(); +} +/* }}} */ + +void zend_init_hooks(void) /* {{{ */ +{ + void* handle = (void*) -1l; + orig_free = (void (*)(void*))dlsym(handle, "free"); + if (orig_free == NULL) { + return; + } + + handle = (void*) -1l; + orig_malloc = (void *(*)())dlsym(handle, "malloc"); + if (orig_malloc == NULL) { + return; + } + + handle = (void*) -1l; + orig_realloc = (void *(*)())dlsym(handle, "realloc"); + if (orig_realloc == NULL) { + return; + } +} +/* }}} */ + + /* {{{ zend_signal_handler_defer * Blocks signals if in critical section */ void zend_signal_handler_defer(int signo, siginfo_t *siginfo, void *context) diff --git a/Zend/zend_signal.h b/Zend/zend_signal.h index 02d6495..f153003 100644 --- a/Zend/zend_signal.h +++ b/Zend/zend_signal.h @@ -39,6 +39,8 @@ typedef struct _zend_signal_entry_t {int flags; /* sigaction style flags */ void* handler; /* signal handler or context */+ int old_flags; + void* old_handler; } zend_signal_entry_t; typedef struct _zend_signal_t { @@ -90,6 +92,7 @@ void zend_signal_startup(); void zend_signal_shutdown(TSRMLS_D); ZEND_API int zend_signal(int signo, void (*handler)(int) TSRMLS_DC); ZEND_API int zend_sigaction(int signo, const struct sigaction *act, struct sigaction *oldact TSRMLS_DC); +ZEND_API void zend_init_hooks(void); #ifdef ZTS #define zend_sigprocmask(signo, set, oldset) tsrm_sigmask((signo), (set), (oldset)) diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index 9f3fc4b..bf7a83a 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -1218,6 +1218,8 @@ int main(int argc, char *argv[]) int ini_ignore = 0; sapi_module_struct *sapi_module = &cli_sapi_module; + zend_init_hooks(); + /* * Do not move this initialization. It needs to happen before argv is used * in any way. diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c index c280884..29dbe8a 100644 --- a/sapi/fpm/fpm/fpm_main.c +++ b/sapi/fpm/fpm/fpm_main.c @@ -1825,6 +1825,8 @@ int main(int argc, char *argv[]) int php_information = 0; int php_allow_to_run_as_root = 0; + zend_init_hooks(); + #ifdef HAVE_SIGNAL_H #if defined(SIGPIPE) && defined(SIG_IGN) signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so