From patchwork Fri Aug 30 19:53:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1979212 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=hw7MrPAB; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4WwTRw6BSFz1yfX for ; Sat, 31 Aug 2024 05:56:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B1C02386482F for ; Fri, 30 Aug 2024 19:56:26 +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.129.124]) by sourceware.org (Postfix) with ESMTP id B60823864C56 for ; Fri, 30 Aug 2024 19:53:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B60823864C56 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 B60823864C56 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=1725047620; cv=none; b=PNz72+EoxKWh3HEMJ5WKMeLBlPVZ72V6Xj7EUjOn+vXDfWAcPtbD9VMiPS5xhQ4IkBEpdks7XBh2b+enllUm5FdiqG/AmCf9ii5vrpKuqgIrtjeEfDwJwKCMUZoZdDC+iRa2Lz/e38qJ7nJfqhs5J7ztR2bN3Y1ZETpMrq4NGjc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047620; c=relaxed/simple; bh=B+CF4Skh3/h2FBJSANTlD0rpPWDKt0IYWn1+VWtC0m0=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=J2XZnf4O5/hqARpEoVWDLQbTfYjoo8tGS/Out0F60+dTfMQKm4SsOe9nuQ5GSLvIC1nLZdYjD6mWlsY0yPrYoCXEI8GjoPbUtnsO9+JAvIu8HUK3RRR+QDV/r2iXHn9U8PqOVUolF4sq6lNaN67IzpJl+dUnCTQA3PNyS2Ymrs8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047618; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=OnbKf1XXDRJngSV1GqPbWSoB+spGt4nDFVWmQemtILk=; b=hw7MrPABZHFzPDNsft3bVZcf7V6AJbe4pHAUhPqDEZx7he8BRpXEjUqidQNGraIBG69qYw gyhd7uXF0RiDUQJ9+kSlCr0nhycclttqvGPw5jUf3HSQa81XgHOelu3MwLTXKlWPwD1tK9 QUxfY+vsRCVCNoYxbVXFr6YKDyZQcJs= Received: from mx-prod-mc-05.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-231-mcc_3jotOZC4QY5fzXfCug-1; Fri, 30 Aug 2024 15:53:36 -0400 X-MC-Unique: mcc_3jotOZC4QY5fzXfCug-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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2C6D11955F03 for ; Fri, 30 Aug 2024 19:53:36 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 13D883001FEF for ; Fri, 30 Aug 2024 19:53:34 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 09/13] dirent: Add tst-closedir-leaks In-Reply-To: Message-ID: References: X-From-Line: c507e98237dc4cec6da0536eed3ac8f13697468f Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:53:31 +0200 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.8 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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 verfies that closedir deallocates memory and closes file descriptors. --- dirent/Makefile | 20 ++++++++++ dirent/tst-closedir-leaks.c | 76 +++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 dirent/tst-closedir-leaks.c diff --git a/dirent/Makefile b/dirent/Makefile index 556f759f65..f9056724f0 100644 --- a/dirent/Makefile +++ b/dirent/Makefile @@ -58,6 +58,7 @@ tests := \ bug-readdir1 \ list \ opendir-tst1 \ + tst-closedir-leaks \ tst-fdopendir \ tst-fdopendir2 \ tst-scandir \ @@ -65,6 +66,18 @@ tests := \ tst-seekdir \ # tests +ifeq ($(run-built-tests),yes) +ifneq ($(PERL),no) +generated += \ + $(objpfx)tst-closedir-leaks-mem.out \ + # generated + +tests-special += \ + $(objpfx)tst-closedir-leaks-mem.out \ + # tests-special +endif # $(PERL) ! no +endif # $(run-built-tests) == yes + CFLAGS-scandir.c += $(uses-callbacks) CFLAGS-scandir64.c += $(uses-callbacks) CFLAGS-scandir-tail.c += $(uses-callbacks) @@ -74,3 +87,10 @@ CFLAGS-dirfd.c += $(config-cflags-wno-ignored-attributes) include ../Rules opendir-tst1-ARGS = --test-dir=${common-objpfx}dirent + +tst-closedir-leaks-ENV += MALLOC_TRACE=$(objpfx)tst-closedir-leaks.mtrace \ + LD_PRELOAD=$(common-objpfx)/malloc/libc_malloc_debug.so + +$(objpfx)tst-closedir-leaks-mem.out: $(objpfx)tst-closedir-leaks.out + $(common-objpfx)malloc/mtrace $(objpfx)tst-closedir-leaks.mtrace > $@; \ + $(evaluate-test) diff --git a/dirent/tst-closedir-leaks.c b/dirent/tst-closedir-leaks.c new file mode 100644 index 0000000000..7a26ddaa81 --- /dev/null +++ b/dirent/tst-closedir-leaks.c @@ -0,0 +1,76 @@ +/* Test for resource leaks in closedir. + 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 +#include +#include +#include +#include +#include +#include + +static void +one_test (enum support_readdir_op op, unsigned int read_limit, + bool use_fdopendir) +{ + struct support_descriptors *fds = support_descriptors_list (); + struct support_dirent e = { 0, }; + + DIR *stream; + if (use_fdopendir) + { + int fd = xopen (".", O_RDONLY | O_DIRECTORY, 0); + stream = xfdopendir (fd); + /* The descriptor fd will be closed by closedir below. */ + } + else + stream = xopendir ("."); + for (unsigned int i = 0; i < read_limit; ++i) + if (!support_readdir (stream, op, &e)) + break; + TEST_COMPARE (closedir (stream), 0); + + free (e.d_name); + support_descriptors_check (fds); + support_descriptors_free (fds); +} + +static int +do_test (void) +{ + mtrace (); + + for (int use_fdopendir = 0; use_fdopendir < 2; ++use_fdopendir) + { + /* No reads, operation does not matter. */ + one_test (SUPPORT_READDIR, 0, use_fdopendir); + + for (enum support_readdir_op op = 0; op <= support_readdir_op_last(); + ++op) + { + one_test (op, 1, use_fdopendir); + one_test (op, -1, use_fdopendir); /* Unlimited reads. */ + } + } + + return 0; +} + +#include