[PATCH v4 2/2] malloc: Add tst-arena-rnd, a test of the randomized arena alignment

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Mon Nov 9 19:38:51 GMT 2026



On 28/10/26 12:12, Hector Marco-Gisbert wrote:
> From: Hector Marco-Gisbert <hecmargi@upv.es>
> 
> Check the properties of the randomized alignment of the thread heaps
> [BZ #22853] without measuring the entropy of the kernel address space
> randomization: blocks allocated and freed from several threads are
> handled correctly and all the heaps of a process share the residue; the
> residue is a multiple of the page size (of the huge page size with
> glibc.malloc.hugetlb=2) and differs between processes; and when address
> space randomization has been disabled for the process
> (ADDR_NO_RANDOMIZE) the heaps are aligned to HEAP_MAX_SIZE as before.

This is much better than the 2020 test.

> 
> Every thread performs the same sequence of allocations in a new heap, so
> the block it keeps is at the same offset from the base of its heap in
> every thread and in every process, and the differences between those
> blocks modulo HEAP_MAX_SIZE are the differences between the residues.
> The test re-executes itself through the command that runs the tests,
> which it receives as arguments, to observe 16 processes; the probability
> that they all draw the same residue is at most 2^-120 (8-bit residues,
> 32-bit targets).  The test runs in the hugetlb and largetcache variants
> of malloc/ and is excluded from the malloc-check, mcheck and threaded
> variants.
> 
> Against the unpatched library the test fails: the 16 processes report
> the same offset.
> 
> Signed-off-by: Hector Marco-Gisbert <hecmargi@upv.es>
> Signed-off-by: Ismael Ripoll <iripoll@disca.upv.es>
> ---
>  malloc/Makefile        |  12 ++
>  malloc/tst-arena-rnd.c | 314 +++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 326 insertions(+)
>  create mode 100644 malloc/tst-arena-rnd.c
> 
> diff --git a/malloc/Makefile b/malloc/Makefile
> index 7b704f5191..3b410b50ff 100644
> --- a/malloc/Makefile
> +++ b/malloc/Makefile
> @@ -31,6 +31,7 @@ tests := \
>    tst-aligned-alloc-random-thread \
>    tst-aligned-alloc-random-thread-cross \
>    tst-alloc_buffer \
> +  tst-arena-rnd \
>    tst-calloc \
>    tst-free-aligned-sized \
>    tst-free-aligned-sized-trace \
> @@ -110,6 +111,7 @@ test-srcs = tst-mtrace
>  # These tests either are run with MALLOC_CHECK_=3 by default or do not work
>  # with MALLOC_CHECK_=3 because they expect a specific failure.
>  tests-exclude-malloc-check = \
> +  tst-arena-rnd \
>    tst-compathooks-off \
>    tst-compathooks-on \
>    tst-malloc-check \
> @@ -178,6 +180,7 @@ ifeq ($(have-GLIBC_2.23)$(build-shared),yesyes)
>  tests-exclude-mcheck = \
>    tst-aligned-alloc-random-thread \
>    tst-aligned-alloc-random-thread-cross \
> +  tst-arena-rnd \
>    tst-compathooks-off \
>    tst-compathooks-on \
>    tst-malloc-backtrace \
> @@ -312,6 +315,7 @@ $(tests-link-with-libpthread:%=$(objpfx)%-malloc-largetcache): \
>  tests-exclude-threaded = \
>    $(tests-link-with-libpthread) \
>    $(tests-static) \
> +  tst-arena-rnd \
>    tst-compathooks-on \
>    tst-interpose-nothread \
>    tst-mallocfork \
> @@ -337,6 +341,14 @@ tests-exclude-hugetlb1 += $(tests-threaded-main) $(tests-threaded-worker)
>  tests-exclude-hugetlb2 += $(tests-threaded-main) $(tests-threaded-worker)
>  tests-exclude-largetcache += $(tests-threaded-main) $(tests-threaded-worker)
>  
> +# tst-arena-rnd re-executes itself through the command that runs the
> +# tests, which it receives as arguments; the tunable variants need them
> +# too, and the threaded variants, which do not get them, are excluded.
> +tst-arena-rnd-ARGS = -- $(host-test-program-cmd)
> +tst-arena-rnd-malloc-hugetlb1-ARGS = -- $(host-test-program-cmd)
> +tst-arena-rnd-malloc-hugetlb2-ARGS = -- $(host-test-program-cmd)
> +tst-arena-rnd-malloc-largetcache-ARGS = -- $(host-test-program-cmd)
> +
>  # These should be removed by `make clean'.
>  extra-objs = mcheck-init.o libmcheck.a
>  others-extras = mcheck-init.o
> diff --git a/malloc/tst-arena-rnd.c b/malloc/tst-arena-rnd.c
> new file mode 100644
> index 0000000000..1a8d5d05f5
> --- /dev/null
> +++ b/malloc/tst-arena-rnd.c
> @@ -0,0 +1,314 @@
> +/* Test the randomized alignment of the thread heaps.
> +   Copyright (C) 2026 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.

Since this is DCO, this should be "Copyright The GNU Toolchain
Authors." (see the Contribution checklist).

> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +/* The heaps of the non-main arenas are reservations of HEAP_MAX_SIZE
> +   bytes whose base address is congruent to a secret, per-process
> +   residue modulo HEAP_MAX_SIZE (arena_rnd in arena.c), instead of being
> +   aligned to HEAP_MAX_SIZE.  This test checks the properties that the
> +   allocator guarantees, without measuring the entropy of the kernel
> +   address space randomization:

If the personality check goes away, as I suggest in 1/2, this
property, the aslr_disabled branch and the ADDR_NO_RANDOMIZE child at
the end all go away too.

> +
> +   - blocks allocated and freed from several threads are handled
> +     correctly, and all the heaps of a process share the residue;
> +   - the residue is a multiple of the page size (of the huge page size
> +     with glibc.malloc.hugetlb=2) and differs between processes;
> +   - when address space randomization is disabled for the process
> +     (ADDR_NO_RANDOMIZE personality, as set by setarch -R or by a
> +     debugger), the residue is zero and the heaps are aligned to
> +     HEAP_MAX_SIZE, as without the randomization.
> +
> +   Every thread performs the same sequence of allocations in a heap of
> +   its own (M_ARENA_MAX is raised so that no thread shares an arena or
> +   uses the main arena), so the block it keeps is at the same offset
> +   from the base of its heap in every thread and in every process: the
> +   differences between those blocks, modulo HEAP_MAX_SIZE, are the
> +   differences between the residues.  The test re-executes itself
> +   (--restart) to observe several processes.  */

The test only ever sees the *first* heap of each arena, and no heap is
ever deleted (a child never unmaps a 64 MiB heap; thread exit does not
delete heaps).  So the code paths the series changes besides
heap_for_ptr are not exercised: aligned_heap_area reuse, grow_heap and
the heap_trim deletion path with its changed assert.  

Could you add a thread that grows its arena past HEAP_MAX_SIZE?  For
example, raise M_MMAP_THRESHOLD and allocate HEAP_MAX_SIZE / 16 blocks
until the arena has three heaps.  Then check that every heap starts at
the same residue (the first block of a new heap is at a fixed offset
from its base, or the heaps can be found in /proc/self/maps), and
free everything in reverse order so heap_trim deletes the heaps.

Also check with hugetlb=1, if you don't have access to an aarch64 box
(and also maybe add a explicit test for this case).


> +
> +#include <getopt.h>
> +#include <inttypes.h>
> +#include <malloc.h>
> +#include <stdbool.h>
> +#include <stdint.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <unistd.h>
> +#include <support/capture_subprocess.h>
> +#include <support/check.h>
> +#include <support/support.h>
> +#include <support/test-driver.h>
> +#include <support/xstdio.h>
> +#include <support/xthread.h>
> +#ifdef __linux__
> +# include <sys/personality.h>
> +#endif
> +
> +/* HEAP_MAX_SIZE of arena.c: twice DEFAULT_MMAP_THRESHOLD_MAX of
> +   malloc.c.  */
> +#if __WORDSIZE == 32
> +# define HEAP_MAX_SIZE (2 * 512 * 1024)
> +#else
> +# define HEAP_MAX_SIZE (2 * 4 * 1024 * 1024 * sizeof (long))
> +#endif
> +/* HEAP_MIN_SIZE of arena.c: the block kept by each thread is allocated
> +   from the first HEAP_MIN_SIZE bytes of its heap.  */
> +#define HEAP_MIN_SIZE (32 * 1024)
> +
> +/* Threads, and therefore heaps, observed in each process.  */
> +#define NTHREADS 4
> +/* Blocks allocated by each thread.  */
> +#define NBLOCKS 64
> +/* Processes observed.  With the smallest residue (8 bits, 32-bit
> +   targets) the probability that 16 independent processes draw the same
> +   residue is 2^-120.  */

That is not the smallest residue, in the hugetlb=2 variant the residue
is a multiple of the huge page size.  With 2 MiB pages and a 64 MiB
HEAP_MAX_SIZE that is 5 bits and on a 16 KiB page AArch64 kernel, wit
32 MiB huge pages, it is 1 bit, so the test would fail with probability
2^-15 there. 

Maybe skip the distinct check, or require fewer distinct residues, when
the residue has few bits.

> +#define NRUNS 16
> +
> +static int restart;
> +#define CMDLINE_OPTIONS \
> +  { "restart", no_argument, &restart, 1 },
> +
> +struct thread_data
> +{
> +  pthread_barrier_t *barrier;
> +  void *block;
> +};
> +
> +/* Allocate blocks of several sizes, free half of them, keep one until
> +   the main thread has recorded it, then free the rest.  Every free ()
> +   resolves the heap of the block with heap_for_ptr.  The sequence is
> +   the same in every thread.  */
> +static void *
> +thread_func (void *closure)
> +{
> +  struct thread_data *data = closure;
> +  void *blocks[NBLOCKS];
> +  for (int i = 0; i < NBLOCKS; ++i)
> +    {
> +      size_t size = 16 + (i * 61) % 4096;
> +      blocks[i] = xmalloc (size);
> +      memset (blocks[i], i, size);
> +    }
> +  for (int i = 0; i < NBLOCKS; i += 2)
> +    free (blocks[i]);
> +  data->block = blocks[1];
> +  xpthread_barrier_wait (data->barrier);
> +  xpthread_barrier_wait (data->barrier);
> +  for (int i = 1; i < NBLOCKS; i += 2)
> +    free (blocks[i]);
> +  return NULL;
> +}
> +
> +/* The alignment of the residue: the page size, or the huge page size
> +   when the heaps are mapped with MAP_HUGETLB (glibc.malloc.hugetlb=2),
> +   which requires it.  arena.c maps the heaps with huge pages only when
> +   a huge page fits in a heap (the huge page size is at most
> +   HEAP_MAX_SIZE); otherwise, as on 32-bit targets with 2 MiB huge
> +   pages, the heaps stay on normal pages and so does the residue.  */
> +static uintptr_t
> +residue_alignment (void)
> +{
> +  uintptr_t pagesize = sysconf (_SC_PAGESIZE);
> +  const char *tunables = getenv ("GLIBC_TUNABLES");
> +  const char *key = "glibc.malloc.hugetlb=";
> +  const char *p = tunables != NULL ? strstr (tunables, key) : NULL;
> +  if (p == NULL)
> +    return pagesize;
> +  p += strlen (key);
> +  if (p[0] != '2' || (p[1] != '\0' && p[1] != ':'))
> +    return pagesize;
> +  FILE *f = xfopen ("/proc/meminfo", "r");
> +  char *line = NULL;
> +  size_t line_len = 0;
> +  uintptr_t hugepagesize = 0;
> +  while (xgetline (&line, &line_len, f) > 0)
> +    {
> +      unsigned long int kb;
> +      if (sscanf (line, "Hugepagesize: %lu kB", &kb) == 1)
> +	{
> +	  hugepagesize = (uintptr_t) kb * 1024;
> +	  break;
> +	}
> +    }
> +  free (line);
> +  xfclose (f);
> +  if (hugepagesize == 0)
> +    FAIL_UNSUPPORTED ("Hugepagesize not found in /proc/meminfo");
> +  if (hugepagesize > HEAP_MAX_SIZE)
> +    return pagesize;
> +  return hugepagesize;
> +}
> +
> +/* Create NTHREADS threads, check that their blocks are at the same
> +   offset modulo HEAP_MAX_SIZE, and print that offset for the parent
> +   process.  */
> +static int
> +observe_heaps (void)
> +{
> +  /* Every thread must get a heap of its own: the default limit on the
> +     number of arenas depends on the number of processors, and when it
> +     is reached threads share an arena or use the main arena, whose
> +     blocks are not at the same offset.  */
> +  TEST_VERIFY_EXIT (mallopt (M_ARENA_MAX, 2 * NTHREADS + 1) == 1);
> +
> +  pthread_barrier_t barrier;
> +  xpthread_barrier_init (&barrier, NULL, NTHREADS + 1);
> +  struct thread_data data[NTHREADS];
> +  pthread_t threads[NTHREADS];
> +  for (int i = 0; i < NTHREADS; ++i)
> +    {
> +      data[i].barrier = &barrier;
> +      data[i].block = NULL;
> +      threads[i] = xpthread_create (NULL, thread_func, &data[i]);
> +    }
> +  xpthread_barrier_wait (&barrier);
> +
> +  uintptr_t offset = (uintptr_t) data[0].block & (HEAP_MAX_SIZE - 1);
> +  for (int i = 0; i < NTHREADS; ++i)
> +    {
> +      if (test_verbose)
> +	printf ("block %d: %p\n", i, data[i].block);
> +      /* All the heaps of the process share the residue.  */
> +      TEST_COMPARE ((uintptr_t) data[i].block & (HEAP_MAX_SIZE - 1),
> +		    offset);
> +    }
> +
> +  xpthread_barrier_wait (&barrier);
> +  for (int i = 0; i < NTHREADS; ++i)
> +    xpthread_join (threads[i]);
> +  xpthread_barrier_destroy (&barrier);
> +
> +  printf ("%" PRIxPTR "\n", offset);
> +  return 0;
> +}
> +
> +/* Run this program again with --restart and return the offset it
> +   prints.  */
> +static uintptr_t
> +observe_child (char *const argv[])
> +{
> +  struct support_capture_subprocess result
> +    = support_capture_subprogram (argv[0], argv, NULL);
> +  support_capture_subprocess_check (&result, "tst-arena-rnd --restart", 0,
> +				    sc_allow_stdout);
> +  uintptr_t offset;
> +  if (sscanf (result.out.buffer, "%" SCNxPTR, &offset) != 1)
> +    FAIL_EXIT1 ("unexpected output of the child: %s", result.out.buffer);
> +  support_capture_subprocess_free (&result);
> +  return offset;
> +}
> +
> +static int
> +do_test (int argc, char **argv)
> +{
> +  if (restart)
> +    return observe_heaps ();
> +
> +  /* The arguments are the command that runs the test program: the
> +     program itself, or the dynamic loader, --library-path, the library
> +     path and the program (tst-arena-rnd-ARGS in the Makefile).  */
> +  TEST_VERIFY_EXIT (argc >= 2);
> +  char *spargv[argc + 2];
> +  int i;
> +  for (i = 0; i < argc - 1; ++i)
> +    spargv[i] = argv[i + 1];
> +  spargv[i++] = (char *) "--direct";
> +  spargv[i++] = (char *) "--restart";
> +  spargv[i] = NULL;

Use support_spawn_wrapped here, it is slight simpler than constructing the
arguments on each call.

> +
> +  bool aslr_disabled = false;
> +#ifdef __linux__
> +  /* On failure personality returns the negated error code instead of
> +     -1 (sysdeps/unix/sysv/linux/personality.c), for example when the
> +     system call is filtered by seccomp in a container.  */
> +  int persona = personality (0xffffffff);
> +  if (persona < 0)
> +    persona = -1;
> +  aslr_disabled = persona != -1 && (persona & ADDR_NO_RANDOMIZE) != 0;
> +#endif

I dont think we should care for this scenario.

> +
> +  uintptr_t alignment = residue_alignment ();
> +  uintptr_t offsets[NRUNS];
> +  int distinct = 0;
> +  for (int run = 0; run < NRUNS; ++run)
> +    {
> +      offsets[run] = observe_child (spargv);
> +      if (test_verbose)
> +	printf ("process %d: offset %#" PRIxPTR "\n", run, offsets[run]);
> +      /* The residues of two processes differ by a multiple of the
> +	 alignment.  */
> +      TEST_COMPARE ((offsets[run] - offsets[0]) & (alignment - 1), 0);
> +      int j;
> +      for (j = 0; j < run; ++j)
> +	if (offsets[j] == offsets[run])
> +	  break;
> +      if (j == run)
> +	++distinct;
> +    }
> +
> +  if (aslr_disabled)
> +    {
> +      /* The test itself runs without address space randomization: the
> +	 children inherit the personality and must use the original,
> +	 HEAP_MAX_SIZE aligned layout, in which the block is in the
> +	 first HEAP_MIN_SIZE bytes of the heap.  */
> +      printf ("info: ADDR_NO_RANDOMIZE is set, expecting aligned heaps\n");
> +      for (int run = 0; run < NRUNS; ++run)
> +	TEST_VERIFY (offsets[run] < HEAP_MIN_SIZE);
> +      return 0;
> +    }
> +
> +  /* The residue is chosen anew by every process.  */
> +  printf ("info: %d distinct residues in %d processes\n", distinct, NRUNS);
> +  TEST_VERIFY (distinct >= 2);
> +
> +#ifdef __linux__

Same as before, the ADDR_NO_RANDOMIZE handling is not really helping much here.

> +  /* Disable address space randomization for a child: the original
> +     layout must come back, and the difference with the other processes
> +     is their residue.  */
> +  if (persona != -1)
> +    personality (persona | ADDR_NO_RANDOMIZE);
> +  int current = persona != -1 ? personality (0xffffffff) : -1;
> +  if (current < 0 || (current & ADDR_NO_RANDOMIZE) == 0)
> +    {
> +      /* The change did not take effect, even if the call did not return
> +	 -1: the system call may be filtered.  */
> +      printf ("info: cannot set ADDR_NO_RANDOMIZE\n");
> +      if (persona != -1)
> +	personality (persona);
> +    }
> +  else
> +    {
> +      uintptr_t offset = observe_child (spargv);
> +      TEST_VERIFY (personality (persona) >= 0);
> +      if (test_verbose)
> +	printf ("process without ASLR: offset %#" PRIxPTR "\n", offset);
> +      TEST_VERIFY (offset < HEAP_MIN_SIZE);
> +      for (int run = 0; run < NRUNS; ++run)
> +	{
> +	  uintptr_t residue = (offsets[run] - offset) & (HEAP_MAX_SIZE - 1);
> +	  TEST_COMPARE (residue & (alignment - 1), 0);
> +	}
> +    }
> +#endif
> +
> +  return 0;
> +}
> +
> +#define TEST_FUNCTION_ARGV do_test
> +#include <support/test-driver.c>



More information about the Libc-alpha mailing list