From patchwork Sun Jul 28 19:03:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1965749 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=ZfkvPIiC; 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 4WX9v80xsMz1ybX for ; Mon, 29 Jul 2024 05:06:12 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 631D9385840A for ; Sun, 28 Jul 2024 19:06:10 +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 76DC8385840A for ; Sun, 28 Jul 2024 19:03:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 76DC8385840A 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 76DC8385840A 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=1722193390; cv=none; b=TIxapyMqVTlocZ1SuYmHvFXA63aprpK9LXJBKkQB5Q7ry7ULHhO0z94HsxG2fS8uMYvy077qmSZrm8630TlA6I0bHDO61HZ8o34GtvidRN8R2/TBYOke3degwbCQrRP1ojsOZ0kjzzJ7oN9dWmAtZyeJ7Z+IUtggFvu0s6t0fsw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722193390; c=relaxed/simple; bh=vhLAmO6a7XLCt6pqtJc5RIc0IaOqPvEi+IWXguJ8dFo=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=YaY7cEmAyE4GV5FTUzLGMW7K8ulO4b7Kut0CboRv2bxzI36jmI0wW05GzluWjLkoD4vcNKb1bvcmD15cKrNgY23Rrl+2oxtrcGTshW2Q9rJESxoZN0akw+tljW4IaWt9OWgUBM6pTsCD3BDZid5O8zUYnfgR2ivvrXTx5wXwRUE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722193388; 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=3+ojhHIHBFbUvbzKL0a9MJ+Iv0u0NU/0XEEaPqGv+cA=; b=ZfkvPIiC4Og+THIf9pbT56bONn335X7jTtI3zZNv8muDZmnp3xnV6l6iliqOyNPm3QjHJn H37msA3YoX9vZHvfMlB08gY4Qsm9I/9eL+zPCx/p6bWbsaZMtZFOuLgnhMY0XiD77QAov2 vZqkHiF5g95/9UGu6zU4tBjlYwC5x6Q= 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-413-jbud1DlPPXqmE7XmVBwV0g-1; Sun, 28 Jul 2024 15:03:06 -0400 X-MC-Unique: jbud1DlPPXqmE7XmVBwV0g-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 D724D19560B3 for ; Sun, 28 Jul 2024 19:03:05 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.224.24]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DADF0300018D for ; Sun, 28 Jul 2024 19:03:04 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 08/13] support: Add support_count_maps In-Reply-To: Message-ID: References: X-From-Line: e3d2b6c46386cbc4a2442186403e4951b1e2814c Mon Sep 17 00:00:00 2001 Date: Sun, 28 Jul 2024 21:03:01 +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.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_H3, 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 --- support/Makefile | 2 ++ support/support.h | 5 +++ support/support_count_maps.c | 51 +++++++++++++++++++++++++++++++ support/tst-support_count_maps.c | 52 ++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 support/support_count_maps.c create mode 100644 support/tst-support_count_maps.c diff --git a/support/Makefile b/support/Makefile index aa57207bdc..bc35faa325 100644 --- a/support/Makefile +++ b/support/Makefile @@ -53,6 +53,7 @@ libsupport-routines = \ support_chroot \ support_copy_file \ support_copy_file_range \ + support_count_maps \ support_create_timer \ support_descriptor_supports_holes \ support_descriptors \ @@ -319,6 +320,7 @@ tests = \ tst-support-process_state \ tst-support_blob_repeat \ tst-support_capture_subprocess \ + tst-support_count_maps \ tst-support_descriptors \ tst-support_format_dns_packet \ tst-support_quote_blob \ diff --git a/support/support.h b/support/support.h index ba21ec9b5a..5769e8c5f3 100644 --- a/support/support.h +++ b/support/support.h @@ -96,6 +96,11 @@ int support_descriptor_supports_holes (int fd); WHY_MSG as part of the diagnostic. */ void support_need_proc (const char *why_msg); +/* Counts the number of mappings in the process address space. + Currently implemented using /proc/self/maps on Linux, so it needs + that to exist. */ +size_t support_count_maps (void); + /* Error-checking wrapper functions which terminate the process on error. */ diff --git a/support/support_count_maps.c b/support/support_count_maps.c new file mode 100644 index 0000000000..2a6dd9fee3 --- /dev/null +++ b/support/support_count_maps.c @@ -0,0 +1,51 @@ +/* Count the number of mappings. + 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 + +size_t +support_count_maps (void) +{ + FILE *fp = fopen ("/proc/self/maps", "r"); + if (fp == NULL) + { + if (errno == ENOENT) + FAIL_UNSUPPORTED ("support_count_maps: test needs /proc/self/maps"); + else + FAIL_EXIT1 ("cannot open /proc/self/maps: %m"); + } + size_t count = 0; + while (true) + { + switch (getc_unlocked (fp)) + { + case EOF: + /* Error will be reported by xfclose below. */ + goto out; + case '\n': + ++count; + break; + } + } + out: + xfclose (fp); + return count; +} diff --git a/support/tst-support_count_maps.c b/support/tst-support_count_maps.c new file mode 100644 index 0000000000..e904a00cd4 --- /dev/null +++ b/support/tst-support_count_maps.c @@ -0,0 +1,52 @@ +/* Test counting the number of mappings. + 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 + +static int +do_test (void) +{ + long int page_size = xsysconf (_SC_PAGESIZE); + + size_t initial = support_count_maps (); + printf ("info: initial number of maps: %zu\n", initial); + + void *ptr = xmmap (NULL, 2 * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1); + size_t one_mapping = support_count_maps (); + printf ("info: new number of maps: %zu\n", one_mapping); + TEST_COMPARE (initial + 1, one_mapping); + + /* The mapping gets split due to the protection flags change. */ + xmprotect (ptr, page_size, PROT_READ); + size_t two_mappings = support_count_maps (); + TEST_COMPARE (initial + 2, two_mappings); + + xmunmap (ptr, 2 * page_size); + size_t final = support_count_maps (); + TEST_COMPARE (initial, final); + + return 0; +} + +#include