[PATCH v4 1/2] malloc: Randomize the alignment of the thread heaps [BZ #22853]
Wilco Dijkstra
Wilco.Dijkstra@arm.com
Tue Nov 10 20:22:53 GMT 2026
Hi Hector,
+ if (mp_.hp_pagesize > pagesize && mp_.hp_pagesize <= heap_max_size ())
+ pagesize = mp_.hp_pagesize;
+ arena_rnd = rnd & (heap_max_size () - 1) & ~(pagesize - 1);
+ }
I think you need to do the same for THP here to avoid the issues Adhemerval
mentioned. However I am wondering why we can't do something simpler by
changing the allocation starting point within the heap like I proposed for mmap.
Eg. starting randomly within the first 4KB page gives 8 random bits. You could
skip a larger size and after a while put this initial skipped chunk on the free list
or use it to hold internal allocator data like tcache.
static __always_inline heap_info *
heap_for_ptr (void *ptr)
{
size_t max_size = heap_max_size ();
- return PTR_ALIGN_DOWN (ptr, max_size);
+ return (heap_info *) ((((uintptr_t) ptr - arena_rnd) & ~(max_size - 1))
+ | arena_rnd);
}
If we do this, it might be better to design a more general way to find the heap/arena
(something on our wish list for a while). There are plenty bits free in the chunk header
to store an index into an array of descriptors or (for 64-bit SIZE_SZ) the offset to the
header. This is non-trivial, but it would remove all restrictions on heap placement and
size (and even allow them to grow), plus get ASLR for free.
Cheers,
Wilco
More information about the Libc-alpha
mailing list