[Bug 268318] www/apache24 with www/mod_php8{0,1,2,3?}: opcache + ASLR turned on crashes Apache

From: <bugzilla-noreply_at_freebsd.org>
Date: Thu, 26 Nov 2026 10:03:21 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=268318

Dan Mahoney <freebsd@gushi.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |freebsd@gushi.org

--- Comment #103 from Dan Mahoney <freebsd@gushi.org> ---
I've root-caused this. Short version: opcache.so is never unloaded, but
libphp.so is, and on reload opcache keeps calling into the old libphp
mapping.

Mechanism:

1. opcache's accel_startup() sets extension->handle = 0 ("Prevent
   unloading", ext/opcache/ZendAccelerator.c), so php_module_shutdown()
   dlcloses every extension except opcache.so.
2. On graceful restart Apache dlcloses libphp.so and dlopens it again.
   With ASLR it lands at a new address.
3. PHP's dlopen(opcache.so) returns the existing, already-relocated
   object (refcount 2). Its lazily bound GOT entries still point into
   the old libphp mapping.
4. accel_startup() -> start_accel_module() -> zend_startup_module@plt
   jumps to the stale address. If that address is now unmapped or
   non-executable you get SIGSEGV; if it happens to land in executable
   code of the new libphp, the parent spins at 100% CPU and stops
   serving (the "survives but isn't functional" reports above).

Confirmed with lldb (GOT slot holds old libphp base + offset of
zend_startup_module) and LD_UTRACE (libphp unloaded and reloaded at a
new base, opcache.so never dlclosed). On Linux, glibc keeps libphp
loaded because opcache has bound symbols in it, and FreeBSD rtld does
not do that, which is why this is FreeBSD-specific.

This also explains why every workaround here is hit-or-miss: disabling
ASLR, reordering LoadModule and changing the extension set only change
the odds that libphp reloads at the same address.

Fix: mark libphp.so NODELETE so it is never unmapped. One line in the
mod_php block (.if defined(PKGNAMEPREFIX)) of lang/php82/Makefile:

LDFLAGS+=       -Wl,-z,nodelete

plus a PORTREVISION bump in www/mod_php82. No PHP source change is
needed.

Tested on 14.5-RELEASE-p4 amd64, apache24-2.4.71 (prefork),
mod_php82-8.2.36 + opcache, ASLR on, no other workarounds: 25 of 25
restarts (22 graceful, 3 SIGHUP) with no crash or hang, and opcache
active after each. Unpatched, the same setup fails on the first
graceful every time; the official 8.2.36 package hangs instead of
crashing.

The same "Prevent unloading" code is in 8.3, 8.4 and master, so the
same line should apply to lang/php83 and lang/php84 (not tested there).

Could this be reopened?

-- 
You are receiving this mail because:
You are the assignee for the bug.