All of lore.kernel.org
 help / color / mirror / Atom feed
From: Willy Tarreau <w@1wt.eu>
To: "Thomas Weißschuh" <linux@weissschuh.net>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/3] selftests/nolibc: test the memory allocator
Date: Sat, 4 Apr 2026 10:51:55 +0200	[thread overview]
Message-ID: <adDRK8D6YBZgv36H@1wt.eu> (raw)
In-Reply-To: <20260401-nolibc-asprintf-v1-2-46292313439f@weissschuh.net>

On Wed, Apr 01, 2026 at 05:07:28PM +0200, Thomas Weißschuh wrote:
> The memory allocator has not seen any testing so far.

Oh indeed, I thought we already had such a test!

> Add a simple testcase for it.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>  tools/testing/selftests/nolibc/nolibc-test.c | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
> 
> diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> index 1efd10152e83..c888e13c6bd8 100644
> --- a/tools/testing/selftests/nolibc/nolibc-test.c
> +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> @@ -1554,6 +1554,30 @@ int test_time_types(void)
>  	return 0;
>  }
>  
> +int test_malloc(void)
> +{
> +	void *ptr1, *ptr2, *ptr3;
> +
> +	ptr1 = malloc(100);
> +	if (!ptr1)
> +		return 1;
> +
> +	ptr2 = realloc(ptr1, 200);
> +	if (!ptr2) {
> +		free(ptr1);
> +		return 2;
> +	}
> +
> +	ptr3 = realloc(ptr2, 2 * getpagesize());
> +	if (!ptr3) {
> +		free(ptr2);
> +		return 3;
> +	}
> +
> +	free(ptr3);
> +	return 0;
> +}
> +

I think we could enhance the test, because there are two key points
that it doesn't cover:
  - realloc must preserve contents
  - we need to make sure that more than one area exists (e.g. a naive
    implementation always returning the same pointer to a buffer would
    succeed).

What about something like this which would test malloc/calloc/realloc
and free (untested, sorry if I got some indexes wrong, but you get the
idea) ?

int test_malloc(void)
{
	int *array1, *array2, *array3;
	int idx;

	/* 1000 to allocate less than a page */
	array1 = malloc(1000 * sizeof(*array1));
	if (!array1)
		return 1;
	for (idx = 0; idx < 1000; idx++)
		array1[idx] = idx;

	/* 2000 to allocate more than a page */
	array2 = calloc(2000, sizeof(*array2));
	if (!array2) {
		free(array1);
		return 2;
	}
	for (idx = 0; idx < 2000; idx++)
		array1[idx] = idx + 1000;

	/* resize array1 into array3 and append array2 at the end,
	 * this requires 3 pages. On success, array1 is freed.
	 */
	array3 = realloc(array1, 3000 * sizeof(*array3));
	if (!array3) {
		free(array2);
		free(array1);
		return 3;
	}
	memcpy(array3 + 1000, array2, sizeof(*array2) * 2000);
	free(array2);

	/* the contents must be contiguous now */
	for (idx = 0; idx < 3000; idx++)
		if (array3[idx] != idx)
			return 4;
	free(array3);
	return 0;
}

>  int run_stdlib(int min, int max)
>  {
>  	int test;
> @@ -1680,6 +1704,7 @@ int run_stdlib(int min, int max)
>  		CASE_TEST(memchr_foobar6_o);        EXPECT_STREQ(1, memchr("foobar", 'o', 6), "oobar"); break;
>  		CASE_TEST(memchr_foobar3_b);        EXPECT_STRZR(1, memchr("foobar", 'b', 3)); break;
>  		CASE_TEST(time_types);              EXPECT_ZR(is_nolibc, test_time_types()); break;
> +		CASE_TEST(malloc);                  EXPECT_ZR(1, test_malloc()); break;
>  
>  		case __LINE__:
>  			return ret; /* must be last */

So it's as you prefer, in any case, I'm obviously OK with the extra test.

Acked-by: Willy Tarreau <w@1wt.eu>

Willy

  reply	other threads:[~2026-04-04  8:51 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-01 15:07 [PATCH 0/3] tools/nolibc: add support for asprintf() Thomas Weißschuh
2026-04-01 15:07 ` [PATCH 1/3] tools/nolibc: use __builtin_offsetof() Thomas Weißschuh
2026-04-04  8:34   ` Willy Tarreau
2026-04-04 15:29     ` David Laight
2026-04-01 15:07 ` [PATCH 2/3] selftests/nolibc: test the memory allocator Thomas Weißschuh
2026-04-04  8:51   ` Willy Tarreau [this message]
2026-04-01 15:07 ` [PATCH 3/3] tools/nolibc: add support for asprintf() Thomas Weißschuh
2026-04-04  8:53   ` Willy Tarreau
2026-04-04 15:34   ` David Laight
2026-04-05 15:39     ` Thomas Weißschuh
2026-04-07 10:46       ` David Laight

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=adDRK8D6YBZgv36H@1wt.eu \
    --to=w@1wt.eu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@weissschuh.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.