From patchwork Tue Jul 9 07:35:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1958219 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=bqbcuCcD; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WJCSy5bMXz1xpd for ; Tue, 9 Jul 2024 17:35:30 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 077C838471C8 for ; Tue, 9 Jul 2024 07:35:29 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 5D4BB38471C3 for ; Tue, 9 Jul 2024 07:35:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5D4BB38471C3 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 5D4BB38471C3 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720510513; cv=none; b=MO3WvP3bCDOin2e/sijyUBZhPwu4msaVwp0WSHkA+EZGSVvDbWr57jcZC2I8Z3F2610aEGNkGN00/C92PCgIzLwCEXc2eSULfe2ikm6Qb0i8rq5WVcuEoEhMNCXeIqfM2oimMn/vUy0vgmfBjvioRGifG1ML3W+JVcaQdqg0v0Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720510513; c=relaxed/simple; bh=UkACFXrXBJRTWgeDheSx8hWl4xBSdcTglODyzTcqrfM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QLArL4b/r6yDqJsBay4H9eYCO/6LfQQMQXww7n6UZxJlhpuO5onhNEdfLNmZZIbfHJrDgoQ31l5DrH4iabhZ/3pU/jvLPxGWU8OzRhK7rzMReTPKmzq6+uPnw83/6uNgXd+Rd1rkpZmHpm/YHQCL2Mmp6BNKZdpJxZ5CxADJR88= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720510511; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=FprNmPvOFa3qpOmsXa63b3kpRpPcI+1i21BhcWcAHBM=; b=bqbcuCcDTGzu+adhw2Y0uePAOVqhF7j65TU92Qc5NHqVvUzeWssv2Cj5Y00oklsckvSR+p yloGAp6l9OeHXovJQIFDuqx7IIwu80u8bBj87ProEthtX040cbH4Q1t/zTeNzYIYwP4rqx NQIN2lwwy6zMa7aYxv3Ip0oXj4C4hoA= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-20-aoEP-1SgPfie1cBtnSXp4w-1; Tue, 09 Jul 2024 03:35:08 -0400 X-MC-Unique: aoEP-1SgPfie1cBtnSXp4w-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D686119560B9 for ; Tue, 9 Jul 2024 07:35:07 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.224.64]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C2FAE3000181 for ; Tue, 9 Jul 2024 07:35:06 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH] malloc: Smoke test for libmemusage Date: Tue, 09 Jul 2024 09:35:03 +0200 Message-ID: <878qyb2f2w.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.9 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org It was reported that libmemusage stopped working due to the changes in commit aaad2123d68275acc0f061e ("elf: Fix slow tls access after dlopen [BZ #19924]"). This shared object uses non-static TLS and interposes malloc, so it runs into the recursion issue worked around in commit 018f0fc3b818d4d1460a ("elf: Support recursive use of dynamic TLS in interposed malloc") with certain applications. While the test added here does not detect the recu issue, it is better than no test at all. --- malloc/Makefile | 16 ++++++++++- malloc/tst-memusage.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) base-commit: 4c09bd400b689f1af8e08103cff3d25e0cba51d4 diff --git a/malloc/Makefile b/malloc/Makefile index 02aff1bd1d..b623c33bd0 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -54,6 +54,7 @@ tests := \ tst-memalign \ tst-memalign-2 \ tst-memalign-3 \ + tst-memusage \ tst-obstack \ tst-posix_memalign \ tst-pvalloc \ @@ -279,8 +280,13 @@ extra-objs += memusagestat.o # kernel interface headers, not something like libgd. So the simplest thing # is to presume that the standard system headers will be ok for this file. $(objpfx)memusagestat.o: sysincludes = # nothing + +ifeq ($(run-built-tests),yes) +tests-special += $(objpfx)/tst-memusage-stat.out endif -endif + +endif # $(LIBGD) +endif # !$(cross-compiling) # Another goal which can be used to override the configure decision. .PHONY: do-memusagestat @@ -419,3 +425,11 @@ $(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 + +tst-memusage-ENV = LD_PRELOAD=$(objpfx)/libmemusage.so \ + MEMUSAGE_OUTPUT=$(objpfx)/tst-memusage-output.data +$(objpfx)tst-memusage: $(objpfx)/libmemusage.so +$(objpfx)tst-memusage-stat.out: $(objpfx)tst-memusage.out $(objpfx)memusagestat + $(test-wrapper-env) $(run-program-env) $(rtld-prefix) \ + $(objpfx)memusagestat $(objpfx)/tst-memusage-output.data >$@ 2>&1; \ + $(evaluate-test) diff --git a/malloc/tst-memusage.c b/malloc/tst-memusage.c new file mode 100644 index 0000000000..dc9de8f85a --- /dev/null +++ b/malloc/tst-memusage.c @@ -0,0 +1,75 @@ +/* Smoke test for libmemusagestat.so. + 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 + . */ + +/* Do not use the test driver because this test needs to avoid + forking, to avoid exercising fork handling in libmemusage. */ + +#include +#include +#include +#include +#include +#include +#include + +static void *allocations[1000]; +static unsigned int allocations_count; + +static void +record (void *ptr) +{ + if (ptr == NULL) + FAIL_EXIT1 ("memory allocation failure"); + if (allocations_count < array_length (allocations)) + allocations[allocations_count++] = ptr; + else + FAIL_EXIT1 ("out of space for pointer tracking"); +} + +static void +free_all (void) +{ + for (unsigned int i = 0; i < allocations_count; ++i) + free (allocations[i]); + allocations_count = 0; +} + +static void * +perform_allocations (void *ignored) +{ + for (int loops = 0; loops < 3; ++loops) + { + for (int i = 0; i < array_length (allocations); ++i) + record (malloc (1000)); + free_all (); + for (int i = 0; i < array_length (allocations); ++i) + record (calloc (1000, 2)); + free_all (); + for (int i = 0; i < array_length (allocations); ++i) + record (realloc (NULL, 4000)); + free_all (); + } + return NULL; +} + +int +main (void) +{ + perform_allocations (NULL); + xpthread_join (xpthread_create (NULL, perform_allocations, NULL)); +}