php / php-src

The PHP Interpreter
https://www.php.net
Other
38.31k stars 7.76k forks source link

opcache/zend_shared_alloc.c: needed but ineffective pre-processor instructions #16567

Open m6w6 opened 1 month ago

m6w6 commented 1 month ago

Description

_GNU_SOURCE enabled sys/mman.h is needed for memfd_create and MFD_CLOEXEC, yet the needed pre-processor instructions at the top of zend_shared_alloc.c are useless since they are wrapped with ifdef HAVE_MEMFD_CREATE, which is defined in a *config.h not yet included.

See https://github.com/php/php-src/blob/master/ext/opcache/zend_shared_alloc.c#L22-L27

As a consequence, the build failed for me on Ubuntu-24.04 with GCC-13.

'./configure' \
'--disable-all' \
'--enable-debug' \
'--enable-cli' \
'--enable-opcache' 
make -j
/bin/bash /home/mike/src/php-src/libtool --silent --preserve-dup-deps --tag=CC --mode=compile cc -Iext/opcache/ -I/home/mike/src/php-src/ext/opcache/ -I/home/mike/src/php-src/main -I/home/mike/src/php-src -I/home/mike/src/php-src/ext/date/lib -I/home/mike/src/php-src/ext/raphf/src -I/home/mike/src/php-src/ext/raphf -I/home/mike/src/php-src/ext/http/src -I/home/mike/src/php-src/TSRM -I/home/mike/src/php-src/Zend    -fno-common -Wstrict-prototypes -Wformat-truncation -Wlogical-op -Wduplicated-cond -Wno-clobbered -Wall -Wextra -Wno-unused-parameter -Wno-sign-compare -g -ffp-contract=off -fvisibility=hidden -O0 -Wno-strict-prototypes -Wimplicit-fallthrough=1 -DZEND_SIGNALS    -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -Iext/opcache/jit/ir -DIR_TARGET_X64 -DIR_PHP -DIR_DEBUG -DZEND_COMPILE_DL_EXT=1 -c /home/mike/src/php-src/ext/opcache/zend_shared_alloc.c -o ext/opcache/zend_shared_alloc.lo  -MMD -MF ext/opcache/zend_shared_alloc.dep -MT ext/opcache/zend_shared_alloc.lo
In file included from /home/mike/src/php-src/Zend/zend.h:454,
                 from /home/mike/src/php-src/Zend/zend_objects.h:23,
                 from /home/mike/src/php-src/Zend/zend_globals.h:35,
                 from /home/mike/src/php-src/Zend/zend_compile.h:843,
                 from /home/mike/src/php-src/Zend/zend_extensions.h:23,
                 from /home/mike/src/php-src/ext/opcache/ZendAccelerator.h:51,
                 from /home/mike/src/php-src/ext/opcache/zend_shared_alloc.c:30:
/home/mike/src/php-src/Zend/zend_operators.h: In function ‘zend_memrchr’:
/home/mike/src/php-src/Zend/zend_operators.h:221:29: warning: implicit declaration of function ‘memrchr’; did you mean ‘memchr’? [-Wimplicit-function-declaration]
  221 |         return (const void*)memrchr(s, c, n);
      |                             ^~~~~~~
      |                             memchr
/home/mike/src/php-src/Zend/zend_operators.h:221:16: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
  221 |         return (const void*)memrchr(s, c, n);
      |                ^
/home/mike/src/php-src/ext/opcache/zend_shared_alloc.c: In function ‘zend_shared_alloc_create_lock’:
/home/mike/src/php-src/ext/opcache/zend_shared_alloc.c:98:21: warning: implicit declaration of function ‘memfd_create’; did you mean ‘timer_create’? [-Wimplicit-function-declaration]
   98 |         lock_file = memfd_create("opcache_lock", MFD_CLOEXEC);
      |                     ^~~~~~~~~~~~
      |                     timer_create
/home/mike/src/php-src/ext/opcache/zend_shared_alloc.c:98:50: error: ‘MFD_CLOEXEC’ undeclared (first use in this function); did you mean ‘FD_CLOEXEC’?
   98 |         lock_file = memfd_create("opcache_lock", MFD_CLOEXEC);
      |                                                  ^~~~~~~~~~~
      |                                                  FD_CLOEXEC
/home/mike/src/php-src/ext/opcache/zend_shared_alloc.c:98:50: note: each undeclared identifier is reported only once for each function it appears in
make: *** [Makefile:794: ext/opcache/zend_shared_alloc.lo] Error 1

PHP Version

master

Operating System

Ubuntu 24.04

arnaud-lb commented 1 month ago

I can reproduce when HAVE_MPROTECT is not defined, because in this case sys/mman.h is never included. This should be fixed by removing the #if defined(__linux__) && defined(HAVE_MEMFD_CREATE) block and updating the condition here to include HAVE_MEMFD_CREATE: https://github.com/php/php-src/blob/4a4371d31c44e02d5922f3179c063dfc1a3473ec/ext/opcache/zend_shared_alloc.c#L43

BTW, do you know why HAVE_MPROTECT is not defined in your case?

m6w6 commented 1 month ago

Ah. I didn't notice there's another occurrence of this include. In that case, _GNU_SOURCE might be missing.

arnaud-lb commented 1 month ago

This is odd because this is supposed to define it: https://github.com/php/php-src/blob/4a4371d31c44e02d5922f3179c063dfc1a3473ec/configure.ac#L122

Does https://github.com/php/php-src/pull/16570 fix the issue for you?