From: [email protected]
Operating system:
PHP version: 5.3CVS-2009-06-13 (CVS)
PHP Bug Type: SPL related
Bug description: spl_autoload_register only registers first closure, then
leaks the others
Description:
------------
The test script below does not leak, but if $a were to successfully load
class "foo" (i.e. move the eval() in $b() into $a) it leaks memory in
zend_execute.c on line 723, which is the portion called when the closure is
assigned to $b.
This is caused by 2 separate bugs.
1) php_spl.c line 501 increases the refcount of the closure zval
2) php_spl.c line 511 checks to see if the function name has already been
registered, but treats all closures as if they were "closure::__invoke"
instead of using some unique aspect of each closure. This then jumps to
the end of spl_autoload_register without decreasing the refcount, and boom,
we have a memory leak.
I don't know anything about the closures implementation, so someone who
knows a better way to get a unique function name for a closure needs to fix
this, both the refcount and the only-one-closure-allowed issues.
Also, the memory leak is triggered by an important feature of Pyrus, and
yes, we are registering multiple closures as autoload callbacks to
implement a dynamic plugin system. It works great except for these two
bugs.
Reproduce code:
---------------
<?php
$a = function ($class) {
echo "a called\n";
};
$b = function ($class) {
eval('class ' . $class . '{}');
echo "b called\n";
};
spl_autoload_register($a);
spl_autoload_register($b);
$c = new foo;
Expected result:
----------------
a called
b called
Actual result:
--------------
a called
Fatal error: Class 'foo' not found in /home/user/workspace/php5/test.php
on line 11
Call Stack:
0.0005 345172 1. {main}() /home/user/workspace/php5/test.php:0
--
Edit bug report at http://bugs.php.net/?id=48541&edit=1
--
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=48541&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=48541&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=48541&r=trysnapshot60
Fixed in CVS:
http://bugs.php.net/fix.php?id=48541&r=fixedcvs
Fixed in CVS and need be documented:
http://bugs.php.net/fix.php?id=48541&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=48541&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=48541&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=48541&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=48541&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=48541&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=48541&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=48541&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=48541&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=48541&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=48541&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=48541&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=48541&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=48541&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=48541&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=48541&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=48541&r=mysqlcfg