[php-src] PR #25666: Tell GCC and Clang that `efree()` releases what `emalloc()` returns
| From: | sebastianbergmann | Date: | Mon, 23 Nov 2026 10:32:46 +0000 |
| Subject: | [php-src] PR #25666: Tell GCC and Clang that `efree()` releases what `emalloc()` returns | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40920@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25666
Author: sebastianbergmann
> **This is not ready to be merged!**
> It is a proof of concept that I am opening as a draft to ask three questions, listed at the
> end.
> As it stands, it adds false-positive warnings to a normal GCC build and would break
>
--enable-werror.
### The problem
A file with six deliberate errors against the request allocator compiles without a single
diagnostic: not from -Wall -Wextra, not from gcc -fanalyzer, not from
clang --analyze.
<details>
<summary>The six errors</summary>
```c
#include "php.h"
void overflow_constant(const char *src) /* A */
{
char *buffer = emalloc(8);
memcpy(buffer, src, 16);
efree(buffer);
}
void overflow_variable(const char *src, size_t length) /* B */
{
char *buffer = emalloc(length);
memcpy(buffer, src, length + 1);
efree(buffer);
}
void wrong_deallocator(void) /* C */
{
char *buffer = emalloc(8);
buffer[0] = 0;
free(buffer);
}
char use_after_free(void) /* D */
{
char *buffer = emalloc(8);
buffer[0] = 1;
efree(buffer);
return buffer[0];
}
void double_free(int again) /* E */
{
char *buffer = emalloc(8);
buffer[0] = 1;
efree(buffer);
if (again) {
efree(buffer);
}
}
int leak(int error) /* F */
{
char *buffer = emalloc(8);
buffer[0] = 1;
if (error) {
return -1;
}
efree(buffer);
return 0;
}
```
</details>
There are two reasons.
**1. Nothing tells the compiler that _efree() releases what _emalloc()
returns.** _emalloc() carries malloc and alloc_size, but GCC
needs malloc(deallocator) and Clang needs
ownership_returns/ownership_takes to pair the two.
**2. In a release build, alloc_size on _emalloc() has no effect.**
zend_alloc.h replaces _emalloc(size) by a macro that tests
__builtin_constant_p(size) and calls _emalloc_8(),
_emalloc_16() and so on for constant sizes. These functions take no argument that
alloc_size could refer to, and because the macro is a conditional expression, the size
is lost for non-constant sizes as well:
| __builtin_dynamic_object_size(p, 0) for | p = emalloc(8) | p =
emalloc(length) |
|---|---|---|
| GCC 16.2 and Clang 23.1, master | unknown (-1) | unknown (-1) |
| GCC 16.2 and Clang 23.1, without the specialisation | 8 | length |
One consequence is that -D_FORTIFY_SOURCE=3 cannot protect a buffer that comes from
emalloc(): an extension that copies 16 bytes into emalloc(8) or into
emalloc(length / 2) runs to completion with and without _FORTIFY_SOURCE,
and its shared object does not even reference __memcpy_chk. A debug build does not
specialise, so the size is visible there.
### What this draft does
* It adds ZEND_ATTRIBUTE_ALLOCATES(pool, deallocator) and
ZEND_ATTRIBUTE_RELEASES(pool, index) to zend_portability.h:
malloc(deallocator, 1) for GCC 11 and later,
ownership_returns/ownership_takes for Clang.
* It puts them on _efree(), _emalloc(), _safe_emalloc(),
_ecalloc(), _estrdup() and _estrndup().
* It switches the size specialisation off when ZEND_ALLOC_NO_SPECIALIZATION is defined
or the Clang Static Analyzer is running (__clang_analyzer__). GCC defines no macro
under -fanalyzer.
What the tools then report for the six errors (master @ 9af77c680e0, GCC
16.2.1, Clang 23.1.5):
| | GCC -Wall -Wextra | gcc -fanalyzer | clang --analyze |
|---|---|---|---|
| master | nothing | nothing | nothing |
| with this change | D | B, D | C, D, E, F |
| with this change and -DZEND_ALLOC_NO_SPECIALIZATION | A, C, D | A, B, C, D, F | C, D,
E, F |
Between them, the two analyzers find all six.
### Why it is not ready
* **False positives in a normal build.** A plain ./configure && make with GCC
16 goes from zero compiler warnings to two kinds, at about ten places:
* -Wmismatched-dealloc in zend_string_release() and
zend_string_free(): pefree(s, persistent) is persistent ? free(s) :
efree(s), and after inlining GCC sees a path on which memory from _emalloc()
reaches free() (ext/openssl/openssl.c:4290,
ext/uri/uri_parser_rfc3986.c:792, Zend/zend_property_hooks.c:95 and
others).
* -Wfree-nonheap-object for free_alloca(p, use_heap) in
Zend/Optimizer/zend_inference.c:480.
Both are wrong, and both would break a build with --enable-werror.
* **gcc -fanalyzer assumes that emalloc() can return NULL**
as soon as the deallocator is named, and reports "dereference of possibly-NULL" for every
use without a check (five times in the file above). returns_nonnull on
_emalloc() removes them, but it is a promise about every allocator that can sit behind
_emalloc(), including custom handlers.
* **erealloc()** and the size-specialised
_emalloc_N()/_efree_N() are not annotated.
* **Switching the specialisation off** costs the fast path it exists for.
* **I have not run the analyzers over php-src with the annotations in place**, so I cannot say yet
how many real findings and how many false positives they produce there. The table above is about six
constructed errors.
### Questions
1. Should the annotations be active in every build (with the two false positives silenced where they
arise), or only in a build for static analysis?
2. Is returns_nonnull a promise that _emalloc() can make?
3. Is a switch such as ZEND_ALLOC_NO_SPECIALIZATION acceptable, and should a build that
sets _FORTIFY_SOURCE define it?