2014-09-28 00:57:12 +00:00
# include "phpdbg_sigsafe.h"
# include "phpdbg.h"
2016-02-16 22:47:37 +00:00
ZEND_EXTERN_MODULE_GLOBALS ( phpdbg )
2014-09-28 00:57:12 +00:00
# define STR(x) #x
# define EXP_STR(x) STR(x)
2014-10-24 17:29:50 +00:00
static void * zend_mm_mem_alloc ( zend_mm_storage * storage , size_t size , size_t alignment ) {
2014-09-28 00:57:12 +00:00
2015-08-21 22:36:31 +00:00
if ( EXPECTED ( size < = PHPDBG_SIGSAFE_MEM_SIZE & & ! PHPDBG_G ( sigsafe_mem ) . allocated ) ) {
2014-09-28 00:57:12 +00:00
PHPDBG_G ( sigsafe_mem ) . allocated = 1 ;
2015-08-21 22:36:31 +00:00
return ( void * ) ( ( ( size_t ) PHPDBG_G ( sigsafe_mem ) . mem & ~ ( alignment - 1 ) ) + alignment ) ;
2014-09-28 00:57:12 +00:00
}
2016-11-20 18:18:50 +00:00
zend_quiet_write ( PHPDBG_G ( io ) [ PHPDBG_STDERR ] . fd , ZEND_STRL ( " Tried to allocate more than " EXP_STR ( PHPDBG_SIGSAFE_MEM_SIZE ) " bytes from stack memory in signal handler ... bailing out of signal handler \n " ) ) ;
2014-09-28 00:57:12 +00:00
if ( * EG ( bailout ) ) {
LONGJMP ( * EG ( bailout ) , FAILURE ) ;
}
2016-11-20 18:18:50 +00:00
zend_quiet_write ( PHPDBG_G ( io ) [ PHPDBG_STDERR ] . fd , ZEND_STRL ( " Bailed out without a bailout address in signal handler! \n " ) ) ;
2014-09-28 00:57:12 +00:00
return NULL ;
}
2014-10-24 17:29:50 +00:00
static void zend_mm_mem_free ( zend_mm_storage * storage , void * ptr , size_t size ) {
2014-09-28 00:57:12 +00:00
}
2014-12-13 22:06:14 +00:00
void phpdbg_set_sigsafe_mem ( char * buffer ) {
2014-09-28 00:57:12 +00:00
phpdbg_signal_safe_mem * mem = & PHPDBG_G ( sigsafe_mem ) ;
2015-05-14 13:47:22 +00:00
const zend_mm_handlers phpdbg_handlers = {
zend_mm_mem_alloc ,
zend_mm_mem_free ,
NULL ,
NULL ,
} ;
2014-09-28 00:57:12 +00:00
mem - > mem = buffer ;
mem - > allocated = 0 ;
2015-05-14 13:47:22 +00:00
mem - > heap = zend_mm_startup_ex ( & phpdbg_handlers , NULL , 0 ) ;
2014-10-24 17:29:50 +00:00
2014-12-13 22:06:14 +00:00
mem - > old_heap = zend_mm_set_heap ( mem - > heap ) ;
2014-09-28 00:57:12 +00:00
}
2014-12-13 22:06:14 +00:00
zend_mm_heap * phpdbg_original_heap_sigsafe_mem ( void ) {
2014-10-02 23:29:41 +00:00
return PHPDBG_G ( sigsafe_mem ) . old_heap ;
}
2014-12-13 22:06:14 +00:00
void phpdbg_clear_sigsafe_mem ( void ) {
zend_mm_set_heap ( phpdbg_original_heap_sigsafe_mem ( ) ) ;
2014-09-28 00:57:12 +00:00
PHPDBG_G ( sigsafe_mem ) . mem = NULL ;
}
2014-12-13 22:06:14 +00:00
zend_bool phpdbg_active_sigsafe_mem ( void ) {
2014-09-28 00:57:12 +00:00
return ! ! PHPDBG_G ( sigsafe_mem ) . mem ;
}