Skip to content

Commit 22eefef

Browse files
fix(profiling): preserve allocation prefix alignment
1 parent 1ab36df commit 22eefef

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

profiling/src/allocation/allocation_ge84.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use libc::{c_char, c_uint};
1414
#[cfg(feature = "debug_stats")]
1515
use crate::allocation::{ALLOCATION_PROFILING_COUNT, ALLOCATION_PROFILING_SIZE};
1616

17-
const PREFIX_SIZE: usize = core::mem::size_of::<usize>();
17+
// Preserve the alignment guaranteed by the allocator we wrap.
18+
const PREFIX_SIZE: usize = core::mem::align_of::<libc::max_align_t>();
1819
const PREFIX_MAGIC: usize = 0xdd0f_cafe;
1920

2021
#[derive(Copy, Clone)]
@@ -662,6 +663,16 @@ unsafe fn alloc_prof_orig_shutdown(full: bool, silent: bool) {
662663
mod tests {
663664
use super::*;
664665

666+
#[test]
667+
fn prefix_preserves_allocator_alignment() {
668+
let base = unsafe { libc::malloc(PREFIX_SIZE + 1) };
669+
let ptr = unsafe { add_prefix(base) };
670+
671+
assert_eq!(ptr as usize % PREFIX_SIZE, 0);
672+
assert_eq!(unsafe { remove_prefix(ptr) }, base);
673+
unsafe { libc::free(base) };
674+
}
675+
665676
#[test]
666677
fn free_handler_tracks_only_when_heap_live_is_enabled() {
667678
assert_eq!(

profiling/tests/phpt/heap_live_prefix_epoch.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
[profiling] heap-live prefix survives object-store and string reallocations
2+
[profiling] heap-live prefix preserves allocations and alignment
33
--SKIPIF--
44
<?php
55
if (PHP_VERSION_ID < 80400)
@@ -29,8 +29,11 @@ for ($i = 0; $i < 32; $i++) {
2929
}
3030

3131
unset($objects, $string);
32+
33+
echo hash('xxh3', 'aligned', options: ['seed' => 42]), "\n";
3234
echo "Done.\n";
3335

3436
?>
3537
--EXPECT--
38+
f2d7e898c1df340a
3639
Done.

0 commit comments

Comments
 (0)