Skip to content

Commit 667a565

Browse files
committed
Merge branch 'PHP-8.3'
2 parents 5c739ac + de5c760 commit 667a565

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

sapi/phpdbg/phpdbg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ ZEND_BEGIN_MODULE_GLOBALS(phpdbg)
254254
HashTable watch_recreation; /* watch elements pending recreation of their respective watchpoints */
255255
HashTable watch_free; /* pointers to watch for being freed */
256256
HashTable *watchlist_mem; /* triggered watchpoints */
257+
HashTable *original_watchlist_mem; /* the original allocation for watchlist_mem, used when watchlist_mem has changed temporarily */
257258
HashTable *watchlist_mem_backup; /* triggered watchpoints backup table while iterating over it */
258259
bool watchpoint_hit; /* a watchpoint was hit */
259260
void (*original_free_function)(void * ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); /* the original AG(mm_heap)->_free function */

sapi/phpdbg/phpdbg_prompt.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,8 @@ int phpdbg_interactive(bool allow_async_unsafe, char *input) /* {{{ */
15471547
ret = phpdbg_stack_execute(&stack, allow_async_unsafe);
15481548
} zend_catch {
15491549
phpdbg_stack_free(&stack);
1550+
phpdbg_destroy_input(&input);
1551+
/* TODO: should use proper unwinding instead of bailing out */
15501552
zend_bailout();
15511553
} zend_end_try();
15521554

sapi/phpdbg/phpdbg_watch.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,9 @@ phpdbg_watch_element *phpdbg_add_watch_element(phpdbg_watchpoint_t *watch, phpdb
519519
phpdbg_watch_element *old_element;
520520
watch = res->ptr;
521521
if ((old_element = zend_hash_find_ptr(&watch->elements, element->str))) {
522-
phpdbg_free_watch_element(element);
522+
if (element != old_element) {
523+
phpdbg_free_watch_element(element);
524+
}
523525
return old_element;
524526
}
525527
}
@@ -1468,6 +1470,7 @@ void phpdbg_setup_watchpoints(void) {
14681470

14691471
/* put these on a separate page, to avoid conflicts with other memory */
14701472
PHPDBG_G(watchlist_mem) = malloc(phpdbg_pagesize > sizeof(HashTable) ? phpdbg_pagesize : sizeof(HashTable));
1473+
PHPDBG_G(original_watchlist_mem) = PHPDBG_G(watchlist_mem);
14711474
zend_hash_init(PHPDBG_G(watchlist_mem), phpdbg_pagesize / (sizeof(Bucket) + sizeof(uint32_t)), NULL, NULL, 1);
14721475
PHPDBG_G(watchlist_mem_backup) = malloc(phpdbg_pagesize > sizeof(HashTable) ? phpdbg_pagesize : sizeof(HashTable));
14731476
zend_hash_init(PHPDBG_G(watchlist_mem_backup), phpdbg_pagesize / (sizeof(Bucket) + sizeof(uint32_t)), NULL, NULL, 1);
@@ -1521,8 +1524,8 @@ void phpdbg_destroy_watchpoints(void) {
15211524
zend_hash_destroy(&PHPDBG_G(watch_recreation));
15221525
zend_hash_destroy(&PHPDBG_G(watch_free));
15231526
zend_hash_destroy(&PHPDBG_G(watch_collisions));
1524-
zend_hash_destroy(PHPDBG_G(watchlist_mem));
1525-
free(PHPDBG_G(watchlist_mem));
1527+
zend_hash_destroy(PHPDBG_G(original_watchlist_mem));
1528+
free(PHPDBG_G(original_watchlist_mem));
15261529
zend_hash_destroy(PHPDBG_G(watchlist_mem_backup));
15271530
free(PHPDBG_G(watchlist_mem_backup));
15281531
}

sapi/phpdbg/tests/gh15210_001.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
GH-15210 use after free after continue
3+
--PHPDBG--
4+
b 4
5+
r
6+
w $a[0]
7+
c
8+
q
9+
--FILE--
10+
<?php
11+
header_register_callback(function() { echo "sent";});
12+
$a = [0];
13+
$a[0] = 1;
14+
?>
15+
--EXPECTF--
16+
[Successful compilation of %s]
17+
prompt> [Breakpoint #0 added at %s:%d]
18+
prompt> [Breakpoint #0 at %s:%d, hits: 1]
19+
>00004: $a[0] = 1;
20+
00005: ?>
21+
00006:
22+
prompt> [Added watchpoint #0 for $a[0]]
23+
prompt> [Breaking on watchpoint $a[0]]
24+
Old value: [Breaking on watchpoint $a[0]]
25+
Old value: 0
26+
New value: 1
27+
>00002: header_register_callback(function() { echo "sent";});
28+
00003: $a = [0];
29+
00004: $a[0] = 1;
30+
prompt> [$a[0] has been removed, removing watchpoint]

sapi/phpdbg/tests/gh15210_002.phpt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
GH-15210 use after free after continue
3+
--PHPDBG--
4+
b 4
5+
r
6+
w $a[0]
7+
c
8+
c
9+
q
10+
--FILE--
11+
<?php
12+
header_register_callback(function() { echo "sent";});
13+
$a = [0];
14+
$a[0] = 1;
15+
?>
16+
--EXPECTF--
17+
[Successful compilation of %s]
18+
prompt> [Breakpoint #0 added at %s:%d]
19+
prompt> [Breakpoint #0 at %s:%d, hits: 1]
20+
>00004: $a[0] = 1;
21+
00005: ?>
22+
00006:
23+
prompt> [Added watchpoint #0 for $a[0]]
24+
prompt> [Breaking on watchpoint $a[0]]
25+
Old value: [Breaking on watchpoint $a[0]]
26+
Old value: 0
27+
New value: 1
28+
>00002: header_register_callback(function() { echo "sent";});
29+
00003: $a = [0];
30+
00004: $a[0] = 1;
31+
prompt> sent0
32+
New value: 1
33+
34+
[$a[0] has been removed, removing watchpoint]
35+
[Script ended normally]
36+
prompt>

0 commit comments

Comments
 (0)