From 9a0b61a6195433a7115b040b47df03ec46f5117d Mon Sep 17 00:00:00 2001 From: Zeev Suraski Date: Sat, 18 Nov 2000 02:43:23 +0000 Subject: [PATCH] Add thread-safety debugging information (idea - Dmitri Dmitrienko) --- Zend/zend_alloc.c | 32 ++++++++++++++++++++++++++++++++ Zend/zend_alloc.h | 4 ++++ 2 files changed, 36 insertions(+) diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index 537b8d68ff..dd17fb33ba 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -137,6 +137,9 @@ ZEND_API void *_emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) p->orig_lineno = __zend_orig_lineno; p->magic = MEM_BLOCK_START_MAGIC; p->reported = 0; + /* Setting the thread id should not be necessary, because we fetched this block + * from this thread's cache + */ AG(cache_stats)[CACHE_INDEX][1]++; #endif p->persistent = 0; @@ -177,6 +180,9 @@ ZEND_API void *_emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) p->orig_lineno = __zend_orig_lineno; p->magic = MEM_BLOCK_START_MAGIC; p->reported = 0; +# ifdef ZTS + p->thread_id = tsrm_thread_id(); +# endif *((long *)(((char *) p) + sizeof(zend_mem_header)+SIZE+PLATFORM_PADDING+END_ALIGNMENT(SIZE))) = MEM_BLOCK_END_MAGIC; #endif #if MEMORY_LIMIT @@ -193,6 +199,17 @@ ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) DECLARE_CACHE_VARS ALS_FETCH(); +#ifdef ZTS + if (p->thread_id != tsrm_thread_id()) { +# if ZEND_DEBUG + tsrm_error(TSRM_ERROR_LEVEL_ERROR, "Memory block allocated at %s:(%d) on thread %x freed at %s:(%d) on thread %x, ignoring", + p->filename, p->lineno, p->thread_id, + __zend_filename, __zend_lineno, tsrm_thread_id()); +# endif + return; + } +#endif + CALCULATE_REAL_SIZE_AND_CACHE_INDEX(p->size); #if ZEND_DEBUG if (!_mem_block_check(ptr, 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC)) { @@ -253,6 +270,21 @@ ZEND_API void *_erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LIN return _emalloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); } +#ifdef ZTS + if (p->thread_id != tsrm_thread_id()) { + void *new_p; + +# if ZEND_DEBUG + tsrm_error(TSRM_ERROR_LEVEL_ERROR, "Memory block allocated at %s:(%d) on thread %x reallocated at %s:(%d) on thread %x, duplicating", + p->filename, p->lineno, p->thread_id, + __zend_filename, __zend_lineno, tsrm_thread_id()); +# endif + new_p = _emalloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); + memcpy(new_p, ptr, p->size); + return new_p; + } +#endif + CALCULATE_REAL_SIZE_AND_CACHE_INDEX(size); HANDLE_BLOCK_INTERRUPTIONS(); diff --git a/Zend/zend_alloc.h b/Zend/zend_alloc.h index ee2cf063f8..1df9f139d6 100644 --- a/Zend/zend_alloc.h +++ b/Zend/zend_alloc.h @@ -23,6 +23,7 @@ #include +#include "../TSRM/TSRM.h" #include "zend_globals_macros.h" #define MEM_BLOCK_START_MAGIC 0x7312F8DCL @@ -38,6 +39,9 @@ typedef struct _zend_mem_header { int reported; char *orig_filename; uint orig_lineno; +# ifdef ZTS + THREAD_T thread_id; +# endif #endif struct _zend_mem_header *pNext; struct _zend_mem_header *pLast; -- 2.40.0