Skip to content

Commit

Permalink
handle chaotic neighbour without crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
realFlowControl committed Mar 20, 2024
1 parent 155d1ca commit 81fa090
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion profiling/src/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ pub fn alloc_prof_rinit() {
ptr::addr_of_mut!((*zend_mm_state).realloc).write(alloc_prof_orig_realloc);
ptr::addr_of_mut!((*zend_mm_state).prepare_restore_zend_heap)
.write((prepare_zend_heap, restore_zend_heap));

// Reset previous handlers to None. There might be a chaotic neighbor that
// registered custom handlers in an earlier request, but it doesn't do so for this
// request. In that case we would restore the neighbouring extensions custom
// handlers to the ZendMM in RSHUTDOWN which would lead to a crash!
ptr::addr_of_mut!((*zend_mm_state).prev_custom_mm_alloc).write(None);
ptr::addr_of_mut!((*zend_mm_state).prev_custom_mm_free).write(None);
ptr::addr_of_mut!((*zend_mm_state).prev_custom_mm_realloc).write(None);
}
}

Expand Down Expand Up @@ -286,7 +294,11 @@ pub fn alloc_prof_rshutdown() {
}
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.");
} else {
// This is the happy path (restore previously installed custom handlers)!
// This is the happy path. Restore previously installed custom handlers or
// NULL-pointers to the ZendMM. In case all pointers are NULL, the ZendMM will reset
// the `use_custom_heap` flag to `None`, in case we restore a neighbouring extension
// custom handlers, ZendMM will call those for future allocations. In either way, we
// have unregistered and we'll not receive any allocation calls anymore.
unsafe {
zend::ddog_php_prof_zend_mm_set_custom_handlers(
heap,
Expand Down

0 comments on commit 81fa090

Please sign in to comment.