Edit report at https://bugs.php.net/bug.php?id=63180&edit=1
ID: 63180 Updated by: dmi...@php.net Reported by: vesselin dot atanasov at gmail dot com Summary: Corruption of hash tables (bugfix attached) Status: Assigned Type: Bug Package: Scripting Engine problem Operating System: Any PHP Version: 5.4Git-2012-09-28 (Git) Assigned To: dmitry Block user comment: N Private report: N New Comment: In general, this is the right observation and the right fix. However, this fix won't allow interned strings usage in EG(function_table), EG(class_table) and EG(zend_constants). This is going to lead to larger memory consumption and slower execution. I think, that might be better to workaround the problem in PCRE extension itself. I know, it's not "sexy" and some other places might need to be fixed as well. :( May be you can think about better solution? Previous Comments: ------------------------------------------------------------------------ [2012-09-28 16:18:49] vesselin dot atanasov at gmail dot com Description: ------------ PHP 5.4 has a bug in the handling if interned (literal) strings. Each time an HTTP request terminates, the interned strings are restored to the snapshot that was taken at the end of the startup sequence, which effectively removes any interned strings that have been added after the snapshot has been made. Usually when a hash item is added, the code in zend_hash.c allocates extra space after the end of the Bucket structure, copies the key there and then points Bucket::arKey to the key copy. However when dealing with interned hash keys the code tries to optimize the algorithm by not allocating extra space after the end of the Bucket structure, but just pointing Bucket::arKey to the passed arKey parameter. if (IS_INTERNED(arKey)) { p = (Bucket *) pemalloc(sizeof(Bucket), ht->persistent); if (!p) { return FAILURE; } p->arKey = arKey; } else { p = (Bucket *) pemalloc(sizeof(Bucket) + nKeyLength, ht->persistent); if (!p) { return FAILURE; } p->arKey = (const char*)(p + 1); memcpy((char*)p->arKey, arKey, nKeyLength); } The problem happens when a persistent hash table gets an interned key as a parameter. What happens is that the table and its bucket are persistent, i.e. they remain even after the current HTTP request has been terminated, but the bucket key is removed from the interned keys table and its memory will be reused by other interned keys upon the next request. This leads to corruption of the array keys in the persistent hash table. One such case is with the PCRE cache. It is initialized in: php_pcre.c:PHP_GINIT_FUNCTION(pcre) by the following code: zend_hash_init(&pcre_globals->pcre_cache, 0, NULL, php_free_pcre_cache, 1); The last parameter (1) means that it is a persistent hash table. However the code in php_pcre.c:pcre_get_compiled_regex_cache(char *regex, int regex_len TSRMLS_DC) just passes the regex parameter to zend_hash_update: zend_hash_update(&PCRE_G(pcre_cache), regex, regex_len+1, (void *)&new_entry, sizeof(pcre_cache_entry), (void**)&pce); Given that in most cases the regex parameter is created from string literals in the compiled code, this means that in most cases we end up with interned, non-persistent keys in the persistent PCRE cache table. So when the next HTTP request comes and we create different interned strings they will overwrite the previous ones in the PCRE cache table. The suggested solution to this bug is to modify the code in zend_hash.c and change it in such a way that copying of keys is skipped only when the key is interned and the hash table is persistent. When either the key is non-interned or the hash table is persistent, the key is copied after the Bucket structure, so that its maximum lifetime will be the same as the lifetime of the hash table. I am attaching a patch which uses the suggested solution. I tested this patch and it solves the problems with PCRE cache corruption that we observed in our PHP code. The patch is for PHP 5.4.5 but it also applies cleanly to the latest git version of PHP 5.4 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=63180&edit=1