Skip to content

Commit 234947f

Browse files
fix allocation profiling shutdown with USE_ZEND_ALLOC=0 crashing (#2294)
1 parent 53f7553 commit 234947f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Diff for: profiling/src/allocation.rs

+10
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ pub fn allocation_profiling_rinit() {
176176
}
177177

178178
pub fn allocation_profiling_rshutdown() {
179+
let allocation_profiling = REQUEST_LOCALS.with(|cell| {
180+
cell.try_borrow()
181+
.map(|locals| locals.profiling_allocation_enabled)
182+
.unwrap_or(false)
183+
});
184+
185+
if !allocation_profiling {
186+
return;
187+
}
188+
179189
// If `is_zend_mm()` is true, the custom handlers have been reset to `None`
180190
// already. This is unexpected, therefore we will not touch the ZendMM handlers
181191
// anymore as resetting to prev handlers might result in segfaults and other

Diff for: profiling/tests/phpt/allocation_jit_01.phpt

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
[profiling] Allocation profiling should not crash when run with `USE_ZEND_ALLOC=0`
3+
--DESCRIPTION--
4+
When running PHP with `USE_ZEND_ALLOC=0` and disabled allocation profiling
5+
(either explicit disabled or due to the JIT bug), PHP will install the system
6+
allocator in https://heap.space/xref/PHP-8.2/Zend/zend_alloc.c?r=4553258d#2895-2918
7+
and set the `AG(heap)->use_custom_heap` to `ZEND_MM_CUSTOM_HEAP_STD` (which is 1),
8+
so future calls to `is_zend_mm()` will return false which might lead to a situation
9+
where we assume we are hooked into, while we are not.
10+
--SKIPIF--
11+
<?php
12+
if (!extension_loaded('datadog-profiling'))
13+
echo "skip: test requires datadog-profiling", PHP_EOL;
14+
?>
15+
--ENV--
16+
USE_ZEND_ALLOC=0
17+
--INI--
18+
datadog.profiling.enabled=yes
19+
datadog.profiling.log_level=debug
20+
datadog.profiling.allocation_enabled=yes
21+
datadog.profiling.experimental_cpu_time_enabled=no
22+
zend_extension=opcache
23+
opcache.enable_cli=1
24+
opcache.jit=tracing
25+
opcache.jit_buffer_size=4M
26+
--FILE--
27+
<?php
28+
echo "Done.", PHP_EOL;
29+
?>
30+
--EXPECTREGEX--
31+
.*Done.
32+
.*Stopping profiler.
33+
.*

0 commit comments

Comments
 (0)