@@ -333,7 +333,8 @@ struct _gc_stack {
333333static zend_never_inline gc_stack * gc_stack_next (gc_stack * stack )
334334{
335335 if (UNEXPECTED (!stack -> next )) {
336- gc_stack * segment = emalloc (sizeof (gc_stack ));
336+ /* Traversal must not bail out on the request memory limit after changing refcounts. */
337+ gc_stack * segment = pemalloc (sizeof (gc_stack ), true);
337338 segment -> prev = stack ;
338339 segment -> next = NULL ;
339340 stack -> next = segment ;
@@ -371,7 +372,7 @@ static void gc_stack_free(gc_stack *stack)
371372
372373 while (p ) {
373374 stack = p -> next ;
374- efree ( p );
375+ pefree ( p , true );
375376 p = stack ;
376377 }
377378}
@@ -1943,7 +1944,7 @@ static zend_never_inline void gc_call_destructors_in_fiber(uint32_t end)
19431944 }
19441945}
19451946
1946- ZEND_API int zend_gc_collect_cycles ( void )
1947+ static int zend_gc_collect_cycles_impl ( gc_stack * stack )
19471948{
19481949 int total_count = 0 ;
19491950 bool should_rerun_gc = 0 ;
@@ -1961,10 +1962,6 @@ ZEND_API int zend_gc_collect_cycles(void)
19611962 zend_refcounted * p ;
19621963 uint32_t gc_flags = 0 ;
19631964 uint32_t idx , end ;
1964- gc_stack stack ;
1965-
1966- stack .prev = NULL ;
1967- stack .next = NULL ;
19681965
19691966 if (GC_G (gc_active )) {
19701967 GC_G (collector_time ) += zend_hrtime () - start_time ;
@@ -1976,17 +1973,16 @@ ZEND_API int zend_gc_collect_cycles(void)
19761973 GC_G (gc_active ) = 1 ;
19771974
19781975 GC_TRACE ("Marking roots" );
1979- gc_mark_roots (& stack );
1976+ gc_mark_roots (stack );
19801977 GC_TRACE ("Scanning roots" );
1981- gc_scan_roots (& stack );
1978+ gc_scan_roots (stack );
19821979
19831980 GC_TRACE ("Collecting roots" );
1984- count = gc_collect_roots (& gc_flags , & stack );
1981+ count = gc_collect_roots (& gc_flags , stack );
19851982
19861983 if (!GC_G (num_roots )) {
19871984 /* nothing to free */
19881985 GC_TRACE ("Nothing to free" );
1989- gc_stack_free (& stack );
19901986 GC_G (gc_active ) = 0 ;
19911987 goto finish ;
19921988 }
@@ -2035,7 +2031,7 @@ ZEND_API int zend_gc_collect_cycles(void)
20352031 while (idx != end ) {
20362032 if (GC_IS_DTOR_GARBAGE (current -> ref )) {
20372033 p = GC_GET_PTR (current -> ref );
2038- count -= gc_remove_nested_data_from_buffer (p , current , & stack );
2034+ count -= gc_remove_nested_data_from_buffer (p , current , stack );
20392035 }
20402036 current ++ ;
20412037 idx ++ ;
@@ -2058,8 +2054,6 @@ ZEND_API int zend_gc_collect_cycles(void)
20582054 }
20592055 }
20602056
2061- gc_stack_free (& stack );
2062-
20632057 /* Destroy zvals. The root buffer may be reallocated. */
20642058 GC_TRACE ("Destroying zvals" );
20652059 zend_hrtime_t free_start_time = zend_hrtime ();
@@ -2144,6 +2138,32 @@ ZEND_API int zend_gc_collect_cycles(void)
21442138 return total_count ;
21452139}
21462140
2141+ static int zend_gc_collect_cycles_with_stack (gc_stack * stack )
2142+ {
2143+ int count ;
2144+
2145+ zend_try {
2146+ count = zend_gc_collect_cycles_impl (stack );
2147+ } zend_catch {
2148+ gc_stack_free (stack );
2149+ zend_bailout ();
2150+ } zend_end_try ();
2151+
2152+ gc_stack_free (stack );
2153+ return count ;
2154+ }
2155+
2156+ ZEND_API int zend_gc_collect_cycles (void )
2157+ {
2158+ /* Keep the modified stack outside the function containing setjmp(), so it remains
2159+ * valid when freeing traversal storage after a bailout. */
2160+ gc_stack stack ;
2161+
2162+ stack .prev = NULL ;
2163+ stack .next = NULL ;
2164+ return zend_gc_collect_cycles_with_stack (& stack );
2165+ }
2166+
21472167ZEND_API void zend_gc_get_status (zend_gc_status * status )
21482168{
21492169 status -> active = GC_G (gc_active );
0 commit comments