Skip to content

Commit af595b3

Browse files
handle chaotic neighbour without crashing
1 parent 155d1ca commit af595b3

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

profiling/src/allocation.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ pub fn alloc_prof_rinit() {
213213
ptr::addr_of_mut!((*zend_mm_state).realloc).write(alloc_prof_orig_realloc);
214214
ptr::addr_of_mut!((*zend_mm_state).prepare_restore_zend_heap)
215215
.write((prepare_zend_heap, restore_zend_heap));
216+
217+
// Reset previous handlers to None. There might be a chaotic neighbor that
218+
// registered custom handlers in an earlier request, but it doesn't do so for this
219+
// request. In that case we would restore the neighbouring extensions custom
220+
// handlers to the ZendMM in RSHUTDOWN which would lead to a crash!
221+
ptr::addr_of_mut!((*zend_mm_state).prev_custom_mm_alloc).write(None);
222+
ptr::addr_of_mut!((*zend_mm_state).prev_custom_mm_free).write(None);
223+
ptr::addr_of_mut!((*zend_mm_state).prev_custom_mm_realloc).write(None);
216224
}
217225
}
218226

@@ -286,7 +294,11 @@ pub fn alloc_prof_rshutdown() {
286294
}
287295
warn!("Found another extension using the custom heap which is unexpected at this point, so the extension handle was `null`'ed to avoid being `dlclose()`'ed.");
288296
} else {
289-
// This is the happy path (restore previously installed custom handlers)!
297+
// This is the happy path. Restore previously installed custom handlers or
298+
// NULL-pointers to the ZendMM. In case all pointers are NULL, the ZendMM will reset
299+
// the `use_custom_heap` flag to `None`, in case we restore a neighbouring extensions
300+
// custom handlers, ZendMM will call that for future allocations. In either way, we
301+
// have unregistered and we'll not receive any allocation calls anymore.
290302
unsafe {
291303
zend::ddog_php_prof_zend_mm_set_custom_handlers(
292304
heap,

0 commit comments

Comments
 (0)