public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Fix for REAL_PAGE_SIZE in PHP
@ 2017-05-02 10:28 Richard H Lee
  2017-05-04 14:39 ` [ATTN: PHP maintainer] " Richard H Lee
  0 siblings, 1 reply; 2+ messages in thread
From: Richard H Lee @ 2017-05-02 10:28 UTC (permalink / raw)
  To: cygwin

There is a bug with PHP on Cygwin where it segfaults if the php file
size is a multiple of 4K.

https://bugs.php.net/bug.php?id=65312

PHP mmap()s source files as it it faster than using normal I/O.

The problem is with the page size in Cygwin. When a file is mmap()ed
to memory, any remaining bytes after the EOF in the last page are
zero'ed out. So when PHP reads the bytes after the EOF, it expects to
find zeroes.

In most systems the page size is 4K. In Windows the page size is also
4K, but aligned to a boundary of 64K (allocation granularity). Cygwin
reconciles this by making the page size 64K. So in reality, Cygwin
pages are 64K big, backed by 4K pages allocated along 64K boundaries.

When PHP reads a file, it looks at the file size. If there are enough
bytes to read ahead in the last memory page after the EOF, PHP will use
mmap. Else it will open the file normally.

When PHP is given a 4K file on Cygwin, it sees that the file and the
bytes for reading ahead will easily fit into a 64K page (in
Cygwin). So it mmap()s the file. However only one 4K page (in Windows)
is mapped to. The remaining 60K pages are not. When PHP reads ahead
past the EOF, it reads into page which has not been mapped to. This
results in a segfault.

Changing the REAL_PAGE_SIZE to 4096 fixes the issue.

Should this fix go upstream to PHP or be submitted to the PHP package
within Cygwin?


Richard

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [ATTN: PHP maintainer] Re: Fix for REAL_PAGE_SIZE in PHP
  2017-05-02 10:28 Fix for REAL_PAGE_SIZE in PHP Richard H Lee
@ 2017-05-04 14:39 ` Richard H Lee
  0 siblings, 0 replies; 2+ messages in thread
From: Richard H Lee @ 2017-05-04 14:39 UTC (permalink / raw)
  To: cygwin

Here is my proposed patch for this bug:

diff --git a/Zend/zend_stream.c b/Zend/zend_stream.c
index 3fd7fa0..f5b9bea 100644
--- a/Zend/zend_stream.c
+++ b/Zend/zend_stream.c
@@ -30,7 +30,11 @@
  # if HAVE_UNISTD_H
  #  include <unistd.h>
  #  if defined(_SC_PAGESIZE)
+#   ifdef __CYGWIN__
+#    define REAL_PAGE_SIZE 4096
+#   else
  #    define REAL_PAGE_SIZE sysconf(_SC_PAGESIZE);
+#   endif
  #  elif defined(_SC_PAGE_SIZE)
  #    define REAL_PAGE_SIZE sysconf(_SC_PAGE_SIZE);
  #  endif
diff --git a/main/main.c b/main/main.c
index 01ed3a6..0909309 100644
--- a/main/main.c
+++ b/main/main.c
@@ -96,7 +96,11 @@
  # if HAVE_UNISTD_H
  #  include <unistd.h>
  #  if defined(_SC_PAGESIZE)
+#   ifdef __CYGWIN__
+#    define REAL_PAGE_SIZE 4096
+#   else
  #    define REAL_PAGE_SIZE sysconf(_SC_PAGESIZE);
+#   endif
  #  elif defined(_SC_PAGE_SIZE)
  #    define REAL_PAGE_SIZE sysconf(_SC_PAGE_SIZE);
  #  endif

This definition needs to be fixed in two places as there are two
different places where a PHP source file can be mmap()ed, depending on
whether it is loaded using php-cli or loaded when php is run from a library.

On 02/05/2017 11:28, Richard H Lee wrote:
> Should this fix go upstream to PHP or be submitted to the PHP
> package within Cygwin?

I ask this as partially accessibly memory pages I guess are a quirk of
Cygwin. But then again programs ideally should not read past the end of
the EOF in the region the file is mmap()ed to.


Richard

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-05-04 14:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-02 10:28 Fix for REAL_PAGE_SIZE in PHP Richard H Lee
2017-05-04 14:39 ` [ATTN: PHP maintainer] " Richard H Lee

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).