From patchwork Tue Jul 16 22:43:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1961350 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Nox5d0wK; 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 4WNvJf33h4z1ySl for ; Wed, 17 Jul 2024 08:44:34 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4153C3858401 for ; Tue, 16 Jul 2024 22:44:32 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yb1-xb34.google.com (mail-yb1-xb34.google.com [IPv6:2607:f8b0:4864:20::b34]) by sourceware.org (Postfix) with ESMTPS id 050AC3858C78 for ; Tue, 16 Jul 2024 22:43:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 050AC3858C78 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 050AC3858C78 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b34 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721169809; cv=none; b=Ig6IOcnfstKaHwFTnCFGzDl9abSy+wG3jU47H5UID3muX2QIW4dgx3W0yjvPVEK69CxClyTPYV85emSHH0pkNrRaycb+Jhbj3iqkrGn6jr6BP5tBa56iAZ1vYxgMnpr41HHlIXnzRYJyF+2EsSJbzNVsWo6Rj2I/ooVO8gQ2jJw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721169809; c=relaxed/simple; bh=7ZCXvABcSy/QaVCIRMknfRYpFmL6Frdgcyy9TbxlndU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=AjBm1R6OeuX0/VS+M35//gcdkv6rApqZx/zxUd0BdJA7XzoSfTe4vk8qCfz+wyYq97Nc2yhhwjAfM8S0yfRAuMfpabgirbVvj5t2lfkvOSkUmZpH68h49jk7UZoYpHeqG1PC1Zdrdhg4Z3Jq+8ymjHk5pHZwi1akGXnb3I5ir/I= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb34.google.com with SMTP id 3f1490d57ef6-e0354459844so6103304276.3 for ; Tue, 16 Jul 2024 15:43:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721169806; x=1721774606; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=SD5K6V8ZzUWewVaOZI5d2dNurMC7+MVePUYh9TSmcDc=; b=Nox5d0wKdz9DRZA5SN6TyUFBA7H63QZorFw4xOsJLMfmnrRSvU8InmMlppAewJ+WJq +vH6s2CQJuhvFoNtY7MoSurQ3V0Gl2UFVYPcekz1pXREChOYQ6zqP1IrJxmeVJsXLBNT Wf1NAgYU6eaPYi1XmR9HJo4VVkpy4oBJsxsSgiyC5vFoUzmfewcVFDiRkvFdF9UEnApd W/F4ZASRLVadWyreqNqGpZ1WfzSIuOcpYarr5arjzATOCyCED3M8rRlKfnKOjq9VG4AI 8Xs3HZFBUFOd8Fq2RVlQicJckUb8PL3bMQeZSjOuKY1ykDN9q0hqTPCJexpaQEhec9Pj iXug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721169806; x=1721774606; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SD5K6V8ZzUWewVaOZI5d2dNurMC7+MVePUYh9TSmcDc=; b=IhlnYXe7tspKbcKsARVM/+R9IpzVPWQXjJADls082XoA0BJb/o9fRwnAahNGVRJLQ9 sAe0ptHLQ/gBzt18mH8U8xq5+9L2Y4Tyx2L/KrSkwNomJGyXtCEkNZgIlMbZOjPyQvp0 4sKDB4AzTEFQ+9tDcsU2necjKrx/37ssyeCL9wtQCrDM4DBCSgFgHBmSovphxNgoS6mS rwZFXolU4G+2r3KJLHY9DUnht8SiVDloQB3nupaMF9LVr0mPKqxjYJNCOcl5aNobnfY7 MIFGQQjmDPw1vZzoZBT4Y9Rbx6+/c1STfV/gZhtFnXG+I6zu/KScCEfw3ftmTuFWdhAP RQHw== X-Gm-Message-State: AOJu0YwrdzPyLqj1tbbMeGIit3daG9CbNdXs854P6ajaYkvuDIdzr/tF Ycp/CBTFbghk/aJvVBXTj/5Pbbllh8zl7Ormds7C8Waqrs6rNZCpF8nrlizwPamOHg== X-Google-Smtp-Source: AGHT+IE5AugcfMYQwK3iNOaKsJhgEjMIZWWc+RHIA/RcmOHcduZZmY5i7kHE/vkyAQtrRcEIOMwo/g== X-Received: by 2002:a05:6902:18c5:b0:e05:674f:fad6 with SMTP id 3f1490d57ef6-e05d565c759mr4786110276.5.1721169806014; Tue, 16 Jul 2024 15:43:26 -0700 (PDT) Received: from gnu-tgl-3.localdomain ([172.58.139.74]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e05a4614b8bsm1373938276.23.2024.07.16.15.43.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jul 2024 15:43:25 -0700 (PDT) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id C1ECBC044B for ; Tue, 16 Jul 2024 15:43:21 -0700 (PDT) From: "H.J. Lu" To: libc-alpha@sourceware.org Subject: [PATCH v2 3/3] Add mremap tests Date: Tue, 16 Jul 2024 15:43:21 -0700 Message-ID: <20240716224321.2352246-4-hjl.tools@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240716224321.2352246-1-hjl.tools@gmail.com> References: <20240716224321.2352246-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3021.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 Add tests for MREMAP_MAYMOVE and MREMAP_FIXED. On Linux, also test MREMAP_DONTUNMAP. Signed-off-by: H.J. Lu --- misc/Makefile | 2 + misc/tst-mremap1.c | 46 +++++++++++++++ misc/tst-mremap2.c | 53 ++++++++++++++++++ sysdeps/unix/sysv/linux/Makefile | 1 + sysdeps/unix/sysv/linux/tst-linux-mremap1.c | 62 +++++++++++++++++++++ 5 files changed, 164 insertions(+) create mode 100644 misc/tst-mremap1.c create mode 100644 misc/tst-mremap2.c create mode 100644 sysdeps/unix/sysv/linux/tst-linux-mremap1.c diff --git a/misc/Makefile b/misc/Makefile index 5d17c562fe..7b7f8351bf 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -257,6 +257,8 @@ tests := \ tst-mntent-blank-passno \ tst-mntent-escape \ tst-mntent2 \ + tst-mremap1 \ + tst-mremap2 \ tst-preadvwritev \ tst-preadvwritev2 \ tst-preadvwritev64 \ diff --git a/misc/tst-mremap1.c b/misc/tst-mremap1.c new file mode 100644 index 0000000000..0469991a6c --- /dev/null +++ b/misc/tst-mremap1.c @@ -0,0 +1,46 @@ +/* Test mremap with MREMAP_MAYMOVE. + 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) +{ + size_t old_size = getpagesize (); + char *old_addr = xmmap (NULL, old_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1); + old_addr[0] = 1; + old_addr[old_size - 1] = 2; + + /* Test MREMAP_MAYMOVE. */ + size_t new_size = old_size + old_size; + char *new_addr = mremap (old_addr, old_size, new_size, MREMAP_MAYMOVE); + TEST_VERIFY_EXIT (new_addr != MAP_FAILED); + new_addr[0] = 1; + new_addr[new_size - 1] = 2; + xmunmap (new_addr, new_size); + + return 0; +} + +#include diff --git a/misc/tst-mremap2.c b/misc/tst-mremap2.c new file mode 100644 index 0000000000..9e226eb90e --- /dev/null +++ b/misc/tst-mremap2.c @@ -0,0 +1,53 @@ +/* Test mremap with MREMAP_FIXED. + 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 + +static int +do_test (void) +{ + size_t old_size = getpagesize (); + size_t new_size = old_size + old_size; + char *old_addr = xmmap (NULL, old_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1); + old_addr[0] = 1; + old_addr[old_size - 1] = 2; + + char *fixed_addr = xmmap (NULL, new_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1); + fixed_addr[0] = 1; + fixed_addr[new_size - 1] = 2; + + /* Test MREMAP_FIXED. */ + char *new_addr = mremap (old_addr, old_size, new_size, + MREMAP_FIXED | MREMAP_MAYMOVE, + fixed_addr); + if (new_addr == MAP_FAILED) + return errno == EINVAL ? EXIT_UNSUPPORTED : EXIT_FAILURE; + new_addr[0] = 1; + new_addr[new_size - 1] = 2; + xmunmap (new_addr, new_size); + + return 0; +} + +#include diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 097b5a26fc..59998c7af4 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -206,6 +206,7 @@ tests += \ tst-getauxval \ tst-gettid \ tst-gettid-kill \ + tst-linux-mremap1 \ tst-memfd_create \ tst-misalign-clone \ tst-mlock2 \ diff --git a/sysdeps/unix/sysv/linux/tst-linux-mremap1.c b/sysdeps/unix/sysv/linux/tst-linux-mremap1.c new file mode 100644 index 0000000000..0d484377bd --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-linux-mremap1.c @@ -0,0 +1,62 @@ +/* Test mremap with MREMAP_DONTUNMAP. + 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) +{ + size_t old_size = getpagesize (); + size_t new_size = old_size; + char *old_addr = xmmap (NULL, old_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1); + old_addr[0] = 1; + old_addr[old_size - 1] = 2; + + /* Create an available 64-page mmap region. */ + size_t fixed_size = old_size * 64; + char *fixed_addr = xmmap (NULL, fixed_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1); + xmunmap (fixed_addr, fixed_size); + + /* Add 3 * pagesize. */ + fixed_size += 3 * old_size; + + /* Test MREMAP_DONTUNMAP. It should return FIXED_ADDR created above. */ + char *new_addr = mremap (old_addr, old_size, new_size, + MREMAP_DONTUNMAP | MREMAP_MAYMOVE, + fixed_addr); + if (new_addr == MAP_FAILED) + return errno == EINVAL ? EXIT_UNSUPPORTED : EXIT_FAILURE; + TEST_VERIFY_EXIT (fixed_addr == new_addr); + old_addr[0] = 3; + old_addr[old_size - 1] = 4; + new_addr[0] = 1; + new_addr[new_size - 1] = 2; + xmunmap (new_addr, new_size); + xmunmap (old_addr, old_size); + + return 0; +} + +#include