[php-src] PR #18172: Add optional heap poisoning and sanity checks in non-debug builds
| From: | arnaud-lb | Date: | Fri, 28 Mar 2025 14:01:28 +0000 |
| Subject: | [php-src] PR #18172: Add optional heap poisoning and sanity checks in non-debug builds | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-31206@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/18172
Author: arnaud-lb
Debugging memory corruption issues in production can be difficult when it's not possible to use
a debug build or ASAN/MSAN/Valgrind (e.g. for performance reasons).
This PR makes it possible to enable some basic heap debugging helpers without rebuilding PHP. This
is controlled by the environment variable
ZEND_MM_DEBUG. The env var takes a
comma-separated list of parameters:
- poison_free=byte: Override freed blocks with the specified byte value (represented as
a number)
- poison_alloc=byte: Override newly allocated blocks with the specified byte value
(represented as a number)
- padding=bytes: Pad allocated blocks with the specified amount of bytes
- check_freelists_on_shutdown=0|1: Enable checking [freelist
consistency](https://github.com/php/php-src/pull/14054) on shutdown
Example:
ZEND_MM_DEBUG=poison_free=0xbe,poison_alloc=0xeb,padding=8,check_freelists_on_shutdown=1 php
....
### Implementation
This is implemented by installing custom handlers when ZEND_MM_DEBUG is set.
### Overhead
This has zero overhead when ZEND_MM_DEBUG is not set.
### Goals:
- Crash earlier after a memory corruption, to extract a useful backtrace
- Be usable in production with reasonable overhead
- Having zero overhead when not enabled
### Non-goals:
- Replace debug builds, valgrind, ASAN, MSAN or other sanitizers