public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Ken Brown <kbrown@cornell.edu>
To: km2z7kca0oge@opayq.com, cygwin@cygwin.com
Subject: Re: Cygwin PHP (all available versions) has a hard 4MB memory limit
Date: Tue, 21 Jul 2020 16:45:04 -0400	[thread overview]
Message-ID: <f7e9513d-b59e-d8c8-6461-5f9dd8ad999d@cornell.edu> (raw)
In-Reply-To: <0077be8f-2c24-68c3-c6ea-f6ece6b7b0cf@cornell.edu>

On 7/18/2020 10:11 PM, Ken Brown via Cygwin wrote:
> On 7/17/2020 5:39 PM, km2z7kca0oge--- via Cygwin wrote:
>> Hi there,
>>
>> Recently I've noticed that PHP seems have to hard 4MB memory limit,
> 
> [...]
> 
>> Example script:
>> ```
>> <?php
>>
>> echo ini_get('memory_limit'), "\n";
>>
>> file_get_contents('http://mirror.cwcs.co.uk/centos/8.2.2004/isos/x86_64/CentOS-8.2.2004-x86_64-dvd1.iso'); 
>> // A large file such as an ISO.
>> ```
>>
>> Output:
>> $ php test.php
>> 128M
>> PHP Fatal error:  Out of memory (allocated 4194304) (tried to allocate 2097184 
>> bytes) in /c/Users/JackBlower/tmp-safe/test.php on line 5
> 
> Thanks for the simple reproduction steps.  I've run this under gdb, and the 
> function zend_mm_alloc_huge, which tries to get a chunk of memory from the 
> system, seems to be failing when it tries to increase the zend heap from 4MB to 
> 6MB.  I'm in the process of building an unoptimized version of php to make 
> debugging easier, in the hope of figuring out why the function is failing.

The problem is that some mmap calls are failing, for two reasons.  The first 
(and less important) is a Cygwin bug, which is now fixed:

https://cygwin.com/git/?p=newlib-cygwin.git;a=commit;h=d8a8d2ce5953af3383aff019596bdf7f7aef6c41

The second is that mmap is sometimes called with MAP_FIXED, and these calls may 
fail.  But these calls occur only because of the following hunk of the patch 
7.1.9-malloc-cygwin.patch, which is used in the Cygwin build of php:

--- origsrc/php-7.1.9/Zend/zend_alloc.c	2017-08-30 11:13:20.000000000 -0500
+++ src/php-7.1.9/Zend/zend_alloc.c	2017-09-26 21:43:38.087650400 -0500
@@ -719,7 +719,7 @@ static void *zend_mm_chunk_alloc_int(siz
  		/* chunk has to be aligned */
  		zend_mm_munmap(ptr, size);
  		ptr = zend_mm_mmap(size + alignment - REAL_PAGE_SIZE);
-#ifdef _WIN32
+#if defined(_WIN32) || defined(__CYGWIN__)
  		offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment);
  		zend_mm_munmap(ptr, size + alignment - REAL_PAGE_SIZE);
  		ptr = zend_mm_mmap_fixed((void*)((char*)ptr + (alignment - offset)), size);

I don't know the reason for this hunk of the patch.  I suspect it was to 
compensate for some deficiencies in Cygwin's mmap that have since been fixed, 
but only Yaakov (the php maintainer) can confirm this.

After rebuilding php with that hunk of the patch removed, I get the following 
with your test case:

$ php test.php
128M
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to 
allocate 65011744 bytes) in /tmp/test.php on line 5

This seems to be what's expected, since 134217728 == 0x8000000 == 128M.  Here's 
another example:

$ php -d "memory_limit=256M" test.php
256M
PHP Fatal error:  Allowed memory size of 268435456 bytes exhausted (tried to 
allocate 132120608 bytes) in /tmp/test.php on line 5

I'll follow up on the cygwin-apps list to see if we can get a rebuilt php into 
the distro.

Ken

  reply	other threads:[~2020-07-21 20:45 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-17 21:39 km2z7kca0oge
2020-07-17 22:39 ` Brian Inglis
2020-07-18  7:29 ` Andrey Repin
2020-07-19  2:11 ` Ken Brown
2020-07-21 20:45   ` Ken Brown [this message]
2020-08-04  1:57     ` Cary Lewis
2020-08-04 10:45       ` Ken Brown
2020-08-04 13:35         ` Cary Lewis
2020-08-04 13:58           ` Ken Brown
2020-07-18 15:33 km2z7kca0oge
2020-07-18 17:32 ` Brian Inglis
2020-07-18 15:54 km2z7kca0oge
2020-07-21 20:58 km2z7kca0oge

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f7e9513d-b59e-d8c8-6461-5f9dd8ad999d@cornell.edu \
    --to=kbrown@cornell.edu \
    --cc=cygwin@cygwin.com \
    --cc=km2z7kca0oge@opayq.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).