Skip to content

Commit 76e3e99

Browse files
committed
Fix issues with phpdbg SIGINT handler
Also fix valgrind warnings in allocator when not using mmap()ed memory
1 parent f9ae74a commit 76e3e99

File tree

6 files changed

+39
-26
lines changed

6 files changed

+39
-26
lines changed

Zend/zend_alloc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,6 +2765,7 @@ ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void
27652765
#endif
27662766
heap->storage = &tmp_storage;
27672767
heap->huge_list = NULL;
2768+
memset(heap->free_slot, 0, sizeof(heap->free_slot));
27682769
storage = _zend_mm_alloc(heap, sizeof(zend_mm_storage) + data_size ZEND_FILE_LINE_CC ZEND_FILE_LINE_CC);
27692770
if (!storage) {
27702771
handlers->chunk_free(&tmp_storage, chunk, ZEND_MM_CHUNK_SIZE);

sapi/phpdbg/phpdbg.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ static PHP_RSHUTDOWN_FUNCTION(phpdbg) /* {{{ */
220220
zend_llist_destroy(&PHPDBG_G(watchlist_mem));
221221

222222
if (PHPDBG_G(buffer)) {
223-
efree(PHPDBG_G(buffer));
223+
free(PHPDBG_G(buffer));
224224
PHPDBG_G(buffer) = NULL;
225225
}
226226

@@ -1085,17 +1085,21 @@ static inline void phpdbg_sigint_handler(int signo) /* {{{ */
10851085
}
10861086
} else {
10871087
/* set signalled only when not interactive */
1088-
if (!(PHPDBG_G(flags) & PHPDBG_IS_INTERACTIVE)) {
1089-
if (PHPDBG_G(flags) & PHPDBG_IS_SIGNALED) {
1090-
char mem[PHPDBG_SIGSAFE_MEM_SIZE + 1];
1091-
1092-
phpdbg_set_sigsafe_mem(mem);
1093-
zend_try {
1094-
phpdbg_force_interruption();
1095-
} zend_end_try()
1096-
phpdbg_clear_sigsafe_mem();
1097-
return;
1088+
if (PHPDBG_G(flags) & PHPDBG_IS_SIGNALED) {
1089+
char mem[PHPDBG_SIGSAFE_MEM_SIZE + 1];
1090+
1091+
phpdbg_set_sigsafe_mem(mem);
1092+
zend_try {
1093+
phpdbg_force_interruption();
1094+
} zend_end_try()
1095+
phpdbg_clear_sigsafe_mem();
1096+
1097+
PHPDBG_G(flags) &= ~PHPDBG_IS_SIGNALED;
1098+
1099+
if (PHPDBG_G(flags) & PHPDBG_IS_STOPPING) {
1100+
zend_bailout();
10981101
}
1102+
} else {
10991103
PHPDBG_G(flags) |= PHPDBG_IS_SIGNALED;
11001104
}
11011105
}
@@ -1182,9 +1186,13 @@ void phpdbg_sigio_handler(int sig, siginfo_t *info, void *context) /* {{{ */
11821186
phpdbg_force_interruption();
11831187
} zend_end_try();
11841188
phpdbg_clear_sigsafe_mem();
1185-
break;
1186-
}
1187-
if (!(PHPDBG_G(flags) & PHPDBG_IS_INTERACTIVE)) {
1189+
1190+
PHPDBG_G(flags) &= ~PHPDBG_IS_SIGNALED;
1191+
1192+
if (PHPDBG_G(flags) & PHPDBG_IS_STOPPING) {
1193+
zend_bailout();
1194+
}
1195+
} else if (!(PHPDBG_G(flags) & PHPDBG_IS_INTERACTIVE)) {
11881196
PHPDBG_G(flags) |= PHPDBG_IS_SIGNALED;
11891197
}
11901198
break;

sapi/phpdbg/phpdbg_cmd.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -768,16 +768,14 @@ PHPDBG_API char *phpdbg_read_input(char *buffered) /* {{{ */
768768

769769
if (buffer && strlen(buffer)) {
770770
if (PHPDBG_G(buffer)) {
771-
efree(PHPDBG_G(buffer));
771+
free(PHPDBG_G(buffer));
772772
}
773-
PHPDBG_G(buffer) = estrdup(buffer);
774-
} else {
775-
if (PHPDBG_G(buffer)) {
776-
if (buffer) {
777-
efree(buffer);
778-
}
779-
buffer = estrdup(PHPDBG_G(buffer));
773+
PHPDBG_G(buffer) = strdup(buffer);
774+
} else if (PHPDBG_G(buffer)) {
775+
if (buffer) {
776+
efree(buffer);
780777
}
778+
buffer = estrdup(PHPDBG_G(buffer));
781779
}
782780

783781
return buffer;

sapi/phpdbg/phpdbg_prompt.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,13 @@ void phpdbg_force_interruption(void) /* {{{ */ {
16281628

16291629
if (data) {
16301630
if (data->func) {
1631-
phpdbg_notice("hardinterrupt", "opline=\"%p\" num=\"%lu\" file=\"%s\" line=\"%u\"", "Current opline: %p (op #%lu) in %s:%u", data->opline, (data->opline - data->func->op_array.opcodes) / sizeof(data->opline), data->func->op_array.filename, data->opline->lineno);
1631+
if (ZEND_USER_CODE(data->func->type)) {
1632+
phpdbg_notice("hardinterrupt", "opline=\"%p\" num=\"%lu\" file=\"%s\" line=\"%u\"", "Current opline: %p (op #%lu) in %s:%u", data->opline, (data->opline - data->func->op_array.opcodes) / sizeof(data->opline), data->func->op_array.filename, data->opline->lineno);
1633+
} else if (data->func->internal_function.function_name) {
1634+
phpdbg_notice("hardinterrupt", "func=\"%s\"", "Current opline: in internal function %s", data->func->internal_function.function_name->val);
1635+
} else {
1636+
phpdbg_notice("hardinterrupt", "", "Current opline: executing internal code");
1637+
}
16321638
} else {
16331639
phpdbg_notice("hardinterrupt", "opline=\"%p\"", "Current opline: %p (op_array information unavailable)", data->opline);
16341640
}

sapi/phpdbg/phpdbg_sigsafe.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
88

99
static void* zend_mm_mem_alloc(zend_mm_storage *storage, size_t size, size_t alignment) {
1010

11-
if (EXPECTED(size == PHPDBG_SIGSAFE_MEM_SIZE && !PHPDBG_G(sigsafe_mem).allocated)) {
11+
if (EXPECTED(size <= PHPDBG_SIGSAFE_MEM_SIZE && !PHPDBG_G(sigsafe_mem).allocated)) {
1212
PHPDBG_G(sigsafe_mem).allocated = 1;
13-
return PHPDBG_G(sigsafe_mem).mem;
13+
return (void *) (((size_t) PHPDBG_G(sigsafe_mem).mem & ~(alignment - 1)) + alignment);
1414
}
1515

1616
quiet_write(PHPDBG_G(io)[PHPDBG_STDERR].fd, ZEND_STRL("Tried to allocate more than " EXP_STR(PHPDBG_SIGSAFE_MEM_SIZE) " bytes from stack memory in signal handler ... bailing out of signal handler\n"));

sapi/phpdbg/phpdbg_sigsafe.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef PHPDBG_SIGSAFE_H
22
#define PHPDBG_SIGSAFE_H
33

4-
#define PHPDBG_SIGSAFE_MEM_SIZE ZEND_MM_CHUNK_SIZE // (1 << 20)
4+
#define PHPDBG_SIGSAFE_MEM_SIZE (ZEND_MM_CHUNK_SIZE * 2)
55

66
#include "zend.h"
77

0 commit comments

Comments
 (0)