Skip to content

Commit 1653fa2

Browse files
cleanup more code and leave some safety comments
1 parent 394560d commit 1653fa2

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

profiling/src/allocation/allocation_ge84.rs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub fn alloc_prof_rinit() {
164164

165165
zend_mm_state.heap = Some(heap);
166166

167-
if !is_zend_mm() {
167+
if unsafe { !zend::is_zend_mm() } {
168168
// Neighboring custom memory handlers found
169169
debug!("Found another extension using the ZendMM custom handler hook");
170170
unsafe {
@@ -218,7 +218,7 @@ pub fn alloc_prof_rinit() {
218218
tls_zend_mm_state_set!(zend_mm_state_init(mm_state));
219219

220220
// `is_zend_mm()` should be false now, as we installed our custom handlers
221-
if is_zend_mm() {
221+
if unsafe { zend::is_zend_mm() } {
222222
// Can't proceed with it being disabled, because that's a system-wide
223223
// setting, not per-request.
224224
panic!("Memory allocation profiling could not be enabled. Please feel free to fill an issue stating the PHP version and installed modules. Most likely the reason is your PHP binary was compiled with `ZEND_MM_CUSTOM` being disabled.");
@@ -232,7 +232,7 @@ pub fn alloc_prof_rshutdown() {
232232
// to `None`. This is unexpected, therefore we will not touch the ZendMM
233233
// handlers anymore as resetting to prev handlers might result in segfaults
234234
// and other undefined behavior.
235-
if is_zend_mm() {
235+
if unsafe { zend::is_zend_mm() } {
236236
return;
237237
}
238238

@@ -355,7 +355,9 @@ unsafe fn alloc_prof_prev_alloc(len: size_t) -> *mut c_void {
355355
}
356356

357357
unsafe fn alloc_prof_orig_alloc(len: size_t) -> *mut c_void {
358-
let heap = zend::zend_mm_get_heap();
358+
// Safety: `ZEND_MM_STATE.heap` will be initialised in `alloc_prof_rinit()` and custom ZendMM
359+
// handlers are only installed and pointing to this function if initialization was succesful.
360+
let heap = tls_zend_mm_state_get!(heap).unwrap_unchecked();
359361
zend::_zend_mm_alloc(heap, len)
360362
}
361363

@@ -376,7 +378,9 @@ unsafe fn alloc_prof_prev_free(ptr: *mut c_void) {
376378
}
377379

378380
unsafe fn alloc_prof_orig_free(ptr: *mut c_void) {
379-
let heap = zend::zend_mm_get_heap();
381+
// Safety: `ZEND_MM_STATE.heap` will be initialised in `alloc_prof_rinit()` and custom ZendMM
382+
// handlers are only installed and pointing to this function if initialization was succesful.
383+
let heap = tls_zend_mm_state_get!(heap).unwrap_unchecked();
380384
zend::_zend_mm_free(heap, ptr);
381385
}
382386

@@ -410,7 +414,9 @@ unsafe fn alloc_prof_prev_realloc(prev_ptr: *mut c_void, len: size_t) -> *mut c_
410414
}
411415

412416
unsafe fn alloc_prof_orig_realloc(prev_ptr: *mut c_void, len: size_t) -> *mut c_void {
413-
let heap = zend::zend_mm_get_heap();
417+
// Safety: `ZEND_MM_STATE.heap` will be initialised in `alloc_prof_rinit()` and custom ZendMM
418+
// handlers are only installed and pointing to this function if initialization was succesful.
419+
let heap = tls_zend_mm_state_get!(heap).unwrap_unchecked();
414420
zend::_zend_mm_realloc(heap, prev_ptr, len)
415421
}
416422

@@ -426,7 +432,9 @@ unsafe fn alloc_prof_prev_gc() -> size_t {
426432
}
427433

428434
unsafe fn alloc_prof_orig_gc() -> size_t {
429-
let heap = tls_zend_mm_state_get!(heap).unwrap();
435+
// Safety: `ZEND_MM_STATE.heap` will be initialised in `alloc_prof_rinit()` and custom ZendMM
436+
// handlers are only installed and pointing to this function if initialization was succesful.
437+
let heap = tls_zend_mm_state_get!(heap).unwrap_unchecked();
430438
let custom_heap = prepare_zend_heap(heap);
431439
let size = zend::zend_mm_gc(heap);
432440
restore_zend_heap(heap, custom_heap);
@@ -438,34 +446,21 @@ unsafe extern "C" fn alloc_prof_shutdown(full: bool, silent: bool) {
438446
}
439447

440448
unsafe fn alloc_prof_prev_shutdown(full: bool, silent: bool) {
441-
// Safety: `ZEND_MM_STATE.prev_custom_mm_shutdown` will be initialised in
442-
// `alloc_prof_rinit()` and only point to this function when
443-
// `prev_custom_mm_shutdown` is also initialised
444-
let shutdown = tls_zend_mm_state_get!(prev_custom_mm_shutdown).unwrap();
445-
shutdown(full, silent)
449+
match tls_zend_mm_state_get!(prev_custom_mm_shutdown) {
450+
Some(shutdown) => shutdown(full, silent),
451+
None => (),
452+
}
446453
}
447454

448455
unsafe fn alloc_prof_orig_shutdown(full: bool, silent: bool) {
456+
// Safety: `ZEND_MM_STATE.heap` will be initialised in `alloc_prof_rinit()` and custom ZendMM
457+
// handlers are only installed and pointing to this function if initialization was succesful.
449458
let heap = tls_zend_mm_state_get!(heap).unwrap_unchecked();
450459
let custom_heap = prepare_zend_heap(heap);
451460
zend::zend_mm_shutdown(heap, full, silent);
452461
restore_zend_heap(heap, custom_heap);
453462
}
454463

455-
/// safe wrapper for `zend::is_zend_mm()`.
456-
/// `true` means the internal ZendMM is being used, `false` means that a custom memory manager is
457-
/// installed. Upstream returns a `c_bool` as of PHP 8.0. PHP 7 returns a `c_int`
458-
fn is_zend_mm() -> bool {
459-
#[cfg(php7)]
460-
unsafe {
461-
zend::is_zend_mm() == 1
462-
}
463-
#[cfg(php8)]
464-
unsafe {
465-
zend::is_zend_mm()
466-
}
467-
}
468-
469464
#[cfg(test)]
470465
mod tests {
471466
use super::*;

0 commit comments

Comments
 (0)