php5 5.6.3+dfsg-1
[pkg-php/php.git] / debian / patches / page_size_fixes.patch
1 Description: Don't assume value of PAGE_SIZE if sys/mman.h does not
2  define it. Instead, use sysconf() at runtime if available.
3 Origin: vendor
4 Forwarded: no
5 Last-Update: 2010-02-09
6
7 Index: php/Zend/zend_stream.c
8 ===================================================================
9 --- php.orig/Zend/zend_stream.c
10 +++ php/Zend/zend_stream.c
11 @@ -27,10 +27,15 @@
12  
13  #include <sys/types.h>
14  #include <sys/stat.h>
15 -#if HAVE_SYS_MMAN_H
16 -# include <sys/mman.h>
17 -# ifndef PAGE_SIZE
18 -#  define PAGE_SIZE 4096
19 +#if HAVE_UNISTD_H
20 +# include <unistd.h>
21 +#endif
22 +#if !HAVE_UNISTD_H || (!defined(_SC_PAGESIZE) && !defined(_SC_PAGE_SIZE))
23 +# if HAVE_SYS_MMAN_H
24 +#  include <sys/mman.h>
25 +#  ifndef PAGE_SIZE
26 +#   define PAGE_SIZE 4096
27 +#  endif
28  # endif
29  #endif
30  
31 @@ -215,9 +220,21 @@ ZEND_API int zend_stream_fixup(zend_file
32  
33         if (old_type == ZEND_HANDLE_FP && !file_handle->handle.stream.isatty && size) {
34  #if HAVE_MMAP
35 +               long page_size = 0;
36 +
37 +# if HAVE_UNISTD_H
38 +#  ifdef _SC_PAGESIZE
39 +               page_size = sysconf(_SC_PAGESIZE);
40 +#  else
41 +               page_size = sysconf(_SC_PAGE_SIZE);
42 +#  endif
43 +# else
44 +               page_size = PAGE_SIZE;
45 +# endif
46 +
47                 if (file_handle->handle.fp &&
48                     size != 0 &&
49 -                   ((size - 1) % PAGE_SIZE) <= PAGE_SIZE - ZEND_MMAP_AHEAD) {
50 +                   ((size - 1) % page_size) <= page_size - ZEND_MMAP_AHEAD) {
51                         /*  *buf[size] is zeroed automatically by the kernel */
52                         *buf = mmap(0, size + ZEND_MMAP_AHEAD, PROT_READ, MAP_PRIVATE, fileno(file_handle->handle.fp), 0);
53                         if (*buf != MAP_FAILED) {
54 Index: php/main/main.c
55 ===================================================================
56 --- php.orig/main/main.c
57 +++ php/main/main.c
58 @@ -91,15 +91,17 @@
59  #include "SAPI.h"
60  #include "rfc1867.h"
61  
62 -#if HAVE_SYS_MMAN_H
63 -# include <sys/mman.h>
64 -# ifndef PAGE_SIZE
65 +#if !HAVE_UNISTD_H || (!defined(_SC_PAGESIZE) && !defined(_SC_PAGE_SIZE))
66 +# if HAVE_SYS_MMAN_H
67 +#  include <sys/mman.h>
68 +#  ifndef PAGE_SIZE
69 +#   define PAGE_SIZE 4096
70 +#  endif
71 +# endif
72 +# ifdef PHP_WIN32
73  #  define PAGE_SIZE 4096
74  # endif
75  #endif
76 -#ifdef PHP_WIN32
77 -# define PAGE_SIZE 4096
78 -#endif
79  /* }}} */
80  
81  PHPAPI int (*php_register_internal_extensions_func)(TSRMLS_D) = php_register_internal_extensions;
82 @@ -1161,6 +1163,17 @@ PHPAPI int php_stream_open_for_zend_ex(c
83         char *p;
84         size_t len, mapped_len;
85         php_stream *stream = php_stream_open_wrapper((char *)filename, "rb", mode, &handle->opened_path);
86 +       long page_size = 0;
87 +
88 +#if HAVE_UNISTD_H
89 +# ifdef _SC_PAGESIZE
90 +       page_size = sysconf(_SC_PAGESIZE);
91 +# else
92 +       page_size = sysconf(_SC_PAGE_SIZE);
93 +# endif
94 +#else
95 +       page_size = PAGE_SIZE;
96 +#endif
97  
98         if (stream) {
99                 handle->filename = (char*)filename;
100 @@ -1173,7 +1186,7 @@ PHPAPI int php_stream_open_for_zend_ex(c
101                 memset(&handle->handle.stream.mmap, 0, sizeof(handle->handle.stream.mmap));
102                 len = php_zend_stream_fsizer(stream TSRMLS_CC);
103                 if (len != 0
104 -               && ((len - 1) % PAGE_SIZE) <= PAGE_SIZE - ZEND_MMAP_AHEAD
105 +               && ((len - 1) % page_size) <= page_size - ZEND_MMAP_AHEAD
106                 && php_stream_mmap_possible(stream)
107                 && (p = php_stream_mmap_range(stream, 0, len, PHP_STREAM_MAP_MODE_SHARED_READONLY, &mapped_len)) != NULL) {
108                         handle->handle.stream.closer   = php_zend_stream_mmap_closer;