Skip to content

Commit 891be06

Browse files
add safety comments
1 parent cb9e138 commit 891be06

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

profiling/src/allocation.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,18 @@ unsafe extern "C" fn alloc_profiling_malloc(len: size_t) -> *mut c_void {
368368
ptr
369369
}
370370

371+
/// Safety: this function pointer is only allowed to point to `allocation_profiling_prev_alloc()`
372+
/// when at the same time the `ZEND_MM_STATE.prev_custom_mm_alloc` is initialised to a valid
373+
/// function pointer, otherwise there will be dragons.
371374
static mut ALLOCATION_PROFILING_ALLOC: unsafe fn(size_t) -> *mut c_void =
372375
allocation_profiling_orig_alloc;
373376

374377
unsafe fn allocation_profiling_prev_alloc(len: size_t) -> *mut c_void {
375378
ZEND_MM_STATE.with(|cell| {
376379
let zend_mm_state = cell.get();
380+
// Safety: `ALLOCATION_PROFILING_ALLOC` will be initialised in
381+
// `allocation_profiling_rinit()` and only point to this function when
382+
// `prev_custom_mm_alloc` is also initialised
377383
let prev = (*zend_mm_state).prev_custom_mm_alloc.unwrap();
378384
prev(len)
379385
})
@@ -395,11 +401,17 @@ unsafe extern "C" fn alloc_profiling_free(ptr: *mut c_void) {
395401
ALLOCATION_PROFILING_FREE(ptr);
396402
}
397403

404+
/// Safety: this function pointer is only allowed to point to `allocation_profiling_prev_free()`
405+
/// when at the same time the `ZEND_MM_STATE.prev_custom_mm_free` is initialised to a valid
406+
/// function pointer, otherwise there will be dragons.
398407
static mut ALLOCATION_PROFILING_FREE: unsafe fn(*mut c_void) = allocation_profiling_orig_free;
399408

400409
unsafe fn allocation_profiling_prev_free(ptr: *mut c_void) {
401410
ZEND_MM_STATE.with(|cell| {
402411
let zend_mm_state = cell.get();
412+
// Safety: `ALLOCATION_PROFILING_FREE` will be initialised in
413+
// `allocation_profiling_free()` and only point to this function when
414+
// `prev_custom_mm_free` is also initialised
403415
let prev = (*zend_mm_state).prev_custom_mm_free.unwrap();
404416
prev(ptr)
405417
})
@@ -430,12 +442,18 @@ unsafe extern "C" fn alloc_profiling_realloc(prev_ptr: *mut c_void, len: size_t)
430442
ptr
431443
}
432444

445+
/// Safety: this function pointer is only allowed to point to `allocation_profiling_prev_realloc()`
446+
/// when at the same time the `ZEND_MM_STATE.prev_custom_mm_realloc` is initialised to a valid
447+
/// function pointer, otherwise there will be dragons.
433448
static mut ALLOCATION_PROFILING_REALLOC: unsafe fn(*mut c_void, size_t) -> *mut c_void =
434449
allocation_profiling_orig_realloc;
435450

436451
unsafe fn allocation_profiling_prev_realloc(prev_ptr: *mut c_void, len: size_t) -> *mut c_void {
437452
ZEND_MM_STATE.with(|cell| {
438453
let zend_mm_state = cell.get();
454+
// Safety: `ALLOCATION_PROFILING_REALLOC` will be initialised in
455+
// `allocation_profiling_realloc()` and only point to this function when
456+
// `prev_custom_mm_realloc` is also initialised
439457
let prev = (*zend_mm_state).prev_custom_mm_realloc.unwrap();
440458
prev(prev_ptr, len)
441459
})

0 commit comments

Comments
 (0)