From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id C2B1C3858D1E for ; Tue, 21 May 2024 08:38:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C2B1C3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C2B1C3858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716280699; cv=none; b=PbMTg4zNlTHnggMPl2Q5XxcUHNrzHKjKeEUAb1pNKF2MpHPIdM/nbH9f0MBAtdJidr6f39sg0KAH4OIohiWpFVzwHVhlOAs/Ad+cIvOPLU4bM/SjxN4aRumts3KVtS0alSi4BPPOsfwR1G+c2XKQFVt+lOcPg4mxH1O4wOtCkNY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716280699; c=relaxed/simple; bh=Kn5givOYGUyCp+PMjGwthG+WbjJnD1VNhBYpjLHPBCQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=r48ksVgrTdAWQO/BtWsHx7lVeJhfXNtnK6X6wz2EFaRL8tHBnHytOAAAxfW0vbmHtB1QAutpLbXMMlCuSuBtOQYvUML+iQtWTR7YaQ3nMuGHXcr2u0m7iO/7hZ3lNFIpVFa2AQ9RBPYymmro3x9DJkL8+KMkjNuxWBrgpdjt7UI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716280696; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=51ybu69ST7DnsOiVG4l/nNbnc3j8oPpn/MuvtZJSoco=; b=QcD3fLr7P3Hd63P3xtCy12LzkrFRdcKlJW7+xqR8+JUgRLvmNRobH7Q5qksCF8Hj59XOdd Y+r0ZYqi9ugpHLbhmEAYBonqRp3mgyv0xfA7fiSpFCvQzncipC0G9gg/aWJ7rpcGglGy0e 7lp8EvkbM8tb+gVwNi3c8B0kEPgRzwM= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-1-IV2nPVhRNheSmqZjWawH8g-1; Tue, 21 May 2024 04:38:14 -0400 X-MC-Unique: IV2nPVhRNheSmqZjWawH8g-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-42003993265so47292765e9.1 for ; Tue, 21 May 2024 01:38:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716280693; x=1716885493; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=51ybu69ST7DnsOiVG4l/nNbnc3j8oPpn/MuvtZJSoco=; b=XTBoFPIRNm58ad2oLIfFGILSUv4ODfZqhheJUuLbOY8Wpa1iPsP47zdksk3p5ecx/N wLRFOVnvW/nT08bZVHbZd9yLd61yxhiX/m/y8G+bkA1WdQnN3vGb6cRv+lkc+X38DdhZ ojtS+9kquLb6g3gdZKQHC0S6B94QLwZboUtPHGLUhkJWmSIOLhwk2+P0cooNuADhs2pq rTkc8wVpy7sAJOg9SyvdQgKrEn11TTO7SHk7qI2OmGqH4LEj/nYRMgL5QkgJP9yLUAWe oYl9x9DpWHLIW9CiUezl39dCEtZPI6rJ+i1gPkvR4CWtxt06qcDwT0F0N4WI1mwZzNyN PuTQ== X-Gm-Message-State: AOJu0Yzwe0pJdBngJ5ByMYy3Wydzjabep+63FI44EQ511vGDry2PoVu0 eW6ZNjnLJWnI1TY1f3qnXRBfbkquSI+vPF5cvOdZM1ze6MFyyCxogMJfHhdzRwTnJWzoDXeNFaR qQ4+JuesMKydoVZa4REz6iLzgvKORUoEoaCfD5hpw3mKnVSUiULqhEBSR6hvtzE/1HvJZtKqLZ6 TGXViZx2IRLpja2iq/+Kcsg/60Smnddc0ziizxyJ9oJlo= X-Received: by 2002:a05:600c:1d9a:b0:420:2986:cce7 with SMTP id 5b1f17b1804b1-4202986d140mr155774375e9.25.1716280692965; Tue, 21 May 2024 01:38:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFCffwqA6E2iJOx9tgL0AhHRU92RkAoIGwvCbcV13Cm7RsEXgdcF+KzJSoQZ1A8PA0WqyqWag== X-Received: by 2002:a05:600c:1d9a:b0:420:2986:cce7 with SMTP id 5b1f17b1804b1-4202986d140mr155774135e9.25.1716280692459; Tue, 21 May 2024 01:38:12 -0700 (PDT) Received: from lenovo-p1.mamux.org ([82.213.234.88]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3502b8a7748sm31491657f8f.49.2024.05.21.01.38.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 01:38:12 -0700 (PDT) From: Miguel Martin To: libc-stable@sourceware.org Cc: Joe Simmons-Talbott , DJ Delorie Subject: [COMMITTED 2.34 2/2] malloc: Improve aligned_alloc and calloc test coverage. Date: Tue, 21 May 2024 10:38:07 +0200 Message-ID: <20240521083807.925020-3-mmartinv@redhat.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240521083807.925020-1-mmartinv@redhat.com> References: <20240521083807.925020-1-mmartinv@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Joe Simmons-Talbott Add a DSO (malloc/tst-aligned_alloc-lib.so) that can be used during testing to interpose malloc with a call that randomly uses either aligned_alloc, __libc_malloc, or __libc_calloc in the place of malloc. Use LD_PRELOAD with the DSO to mirror malloc/tst-malloc.c testing as an example in malloc/tst-malloc-random.c. Add malloc/tst-aligned-alloc-random.c as another example that does a number of malloc calls with randomly sized, but limited to 0xffff, requests. The intention is to be able to utilize existing malloc testing to ensure that similar allocation APIs are also exposed to the same rigors. Reviewed-by: DJ Delorie (cherry picked from commit 3395157ff2b0657d70c36169156f67440205c8bf) --- malloc/Makefile | 16 ++++++- malloc/tst-aligned-alloc-random.c | 43 ++++++++++++++++++ malloc/tst-aligned_alloc-lib.c | 72 +++++++++++++++++++++++++++++++ malloc/tst-malloc-random.c | 20 +++++++++ malloc/tst-malloc.c | 4 ++ 5 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 malloc/tst-aligned-alloc-random.c create mode 100644 malloc/tst-aligned_alloc-lib.c create mode 100644 malloc/tst-malloc-random.c diff --git a/malloc/Makefile b/malloc/Makefile index b02c49a687..5bd73a32b5 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -39,11 +39,13 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \ tst-malloc-tcache-leak \ tst-malloc_info tst-mallinfo2 \ tst-malloc-too-large \ + tst-malloc-random \ tst-malloc-stats-cancellation \ tst-tcfree1 tst-tcfree2 tst-tcfree3 \ tst-safe-linking \ tst-mallocalign1 \ - tst-aligned-alloc + tst-aligned-alloc \ + tst-aligned-alloc-random tests-static := \ tst-interpose-static-nothread \ @@ -123,7 +125,8 @@ extra-libs = libmemusage libc_malloc_debug extra-libs-others = $(extra-libs) # Helper objects for some tests. -extra-tests-objs += \ +extra-test-objs += \ + tst-aligned_alloc-lib.so \ tst-interpose-aux-nothread.o \ tst-interpose-aux-thread.o \ @@ -131,6 +134,9 @@ test-extras = \ tst-interpose-aux-nothread \ tst-interpose-aux-thread \ +modules-names = \ + tst-aligned_alloc-lib + libmemusage-routines = memusage libmemusage-inhibit-o = $(filter-out .os,$(object-suffixes)) @@ -329,3 +335,9 @@ tst-mallocstate-malloc-check-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so # libc_malloc_debug.so. $(objpfx)tst-mallocstate: $(objpfx)libc_malloc_debug.so $(objpfx)tst-mallocstate-malloc-check: $(objpfx)libc_malloc_debug.so + +$(objpfx)tst-aligned-alloc-random.out: $(objpfx)tst-aligned_alloc-lib.so +$(objpfx)tst-malloc-random.out: $(objpfx)tst-aligned_alloc-lib.so + +tst-aligned-alloc-random-ENV = LD_PRELOAD=$(objpfx)tst-aligned_alloc-lib.so +tst-malloc-random-ENV = LD_PRELOAD=$(objpfx)tst-aligned_alloc-lib.so diff --git a/malloc/tst-aligned-alloc-random.c b/malloc/tst-aligned-alloc-random.c new file mode 100644 index 0000000000..f2825ce38f --- /dev/null +++ b/malloc/tst-aligned-alloc-random.c @@ -0,0 +1,43 @@ +/* Test for randomized malloc that calls aligned_alloc + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 + . */ + +#include +#include +#include + +static int +do_test (void) +{ + void *p1; + int i; + + srandom (time (NULL)); + + for (i = 0; i < 1024; i++) + { + size_t size = random () & 0xffff; + + p1 = malloc (size); + TEST_VERIFY (p1 != NULL); + } + + return 0; +} + + +#include diff --git a/malloc/tst-aligned_alloc-lib.c b/malloc/tst-aligned_alloc-lib.c new file mode 100644 index 0000000000..0205df5acf --- /dev/null +++ b/malloc/tst-aligned_alloc-lib.c @@ -0,0 +1,72 @@ +/* Module used for improved aligned_alloc testing. + Copyright (C) 2024 Free Software Foundation, Inc. + Copyright The GNU Toolchain Authors. + This file is part of the GNU C Library. + + 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; see the file COPYING.LIB. If + not, see . */ + +#include +#include +#include + +extern void *__libc_malloc (size_t size); +extern void *__libc_calloc (size_t n, size_t size); + +int aligned_alloc_count = 0; +int libc_malloc_count = 0; +int libc_calloc_count = 0; + +/* Get a random alignment value. Biased towards the smaller values. Must be + a power of 2. */ +static size_t get_random_alignment (void) +{ + size_t aligns[] = { + 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384 + }; + + return aligns[random () % array_length (aligns)]; +} + +static void *get_random_alloc (size_t size) +{ + void *retval; + size_t align; + + switch (random() % 3) + { + case 1: + align = get_random_alignment (); + retval = aligned_alloc (align, size); + aligned_alloc_count++; + break; + case 2: + retval = __libc_calloc (1, size); + libc_calloc_count++; + break; + default: + retval = __libc_malloc (size); + libc_malloc_count++; + break; + } + + return retval; +} + + +void * __random_malloc (size_t size) +{ + return get_random_alloc (size); +} +strong_alias (__random_malloc, malloc) diff --git a/malloc/tst-malloc-random.c b/malloc/tst-malloc-random.c new file mode 100644 index 0000000000..762b70c918 --- /dev/null +++ b/malloc/tst-malloc-random.c @@ -0,0 +1,20 @@ +/* Test malloc with random calls to aligned_alloc and calloc. + + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 + . */ + +#include "tst-malloc.c" diff --git a/malloc/tst-malloc.c b/malloc/tst-malloc.c index a587cbded6..98be3ee254 100644 --- a/malloc/tst-malloc.c +++ b/malloc/tst-malloc.c @@ -19,7 +19,9 @@ #include #include #include +#include #include +#include static int errors = 0; @@ -36,6 +38,8 @@ do_test (void) void *p, *q; int save; + srandom (time (NULL)); + errno = 0; DIAG_PUSH_NEEDS_COMMENT; -- 2.45.1