From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mta1.formilux.org (mta1.formilux.org [51.159.59.229]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F05E2E6CA8 for ; Sat, 4 Apr 2026 08:51:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=51.159.59.229 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775292719; cv=none; b=FGLrUTLN9nvvOOYsKnTlc6lXHAvMb1JOLBgjkkbwYn4pogbxUL2/3O/jqEHBuy8VvR8hvV5fT+Tfh6eRg5YhsdigOwYbxby+p4W2+G0+bleCLVSf7xcwiF3NF79ZNryAQCJpg3ME5PTKZqUE+yvEH/YWvRdtUEakwLYAEjUl1bU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775292719; c=relaxed/simple; bh=a+OyAjLRZxAXh+OgkIdiAvemVFsr3Ky+AA0QIdwjhTs=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=b8l7a+/F1JTGLFpwTPP1naL/eZmSmQ41od65i85ckUrgWVly/QTFABnI2+6lV1waCOBYCENeZDo1M3kTtH1BNUddfadtpaxJDTQKH6ahK8RfObQMB3JNfUQcEnYnclrxE78dEbd6g6inSmuWEddLzhClND964tncAwGCmWkLCb8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=1wt.eu; spf=pass smtp.mailfrom=1wt.eu; dkim=pass (1024-bit key) header.d=1wt.eu header.i=@1wt.eu header.b=bFC0UeI2; arc=none smtp.client-ip=51.159.59.229 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=1wt.eu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=1wt.eu Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=1wt.eu header.i=@1wt.eu header.b="bFC0UeI2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1wt.eu; s=mail; t=1775292715; bh=7WY9dS/AeK87/brdyprkwLjPa2lflxC6z3EHHSZ97SU=; h=From:Message-ID:From; b=bFC0UeI2VnD2Jpl1Fv4UYwTCpulFM2XdidVk063mL2e6qSschq+71fYc9j7jOwPPm 412vdpV/fcJYaziLQkOEuCLgZlzGFvbbQy8pF+3+ZvyLheKNqLeT2HLH9ZL1MSxZN4 plCqm08vEVowXeuyHBmPYSFPDb0OA/XO8BPYed+E= Received: from 1wt.eu (ded1.1wt.eu [163.172.96.212]) by mta1.formilux.org (Postfix) with ESMTP id 84D3EC0F42; Sat, 04 Apr 2026 10:51:55 +0200 (CEST) Date: Sat, 4 Apr 2026 10:51:55 +0200 From: Willy Tarreau To: Thomas =?iso-8859-1?Q?Wei=DFschuh?= Cc: linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/3] selftests/nolibc: test the memory allocator Message-ID: References: <20260401-nolibc-asprintf-v1-0-46292313439f@weissschuh.net> <20260401-nolibc-asprintf-v1-2-46292313439f@weissschuh.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit 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 > --- > 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 Willy