Skip to content

Commit e228395

Browse files
committed
Use pemalloc/perealloc for persistent allocations
Replace direct malloc/realloc calls with OOM-safe pemalloc/perealloc (persistent: true) counterparts. Fixes phpGH-19200 Fixes phpGH-17013 Closes phpGH-21625
1 parent 334287d commit e228395

9 files changed

Lines changed: 26 additions & 34 deletions

File tree

Zend/zend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ ZEND_API void zend_append_version_info(const zend_extension *extension) /* {{{ *
13031303

13041304
snprintf(new_info, new_info_length, " with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);
13051305

1306-
zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length + 1);
1306+
zend_version_info = (char *) perealloc(zend_version_info, zend_version_info_length+new_info_length + 1, true);
13071307
strncat(zend_version_info, new_info, new_info_length);
13081308
zend_version_info_length += new_info_length;
13091309
free(new_info);

Zend/zend_API.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,19 +2504,19 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
25042504
dl_loaded_count++;
25052505
}
25062506
} ZEND_HASH_FOREACH_END();
2507-
module_request_startup_handlers = (zend_module_entry**)realloc(
2507+
module_request_startup_handlers = (zend_module_entry**)perealloc(
25082508
module_request_startup_handlers,
25092509
sizeof(zend_module_entry*) *
25102510
(startup_count + 1 +
25112511
shutdown_count + 1 +
2512-
post_deactivate_count + 1));
2512+
post_deactivate_count + 1), true);
25132513
module_request_startup_handlers[startup_count] = NULL;
25142514
module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
25152515
module_request_shutdown_handlers[shutdown_count] = NULL;
25162516
module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
25172517
module_post_deactivate_handlers[post_deactivate_count] = NULL;
25182518
/* Cannot reuse module_request_startup_handlers because it is freed in zend_destroy_modules, which happens before zend_unload_modules. */
2519-
modules_dl_loaded = realloc(modules_dl_loaded, sizeof(zend_module_entry*) * (dl_loaded_count + 1));
2519+
modules_dl_loaded = perealloc(modules_dl_loaded, sizeof(zend_module_entry*) * (dl_loaded_count + 1), true);
25202520
modules_dl_loaded[dl_loaded_count] = NULL;
25212521
startup_count = 0;
25222522

@@ -2543,10 +2543,10 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
25432543
}
25442544
} ZEND_HASH_FOREACH_END();
25452545

2546-
class_cleanup_handlers = (zend_class_entry**)realloc(
2546+
class_cleanup_handlers = (zend_class_entry**)perealloc(
25472547
class_cleanup_handlers,
25482548
sizeof(zend_class_entry*) *
2549-
(class_count + 1));
2549+
(class_count + 1), true);
25502550
class_cleanup_handlers[class_count] = NULL;
25512551

25522552
if (class_count) {
@@ -3143,7 +3143,7 @@ ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend
31433143
}
31443144
lowercase_name = zend_string_tolower_ex(internal_function->function_name, type == MODULE_PERSISTENT);
31453145
lowercase_name = zend_new_interned_string(lowercase_name);
3146-
reg_function = malloc(sizeof(zend_internal_function));
3146+
reg_function = pemalloc(sizeof(zend_internal_function), true);
31473147
memcpy(reg_function, &function, sizeof(zend_internal_function));
31483148
if (zend_hash_add_ptr(target_function_table, lowercase_name, reg_function) == NULL) {
31493149
unload=1;
@@ -3161,8 +3161,8 @@ ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend
31613161
zend_flf_capacity *= 2;
31623162
}
31633163
/* +1 for NULL terminator */
3164-
zend_flf_handlers = realloc(zend_flf_handlers, (zend_flf_capacity + 1) * sizeof(void *));
3165-
zend_flf_functions = realloc(zend_flf_functions, (zend_flf_capacity + 1) * sizeof(zend_function *));
3164+
zend_flf_handlers = perealloc(zend_flf_handlers, (zend_flf_capacity + 1) * sizeof(void *), true);
3165+
zend_flf_functions = perealloc(zend_flf_functions, (zend_flf_capacity + 1) * sizeof(zend_function *), true);
31663166
}
31673167
zend_flf_handlers[zend_flf_count] = flf_info->handler;
31683168
zend_flf_functions[zend_flf_count] = (zend_function *)reg_function;
@@ -3208,7 +3208,7 @@ ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend
32083208

32093209
/* Treat return type as an extra argument */
32103210
num_args++;
3211-
new_arg_info = malloc(sizeof(zend_arg_info) * num_args);
3211+
new_arg_info = pemalloc(sizeof(zend_arg_info) * num_args, true);
32123212
reg_function->arg_info = new_arg_info + 1;
32133213
for (i = 0; i < num_args; i++) {
32143214
zend_convert_internal_arg_info(&new_arg_info[i], &arg_info[i],
@@ -3493,7 +3493,7 @@ ZEND_API int zend_next_free_module(void) /* {{{ */
34933493

34943494
static zend_class_entry *do_register_internal_class(const zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */
34953495
{
3496-
zend_class_entry *class_entry = malloc(sizeof(zend_class_entry));
3496+
zend_class_entry *class_entry = pemalloc(sizeof(zend_class_entry), true);
34973497
zend_string *lowercase_name;
34983498
*class_entry = *orig_class_entry;
34993499

Zend/zend_inheritance.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,11 +1595,7 @@ static void zend_do_inherit_interfaces(zend_class_entry *ce, const zend_class_en
15951595

15961596
ce_num = ce->num_interfaces;
15971597

1598-
if (ce->type == ZEND_INTERNAL_CLASS) {
1599-
ce->interfaces = (zend_class_entry **) realloc(ce->interfaces, sizeof(zend_class_entry *) * (ce_num + if_num));
1600-
} else {
1601-
ce->interfaces = (zend_class_entry **) erealloc(ce->interfaces, sizeof(zend_class_entry *) * (ce_num + if_num));
1602-
}
1598+
ce->interfaces = (zend_class_entry **) perealloc(ce->interfaces, sizeof(zend_class_entry *) * (ce_num + if_num), ce->type == ZEND_INTERNAL_CLASS);
16031599

16041600
/* Inherit the interfaces, only if they're not already inherited by the class */
16051601
while (if_num--) {
@@ -2234,11 +2230,7 @@ ZEND_API void zend_do_implement_interface(zend_class_entry *ce, zend_class_entry
22342230
} ZEND_HASH_FOREACH_END();
22352231
} else {
22362232
if (ce->num_interfaces >= current_iface_num) {
2237-
if (ce->type == ZEND_INTERNAL_CLASS) {
2238-
ce->interfaces = (zend_class_entry **) realloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num));
2239-
} else {
2240-
ce->interfaces = (zend_class_entry **) erealloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num));
2241-
}
2233+
ce->interfaces = (zend_class_entry **) perealloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num), ce->type == ZEND_INTERNAL_CLASS);
22422234
}
22432235
ce->interfaces[ce->num_interfaces++] = iface;
22442236

ext/opcache/zend_accelerator_blacklist.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static inline void zend_accel_blacklist_allocate(zend_blacklist *blacklist)
225225
{
226226
if (blacklist->pos == blacklist->size) {
227227
blacklist->size += ZEND_BLACKLIST_BLOCK_SIZE;
228-
blacklist->entries = (zend_blacklist_entry *) realloc(blacklist->entries, sizeof(zend_blacklist_entry)*blacklist->size);
228+
blacklist->entries = (zend_blacklist_entry *) perealloc(blacklist->entries, sizeof(zend_blacklist_entry)*blacklist->size, true);
229229
}
230230
}
231231

main/network.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,15 +1459,15 @@ static struct hostent * gethostname_re (const char *host,struct hostent *hostbuf
14591459

14601460
if (*hstbuflen == 0) {
14611461
*hstbuflen = 1024;
1462-
*tmphstbuf = (char *)malloc (*hstbuflen);
1462+
*tmphstbuf = (char *)pemalloc(*hstbuflen, true);
14631463
}
14641464

14651465
while (( res =
14661466
gethostbyname_r(host,hostbuf,*tmphstbuf,*hstbuflen,&hp,&herr))
14671467
&& (errno == ERANGE)) {
14681468
/* Enlarge the buffer. */
14691469
*hstbuflen *= 2;
1470-
*tmphstbuf = (char *)realloc (*tmphstbuf,*hstbuflen);
1470+
*tmphstbuf = (char *)perealloc(*tmphstbuf, *hstbuflen, true);
14711471
}
14721472

14731473
if (res != 0) {
@@ -1485,15 +1485,15 @@ static struct hostent * gethostname_re (const char *host,struct hostent *hostbuf
14851485

14861486
if (*hstbuflen == 0) {
14871487
*hstbuflen = 1024;
1488-
*tmphstbuf = (char *)malloc (*hstbuflen);
1488+
*tmphstbuf = (char *)pemalloc(*hstbuflen, true);
14891489
}
14901490

14911491
while ((NULL == ( hp =
14921492
gethostbyname_r(host,hostbuf,*tmphstbuf,*hstbuflen,&herr)))
14931493
&& (errno == ERANGE)) {
14941494
/* Enlarge the buffer. */
14951495
*hstbuflen *= 2;
1496-
*tmphstbuf = (char *)realloc (*tmphstbuf,*hstbuflen);
1496+
*tmphstbuf = (char *)perealloc(*tmphstbuf, *hstbuflen, true);
14971497
}
14981498
return hp;
14991499
}
@@ -1503,11 +1503,11 @@ static struct hostent * gethostname_re (const char *host,struct hostent *hostbuf
15031503
{
15041504
if (*hstbuflen == 0) {
15051505
*hstbuflen = sizeof(struct hostent_data);
1506-
*tmphstbuf = (char *)malloc (*hstbuflen);
1506+
*tmphstbuf = (char *)pemalloc(*hstbuflen, true);
15071507
} else {
15081508
if (*hstbuflen < sizeof(struct hostent_data)) {
15091509
*hstbuflen = sizeof(struct hostent_data);
1510-
*tmphstbuf = (char *)realloc(*tmphstbuf, *hstbuflen);
1510+
*tmphstbuf = (char *)perealloc(*tmphstbuf, *hstbuflen, true);
15111511
}
15121512
}
15131513
memset((void *)(*tmphstbuf),0,*hstbuflen);

main/php_ini.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ void php_init_config(void)
694694

695695
if (total_l) {
696696
size_t php_ini_scanned_files_len = (php_ini_scanned_files) ? strlen(php_ini_scanned_files) + 1 : 0;
697-
php_ini_scanned_files = (char *) realloc(php_ini_scanned_files, php_ini_scanned_files_len + total_l + 1);
697+
php_ini_scanned_files = (char *) perealloc(php_ini_scanned_files, php_ini_scanned_files_len + total_l + 1, true);
698698
if (!php_ini_scanned_files_len) {
699699
*php_ini_scanned_files = '\0';
700700
}

main/php_ini_builder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static inline char *php_ini_builder_finish(struct php_ini_builder *b)
6060
static inline void php_ini_builder_realloc(struct php_ini_builder *b, size_t delta)
6161
{
6262
/* reserve enough space for the null terminator */
63-
b->value = realloc(b->value, b->length + delta + 1);
63+
b->value = perealloc(b->value, b->length + delta + 1, true);
6464
}
6565

6666
/**

sapi/phpdbg/phpdbg.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,9 +1199,9 @@ int main(int argc, char **argv) /* {{{ */
11991199
case 'z':
12001200
zend_extensions_len++;
12011201
if (zend_extensions_list) {
1202-
zend_extensions_list = realloc(zend_extensions_list, sizeof(char*) * zend_extensions_len);
1202+
zend_extensions_list = perealloc(zend_extensions_list, sizeof(char*) * zend_extensions_len, true);
12031203
} else {
1204-
zend_extensions_list = malloc(sizeof(char*) * zend_extensions_len);
1204+
zend_extensions_list = pemalloc(sizeof(char*) * zend_extensions_len, true);
12051205
}
12061206
zend_extensions_list[zend_extensions_len-1] = strdup(php_optarg);
12071207
break;

sapi/phpdbg/phpdbg_prompt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ static void phpdbg_line_init(char *cmd, struct phpdbg_init_state *state) {
215215

216216
if (state->in_code) {
217217
if (state->code == NULL) {
218-
state->code = malloc(cmd_len + 1);
218+
state->code = pemalloc(cmd_len + 1, true);
219219
} else {
220-
state->code = realloc(state->code, state->code_len + cmd_len + 1);
220+
state->code = perealloc(state->code, state->code_len + cmd_len + 1, true);
221221
}
222222

223223
if (state->code) {

0 commit comments

Comments
 (0)