[RFC] Supporting malloc_usable_size
Wilco Dijkstra
Wilco.Dijkstra@arm.com
Fri Dec 2 13:54:18 GMT 2022
Hi Siddhesh,
> Not with the current malloc implementation I suppose but I get what you
> mean. Florian had mentioned a similar caveat where a malloc
> implementation could coalesce adjacent free blocks with the end of an
> allocated block and change the value of malloc_usable_size at any
> arbitrary point in time too.
A different allocator might keep track of separate values for requested and
allocated block sizes. Then it could reduce the allocated block size and thus
malloc_usable_size. So unless we clearly specify the guarantees, one has to
assume the malloc_usable_size is unsafe to be used.
> However the man page starts with "Although the excess bytes can be
> overwritten by the application without ill effects" and maybe that
> reassurance needs to be dropped.
If we ever allow use of the extra memory returned by malloc_usable_size, the
implementation *must* be identical to performing a realloc that returns the
original pointer (as in, if we keep track of the actual size passed to malloc, or
do security stuff like pointer tagging, we must update all that in every
malloc_usable_size call).
So we could redefine malloc_usable_size in terms of being equivalent to realloc
(which will make it more complex and expensive), or say that you must never use
the extra space and make it a debug-only feature. My preference is the latter
since we already have realloc, and if we ensure it's efficient, there is no real need
for user code to ever use a non-standard interface.
Cheers,
Wilco
More information about the Libc-alpha
mailing list