[PATCH v4 1/1] malloc: aarch64: Add ifuncs for malloc functions

Yury Khrustalev yury.khrustalev@arm.com
Thu Jun 4 11:21:14 GMT 2026


On Wed, Jun 03, 2026 at 10:46:16AM -0300, Adhemerval Zanella Netto wrote:
> 
> On 01/06/26 07:30, Yury Khrustalev wrote:
> > Introduce ifuncs and resolvers for functions pertinent to the
> > malloc interface on the AArch64 target: malloc, calloc, free,
> > realloc, memalign, valloc, pvalloc, posix_memalign, aligned_alloc,
> > free_sized, free_aligned_sized, malloc_usable_size.
> > 
> > A target can define the USE_MULTIARCH_MALLOC macro. In this case
> > it must provide alternative aliases for the malloc functions that
> > point to the ifuncs.
> > 
> > This implementation respects the --disable-multi-arch configure
> > flag. If multi-arch support is disabled, the generic aliases
> > are used on aarch64.
> > 
> > This patch contains aarch64-specific resolvers. At this moment they
> > return core implementations but in the future they can be changed
> > to support for features, e.g. to handle memory tagging.
> 
> The patch looks ok, some comments below. I think the only missing piece 
> is to check if external malloc-intercepting tools like valgrind, ASan/TSan, 
> heap profilers, and gdb's inferior call malloc(...) handle the ifunc change
> (as we discussed on the weekly call).

I've checked it with gdb and lldb as well as with valgrind memcheck and
massif tools, address, hwadderess, thread and leak sanitizers and also
using the heaptrack heap profiler. The output before and after looks the
same except for 2 things I describe below. The heap usage, memory leaks,
and out of buffer accesses are reported in the same way before and after
the change.

One difference in the valgrind output is that it detects that the malloc
functions are ifuncs now and you see

  REDIR: 0x4958810 (libc.so.6:aligned_alloc) redirected to 0x48507cc (_vgnU_ifunc_wrapper)
  REDIR: 0x4956c84 (libc.so.6:__aligned_alloc) redirected to 0x488d708 (aligned_alloc)

instead of

  REDIR: 0x4956c84 (libc.so.6:aligned_alloc) redirected to 0x488d708 (aligned_alloc)

This is expected I guess, and it doesn't affect valgrind's operation.

The second thing in the debuggers and in thread stacks in sanitizers the
malloc functions appear as "__lib_malloc" instead of "malloc" (for example)
which is also expected as a tool would show the symbol that the corresponding
ifunc resolves to. When we have "__libc_malloc" and "__libc_malloc_mte", it
would show the appropriate symbol which is how it should be, I think.

So, I think that this patch should not introduce any issues for sanitizers,
profiling and debugging tools.

> 
> ...
>
> > +weak_alias (__free_sized, free_sized)
> > +weak_alias (__free_aligned_sized, free_aligned_sized)
> > +#endif /* !USE_MULTIARCH_MALLOC */
> >  
> >  weak_alias (__malloc_info, malloc_info)
> >  weak_alias (__libc_mallinfo, mallinfo)
> 
> We discussed this privately; the cfree bypasses the redirect: compat_symbol (..., cfree, GLIBC_2_0)
> is kept outside the USE_MULTIARCH_MALLOC guard.
> 
> It does not matter for this patch, because aarch64 does not support multiple, 
> potentially incompatible malloc implementations.
> 
> But it might become an issue (though highly unlikely, since cfree will be present 
> only in older binaries). Maybe add a route it through __libc_free_redirect too.

Yes, I will fix it in the next version of the patch.

> 
> ...
>
> > diff --git a/sysdeps/aarch64/multiarch/malloc-ifuncs.h b/sysdeps/aarch64/multiarch/malloc-ifuncs.h
> > new file mode 100644
> > index 0000000000..98e913d49c
> > --- /dev/null
> > +++ b/sysdeps/aarch64/multiarch/malloc-ifuncs.h
> > +
>
> ...
>
> > +#define USE_MULTIARCH_MALLOC 1
> > +
> > +#include <stddef.h>
> > +#include <sys/cdefs.h>
> > +
> > +/* Core implementations of malloc functions.  An ifunc resolver must
> > +   use this implementations as a fallback option.  Other implementations
> 
> s/this/these
> 
> > +   may internally call these core function.  */
> 
> s/function/functions
> 

OK

> ...
>
> > diff --git a/sysdeps/generic/malloc-ifuncs.h b/sysdeps/generic/malloc-ifuncs.h
> > new file mode 100644
> > index 0000000000..aedc56ae33
> > --- /dev/null
> > +++ b/sysdeps/generic/malloc-ifuncs.h
> 
> ...
>
> This header is already included iff USE_MULTIARCH is defined:
> 
>  27 #if defined(USE_MULTIARCH)
>  28 # include <malloc-ifuncs.h>
>  29 #endif
>  30
> 

OK

> > +
> > +#endif /* GENERIC_MALLOC_IFUNCS_H */
> 

Thanks,
Yury



More information about the Libc-alpha mailing list