[php-src] Issue #13759: Off-by-one error in DateTime object memory usage?
| From: | mikkorantalainen | Date: | Tue, 19 Mar 2024 15:15:38 +0000 |
| Subject: | [php-src] Issue #13759: Off-by-one error in DateTime object memory usage? | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-246618@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/13759
Author: mikkorantalainen
### Description
It appears that creating and deleting DateTime objects leaks about one byte per object without upper
limit. This is typically not important for short lived process but in long running process handling
lots of timestamps this might become a problem. The fact that the leak seems to be 1 byte on average
suggests that this might be some kind of off-by-one issue which has always potential for being a
security bug, too.
The following code:
```php
<?php
# https://bugs.php.net/bug.php?id=47351
# and last comment of https://bugs.php.net/bug.php?id=70098
#
# PHP may leak DateTime objects by design?
define("BLOCK", 100000);
define("MAX", BLOCK*10);
$baseline_usage = memory_get_usage();
$baseline_peak = memory_get_peak_usage(true);
echo sprintf("Counting used memory / peak memory after using memory after %d objects.\n",
MAX);
echo sprintf("Before loop: %d/%d bytes\n", memory_get_usage()-$baseline_usage,
memory_get_peak_usage(true)-$baseline_peak);
echo sprintf(" Loops Usage Peak usage\n");
$d = array();
for ($i=1; $i <= MAX; $i++)
{
if (1) # DateTime leak
$d[] = array(new \DateTime(), &$d);
else # no leak for unique strings
$d[] = array(sha1($i), &$d);
if ($i % BLOCK === 0)
{
echo sprintf("%8d: %8d/%d bytes\n", $i, memory_get_usage()-$baseline_usage,
memory_get_peak_usage(true)-$baseline_peak);
$d = array(); # reset array to keep peak usage sensible
}
}
echo sprintf("After the loop:\n");
$d = array();
echo sprintf("after releasing \$d: %d/%d bytes\n", memory_get_usage()-$baseline_usage,
memory_get_peak_usage(true)-$baseline_peak);
unset($d);
unset($i);
gc_collect_cycles();
echo sprintf("after unset + gc_collect_cycles: %d/%d bytes\n",
memory_get_usage()-$baseline_usage, memory_get_peak_usage(true)-$baseline_peak);
echo sprintf("Avg leak per object: %.2f bytes\n",
(memory_get_usage()-$baseline_usage)/MAX);
```
Resulted in this output:
```
Counting used memory / peak memory after using memory after 1000000 objects.
Before loop: 32/0 bytes
Loops Usage Peak usage
100000: 77401072/77598720 bytes
200000: 79081072/79699968 bytes
300000: 77401072/81797120 bytes
400000: 77961072/81797120 bytes
500000: 78521072/81797120 bytes
600000: 79081072/81797120 bytes
700000: 74041072/81797120 bytes
800000: 74041072/81797120 bytes
900000: 74041072/81797120 bytes
1000000: 74041072/81797120 bytes
After the loop:
after releasing $d: 1042592/81797120 bytes
after unset + gc_collect_cycles: 1042560/81797120 bytes
Avg leak per object: 1.04 bytes
```
But I expected this output instead:
```
Counting used memory / peak memory after using memory after 1000000 objects.
Before loop: 32/0 bytes
Loops Usage Peak usage
100000: 77401072/77598720 bytes
200000: 79081072/79699968 bytes
300000: 77401072/81797120 bytes
400000: 77961072/81797120 bytes
500000: 78521072/81797120 bytes
600000: 79081072/81797120 bytes
700000: 74041072/81797120 bytes
800000: 74041072/81797120 bytes
900000: 74041072/81797120 bytes
1000000: 74041072/81797120 bytes
After the loop:
after releasing $d: 64/81797120 bytes
after unset + gc_collect_cycles: 32/81797120 bytes
Avg leak per object: 0.00 bytes
```
Changing the
if (1) inside the loop to if (0) changes the code to use
sha1() instead of new DateTime() to demonstrate that it's only
DateTime() that leaks. Luckily the amount of leaked RAM is only around 1 byte per
object but I couldn't find any upper limit for the leak so it doesn't seem to be bounded
by any internal buffer and appears to be true leak.
### PHP Version
PHP 8.1.2-1ubuntu2.14
### Operating System
Ubuntu 22.04.4 LTS