Skip to content

Commit 4130fe4

Browse files
committed
Make MSVCRT memory leak checking usable for the test suite
While basic support for MSVCRT debugging has been added long ago[1], the leak checking is not usable for the test suite, because we are no longer calling `xmlCleanupParser()` on RSHUTDOWN of ext/libxml[2], and therefore a few bogus leaks are reported whenever ext/libxml is unloaded. We therefore ignore memory leaks for this case. We introduce `ZEND_IGNORE_LEAKS_BEGIN()` and `ZEND_IGNORE_LEAKS_END()` to keep those ignores better readable, and also because these *might* be useful for other leak checkers as well. We also explicitly free the `zend_handlers_table` and the `p5s` to avoid spurious leak reports. [1] <http://git.php.net/?p=php-src.git;a=commit;h=d756e1db2324c1f4ab6f9b52e329959ce6a02bc3> [2] <http://git.php.net/?p=php-src.git;a=commit;h=8742276eb3905eb97a585417000c7b8df85006d4>
1 parent 984d508 commit 4130fe4

File tree

6 files changed

+41
-4
lines changed

6 files changed

+41
-4
lines changed

Zend/zend_portability.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,4 +648,12 @@ static zend_always_inline double _zend_get_nan(void) /* {{{ */
648648
# define ZEND_PREFER_RELOAD
649649
#endif
650650

651+
#if defined(ZEND_WIN32) && defined(_DEBUG) && defined(PHP_WIN32_DEBUG_HEAP)
652+
# define ZEND_IGNORE_LEAKS_BEGIN() _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) & ~_CRTDBG_ALLOC_MEM_DF)
653+
# define ZEND_IGNORE_LEAKS_END() _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_ALLOC_MEM_DF)
654+
#else
655+
# define ZEND_IGNORE_LEAKS_BEGIN()
656+
# define ZEND_IGNORE_LEAKS_END()
657+
#endif
658+
651659
#endif /* ZEND_PORTABILITY_H */

Zend/zend_strtod.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ Bigint {
546546
static Bigint *freelist[Kmax+1];
547547

548548
static void destroy_freelist(void);
549+
static void free_p5s(void);
549550

550551
#ifdef ZTS
551552
static MUTEX_T dtoa_mutex;
@@ -564,6 +565,8 @@ ZEND_API int zend_startup_strtod(void) /* {{{ */
564565
ZEND_API int zend_shutdown_strtod(void) /* {{{ */
565566
{
566567
destroy_freelist();
568+
free_p5s();
569+
567570
#ifdef ZTS
568571
tsrm_mutex_free(dtoa_mutex);
569572
dtoa_mutex = NULL;
@@ -4540,6 +4543,19 @@ static void destroy_freelist(void)
45404543
FREE_DTOA_LOCK(0)
45414544
}
45424545

4546+
static void free_p5s(void)
4547+
{
4548+
Bigint **listp, *tmp;
4549+
4550+
ACQUIRE_DTOA_LOCK(1)
4551+
listp = &p5s;
4552+
while ((tmp = *listp) != NULL) {
4553+
*listp = tmp->next;
4554+
free(tmp);
4555+
}
4556+
FREE_DTOA_LOCK(1)
4557+
}
4558+
45434559
#ifdef __cplusplus
45444560
}
45454561
#endif

Zend/zend_vm_execute.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61333,13 +61333,17 @@ void zend_vm_init(void)
6133361333
VM_TRACE_START();
6133461334
}
6133561335

61336+
static HashTable *zend_handlers_table = NULL;
61337+
6133661338
void zend_vm_dtor(void)
6133761339
{
6133861340
VM_TRACE_END();
61341+
if (zend_handlers_table) {
61342+
zend_hash_destroy(zend_handlers_table);
61343+
free(zend_handlers_table);
61344+
}
6133961345
}
6134061346

61341-
static HashTable *zend_handlers_table = NULL;
61342-
6134361347
static void init_opcode_serialiser(void)
6134461348
{
6134561349
int i;

Zend/zend_vm_execute.skl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,17 @@ void {%INITIALIZER_NAME%}(void)
6767
VM_TRACE_START();
6868
}
6969

70+
static HashTable *zend_handlers_table = NULL;
71+
7072
void zend_vm_dtor(void)
7173
{
7274
VM_TRACE_END();
75+
if (zend_handlers_table) {
76+
zend_hash_destroy(zend_handlers_table);
77+
free(zend_handlers_table);
78+
}
7379
}
7480

75-
static HashTable *zend_handlers_table = NULL;
76-
7781
static void init_opcode_serialiser(void)
7882
{
7983
int i;

ext/libxml/config.w32

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ if (PHP_LIBXML == "yes") {
1616
ADD_DEF_FILE("ext\\libxml\\php_libxml2.def");
1717
}
1818
PHP_INSTALL_HEADERS("ext/libxml/", "php_libxml.h");
19+
if (PHP_CRT_DEBUG == "yes") {
20+
ADD_FLAG("CFLAGS_LIBXML", "/D PHP_WIN32_DEBUG_HEAP");
21+
}
1922
} else {
2023
WARNING("libxml support can't be enabled, iconv or libxml are missing")
2124
PHP_LIBXML = "no"

ext/libxml/libxml.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,9 @@ PHP_LIBXML_API void php_libxml_initialize(void)
752752
{
753753
if (!_php_libxml_initialized) {
754754
/* we should be the only one's to ever init!! */
755+
ZEND_IGNORE_LEAKS_BEGIN();
755756
xmlInitParser();
757+
ZEND_IGNORE_LEAKS_END();
756758

757759
_php_libxml_default_entity_loader = xmlGetExternalEntityLoader();
758760
xmlSetExternalEntityLoader(_php_libxml_pre_ext_ent_loader);

0 commit comments

Comments
 (0)