[Pkg-php-commits] [php/debian-experimental] enable (refreshed) suhosin.patch again

Ondrej Sury ondrej at howl.office.nic.cz
Tue Aug 25 14:57:23 UTC 2009


---
 debian/changelog             |    1 +
 debian/patches/series        |    1 +
 debian/patches/suhosin.patch | 4316 +++++++++++++++++++++++++++++++++++-------
 3 files changed, 3582 insertions(+), 736 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index ea9edfd..639329e 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -2,6 +2,7 @@ php5 (5.3.0-3) unstable; urgency=low
 
   * Upload to unstable
   * Fix segmentation fault in php-gd (Closes: #543496)
+  * Update suhosin patch to 0.9.8 *BETA* and enable it again
 
  -- Ondřej Surý <ondrej at debian.org>  Tue, 25 Aug 2009 14:46:40 +0200
 
diff --git a/debian/patches/series b/debian/patches/series
index 3a779f6..3530dee 100644
--- a/debian/patches/series
+++ b/debian/patches/series
@@ -24,6 +24,7 @@ libtool2.2.patch
 113-php.ini_securitynotes.patch
 114-php_gd_segfault.patch
 libdb_is_-ldb
+suhosin.patch
 fix_broken_upstream_tests.patch
 use_embedded_timezonedb.patch
 manpage_spelling.patch
diff --git a/debian/patches/suhosin.patch b/debian/patches/suhosin.patch
index fb122ce..09c913e 100644
--- a/debian/patches/suhosin.patch
+++ b/debian/patches/suhosin.patch
@@ -2,269 +2,30 @@ suhosin hardening patch
 
 this patch was downloaded from:
 
-  http://download.suhosin.org/suhosin-patch-5.2.10-0.9.7.patch.gz
+  http://www.suspekt.org/downloads/suhosin-patch-5.3.0-0.9.8-BETA-1.patch
 
 the following modifications have been made:
 
  * removed changes to ./configure since this file is autogenerated
  * "quilt refresh" has been run to clean up the offsets, etc
---- php.orig/TSRM/TSRM.h
-+++ php/TSRM/TSRM.h
-@@ -40,6 +40,13 @@ typedef long tsrm_intptr_t;
- typedef unsigned long tsrm_uintptr_t;
- #endif
- 
-+#if SUHOSIN_PATCH
-+# if HAVE_REALPATH
-+#  undef realpath
-+#  define realpath php_realpath
-+# endif
-+#endif
-+
- /* Only compile multi-threading functions if we're in ZTS mode */
- #ifdef ZTS
- 
-@@ -95,6 +102,7 @@ typedef void (*ts_allocate_dtor)(void *,
- 
- #define THREAD_HASH_OF(thr,ts)  (unsigned long)thr%(unsigned long)ts
- 
-+
- #ifdef __cplusplus
- extern "C" {
- #endif
---- php.orig/TSRM/tsrm_virtual_cwd.c
-+++ php/TSRM/tsrm_virtual_cwd.c
-@@ -279,6 +279,191 @@ static void cwd_globals_dtor(virtual_cwd
- }
- /* }}} */
- 
-+#if SUHOSIN_PATCH
-+CWD_API char *php_realpath(const char *path, char *resolved)
-+{
-+     struct stat sb;
-+     char *p, *q, *s;
-+     size_t left_len, resolved_len;
-+     unsigned symlinks;
-+     int serrno, slen;
-+     int is_dir = 1;
-+     char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
-+
-+     serrno = errno;
-+     symlinks = 0;
-+     if (path[0] == '/') {
-+             resolved[0] = '/';
-+             resolved[1] = '\0';
-+             if (path[1] == '\0')
-+                 return (resolved);
-+             resolved_len = 1;
-+             left_len = strlcpy(left, path + 1, sizeof(left));
-+     } else {
-+             if (getcwd(resolved, PATH_MAX) == NULL) {
-+                     strlcpy(resolved, ".", PATH_MAX);
-+                     return (NULL);
-+             }
-+             resolved_len = strlen(resolved);
-+             left_len = strlcpy(left, path, sizeof(left));
-+     }
-+     if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
-+             errno = ENAMETOOLONG;
-+             return (NULL);
-+     }
-+
-+     /*
-+      * Iterate over path components in `left'.
-+      */
-+     while (left_len != 0) {
-+             /*
-+              * Extract the next path component and adjust `left'
-+              * and its length.
-+              */
-+             p = strchr(left, '/');
-+             s = p ? p : left + left_len;
-+             if (s - left >= sizeof(next_token)) {
-+                     errno = ENAMETOOLONG;
-+                     return (NULL);
-+             }
-+             memcpy(next_token, left, s - left);
-+             next_token[s - left] = '\0';
-+             left_len -= s - left;
-+             if (p != NULL)
-+                     memmove(left, s + 1, left_len + 1);
-+             if (resolved[resolved_len - 1] != '/') {
-+                     if (resolved_len + 1 >= PATH_MAX) {
-+                             errno = ENAMETOOLONG;
-+                             return (NULL);
-+                     }
-+                     resolved[resolved_len++] = '/';
-+                     resolved[resolved_len] = '\0';
-+             }
-+             if (next_token[0] == '\0')
-+                     continue;
-+             else if (strcmp(next_token, ".") == 0){
-+                     if (!is_dir) {
-+                             resolved_len = strlcat(resolved, "#", PATH_MAX);
-+                             if (resolved_len >= PATH_MAX) {
-+                                     errno = ENAMETOOLONG;
-+                                     return (NULL);
-+                             }
-+                             return resolved;
-+                     }
-+                     continue;
-+             } else if (strcmp(next_token, "..") == 0) {
-+                     /*
-+                      * Strip the last path component except when we have
-+                      * single "/"
-+                      */
-+                     if (!is_dir) {
-+                             errno = ENOENT;
-+                             return (NULL);
-+                     }
-+                     if (resolved_len > 1) {
-+                             resolved[resolved_len - 1] = '\0';
-+                             q = strrchr(resolved, '/');
-+                             *q = '\0';
-+                             resolved_len = q - resolved;
-+                     }
-+                     continue;
-+             }
-+
-+             /*
-+              * Append the next path component and lstat() it. If
-+              * lstat() fails we still can return successfully if
-+              * there are no more path components left.
-+              */
-+             resolved_len = strlcat(resolved, next_token, PATH_MAX);
-+             if (resolved_len >= PATH_MAX) {
-+                     errno = ENAMETOOLONG;
-+                     return (NULL);
-+             }
-+             if (lstat(resolved, &sb) != 0) {
-+                     if (errno == ENOENT) {
-+		     if (p == NULL) {
-+	         		errno = serrno;
-+				return NULL;
-+                     		return (resolved);
-+    			} /*else if (strstr(left, "/.") == NULL && strstr(left, "./") == NULL) {
-+         				resolved_len = strlcat(resolved, "/", PATH_MAX);
-+         				resolved_len = strlcat(resolved, left, PATH_MAX);
-+         				if (resolved_len >= PATH_MAX) {
-+                 				errno = ENAMETOOLONG;
-+                 				return (NULL);
-+         				}
-+                         		errno = serrno;
-+                         		return (resolved);
-+				} */
-+                     }
-+                     return (NULL);
-+             }
-+             if (S_ISLNK(sb.st_mode)) {
-+                     if (symlinks++ > MAXSYMLINKS) {
-+                             errno = ELOOP;
-+                             return (NULL);
-+            	     }
-+                     slen = readlink(resolved, symlink, sizeof(symlink) - 1);
-+                     if (slen < 0)
-+                             return (NULL);
-+                     symlink[slen] = '\0';
-+                     if (symlink[0] == '/') {
-+                             resolved[1] = 0;
-+                             resolved_len = 1;
-+                     } else if (resolved_len > 1) {
-+                             /* Strip the last path component. */
-+                             resolved[resolved_len - 1] = '\0';
-+                             q = strrchr(resolved, '/');
-+                             *q = '\0';
-+                             resolved_len = q - resolved;
-+                     }
-+
-+                     /*
-+                      * If there are any path components left, then
-+                      * append them to symlink. The result is placed
-+                      * in `left'.
-+                      */
-+                     if (p != NULL) {
-+                             if (symlink[slen - 1] != '/') {
-+                                     if (slen + 1 >= sizeof(symlink)) {
-+                                             errno = ENAMETOOLONG;
-+                                             return (NULL);
-+                                     }
-+                                     symlink[slen] = '/';
-+                                     symlink[slen + 1] = 0;
-+                             }
-+                             left_len = strlcat(symlink, left, sizeof(left));
-+                             if (left_len >= sizeof(left)) {
-+                                     errno = ENAMETOOLONG;
-+                                     return (NULL);
-+                             }
-+                     }
-+                     left_len = strlcpy(left, symlink, sizeof(left));
-+             } else {
-+         		if (S_ISDIR(sb.st_mode)) {
-+                 		is_dir = 1;
-+         		} else {
-+                 		is_dir = 0;
-+         		}
-+	    }
-+     }
-+
-+     /*
-+      * Remove trailing slash except when the resolved pathname
-+      * is a single "/".
-+      */
-+     if (resolved_len > 1 && resolved[resolved_len - 1] == '/') {
-+             if (!is_dir) {
-+                     errno = ENOENT;
-+                     return (NULL);
-+             }
-+             resolved[resolved_len - 1] = '\0';
-+     }
-+     return (resolved);
-+}
-+#endif
-+
-+
- CWD_API void virtual_cwd_startup(void) /* {{{ */
- {
- 	char cwd[MAXPATHLEN];
---- php.orig/TSRM/tsrm_virtual_cwd.h
-+++ php/TSRM/tsrm_virtual_cwd.h
-@@ -141,6 +141,22 @@ typedef struct _cwd_state {
- 
- typedef int (*verify_path_func)(const cwd_state *);
- 
-+#ifndef HAVE_STRLCPY
-+CWD_API size_t php_strlcpy(char *dst, const char *src, size_t siz);
-+#undef strlcpy
-+#define strlcpy php_strlcpy
-+#endif
-+
-+#ifndef HAVE_STRLCAT
-+CWD_API size_t php_strlcat(char *dst, const char *src, size_t siz);
-+#undef strlcat
-+#define strlcat php_strlcat
-+#endif
-+
-+
-+#if SUHOSIN_PATCH
-+CWD_API char *php_realpath(const char *path, char *resolved);
-+#endif
- CWD_API void virtual_cwd_startup(void);
- CWD_API void virtual_cwd_shutdown(void);
- CWD_API char *virtual_getcwd_ex(size_t *length TSRMLS_DC);
---- php.orig/Zend/Makefile.am
-+++ php/Zend/Makefile.am
-@@ -17,7 +17,7 @@ libZend_la_SOURCES=\
+Index: pkg-php/Zend/Makefile.am
+===================================================================
+--- pkg-php.orig/Zend/Makefile.am	2009-08-25 15:11:47.000000000 +0200
++++ pkg-php/Zend/Makefile.am	2009-08-25 15:13:09.000000000 +0200
+@@ -17,7 +17,7 @@
  	zend_objects_API.c zend_ts_hash.c zend_stream.c \
  	zend_default_classes.c \
  	zend_iterators.c zend_interfaces.c zend_exceptions.c \
 -	zend_strtod.c zend_closures.c zend_float.c
-+	zend_strtod.c zend_closures.c zend_float.c zend_canary.c
++	zend_strtod.c zend_closures.c zend_float.c zend_canary.c zend_alloc_canary.c 
  
  libZend_la_LDFLAGS =
  libZend_la_LIBADD = @ZEND_EXTRA_LIBS@
---- php.orig/Zend/Zend.dsp
-+++ php/Zend/Zend.dsp
-@@ -239,6 +239,10 @@ SOURCE=.\zend_strtod.c
+Index: pkg-php/Zend/Zend.dsp
+===================================================================
+--- pkg-php.orig/Zend/Zend.dsp	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/Zend/Zend.dsp	2009-08-25 15:13:09.000000000 +0200
+@@ -247,6 +247,14 @@
  # End Source File
  # Begin Source File
  
@@ -272,12 +33,18 @@ the following modifications have been made:
 +# End Source File
 +# Begin Source File
 +
++SOURCE=.\zend_alloc_canary.c
++# End Source File
++# Begin Source File
++
  SOURCE=.\zend_ts_hash.c
  # End Source File
  # Begin Source File
---- php.orig/Zend/ZendTS.dsp
-+++ php/Zend/ZendTS.dsp
-@@ -273,6 +273,10 @@ SOURCE=.\zend_strtod.c
+Index: pkg-php/Zend/ZendTS.dsp
+===================================================================
+--- pkg-php.orig/Zend/ZendTS.dsp	2009-08-25 15:11:47.000000000 +0200
++++ pkg-php/Zend/ZendTS.dsp	2009-08-25 15:13:09.000000000 +0200
+@@ -277,6 +277,14 @@
  # End Source File
  # Begin Source File
  
@@ -285,22 +52,29 @@ the following modifications have been made:
 +# End Source File
 +# Begin Source File
 +
++SOURCE=.\zend_alloc_canary.c
++# End Source File
++# Begin Source File
++
  SOURCE=.\zend_ts_hash.c
  # End Source File
  # Begin Source File
---- php.orig/Zend/zend.c
-+++ php/Zend/zend.c
-@@ -60,6 +60,9 @@ int (*zend_vspprintf)(char **pbuf, size_
+Index: pkg-php/Zend/zend.c
+===================================================================
+--- pkg-php.orig/Zend/zend.c	2009-08-25 15:11:47.000000000 +0200
++++ pkg-php/Zend/zend.c	2009-08-25 15:13:09.000000000 +0200
+@@ -60,6 +60,10 @@
  ZEND_API char *(*zend_getenv)(char *name, size_t name_len TSRMLS_DC);
  ZEND_API char *(*zend_resolve_path)(const char *filename, int filename_len TSRMLS_DC);
  
 +#if SUHOSIN_PATCH
 +ZEND_API void (*zend_suhosin_log)(int loglevel, char *fmt, ...);
 +#endif
++
  void (*zend_on_timeout)(int seconds TSRMLS_DC);
  
  static void (*zend_message_dispatcher_p)(long message, void *data TSRMLS_DC);
-@@ -88,8 +91,88 @@ static ZEND_INI_MH(OnUpdateGCEnabled) /*
+@@ -88,6 +92,74 @@
  }
  /* }}} */
  
@@ -374,24 +148,12 @@ the following modifications have been made:
 +
  ZEND_INI_BEGIN()
  	ZEND_INI_ENTRY("error_reporting",				NULL,		ZEND_INI_ALL,		OnUpdateErrorReporting)
-+#if SUHOSIN_PATCH
-+	ZEND_INI_ENTRY("suhosin.log.syslog",			NULL,		ZEND_INI_PERDIR|ZEND_INI_SYSTEM,	OnUpdateSuhosin_log_syslog)
-+	ZEND_INI_ENTRY("suhosin.log.syslog.facility",		NULL,		ZEND_INI_PERDIR|ZEND_INI_SYSTEM,	OnUpdateSuhosin_log_syslog_facility)
-+	ZEND_INI_ENTRY("suhosin.log.syslog.priority",		NULL,		ZEND_INI_PERDIR|ZEND_INI_SYSTEM,	OnUpdateSuhosin_log_syslog_priority)
-+	ZEND_INI_ENTRY("suhosin.log.sapi",				NULL,		ZEND_INI_PERDIR|ZEND_INI_SYSTEM,	OnUpdateSuhosin_log_sapi)
-+	ZEND_INI_ENTRY("suhosin.log.script",			NULL,		ZEND_INI_PERDIR|ZEND_INI_SYSTEM,	OnUpdateSuhosin_log_script)
-+	ZEND_INI_ENTRY("suhosin.log.script.name",			NULL,		ZEND_INI_SYSTEM,	OnUpdateSuhosin_log_scriptname)
-+	STD_ZEND_INI_BOOLEAN("suhosin.log.use-x-forwarded-for",	"0",		ZEND_INI_PERDIR|ZEND_INI_SYSTEM,	OnUpdateBool, log_use_x_forwarded_for,	suhosin_patch_globals_struct,	suhosin_patch_globals)
-+	ZEND_INI_ENTRY("suhosin.log.phpscript",			"0",		ZEND_INI_PERDIR|ZEND_INI_SYSTEM,	OnUpdateSuhosin_log_phpscript)
-+	STD_ZEND_INI_ENTRY("suhosin.log.phpscript.name",			NULL,		ZEND_INI_PERDIR|ZEND_INI_SYSTEM,	OnUpdateString, log_phpscriptname, suhosin_patch_globals_struct,	suhosin_patch_globals)
-+	STD_ZEND_INI_BOOLEAN("suhosin.log.phpscript.is_safe",			"0",		ZEND_INI_SYSTEM,	OnUpdateBool, log_phpscript_is_safe,	suhosin_patch_globals_struct,	suhosin_patch_globals)
-+#endif
  	STD_ZEND_INI_BOOLEAN("zend.enable_gc",				"1",	ZEND_INI_ALL,		OnUpdateGCEnabled,      gc_enabled,     zend_gc_globals,        gc_globals)
- #ifdef ZEND_MULTIBYTE
-  	STD_ZEND_INI_BOOLEAN("detect_unicode", "1", ZEND_INI_ALL, OnUpdateBool, detect_unicode, zend_compiler_globals, compiler_globals)
---- php.orig/Zend/zend.h
-+++ php/Zend/zend.h
-@@ -634,6 +634,9 @@ extern ZEND_API int (*zend_stream_open_f
+Index: pkg-php/Zend/zend.h
+===================================================================
+--- pkg-php.orig/Zend/zend.h	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/Zend/zend.h	2009-08-25 15:13:09.000000000 +0200
+@@ -645,6 +645,9 @@
  extern int (*zend_vspprintf)(char **pbuf, size_t max_len, const char *format, va_list ap);
  extern ZEND_API char *(*zend_getenv)(char *name, size_t name_len TSRMLS_DC);
  extern ZEND_API char *(*zend_resolve_path)(const char *filename, int filename_len TSRMLS_DC);
@@ -401,12 +163,13 @@ the following modifications have been made:
  
  ZEND_API void zend_error(int type, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
  
-@@ -773,6 +776,13 @@ ZEND_API void zend_save_error_handling(z
+@@ -784,6 +787,14 @@
  ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current TSRMLS_DC);
  ZEND_API void zend_restore_error_handling(zend_error_handling *saved TSRMLS_DC);
  
 +#if SUHOSIN_PATCH
 +#include "suhosin_globals.h"
++#include "suhosin_patch.h"
 +#include "php_syslog.h"
 +
 +ZEND_API size_t zend_canary();
@@ -415,13 +178,42 @@ the following modifications have been made:
  #endif /* ZEND_H */
  
  /*
---- php.orig/Zend/zend_alloc.c
-+++ php/Zend/zend_alloc.c
-@@ -319,13 +319,26 @@ static const zend_mm_mem_handlers mem_ha
+Index: pkg-php/Zend/zend_alloc.c
+===================================================================
+--- pkg-php.orig/Zend/zend_alloc.c	2009-08-25 15:11:47.000000000 +0200
++++ pkg-php/Zend/zend_alloc.c	2009-08-25 15:13:09.000000000 +0200
+@@ -32,6 +32,10 @@
+ # include <unistd.h>
+ #endif
+ 
++#if SUHOSIN_PATCH
++#include "suhosin_patch.h"
++#endif
++
+ #ifdef ZEND_WIN32
+ # include <wincrypt.h>
+ # include <process.h>
+@@ -59,6 +63,7 @@
+ # define PTR_FMT "0x%0.8lx"
+ #endif
+ 
++#ifndef SUHOSIN_MM_CLONE_FILE
+ #if ZEND_DEBUG
+ void zend_debug_alloc_output(char *format, ...)
+ {
+@@ -76,6 +81,7 @@
+ #endif
+ }
+ #endif
++#endif
+ 
+ #if (defined (__GNUC__) && __GNUC__ > 2 ) && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX)
+ static void zend_mm_panic(const char *message) __attribute__ ((noreturn));
+@@ -324,13 +330,28 @@
  #define	MEM_BLOCK_GUARD  0x2A8FCC84
  #define	MEM_BLOCK_LEAK   0x6C5E8F2D
  
-+#if SUHOSIN_PATCH
++#if SUHOSIN_MM_WITH_CANARY_PROTECTION
 +# define CANARY_SIZE sizeof(size_t)
 +#else
 +# define CANARY_SIZE 0
@@ -434,29 +226,40 @@ the following modifications have been made:
  #endif
 -	size_t _size;
 -	size_t _prev;
-+#if SUHOSIN_PATCH
++#if SUHOSIN_MM_WITH_CANARY_PROTECTION
 +	size_t canary_1;
 +#endif
 +  	size_t _size;
 +  	size_t _prev;
 +#if SUHOSIN_PATCH
 +	size_t size;
++#if SUHOSIN_MM_WITH_CANARY_PROTECTION
 +	size_t canary_2;
 +#endif
++#endif
  } zend_mm_block_info;
  
  #if ZEND_DEBUG
-@@ -434,6 +447,9 @@ struct _zend_mm_heap {
+@@ -404,7 +425,7 @@
+ # define ZEND_MM_CACHE_STAT 0
+ #endif
+ 
+-struct _zend_mm_heap {
++typedef struct _zend_mm_heap {
+ 	int                 use_zend_alloc;
+ 	void               *(*_malloc)(size_t);
+ 	void                (*_free)(void*);
+@@ -439,6 +460,9 @@
  		int miss;
  	} cache_stat[ZEND_MM_NUM_BUCKETS+1];
  #endif
 +#if SUHOSIN_PATCH
-+	size_t              canary_1,canary_2,canary_3;
++ 	size_t              canary_1,canary_2,canary_3;
 +#endif
  };
  
  #define ZEND_MM_SMALL_FREE_BUCKET(heap, index) \
-@@ -523,15 +539,15 @@ static unsigned int _zend_mm_cookie = 0;
+@@ -528,15 +552,15 @@
  #define ZEND_MM_ALIGNED_SIZE(size)			((size + ZEND_MM_ALIGNMENT - 1) & ZEND_MM_ALIGNMENT_MASK)
  #define ZEND_MM_ALIGNED_HEADER_SIZE			ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_block))
  #define ZEND_MM_ALIGNED_FREE_HEADER_SIZE	ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_small_free_block))
@@ -475,48 +278,44 @@ the following modifications have been made:
  
  #define ZEND_MM_BUCKET_INDEX(true_size)		((true_size>>ZEND_MM_ALIGNMENT_LOG2)-(ZEND_MM_ALIGNED_MIN_HEADER_SIZE>>ZEND_MM_ALIGNMENT_LOG2))
  
-@@ -593,6 +609,48 @@ static unsigned int _zend_mm_cookie = 0;
+@@ -598,6 +622,44 @@
  
  #endif
  
-+#if SUHOSIN_PATCH
++#if SUHOSIN_MM_WITH_CANARY_PROTECTION
 +
 +# define SUHOSIN_MM_CHECK_CANARIES(block, MFUNCTION) do { \
 +        char *p = SUHOSIN_MM_END_CANARY_PTR(block); size_t check; \
-+		if (((block)->info.canary_1 != heap->canary_1) || ((block)->info.canary_2 != heap->canary_2)) { \
-+			canary_mismatch: \
-+            zend_suhosin_log(S_MEMORY, "canary mismatch on " MFUNCTION " - heap overflow detected"); \
-+            exit(1); \
-+		} \
++	if (((block)->info.canary_1 != heap->canary_1) || ((block)->info.canary_2 != heap->canary_2)) { \
++		canary_mismatch: \
++		zend_suhosin_log(S_MEMORY, "canary mismatch on " MFUNCTION " - heap overflow detected at %p", (block)); \
++                if (SUHOSIN_CONFIG(SUHOSIN_MM_IGNORE_CANARY_VIOLATION) == 0) { _exit(1); } else { (block)->info.canary_1 = heap->canary_1; (block)->info.canary_2 = heap->canary_2; }\
++	} \
 +        memcpy(&check, p, CANARY_SIZE); \
 +        if (check != heap->canary_3) { \
-+            zend_suhosin_log(S_MEMORY, "canary mismatch on " MFUNCTION " - heap overflow detected"); \
-+            exit(1); \
-+            goto canary_mismatch; \
++                zend_suhosin_log(S_MEMORY, "end canary mismatch on " MFUNCTION " - heap overflow detected at %p", (block)); \
++                if (SUHOSIN_CONFIG(SUHOSIN_MM_IGNORE_CANARY_VIOLATION) == 0) { _exit(1); } else { memcpy(p, heap->canary_3, CANARY_SIZE); } \
 +        } \
 +	} while (0)
 +
 +# define SUHOSIN_MM_SET_CANARIES(block) do { \
-+      (block)->info.canary_1 = heap->canary_1; \
-+      (block)->info.canary_2 = heap->canary_2; \
-+    } while (0)      
++        (block)->info.canary_1 = heap->canary_1; \
++        (block)->info.canary_2 = heap->canary_2; \
++        } while (0)      
 +
 +# define SUHOSIN_MM_END_CANARY_PTR(block) \
 +	(char *)(((char*)(ZEND_MM_DATA_OF(block))) + ((zend_mm_block*)(block))->info.size + END_MAGIC_SIZE)
 +
 +# define SUHOSIN_MM_SET_END_CANARY(block) do { \
-+		char *p = SUHOSIN_MM_END_CANARY_PTR(block); \
-+		memcpy(p, &heap->canary_3, CANARY_SIZE); \
++	char *p = SUHOSIN_MM_END_CANARY_PTR(block); \
++	memcpy(p, &heap->canary_3, CANARY_SIZE); \
 +	} while (0)
 +
 +#else
 +
-+# define SUHOSIN_MM_CHECK_CANARIES(block)
-+
++# define SUHOSIN_MM_CHECK_CANARIES(block, MFUNCTION)
 +# define SUHOSIN_MM_SET_CANARIES(block)
-+
 +# define SUHOSIN_MM_END_CANARY_PTR(block)
-+
 +# define SUHOSIN_MM_SET_END_CANARY(block)
 +
 +#endif
@@ -524,111 +323,343 @@ the following modifications have been made:
  
  #if ZEND_MM_HEAP_PROTECTION
  
-@@ -801,6 +859,12 @@ static inline void zend_mm_remove_from_f
+@@ -718,7 +780,7 @@
+ #endif
+ }
+ 
+-static inline void zend_mm_add_to_rest_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
++static void zend_mm_add_to_rest_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
+ {
+ 	zend_mm_free_block *prev, *next;
+ 
+@@ -735,7 +797,7 @@
+ 	prev->next_free_block = next->prev_free_block = mm_block;
+ }
+ 
+-static inline void zend_mm_add_to_free_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
++static void zend_mm_add_to_free_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
+ {
+ 	size_t size;
+ 	size_t index;
+@@ -796,7 +858,7 @@
+ 	}
+ }
+ 
+-static inline void zend_mm_remove_from_free_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
++static void zend_mm_remove_from_free_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
+ {
+ 	zend_mm_free_block *prev = mm_block->prev_free_block;
+ 	zend_mm_free_block *next = mm_block->next_free_block;
+@@ -806,6 +868,12 @@
  	if (EXPECTED(prev == mm_block)) {
  		zend_mm_free_block **rp, **cp;
  
 +#if SUHOSIN_PATCH
-+        if (next != mm_block) {
-+ 		    zend_suhosin_log(S_MEMORY, "heap corrupt on efree() - heap corruption detected");
-+		    exit(1);
-+        }
++                if (next != mm_block) {
++                        zend_suhosin_log(S_MEMORY, "zend_mm_heap corrupted at %p", mm_block);
++                        _exit(1);
++                }
 +#endif
  #if ZEND_MM_SAFE_UNLINKING
  		if (UNEXPECTED(next != mm_block)) {
  			zend_mm_panic("zend_mm_heap corrupted");
-@@ -839,6 +903,12 @@ subst_block:
+@@ -844,6 +912,13 @@
  		}
  	} else {
  
 +#if SUHOSIN_PATCH
-+        if (prev->next_free_block != mm_block || next->prev_free_block != mm_block) {
-+ 		    zend_suhosin_log(S_MEMORY, "linked list corrupt on efree() - heap corruption detected");
-+		    exit(1);
-+        }
++                if (prev->next_free_block != mm_block || next->prev_free_block != mm_block) {
++                        zend_suhosin_log(S_MEMORY, "zend_mm_head corrupted at %p", mm_block);
++		        _exit(1);
++                }
 +#endif    
++
  #if ZEND_MM_SAFE_UNLINKING
  		if (UNEXPECTED(prev->next_free_block != mm_block) || UNEXPECTED(next->prev_free_block != mm_block)) {
  			zend_mm_panic("zend_mm_heap corrupted");
-@@ -886,6 +956,11 @@ static inline void zend_mm_init(zend_mm_
+@@ -867,7 +942,7 @@
+ 	}
+ }
+ 
+-static inline void zend_mm_init(zend_mm_heap *heap)
++static void zend_mm_init(zend_mm_heap *heap)
+ {
+ 	zend_mm_free_block* p;
+ 	int i;
+@@ -891,6 +966,13 @@
  		heap->large_free_buckets[i] = NULL;
  	}
  	heap->rest_buckets[0] = heap->rest_buckets[1] = ZEND_MM_REST_BUCKET(heap);
 +#if SUHOSIN_PATCH
-+	heap->canary_1 = zend_canary();
-+	heap->canary_2 = zend_canary();
-+	heap->canary_3 = zend_canary();
-+#endif    
++        if (SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION)) {
++	        heap->canary_1 = zend_canary();
++	        heap->canary_2 = zend_canary();
++	        heap->canary_3 = zend_canary();
++	}
++#endif
  }
  
  static void zend_mm_del_segment(zend_mm_heap *heap, zend_mm_segment *segment)
-@@ -1785,6 +1860,11 @@ static void *_zend_mm_alloc_int(zend_mm_
+@@ -999,11 +1081,16 @@
+ }
+ #endif
+ 
++
+ /* Notes:
+  * - This function may alter the block_sizes values to match platform alignment
+  * - This function does *not* perform sanity checks on the arguments
+  */
+-ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params)
++#if SUHOSIN_MM_WITH_CANARY_PROTECTION
++zend_mm_heap *__zend_mm_startup_canary_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params)
++#else
++static zend_mm_heap *__zend_mm_startup_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params)
++#endif
+ {
+ 	zend_mm_storage *storage;
+ 	zend_mm_heap    *heap;
+@@ -1073,12 +1160,12 @@
+ 	heap->reserve = NULL;
+ 	heap->reserve_size = reserve_size;
+ 	if (reserve_size > 0) {
+-		heap->reserve = _zend_mm_alloc_int(heap, reserve_size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
++		heap->reserve = _zend_mm_alloc(heap, reserve_size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
+ 	}
+ 	if (internal) {
+ 		int i;
+ 		zend_mm_free_block *p, *q, *orig;
+-		zend_mm_heap *mm_heap = _zend_mm_alloc_int(heap, sizeof(zend_mm_heap)  ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
++		zend_mm_heap *mm_heap = _zend_mm_alloc(heap, sizeof(zend_mm_heap)  ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
+ 
+ 		*mm_heap = *heap;
+ 
+@@ -1109,7 +1196,11 @@
+ 	return heap;
+ }
+ 
+-ZEND_API zend_mm_heap *zend_mm_startup(void)
++#if SUHOSIN_MM_WITH_CANARY_PROTECTION
++zend_mm_heap *__zend_mm_startup_canary(void)
++#else
++static zend_mm_heap *__zend_mm_startup(void)
++#endif
+ {
+ 	int i;
+ 	size_t seg_size;
+@@ -1163,6 +1254,27 @@
+ 	return heap;
+ }
+ 
++#ifndef SUHOSIN_MM_CLONE_FILE
++zend_mm_heap_canary *__zend_mm_startup_canary_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params);
++zend_mm_heap_canary *__zend_mm_startup_canary(void);
++
++ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params)
++{
++        if (SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION)) {
++                return (zend_mm_heap *)__zend_mm_startup_canary_ex(handlers, block_size, reserve_size, internal, params);
++        }
++        return __zend_mm_startup_ex(handlers, block_size, reserve_size, internal, params);
++}
++ZEND_API zend_mm_heap *zend_mm_startup(void)
++{
++        if (SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION)) {
++                return (zend_mm_heap *)__zend_mm_startup_canary();
++        }
++        return __zend_mm_startup();        
++}
++
++#endif
++
+ #if ZEND_DEBUG
+ static long zend_mm_find_leaks(zend_mm_segment *segment, zend_mm_block *b)
+ {
+@@ -1531,7 +1643,11 @@
+ }
+ #endif
+ 
+-ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC)
++#if SUHOSIN_MM_WITH_CANARY_PROTECTION
++void __zend_mm_shutdown_canary(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC)
++#else
++static void __zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC)
++#endif
+ {
+ 	zend_mm_storage *storage;
+ 	zend_mm_segment *segment;
+@@ -1541,7 +1657,7 @@
+ 	if (heap->reserve) {
+ #if ZEND_DEBUG
+ 		if (!silent) {
+-			_zend_mm_free_int(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
++			_zend_mm_free(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
+ 		}
+ #endif
+ 		heap->reserve = NULL;
+@@ -1624,12 +1740,23 @@
+ 		heap->size = 0;
+ 		heap->peak = 0;
+ 		if (heap->reserve_size) {
+-			heap->reserve = _zend_mm_alloc_int(heap, heap->reserve_size  ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
++			heap->reserve = _zend_mm_alloc(heap, heap->reserve_size  ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
+ 		}
+ 		heap->overflow = 0;
+ 	}
+ }
+ 
++#ifndef SUHOSIN_MM_CLONE_FILE
++ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC)
++{
++        if (SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION)) {
++                __zend_mm_shutdown_canary(heap, full_shutdown, silent TSRMLS_CC);
++                return;
++        }
++        __zend_mm_shutdown(heap, full_shutdown, silent TSRMLS_CC);
++}
++#endif
++
+ static void zend_mm_safe_error(zend_mm_heap *heap,
+ 	const char *format,
+ 	size_t limit,
+@@ -1640,7 +1767,11 @@
+ 	size_t size)
+ {
+ 	if (heap->reserve) {
++#if SUHOSIN_MM_WITH_CANARY_PROTECTION	        
++		_zend_mm_free_canary_int(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
++#else
+ 		_zend_mm_free_int(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
++#endif
+ 		heap->reserve = NULL;
+ 	}
+ 	if (heap->overflow == 0) {
+@@ -1763,6 +1894,9 @@
+ 	return best_fit->next_free_block;
+ }
+ 
++#if SUHOSIN_PATCH
++void *_zend_mm_alloc_canary_int(zend_mm_heap_canary *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
++#endif
+ static void *_zend_mm_alloc_int(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
+ {
+ 	zend_mm_free_block *best_fit;
+@@ -1772,7 +1906,7 @@
+ 	size_t segment_size;
+ 	zend_mm_segment *segment;
+ 	int keep_rest = 0;
+-
++	
+ 	if (EXPECTED(ZEND_MM_SMALL_SIZE(true_size))) {
+ 		size_t index = ZEND_MM_BUCKET_INDEX(true_size);
+ 		size_t bitmap;
+@@ -1790,6 +1924,11 @@
  			best_fit = heap->cache[index];
  			heap->cache[index] = best_fit->prev_free_block;
  			heap->cached -= true_size;
 +#if SUHOSIN_PATCH
-+            SUHOSIN_MM_SET_CANARIES(best_fit);
-+            ((zend_mm_block*)best_fit)->info.size = size;
-+            SUHOSIN_MM_SET_END_CANARY(best_fit);
-+#endif
++                        SUHOSIN_MM_SET_CANARIES(best_fit);
++                        ((zend_mm_block*)best_fit)->info.size = size;
++                        SUHOSIN_MM_SET_END_CANARY(best_fit);
++#endif			
  			ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_CACHED);
  			ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);
  			return ZEND_MM_DATA_OF(best_fit);
-@@ -1924,6 +2004,12 @@ zend_mm_finished_searching_for_block:
+@@ -1929,13 +2068,19 @@
  
  	ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 1);
  
 +#if SUHOSIN_PATCH
-+    SUHOSIN_MM_SET_CANARIES(best_fit);
-+    ((zend_mm_block*)best_fit)->info.size = size;
-+    SUHOSIN_MM_SET_END_CANARY(best_fit);
++        SUHOSIN_MM_SET_CANARIES(best_fit);
++        ((zend_mm_block*)best_fit)->info.size = size;
++        SUHOSIN_MM_SET_END_CANARY(best_fit);
 +#endif
-+
++        
  	heap->size += true_size;
  	if (heap->peak < heap->size) {
  		heap->peak = heap->size;
-@@ -1947,6 +2033,9 @@ static void _zend_mm_free_int(zend_mm_he
+ 	}
+ 
+ 	HANDLE_UNBLOCK_INTERRUPTIONS();
+-
++	
+ 	return ZEND_MM_DATA_OF(best_fit);
+ }
+ 
+@@ -1952,12 +2097,19 @@
  
  	mm_block = ZEND_MM_HEADER_OF(p);
  	size = ZEND_MM_BLOCK_SIZE(mm_block);
 +#if SUHOSIN_PATCH
-+    SUHOSIN_MM_CHECK_CANARIES(mm_block, "efree()");
++        SUHOSIN_MM_CHECK_CANARIES(mm_block, "efree()");
 +#endif    
  	ZEND_MM_CHECK_PROTECTION(mm_block);
  
  #if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
-@@ -2009,6 +2098,9 @@ static void *_zend_mm_realloc_int(zend_m
+ 	memset(ZEND_MM_DATA_OF(mm_block), 0x5a, mm_block->debug.size);
+ #endif
+-
++#if SUHOSIN_PATCH
++        if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_DESTROY_FREE_MEMORY))) {
++                memset(ZEND_MM_DATA_OF(mm_block), 0x5a, mm_block->info.size);
++        }
++#endif
+ #if ZEND_MM_CACHE
+ 	if (EXPECTED(ZEND_MM_SMALL_SIZE(size)) && EXPECTED(heap->cached < ZEND_MM_CACHE_SIZE)) {
+ 		size_t index = ZEND_MM_BUCKET_INDEX(size);
+@@ -2000,6 +2152,9 @@
+ 	HANDLE_UNBLOCK_INTERRUPTIONS();
+ }
+ 
++#if SUHOSIN_PATCH
++void *_zend_mm_realloc_canary_int(zend_mm_heap_canary *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
++#endif
+ static void *_zend_mm_realloc_int(zend_mm_heap *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
+ {
+ 	zend_mm_block *mm_block = ZEND_MM_HEADER_OF(p);
+@@ -2009,11 +2164,18 @@
+ 	void *ptr;
+ 
+ 	if (UNEXPECTED(!p) || !ZEND_MM_VALID_PTR(p)) {
++#ifdef SUHOSIN_MM_WITH_CANARY_PROTECTION
++		return _zend_mm_alloc_canary_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#else
+ 		return _zend_mm_alloc_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#endif
+ 	}
  	mm_block = ZEND_MM_HEADER_OF(p);
  	true_size = ZEND_MM_TRUE_SIZE(size);
  	orig_size = ZEND_MM_BLOCK_SIZE(mm_block);
 +#if SUHOSIN_PATCH
-+    SUHOSIN_MM_CHECK_CANARIES(mm_block, "erealloc()");
-+#endif    
++        SUHOSIN_MM_CHECK_CANARIES(mm_block, "erealloc()");
++#endif	
  	ZEND_MM_CHECK_PROTECTION(mm_block);
  
  	if (UNEXPECTED(true_size < size)) {
-@@ -2040,6 +2132,11 @@ static void *_zend_mm_realloc_int(zend_m
+@@ -2045,6 +2207,11 @@
  			HANDLE_UNBLOCK_INTERRUPTIONS();
  		}
  		ZEND_MM_SET_DEBUG_INFO(mm_block, size, 0, 0);
 +#if SUHOSIN_PATCH
-+        SUHOSIN_MM_SET_CANARIES(mm_block);
-+        ((zend_mm_block*)mm_block)->info.size = size;
-+        SUHOSIN_MM_SET_END_CANARY(mm_block);
++                SUHOSIN_MM_SET_CANARIES(mm_block);
++                ((zend_mm_block*)mm_block)->info.size = size;
++                SUHOSIN_MM_SET_END_CANARY(mm_block);
 +#endif
  		return p;
  	}
  
-@@ -2059,13 +2156,18 @@ static void *_zend_mm_realloc_int(zend_m
+@@ -2063,14 +2230,19 @@
+ 			best_fit = heap->cache[index];
  			heap->cache[index] = best_fit->prev_free_block;
  			ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_CACHED);
- 			ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);
+-			ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);
+-	
++			ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);                        	
 +#if SUHOSIN_PATCH
-+            SUHOSIN_MM_SET_CANARIES(best_fit);
-+            ((zend_mm_block*)best_fit)->info.size = size;
-+            SUHOSIN_MM_SET_END_CANARY(best_fit);
++                        SUHOSIN_MM_SET_CANARIES(best_fit);
++                        ((zend_mm_block*)best_fit)->info.size = size;
++                        SUHOSIN_MM_SET_END_CANARY(best_fit);
 +#endif
- 	
++
  			ptr = ZEND_MM_DATA_OF(best_fit);
  
  #if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
@@ -639,77 +670,2700 @@ the following modifications have been made:
  #endif
  
  			heap->cached -= true_size - orig_size;
-@@ -2123,6 +2225,11 @@ static void *_zend_mm_realloc_int(zend_m
- 			if (heap->peak < heap->size) {
+@@ -2086,7 +2258,6 @@
+ 				heap->cache_stat[index].max_count = heap->cache_stat[index].count;
+ 			}
+ #endif
+-
+ 			return ptr;
+ 		}
+ 	}
+@@ -2129,6 +2300,11 @@
  				heap->peak = heap->size;
  			}
+ 			HANDLE_UNBLOCK_INTERRUPTIONS();
 +#if SUHOSIN_PATCH
-+            SUHOSIN_MM_SET_CANARIES(mm_block);
-+            ((zend_mm_block*)mm_block)->info.size = size;
-+            SUHOSIN_MM_SET_END_CANARY(mm_block);
++                        SUHOSIN_MM_SET_CANARIES(mm_block);
++                        ((zend_mm_block*)mm_block)->info.size = size;
++                        SUHOSIN_MM_SET_END_CANARY(mm_block);
 +#endif
- 			HANDLE_UNBLOCK_INTERRUPTIONS();
  			return p;
  		} else if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
-@@ -2226,6 +2333,11 @@ out_of_memory:
+ 				   ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_BLOCK_AT(next_block, ZEND_MM_FREE_BLOCK_SIZE(next_block)))) {
+@@ -2231,38 +2407,90 @@
  		}
  
  		HANDLE_UNBLOCK_INTERRUPTIONS();
 +#if SUHOSIN_PATCH
-+        SUHOSIN_MM_SET_CANARIES(mm_block);
-+        ((zend_mm_block*)mm_block)->info.size = size;
-+        SUHOSIN_MM_SET_END_CANARY(mm_block);
++                SUHOSIN_MM_SET_CANARIES(mm_block);
++                ((zend_mm_block*)mm_block)->info.size = size;
++                SUHOSIN_MM_SET_END_CANARY(mm_block);
 +#endif
  		return ZEND_MM_DATA_OF(mm_block);
  	}
  
-@@ -2233,7 +2345,7 @@ out_of_memory:
++#ifdef SUHOSIN_MM_WITH_CANARY_PROTECTION
++	ptr = _zend_mm_alloc_canary_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#else
+ 	ptr = _zend_mm_alloc_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#endif
  #if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
  	memcpy(ptr, p, mm_block->debug.size);
  #else
 -	memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE);
 +	memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE - CANARY_SIZE);
  #endif
++#ifdef SUHOSIN_MM_WITH_CANARY_PROTECTION
++	_zend_mm_free_canary_int(heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#else
  	_zend_mm_free_int(heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#endif
  	return ptr;
-@@ -2509,6 +2621,17 @@ ZEND_API void shutdown_memory_manager(in
- 	zend_mm_shutdown(AG(mm_heap), full_shutdown, silent TSRMLS_CC);
  }
  
++#ifndef SUHOSIN_MM_CLONE_FILE
+ ZEND_API void *_zend_mm_alloc(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
+ {
+-	return _zend_mm_alloc_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#if SUHOSIN_PATCH	
++	if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) == 0))
++#endif
++	        return _zend_mm_alloc_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
 +#if SUHOSIN_PATCH
-+ZEND_API void suhosin_clear_mm_canaries(TSRMLS_D)
-+{
-+/*  NOT HERE
++        return _zend_mm_alloc_canary_int((zend_mm_heap_canary *)heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#endif
+ }
+ 
+ ZEND_API void _zend_mm_free(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
+ {
+-	_zend_mm_free_int(heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#if SUHOSIN_PATCH	
++	if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) == 0))
++#endif
++                { _zend_mm_free_int(heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); return; }
++#if SUHOSIN_PATCH
++        _zend_mm_free_canary_int((zend_mm_heap_canary *)heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#endif
+ }
+ 
+ ZEND_API void *_zend_mm_realloc(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
+ {
+-	return _zend_mm_realloc_int(heap, ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#if SUHOSIN_PATCH	
++	if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) == 0))
++#endif
++        	return _zend_mm_realloc_int(heap, ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#if SUHOSIN_PATCH	
++	return _zend_mm_realloc_canary_int((zend_mm_heap_canary *)heap, ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#endif
+ }
+ 
+ ZEND_API size_t _zend_mm_block_size(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
+ {
+ 	zend_mm_block *mm_block;
+ 
++	if (SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) != 0) {
++                return _zend_mm_block_size_canary((zend_mm_heap_canary *)heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++	}
 +
-+    AG(mm_heap)->canary_1 = zend_canary();
-+    AG(mm_heap)->canary_2 = zend_canary();
-+    AG(mm_heap)->canary_3 = zend_canary(); */
++	if (!ZEND_MM_VALID_PTR(p)) {
++		return 0;
++	}
++	mm_block = ZEND_MM_HEADER_OF(p);
++	ZEND_MM_CHECK_PROTECTION(mm_block);
++#if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
++	return mm_block->debug.size;
++#else
++	return ZEND_MM_BLOCK_SIZE(mm_block);
++#endif
 +}
++#else
++ZEND_API size_t _zend_mm_block_size_canary(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
++{
++	zend_mm_block *mm_block;
++
+ 	if (!ZEND_MM_VALID_PTR(p)) {
+ 		return 0;
+ 	}
+@@ -2275,6 +2503,8 @@
+ #endif
+ }
+ 
 +#endif
 +
+ /**********************/
+ /* Allocation Manager */
+ /**********************/
+@@ -2291,6 +2521,7 @@
+ static zend_alloc_globals alloc_globals;
+ #endif
+ 
++#ifndef SUHOSIN_MM_CLONE_FILE
+ ZEND_API int is_zend_mm(TSRMLS_D)
+ {
+ 	return AG(mm_heap)->use_zend_alloc;
+@@ -2303,7 +2534,13 @@
+ 	if (UNEXPECTED(!AG(mm_heap)->use_zend_alloc)) {
+ 		return AG(mm_heap)->_malloc(size);
+ 	}
++#if SUHOSIN_PATCH	
++	if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) == 0))
++#endif
+ 	return _zend_mm_alloc_int(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#if SUHOSIN_PATCH	
++        return _zend_mm_alloc_canary_int((zend_mm_heap_canary *)AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#endif
+ }
+ 
+ ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
+@@ -2314,7 +2551,13 @@
+ 		AG(mm_heap)->_free(ptr);
+ 		return;
+ 	}
+-	_zend_mm_free_int(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#if SUHOSIN_PATCH	
++	if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) == 0))
++#endif
++                { _zend_mm_free_int(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); return; }
++#if SUHOSIN_PATCH	
++        _zend_mm_free_canary_int((zend_mm_heap_canary *)AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#endif
+ }
+ 
+ ZEND_API void *_erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
+@@ -2324,7 +2567,13 @@
+ 	if (UNEXPECTED(!AG(mm_heap)->use_zend_alloc)) {
+ 		return AG(mm_heap)->_realloc(ptr, size);
+ 	}
++#if SUHOSIN_PATCH	
++	if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) == 0))
++#endif
+ 	return _zend_mm_realloc_int(AG(mm_heap), ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#if SUHOSIN_PATCH	
++        return _zend_mm_realloc_canary_int((zend_mm_heap_canary *)AG(mm_heap), ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#endif
+ }
+ 
+ ZEND_API size_t _zend_mem_block_size(void *ptr TSRMLS_DC ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
+@@ -2332,8 +2581,15 @@
+ 	if (UNEXPECTED(!AG(mm_heap)->use_zend_alloc)) {
+ 		return 0;
+ 	}
+-	return _zend_mm_block_size(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#if SUHOSIN_PATCH	
++	if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) == 0))
++#endif
++        	return _zend_mm_block_size(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#if SUHOSIN_PATCH	
++	return _zend_mm_block_size_canary((zend_mm_heap_canary *)AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#endif
+ }
++#endif
+ 
+ #if defined(__GNUC__) && defined(i386)
+ 
+@@ -2404,7 +2660,7 @@
+ }
+ #endif
+ 
+-
++#ifndef SUHOSIN_MM_CLONE_FILE
+ ZEND_API void *_safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
+ {
+ 	return emalloc_rel(safe_address(nmemb, size, offset));
+@@ -2517,6 +2773,7 @@
+ {
+ 	zend_mm_shutdown(AG(mm_heap), full_shutdown, silent TSRMLS_CC);
+ }
++#endif
+ 
  static void alloc_globals_ctor(zend_alloc_globals *alloc_globals TSRMLS_DC)
  {
- 	char *tmp;
---- php.orig/Zend/zend_alloc.h
-+++ php/Zend/zend_alloc.h
-@@ -129,6 +129,9 @@ ZEND_API int zend_set_memory_limit(size_
+@@ -2541,6 +2798,7 @@
+ }
+ #endif
  
- ZEND_API void start_memory_manager(TSRMLS_D);
- ZEND_API void shutdown_memory_manager(int silent, int full_shutdown TSRMLS_DC);
-+#if SUHOSIN_PATCH
-+ZEND_API void suhosin_clear_mm_canaries(TSRMLS_D);
++#ifndef SUHOSIN_MM_CLONE_FILE
+ ZEND_API void start_memory_manager(TSRMLS_D)
+ {
+ #ifdef ZTS
+@@ -2605,6 +2863,7 @@
+ 	zend_debug_alloc_output("------------------------------------------------\n");
+ }
+ #endif
 +#endif
- ZEND_API int is_zend_mm(TSRMLS_D);
  
- #if ZEND_DEBUG
---- /dev/null
-+++ php/Zend/zend_canary.c
+ /*
+  * Local variables:
+Index: pkg-php/Zend/zend_alloc.h
+===================================================================
+--- pkg-php.orig/Zend/zend_alloc.h	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/Zend/zend_alloc.h	2009-08-25 15:13:09.000000000 +0200
+@@ -189,6 +189,8 @@
+ 
+ /* Heap functions */
+ typedef struct _zend_mm_heap zend_mm_heap;
++typedef struct _zend_mm_heap_canary zend_mm_heap_canary;
++
+ 
+ ZEND_API zend_mm_heap *zend_mm_startup(void);
+ ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC);
+Index: pkg-php/Zend/zend_alloc_canary.c
+===================================================================
+--- /dev/null	1970-01-01 00:00:00.000000000 +0000
++++ pkg-php/Zend/zend_alloc_canary.c	2009-08-25 15:13:09.000000000 +0200
+@@ -0,0 +1,2441 @@
++/*
++   +----------------------------------------------------------------------+
++   | Suhosin-Patch for PHP                                                |
++   +----------------------------------------------------------------------+
++   | Copyright (c) 2004-2009 Stefan Esser                                 |
++   +----------------------------------------------------------------------+
++   | This source file is subject to version 2.02 of the PHP license,      |
++   | that is bundled with this package in the file LICENSE, and is        |
++   | available at through the world-wide-web at                           |
++   | http://www.php.net/license/2_02.txt.                                 |
++   | If you did not receive a copy of the PHP license and are unable to   |
++   | obtain it through the world-wide-web, please send a note to          |
++   | license at php.net so we can mail you a copy immediately.               |
++   +----------------------------------------------------------------------+
++   | Author: Stefan Esser <stefan.esser at sektioneins.de>                   |
++   +----------------------------------------------------------------------+
++ */
++/* $Id: zend_alloc_canary.c, $ */
++
++#include "zend.h"
++#include "zend_alloc.h"
++#include "zend_globals.h"
++#include "zend_operators.h"
++
++#ifdef HAVE_SIGNAL_H
++# include <signal.h>
++#endif
++#ifdef HAVE_UNISTD_H
++# include <unistd.h>
++#endif
++
++#if SUHOSIN_PATCH
++#include "suhosin_patch.h"
++#endif
++
++#ifdef ZEND_WIN32
++# include <wincrypt.h>
++# include <process.h>
++#endif
++
++#ifndef ZEND_MM_HEAP_PROTECTION
++# define ZEND_MM_HEAP_PROTECTION ZEND_DEBUG
++#endif
++
++#ifndef ZEND_MM_SAFE_UNLINKING
++# define ZEND_MM_SAFE_UNLINKING 1
++#endif
++
++#ifndef ZEND_MM_COOKIES
++# define ZEND_MM_COOKIES ZEND_DEBUG
++#endif
++
++#ifdef _WIN64
++# define PTR_FMT "0x%0.16I64x"
++/*
++#elif sizeof(long) == 8
++# define PTR_FMT "0x%0.16lx"
++*/
++#else
++# define PTR_FMT "0x%0.8lx"
++#endif
++
++#define SUHOSIN_MM_WITH_CANARY_PROTECTION 1
++
++#if (defined (__GNUC__) && __GNUC__ > 2 ) && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX)
++static void zend_mm_panic(const char *message) __attribute__ ((noreturn));
++#endif
++
++static void zend_mm_panic(const char *message)
++{
++	fprintf(stderr, "%s\n", message);
++#if ZEND_DEBUG && defined(HAVE_KILL) && defined(HAVE_GETPID)
++	kill(getpid(), SIGSEGV);
++#endif
++	exit(1);
++}
++
++/*******************/
++/* Storage Manager */
++/*******************/
++
++#ifdef ZEND_WIN32
++#  define HAVE_MEM_WIN32    /* use VirtualAlloc() to allocate memory     */
++#endif
++#define HAVE_MEM_MALLOC     /* use malloc() to allocate segments         */
++
++#include <sys/types.h>
++#include <sys/stat.h>
++#if HAVE_LIMITS_H
++#include <limits.h>
++#endif
++#include <fcntl.h>
++#include <errno.h>
++
++#if defined(HAVE_MEM_MMAP_ANON) || defined(HAVE_MEM_MMAP_ZERO)
++# ifdef HAVE_MREMAP
++#  ifndef _GNU_SOURCE
++#   define _GNU_SOURCE
++#  endif
++#  ifndef __USE_GNU
++#   define __USE_GNU
++#  endif
++# endif
++# include <sys/mman.h>
++# ifndef MAP_ANON
++#  ifdef MAP_ANONYMOUS
++#   define MAP_ANON MAP_ANONYMOUS
++#  endif
++# endif
++# ifndef MREMAP_MAYMOVE
++#  define MREMAP_MAYMOVE 0
++# endif
++# ifndef MAP_FAILED
++#  define MAP_FAILED ((void*)-1)
++# endif
++#endif
++
++static zend_mm_storage* zend_mm_mem_dummy_init(void *params)
++{
++	return malloc(sizeof(zend_mm_storage));
++}
++
++static void zend_mm_mem_dummy_dtor(zend_mm_storage *storage)
++{
++	free(storage);
++}
++
++static void zend_mm_mem_dummy_compact(zend_mm_storage *storage)
++{
++}
++
++#if defined(HAVE_MEM_MMAP_ANON) || defined(HAVE_MEM_MMAP_ZERO)
++
++static zend_mm_segment* zend_mm_mem_mmap_realloc(zend_mm_storage *storage, zend_mm_segment* segment, size_t size)
++{
++	zend_mm_segment *ret;
++#ifdef HAVE_MREMAP
++#if defined(__NetBSD__)
++	/* NetBSD 5 supports mremap but takes an extra newp argument */
++	ret = (zend_mm_segment*)mremap(segment, segment->size, segment, size, MREMAP_MAYMOVE);
++#else
++	ret = (zend_mm_segment*)mremap(segment, segment->size, size, MREMAP_MAYMOVE);
++#endif
++	if (ret == MAP_FAILED) {
++#endif
++		ret = storage->handlers->_alloc(storage, size);
++		if (ret) {
++			memcpy(ret, segment, size > segment->size ? segment->size : size);
++			storage->handlers->_free(storage, segment);
++		}
++#ifdef HAVE_MREMAP
++	}
++#endif
++	return ret;
++}
++
++static void zend_mm_mem_mmap_free(zend_mm_storage *storage, zend_mm_segment* segment)
++{
++	munmap((void*)segment, segment->size);
++}
++
++#endif
++
++#ifdef HAVE_MEM_MMAP_ANON
++
++static zend_mm_segment* zend_mm_mem_mmap_anon_alloc(zend_mm_storage *storage, size_t size)
++{
++	zend_mm_segment *ret = (zend_mm_segment*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
++	if (ret == MAP_FAILED) {
++		ret = NULL;
++	}
++	return ret;
++}
++
++# define ZEND_MM_MEM_MMAP_ANON_DSC {"mmap_anon", zend_mm_mem_dummy_init, zend_mm_mem_dummy_dtor, zend_mm_mem_dummy_compact, zend_mm_mem_mmap_anon_alloc, zend_mm_mem_mmap_realloc, zend_mm_mem_mmap_free}
++
++#endif
++
++#ifdef HAVE_MEM_MMAP_ZERO
++
++static int zend_mm_dev_zero_fd = -1;
++
++static zend_mm_storage* zend_mm_mem_mmap_zero_init(void *params)
++{
++	if (zend_mm_dev_zero_fd != -1) {
++		zend_mm_dev_zero_fd = open("/dev/zero", O_RDWR, S_IRUSR | S_IWUSR);
++	}
++	if (zend_mm_dev_zero_fd >= 0) {
++		return malloc(sizeof(zend_mm_storage));
++	} else {
++		return NULL;
++	}
++}
++
++static void zend_mm_mem_mmap_zero_dtor(zend_mm_storage *storage)
++{
++	close(zend_mm_dev_zero_fd);
++	free(storage);
++}
++
++static zend_mm_segment* zend_mm_mem_mmap_zero_alloc(zend_mm_storage *storage, size_t size)
++{
++	zend_mm_segment *ret = (zend_mm_segment*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, zend_mm_dev_zero_fd, 0);
++	if (ret == MAP_FAILED) {
++		ret = NULL;
++	}
++	return ret;
++}
++
++# define ZEND_MM_MEM_MMAP_ZERO_DSC {"mmap_zero", zend_mm_mem_mmap_zero_init, zend_mm_mem_mmap_zero_dtor, zend_mm_mem_dummy_compact, zend_mm_mem_mmap_zero_alloc, zend_mm_mem_mmap_realloc, zend_mm_mem_mmap_free}
++
++#endif
++
++#ifdef HAVE_MEM_WIN32
++
++static zend_mm_storage* zend_mm_mem_win32_init(void *params)
++{
++	HANDLE heap = HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
++	zend_mm_storage* storage;
++
++	if (heap == NULL) {
++		return NULL;
++	}
++	storage = (zend_mm_storage*)malloc(sizeof(zend_mm_storage));
++	storage->data = (void*) heap;
++	return storage;
++}
++
++static void zend_mm_mem_win32_dtor(zend_mm_storage *storage)
++{
++	HeapDestroy((HANDLE)storage->data);
++	free(storage);
++}
++
++static void zend_mm_mem_win32_compact(zend_mm_storage *storage)
++{
++    HeapDestroy((HANDLE)storage->data);
++    storage->data = (void*)HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
++}
++
++static zend_mm_segment* zend_mm_mem_win32_alloc(zend_mm_storage *storage, size_t size)
++{
++	return (zend_mm_segment*) HeapAlloc((HANDLE)storage->data, HEAP_NO_SERIALIZE, size);
++}
++
++static void zend_mm_mem_win32_free(zend_mm_storage *storage, zend_mm_segment* segment)
++{
++	HeapFree((HANDLE)storage->data, HEAP_NO_SERIALIZE, segment);
++}
++
++static zend_mm_segment* zend_mm_mem_win32_realloc(zend_mm_storage *storage, zend_mm_segment* segment, size_t size)
++{
++	return (zend_mm_segment*) HeapReAlloc((HANDLE)storage->data, HEAP_NO_SERIALIZE, segment, size);
++}
++
++# define ZEND_MM_MEM_WIN32_DSC {"win32", zend_mm_mem_win32_init, zend_mm_mem_win32_dtor, zend_mm_mem_win32_compact, zend_mm_mem_win32_alloc, zend_mm_mem_win32_realloc, zend_mm_mem_win32_free}
++
++#endif
++
++#ifdef HAVE_MEM_MALLOC
++
++static zend_mm_segment* zend_mm_mem_malloc_alloc(zend_mm_storage *storage, size_t size)
++{
++	return (zend_mm_segment*)malloc(size);
++}
++
++static zend_mm_segment* zend_mm_mem_malloc_realloc(zend_mm_storage *storage, zend_mm_segment *ptr, size_t size)
++{
++	return (zend_mm_segment*)realloc(ptr, size);
++}
++
++static void zend_mm_mem_malloc_free(zend_mm_storage *storage, zend_mm_segment *ptr)
++{
++	free(ptr);
++}
++
++# define ZEND_MM_MEM_MALLOC_DSC {"malloc", zend_mm_mem_dummy_init, zend_mm_mem_dummy_dtor, zend_mm_mem_dummy_compact, zend_mm_mem_malloc_alloc, zend_mm_mem_malloc_realloc, zend_mm_mem_malloc_free}
++
++#endif
++
++static const zend_mm_mem_handlers mem_handlers[] = {
++#ifdef HAVE_MEM_WIN32
++	ZEND_MM_MEM_WIN32_DSC,
++#endif
++#ifdef HAVE_MEM_MALLOC
++	ZEND_MM_MEM_MALLOC_DSC,
++#endif
++#ifdef HAVE_MEM_MMAP_ANON
++	ZEND_MM_MEM_MMAP_ANON_DSC,
++#endif
++#ifdef HAVE_MEM_MMAP_ZERO
++	ZEND_MM_MEM_MMAP_ZERO_DSC,
++#endif
++	{NULL, NULL, NULL, NULL, NULL, NULL}
++};
++
++# define ZEND_MM_STORAGE_DTOR()						heap->storage->handlers->dtor(heap->storage)
++# define ZEND_MM_STORAGE_ALLOC(size)				heap->storage->handlers->_alloc(heap->storage, size)
++# define ZEND_MM_STORAGE_REALLOC(ptr, size)			heap->storage->handlers->_realloc(heap->storage, ptr, size)
++# define ZEND_MM_STORAGE_FREE(ptr)					heap->storage->handlers->_free(heap->storage, ptr)
++
++/****************/
++/* Heap Manager */
++/****************/
++
++#define MEM_BLOCK_VALID  0x7312F8DC
++#define	MEM_BLOCK_FREED  0x99954317
++#define	MEM_BLOCK_CACHED 0xFB8277DC
++#define	MEM_BLOCK_GUARD  0x2A8FCC84
++#define	MEM_BLOCK_LEAK   0x6C5E8F2D
++
++#if SUHOSIN_MM_WITH_CANARY_PROTECTION
++# define CANARY_SIZE sizeof(size_t)
++#else
++# define CANARY_SIZE 0
++#endif
++
++/* mm block type */
++typedef struct _zend_mm_block_info_canary {
++#if ZEND_MM_COOKIES
++	size_t _cookie;
++#endif
++#if SUHOSIN_MM_WITH_CANARY_PROTECTION
++	size_t canary_1;
++#endif
++  	size_t _size;
++  	size_t _prev;
++#if SUHOSIN_PATCH
++	size_t size;
++#if SUHOSIN_MM_WITH_CANARY_PROTECTION
++	size_t canary_2;
++#endif
++#endif
++} zend_mm_block_info_canary;
++
++#if ZEND_DEBUG
++
++typedef struct _zend_mm_debug_info_canary {
++	char *filename;
++	uint lineno;
++	char *orig_filename;
++	uint orig_lineno;
++	size_t size;
++#if ZEND_MM_HEAP_PROTECTION
++	unsigned int start_magic;
++#endif
++} zend_mm_debug_info_canary;
++
++#elif ZEND_MM_HEAP_PROTECTION
++
++typedef struct _zend_mm_debug_info_canary {
++	size_t size;
++	unsigned int start_magic;
++} zend_mm_debug_info_canary;
++
++#endif
++
++typedef struct _zend_mm_block_canary {
++	zend_mm_block_info_canary info;
++#if ZEND_DEBUG
++	unsigned int magic;
++# ifdef ZTS
++	THREAD_T thread_id;
++# endif
++	zend_mm_debug_info_canary debug;
++#elif ZEND_MM_HEAP_PROTECTION
++	zend_mm_debug_info_canary debug;
++#endif
++} zend_mm_block_canary;
++
++typedef struct _zend_mm_small_free_block_canary {
++	zend_mm_block_info_canary info;
++#if ZEND_DEBUG
++	unsigned int magic;
++# ifdef ZTS
++	THREAD_T thread_id;
++# endif
++#endif
++	struct _zend_mm_free_block_canary *prev_free_block;
++	struct _zend_mm_free_block_canary *next_free_block;
++} zend_mm_small_free_block_canary;
++
++typedef struct _zend_mm_free_block_canary {
++	zend_mm_block_info_canary info;
++#if ZEND_DEBUG
++	unsigned int magic;
++# ifdef ZTS
++	THREAD_T thread_id;
++# endif
++#endif
++	struct _zend_mm_free_block_canary *prev_free_block;
++	struct _zend_mm_free_block_canary *next_free_block;
++
++	struct _zend_mm_free_block_canary **parent;
++	struct _zend_mm_free_block_canary *child[2];
++} zend_mm_free_block_canary;
++
++#define ZEND_MM_NUM_BUCKETS (sizeof(size_t) << 3)
++
++#define ZEND_MM_CACHE 1
++#define ZEND_MM_CACHE_SIZE (ZEND_MM_NUM_BUCKETS * 4 * 1024)
++
++#ifndef ZEND_MM_CACHE_STAT
++# define ZEND_MM_CACHE_STAT 0
++#endif
++
++typedef struct _zend_mm_heap_canary {
++	int                 use_zend_alloc;
++	void               *(*_malloc)(size_t);
++	void                (*_free)(void*);
++	void               *(*_realloc)(void*, size_t);
++	size_t              free_bitmap;
++	size_t              large_free_bitmap;
++	size_t              block_size;
++	size_t              compact_size;
++	zend_mm_segment    *segments_list;
++	zend_mm_storage    *storage;
++	size_t              real_size;
++	size_t              real_peak;
++	size_t              limit;
++	size_t              size;
++	size_t              peak;
++	size_t              reserve_size;
++	void               *reserve;
++	int                 overflow;
++	int                 internal;
++#if ZEND_MM_CACHE
++	unsigned int        cached;
++	zend_mm_free_block_canary *cache[ZEND_MM_NUM_BUCKETS];
++#endif
++	zend_mm_free_block_canary *free_buckets[ZEND_MM_NUM_BUCKETS*2];
++	zend_mm_free_block_canary *large_free_buckets[ZEND_MM_NUM_BUCKETS];
++	zend_mm_free_block_canary *rest_buckets[2];
++#if ZEND_MM_CACHE_STAT
++	struct {
++		int count;
++		int max_count;
++		int hit;
++		int miss;
++	} cache_stat[ZEND_MM_NUM_BUCKETS+1];
++#endif
++#if SUHOSIN_PATCH
++ 	size_t              canary_1,canary_2,canary_3;
++#endif
++};
++
++#define ZEND_MM_SMALL_FREE_BUCKET(heap, index) \
++	(zend_mm_free_block_canary*) ((char*)&heap->free_buckets[index * 2] + \
++		sizeof(zend_mm_free_block_canary*) * 2 - \
++		sizeof(zend_mm_small_free_block_canary))
++
++#define ZEND_MM_REST_BUCKET(heap) \
++	(zend_mm_free_block_canary*)((char*)&heap->rest_buckets[0] + \
++		sizeof(zend_mm_free_block_canary*) * 2 - \
++		sizeof(zend_mm_small_free_block_canary))
++
++#if ZEND_MM_COOKIES
++
++static unsigned int _zend_mm_cookie = 0;
++
++# define ZEND_MM_COOKIE(block) \
++	(((size_t)(block)) ^ _zend_mm_cookie)
++# define ZEND_MM_SET_COOKIE(block) \
++	(block)->info._cookie = ZEND_MM_COOKIE(block)
++# define ZEND_MM_CHECK_COOKIE(block) \
++	if (UNEXPECTED((block)->info._cookie != ZEND_MM_COOKIE(block))) { \
++		zend_mm_panic("zend_mm_heap corrupted"); \
++	}
++#else
++# define ZEND_MM_SET_COOKIE(block)
++# define ZEND_MM_CHECK_COOKIE(block)
++#endif
++
++/* Default memory segment size */
++#define ZEND_MM_SEG_SIZE   (256 * 1024)
++
++/* Reserved space for error reporting in case of memory overflow */
++#define ZEND_MM_RESERVE_SIZE            (8*1024)
++
++#ifdef _WIN64
++# define ZEND_MM_LONG_CONST(x)	(x##i64)
++#else
++# define ZEND_MM_LONG_CONST(x)	(x##L)
++#endif
++
++#define ZEND_MM_TYPE_MASK		ZEND_MM_LONG_CONST(0x3)
++
++#define ZEND_MM_FREE_BLOCK		ZEND_MM_LONG_CONST(0x0)
++#define ZEND_MM_USED_BLOCK		ZEND_MM_LONG_CONST(0x1)
++#define ZEND_MM_GUARD_BLOCK		ZEND_MM_LONG_CONST(0x3)
++
++#define ZEND_MM_BLOCK(b, type, size)	do { \
++											size_t _size = (size); \
++											(b)->info._size = (type) | _size; \
++											ZEND_MM_BLOCK_AT(b, _size)->info._prev = (type) | _size; \
++											ZEND_MM_SET_COOKIE(b); \
++										} while (0);
++#define ZEND_MM_LAST_BLOCK(b)			do { \
++		(b)->info._size = ZEND_MM_GUARD_BLOCK | ZEND_MM_ALIGNED_HEADER_SIZE; \
++		ZEND_MM_SET_MAGIC(b, MEM_BLOCK_GUARD); \
++ 	} while (0);
++#define ZEND_MM_BLOCK_SIZE(b)			((b)->info._size & ~ZEND_MM_TYPE_MASK)
++#define ZEND_MM_IS_FREE_BLOCK(b)		(!((b)->info._size & ZEND_MM_USED_BLOCK))
++#define ZEND_MM_IS_USED_BLOCK(b)		((b)->info._size & ZEND_MM_USED_BLOCK)
++#define ZEND_MM_IS_GUARD_BLOCK(b)		(((b)->info._size & ZEND_MM_TYPE_MASK) == ZEND_MM_GUARD_BLOCK)
++
++#define ZEND_MM_NEXT_BLOCK(b)			ZEND_MM_BLOCK_AT(b, ZEND_MM_BLOCK_SIZE(b))
++#define ZEND_MM_PREV_BLOCK(b)			ZEND_MM_BLOCK_AT(b, -(int)((b)->info._prev & ~ZEND_MM_TYPE_MASK))
++
++#define ZEND_MM_PREV_BLOCK_IS_FREE(b)	(!((b)->info._prev & ZEND_MM_USED_BLOCK))
++
++#define ZEND_MM_MARK_FIRST_BLOCK(b)		((b)->info._prev = ZEND_MM_GUARD_BLOCK)
++#define ZEND_MM_IS_FIRST_BLOCK(b)		((b)->info._prev == ZEND_MM_GUARD_BLOCK)
++
++/* optimized access */
++#define ZEND_MM_FREE_BLOCK_SIZE(b)		(b)->info._size
++
++#ifndef ZEND_MM_ALIGNMENT
++# define ZEND_MM_ALIGNMENT 8
++# define ZEND_MM_ALIGNMENT_LOG2 3
++#elif ZEND_MM_ALIGNMENT < 4
++# undef ZEND_MM_ALIGNMENT
++# undef ZEND_MM_ALIGNMENT_LOG2
++# define ZEND_MM_ALIGNMENT 4
++# define ZEND_MM_ALIGNMENT_LOG2 2
++#endif
++
++#define ZEND_MM_ALIGNMENT_MASK ~(ZEND_MM_ALIGNMENT-1)
++
++/* Aligned header size */
++#define ZEND_MM_ALIGNED_SIZE(size)			((size + ZEND_MM_ALIGNMENT - 1) & ZEND_MM_ALIGNMENT_MASK)
++#define ZEND_MM_ALIGNED_HEADER_SIZE			ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_block_canary))
++#define ZEND_MM_ALIGNED_FREE_HEADER_SIZE	ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_small_free_block_canary))
++#define ZEND_MM_MIN_ALLOC_BLOCK_SIZE		ZEND_MM_ALIGNED_SIZE(ZEND_MM_ALIGNED_HEADER_SIZE + END_MAGIC_SIZE + CANARY_SIZE)
++#define ZEND_MM_ALIGNED_MIN_HEADER_SIZE		(ZEND_MM_MIN_ALLOC_BLOCK_SIZE>ZEND_MM_ALIGNED_FREE_HEADER_SIZE?ZEND_MM_MIN_ALLOC_BLOCK_SIZE:ZEND_MM_ALIGNED_FREE_HEADER_SIZE)
++#define ZEND_MM_ALIGNED_SEGMENT_SIZE		ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_segment))
++
++#define ZEND_MM_MIN_SIZE					((ZEND_MM_ALIGNED_MIN_HEADER_SIZE>(ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE+CANARY_SIZE))?(ZEND_MM_ALIGNED_MIN_HEADER_SIZE-(ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE+CANARY_SIZE)):0)
++
++#define ZEND_MM_MAX_SMALL_SIZE				((ZEND_MM_NUM_BUCKETS<<ZEND_MM_ALIGNMENT_LOG2)+ZEND_MM_ALIGNED_MIN_HEADER_SIZE)
++
++#define ZEND_MM_TRUE_SIZE(size)				((size<ZEND_MM_MIN_SIZE)?(ZEND_MM_ALIGNED_MIN_HEADER_SIZE):(ZEND_MM_ALIGNED_SIZE(size+ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE+CANARY_SIZE)))
++
++#define ZEND_MM_BUCKET_INDEX(true_size)		((true_size>>ZEND_MM_ALIGNMENT_LOG2)-(ZEND_MM_ALIGNED_MIN_HEADER_SIZE>>ZEND_MM_ALIGNMENT_LOG2))
++
++#define ZEND_MM_SMALL_SIZE(true_size)		(true_size < ZEND_MM_MAX_SMALL_SIZE)
++
++/* Memory calculations */
++#define ZEND_MM_BLOCK_AT(blk, offset)	((zend_mm_block_canary *) (((char *) (blk))+(offset)))
++#define ZEND_MM_DATA_OF(p)				((void *) (((char *) (p))+ZEND_MM_ALIGNED_HEADER_SIZE))
++#define ZEND_MM_HEADER_OF(blk)			ZEND_MM_BLOCK_AT(blk, -(int)ZEND_MM_ALIGNED_HEADER_SIZE)
++
++/* Debug output */
++#if ZEND_DEBUG
++
++# ifdef ZTS
++#  define ZEND_MM_SET_THREAD_ID(block) \
++	((zend_mm_block_canary*)(block))->thread_id = tsrm_thread_id()
++#  define ZEND_MM_BAD_THREAD_ID(block) ((block)->thread_id != tsrm_thread_id())
++# else
++#  define ZEND_MM_SET_THREAD_ID(block)
++#  define ZEND_MM_BAD_THREAD_ID(block) 0
++# endif
++
++# define ZEND_MM_VALID_PTR(block) \
++	zend_mm_check_ptr(heap, block, 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC)
++
++# define ZEND_MM_SET_MAGIC(block, val) do { \
++		(block)->magic = (val); \
++	} while (0)
++
++# define ZEND_MM_CHECK_MAGIC(block, val) do { \
++		if ((block)->magic != (val)) { \
++			zend_mm_panic("zend_mm_heap corrupted"); \
++		} \
++	} while (0)
++
++# define ZEND_MM_SET_DEBUG_INFO(block, __size, set_valid, set_thread) do { \
++		((zend_mm_block_canary*)(block))->debug.filename = __zend_filename; \
++		((zend_mm_block_canary*)(block))->debug.lineno = __zend_lineno; \
++		((zend_mm_block_canary*)(block))->debug.orig_filename = __zend_orig_filename; \
++		((zend_mm_block_canary*)(block))->debug.orig_lineno = __zend_orig_lineno; \
++		ZEND_MM_SET_BLOCK_SIZE(block, __size); \
++		if (set_valid) { \
++			ZEND_MM_SET_MAGIC(block, MEM_BLOCK_VALID); \
++		} \
++		if (set_thread) { \
++			ZEND_MM_SET_THREAD_ID(block); \
++		} \
++	} while (0)
++
++#else
++
++# define ZEND_MM_VALID_PTR(ptr) EXPECTED(ptr != NULL)
++
++# define ZEND_MM_SET_MAGIC(block, val)
++
++# define ZEND_MM_CHECK_MAGIC(block, val)
++
++# define ZEND_MM_SET_DEBUG_INFO(block, __size, set_valid, set_thread) ZEND_MM_SET_BLOCK_SIZE(block, __size)
++
++#endif
++
++#if SUHOSIN_MM_WITH_CANARY_PROTECTION
++
++# define SUHOSIN_MM_CHECK_CANARIES(block, MFUNCTION) do { \
++        char *p = SUHOSIN_MM_END_CANARY_PTR(block); size_t check; \
++	if (((block)->info.canary_1 != heap->canary_1) || ((block)->info.canary_2 != heap->canary_2)) { \
++		canary_mismatch: \
++		zend_suhosin_log(S_MEMORY, "canary mismatch on " MFUNCTION " - heap overflow detected at %p", (block)); \
++                if (SUHOSIN_CONFIG(SUHOSIN_MM_IGNORE_CANARY_VIOLATION) == 0) { _exit(1); } else { (block)->info.canary_1 = heap->canary_1; (block)->info.canary_2 = heap->canary_2; }\
++	} \
++        memcpy(&check, p, CANARY_SIZE); \
++        if (check != heap->canary_3) { \
++                zend_suhosin_log(S_MEMORY, "end canary mismatch on " MFUNCTION " - heap overflow detected at %p", (block)); \
++                if (SUHOSIN_CONFIG(SUHOSIN_MM_IGNORE_CANARY_VIOLATION) == 0) { _exit(1); } else { memcpy(p, heap->canary_3, CANARY_SIZE); } \
++        } \
++	} while (0)
++
++# define SUHOSIN_MM_SET_CANARIES(block) do { \
++        (block)->info.canary_1 = heap->canary_1; \
++        (block)->info.canary_2 = heap->canary_2; \
++        } while (0)      
++
++# define SUHOSIN_MM_END_CANARY_PTR(block) \
++	(char *)(((char*)(ZEND_MM_DATA_OF(block))) + ((zend_mm_block_canary*)(block))->info.size + END_MAGIC_SIZE)
++
++# define SUHOSIN_MM_SET_END_CANARY(block) do { \
++	char *p = SUHOSIN_MM_END_CANARY_PTR(block); \
++	memcpy(p, &heap->canary_3, CANARY_SIZE); \
++	} while (0)
++
++#else
++
++# define SUHOSIN_MM_CHECK_CANARIES(block, MFUNCTION)
++# define SUHOSIN_MM_SET_CANARIES(block)
++# define SUHOSIN_MM_END_CANARY_PTR(block)
++# define SUHOSIN_MM_SET_END_CANARY(block)
++
++#endif
++
++
++#if ZEND_MM_HEAP_PROTECTION
++
++# define ZEND_MM_CHECK_PROTECTION(block) \
++	do { \
++		if ((block)->debug.start_magic != _mem_block_start_magic || \
++		    memcmp(ZEND_MM_END_MAGIC_PTR(block), &_mem_block_end_magic, END_MAGIC_SIZE) != 0) { \
++		    zend_mm_panic("zend_mm_heap corrupted"); \
++		} \
++	} while (0)
++
++# define ZEND_MM_END_MAGIC_PTR(block) \
++	(((char*)(ZEND_MM_DATA_OF(block))) + ((zend_mm_block_canary*)(block))->debug.size)
++
++# define END_MAGIC_SIZE sizeof(unsigned int)
++
++# define ZEND_MM_SET_BLOCK_SIZE(block, __size) do { \
++		char *p; \
++		((zend_mm_block_canary*)(block))->debug.size = (__size); \
++		p = ZEND_MM_END_MAGIC_PTR(block); \
++		((zend_mm_block_canary*)(block))->debug.start_magic = _mem_block_start_magic; \
++		memcpy(p, &_mem_block_end_magic, END_MAGIC_SIZE); \
++	} while (0)
++
++static unsigned int _mem_block_start_magic = 0;
++static unsigned int _mem_block_end_magic   = 0;
++
++#else
++
++# if ZEND_DEBUG
++#  define ZEND_MM_SET_BLOCK_SIZE(block, _size) \
++	((zend_mm_block_canary*)(block))->debug.size = (_size)
++# else
++#  define ZEND_MM_SET_BLOCK_SIZE(block, _size)
++# endif
++
++# define ZEND_MM_CHECK_PROTECTION(block)
++
++# define END_MAGIC_SIZE 0
++
++#endif
++
++#if ZEND_MM_SAFE_UNLINKING
++# define ZEND_MM_CHECK_BLOCK_LINKAGE(block) \
++	if (UNEXPECTED((block)->info._size != ZEND_MM_BLOCK_AT(block, ZEND_MM_FREE_BLOCK_SIZE(block))->info._prev) || \
++		UNEXPECTED(!UNEXPECTED(ZEND_MM_IS_FIRST_BLOCK(block)) && \
++	    UNEXPECTED(ZEND_MM_PREV_BLOCK(block)->info._size != (block)->info._prev))) { \
++	    zend_mm_panic("zend_mm_heap corrupted"); \
++	}
++#define ZEND_MM_CHECK_TREE(block) \
++	if (UNEXPECTED(*((block)->parent) != (block))) { \
++		zend_mm_panic("zend_mm_heap corrupted"); \
++	}
++#else
++# define ZEND_MM_CHECK_BLOCK_LINKAGE(block)
++# define ZEND_MM_CHECK_TREE(block)
++#endif
++
++#define ZEND_MM_LARGE_BUCKET_INDEX(S) zend_mm_high_bit(S)
++
++void *_zend_mm_alloc_canary_int(zend_mm_heap_canary *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
++void _zend_mm_free_canary_int(zend_mm_heap_canary *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
++void *_zend_mm_realloc_canary_int(zend_mm_heap_canary *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
++
++
++static inline unsigned int zend_mm_high_bit(size_t _size)
++{
++#if defined(__GNUC__) && defined(i386)
++	unsigned int n;
++
++	__asm__("bsrl %1,%0\n\t" : "=r" (n) : "rm"  (_size));
++	return n;
++#elif defined(__GNUC__) && defined(__x86_64__)
++	unsigned long n;
++
++        __asm__("bsrq %1,%0\n\t" : "=r" (n) : "rm"  (_size));
++        return (unsigned int)n;
++#elif defined(_MSC_VER) && defined(_M_IX86)
++	__asm {
++		bsr eax, _size
++	}
++#else
++	unsigned int n = 0;
++	while (_size != 0) {
++		_size = _size >> 1;
++		n++;
++	}
++	return n-1;
++#endif
++}
++
++static inline unsigned int zend_mm_low_bit(size_t _size)
++{
++#if defined(__GNUC__) && defined(i386)
++	unsigned int n;
++
++	__asm__("bsfl %1,%0\n\t" : "=r" (n) : "rm"  (_size));
++	return n;
++#elif defined(__GNUC__) && defined(__x86_64__)
++        unsigned long n;
++
++        __asm__("bsfq %1,%0\n\t" : "=r" (n) : "rm"  (_size));
++        return (unsigned int)n;
++#elif defined(_MSC_VER) && defined(_M_IX86)
++	__asm {
++		bsf eax, _size
++   }
++#else
++	static const int offset[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
++	unsigned int n;
++	unsigned int index = 0;
++
++	do {
++		n = offset[_size & 15];
++		_size >>= 4;
++		index += n;
++	} while (n == 4);
++	return index;
++#endif
++}
++
++static void zend_mm_add_to_rest_list(zend_mm_heap_canary *heap, zend_mm_free_block_canary *mm_block)
++{
++	zend_mm_free_block_canary *prev, *next;
++
++	ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_FREED);
++
++	if (!ZEND_MM_SMALL_SIZE(ZEND_MM_FREE_BLOCK_SIZE(mm_block))) {
++		mm_block->parent = NULL;
++	}
++
++	prev = heap->rest_buckets[0];
++	next = prev->next_free_block;
++	mm_block->prev_free_block = prev;
++	mm_block->next_free_block = next;
++	prev->next_free_block = next->prev_free_block = mm_block;
++}
++
++static void zend_mm_add_to_free_list(zend_mm_heap_canary *heap, zend_mm_free_block_canary *mm_block)
++{
++	size_t size;
++	size_t index;
++
++	ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_FREED);
++
++	size = ZEND_MM_FREE_BLOCK_SIZE(mm_block);
++	if (EXPECTED(!ZEND_MM_SMALL_SIZE(size))) {
++		zend_mm_free_block_canary **p;
++
++		index = ZEND_MM_LARGE_BUCKET_INDEX(size);
++		p = &heap->large_free_buckets[index];
++		mm_block->child[0] = mm_block->child[1] = NULL;
++		if (!*p) {
++			*p = mm_block;
++			mm_block->parent = p;
++			mm_block->prev_free_block = mm_block->next_free_block = mm_block;
++			heap->large_free_bitmap |= (ZEND_MM_LONG_CONST(1) << index);
++		} else {
++			size_t m;
++
++			for (m = size << (ZEND_MM_NUM_BUCKETS - index); ; m <<= 1) {
++				zend_mm_free_block_canary *prev = *p;
++
++				if (ZEND_MM_FREE_BLOCK_SIZE(prev) != size) {
++					p = &prev->child[(m >> (ZEND_MM_NUM_BUCKETS-1)) & 1];
++					if (!*p) {
++						*p = mm_block;
++						mm_block->parent = p;
++						mm_block->prev_free_block = mm_block->next_free_block = mm_block;
++						break;
++					}
++				} else {
++					zend_mm_free_block_canary *next = prev->next_free_block;
++
++					prev->next_free_block = next->prev_free_block = mm_block;
++					mm_block->next_free_block = next;
++					mm_block->prev_free_block = prev;
++					mm_block->parent = NULL;
++					break;
++				}
++			}
++		}
++	} else {
++		zend_mm_free_block_canary *prev, *next;
++
++		index = ZEND_MM_BUCKET_INDEX(size);
++
++		prev = ZEND_MM_SMALL_FREE_BUCKET(heap, index);
++		if (prev->prev_free_block == prev) {
++			heap->free_bitmap |= (ZEND_MM_LONG_CONST(1) << index);
++		}
++		next = prev->next_free_block;
++
++		mm_block->prev_free_block = prev;
++		mm_block->next_free_block = next;
++		prev->next_free_block = next->prev_free_block = mm_block;
++	}
++}
++
++static void zend_mm_remove_from_free_list(zend_mm_heap_canary *heap, zend_mm_free_block_canary *mm_block)
++{
++	zend_mm_free_block_canary *prev = mm_block->prev_free_block;
++	zend_mm_free_block_canary *next = mm_block->next_free_block;
++
++	ZEND_MM_CHECK_MAGIC(mm_block, MEM_BLOCK_FREED);
++
++	if (EXPECTED(prev == mm_block)) {
++		zend_mm_free_block_canary **rp, **cp;
++
++#if SUHOSIN_PATCH
++                if (next != mm_block) {
++                        zend_suhosin_log(S_MEMORY, "zend_mm_heap corrupted at %p", mm_block);
++                        _exit(1);
++                }
++#endif
++#if ZEND_MM_SAFE_UNLINKING
++		if (UNEXPECTED(next != mm_block)) {
++			zend_mm_panic("zend_mm_heap corrupted");
++		}
++#endif
++
++		rp = &mm_block->child[mm_block->child[1] != NULL];
++		prev = *rp;
++		if (EXPECTED(prev == NULL)) {
++			size_t index = ZEND_MM_LARGE_BUCKET_INDEX(ZEND_MM_FREE_BLOCK_SIZE(mm_block));
++
++			ZEND_MM_CHECK_TREE(mm_block);
++			*mm_block->parent = NULL;
++			if (mm_block->parent == &heap->large_free_buckets[index]) {
++				heap->large_free_bitmap &= ~(ZEND_MM_LONG_CONST(1) << index);
++		    }
++		} else {
++			while (*(cp = &(prev->child[prev->child[1] != NULL])) != NULL) {
++				prev = *cp;
++				rp = cp;
++			}
++			*rp = NULL;
++
++subst_block:
++			ZEND_MM_CHECK_TREE(mm_block);
++			*mm_block->parent = prev;
++			prev->parent = mm_block->parent;
++			if ((prev->child[0] = mm_block->child[0])) {
++				ZEND_MM_CHECK_TREE(prev->child[0]);
++				prev->child[0]->parent = &prev->child[0];
++			}
++			if ((prev->child[1] = mm_block->child[1])) {
++				ZEND_MM_CHECK_TREE(prev->child[1]);
++				prev->child[1]->parent = &prev->child[1];
++			}
++		}
++	} else {
++
++#if SUHOSIN_PATCH
++                if (prev->next_free_block != mm_block || next->prev_free_block != mm_block) {
++                        zend_suhosin_log(S_MEMORY, "zend_mm_head corrupted at %p", mm_block);
++		        _exit(1);
++                }
++#endif    
++
++#if ZEND_MM_SAFE_UNLINKING
++		if (UNEXPECTED(prev->next_free_block != mm_block) || UNEXPECTED(next->prev_free_block != mm_block)) {
++			zend_mm_panic("zend_mm_heap corrupted");
++		}
++#endif
++
++		prev->next_free_block = next;
++		next->prev_free_block = prev;
++
++		if (EXPECTED(ZEND_MM_SMALL_SIZE(ZEND_MM_FREE_BLOCK_SIZE(mm_block)))) {
++			if (EXPECTED(prev == next)) {
++				size_t index = ZEND_MM_BUCKET_INDEX(ZEND_MM_FREE_BLOCK_SIZE(mm_block));
++
++				if (EXPECTED(heap->free_buckets[index*2] == heap->free_buckets[index*2+1])) {
++					heap->free_bitmap &= ~(ZEND_MM_LONG_CONST(1) << index);
++				}
++			}
++		} else if (UNEXPECTED(mm_block->parent != NULL)) {
++			goto subst_block;
++		}
++	}
++}
++
++static void zend_mm_init(zend_mm_heap_canary *heap)
++{
++	zend_mm_free_block_canary* p;
++	int i;
++
++	heap->free_bitmap = 0;
++	heap->large_free_bitmap = 0;
++#if ZEND_MM_CACHE
++	heap->cached = 0;
++	memset(heap->cache, 0, sizeof(heap->cache));
++#endif
++#if ZEND_MM_CACHE_STAT
++	for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
++		heap->cache_stat[i].count = 0;
++	}
++#endif
++	p = ZEND_MM_SMALL_FREE_BUCKET(heap, 0);
++	for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
++		p->next_free_block = p;
++		p->prev_free_block = p;
++		p = (zend_mm_free_block_canary*)((char*)p + sizeof(zend_mm_free_block_canary*) * 2);
++		heap->large_free_buckets[i] = NULL;
++	}
++	heap->rest_buckets[0] = heap->rest_buckets[1] = ZEND_MM_REST_BUCKET(heap);
++#if SUHOSIN_PATCH
++        if (SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION)) {
++	        heap->canary_1 = zend_canary();
++	        heap->canary_2 = zend_canary();
++	        heap->canary_3 = zend_canary();
++	}
++#endif
++}
++
++static void zend_mm_del_segment(zend_mm_heap_canary *heap, zend_mm_segment *segment)
++{
++	zend_mm_segment **p = &heap->segments_list;
++
++	while (*p != segment) {
++		p = &(*p)->next_segment;
++	}
++	*p = segment->next_segment;
++	heap->real_size -= segment->size;
++	ZEND_MM_STORAGE_FREE(segment);
++}
++
++#if ZEND_MM_CACHE
++static void zend_mm_free_cache(zend_mm_heap_canary *heap)
++{
++	int i;
++
++	for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
++		if (heap->cache[i]) {
++			zend_mm_free_block_canary *mm_block = heap->cache[i];
++
++			while (mm_block) {
++				size_t size = ZEND_MM_BLOCK_SIZE(mm_block);
++				zend_mm_free_block_canary *q = mm_block->prev_free_block;
++				zend_mm_block_canary *next_block = ZEND_MM_NEXT_BLOCK(mm_block);
++
++				heap->cached -= size;
++
++				if (ZEND_MM_PREV_BLOCK_IS_FREE(mm_block)) {
++					mm_block = (zend_mm_free_block_canary*)ZEND_MM_PREV_BLOCK(mm_block);
++					size += ZEND_MM_FREE_BLOCK_SIZE(mm_block);
++					zend_mm_remove_from_free_list(heap, (zend_mm_free_block_canary *) mm_block);
++				}
++				if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
++					size += ZEND_MM_FREE_BLOCK_SIZE(next_block);
++					zend_mm_remove_from_free_list(heap, (zend_mm_free_block_canary *) next_block);
++				}
++				ZEND_MM_BLOCK(mm_block, ZEND_MM_FREE_BLOCK, size);
++
++				if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
++				    ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_NEXT_BLOCK(mm_block))) {
++					zend_mm_del_segment(heap, (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE));
++				} else {
++					zend_mm_add_to_free_list(heap, (zend_mm_free_block_canary *) mm_block);
++				}
++
++				mm_block = q;
++			}
++			heap->cache[i] = NULL;
++#if ZEND_MM_CACHE_STAT
++			heap->cache_stat[i].count = 0;
++#endif
++		}
++	}
++}
++#endif
++
++#if ZEND_MM_HEAP_PROTECTION || ZEND_MM_COOKIES
++static void zend_mm_random(unsigned char *buf, size_t size)
++{
++	size_t i = 0;
++	unsigned char t;
++
++#ifdef ZEND_WIN32
++	HCRYPTPROV   hCryptProv;
++
++	if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0)) {
++		do {
++			BOOL ret = CryptGenRandom(hCryptProv, size, buf);
++			CryptReleaseContext(hCryptProv, 0);
++			if (ret) {
++				while (i < size && buf[i] != 0) {
++					i++;
++				}
++				if (i == size) {
++				    return;
++				}
++		   }
++		} while (0);
++	}
++#elif defined(HAVE_DEV_URANDOM)
++	int fd = open("/dev/urandom", 0);
++
++	if (fd >= 0) {
++		if (read(fd, buf, size) == size) {
++			while (i < size && buf[i] != 0) {
++				i++;
++			}
++			if (i == size) {
++				close(fd);
++			    return;
++			}
++		}
++		close(fd);
++	}
++#endif
++	t = (unsigned char)getpid();
++	while (i < size) {
++		do {
++			buf[i] = ((unsigned char)rand()) ^ t;
++		} while (buf[i] == 0);
++		t = buf[i++] << 1;
++    }
++}
++#endif
++
++
++/* Notes:
++ * - This function may alter the block_sizes values to match platform alignment
++ * - This function does *not* perform sanity checks on the arguments
++ */
++zend_mm_heap_canary *__zend_mm_startup_canary_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params)
++{
++	zend_mm_storage *storage;
++	zend_mm_heap_canary    *heap;
++
++#if 0
++	int i;
++
++	printf("ZEND_MM_ALIGNMENT=%d\n", ZEND_MM_ALIGNMENT);
++	printf("ZEND_MM_ALIGNMENT_LOG2=%d\n", ZEND_MM_ALIGNMENT_LOG2);
++	printf("ZEND_MM_MIN_SIZE=%d\n", ZEND_MM_MIN_SIZE);
++	printf("ZEND_MM_MAX_SMALL_SIZE=%d\n", ZEND_MM_MAX_SMALL_SIZE);
++	printf("ZEND_MM_ALIGNED_HEADER_SIZE=%d\n", ZEND_MM_ALIGNED_HEADER_SIZE);
++	printf("ZEND_MM_ALIGNED_FREE_HEADER_SIZE=%d\n", ZEND_MM_ALIGNED_FREE_HEADER_SIZE);
++	printf("ZEND_MM_MIN_ALLOC_BLOCK_SIZE=%d\n", ZEND_MM_MIN_ALLOC_BLOCK_SIZE);
++	printf("ZEND_MM_ALIGNED_MIN_HEADER_SIZE=%d\n", ZEND_MM_ALIGNED_MIN_HEADER_SIZE);
++	printf("ZEND_MM_ALIGNED_SEGMENT_SIZE=%d\n", ZEND_MM_ALIGNED_SEGMENT_SIZE);
++	for (i = 0; i < ZEND_MM_MAX_SMALL_SIZE; i++) {
++		printf("%3d%c: %3ld %d %2ld\n", i, (i == ZEND_MM_MIN_SIZE?'*':' '), (long)ZEND_MM_TRUE_SIZE(i), ZEND_MM_SMALL_SIZE(ZEND_MM_TRUE_SIZE(i)), (long)ZEND_MM_BUCKET_INDEX(ZEND_MM_TRUE_SIZE(i)));
++	}
++	exit(0);
++#endif
++
++#if ZEND_MM_HEAP_PROTECTION
++	if (_mem_block_start_magic == 0) {
++		zend_mm_random((unsigned char*)&_mem_block_start_magic, sizeof(_mem_block_start_magic));
++	}
++	if (_mem_block_end_magic == 0) {
++		zend_mm_random((unsigned char*)&_mem_block_end_magic, sizeof(_mem_block_end_magic));
++	}
++#endif
++#if ZEND_MM_COOKIES
++	if (_zend_mm_cookie == 0) {
++		zend_mm_random((unsigned char*)&_zend_mm_cookie, sizeof(_zend_mm_cookie));
++	}
++#endif
++
++	if (zend_mm_low_bit(block_size) != zend_mm_high_bit(block_size)) {
++		fprintf(stderr, "'block_size' must be a power of two\n");
++		exit(255);
++	}
++	storage = handlers->init(params);
++	if (!storage) {
++		fprintf(stderr, "Cannot initialize zend_mm storage [%s]\n", handlers->name);
++		exit(255);
++	}
++	storage->handlers = handlers;
++
++	heap = malloc(sizeof(struct _zend_mm_heap_canary));
++
++	heap->storage = storage;
++	heap->block_size = block_size;
++	heap->compact_size = 0;
++	heap->segments_list = NULL;
++	zend_mm_init(heap);
++# if ZEND_MM_CACHE_STAT
++	memset(heap->cache_stat, 0, sizeof(heap->cache_stat));
++# endif
++
++	heap->use_zend_alloc = 1;
++	heap->real_size = 0;
++	heap->overflow = 0;
++	heap->real_peak = 0;
++	heap->limit = ZEND_MM_LONG_CONST(1)<<(ZEND_MM_NUM_BUCKETS-2);
++	heap->size = 0;
++	heap->peak = 0;
++	heap->internal = internal;
++	heap->reserve = NULL;
++	heap->reserve_size = reserve_size;
++	if (reserve_size > 0) {
++		heap->reserve = _zend_mm_alloc((zend_mm_heap *)heap, reserve_size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
++	}
++	if (internal) {
++		int i;
++		zend_mm_free_block_canary *p, *q, *orig;
++		zend_mm_heap_canary *mm_heap = _zend_mm_alloc((zend_mm_heap *)heap, sizeof(zend_mm_heap_canary)  ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
++
++		*mm_heap = *heap;
++
++		p = ZEND_MM_SMALL_FREE_BUCKET(mm_heap, 0);
++		orig = ZEND_MM_SMALL_FREE_BUCKET(heap, 0);
++		for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
++			q = p;
++			while (q->prev_free_block != orig) {
++				q = q->prev_free_block;
++			}
++			q->prev_free_block = p;
++			q = p;
++			while (q->next_free_block != orig) {
++				q = q->next_free_block;
++			}
++			q->next_free_block = p;
++			p = (zend_mm_free_block_canary*)((char*)p + sizeof(zend_mm_free_block_canary*) * 2);
++			orig = (zend_mm_free_block_canary*)((char*)orig + sizeof(zend_mm_free_block_canary*) * 2);
++			if (mm_heap->large_free_buckets[i]) {
++				mm_heap->large_free_buckets[i]->parent = &mm_heap->large_free_buckets[i];
++			}
++		}
++		mm_heap->rest_buckets[0]->next_free_block = mm_heap->rest_buckets[1]->prev_free_block = ZEND_MM_REST_BUCKET(mm_heap);
++
++		free(heap);
++		heap = mm_heap;
++	}
++	return heap;
++}
++
++zend_mm_heap_canary *__zend_mm_startup_canary(void)
++{
++	int i;
++	size_t seg_size;
++	char *mem_type = getenv("ZEND_MM_MEM_TYPE");
++	char *tmp;
++	const zend_mm_mem_handlers *handlers;
++	zend_mm_heap_canary *heap;
++
++	if (mem_type == NULL) {
++		i = 0;
++	} else {
++		for (i = 0; mem_handlers[i].name; i++) {
++			if (strcmp(mem_handlers[i].name, mem_type) == 0) {
++				break;
++			}
++		}
++		if (!mem_handlers[i].name) {
++			fprintf(stderr, "Wrong or unsupported zend_mm storage type '%s'\n", mem_type);
++			fprintf(stderr, "  supported types:\n");
++			for (i = 0; mem_handlers[i].name; i++) {
++				fprintf(stderr, "    '%s'\n", mem_handlers[i].name);
++			}
++			exit(255);
++		}
++	}
++	handlers = &mem_handlers[i];
++
++	tmp = getenv("ZEND_MM_SEG_SIZE");
++	if (tmp) {
++		seg_size = zend_atoi(tmp, 0);
++		if (zend_mm_low_bit(seg_size) != zend_mm_high_bit(seg_size)) {
++			fprintf(stderr, "ZEND_MM_SEG_SIZE must be a power of two\n");
++			exit(255);
++		} else if (seg_size < ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE) {
++			fprintf(stderr, "ZEND_MM_SEG_SIZE is too small\n");
++			exit(255);
++		}
++	} else {
++		seg_size = ZEND_MM_SEG_SIZE;
++	}
++
++	heap = __zend_mm_startup_canary_ex(handlers, seg_size, ZEND_MM_RESERVE_SIZE, 0, NULL);
++	if (heap) {
++		tmp = getenv("ZEND_MM_COMPACT");
++		if (tmp) {
++			heap->compact_size = zend_atoi(tmp, 0);
++		} else {
++			heap->compact_size = 2 * 1024 * 1024;
++		}
++	}
++	return heap;
++}
++
++#if ZEND_DEBUG
++static long zend_mm_find_leaks(zend_mm_segment *segment, zend_mm_block_canary *b)
++{
++	long leaks = 0;
++	zend_mm_block_canary *p, *q;
++
++	p = ZEND_MM_NEXT_BLOCK(b);
++	while (1) {
++		if (ZEND_MM_IS_GUARD_BLOCK(p)) {
++			ZEND_MM_CHECK_MAGIC(p, MEM_BLOCK_GUARD);
++			segment = segment->next_segment;
++			if (!segment) {
++				break;
++			}
++			p = (zend_mm_block_canary *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
++			continue;
++		}
++		q = ZEND_MM_NEXT_BLOCK(p);
++		if (q <= p ||
++		    (char*)q > (char*)segment + segment->size ||
++		    p->info._size != q->info._prev) {
++		    zend_mm_panic("zend_mm_heap corrupted");
++		}
++		if (!ZEND_MM_IS_FREE_BLOCK(p)) {
++			if (p->magic == MEM_BLOCK_VALID) {
++				if (p->debug.filename==b->debug.filename && p->debug.lineno==b->debug.lineno) {
++					ZEND_MM_SET_MAGIC(p, MEM_BLOCK_LEAK);
++					leaks++;
++				}
++#if ZEND_MM_CACHE
++			} else if (p->magic == MEM_BLOCK_CACHED) {
++				/* skip it */
++#endif
++			} else if (p->magic != MEM_BLOCK_LEAK) {
++			    zend_mm_panic("zend_mm_heap corrupted");
++			}
++		}
++		p = q;
++	}
++	return leaks;
++}
++
++static void zend_mm_check_leaks(zend_mm_heap_canary *heap TSRMLS_DC)
++{
++	zend_mm_segment *segment = heap->segments_list;
++	zend_mm_block_canary *p, *q;
++	zend_uint total = 0;
++
++	if (!segment) {
++		return;
++	}
++	p = (zend_mm_block_canary *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
++	while (1) {
++		q = ZEND_MM_NEXT_BLOCK(p);
++		if (q <= p ||
++		    (char*)q > (char*)segment + segment->size ||
++		    p->info._size != q->info._prev) {
++			zend_mm_panic("zend_mm_heap corrupted");
++		}
++		if (!ZEND_MM_IS_FREE_BLOCK(p)) {
++			if (p->magic == MEM_BLOCK_VALID) {
++				long repeated;
++				zend_leak_info leak;
++
++				ZEND_MM_SET_MAGIC(p, MEM_BLOCK_LEAK);
++
++				leak.addr = ZEND_MM_DATA_OF(p);
++				leak.size = p->debug.size;
++				leak.filename = p->debug.filename;
++				leak.lineno = p->debug.lineno;
++				leak.orig_filename = p->debug.orig_filename;
++				leak.orig_lineno = p->debug.orig_lineno;
++
++				zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL TSRMLS_CC);
++				zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak TSRMLS_CC);
++				repeated = zend_mm_find_leaks(segment, p);
++				total += 1 + repeated;
++				if (repeated) {
++					zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(zend_uintptr_t)repeated TSRMLS_CC);
++				}
++#if ZEND_MM_CACHE
++			} else if (p->magic == MEM_BLOCK_CACHED) {
++				/* skip it */
++#endif
++			} else if (p->magic != MEM_BLOCK_LEAK) {
++				zend_mm_panic("zend_mm_heap corrupted");
++			}
++		}
++		if (ZEND_MM_IS_GUARD_BLOCK(q)) {
++			segment = segment->next_segment;
++			if (!segment) {
++				break;
++			}
++			q = (zend_mm_block_canary *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
++		}
++		p = q;
++	}
++	if (total) {
++		zend_message_dispatcher(ZMSG_MEMORY_LEAKS_GRAND_TOTAL, &total TSRMLS_CC);
++	}
++}
++
++static int zend_mm_check_ptr(zend_mm_heap_canary *heap, void *ptr, int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
++{
++	zend_mm_block_canary *p;
++	int no_cache_notice = 0;
++	int had_problems = 0;
++	int valid_beginning = 1;
++
++	if (silent==2) {
++		silent = 1;
++		no_cache_notice = 1;
++	} else if (silent==3) {
++		silent = 0;
++		no_cache_notice = 1;
++	}
++	if (!silent) {
++		TSRMLS_FETCH();
++		
++		zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL TSRMLS_CC);
++		zend_debug_alloc_output("---------------------------------------\n");
++		zend_debug_alloc_output("%s(%d) : Block "PTR_FMT" status:\n" ZEND_FILE_LINE_RELAY_CC, ptr);
++		if (__zend_orig_filename) {
++			zend_debug_alloc_output("%s(%d) : Actual location (location was relayed)\n" ZEND_FILE_LINE_ORIG_RELAY_CC);
++		}
++		if (!ptr) {
++			zend_debug_alloc_output("NULL\n");
++			zend_debug_alloc_output("---------------------------------------\n");
++			return 0;
++		}
++	}
++
++	if (!ptr) {
++		if (silent) {
++			return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++		}
++	}
++
++	p = ZEND_MM_HEADER_OF(ptr);
++
++#ifdef ZTS
++	if (ZEND_MM_BAD_THREAD_ID(p)) {
++		if (!silent) {
++			zend_debug_alloc_output("Invalid pointer: ((thread_id=0x%0.8X) != (expected=0x%0.8X))\n", (long)p->thread_id, (long)tsrm_thread_id());
++			had_problems = 1;
++		} else {
++			return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++		}
++	}
++#endif
++
++	if (p->info._size != ZEND_MM_NEXT_BLOCK(p)->info._prev) {
++		if (!silent) {
++			zend_debug_alloc_output("Invalid pointer: ((size="PTR_FMT") != (next.prev="PTR_FMT"))\n", p->info._size, ZEND_MM_NEXT_BLOCK(p)->info._prev);
++			had_problems = 1;
++		} else {
++			return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++		}
++	}
++	if (p->info._prev != ZEND_MM_GUARD_BLOCK &&
++	    ZEND_MM_PREV_BLOCK(p)->info._size != p->info._prev) {
++		if (!silent) {
++			zend_debug_alloc_output("Invalid pointer: ((prev="PTR_FMT") != (prev.size="PTR_FMT"))\n", p->info._prev, ZEND_MM_PREV_BLOCK(p)->info._size);
++			had_problems = 1;
++		} else {
++			return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++		}
++	}
++
++	if (had_problems) {
++		zend_debug_alloc_output("---------------------------------------\n");
++		return 0;
++	}
++
++	if (!silent) {
++		zend_debug_alloc_output("%10s\t","Beginning:  ");
++	}
++
++	if (!ZEND_MM_IS_USED_BLOCK(p)) {
++		if (!silent) {
++			if (p->magic != MEM_BLOCK_FREED) {
++				zend_debug_alloc_output("Freed (magic=0x%0.8X, expected=0x%0.8X)\n", p->magic, MEM_BLOCK_FREED);
++			} else {
++				zend_debug_alloc_output("Freed\n");
++			}
++			had_problems = 1;
++		} else {
++			return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++		}
++	} else if (ZEND_MM_IS_GUARD_BLOCK(p)) {
++		if (!silent) {
++			if (p->magic != MEM_BLOCK_FREED) {
++				zend_debug_alloc_output("Guard (magic=0x%0.8X, expected=0x%0.8X)\n", p->magic, MEM_BLOCK_FREED);
++			} else {
++				zend_debug_alloc_output("Guard\n");
++			}
++			had_problems = 1;
++		} else {
++			return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++		}
++	} else {
++		switch (p->magic) {
++			case MEM_BLOCK_VALID:
++			case MEM_BLOCK_LEAK:
++				if (!silent) {
++					zend_debug_alloc_output("OK (allocated on %s:%d, %d bytes)\n", p->debug.filename, p->debug.lineno, (int)p->debug.size);
++				}
++				break; /* ok */
++			case MEM_BLOCK_CACHED:
++				if (!no_cache_notice) {
++					if (!silent) {
++						zend_debug_alloc_output("Cached\n");
++						had_problems = 1;
++					} else {
++						return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++					}
++				}
++			case MEM_BLOCK_FREED:
++				if (!silent) {
++					zend_debug_alloc_output("Freed (invalid)\n");
++					had_problems = 1;
++				} else {
++					return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++				}
++				break;
++			case MEM_BLOCK_GUARD:
++				if (!silent) {
++					zend_debug_alloc_output("Guard (invalid)\n");
++					had_problems = 1;
++				} else {
++					return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++				}
++				break;
++			default:
++				if (!silent) {
++					zend_debug_alloc_output("Unknown (magic=0x%0.8X, expected=0x%0.8X)\n", p->magic, MEM_BLOCK_VALID);
++					had_problems = 1;
++					valid_beginning = 0;
++				} else {
++					return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++				}
++				break;
++		}
++	}
++
++#if ZEND_MM_HEAP_PROTECTION
++	if (!valid_beginning) {
++		if (!silent) {
++			zend_debug_alloc_output("%10s\t", "Start:");
++			zend_debug_alloc_output("Unknown\n");
++			zend_debug_alloc_output("%10s\t", "End:");
++			zend_debug_alloc_output("Unknown\n");
++		}
++	} else {
++		char *end_magic = ZEND_MM_END_MAGIC_PTR(p);
++
++		if (p->debug.start_magic == _mem_block_start_magic) {
++			if (!silent) {
++				zend_debug_alloc_output("%10s\t", "Start:");
++				zend_debug_alloc_output("OK\n");
++			}
++		} else {
++			char *overflow_ptr, *magic_ptr=(char *) &_mem_block_start_magic;
++			int overflows=0;
++			int i;
++
++			if (silent) {
++				return _mem_block_check(ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++			}
++			had_problems = 1;
++			overflow_ptr = (char *) &p->debug.start_magic;
++			i = END_MAGIC_SIZE;
++			while (--i >= 0) {
++				if (overflow_ptr[i]!=magic_ptr[i]) {
++					overflows++;
++				}
++			}
++			zend_debug_alloc_output("%10s\t", "Start:");
++			zend_debug_alloc_output("Overflown (magic=0x%0.8X instead of 0x%0.8X)\n", p->debug.start_magic, _mem_block_start_magic);
++			zend_debug_alloc_output("%10s\t","");
++			if (overflows >= END_MAGIC_SIZE) {
++				zend_debug_alloc_output("At least %d bytes overflown\n", END_MAGIC_SIZE);
++			} else {
++				zend_debug_alloc_output("%d byte(s) overflown\n", overflows);
++			}
++		}
++		if (memcmp(end_magic, &_mem_block_end_magic, END_MAGIC_SIZE)==0) {
++			if (!silent) {
++				zend_debug_alloc_output("%10s\t", "End:");
++				zend_debug_alloc_output("OK\n");
++			}
++		} else {
++			char *overflow_ptr, *magic_ptr=(char *) &_mem_block_end_magic;
++			int overflows=0;
++			int i;
++
++			if (silent) {
++				return _mem_block_check(ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++			}
++			had_problems = 1;
++			overflow_ptr = (char *) end_magic;
++
++			for (i=0; i < END_MAGIC_SIZE; i++) {
++				if (overflow_ptr[i]!=magic_ptr[i]) {
++					overflows++;
++				}
++			}
++
++			zend_debug_alloc_output("%10s\t", "End:");
++			zend_debug_alloc_output("Overflown (magic=0x%0.8X instead of 0x%0.8X)\n", *end_magic, _mem_block_end_magic);
++			zend_debug_alloc_output("%10s\t","");
++			if (overflows >= END_MAGIC_SIZE) {
++				zend_debug_alloc_output("At least %d bytes overflown\n", END_MAGIC_SIZE);
++			} else {
++				zend_debug_alloc_output("%d byte(s) overflown\n", overflows);
++			}
++		}
++	}
++#endif
++
++	if (!silent) {
++		zend_debug_alloc_output("---------------------------------------\n");
++	}
++	return ((!had_problems) ? 1 : 0);
++}
++
++static int zend_mm_check_heap(zend_mm_heap_canary *heap, int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
++{
++	zend_mm_segment *segment = heap->segments_list;
++	zend_mm_block_canary *p, *q;
++	int errors = 0;
++
++	if (!segment) {
++		return 0;
++	}
++	p = (zend_mm_block_canary *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
++	while (1) {
++		q = ZEND_MM_NEXT_BLOCK(p);
++		if (q <= p ||
++		    (char*)q > (char*)segment + segment->size ||
++		    p->info._size != q->info._prev) {
++			zend_mm_panic("zend_mm_heap corrupted");
++		}
++		if (!ZEND_MM_IS_FREE_BLOCK(p)) {
++			if (p->magic == MEM_BLOCK_VALID || p->magic == MEM_BLOCK_LEAK) {
++				if (!zend_mm_check_ptr(heap, ZEND_MM_DATA_OF(p), (silent?2:3) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC)) {
++					errors++;
++				}
++#if ZEND_MM_CACHE
++			} else if (p->magic == MEM_BLOCK_CACHED) {
++				/* skip it */
++#endif
++			} else if (p->magic != MEM_BLOCK_LEAK) {
++				zend_mm_panic("zend_mm_heap corrupted");
++			}
++		}
++		if (ZEND_MM_IS_GUARD_BLOCK(q)) {
++			segment = segment->next_segment;
++			if (!segment) {
++				return errors;
++			}
++			q = (zend_mm_block_canary *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
++		}
++		p = q;
++	}
++}
++#endif
++
++void __zend_mm_shutdown_canary(zend_mm_heap_canary *heap, int full_shutdown, int silent TSRMLS_DC)
++{
++	zend_mm_storage *storage;
++	zend_mm_segment *segment;
++	zend_mm_segment *prev;
++	int internal;
++
++	if (heap->reserve) {
++#if ZEND_DEBUG
++		if (!silent) {
++			_zend_mm_free(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
++		}
++#endif
++		heap->reserve = NULL;
++	}
++
++#if ZEND_MM_CACHE_STAT
++	if (full_shutdown) {
++		FILE *f;
++
++		f = fopen("zend_mm.log", "w");
++		if (f) {
++			int i,j;
++			size_t size, true_size, min_size, max_size;
++			int hit = 0, miss = 0;
++
++			fprintf(f, "\nidx min_size max_size true_size  max_len     hits   misses\n");
++			size = 0;
++			while (1) {
++				true_size = ZEND_MM_TRUE_SIZE(size);
++				if (ZEND_MM_SMALL_SIZE(true_size)) {
++					min_size = size;
++					i = ZEND_MM_BUCKET_INDEX(true_size);
++					size++;
++					while (1) {
++						true_size = ZEND_MM_TRUE_SIZE(size);
++						if (ZEND_MM_SMALL_SIZE(true_size)) {
++							j = ZEND_MM_BUCKET_INDEX(true_size);
++							if (j > i) {
++								max_size = size-1;
++								break;
++							}
++						} else {
++							max_size = size-1;
++							break;
++						}
++						size++;
++					}
++					hit += heap->cache_stat[i].hit;
++					miss += heap->cache_stat[i].miss;
++					fprintf(f, "%2d %8d %8d %9d %8d %8d %8d\n", i, (int)min_size, (int)max_size, ZEND_MM_TRUE_SIZE(max_size), heap->cache_stat[i].max_count, heap->cache_stat[i].hit, heap->cache_stat[i].miss);
++				} else {
++					break;
++				}
++			}
++			fprintf(f, "                                        %8d %8d\n", hit, miss);
++			fprintf(f, "                                        %8d %8d\n", heap->cache_stat[ZEND_MM_NUM_BUCKETS].hit, heap->cache_stat[ZEND_MM_NUM_BUCKETS].miss);
++			fclose(f);
++		}
++	}
++#endif
++
++#if ZEND_DEBUG
++	if (!silent) {
++		zend_mm_check_leaks(heap TSRMLS_CC);
++	}
++#endif
++
++	internal = heap->internal;
++	storage = heap->storage;
++	segment = heap->segments_list;
++	while (segment) {
++		prev = segment;
++		segment = segment->next_segment;
++		ZEND_MM_STORAGE_FREE(prev);
++	}
++	if (full_shutdown) {
++		storage->handlers->dtor(storage);
++		if (!internal) {
++			free(heap);
++		}
++	} else {
++		if (heap->compact_size &&
++		    heap->real_peak > heap->compact_size) {
++			storage->handlers->compact(storage);
++		}
++		heap->segments_list = NULL;
++		zend_mm_init(heap);
++		heap->real_size = 0;
++		heap->real_peak = 0;
++		heap->size = 0;
++		heap->peak = 0;
++		if (heap->reserve_size) {
++			heap->reserve = _zend_mm_alloc((zend_mm_heap *)heap, heap->reserve_size  ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
++		}
++		heap->overflow = 0;
++	}
++}
++
++static void zend_mm_safe_error(zend_mm_heap_canary *heap,
++	const char *format,
++	size_t limit,
++#if ZEND_DEBUG
++	const char *filename,
++	uint lineno,
++#endif
++	size_t size)
++{
++	if (heap->reserve) {
++		_zend_mm_free_canary_int(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
++		heap->reserve = NULL;
++	}
++	if (heap->overflow == 0) {
++		char *error_filename;
++		uint error_lineno;
++		TSRMLS_FETCH();
++		if (zend_is_compiling(TSRMLS_C)) {
++			error_filename = zend_get_compiled_filename(TSRMLS_C);
++			error_lineno = zend_get_compiled_lineno(TSRMLS_C);
++		} else if (EG(in_execution)) {
++			error_filename = EG(active_op_array)?EG(active_op_array)->filename:NULL;
++			error_lineno = EG(opline_ptr)?(*EG(opline_ptr))->lineno:0;
++		} else {
++			error_filename = NULL;
++			error_lineno = 0;
++		}
++		if (!error_filename) {
++			error_filename = "Unknown";
++		}
++		heap->overflow = 1;
++		zend_try {
++			zend_error_noreturn(E_ERROR,
++				format,
++				limit,
++#if ZEND_DEBUG
++				filename,
++				lineno,
++#endif
++				size);
++		} zend_catch {
++			if (heap->overflow == 2) {
++				fprintf(stderr, "\nFatal error: ");
++				fprintf(stderr,
++					format,
++					limit,
++#if ZEND_DEBUG
++					filename,
++					lineno,
++#endif
++					size);
++				fprintf(stderr, " in %s on line %d\n", error_filename, error_lineno);
++			}
++		} zend_end_try();
++	} else {
++		heap->overflow = 2;
++	}
++	zend_bailout();
++}
++
++static zend_mm_free_block_canary *zend_mm_search_large_block(zend_mm_heap_canary *heap, size_t true_size)
++{
++	zend_mm_free_block_canary *best_fit;
++	size_t index = ZEND_MM_LARGE_BUCKET_INDEX(true_size);
++	size_t bitmap = heap->large_free_bitmap >> index;
++	zend_mm_free_block_canary *p;
++
++	if (bitmap == 0) {
++		return NULL;
++	}
++
++	if (UNEXPECTED((bitmap & 1) != 0)) {
++		/* Search for best "large" free block */
++		zend_mm_free_block_canary *rst = NULL;
++		size_t m;
++		size_t best_size = -1;
++
++		best_fit = NULL;
++		p = heap->large_free_buckets[index];
++		for (m = true_size << (ZEND_MM_NUM_BUCKETS - index); ; m <<= 1) {
++			if (UNEXPECTED(ZEND_MM_FREE_BLOCK_SIZE(p) == true_size)) {
++				return p->next_free_block;
++			} else if (ZEND_MM_FREE_BLOCK_SIZE(p) >= true_size &&
++			           ZEND_MM_FREE_BLOCK_SIZE(p) < best_size) {
++				best_size = ZEND_MM_FREE_BLOCK_SIZE(p);
++				best_fit = p;
++			}
++			if ((m & (ZEND_MM_LONG_CONST(1) << (ZEND_MM_NUM_BUCKETS-1))) == 0) {
++				if (p->child[1]) {
++					rst = p->child[1];
++				}
++				if (p->child[0]) {
++					p = p->child[0];
++				} else {
++					break;
++				}
++			} else if (p->child[1]) {
++				p = p->child[1];
++			} else {
++				break;
++			}
++		}
++
++		for (p = rst; p; p = p->child[p->child[0] != NULL]) {
++			if (UNEXPECTED(ZEND_MM_FREE_BLOCK_SIZE(p) == true_size)) {
++				return p->next_free_block;
++			} else if (ZEND_MM_FREE_BLOCK_SIZE(p) > true_size &&
++			           ZEND_MM_FREE_BLOCK_SIZE(p) < best_size) {
++				best_size = ZEND_MM_FREE_BLOCK_SIZE(p);
++				best_fit = p;
++			}
++		}
++
++		if (best_fit) {
++			return best_fit->next_free_block;
++		}
++		bitmap = bitmap >> 1;
++		if (!bitmap) {
++			return NULL;
++		}
++		index++;
++	}
++
++	/* Search for smallest "large" free block */
++	best_fit = p = heap->large_free_buckets[index + zend_mm_low_bit(bitmap)];
++	while ((p = p->child[p->child[0] != NULL])) {
++		if (ZEND_MM_FREE_BLOCK_SIZE(p) < ZEND_MM_FREE_BLOCK_SIZE(best_fit)) {
++			best_fit = p;
++		}
++	}
++	return best_fit->next_free_block;
++}
++
++void *_zend_mm_alloc_canary_int(zend_mm_heap_canary *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
++{
++	zend_mm_free_block_canary *best_fit;
++	size_t true_size = ZEND_MM_TRUE_SIZE(size);
++	size_t block_size;
++	size_t remaining_size;
++	size_t segment_size;
++	zend_mm_segment *segment;
++	int keep_rest = 0;
++	
++	if (EXPECTED(ZEND_MM_SMALL_SIZE(true_size))) {
++		size_t index = ZEND_MM_BUCKET_INDEX(true_size);
++		size_t bitmap;
++
++		if (UNEXPECTED(true_size < size)) {
++			goto out_of_memory;
++		}
++#if ZEND_MM_CACHE
++		if (EXPECTED(heap->cache[index] != NULL)) {
++			/* Get block from cache */
++#if ZEND_MM_CACHE_STAT
++			heap->cache_stat[index].count--;
++			heap->cache_stat[index].hit++;
++#endif
++			best_fit = heap->cache[index];
++			heap->cache[index] = best_fit->prev_free_block;
++			heap->cached -= true_size;
++#if SUHOSIN_PATCH
++                        SUHOSIN_MM_SET_CANARIES(best_fit);
++                        ((zend_mm_block_canary*)best_fit)->info.size = size;
++                        SUHOSIN_MM_SET_END_CANARY(best_fit);
++#endif			
++			ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_CACHED);
++			ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);
++			return ZEND_MM_DATA_OF(best_fit);
++ 		}
++#if ZEND_MM_CACHE_STAT
++		heap->cache_stat[index].miss++;
++#endif
++#endif
++
++		bitmap = heap->free_bitmap >> index;
++		if (bitmap) {
++			/* Found some "small" free block that can be used */
++			index += zend_mm_low_bit(bitmap);
++			best_fit = heap->free_buckets[index*2];
++#if ZEND_MM_CACHE_STAT
++			heap->cache_stat[ZEND_MM_NUM_BUCKETS].hit++;
++#endif
++			goto zend_mm_finished_searching_for_block;
++		}
++	}
++
++#if ZEND_MM_CACHE_STAT
++	heap->cache_stat[ZEND_MM_NUM_BUCKETS].miss++;
++#endif
++
++	best_fit = zend_mm_search_large_block(heap, true_size);
++
++	if (!best_fit && heap->real_size >= heap->limit - heap->block_size) {
++		zend_mm_free_block_canary *p = heap->rest_buckets[0];
++		size_t best_size = -1;
++
++		while (p != ZEND_MM_REST_BUCKET(heap)) {
++			if (UNEXPECTED(ZEND_MM_FREE_BLOCK_SIZE(p) == true_size)) {
++				best_fit = p;
++				goto zend_mm_finished_searching_for_block;
++			} else if (ZEND_MM_FREE_BLOCK_SIZE(p) > true_size &&
++			           ZEND_MM_FREE_BLOCK_SIZE(p) < best_size) {
++				best_size = ZEND_MM_FREE_BLOCK_SIZE(p);
++				best_fit = p;
++			}
++			p = p->prev_free_block;
++		}
++	}
++
++	if (!best_fit) {
++		if (true_size > heap->block_size - (ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE)) {
++			/* Make sure we add a memory block which is big enough,
++			   segment must have header "size" and trailer "guard" block */
++			segment_size = true_size + ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE;
++			segment_size = (segment_size + (heap->block_size-1)) & ~(heap->block_size-1);
++			keep_rest = 1;
++		} else {
++			segment_size = heap->block_size;
++		}
++
++		HANDLE_BLOCK_INTERRUPTIONS();
++
++		if (segment_size < true_size ||
++		    heap->real_size + segment_size > heap->limit) {
++			/* Memory limit overflow */
++#if ZEND_MM_CACHE
++			zend_mm_free_cache(heap);
++#endif
++			HANDLE_UNBLOCK_INTERRUPTIONS();
++#if ZEND_DEBUG
++			zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted at %s:%d (tried to allocate %lu bytes)", heap->limit, __zend_filename, __zend_lineno, size);
++#else
++			zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted (tried to allocate %lu bytes)", heap->limit, size);
++#endif
++		}
++
++		segment = (zend_mm_segment *) ZEND_MM_STORAGE_ALLOC(segment_size);
++
++		if (!segment) {
++			/* Storage manager cannot allocate memory */
++#if ZEND_MM_CACHE
++			zend_mm_free_cache(heap);
++#endif
++			HANDLE_UNBLOCK_INTERRUPTIONS();
++out_of_memory:
++#if ZEND_DEBUG
++			zend_mm_safe_error(heap, "Out of memory (allocated %ld) at %s:%d (tried to allocate %lu bytes)", heap->real_size, __zend_filename, __zend_lineno, size);
++#else
++			zend_mm_safe_error(heap, "Out of memory (allocated %ld) (tried to allocate %lu bytes)", heap->real_size, size);
++#endif
++			return NULL;
++		}
++
++		heap->real_size += segment_size;
++		if (heap->real_size > heap->real_peak) {
++			heap->real_peak = heap->real_size;
++		}
++
++		segment->size = segment_size;
++		segment->next_segment = heap->segments_list;
++		heap->segments_list = segment;
++
++		best_fit = (zend_mm_free_block_canary *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
++		ZEND_MM_MARK_FIRST_BLOCK(best_fit);
++
++		block_size = segment_size - ZEND_MM_ALIGNED_SEGMENT_SIZE - ZEND_MM_ALIGNED_HEADER_SIZE;
++
++		ZEND_MM_LAST_BLOCK(ZEND_MM_BLOCK_AT(best_fit, block_size));
++
++	} else {
++zend_mm_finished_searching_for_block:
++		/* remove from free list */
++		HANDLE_BLOCK_INTERRUPTIONS();
++		ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_FREED);
++		ZEND_MM_CHECK_COOKIE(best_fit);
++		ZEND_MM_CHECK_BLOCK_LINKAGE(best_fit);
++		zend_mm_remove_from_free_list(heap, best_fit);
++
++		block_size = ZEND_MM_FREE_BLOCK_SIZE(best_fit);
++	}
++
++	remaining_size = block_size - true_size;
++
++	if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
++		true_size = block_size;
++		ZEND_MM_BLOCK(best_fit, ZEND_MM_USED_BLOCK, true_size);
++	} else {
++		zend_mm_free_block_canary *new_free_block;
++
++		/* prepare new free block */
++		ZEND_MM_BLOCK(best_fit, ZEND_MM_USED_BLOCK, true_size);
++		new_free_block = (zend_mm_free_block_canary *) ZEND_MM_BLOCK_AT(best_fit, true_size);
++		ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);
++
++		/* add the new free block to the free list */
++		if (EXPECTED(!keep_rest)) {
++			zend_mm_add_to_free_list(heap, new_free_block);
++		} else {
++			zend_mm_add_to_rest_list(heap, new_free_block);
++		}
++	}
++
++	ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 1);
++
++#if SUHOSIN_PATCH
++        SUHOSIN_MM_SET_CANARIES(best_fit);
++        ((zend_mm_block_canary*)best_fit)->info.size = size;
++        SUHOSIN_MM_SET_END_CANARY(best_fit);
++#endif
++        
++	heap->size += true_size;
++	if (heap->peak < heap->size) {
++		heap->peak = heap->size;
++	}
++
++	HANDLE_UNBLOCK_INTERRUPTIONS();
++	return ZEND_MM_DATA_OF(best_fit);
++}
++
++
++void _zend_mm_free_canary_int(zend_mm_heap_canary *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
++{
++	zend_mm_block_canary *mm_block;
++	zend_mm_block_canary *next_block;
++	size_t size;
++
++	if (!ZEND_MM_VALID_PTR(p)) {
++		return;
++	}
++
++	mm_block = ZEND_MM_HEADER_OF(p);
++	size = ZEND_MM_BLOCK_SIZE(mm_block);
++#if SUHOSIN_PATCH
++        SUHOSIN_MM_CHECK_CANARIES(mm_block, "efree()");
++#endif    
++	ZEND_MM_CHECK_PROTECTION(mm_block);
++
++#if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
++	memset(ZEND_MM_DATA_OF(mm_block), 0x5a, mm_block->debug.size);
++#endif
++#if SUHOSIN_PATCH
++        if (UNEXPECTED(SUHOSIN_CONFIG(SUHOSIN_MM_DESTROY_FREE_MEMORY))) {
++                memset(ZEND_MM_DATA_OF(mm_block), 0x5a, mm_block->info.size);
++        }
++#endif
++#if ZEND_MM_CACHE
++	if (EXPECTED(ZEND_MM_SMALL_SIZE(size)) && EXPECTED(heap->cached < ZEND_MM_CACHE_SIZE)) {
++		size_t index = ZEND_MM_BUCKET_INDEX(size);
++		zend_mm_free_block_canary **cache = &heap->cache[index];
++
++		((zend_mm_free_block_canary*)mm_block)->prev_free_block = *cache;
++		*cache = (zend_mm_free_block_canary*)mm_block;
++		heap->cached += size;
++		ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_CACHED);
++#if ZEND_MM_CACHE_STAT
++		if (++heap->cache_stat[index].count > heap->cache_stat[index].max_count) {
++			heap->cache_stat[index].max_count = heap->cache_stat[index].count;
++		}
++#endif
++		return;
++	}
++#endif
++
++	HANDLE_BLOCK_INTERRUPTIONS();
++
++	heap->size -= size;
++
++	next_block = ZEND_MM_BLOCK_AT(mm_block, size);
++	if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
++		zend_mm_remove_from_free_list(heap, (zend_mm_free_block_canary *) next_block);
++		size += ZEND_MM_FREE_BLOCK_SIZE(next_block);
++	}
++	if (ZEND_MM_PREV_BLOCK_IS_FREE(mm_block)) {
++		mm_block = ZEND_MM_PREV_BLOCK(mm_block);
++		zend_mm_remove_from_free_list(heap, (zend_mm_free_block_canary *) mm_block);
++		size += ZEND_MM_FREE_BLOCK_SIZE(mm_block);
++	}
++	if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
++	    ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_BLOCK_AT(mm_block, size))) {
++		zend_mm_del_segment(heap, (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE));
++	} else {
++		ZEND_MM_BLOCK(mm_block, ZEND_MM_FREE_BLOCK, size);
++		zend_mm_add_to_free_list(heap, (zend_mm_free_block_canary *) mm_block);
++	}
++	HANDLE_UNBLOCK_INTERRUPTIONS();
++}
++
++void *_zend_mm_realloc_canary_int(zend_mm_heap_canary *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
++{
++	zend_mm_block_canary *mm_block = ZEND_MM_HEADER_OF(p);
++	zend_mm_block_canary *next_block;
++	size_t true_size;
++	size_t orig_size;
++	void *ptr;
++
++	if (UNEXPECTED(!p) || !ZEND_MM_VALID_PTR(p)) {
++		return _zend_mm_alloc_canary_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++	}
++	mm_block = ZEND_MM_HEADER_OF(p);
++	true_size = ZEND_MM_TRUE_SIZE(size);
++	orig_size = ZEND_MM_BLOCK_SIZE(mm_block);
++#if SUHOSIN_PATCH
++        SUHOSIN_MM_CHECK_CANARIES(mm_block, "erealloc()");
++#endif	
++	ZEND_MM_CHECK_PROTECTION(mm_block);
++
++	if (UNEXPECTED(true_size < size)) {
++		goto out_of_memory;
++	}
++
++	if (true_size <= orig_size) {
++		size_t remaining_size = orig_size - true_size;
++
++		if (remaining_size >= ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
++			zend_mm_free_block_canary *new_free_block;
++
++			HANDLE_BLOCK_INTERRUPTIONS();
++			next_block = ZEND_MM_BLOCK_AT(mm_block, orig_size);
++			if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
++				remaining_size += ZEND_MM_FREE_BLOCK_SIZE(next_block);
++				zend_mm_remove_from_free_list(heap, (zend_mm_free_block_canary *) next_block);
++			}
++
++			/* prepare new free block */
++			ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
++			new_free_block = (zend_mm_free_block_canary *) ZEND_MM_BLOCK_AT(mm_block, true_size);
++
++			ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);
++
++			/* add the new free block to the free list */
++			zend_mm_add_to_free_list(heap, new_free_block);
++			heap->size += (true_size - orig_size);
++			HANDLE_UNBLOCK_INTERRUPTIONS();
++		}
++		ZEND_MM_SET_DEBUG_INFO(mm_block, size, 0, 0);
++#if SUHOSIN_PATCH
++                SUHOSIN_MM_SET_CANARIES(mm_block);
++                ((zend_mm_block_canary*)mm_block)->info.size = size;
++                SUHOSIN_MM_SET_END_CANARY(mm_block);
++#endif
++		return p;
++	}
++
++#if ZEND_MM_CACHE
++	if (ZEND_MM_SMALL_SIZE(true_size)) {
++		size_t index = ZEND_MM_BUCKET_INDEX(true_size);
++		
++		if (heap->cache[index] != NULL) {
++			zend_mm_free_block_canary *best_fit;
++			zend_mm_free_block_canary **cache;
++
++#if ZEND_MM_CACHE_STAT
++			heap->cache_stat[index].count--;
++			heap->cache_stat[index].hit++;
++#endif
++			best_fit = heap->cache[index];
++			heap->cache[index] = best_fit->prev_free_block;
++			ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_CACHED);
++			ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);                        	
++#if SUHOSIN_PATCH
++                        SUHOSIN_MM_SET_CANARIES(best_fit);
++                        ((zend_mm_block_canary*)best_fit)->info.size = size;
++                        SUHOSIN_MM_SET_END_CANARY(best_fit);
++#endif
++
++			ptr = ZEND_MM_DATA_OF(best_fit);
++
++#if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
++			memcpy(ptr, p, mm_block->debug.size);
++#else
++			memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE - CANARY_SIZE);
++#endif
++
++			heap->cached -= true_size - orig_size;
++
++			index = ZEND_MM_BUCKET_INDEX(orig_size);
++			cache = &heap->cache[index];
++
++			((zend_mm_free_block_canary*)mm_block)->prev_free_block = *cache;
++			*cache = (zend_mm_free_block_canary*)mm_block;
++			ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_CACHED);
++#if ZEND_MM_CACHE_STAT
++			if (++heap->cache_stat[index].count > heap->cache_stat[index].max_count) {
++				heap->cache_stat[index].max_count = heap->cache_stat[index].count;
++			}
++#endif
++			return ptr;
++		}
++	}
++#endif
++
++	next_block = ZEND_MM_BLOCK_AT(mm_block, orig_size);
++
++	if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
++		ZEND_MM_CHECK_COOKIE(next_block);
++		ZEND_MM_CHECK_BLOCK_LINKAGE(next_block);
++		if (orig_size + ZEND_MM_FREE_BLOCK_SIZE(next_block) >= true_size) {
++			size_t block_size = orig_size + ZEND_MM_FREE_BLOCK_SIZE(next_block);
++			size_t remaining_size = block_size - true_size;
++
++			HANDLE_BLOCK_INTERRUPTIONS();
++			zend_mm_remove_from_free_list(heap, (zend_mm_free_block_canary *) next_block);
++
++			if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
++				true_size = block_size;
++				ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
++			} else {
++				zend_mm_free_block_canary *new_free_block;
++
++				/* prepare new free block */
++				ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
++				new_free_block = (zend_mm_free_block_canary *) ZEND_MM_BLOCK_AT(mm_block, true_size);
++				ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);
++
++				/* add the new free block to the free list */
++				if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
++				    ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_BLOCK_AT(new_free_block, remaining_size))) {
++					zend_mm_add_to_rest_list(heap, new_free_block);
++				} else {
++					zend_mm_add_to_free_list(heap, new_free_block);
++				}
++			}
++			ZEND_MM_SET_DEBUG_INFO(mm_block, size, 0, 0);
++			heap->size = heap->size + true_size - orig_size;
++			if (heap->peak < heap->size) {
++				heap->peak = heap->size;
++			}
++			HANDLE_UNBLOCK_INTERRUPTIONS();
++#if SUHOSIN_PATCH
++                        SUHOSIN_MM_SET_CANARIES(mm_block);
++                        ((zend_mm_block_canary*)mm_block)->info.size = size;
++                        SUHOSIN_MM_SET_END_CANARY(mm_block);
++#endif
++			return p;
++		} else if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
++				   ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_BLOCK_AT(next_block, ZEND_MM_FREE_BLOCK_SIZE(next_block)))) {
++			HANDLE_BLOCK_INTERRUPTIONS();
++			zend_mm_remove_from_free_list(heap, (zend_mm_free_block_canary *) next_block);
++			goto realloc_segment;
++		}
++	} else if (ZEND_MM_IS_FIRST_BLOCK(mm_block) && ZEND_MM_IS_GUARD_BLOCK(next_block)) {
++		zend_mm_segment *segment;
++		zend_mm_segment *segment_copy;
++		size_t segment_size;
++		size_t block_size;
++		size_t remaining_size;
++
++		HANDLE_BLOCK_INTERRUPTIONS();
++realloc_segment:
++		/* segment size, size of block and size of guard block */
++		if (true_size > heap->block_size - (ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE)) {
++			segment_size = true_size+ZEND_MM_ALIGNED_SEGMENT_SIZE+ZEND_MM_ALIGNED_HEADER_SIZE;
++			segment_size = (segment_size + (heap->block_size-1)) & ~(heap->block_size-1);
++		} else {
++			segment_size = heap->block_size;
++		}
++
++		segment_copy = (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE);
++		if (segment_size < true_size ||
++		    heap->real_size + segment_size - segment_copy->size > heap->limit) {
++			if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
++				zend_mm_add_to_free_list(heap, (zend_mm_free_block_canary *) next_block);
++			}
++#if ZEND_MM_CACHE
++			zend_mm_free_cache(heap);
++#endif
++			HANDLE_UNBLOCK_INTERRUPTIONS();
++#if ZEND_DEBUG
++			zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted at %s:%d (tried to allocate %ld bytes)", heap->limit, __zend_filename, __zend_lineno, size);
++#else
++			zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted (tried to allocate %ld bytes)", heap->limit, size);
++#endif
++			return NULL;
++		}
++
++		segment = ZEND_MM_STORAGE_REALLOC(segment_copy, segment_size);
++		if (!segment) {
++#if ZEND_MM_CACHE
++			zend_mm_free_cache(heap);
++#endif
++			HANDLE_UNBLOCK_INTERRUPTIONS();
++out_of_memory:
++#if ZEND_DEBUG
++			zend_mm_safe_error(heap, "Out of memory (allocated %ld) at %s:%d (tried to allocate %ld bytes)", heap->real_size, __zend_filename, __zend_lineno, size);
++#else
++			zend_mm_safe_error(heap, "Out of memory (allocated %ld) (tried to allocate %ld bytes)", heap->real_size, size);
++#endif
++			return NULL;
++		}
++		heap->real_size += segment_size - segment->size;
++		if (heap->real_size > heap->real_peak) {
++			heap->real_peak = heap->real_size;
++		}
++
++		segment->size = segment_size;
++
++		if (segment != segment_copy) {
++			zend_mm_segment **seg = &heap->segments_list;
++			while (*seg != segment_copy) {
++				seg = &(*seg)->next_segment;
++			}
++			*seg = segment;
++			mm_block = (zend_mm_block_canary *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
++			ZEND_MM_MARK_FIRST_BLOCK(mm_block);
++		}
++
++		block_size = segment_size - ZEND_MM_ALIGNED_SEGMENT_SIZE - ZEND_MM_ALIGNED_HEADER_SIZE;
++		remaining_size = block_size - true_size;
++
++		/* setup guard block */
++		ZEND_MM_LAST_BLOCK(ZEND_MM_BLOCK_AT(mm_block, block_size));
++
++		if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
++			true_size = block_size;
++			ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
++		} else {
++			zend_mm_free_block_canary *new_free_block;
++
++			/* prepare new free block */
++			ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
++			new_free_block = (zend_mm_free_block_canary *) ZEND_MM_BLOCK_AT(mm_block, true_size);
++			ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);
++
++			/* add the new free block to the free list */
++			zend_mm_add_to_rest_list(heap, new_free_block);
++		}
++
++		ZEND_MM_SET_DEBUG_INFO(mm_block, size, 1, 1);
++
++		heap->size = heap->size + true_size - orig_size;
++		if (heap->peak < heap->size) {
++			heap->peak = heap->size;
++		}
++
++		HANDLE_UNBLOCK_INTERRUPTIONS();
++#if SUHOSIN_PATCH
++                SUHOSIN_MM_SET_CANARIES(mm_block);
++                ((zend_mm_block_canary*)mm_block)->info.size = size;
++                SUHOSIN_MM_SET_END_CANARY(mm_block);
++#endif
++		return ZEND_MM_DATA_OF(mm_block);
++	}
++
++	ptr = _zend_mm_alloc_canary_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++#if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
++	memcpy(ptr, p, mm_block->debug.size);
++#else
++	memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE - CANARY_SIZE);
++#endif
++	_zend_mm_free_canary_int(heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
++	return ptr;
++}
++
++ZEND_API size_t _zend_mm_block_size_canary(zend_mm_heap_canary *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
++{
++	zend_mm_block_canary *mm_block;
++
++	if (!ZEND_MM_VALID_PTR(p)) {
++		return 0;
++	}
++	mm_block = ZEND_MM_HEADER_OF(p);
++	ZEND_MM_CHECK_PROTECTION(mm_block);
++#if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
++	return mm_block->debug.size;
++#else
++	return ZEND_MM_BLOCK_SIZE(mm_block);
++#endif
++}
++
++#if defined(__GNUC__) && defined(i386)
++
++static inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
++{
++	size_t res = nmemb;
++	unsigned long overflow = 0;
++
++	__asm__ ("mull %3\n\taddl %4,%0\n\tadcl %1,%1"
++	     : "=&a"(res), "=&d" (overflow)
++	     : "%0"(res),
++	       "rm"(size),
++	       "rm"(offset));
++	
++	if (UNEXPECTED(overflow)) {
++		zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
++		return 0;
++	}
++	return res;
++}
++
++#elif defined(__GNUC__) && defined(__x86_64__)
++
++static inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
++{
++        size_t res = nmemb;
++        unsigned long overflow = 0;
++
++        __asm__ ("mulq %3\n\taddq %4,%0\n\tadcq %1,%1"
++             : "=&a"(res), "=&d" (overflow)
++             : "%0"(res),
++               "rm"(size),
++               "rm"(offset));
++
++        if (UNEXPECTED(overflow)) {
++                zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
++                return 0;
++        }
++        return res;
++}
++
++#elif SIZEOF_SIZE_T == 4 && defined(HAVE_ZEND_LONG64)
++
++static inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
++{
++	zend_ulong64 res = (zend_ulong64)nmemb * (zend_ulong64)size + (zend_ulong64)offset;
++
++	if (UNEXPECTED(res > (zend_ulong64)0xFFFFFFFFL)) {
++		zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
++		return 0;
++	}
++	return (size_t) res;
++}
++
++#else
++
++static inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
++{
++	size_t res = nmemb * size + offset;
++	double _d  = (double)nmemb * (double)size + (double)offset;
++	double _delta = (double)res - _d;
++
++	if (UNEXPECTED((_d + _delta ) != _d)) {
++		zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
++		return 0;
++	}
++	return res;
++}
++#endif
++
++/*
++ * Local variables:
++ * tab-width: 4
++ * c-basic-offset: 4
++ * indent-tabs-mode: t
++ * End:
++ */
++
+Index: pkg-php/Zend/zend_canary.c
+===================================================================
+--- /dev/null	1970-01-01 00:00:00.000000000 +0000
++++ pkg-php/Zend/zend_canary.c	2009-08-25 15:13:09.000000000 +0200
 @@ -0,0 +1,64 @@
 +/*
 +   +----------------------------------------------------------------------+
 +   | Suhosin-Patch for PHP                                                |
 +   +----------------------------------------------------------------------+
-+   | Copyright (c) 2004-2006 Stefan Esser                                 |
++   | Copyright (c) 2004-2009 Stefan Esser                                 |
 +   +----------------------------------------------------------------------+
 +   | This source file is subject to version 2.02 of the PHP license,      |
 +   | that is bundled with this package in the file LICENSE, and is        |
@@ -719,7 +3373,7 @@ the following modifications have been made:
 +   | obtain it through the world-wide-web, please send a note to          |
 +   | license at php.net so we can mail you a copy immediately.               |
 +   +----------------------------------------------------------------------+
-+   | Author: Stefan Esser <sesser at hardened-php.net>                       |
++   | Author: Stefan Esser <stefan.esser at sektioneins.de>                   |
 +   +----------------------------------------------------------------------+
 + */
 +/* $Id: zend_canary.c,v 1.1 2004/11/26 12:45:41 ionic Exp $ */
@@ -769,55 +3423,46 @@ the following modifications have been made:
 + * vim600: sw=4 ts=4 fdm=marker
 + * vim<600: sw=4 ts=4
 + */
---- php.orig/Zend/zend_compile.c
-+++ php/Zend/zend_compile.c
-@@ -54,7 +54,6 @@ static void zend_duplicate_property_info
- 	property_info->name = zend_strndup(property_info->name, property_info->name_length);
- }
- 
--
- static void zend_destroy_property_info(zend_property_info *property_info)
- {
- 	efree(property_info->name);
-@@ -68,6 +67,10 @@ static void zend_destroy_property_info_i
- {
+Index: pkg-php/Zend/zend_compile.c
+===================================================================
+--- pkg-php.orig/Zend/zend_compile.c	2009-08-25 15:11:47.000000000 +0200
++++ pkg-php/Zend/zend_compile.c	2009-08-25 15:13:09.000000000 +0200
+@@ -69,6 +69,11 @@
  	free(property_info->name);
  }
+ 
 +#if SUHOSIN_PATCH
 +void *suhosin_zend_destroy_property_info_internal = zend_destroy_property_info_internal;
 +void *suhosin_zend_destroy_property_info = zend_destroy_property_info;
 +#endif
- 
++
  static void build_runtime_defined_function_key(zval *result, const char *name, int name_length TSRMLS_DC)
  {
---- php.orig/Zend/zend_compile.h
-+++ php/Zend/zend_compile.h
-@@ -608,6 +608,11 @@ ZEND_API size_t zend_dirname(char *path,
- 
- int zendlex(znode *zendlval TSRMLS_DC);
+ 	char char_pos_buf[32];
+Index: pkg-php/Zend/zend_compile.h
+===================================================================
+--- pkg-php.orig/Zend/zend_compile.h	2009-08-25 15:11:47.000000000 +0200
++++ pkg-php/Zend/zend_compile.h	2009-08-25 15:13:09.000000000 +0200
+@@ -606,6 +606,11 @@
+ ZEND_API int zend_auto_global_disable_jit(const char *varname, zend_uint varname_length TSRMLS_DC);
+ ZEND_API size_t zend_dirname(char *path, size_t len);
  
 +#if SUHOSIN_PATCH
 +extern void *suhosin_zend_destroy_property_info_internal;
 +extern void *suhosin_zend_destroy_property_info;
 +#endif
 +
- /* BEGIN: OPCODES */
- 
- #include "zend_vm_opcodes.h"
-@@ -732,6 +737,7 @@ int zendlex(znode *zendlval TSRMLS_DC);
- #define ZEND_RETURNS_FUNCTION 1<<0
- #define ZEND_RETURNS_NEW      1<<1
- 
-+
- END_EXTERN_C()
+ int zendlex(znode *zendlval TSRMLS_DC);
  
- #define ZEND_CLONE_FUNC_NAME		"__clone"
---- php.orig/Zend/zend_constants.c
-+++ php/Zend/zend_constants.c
-@@ -112,6 +112,75 @@ void zend_register_standard_constants(TS
- 	REGISTER_MAIN_LONG_CONSTANT("E_USER_DEPRECATED", E_USER_DEPRECATED, CONST_PERSISTENT | CONST_CS);
+ /* BEGIN: OPCODES */
+Index: pkg-php/Zend/zend_constants.c
+===================================================================
+--- pkg-php.orig/Zend/zend_constants.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/Zend/zend_constants.c	2009-08-25 15:13:09.000000000 +0200
+@@ -113,6 +113,76 @@
  
  	REGISTER_MAIN_LONG_CONSTANT("E_ALL", E_ALL, CONST_PERSISTENT | CONST_CS);
+ 
 +#if SUHOSIN_PATCH
 +	REGISTER_MAIN_LONG_CONSTANT("S_MEMORY", S_MEMORY, CONST_PERSISTENT | CONST_CS);
 +	REGISTER_MAIN_LONG_CONSTANT("S_VARS", S_VARS, CONST_PERSISTENT | CONST_CS);
@@ -864,7 +3509,7 @@ the following modifications have been made:
 +	/* AIX doesn't have LOG_AUTHPRIV */
 +	REGISTER_MAIN_LONG_CONSTANT("LOG_AUTHPRIV", LOG_AUTHPRIV, CONST_CS | CONST_PERSISTENT);
 +#endif
-+#if !defined(PHP_WIN32) && !defined(NETWARE)
++#ifndef PHP_WIN32
 +	REGISTER_MAIN_LONG_CONSTANT("LOG_LOCAL0", LOG_LOCAL0, CONST_CS | CONST_PERSISTENT);
 +	REGISTER_MAIN_LONG_CONSTANT("LOG_LOCAL1", LOG_LOCAL1, CONST_CS | CONST_PERSISTENT);
 +	REGISTER_MAIN_LONG_CONSTANT("LOG_LOCAL2", LOG_LOCAL2, CONST_CS | CONST_PERSISTENT);
@@ -887,11 +3532,14 @@ the following modifications have been made:
 +	REGISTER_MAIN_LONG_CONSTANT("LOG_PERROR", LOG_PERROR, CONST_CS | CONST_PERSISTENT); /*log to stderr*/
 +#endif
 +#endif
- 
++
  	/* true/false constants */
  	{
---- php.orig/Zend/zend_errors.h
-+++ php/Zend/zend_errors.h
+ 		zend_constant c;
+Index: pkg-php/Zend/zend_errors.h
+===================================================================
+--- pkg-php.orig/Zend/zend_errors.h	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/Zend/zend_errors.h	2009-08-25 15:13:09.000000000 +0200
 @@ -41,6 +41,20 @@
  #define E_ALL (E_ERROR | E_WARNING | E_PARSE | E_NOTICE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE | E_RECOVERABLE_ERROR | E_DEPRECATED | E_USER_DEPRECATED)
  #define E_CORE (E_CORE_ERROR | E_CORE_WARNING)
@@ -913,20 +3561,22 @@ the following modifications have been made:
  #endif /* ZEND_ERRORS_H */
  
  /*
---- php.orig/Zend/zend_hash.c
-+++ php/Zend/zend_hash.c
+Index: pkg-php/Zend/zend_hash.c
+===================================================================
+--- pkg-php.orig/Zend/zend_hash.c	2009-08-25 15:11:47.000000000 +0200
++++ pkg-php/Zend/zend_hash.c	2009-08-25 15:13:09.000000000 +0200
 @@ -20,6 +20,7 @@
- /* $Id: zend_hash.c,v 1.121.2.4.2.8.2.8 2008/12/31 11:15:32 sebastian Exp $ */
+ /* $Id: zend_hash.c,v 1.121.2.4.2.8.2.9 2009/06/07 19:28:15 mattwil Exp $ */
  
  #include "zend.h"
 +#include "zend_compile.h"
  
  #define CONNECT_TO_BUCKET_DLLIST(element, list_head)		\
  	(element)->pNext = (list_head);							\
-@@ -132,7 +133,189 @@ ZEND_API ulong zend_hash_func(const char
- 		(p)->pDataPtr=NULL;												\
+@@ -133,6 +134,191 @@
  	}
  
+ 
 +#if SUHOSIN_PATCH
 +#ifdef ZTS
 +MUTEX_T zend_hash_dprot_mx_reader;
@@ -1097,7 +3747,9 @@ the following modifications have been made:
 +			zend_hash_dprot_end_read();
 +		
 +			zend_suhosin_log(S_MEMORY, "possible memory corruption detected - unknown Hashtable destructor");
-+			exit(1);
++			if (SUHOSIN_CONFIG(SUHOSIN_HT_IGNORE_INVALID_DESTRUCTOR) == 0) {
++			        _exit(1);
++		        }
 +			return;
 +		}
 +	
@@ -1105,7 +3757,7 @@ the following modifications have been made:
 +	
 +	zend_hash_dprot_end_read();
 +}
- 
++
 +#else
 +#define zend_hash_add_destructor(pDestructor) do {} while(0)
 +#define zend_hash_check_destructor(pDestructor) do {} while(0)
@@ -1113,7 +3765,7 @@ the following modifications have been made:
  
  ZEND_API int _zend_hash_init(HashTable *ht, uint nSize, hash_func_t pHashFunction, dtor_func_t pDestructor, zend_bool persistent ZEND_FILE_LINE_DC)
  {
-@@ -153,6 +336,7 @@ ZEND_API int _zend_hash_init(HashTable *
+@@ -153,6 +339,7 @@
  
  	ht->nTableMask = ht->nTableSize - 1;
  	ht->pDestructor = pDestructor;
@@ -1121,32 +3773,31 @@ the following modifications have been made:
  	ht->arBuckets = NULL;
  	ht->pListHead = NULL;
  	ht->pListTail = NULL;
-@@ -230,6 +414,8 @@ ZEND_API int _zend_hash_add_or_update(Ha
+@@ -230,6 +417,7 @@
  					return FAILURE;
  				}
  #endif
-+				
 +				zend_hash_check_destructor(ht->pDestructor);
  				if (ht->pDestructor) {
  					ht->pDestructor(p->pData);
  				}
-@@ -295,6 +481,7 @@ ZEND_API int _zend_hash_quick_add_or_upd
+@@ -295,6 +483,7 @@
  					return FAILURE;
  				}
  #endif
-+				zend_hash_check_destructor(ht->pDestructor);
++                                zend_hash_check_destructor(ht->pDestructor);
  				if (ht->pDestructor) {
  					ht->pDestructor(p->pData);
  				}
-@@ -370,6 +557,7 @@ ZEND_API int _zend_hash_index_update_or_
+@@ -370,6 +559,7 @@
  				return FAILURE;
  			}
  #endif
-+			zend_hash_check_destructor(ht->pDestructor);
++                        zend_hash_check_destructor(ht->pDestructor);
  			if (ht->pDestructor) {
  				ht->pDestructor(p->pData);
  			}
-@@ -493,6 +681,7 @@ ZEND_API int zend_hash_del_key_or_index(
+@@ -493,6 +683,7 @@
  			if (ht->pInternalPointer == p) {
  				ht->pInternalPointer = p->pListNext;
  			}
@@ -1154,35 +3805,35 @@ the following modifications have been made:
  			if (ht->pDestructor) {
  				ht->pDestructor(p->pData);
  			}
-@@ -518,6 +707,8 @@ ZEND_API void zend_hash_destroy(HashTabl
- 
+@@ -519,6 +710,7 @@
  	SET_INCONSISTENT(HT_IS_DESTROYING);
  
-+	zend_hash_check_destructor(ht->pDestructor);
-+
  	p = ht->pListHead;
++	zend_hash_check_destructor(ht->pDestructor);
  	while (p != NULL) {
  		q = p;
-@@ -544,6 +735,8 @@ ZEND_API void zend_hash_clean(HashTable 
- 
+ 		p = p->pListNext;
+@@ -545,6 +737,7 @@
  	SET_INCONSISTENT(HT_CLEANING);
  
-+	zend_hash_check_destructor(ht->pDestructor);
-+
  	p = ht->pListHead;
++	zend_hash_check_destructor(ht->pDestructor);
  	while (p != NULL) {
  		q = p;
-@@ -607,6 +800,7 @@ static Bucket *zend_hash_apply_deleter(H
+ 		p = p->pListNext;
+@@ -607,6 +800,7 @@
  	ht->nNumOfElements--;
  	HANDLE_UNBLOCK_INTERRUPTIONS();
  
-+	zend_hash_check_destructor(ht->pDestructor);
++        zend_hash_check_destructor(ht->pDestructor);
  	if (ht->pDestructor) {
  		ht->pDestructor(p->pData);
  	}
---- php.orig/Zend/zend_llist.c
-+++ php/Zend/zend_llist.c
-@@ -23,6 +23,184 @@
+Index: pkg-php/Zend/zend_llist.c
+===================================================================
+--- pkg-php.orig/Zend/zend_llist.c	2009-08-25 15:11:47.000000000 +0200
++++ pkg-php/Zend/zend_llist.c	2009-08-25 15:13:09.000000000 +0200
+@@ -23,6 +23,186 @@
  #include "zend_llist.h"
  #include "zend_qsort.h"
  
@@ -1350,8 +4001,10 @@ the following modifications have been made:
 +		if (!found) {
 +			zend_llist_dprot_end_read();
 +		
-+			zend_suhosin_log(S_MEMORY, "possible memory corruption detected - unknown llist destructor");
-+			exit(1);
++		        zend_suhosin_log(S_MEMORY, "possible memory corruption detected - unknown llist destructor");
++			if (SUHOSIN_CONFIG(SUHOSIN_LL_IGNORE_INVALID_DESTRUCTOR) == 0) {
++			        _exit(1);
++		        }
 +			return;
 +		}
 +	
@@ -1367,7 +4020,7 @@ the following modifications have been made:
  ZEND_API void zend_llist_init(zend_llist *l, size_t size, llist_dtor_func_t dtor, unsigned char persistent)
  {
  	l->head  = NULL;
-@@ -30,6 +208,7 @@ ZEND_API void zend_llist_init(zend_llist
+@@ -30,6 +210,7 @@
  	l->count = 0;
  	l->size  = size;
  	l->dtor  = dtor;
@@ -1375,7 +4028,7 @@ the following modifications have been made:
  	l->persistent = persistent;
  }
  
-@@ -81,6 +260,7 @@ ZEND_API void zend_llist_prepend_element
+@@ -81,6 +262,7 @@
  			} else {\
  				(l)->tail = (current)->prev;\
  			}\
@@ -1383,7 +4036,7 @@ the following modifications have been made:
  			if ((l)->dtor) {\
  				(l)->dtor((current)->data);\
  			}\
-@@ -108,6 +288,7 @@ ZEND_API void zend_llist_destroy(zend_ll
+@@ -108,6 +290,7 @@
  {
  	zend_llist_element *current=l->head, *next;
  	
@@ -1391,17 +4044,134 @@ the following modifications have been made:
  	while (current) {
  		next = current->next;
  		if (l->dtor) {
-@@ -133,6 +314,7 @@ ZEND_API void *zend_llist_remove_tail(ze
+@@ -133,6 +316,7 @@
  	zend_llist_element *old_tail;
  	void *data;
  
-+	zend_llist_check_destructor(l->dtor);
++	zend_llist_check_destructor((l)->dtor);
  	if ((old_tail = l->tail)) {
  		if (old_tail->prev) {
  			old_tail->prev->next = NULL;
---- php.orig/configure.in
-+++ php/configure.in
-@@ -304,6 +304,7 @@ sinclude(Zend/Zend.m4)
+Index: pkg-php/Zend/zend_operators.c
+===================================================================
+--- pkg-php.orig/Zend/zend_operators.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/Zend/zend_operators.c	2009-08-25 15:13:09.000000000 +0200
+@@ -152,9 +152,14 @@
+ 		case IS_STRING:
+ 			{
+ 				char *strval;
++                                int strl;
+ 
+ 				strval = Z_STRVAL_P(op);
+-				if ((Z_TYPE_P(op)=is_numeric_string(strval, Z_STRLEN_P(op), &Z_LVAL_P(op), &Z_DVAL_P(op), 1)) == 0) {
++                                strl   = Z_STRLEN_P(op);
++#if SUHOSIN_PATCH
++                                Z_STRLEN_P(op) = 0;
++#endif
++				if ((Z_TYPE_P(op)=is_numeric_string(strval, strl, &Z_LVAL_P(op), &Z_DVAL_P(op), 1)) == 0) {
+ 					ZVAL_LONG(op, 0);
+ 				}
+ 				STR_FREE(strval);
+@@ -186,7 +191,8 @@
+ 	} else {														\
+ 		switch (Z_TYPE_P(op)) {										\
+ 			case IS_STRING:											\
+-				{													\
++				{ \
++                                        Z_STRLEN(holder) = 0;													\
+ 					if ((Z_TYPE(holder)=is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &Z_LVAL(holder), &Z_DVAL(holder), 1)) == 0) {	\
+ 						ZVAL_LONG(&(holder), 0);							\
+ 					}														\
+@@ -228,6 +234,7 @@
+ 				Z_LVAL(holder) = zend_dval_to_lval(Z_DVAL_P(op));	\
+ 				break;												\
+ 			case IS_STRING:											\
++                                Z_STRLEN(holder) = 0; \
+ 				Z_LVAL(holder) = strtol(Z_STRVAL_P(op), NULL, 10);	\
+ 				break;												\
+ 			case IS_ARRAY:											\
+@@ -270,6 +277,7 @@
+ 				Z_LVAL(holder) = (Z_DVAL_P(op) ? 1 : 0);			\
+ 				break;												\
+ 			case IS_STRING:											\
++                                Z_STRLEN(holder) = 0; \
+ 				if (Z_STRLEN_P(op) == 0								\
+ 					|| (Z_STRLEN_P(op)==1 && Z_STRVAL_P(op)[0]=='0')) {	\
+ 					Z_LVAL(holder) = 0;								\
+@@ -355,6 +363,9 @@
+ 			{
+ 				char *strval = Z_STRVAL_P(op);
+ 
++#if SUHOSIN_PATCH
++                                Z_STRLEN_P(op) = 0;
++#endif
+ 				Z_LVAL_P(op) = strtol(strval, NULL, base);
+ 				STR_FREE(strval);
+ 			}
+@@ -415,6 +426,9 @@
+ 			{
+ 				char *strval = Z_STRVAL_P(op);
+ 
++#if SUHOSIN_PATCH
++                                Z_STRLEN_P(op) = 0;
++#endif
+ 				Z_DVAL_P(op) = zend_strtod(strval, NULL);
+ 				STR_FREE(strval);
+ 			}
+@@ -501,8 +515,14 @@
+ 
+ 				if (Z_STRLEN_P(op) == 0
+ 					|| (Z_STRLEN_P(op)==1 && Z_STRVAL_P(op)[0]=='0')) {
++#if SUHOSIN_PATCH
++                                        Z_STRLEN_P(op) = 0;
++#endif
+ 					Z_LVAL_P(op) = 0;
+ 				} else {
++#if SUHOSIN_PATCH
++                                        Z_STRLEN_P(op) = 0;
++#endif
+ 					Z_LVAL_P(op) = 1;
+ 				}
+ 				STR_FREE(strval);
+@@ -616,6 +636,9 @@
+ 	*entry = *op;
+ 	INIT_PZVAL(entry);
+ 
++#if SUHOSIN_PATCH
++        Z_STRLEN_P(op) = 0;
++#endif
+ 	switch (type) {
+ 		case IS_ARRAY:
+ 			ALLOC_HASHTABLE(Z_ARRVAL_P(op));
+Index: pkg-php/Zend/zend_variables.c
+===================================================================
+--- pkg-php.orig/Zend/zend_variables.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/Zend/zend_variables.c	2009-08-25 15:13:09.000000000 +0200
+@@ -34,6 +34,9 @@
+ 		case IS_CONSTANT:
+ 			CHECK_ZVAL_STRING_REL(zvalue);
+ 			STR_FREE_REL(zvalue->value.str.val);
++#if SUHOSIN_PATCH
++                        zvalue->value.str.len = 0;
++#endif
+ 			break;
+ 		case IS_ARRAY:
+ 		case IS_CONSTANT_ARRAY: {
+@@ -78,6 +81,9 @@
+ 		case IS_CONSTANT:
+ 			CHECK_ZVAL_STRING_REL(zvalue);
+ 			free(zvalue->value.str.val);
++#if SUHOSIN_PATCH
++                        zvalue->value.str.len = 0;
++#endif
+ 			break;
+ 		case IS_ARRAY:
+ 		case IS_CONSTANT_ARRAY:
+Index: pkg-php/configure.in
+===================================================================
+--- pkg-php.orig/configure.in	2009-08-25 15:13:09.000000000 +0200
++++ pkg-php/configure.in	2009-08-25 15:13:09.000000000 +0200
+@@ -307,6 +307,7 @@
  sinclude(TSRM/threads.m4)
  sinclude(TSRM/tsrm.m4)
  
@@ -1409,7 +4179,7 @@ the following modifications have been made:
  
  divert(2)
  
-@@ -1427,7 +1428,7 @@ PHP_ADD_SOURCES(main, main.c snprintf.c 
+@@ -1422,7 +1423,7 @@
         php_ini.c SAPI.c rfc1867.c php_content_types.c strlcpy.c \
         strlcat.c mergesort.c reentrancy.c php_variables.c php_ticks.c \
         network.c php_open_temporary_file.c php_logos.c \
@@ -1418,34 +4188,24 @@ the following modifications have been made:
  
  PHP_ADD_SOURCES(main/streams, streams.c cast.c memory.c filter.c \
         plain_wrapper.c userspace.c transports.c xp_socket.c mmap.c \
-@@ -1455,7 +1456,7 @@ PHP_ADD_SOURCES(Zend, \
+@@ -1450,7 +1451,7 @@
      zend_list.c zend_indent.c zend_builtin_functions.c zend_sprintf.c \
      zend_ini.c zend_qsort.c zend_multibyte.c zend_ts_hash.c zend_stream.c \
      zend_iterators.c zend_interfaces.c zend_exceptions.c zend_strtod.c zend_gc.c \
 -    zend_closures.c zend_float.c)
-+    zend_closures.c zend_float.c zend_canary.c)
++    zend_closures.c zend_float.c zend_canary.c zend_alloc_canary.c )
  
  if test -r "$abs_srcdir/Zend/zend_objects.c"; then
    PHP_ADD_SOURCES(Zend, zend_objects.c zend_object_handlers.c zend_objects_API.c zend_default_classes.c)
---- php.orig/ext/standard/basic_functions.c
-+++ php/ext/standard/basic_functions.c
-@@ -3127,7 +3127,9 @@ const zend_function_entry basic_function
- 	PHP_FALIAS(socket_get_status, stream_get_meta_data,						arginfo_stream_get_meta_data)
- 
- #if (!defined(__BEOS__) && !defined(NETWARE) && HAVE_REALPATH) || defined(ZTS)
--	PHP_FE(realpath,														arginfo_realpath)
-+#undef realpath
-+	PHP_NAMED_FE(realpath,			PHP_FN(real_path),						arginfo_realpath)
-+#define realpath real_path
- #endif
- 
- #ifdef HAVE_FNMATCH
---- php.orig/ext/standard/dl.c
-+++ php/ext/standard/dl.c
-@@ -239,6 +239,19 @@ PHPAPI int php_load_extension(char *file
+Index: pkg-php/ext/standard/dl.c
+===================================================================
+--- pkg-php.orig/ext/standard/dl.c	2009-08-25 15:13:09.000000000 +0200
++++ pkg-php/ext/standard/dl.c	2009-08-25 15:13:09.000000000 +0200
+@@ -239,6 +239,23 @@
  			return FAILURE;
  		}
  	}
++
 +#if SUHOSIN_PATCH
 +	if (strncmp("suhosin", module_entry->name, sizeof("suhosin")-1) == 0) {
 +		void *log_func;
@@ -1456,44 +4216,27 @@ the following modifications have been made:
 +		}
 +		if (log_func != NULL) {
 +			zend_suhosin_log = log_func;
++		} else {
++                        zend_suhosin_log(S_MISC, "could not replace logging function");
 +		}
 +	}
 +#endif	
++
  	return SUCCESS;
  }
  /* }}} */
---- php.orig/ext/standard/file.c
-+++ php/ext/standard/file.c
-@@ -2352,7 +2352,7 @@ out:
- #if (!defined(__BEOS__) && !defined(NETWARE) && HAVE_REALPATH) || defined(ZTS)
- /* {{{ proto string realpath(string path)
-    Return the resolved path */
--PHP_FUNCTION(realpath)
-+PHP_FUNCTION(real_path)
- {
- 	char *filename;
- 	int filename_len;
---- php.orig/ext/standard/file.h
-+++ php/ext/standard/file.h
-@@ -61,7 +61,7 @@ PHP_FUNCTION(flock);
- PHP_FUNCTION(fd_set);
- PHP_FUNCTION(fd_isset);
- #if (!defined(__BEOS__) && !defined(NETWARE) && HAVE_REALPATH) || defined(ZTS)
--PHP_FUNCTION(realpath);
-+PHP_FUNCTION(real_path);
- #endif
- #ifdef HAVE_FNMATCH
- PHP_FUNCTION(fnmatch);
---- php.orig/ext/standard/info.c
-+++ php/ext/standard/info.c
-@@ -842,6 +842,31 @@ PHPAPI void php_print_info(int flag TSRM
+Index: pkg-php/ext/standard/info.c
+===================================================================
+--- pkg-php.orig/ext/standard/info.c	2009-08-25 15:13:09.000000000 +0200
++++ pkg-php/ext/standard/info.c	2009-08-25 15:13:09.000000000 +0200
+@@ -842,6 +842,33 @@
  		
  		php_info_print_table_end();
  
 +		/* Suhosin Patch */
 +		php_info_print_box_start(0);
 +		if (expose_php && !sapi_module.phpinfo_as_text) {
-+			PUTS("<a href=\"http://www.hardened-php.net/suhosin/index.html\"><img border=\"0\" src=\"");
++			PUTS("<a href=\"http://www.suhosin.org\"><img border=\"0\" src=\"");
 +			if (SG(request_info).request_uri) {
 +				char *elem_esc = php_info_html_esc(SG(request_info).request_uri TSRMLS_CC);
 +				PUTS(elem_esc);
@@ -1509,18 +4252,22 @@ the following modifications have been made:
 +		}
 +		PUTS(!sapi_module.phpinfo_as_text?"<br />":"\n");
 +		if (sapi_module.phpinfo_as_text) {
-+			PUTS("Copyright (c) 2006 Hardened-PHP Project\n");
++			PUTS("Copyright (c) 2006-2007 Hardened-PHP Project\n");
++			PUTS("Copyright (c) 2007-2009 SektionEins GmbH\n");
 +		} else {
-+			PUTS("Copyright (c) 2006 <a href=\"http://www.hardened-php.net/\">Hardened-PHP Project</a>\n");
++			PUTS("Copyright (c) 2006-2007 <a href=\"http://www.hardened-php.net/\">Hardened-PHP Project</a>\n");
++			PUTS("Copyright (c) 2007-2009 <a href=\"http://www.sektioneins.de/\">SektionEins GmbH</a>\n");
 +		}
 +		php_info_print_box_end();
-+		
++
  		/* Zend Engine */
  		php_info_print_box_start(0);
  		if (expose_php && !sapi_module.phpinfo_as_text) {
---- php.orig/ext/standard/syslog.c
-+++ php/ext/standard/syslog.c
-@@ -42,6 +42,7 @@ static void start_syslog(TSRMLS_D);
+Index: pkg-php/ext/standard/syslog.c
+===================================================================
+--- pkg-php.orig/ext/standard/syslog.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/ext/standard/syslog.c	2009-08-25 15:13:09.000000000 +0200
+@@ -42,6 +42,7 @@
   */
  PHP_MINIT_FUNCTION(syslog)
  {
@@ -1528,7 +4275,7 @@ the following modifications have been made:
  	/* error levels */
  	REGISTER_LONG_CONSTANT("LOG_EMERG", LOG_EMERG, CONST_CS | CONST_PERSISTENT); /* system unusable */
  	REGISTER_LONG_CONSTANT("LOG_ALERT", LOG_ALERT, CONST_CS | CONST_PERSISTENT); /* immediate action required */
-@@ -97,6 +98,7 @@ PHP_MINIT_FUNCTION(syslog)
+@@ -97,6 +98,7 @@
  	/* AIX doesn't have LOG_PERROR */
  	REGISTER_LONG_CONSTANT("LOG_PERROR", LOG_PERROR, CONST_CS | CONST_PERSISTENT); /*log to stderr*/
  #endif
@@ -1536,185 +4283,91 @@ the following modifications have been made:
  	BG(syslog_device)=NULL;
  
  	return SUCCESS;
---- php.orig/main/fopen_wrappers.c
-+++ php/main/fopen_wrappers.c
-@@ -166,7 +166,7 @@ PHPAPI int php_check_specific_open_based
- 
- 	/* normalize and expand path */
- 	if (expand_filepath(path, resolved_name TSRMLS_CC) == NULL) {
--		return -1;
-+		return -2;
- 	}
- 
- 	path_len = strlen(resolved_name);
-@@ -238,6 +238,12 @@ PHPAPI int php_check_specific_open_based
- 			}
- 		}
- 
-+		if (resolved_name_len == resolved_basedir_len - 1) {
-+			if (resolved_basedir[resolved_basedir_len - 1] == PHP_DIR_SEPARATOR) {
-+				resolved_basedir_len--;
-+			}
-+		}
-+
- 		/* Check the path */
- #if defined(PHP_WIN32) || defined(NETWARE)
- 		if (strncasecmp(resolved_basedir, resolved_name, resolved_basedir_len) == 0) {
-@@ -261,7 +267,7 @@ PHPAPI int php_check_specific_open_based
- 		}
- 	} else {
- 		/* Unable to resolve the real path, return -1 */
--		return -1;
-+		return -3;
- 	}
- }
- /* }}} */
-@@ -280,22 +286,44 @@ PHPAPI int php_check_open_basedir_ex(con
- 		char *pathbuf;
- 		char *ptr;
- 		char *end;
-+		char path_copy[MAXPATHLEN];
-+		int path_len;
-+
-+		/* Special case path ends with a trailing slash */
-+		path_len = strlen(path);
-+		if (path_len >= MAXPATHLEN) {
-+			errno = EPERM; /* we deny permission to open it */
-+			return -1;
-+		}
-+		if (path_len > 0 && path[path_len-1] == PHP_DIR_SEPARATOR) {
-+			memcpy(path_copy, path, path_len+1);
-+			while (path_len > 1 && path_copy[path_len-1] == PHP_DIR_SEPARATOR) path_len--;
-+			path_copy[path_len] = '\0';
-+			path = (const char *)&path_copy;
-+		}
- 
- 		pathbuf = estrdup(PG(open_basedir));
- 
- 		ptr = pathbuf;
- 
- 		while (ptr && *ptr) {
-+			int res;
- 			end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
- 			if (end != NULL) {
- 				*end = '\0';
- 				end++;
- 			}
+Index: pkg-php/main/fopen_wrappers.c
+===================================================================
+--- pkg-php.orig/main/fopen_wrappers.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/main/fopen_wrappers.c	2009-08-25 15:13:09.000000000 +0200
+@@ -85,15 +85,10 @@
+ PHPAPI ZEND_INI_MH(OnUpdateBaseDir)
+ {
+ 	char **p, *pathbuf, *ptr, *end;
+-#ifndef ZTS
+-	char *base = (char *) mh_arg2;
+-#else
+-	char *base = (char *) ts_resource(*((int *) mh_arg2));
+-#endif
  
--			if (php_check_specific_open_basedir(ptr, path TSRMLS_CC) == 0) {
-+			res = php_check_specific_open_basedir(ptr, path TSRMLS_CC);
-+			if (res == 0) {
- 				efree(pathbuf);
- 				return 0;
- 			}
-+			if (res == -2) {
-+				efree(pathbuf);
-+				errno = EPERM;
-+				return -1;
-+			}
+-	p = (char **) (base + (size_t) mh_arg1);
++        p = &PG(open_basedir);
  
- 			ptr = end;
- 		}
---- php.orig/main/main.c
-+++ php/main/main.c
-@@ -89,6 +89,9 @@
+-	if (stage == PHP_INI_STAGE_STARTUP || stage == PHP_INI_STAGE_SHUTDOWN) {
++	if (stage == PHP_INI_STAGE_STARTUP || stage == PHP_INI_STAGE_SHUTDOWN || stage == PHP_INI_STAGE_ACTIVATE || stage == PHP_INI_STAGE_DEACTIVATE) {
+ 		/* We're in a PHP_INI_SYSTEM context, no restrictions */
+ 		*p = new_value;
+ 		return SUCCESS;
+Index: pkg-php/main/main.c
+===================================================================
+--- pkg-php.orig/main/main.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/main/main.c	2009-08-25 15:13:09.000000000 +0200
+@@ -90,6 +90,9 @@
  
  #include "SAPI.h"
  #include "rfc1867.h"
 +#if SUHOSIN_PATCH
 +#include "suhosin_globals.h"
 +#endif
- /* }}} */
  
- PHPAPI int (*php_register_internal_extensions_func)(TSRMLS_D) = php_register_internal_extensions;
-@@ -1439,7 +1442,7 @@ void php_request_shutdown_for_exec(void 
+ #if HAVE_SYS_MMAN_H
+ # include <sys/mman.h>
+@@ -468,7 +471,7 @@
+ 	STD_PHP_INI_ENTRY("extension_dir",			PHP_EXTENSION_DIR,		PHP_INI_SYSTEM,		OnUpdateStringUnempty,	extension_dir,			php_core_globals,	core_globals)
+ 	STD_PHP_INI_ENTRY("include_path",			PHP_INCLUDE_PATH,		PHP_INI_ALL,		OnUpdateStringUnempty,	include_path,			php_core_globals,	core_globals)
+ 	PHP_INI_ENTRY("max_execution_time",			"30",		PHP_INI_ALL,			OnUpdateTimeout)
+-	STD_PHP_INI_ENTRY("open_basedir",			NULL,		PHP_INI_ALL,		OnUpdateBaseDir,			open_basedir,			php_core_globals,	core_globals)
++	PHP_INI_ENTRY("open_basedir",			NULL,		PHP_INI_ALL,		OnUpdateBaseDir)
+ 	STD_PHP_INI_ENTRY("safe_mode_exec_dir",		PHP_SAFE_MODE_EXEC_DIR,	PHP_INI_SYSTEM,		OnUpdateString,			safe_mode_exec_dir,		php_core_globals,	core_globals)
  
- 	/* used to close fd's in the 3..255 range here, but it's problematic
- 	 */
--	shutdown_memory_manager(1, 1 TSRMLS_CC);
-+	shutdown_memory_manager(1, 1 TSRMLS_CC);	
+ 	STD_PHP_INI_BOOLEAN("file_uploads",			"1",		PHP_INI_SYSTEM,		OnUpdateBool,			file_uploads,			php_core_globals,	core_globals)
+@@ -1721,6 +1724,10 @@
  }
- /* }}} */
- 
-@@ -1480,6 +1483,9 @@ void php_request_shutdown_for_hook(void 
- 
- 	zend_try {
- 		shutdown_memory_manager(CG(unclean_shutdown), 0 TSRMLS_CC);
-+#if SUHOSIN_PATCH
-+		suhosin_clear_mm_canaries(TSRMLS_C);
-+#endif
- 	} zend_end_try();
+ #endif
  
- 	zend_try {
-@@ -1579,6 +1585,9 @@ void php_request_shutdown(void *dummy)
- 	/* 11. Free Willy (here be crashes) */
- 	zend_try {
- 		shutdown_memory_manager(CG(unclean_shutdown) || !report_memleaks, 0 TSRMLS_CC);
 +#if SUHOSIN_PATCH
-+		suhosin_clear_mm_canaries(TSRMLS_C);
++PHPAPI void suhosin_startup();
 +#endif
- 	} zend_end_try();
- 
- 	/* 12. Reset max_execution_time */
-@@ -1746,6 +1755,9 @@ int php_module_startup(sapi_module_struc
- #ifdef ZTS
++
+ /* {{{ php_module_startup
+  */
+ int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_modules, uint num_additional_modules)
+@@ -1765,6 +1772,10 @@
  	tsrm_ls = ts_resource(0);
  #endif
+ 
 +#if SUHOSIN_PATCH
-+	suhosin_startup();
++        suhosin_startup();
 +#endif
- 
++
  	module_shutdown = 0;
  	module_startup = 1;
-@@ -1863,6 +1875,10 @@ int php_module_startup(sapi_module_struc
- 	REGISTER_MAIN_STRINGL_CONSTANT("PHP_CONFIG_FILE_PATH", PHP_CONFIG_FILE_PATH, strlen(PHP_CONFIG_FILE_PATH), CONST_PERSISTENT | CONST_CS);
+ 	sapi_initialize_empty_request(TSRMLS_C);
+@@ -1884,7 +1895,11 @@
  	REGISTER_MAIN_STRINGL_CONSTANT("PHP_CONFIG_FILE_SCAN_DIR", PHP_CONFIG_FILE_SCAN_DIR, sizeof(PHP_CONFIG_FILE_SCAN_DIR)-1, CONST_PERSISTENT | CONST_CS);
  	REGISTER_MAIN_STRINGL_CONSTANT("PHP_SHLIB_SUFFIX", PHP_SHLIB_SUFFIX, sizeof(PHP_SHLIB_SUFFIX)-1, CONST_PERSISTENT | CONST_CS);
-+#if SUHOSIN_PATCH
-+	REGISTER_MAIN_LONG_CONSTANT("SUHOSIN_PATCH", 1, CONST_PERSISTENT | CONST_CS);
-+	REGISTER_MAIN_STRINGL_CONSTANT("SUHOSIN_PATCH_VERSION", SUHOSIN_PATCH_VERSION, sizeof(SUHOSIN_PATCH_VERSION)-1, CONST_PERSISTENT | CONST_CS);
-+#endif
  	REGISTER_MAIN_STRINGL_CONSTANT("PHP_EOL", PHP_EOL, sizeof(PHP_EOL)-1, CONST_PERSISTENT | CONST_CS);
- 	REGISTER_MAIN_LONG_CONSTANT("PHP_MAXPATHLEN", MAXPATHLEN, CONST_PERSISTENT | CONST_CS);
- 	REGISTER_MAIN_LONG_CONSTANT("PHP_INT_MAX", LONG_MAX, CONST_PERSISTENT | CONST_CS);
-@@ -1990,7 +2006,9 @@ int php_module_startup(sapi_module_struc
- 	module_startup = 0;
- 
- 	shutdown_memory_manager(1, 0 TSRMLS_CC);
--
-+#if SUHOSIN_PATCH
-+	suhosin_clear_mm_canaries(TSRMLS_C);
-+#endif
- 	/* we're done */
- 	return SUCCESS;
- }
-@@ -2049,6 +2067,9 @@ void php_module_shutdown(TSRMLS_D)
- #ifndef ZTS
- 	zend_ini_shutdown(TSRMLS_C);
- 	shutdown_memory_manager(CG(unclean_shutdown), 1 TSRMLS_CC);
+-	REGISTER_MAIN_LONG_CONSTANT("PHP_MAXPATHLEN", MAXPATHLEN, CONST_PERSISTENT | CONST_CS);
 +#if SUHOSIN_PATCH
-+	suhosin_clear_mm_canaries(TSRMLS_C);
-+#endif
- 	core_globals_dtor(&core_globals TSRMLS_CC);
- 	gc_globals_dtor(TSRMLS_C);
- #else
---- php.orig/main/php.h
-+++ php/main/php.h
-@@ -40,6 +40,13 @@
- #undef sprintf
- #define sprintf php_sprintf
++        REGISTER_MAIN_LONG_CONSTANT("SUHOSIN_PATCH", 1, CONST_PERSISTENT | CONST_CS);
++        REGISTER_MAIN_STRINGL_CONSTANT("SUHOSIN_PATCH_VERSION", SUHOSIN_PATCH_VERSION, sizeof(SUHOSIN_PATCH_VERSION)-1, CONST_PERSISTENT | CONST_CS);
++#endif	
++        REGISTER_MAIN_LONG_CONSTANT("PHP_MAXPATHLEN", MAXPATHLEN, CONST_PERSISTENT | CONST_CS);
+ 	REGISTER_MAIN_LONG_CONSTANT("PHP_INT_MAX", LONG_MAX, CONST_PERSISTENT | CONST_CS);
+ 	REGISTER_MAIN_LONG_CONSTANT("PHP_INT_SIZE", sizeof(long), CONST_PERSISTENT | CONST_CS);
  
-+#if SUHOSIN_PATCH
-+#if HAVE_REALPATH
-+#undef realpath
-+#define realpath php_realpath
-+#endif
-+#endif
-+
- /* PHP's DEBUG value must match Zend's ZEND_DEBUG value */
- #undef PHP_DEBUG
- #define PHP_DEBUG ZEND_DEBUG
-@@ -450,6 +457,10 @@ END_EXTERN_C()
+Index: pkg-php/main/php.h
+===================================================================
+--- pkg-php.orig/main/php.h	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/main/php.h	2009-08-25 15:13:09.000000000 +0200
+@@ -450,6 +450,10 @@
  #endif
  #endif /* !XtOffsetOf */
  
@@ -1725,9 +4378,11 @@ the following modifications have been made:
  #endif
  
  /*
---- php.orig/main/php_config.h.in
-+++ php/main/php_config.h.in
-@@ -809,6 +809,9 @@
+Index: pkg-php/main/php_config.h.in
+===================================================================
+--- pkg-php.orig/main/php_config.h.in	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/main/php_config.h.in	2009-08-25 15:13:09.000000000 +0200
+@@ -836,6 +836,9 @@
  /* Define if the target system has /dev/urandom device */
  #undef HAVE_DEV_URANDOM
  
@@ -1737,9 +4392,11 @@ the following modifications have been made:
  /* Whether you have AOLserver */
  #undef HAVE_AOLSERVER
  
---- php.orig/main/php_logos.c
-+++ php/main/php_logos.c
-@@ -50,6 +50,10 @@ PHPAPI int php_unregister_info_logo(char
+Index: pkg-php/main/php_logos.c
+===================================================================
+--- pkg-php.orig/main/php_logos.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/main/php_logos.c	2009-08-25 15:13:09.000000000 +0200
+@@ -50,6 +50,10 @@
  	return zend_hash_del(&phpinfo_logo_hash, logo_string, strlen(logo_string));
  }
  
@@ -1750,52 +4407,59 @@ the following modifications have been made:
  int php_init_info_logos(void)
  {
  	if(zend_hash_init(&phpinfo_logo_hash, 0, NULL, NULL, 1)==FAILURE) 
-@@ -58,6 +62,9 @@ int php_init_info_logos(void)
+@@ -58,7 +62,9 @@
  	php_register_info_logo(PHP_LOGO_GUID    , "image/gif", php_logo    , sizeof(php_logo));
  	php_register_info_logo(PHP_EGG_LOGO_GUID, "image/gif", php_egg_logo, sizeof(php_egg_logo));
  	php_register_info_logo(ZEND_LOGO_GUID   , "image/gif", zend_logo   , sizeof(zend_logo));
+-
 +#if SUHOSIN_PATCH
-+	php_register_info_logo(SUHOSIN_LOGO_GUID, "image/jpeg", suhosin_logo, sizeof(suhosin_logo));
-+#endif	
- 
++	php_register_info_logo(SUHOSIN_LOGO_GUID, "image/jpeg", suhosin_logo   , sizeof(suhosin_logo));
++#endif
  	return SUCCESS;
  }
---- php.orig/main/snprintf.c
-+++ php/main/snprintf.c
-@@ -1091,7 +1091,11 @@ static int format_converter(register buf
+ 
+Index: pkg-php/main/snprintf.c
+===================================================================
+--- pkg-php.orig/main/snprintf.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/main/snprintf.c	2009-08-25 15:13:09.000000000 +0200
+@@ -1091,7 +1091,11 @@
  
  
  				case 'n':
 +#if SUHOSIN_PATCH
-+					zend_suhosin_log(S_MISC, "'n' specifier within format string");
++                                        zend_suhosin_log(S_MISC, "'n' specifier within format string");
 +#else
  					*(va_arg(ap, int *)) = cc;
 +#endif
  					goto skip_output;
  
  					/*
---- php.orig/main/spprintf.c
-+++ php/main/spprintf.c
-@@ -698,7 +698,11 @@ static void xbuf_format_converter(smart_
+Index: pkg-php/main/spprintf.c
+===================================================================
+--- pkg-php.orig/main/spprintf.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/main/spprintf.c	2009-08-25 15:13:09.000000000 +0200
+@@ -698,7 +698,11 @@
  
  
  				case 'n':
 +#if SUHOSIN_PATCH
-+					zend_suhosin_log(S_MISC, "'n' specifier within format string");
++                                        zend_suhosin_log(S_MISC, "'n' specifier within format string");
 +#else
  					*(va_arg(ap, int *)) = xbuf->len;
 +#endif
  					goto skip_output;
  
  					/*
---- /dev/null
-+++ php/main/suhosin_globals.h
+Index: pkg-php/main/suhosin_globals.h
+===================================================================
+--- /dev/null	1970-01-01 00:00:00.000000000 +0000
++++ pkg-php/main/suhosin_globals.h	2009-08-25 15:13:09.000000000 +0200
 @@ -0,0 +1,61 @@
 +/*
 +   +----------------------------------------------------------------------+
 +   | Suhosin-Patch for PHP                                                |
 +   +----------------------------------------------------------------------+
-+   | Copyright (c) 2004-2006 Stefan Esser                                 |
++   | Copyright (c) 2004-2009 Stefan Esser                                 |
 +   +----------------------------------------------------------------------+
 +   | This source file is subject to version 2.02 of the PHP license,      |
 +   | that is bundled with this package in the file LICENSE, and is        |
@@ -1805,7 +4469,7 @@ the following modifications have been made:
 +   | obtain it through the world-wide-web, please send a note to          |
 +   | license at php.net so we can mail you a copy immediately.               |
 +   +----------------------------------------------------------------------+
-+   | Author: Stefan Esser <sesser at hardened-php.net>                       |
++   | Author: Stefan Esser <stefan.esser at sektioneins.de>                   |
 +   +----------------------------------------------------------------------+
 + */
 +
@@ -1852,8 +4516,10 @@ the following modifications have been made:
 + * c-basic-offset: 4
 + * End:
 + */
---- /dev/null
-+++ php/main/suhosin_logo.h
+Index: pkg-php/main/suhosin_logo.h
+===================================================================
+--- /dev/null	1970-01-01 00:00:00.000000000 +0000
++++ pkg-php/main/suhosin_logo.h	2009-08-25 15:13:09.000000000 +0200
 @@ -0,0 +1,178 @@
 +static unsigned char suhosin_logo[] =
 +	"\xff\xd8\xff\xe0\x00\x10\x4a\x46\x49\x46\x00\x01\x01\x01\x00\x48"
@@ -2033,9 +4699,11 @@ the following modifications have been made:
 +	"\x21\xb6\x99\x69\xbc\x25\xb6\xdb\x6d\x18\xc2\x10\xda\x12\x94\xa1"
 +	"\x38\xc2\x53\x8c\x63\x18\xc7\x9d\xbe\x7f\xff\xd9"
 +	;
---- /dev/null
-+++ php/main/suhosin_patch.c
-@@ -0,0 +1,380 @@
+Index: pkg-php/main/suhosin_patch.c
+===================================================================
+--- /dev/null	1970-01-01 00:00:00.000000000 +0000
++++ pkg-php/main/suhosin_patch.c	2009-08-25 15:13:09.000000000 +0200
+@@ -0,0 +1,455 @@
 +/*
 +   +----------------------------------------------------------------------+
 +   | Suhosin Patch for PHP                                                |
@@ -2097,6 +4765,20 @@ the following modifications have been made:
 +struct _suhosin_patch_globals suhosin_patch_globals;
 +#endif
 +
++/* hack that needs to be fixed */
++#ifndef PAGE_SIZE
++#define PAGE_SIZE 4096
++#endif
++
++#ifdef ZEND_WIN32
++__declspec(align(PAGE_SIZE))
++#endif
++char suhosin_config[PAGE_SIZE] 
++#if defined(__GNUC__) 
++    __attribute__ ((aligned(PAGE_SIZE)))
++#endif
++;
++
 +static void php_security_log(int loglevel, char *fmt, ...);
 +
 +static void suhosin_patch_globals_ctor(suhosin_patch_globals_struct *suhosin_patch_globals TSRMLS_DC)
@@ -2104,6 +4786,64 @@ the following modifications have been made:
 +	memset(suhosin_patch_globals, 0, sizeof(*suhosin_patch_globals));
 +}
 +
++static void suhosin_read_configuration_from_environment()
++{
++        char *tmp;
++        
++        /* check if canary protection should be activated or not */
++        tmp = getenv("SUHOSIN_MM_USE_CANARY_PROTECTION");
++        /* default to activated */
++        SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) = 1;
++        if (tmp) {
++                int flag = zend_atoi(tmp, 0);
++                SUHOSIN_CONFIG(SUHOSIN_MM_USE_CANARY_PROTECTION) = flag;
++        }
++        
++        /* check if free memory should be overwritten with 0xFF or not */
++        tmp = getenv("SUHOSIN_MM_DESTROY_FREE_MEMORY");
++        /* default to deactivated */
++        SUHOSIN_CONFIG(SUHOSIN_MM_DESTROY_FREE_MEMORY) = 0;
++        if (tmp) {
++                int flag = zend_atoi(tmp, 0);
++                SUHOSIN_CONFIG(SUHOSIN_MM_DESTROY_FREE_MEMORY) = flag;
++        }
++        
++        /* check if canary violations should be ignored */
++        tmp = getenv("SUHOSIN_MM_IGNORE_CANARY_VIOLATION");
++        /* default to NOT ignore */
++        SUHOSIN_CONFIG(SUHOSIN_MM_IGNORE_CANARY_VIOLATION) = 0;
++        if (tmp) {
++                int flag = zend_atoi(tmp, 0);
++                SUHOSIN_CONFIG(SUHOSIN_MM_IGNORE_CANARY_VIOLATION) = flag;
++        }
++
++        /* check if invalid hashtable destructors should be ignored */
++        tmp = getenv("SUHOSIN_HT_IGNORE_INVALID_DESTRUCTOR");
++        /* default to NOT ignore */
++        SUHOSIN_CONFIG(SUHOSIN_HT_IGNORE_INVALID_DESTRUCTOR) = 0;
++        if (tmp) {
++                int flag = zend_atoi(tmp, 0);
++                SUHOSIN_CONFIG(SUHOSIN_HT_IGNORE_INVALID_DESTRUCTOR) = flag;
++        }
++
++        /* check if invalid linkedlist destructors should be ignored */
++        tmp = getenv("SUHOSIN_LL_IGNORE_INVALID_DESTRUCTOR");
++        /* default to NOT ignore */
++        SUHOSIN_CONFIG(SUHOSIN_LL_IGNORE_INVALID_DESTRUCTOR) = 0;
++        if (tmp) {
++                int flag = zend_atoi(tmp, 0);
++                SUHOSIN_CONFIG(SUHOSIN_LL_IGNORE_INVALID_DESTRUCTOR) = flag;
++        }
++
++}
++
++static void suhosin_write_protect_configuration()
++{
++#if defined(__GNUC__)
++        mprotect(suhosin_config, PAGE_SIZE, PROT_READ);
++#endif
++}
++
 +PHPAPI void suhosin_startup()
 +{
 +#ifdef ZTS
@@ -2112,6 +4852,9 @@ the following modifications have been made:
 +	suhosin_patch_globals_ctor(&suhosin_patch_globals TSRMLS_CC);
 +#endif
 +	zend_suhosin_log = php_security_log;
++	
++        suhosin_read_configuration_from_environment();
++        suhosin_write_protect_configuration();
 +}
 +
 +/*PHPAPI void suhosin_clear_mm_canaries(TSRMLS_D)
@@ -2416,14 +5159,16 @@ the following modifications have been made:
 + * vim600: sw=4 ts=4 fdm=marker
 + * vim<600: sw=4 ts=4
 + */
---- /dev/null
-+++ php/main/suhosin_patch.h
-@@ -0,0 +1,40 @@
+Index: pkg-php/main/suhosin_patch.h
+===================================================================
+--- /dev/null	1970-01-01 00:00:00.000000000 +0000
++++ pkg-php/main/suhosin_patch.h	2009-08-25 15:13:09.000000000 +0200
+@@ -0,0 +1,61 @@
 +/*
 +   +----------------------------------------------------------------------+
 +   | Suhosin Patch for PHP                                                |
 +   +----------------------------------------------------------------------+
-+   | Copyright (c) 2004-2006 Stefan Esser                                 |
++   | Copyright (c) 2004-2009 Stefan Esser                                 |
 +   +----------------------------------------------------------------------+
 +   | This source file is subject to version 2.02 of the PHP license,      |
 +   | that is bundled with this package in the file LICENSE, and is        |
@@ -2433,7 +5178,7 @@ the following modifications have been made:
 +   | obtain it through the world-wide-web, please send a note to          |
 +   | license at php.net so we can mail you a copy immediately.               |
 +   +----------------------------------------------------------------------+
-+   | Author: Stefan Esser <sesser at hardened-php.net>                       |
++   | Author: Stefan Esser <stefan.esser at sektioneins.de>                   |
 +   +----------------------------------------------------------------------+
 + */
 +
@@ -2444,11 +5189,32 @@ the following modifications have been made:
 +
 +#include "zend.h"
 +
-+PHPAPI void suhosin_startup();
-+#define SUHOSIN_PATCH_VERSION "0.9.7"
++#define SUHOSIN_PATCH_VERSION "0.9.8"
 +
 +#define SUHOSIN_LOGO_GUID "SUHO8567F54-D428-14d2-A769-00DA302A5F18"
 +
++#define SUHOSIN_CONFIG(idx) suhosin_config[(idx)]
++#define SUHOSIN_MM_USE_CANARY_PROTECTION        0
++#define SUHOSIN_MM_DESTROY_FREE_MEMORY          1
++#define SUHOSIN_MM_IGNORE_CANARY_VIOLATION      2
++#define SUHOSIN_HT_IGNORE_INVALID_DESTRUCTOR    3
++#define SUHOSIN_LL_IGNORE_INVALID_DESTRUCTOR    4
++
++#include <sys/types.h>
++#include <sys/stat.h>
++#include <sys/mman.h>
++
++#if defined(DARWIN)
++#include <mach/vm_param.h>
++#endif
++
++/* hack that needs to be fixed */
++#ifndef PAGE_SIZE
++#define PAGE_SIZE 4096
++#endif
++
++extern char suhosin_config[PAGE_SIZE];
++
 +#endif
 +
 +#endif /* SUHOSIN_PATCH_H */
@@ -2459,8 +5225,10 @@ the following modifications have been made:
 + * c-basic-offset: 4
 + * End:
 + */
---- /dev/null
-+++ php/main/suhosin_patch.m4
+Index: pkg-php/main/suhosin_patch.m4
+===================================================================
+--- /dev/null	1970-01-01 00:00:00.000000000 +0000
++++ pkg-php/main/suhosin_patch.m4	2009-08-25 15:13:09.000000000 +0200
 @@ -0,0 +1,8 @@
 +dnl
 +dnl $Id: suhosin_patch.m4,v 1.1 2004/11/14 13:24:24 ionic Exp $
@@ -2470,9 +5238,11 @@ the following modifications have been made:
 +
 +AC_DEFINE(SUHOSIN_PATCH, 1, [Suhosin Patch])
 +
---- php.orig/sapi/apache/mod_php5.c
-+++ php/sapi/apache/mod_php5.c
-@@ -967,7 +967,11 @@ static void php_init_handler(server_rec 
+Index: pkg-php/sapi/apache/mod_php5.c
+===================================================================
+--- pkg-php.orig/sapi/apache/mod_php5.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/sapi/apache/mod_php5.c	2009-08-25 15:13:09.000000000 +0200
+@@ -967,7 +967,11 @@
  	{
  		TSRMLS_FETCH();
  		if (PG(expose_php)) {
@@ -2480,13 +5250,15 @@ the following modifications have been made:
 +			ap_add_version_component("PHP/" PHP_VERSION " with Suhosin-Patch");
 +#else
  			ap_add_version_component("PHP/" PHP_VERSION);
-+#endif			
++#endif
  		}
  	}
  #endif
---- php.orig/sapi/apache2filter/sapi_apache2.c
-+++ php/sapi/apache2filter/sapi_apache2.c
-@@ -581,7 +581,11 @@ static void php_apache_add_version(apr_p
+Index: pkg-php/sapi/apache2filter/sapi_apache2.c
+===================================================================
+--- pkg-php.orig/sapi/apache2filter/sapi_apache2.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/sapi/apache2filter/sapi_apache2.c	2009-08-25 15:13:09.000000000 +0200
+@@ -581,7 +581,11 @@
  {
  	TSRMLS_FETCH();
  	if (PG(expose_php)) {
@@ -2498,9 +5270,11 @@ the following modifications have been made:
  	}
  }
  
---- php.orig/sapi/apache2handler/sapi_apache2.c
-+++ php/sapi/apache2handler/sapi_apache2.c
-@@ -386,7 +386,11 @@ static void php_apache_add_version(apr_p
+Index: pkg-php/sapi/apache2handler/sapi_apache2.c
+===================================================================
+--- pkg-php.orig/sapi/apache2handler/sapi_apache2.c	2009-08-25 15:13:09.000000000 +0200
++++ pkg-php/sapi/apache2handler/sapi_apache2.c	2009-08-25 15:13:09.000000000 +0200
+@@ -386,7 +386,11 @@
  {
  	TSRMLS_FETCH();
  	if (PG(expose_php)) {
@@ -2512,65 +5286,135 @@ the following modifications have been made:
  	}
  }
  
---- php.orig/sapi/cgi/cgi_main.c
-+++ php/sapi/cgi/cgi_main.c
-@@ -1859,10 +1859,18 @@ consult the installation file that came 
+Index: pkg-php/sapi/apache_hooks/mod_php5.c
+===================================================================
+--- pkg-php.orig/sapi/apache_hooks/mod_php5.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/sapi/apache_hooks/mod_php5.c	2009-08-25 15:13:09.000000000 +0200
+@@ -1256,7 +1256,11 @@
+ 	{
+ 		TSRMLS_FETCH();
+ 		if (PG(expose_php)) {
++#if SUHOSIN_PATCH
++			ap_add_version_component("PHP/" PHP_VERSION " with Suhosin-Patch");
++#else
+ 			ap_add_version_component("PHP/" PHP_VERSION);
++#endif
+ 		}
+ 	}
+ #endif
+Index: pkg-php/sapi/cgi/cgi_main.c
+===================================================================
+--- pkg-php.orig/sapi/cgi/cgi_main.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/sapi/cgi/cgi_main.c	2009-08-25 15:13:09.000000000 +0200
+@@ -1907,11 +1907,19 @@
  								SG(headers_sent) = 1;
  								SG(request_info).no_headers = 1;
  							}
 +#if SUHOSIN_PATCH
- #if ZEND_DEBUG
--							php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
-+						php_printf("PHP %s with Suhosin-Patch %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, SUHOSIN_PATCH_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
- #else
--							php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
-+						php_printf("PHP %s with Suhosin-Patch %s (%s) (built: %s %s)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, SUHOSIN_PATCH_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
-+#endif
-+#else
 +#if ZEND_DEBUG
-+						php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
++							php_printf("PHP %s with Suhosin-Patch (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
 +#else
-+						php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
++							php_printf("PHP %s with Suhosin-Patch (%s) (built: %s %s)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
 +#endif
++#else
+ #if ZEND_DEBUG
+ 							php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
+ #else
+ 							php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
  #endif
++#endif
  							php_request_shutdown((void *) 0);
  							exit_status = 0;
---- php.orig/sapi/cli/php_cli.c
-+++ php/sapi/cli/php_cli.c
-@@ -798,8 +798,14 @@ int main(int argc, char *argv[])
+ 							goto out;
+Index: pkg-php/sapi/cli/php_cli.c
+===================================================================
+--- pkg-php.orig/sapi/cli/php_cli.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/sapi/cli/php_cli.c	2009-08-25 15:13:09.000000000 +0200
+@@ -827,7 +827,11 @@
  				}
  
  				request_started = 1;
 -				php_printf("PHP %s (%s) (built: %s %s) %s\nCopyright (c) 1997-2009 The PHP Group\n%s",
--					PHP_VERSION, sapi_module.name, __DATE__, __TIME__,
++				php_printf("PHP %s "
 +#if SUHOSIN_PATCH
-+				php_printf("PHP %s with Suhosin-Patch %s (%s) (built: %s %s) %s\nCopyright (c) 1997-2009 The PHP Group\n%s", 
-+					PHP_VERSION, SUHOSIN_PATCH_VERSION,
-+#else
-+			    php_printf("PHP %s (%s) (built: %s %s) %s\nCopyright (c) 1997-2009 The PHP Group\n%s",
-+					PHP_VERSION,
++                                        "with Suhosin-Patch "
 +#endif
-+					sapi_module.name, __DATE__, __TIME__,
++				        "(%s) (built: %s %s) %s\nCopyright (c) 1997-2009 The PHP Group\n%s",
+ 					PHP_VERSION, sapi_module.name, __DATE__, __TIME__,
  #if ZEND_DEBUG && defined(HAVE_GCOV)
  					"(DEBUG GCOV)",
- #elif ZEND_DEBUG
---- php.orig/win32/build/config.w32
-+++ php/win32/build/config.w32
-@@ -312,7 +312,7 @@ ADD_SOURCES("Zend", "zend_language_parse
+Index: pkg-php/sapi/litespeed/lsapi_main.c
+===================================================================
+--- pkg-php.orig/sapi/litespeed/lsapi_main.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/sapi/litespeed/lsapi_main.c	2009-08-25 15:13:09.000000000 +0200
+@@ -545,11 +545,19 @@
+ 				break;
+ 			case 'v':
+ 				if (php_request_startup(TSRMLS_C) != FAILURE) {
++#if SUHOSIN_PATCH
++#if ZEND_DEBUG
++					php_printf("PHP %s with Suhosin-Patch (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2004 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
++#else
++					php_printf("PHP %s with Suhosin-Patch (%s) (built: %s %s)\nCopyright (c) 1997-2004 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
++#endif
++#else
+ #if ZEND_DEBUG
+ 					php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2004 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
+ #else
+ 					php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2004 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
+ #endif
++#endif
+ #ifdef PHP_OUTPUT_NEWAPI
+                     php_output_end_all(TSRMLS_C);
+ #else
+Index: pkg-php/sapi/milter/php_milter.c
+===================================================================
+--- pkg-php.orig/sapi/milter/php_milter.c	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/sapi/milter/php_milter.c	2009-08-25 15:13:09.000000000 +0200
+@@ -1102,7 +1102,11 @@
+ 				}
+ 				SG(headers_sent) = 1;
+ 				SG(request_info).no_headers = 1;
++#if SUHOSIN_PATCH
++				php_printf("PHP %s with Suhosin-Patch (%s) (built: %s %s)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
++#else
+ 				php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
++#endif
+ 				php_end_ob_buffers(1 TSRMLS_CC);
+ 				exit(1);
+ 				break;
+Index: pkg-php/win32/build/config.w32
+===================================================================
+--- pkg-php.orig/win32/build/config.w32	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/win32/build/config.w32	2009-08-25 15:13:09.000000000 +0200
+@@ -322,7 +322,7 @@
  	zend_stream.c zend_iterators.c zend_interfaces.c zend_objects.c \
  	zend_object_handlers.c zend_objects_API.c \
  	zend_default_classes.c zend_execute.c zend_strtod.c zend_gc.c zend_closures.c \
 -	zend_float.c");
-+	zend_float.c zend_canary.c");
++	zend_float.c zend_canary.c zend_alloc_canary.c");
  
- ADD_SOURCES("main", "main.c snprintf.c spprintf.c safe_mode.c getopt.c fopen_wrappers.c \
- 	php_scandir.c php_ini.c SAPI.c rfc1867.c php_content_types.c strlcpy.c \
-@@ -364,6 +364,8 @@ if (PHP_ZEND_MULTIBYTE == "yes") {
+ if (VCVERS == 1200) {
+ 	AC_DEFINE('ZEND_DVAL_TO_LVAL_CAST_OK', 1);
+@@ -377,6 +377,7 @@
+  
  AC_DEFINE('HAVE_USLEEP', 1);
  AC_DEFINE('HAVE_STRCOLL', 1);
- 
 +AC_DEFINE('SUHOSIN_PATCH', 1);
-+
+ 
  /* For snapshot builders, where can we find the additional
   * files that make up the snapshot template? */
- ARG_WITH("snapshot-template", "Path to snapshot builder template dir", "no");
+Index: pkg-php/win32/build/config.w32.h.in
+===================================================================
+--- pkg-php.orig/win32/build/config.w32.h.in	2009-08-25 15:11:46.000000000 +0200
++++ pkg-php/win32/build/config.w32.h.in	2009-08-25 15:13:09.000000000 +0200
+@@ -149,6 +149,9 @@
+ /* Win32 supports strcoll */
+ #define HAVE_STRCOLL 1
+ 
++/* Suhosin Patch support */
++#define SUHOSIN_PATCH 1
++
+ /* Win32 supports socketpair by the emulation in win32/sockets.c */
+ #define HAVE_SOCKETPAIR 1
+ #define HAVE_SOCKLEN_T 1
-- 
1.5.6.5





More information about the Pkg-php-commits mailing list