* [LTP] [PATCH v2 1/3] mremap02: Convert to new API
@ 2024-02-27 8:42 Yang Xu via ltp
2024-02-27 8:42 ` [LTP] [PATCH v2 2/3] mremap03: " Yang Xu via ltp
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Yang Xu via ltp @ 2024-02-27 8:42 UTC (permalink / raw)
To: ltp
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
testcases/kernel/syscalls/mremap/mremap02.c | 195 ++++----------------
1 file changed, 40 insertions(+), 155 deletions(-)
diff --git a/testcases/kernel/syscalls/mremap/mremap02.c b/testcases/kernel/syscalls/mremap/mremap02.c
index 2dabc6847..9e4a67c79 100644
--- a/testcases/kernel/syscalls/mremap/mremap02.c
+++ b/testcases/kernel/syscalls/mremap/mremap02.c
@@ -1,178 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
- *
- * Copyright (c) International Business Machines Corp., 2001
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (c) International Business Machines Corp., 2001
+ * Copyright (c) Linux Test Project, 2001-2024
+ * 07/2001 Ported by Wayne Boyer
*/
-/*
- * Test Name: mremap02
- *
- * Test Description:
- * Verify that,
- * mremap() fails when used to expand the existing virtual memory mapped
- * region to the requested size, if the virtual memory area previously
- * mapped was not page aligned or invalid argument specified.
- *
- * Expected Result:
- * mremap() should return -1 and set errno to EINVAL.
- *
- * Algorithm:
- * Setup:
- * Setup signal handling.
- * Pause for SIGUSR1 if option specified.
- *
- * Test:
- * Loop if the proper options are given.
- * Execute system call
- * Check return code, if system call failed (return=-1)
- * if errno set == expected errno
- * Issue sys call fails with expected return value and errno.
- * Otherwise,
- * Issue sys call fails with unexpected errno.
- * Otherwise,
- * Issue sys call returns unexpected value.
- *
- * Cleanup:
- * Print errno log and/or timing stats if options given
+/*\
+ * [Description]
*
- * Usage: <for command-line>
- * mremap02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- * where, -c n : Run n copies concurrently.
- * -e : Turn on errno logging.
- * -i n : Execute test n times.
- * -I x : Execute test for x seconds.
- * -p x : Pause for x seconds between iterations.
- * -t : Turn on syscall timing.
+ * Test for EINVAL error.
*
- * HISTORY
- * 07/2001 Ported by Wayne Boyer
- *
- * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
- * Modified.
- * - #include <linux/mman.h> should not be included as per man page for
- * mremap, #include <sys/mman.h> alone should do the job. But inorder
- * to include definition of MREMAP_MAYMOVE defined in bits/mman.h
- * (included by sys/mman.h) __USE_GNU needs to be defined.
- * There may be a more elegant way of doing this...
- *
- *
- * RESTRICTIONS:
- * None.
+ * - mremap fail with virtual memory area previously mapped was not page aligned.
*/
-#define _GNU_SOURCE
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-
-#include "test.h"
-char *TCID = "mremap02";
-int TST_TOTAL = 1;
-char *addr; /* addr of memory mapped region */
-int memsize; /* memory mapped size */
-int newsize; /* new size of virtual memory block */
+#define _GNU_SOURCE
+#include "tst_test.h"
-void setup(); /* Main setup function of test */
-void cleanup(); /* cleanup function for the test */
+static char *addr;
+static int memsize;
+static int newsize;
-int main(int ac, char **av)
+static void verify_mremap(void)
{
- int lc;
-
- tst_parse_opts(ac, av, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
+ errno = 0;
+ addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE);
+ TST_ERR = errno;
- tst_count = 0;
-
- /*
- * Attempt to expand the existing mapped
- * memory region (memsize) by newsize limits using
- * mremap() should fail as old virtual address is not
- * page aligned.
- */
- errno = 0;
- addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE);
- TEST_ERRNO = errno;
-
- /* Check for the return value of mremap() */
- if (addr != MAP_FAILED) {
- tst_resm(TFAIL,
- "mremap returned invalid value, expected: -1");
-
- /* Unmap the mapped memory region */
- if (munmap(addr, newsize) != 0) {
- tst_brkm(TBROK, cleanup, "munmap fails to "
- "unmap the expanded memory region, "
- "error=%d", errno);
- }
- continue;
- }
-
- if (errno == EINVAL) {
- tst_resm(TPASS, "mremap() Failed, 'invalid argument "
- "specified' - errno %d", TEST_ERRNO);
- } else {
- tst_resm(TFAIL, "mremap() Failed, "
- "'Unexpected errno %d", TEST_ERRNO);
- }
+ if (addr != MAP_FAILED) {
+ tst_res(TFAIL | TTERRNO,
+ "mremap returned invalid value, expected: -1");
}
- cleanup();
- tst_exit();
-
+ if (errno == EINVAL) {
+ tst_res(TPASS | TTERRNO, "mremap() Failed, 'invalid argument "
+ "specified' - errno %d", TST_ERR);
+ } else {
+ tst_res(TFAIL | TTERRNO, "mremap() Failed, "
+ "'Unexpected errno %d", TST_ERR);
+ }
}
-/*
- * setup() - performs all ONE TIME setup for this test.
- *
- * Get system page size, Set the size of virtual memory area and the
- * new size after resize, Set the virtual memory address such that it
- * is not aligned.
- */
-void setup(void)
-{
- tst_sig(FORK, DEF_HANDLER, cleanup);
-
- TEST_PAUSE;
-
- /* Get the system page size */
- if ((memsize = getpagesize()) < 0) {
- tst_brkm(TFAIL, NULL,
- "getpagesize() fails to get system page size");
- }
-
- /* Get the New size of virtual memory block after resize */
+static void setup(void)
+{
+ memsize = SAFE_SYSCONF(_SC_PAGESIZE);
+ addr = SAFE_MMAP(NULL, memsize, PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
newsize = (memsize * 2);
-
- /* Set the old virtual memory address */
addr = (char *)(addr + (memsize - 1));
}
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- * completion or premature exit.
- */
-void cleanup(void)
+static void cleanup(void)
{
-
- /* Exit the program */
+ if (addr != MAP_FAILED)
+ SAFE_MUNMAP(addr, newsize);
}
+static struct tst_test test = {
+ .test_all = verify_mremap,
+ .setup = setup,
+ .cleanup = cleanup,
+};
--
2.43.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [LTP] [PATCH v2 2/3] mremap03: Convert to new API
2024-02-27 8:42 [LTP] [PATCH v2 1/3] mremap02: Convert to new API Yang Xu via ltp
@ 2024-02-27 8:42 ` Yang Xu via ltp
2024-03-04 19:49 ` Avinesh Kumar
2024-02-27 8:42 ` [LTP] [PATCH v2 3/3] mremap04: " Yang Xu via ltp
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: Yang Xu via ltp @ 2024-02-27 8:42 UTC (permalink / raw)
To: ltp
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
testcases/kernel/syscalls/mremap/mremap03.c | 201 ++++----------------
1 file changed, 39 insertions(+), 162 deletions(-)
diff --git a/testcases/kernel/syscalls/mremap/mremap03.c b/testcases/kernel/syscalls/mremap/mremap03.c
index 02b79bc47..4e55dbda6 100644
--- a/testcases/kernel/syscalls/mremap/mremap03.c
+++ b/testcases/kernel/syscalls/mremap/mremap03.c
@@ -1,187 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
- *
- * Copyright (c) International Business Machines Corp., 2001
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (c) International Business Machines Corp., 2001
+ * Copyright (c) Linux Test Project, 2001-2024
+ * 07/2001 Ported by Wayne Boyer
*/
-/*
- * Test Name: mremap03
- *
- * Test Description:
- * Verify that,
- * mremap() fails when used to expand the existing virtual memory mapped
- * region to the requested size, if there already exists mappings that
- * cover the whole address space requsted or the old address specified was
- * not mapped.
- *
- * Expected Result:
- * mremap() should return -1 and set errno to EFAULT.
- *
- * Algorithm:
- * Setup:
- * Setup signal handling.
- * Pause for SIGUSR1 if option specified.
- *
- * Test:
- * Loop if the proper options are given.
- * Execute system call
- * Check return code, if system call failed (return=-1)
- * if errno set == expected errno
- * Issue sys call fails with expected return value and errno.
- * Otherwise,
- * Issue sys call fails with unexpected errno.
- * Otherwise,
- * Issue sys call returns unexpected value.
- *
- * Cleanup:
- * Print errno log and/or timing stats if options given
+/*\
+ * [Description]
*
- * Usage: <for command-line>
- * mremap03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- * where, -c n : Run n copies concurrently.
- * -e : Turn on errno logging.
- * -i n : Execute test n times.
- * -I x : Execute test for x seconds.
- * -p x : Pause for x seconds between iterations.
- * -t : Turn on syscall timing.
+ * Test for EFAULT error.
*
- * HISTORY
- * 07/2001 Ported by Wayne Boyer
- *
- * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
- * Modified.
- * - #include <linux/mman.h> should not be included as per man page for
- * mremap, #include <sys/mman.h> alone should do the job. But inorder
- * to include definition of MREMAP_MAYMOVE defined in bits/mman.h
- * (included by sys/mman.h) __USE_GNU needs to be defined.
- * There may be a more elegant way of doing this...
- *
- *
- * RESTRICTIONS:
- * None.
+ * - mremap fail with the old address specified was not mapped.
*/
-#define _GNU_SOURCE
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-#include "test.h"
+#define _GNU_SOURCE
+#include "tst_test.h"
-char *TCID = "mremap03";
-int TST_TOTAL = 1;
static char *bad_addr;
-static char *addr; /* addr of memory mapped region */
-int memsize; /* memory mapped size */
-int newsize; /* new size of virtual memory block */
+static char *addr;
+static int memsize;
+static int newsize;
-void setup(); /* Main setup function of test */
-void cleanup(); /* cleanup function for the test */
-
-int main(int ac, char **av)
+static void verify_mremap(void)
{
- int lc;
-
- tst_parse_opts(ac, av, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
-
- tst_count = 0;
+ errno = 0;
+ addr = mremap(bad_addr, memsize, newsize, MREMAP_MAYMOVE);
+ TST_ERR = errno;
- /*
- * Attempt to expand the existing mapped
- * memory region (memsize) by newsize limits
- * using mremap() should fail as specified old
- * virtual address was not mapped.
- */
- errno = 0;
- addr = mremap(bad_addr, memsize, newsize, MREMAP_MAYMOVE);
- TEST_ERRNO = errno;
-
- /* Check for the return value of mremap() */
- if (addr != MAP_FAILED) {
- tst_resm(TFAIL,
- "mremap returned invalid value, expected: -1");
-
- /* Unmap the mapped memory region */
- if (munmap(addr, newsize) != 0) {
- tst_brkm(TFAIL, cleanup, "munmap fails to "
- "unmap the expanded memory region, "
- " error=%d", errno);
- }
- continue;
- }
-
- /* Check for the expected errno */
- if (errno == EFAULT) {
- tst_resm(TPASS, "mremap() Fails, 'old region not "
- "mapped', errno %d", TEST_ERRNO);
- } else {
- tst_resm(TFAIL, "mremap() Fails, "
- "'Unexpected errno %d", TEST_ERRNO);
- }
+ if (addr != MAP_FAILED) {
+ tst_res(TFAIL | TTERRNO,
+ "mremap returned invalid value, expected: -1");
}
- cleanup();
- tst_exit();
-
+ if (errno == EFAULT) {
+ tst_res(TPASS | TTERRNO, "mremap() Failed, 'old region not "
+ "mapped' - errno %d", TST_ERR);
+ } else {
+ tst_res(TFAIL | TTERRNO, "mremap() Failed, "
+ "'Unexpected errno %d", TST_ERR);
+ }
}
-/*
- * setup() - performs all ONE TIME setup for this test.
- *
- * Get system page size.
- * Set the old address point some high address which is not mapped.
- */
void setup(void)
{
- int page_sz; /* system page size */
-
- tst_sig(FORK, DEF_HANDLER, cleanup);
-
- TEST_PAUSE;
+ int page_sz;
- /* Get the system page size */
- if ((page_sz = getpagesize()) < 0) {
- tst_brkm(TFAIL, NULL,
- "getpagesize() fails to get system page size");
- }
-
- /* Get the size of virtual memory area to be mapped */
+ page_sz = SAFE_SYSCONF(_SC_PAGESIZE);
memsize = (1000 * page_sz);
-
- /* Get the New size of virtual memory block after resize */
newsize = (memsize * 2);
-
- /*
- * Set the old virtual address point to some address
- * which is not mapped.
- */
- bad_addr = tst_get_bad_addr(cleanup);
+ bad_addr = tst_get_bad_addr(NULL);
}
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- * completion or premature exit.
- */
-void cleanup(void)
+static void cleanup(void)
{
-
- /* Exit the program */
-
+ if (addr != MAP_FAILED)
+ SAFE_MUNMAP(addr, newsize);
}
+
+static struct tst_test test = {
+ .test_all = verify_mremap,
+ .setup = setup,
+ .cleanup = cleanup,
+};
--
2.43.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [LTP] [PATCH v2 3/3] mremap04: Convert to new API
2024-02-27 8:42 [LTP] [PATCH v2 1/3] mremap02: Convert to new API Yang Xu via ltp
2024-02-27 8:42 ` [LTP] [PATCH v2 2/3] mremap03: " Yang Xu via ltp
@ 2024-02-27 8:42 ` Yang Xu via ltp
2024-03-04 20:49 ` Avinesh Kumar
2024-03-01 12:31 ` [LTP] [PATCH v2 1/3] mremap02: " Avinesh Kumar
2024-03-01 13:06 ` Martin Doucha
3 siblings, 1 reply; 7+ messages in thread
From: Yang Xu via ltp @ 2024-02-27 8:42 UTC (permalink / raw)
To: ltp
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
testcases/kernel/syscalls/mremap/Makefile | 4 +-
testcases/kernel/syscalls/mremap/mremap04.c | 269 ++++----------------
2 files changed, 53 insertions(+), 220 deletions(-)
diff --git a/testcases/kernel/syscalls/mremap/Makefile b/testcases/kernel/syscalls/mremap/Makefile
index 190b7659d..f286dd9bf 100644
--- a/testcases/kernel/syscalls/mremap/Makefile
+++ b/testcases/kernel/syscalls/mremap/Makefile
@@ -3,10 +3,10 @@
top_srcdir ?= ../../../..
-LTPLIBS = ltpipc
+LTPLIBS = ltpnewipc
include $(top_srcdir)/include/mk/testcases.mk
-mremap04: LTPLDLIBS = -lltpipc
+mremap04: LTPLDLIBS = -lltpnewipc
include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/mremap/mremap04.c b/testcases/kernel/syscalls/mremap/mremap04.c
index 53902df73..8d819184e 100644
--- a/testcases/kernel/syscalls/mremap/mremap04.c
+++ b/testcases/kernel/syscalls/mremap/mremap04.c
@@ -1,245 +1,78 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
- *
- * Copyright (c) International Business Machines Corp., 2001
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (c) International Business Machines Corp., 2001
+ * Copyright (c) Linux Test Project, 2001-2024
+ * 07/2001 Ported by Wayne Boyer
*/
-/*
- * Test Name: mremap04
- *
- * Test Description:
- * Verify that,
- * mremap() fails when used to expand the existing virtual memory mapped
- * region to the requested size, if the memory area cannot be expanded at
- * the current virtual address and MREMAP_MAYMOVE flag not set.
- *
- * Expected Result:
- * mremap() should return -1 and set errno to ENOMEM.
- *
- * Algorithm:
- * Setup:
- * Setup signal handling.
- * Create temporary directory.
- * Pause for SIGUSR1 if option specified.
- *
- * Test:
- * Loop if the proper options are given.
- * Execute system call
- * Check return code, if system call failed (return=-1)
- * if errno set == expected errno
- * Issue sys call failed with expected return value and errno.
- * Otherwise,
- * Issue sys call failed with unexpected errno.
- * Otherwise,
- * Issue sys call returns unexpected value.
- *
- * Cleanup:
- * Print errno log and/or timing stats if options given
- * Delete the temporary directory(s)/file(s) created.
- *
- * Usage: <for command-line>
- * mremap04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- * where, -c n : Run n copies concurrently.
- * -e : Turn on errno logging.
- * -i n : Execute test n times.
- * -I x : Execute test for x seconds.
- * -p x : Pause for x seconds between iterations.
- * -t : Turn on syscall timing.
- *
- * HISTORY
- * 07/2001 Ported by Wayne Boyer
+/*\
+ * [Description]
*
- * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
- * Modified.
- * - #include <linux/mman.h> should not be included as per man page for
- * mremap, #include <sys/mman.h> alone should do the job. But inorder
- * to include definition of MREMAP_MAYMOVE defined in bits/mman.h
- * (included by sys/mman.h) __USE_GNU needs to be defined.
- * There may be a more elegant way of doing this...
+ * Test for ENOMEM error.
*
- * 26/02/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
- * - Fix concurrency issue. Use a shm key from getipckey instead of
- * a fixed hard-coded value.
- *
- * RESTRICTIONS:
- * None.
+ * - mremap fail when the memory area cannot be expanded at the current virtual address
+ * and MREMAP_MAYMOVE flag not set
*/
+
#define _GNU_SOURCE
-#include <errno.h>
-#include <unistd.h>
-#include <sys/mman.h>
#include <sys/ipc.h>
#include <sys/shm.h>
+#include "tst_test.h"
+#include "libnewipc.h"
+#include "tst_safe_sysv_ipc.h"
-#include "test.h"
-
-#define SHM_MODE (SHM_R | SHM_W) /* mode permissions of shared memory */
+#define SHM_MODE (SHM_R | SHM_W)
-char *TCID = "mremap04";
-int TST_TOTAL = 1;
-char *addr; /* addr of memory mapped region */
-char *shmaddr; /* pointer to shared memory segment */
-int shmid; /* shared memory identifier. */
-int memsize; /* memory mapped size */
-int newsize; /* new size of virtual memory block */
+static char *addr;
+static char *shmaddr;
+static int shm_id;
+static int memsize;
+static int newsize;
-void setup(); /* Main setup function of test */
-void cleanup(); /* cleanup function for the test */
-
-extern int getipckey();
-
-int main(int ac, char **av)
+static void verify_mremap(void)
{
- int lc;
-
- tst_parse_opts(ac, av, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
-
- tst_count = 0;
-
- /*
- * Attempt to expand the existing shared
- * memory region of newsize by newsize limits
- * using mremap() should fail as specified
- * memory area already locked and MREMAP_MAYMOVE
- * flag unset.
- */
- errno = 0;
- addr = mremap(shmaddr, memsize, newsize, 0);
- TEST_ERRNO = errno;
-
- /* Check for the return value of mremap() */
- if (addr != MAP_FAILED) {
- tst_resm(TFAIL,
- "mremap returned invalid value, expected: -1");
-
- /* Unmap the mapped memory region */
- if (munmap(addr, newsize) != 0) {
- tst_brkm(TFAIL, cleanup, "munmap failed to "
- "unmap the expanded memory region, "
- "error=%d", errno);
- }
- continue;
- }
+ errno = 0;
+ addr = mremap(shmaddr, memsize, newsize, 0);
+ TST_ERR = errno;
- if (TEST_ERRNO == ENOMEM) {
- tst_resm(TPASS, "mremap() failed, "
- "'MREMAP_MAYMOVE flag unset', "
- "errno %d", TEST_ERRNO);
- } else {
- tst_resm(TFAIL, "mremap() failed, "
- "Unexpected errno %d", TEST_ERRNO);
- }
+ if (addr != MAP_FAILED) {
+ tst_res(TFAIL | TTERRNO,
+ "mremap returned invalid value, expected: -1");
}
- cleanup();
- tst_exit();
-
+ if (TST_ERR == ENOMEM) {
+ tst_res(TPASS | TTERRNO, "mremap() failed, "
+ "'MREMAP_MAYMOVE flag unset', "
+ "errno %d", TST_ERR);
+ } else {
+ tst_res(TFAIL | TTERRNO, "mremap() failed, "
+ "Unexpected errno %d", TST_ERR);
+ }
}
-/*
- * setup() - performs all ONE TIME setup for this test.
- *
- * Get system page size, Set the size of virtual memory area and the
- * newsize after resize,
- * Create a named shared memory segment SHMKEY of newsize and mode SHM_MODE
- * by using shmget() which returns a shared memory identifier associated
- * with the created shared memory segment.
- * Call shmat() to attach the shared memory segment to the data segment of the
- * calling process. The segment is attached at the first available address as
- * selected by the system.
- */
-void setup(void)
+static void setup(void)
{
key_t shmkey;
- tst_sig(FORK, DEF_HANDLER, cleanup);
-
- TEST_PAUSE;
-
- tst_tmpdir();
-
- /* Get the system page size */
- if ((memsize = getpagesize()) < 0) {
- tst_brkm(TBROK, NULL,
- "getpagesize() failed to get system page size");
- }
-
- /* Get the New size of virtual memory block after resize */
+ memsize = SAFE_SYSCONF(_SC_PAGESIZE);
newsize = (memsize * 2);
-
- /* get an IPC resource key */
- shmkey = getipckey();
-
- /*
- * Create a shared memory segment represented by SHMKEY of
- * specified size 'newsize' and mode permissions 'SHM_MODE'.
- */
- shmid = shmget(shmkey, newsize, IPC_CREAT | SHM_MODE);
- if (shmid == -1) {
- tst_brkm(TBROK, NULL, "shmget() Failed to create a shared "
- "memory, error:%d", errno);
- }
-
- /*
- * Attach the shared memory segment associated with the shared
- * memory identifier specified by "shmid" to the data segment of
- * the calling process at the first available address as selected
- * by the system.
- */
- shmaddr = shmat(shmid, NULL, 0);
- if (shmaddr == (void *)-1) {
- tst_brkm(TBROK, cleanup, "shmat() Failed to attach shared "
- "memory, error:%d", errno);
- }
+ shmkey = GETIPCKEY();
+ shm_id = SAFE_SHMGET(shmkey, newsize, IPC_CREAT | SHM_MODE);
+ shmaddr = SAFE_SHMAT(shm_id, NULL, 0);
}
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- * completion or premature exit.
- * Detach the shared memory segment and remove the shared memory
- * identifier associated with the shared memory.
- */
-void cleanup(void)
+static void cleanup(void)
{
+ SAFE_SHMDT(shmaddr);
+ SAFE_SHMCTL(shm_id, IPC_RMID, 0);
- /*
- * Detach the shared memory segment attached to
- * the calling process's data segment
- */
- if (shmdt(shmaddr) < 0) {
- tst_brkm(TFAIL, NULL, "shmdt() Failed to detach shared "
- "memory, error:%d", errno);
- }
-
- /*
- * Remove the shared memory identifier associated with
- * the shared memory segment and destroy the shared memory
- * segment.
- */
- if (shmctl(shmid, IPC_RMID, 0) < 0) {
- tst_brkm(TFAIL, NULL, "shmctl() Failed to remove shared "
- "memory, error:%d", errno);
- }
-
- tst_rmdir();
-
- /* Exit the program */
-
+ if (addr != MAP_FAILED)
+ SAFE_MUNMAP(addr, newsize);
}
+
+static struct tst_test test = {
+ .setup = setup,
+ .cleanup = cleanup,
+ .needs_tmpdir = 1,
+ .test_all = verify_mremap,
+};
--
2.43.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [LTP] [PATCH v2 1/3] mremap02: Convert to new API
2024-02-27 8:42 [LTP] [PATCH v2 1/3] mremap02: Convert to new API Yang Xu via ltp
2024-02-27 8:42 ` [LTP] [PATCH v2 2/3] mremap03: " Yang Xu via ltp
2024-02-27 8:42 ` [LTP] [PATCH v2 3/3] mremap04: " Yang Xu via ltp
@ 2024-03-01 12:31 ` Avinesh Kumar
2024-03-01 13:06 ` Martin Doucha
3 siblings, 0 replies; 7+ messages in thread
From: Avinesh Kumar @ 2024-03-01 12:31 UTC (permalink / raw)
To: Yang Xu; +Cc: ltp
On Tuesday, February 27, 2024 9:42:42 AM CET Yang Xu via ltp wrote:
> Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
> ---
> testcases/kernel/syscalls/mremap/mremap02.c | 195 ++++----------------
> 1 file changed, 40 insertions(+), 155 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/mremap/mremap02.c
> b/testcases/kernel/syscalls/mremap/mremap02.c index 2dabc6847..9e4a67c79
> 100644
> --- a/testcases/kernel/syscalls/mremap/mremap02.c
> +++ b/testcases/kernel/syscalls/mremap/mremap02.c
> @@ -1,178 +1,63 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> /*
> - *
> - * Copyright (c) International Business Machines Corp., 2001
> - *
> - * This program is free software; you can redistribute it and/or modify
> - * it under the terms of the GNU General Public License as published by
> - * the Free Software Foundation; either version 2 of the License, or
> - * (at your option) any later version.
> - *
> - * This program 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 General Public License for more details.
> - *
> - * You should have received a copy of the GNU General Public License
> - * along with this program; if not, write to the Free Software
> - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
> 02110-1301 USA + * Copyright (c) International Business Machines Corp.,
> 2001
> + * Copyright (c) Linux Test Project, 2001-2024
> + * 07/2001 Ported by Wayne Boyer
> */
>
> -/*
> - * Test Name: mremap02
> - *
> - * Test Description:
> - * Verify that,
> - * mremap() fails when used to expand the existing virtual memory mapped
> - * region to the requested size, if the virtual memory area previously
> - * mapped was not page aligned or invalid argument specified.
> - *
> - * Expected Result:
> - * mremap() should return -1 and set errno to EINVAL.
> - *
> - * Algorithm:
> - * Setup:
> - * Setup signal handling.
> - * Pause for SIGUSR1 if option specified.
> - *
> - * Test:
> - * Loop if the proper options are given.
> - * Execute system call
> - * Check return code, if system call failed (return=-1)
> - * if errno set == expected errno
> - * Issue sys call fails with expected return value and errno.
> - * Otherwise,
> - * Issue sys call fails with unexpected errno.
> - * Otherwise,
> - * Issue sys call returns unexpected value.
> - *
> - * Cleanup:
> - * Print errno log and/or timing stats if options given
> +/*\
> + * [Description]
> *
> - * Usage: <for command-line>
> - * mremap02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
> - * where, -c n : Run n copies concurrently.
> - * -e : Turn on errno logging.
> - * -i n : Execute test n times.
> - * -I x : Execute test for x seconds.
> - * -p x : Pause for x seconds between iterations.
> - * -t : Turn on syscall timing.
> + * Test for EINVAL error.
> *
> - * HISTORY
> - * 07/2001 Ported by Wayne Boyer
> - *
> - * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
> - * Modified.
> - * - #include <linux/mman.h> should not be included as per man page
> for - * mremap, #include <sys/mman.h> alone should do the job. But
> inorder - * to include definition of MREMAP_MAYMOVE defined in
> bits/mman.h - * (included by sys/mman.h) __USE_GNU needs to be
> defined. - * There may be a more elegant way of doing this...
> - *
> - *
> - * RESTRICTIONS:
> - * None.
> + * - mremap fail with virtual memory area previously mapped was not page
> aligned. */
> -#define _GNU_SOURCE
> -#include <errno.h>
> -#include <unistd.h>
> -#include <fcntl.h>
> -#include <sys/mman.h>
> -
> -#include "test.h"
>
> -char *TCID = "mremap02";
> -int TST_TOTAL = 1;
> -char *addr; /* addr of memory mapped region */
> -int memsize; /* memory mapped size */
> -int newsize; /* new size of virtual memory block */
> +#define _GNU_SOURCE
> +#include "tst_test.h"
>
> -void setup(); /* Main setup function of test */
> -void cleanup(); /* cleanup function for the test */
> +static char *addr;
> +static int memsize;
> +static int newsize;
>
> -int main(int ac, char **av)
> +static void verify_mremap(void)
> {
> - int lc;
> -
> - tst_parse_opts(ac, av, NULL, NULL);
> -
> - setup();
> -
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> + errno = 0;
> + addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE);
> + TST_ERR = errno;
>
> - tst_count = 0;
> -
> - /*
> - * Attempt to expand the existing mapped
> - * memory region (memsize) by newsize limits using
> - * mremap() should fail as old virtual address is not
> - * page aligned.
> - */
> - errno = 0;
> - addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE);
> - TEST_ERRNO = errno;
> -
> - /* Check for the return value of mremap() */
> - if (addr != MAP_FAILED) {
> - tst_resm(TFAIL,
> - "mremap returned invalid value, expected: -1");
should be - "expected: (void *)-1"
> -
> - /* Unmap the mapped memory region */
> - if (munmap(addr, newsize) != 0) {
> - tst_brkm(TBROK, cleanup, "munmap fails to "
> - "unmap the expanded memory region, "
> - "error=%d", errno);
> - }
> - continue;
> - }
> -
> - if (errno == EINVAL) {
> - tst_resm(TPASS, "mremap() Failed, 'invalid argument "
> - "specified' - errno %d", TEST_ERRNO);
> - } else {
> - tst_resm(TFAIL, "mremap() Failed, "
> - "'Unexpected errno %d", TEST_ERRNO);
> - }
> + if (addr != MAP_FAILED) {
> + tst_res(TFAIL | TTERRNO,
> + "mremap returned invalid value, expected: -1");
> }
>
> - cleanup();
> - tst_exit();
> -
> + if (errno == EINVAL) {
> + tst_res(TPASS | TTERRNO, "mremap() Failed, 'invalid argument "
> + "specified' - errno %d", TST_ERR);
I think, 'errno %d", TST_ERR)' is not required if we are using TTERRNO here.
> + } else {
> + tst_res(TFAIL | TTERRNO, "mremap() Failed, "
> + "'Unexpected errno %d", TST_ERR);
> + }
> }
>
> -/*
> - * setup() - performs all ONE TIME setup for this test.
> - *
> - * Get system page size, Set the size of virtual memory area and the
> - * new size after resize, Set the virtual memory address such that it
> - * is not aligned.
> - */
> -void setup(void)
> -{
>
> - tst_sig(FORK, DEF_HANDLER, cleanup);
> -
> - TEST_PAUSE;
> -
> - /* Get the system page size */
> - if ((memsize = getpagesize()) < 0) {
> - tst_brkm(TFAIL, NULL,
> - "getpagesize() fails to get system page size");
> - }
> -
> - /* Get the New size of virtual memory block after resize */
> +static void setup(void)
> +{
> + memsize = SAFE_SYSCONF(_SC_PAGESIZE);
> + addr = SAFE_MMAP(NULL, memsize, PROT_WRITE,
> + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
> newsize = (memsize * 2);
> -
> - /* Set the old virtual memory address */
> addr = (char *)(addr + (memsize - 1));
We should use a different variable and not overwrite addr so we can
successfully munmap in cleanup().
> }
>
> -/*
> - * cleanup() - performs all ONE TIME cleanup for this test at
> - * completion or premature exit.
> - */
> -void cleanup(void)
> +static void cleanup(void)
> {
> -
> - /* Exit the program */
> + if (addr != MAP_FAILED)
> + SAFE_MUNMAP(addr, newsize);
we need to unmap the original mapping which we did in setup() and if the
testcase is failing (remap() somehow succeeds) then we need to munmap the
new mapping inside the main test function.
>
> }
> +static struct tst_test test = {
> + .test_all = verify_mremap,
> + .setup = setup,
> + .cleanup = cleanup,
> +};
Regards,
Avinesh
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [LTP] [PATCH v2 1/3] mremap02: Convert to new API
2024-02-27 8:42 [LTP] [PATCH v2 1/3] mremap02: Convert to new API Yang Xu via ltp
` (2 preceding siblings ...)
2024-03-01 12:31 ` [LTP] [PATCH v2 1/3] mremap02: " Avinesh Kumar
@ 2024-03-01 13:06 ` Martin Doucha
3 siblings, 0 replies; 7+ messages in thread
From: Martin Doucha @ 2024-03-01 13:06 UTC (permalink / raw)
To: Yang Xu, ltp
Hi,
some suggestions below.
On 27. 02. 24 9:42, Yang Xu via ltp wrote:
> Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
> ---
> testcases/kernel/syscalls/mremap/mremap02.c | 195 ++++----------------
> 1 file changed, 40 insertions(+), 155 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/mremap/mremap02.c b/testcases/kernel/syscalls/mremap/mremap02.c
> index 2dabc6847..9e4a67c79 100644
> --- a/testcases/kernel/syscalls/mremap/mremap02.c
> +++ b/testcases/kernel/syscalls/mremap/mremap02.c
> @@ -1,178 +1,63 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> /*
> - *
> - * Copyright (c) International Business Machines Corp., 2001
> - *
> - * This program is free software; you can redistribute it and/or modify
> - * it under the terms of the GNU General Public License as published by
> - * the Free Software Foundation; either version 2 of the License, or
> - * (at your option) any later version.
> - *
> - * This program 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 General Public License for more details.
> - *
> - * You should have received a copy of the GNU General Public License
> - * along with this program; if not, write to the Free Software
> - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + * Copyright (c) International Business Machines Corp., 2001
> + * Copyright (c) Linux Test Project, 2001-2024
> + * 07/2001 Ported by Wayne Boyer
> */
>
> -/*
> - * Test Name: mremap02
> - *
> - * Test Description:
> - * Verify that,
> - * mremap() fails when used to expand the existing virtual memory mapped
> - * region to the requested size, if the virtual memory area previously
> - * mapped was not page aligned or invalid argument specified.
> - *
> - * Expected Result:
> - * mremap() should return -1 and set errno to EINVAL.
> - *
> - * Algorithm:
> - * Setup:
> - * Setup signal handling.
> - * Pause for SIGUSR1 if option specified.
> - *
> - * Test:
> - * Loop if the proper options are given.
> - * Execute system call
> - * Check return code, if system call failed (return=-1)
> - * if errno set == expected errno
> - * Issue sys call fails with expected return value and errno.
> - * Otherwise,
> - * Issue sys call fails with unexpected errno.
> - * Otherwise,
> - * Issue sys call returns unexpected value.
> - *
> - * Cleanup:
> - * Print errno log and/or timing stats if options given
> +/*\
> + * [Description]
> *
> - * Usage: <for command-line>
> - * mremap02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
> - * where, -c n : Run n copies concurrently.
> - * -e : Turn on errno logging.
> - * -i n : Execute test n times.
> - * -I x : Execute test for x seconds.
> - * -p x : Pause for x seconds between iterations.
> - * -t : Turn on syscall timing.
> + * Test for EINVAL error.
> *
> - * HISTORY
> - * 07/2001 Ported by Wayne Boyer
> - *
> - * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
> - * Modified.
> - * - #include <linux/mman.h> should not be included as per man page for
> - * mremap, #include <sys/mman.h> alone should do the job. But inorder
> - * to include definition of MREMAP_MAYMOVE defined in bits/mman.h
> - * (included by sys/mman.h) __USE_GNU needs to be defined.
> - * There may be a more elegant way of doing this...
> - *
> - *
> - * RESTRICTIONS:
> - * None.
> + * - mremap fail with virtual memory area previously mapped was not page aligned.
> */
> -#define _GNU_SOURCE
> -#include <errno.h>
> -#include <unistd.h>
> -#include <fcntl.h>
> -#include <sys/mman.h>
> -
> -#include "test.h"
>
> -char *TCID = "mremap02";
> -int TST_TOTAL = 1;
> -char *addr; /* addr of memory mapped region */
> -int memsize; /* memory mapped size */
> -int newsize; /* new size of virtual memory block */
> +#define _GNU_SOURCE
> +#include "tst_test.h"
>
> -void setup(); /* Main setup function of test */
> -void cleanup(); /* cleanup function for the test */
> +static char *addr;
> +static int memsize;
> +static int newsize;
>
> -int main(int ac, char **av)
> +static void verify_mremap(void)
> {
> - int lc;
> -
> - tst_parse_opts(ac, av, NULL, NULL);
> -
> - setup();
> -
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> + errno = 0;
> + addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE);
> + TST_ERR = errno;
You can simply use TESTPTR(mremap(...)); here and then check TST_RET_PTR
value. That macro will handle errno for you.
>
> - tst_count = 0;
> -
> - /*
> - * Attempt to expand the existing mapped
> - * memory region (memsize) by newsize limits using
> - * mremap() should fail as old virtual address is not
> - * page aligned.
> - */
> - errno = 0;
> - addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE);
> - TEST_ERRNO = errno;
> -
> - /* Check for the return value of mremap() */
> - if (addr != MAP_FAILED) {
> - tst_resm(TFAIL,
> - "mremap returned invalid value, expected: -1");
> -
> - /* Unmap the mapped memory region */
> - if (munmap(addr, newsize) != 0) {
> - tst_brkm(TBROK, cleanup, "munmap fails to "
> - "unmap the expanded memory region, "
> - "error=%d", errno);
> - }
> - continue;
> - }
> -
> - if (errno == EINVAL) {
> - tst_resm(TPASS, "mremap() Failed, 'invalid argument "
> - "specified' - errno %d", TEST_ERRNO);
> - } else {
> - tst_resm(TFAIL, "mremap() Failed, "
> - "'Unexpected errno %d", TEST_ERRNO);
> - }
> + if (addr != MAP_FAILED) {
> + tst_res(TFAIL | TTERRNO,
> + "mremap returned invalid value, expected: -1");
The expected value is MAP_FAILED. It's value might theoretically be
different from -1.
Also, you should unmap the new mapping here, set addr = MAP_FAILED; and
return from the function. The errno checks are pointless if the call
succeeded.
You should also consider either using tst_brk(TBROK) here or moving the
initial SAFE_MMAP() from setup() to the start of this function.
Otherwise when the test runs in multiple loops (-i/-I parameters), a
successful mremap() will break the test mapping for all further loops.
> }
>
> - cleanup();
> - tst_exit();
> -
> + if (errno == EINVAL) {
You want to check TST_ERR here.
> + tst_res(TPASS | TTERRNO, "mremap() Failed, 'invalid argument "
> + "specified' - errno %d", TST_ERR);
tst_res() will print the error code and name automatically when you pass
the TTERRNO flag.
> + } else {
> + tst_res(TFAIL | TTERRNO, "mremap() Failed, "
> + "'Unexpected errno %d", TST_ERR);
> + }
> }
>
> -/*
> - * setup() - performs all ONE TIME setup for this test.
> - *
> - * Get system page size, Set the size of virtual memory area and the
> - * new size after resize, Set the virtual memory address such that it
> - * is not aligned.
> - */
> -void setup(void)
> -{
>
> - tst_sig(FORK, DEF_HANDLER, cleanup);
> -
> - TEST_PAUSE;
> -
> - /* Get the system page size */
> - if ((memsize = getpagesize()) < 0) {
> - tst_brkm(TFAIL, NULL,
> - "getpagesize() fails to get system page size");
> - }
> -
> - /* Get the New size of virtual memory block after resize */
> +static void setup(void)
> +{
> + memsize = SAFE_SYSCONF(_SC_PAGESIZE);
> + addr = SAFE_MMAP(NULL, memsize, PROT_WRITE,
> + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
> newsize = (memsize * 2);
> -
> - /* Set the old virtual memory address */
> addr = (char *)(addr + (memsize - 1));
You should keep the original pointer for cleanup(). It'd be much better
to explicitly call mremap(addr + offset, ...) in the main test function.
> }
>
> -/*
> - * cleanup() - performs all ONE TIME cleanup for this test at
> - * completion or premature exit.
> - */
> -void cleanup(void)
> +static void cleanup(void)
> {
> -
> - /* Exit the program */
> + if (addr != MAP_FAILED)
> + SAFE_MUNMAP(addr, newsize);
This should be SAFE_MUNMAP(addr, memsize); The mapping size is not
supposed to change during the test and the next page may also be mapped
e.g. by libc.
>
> }
> +static struct tst_test test = {
> + .test_all = verify_mremap,
> + .setup = setup,
> + .cleanup = cleanup,
> +};
--
Martin Doucha mdoucha@suse.cz
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [LTP] [PATCH v2 2/3] mremap03: Convert to new API
2024-02-27 8:42 ` [LTP] [PATCH v2 2/3] mremap03: " Yang Xu via ltp
@ 2024-03-04 19:49 ` Avinesh Kumar
0 siblings, 0 replies; 7+ messages in thread
From: Avinesh Kumar @ 2024-03-04 19:49 UTC (permalink / raw)
To: Yang Xu; +Cc: ltp
Hi Yang Xu,
some comments below.
On Tuesday, February 27, 2024 9:42:43 AM CET Yang Xu via ltp wrote:
> Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
> ---
> testcases/kernel/syscalls/mremap/mremap03.c | 201 ++++----------------
> 1 file changed, 39 insertions(+), 162 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/mremap/mremap03.c
> b/testcases/kernel/syscalls/mremap/mremap03.c index 02b79bc47..4e55dbda6
> 100644
> --- a/testcases/kernel/syscalls/mremap/mremap03.c
> +++ b/testcases/kernel/syscalls/mremap/mremap03.c
> @@ -1,187 +1,64 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> /*
> - *
> - * Copyright (c) International Business Machines Corp., 2001
> - *
> - * This program is free software; you can redistribute it and/or modify
> - * it under the terms of the GNU General Public License as published by
> - * the Free Software Foundation; either version 2 of the License, or
> - * (at your option) any later version.
> - *
> - * This program 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 General Public License for more details.
> - *
> - * You should have received a copy of the GNU General Public License
> - * along with this program; if not, write to the Free Software
> - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
> 02110-1301 USA + * Copyright (c) International Business Machines Corp.,
> 2001
> + * Copyright (c) Linux Test Project, 2001-2024
> + * 07/2001 Ported by Wayne Boyer
> */
>
> -/*
> - * Test Name: mremap03
> - *
> - * Test Description:
> - * Verify that,
> - * mremap() fails when used to expand the existing virtual memory mapped
> - * region to the requested size, if there already exists mappings that
> - * cover the whole address space requsted or the old address specified
> was - * not mapped.
> - *
> - * Expected Result:
> - * mremap() should return -1 and set errno to EFAULT.
> - *
> - * Algorithm:
> - * Setup:
> - * Setup signal handling.
> - * Pause for SIGUSR1 if option specified.
> - *
> - * Test:
> - * Loop if the proper options are given.
> - * Execute system call
> - * Check return code, if system call failed (return=-1)
> - * if errno set == expected errno
> - * Issue sys call fails with expected return value and errno.
> - * Otherwise,
> - * Issue sys call fails with unexpected errno.
> - * Otherwise,
> - * Issue sys call returns unexpected value.
> - *
> - * Cleanup:
> - * Print errno log and/or timing stats if options given
> +/*\
> + * [Description]
> *
> - * Usage: <for command-line>
> - * mremap03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
> - * where, -c n : Run n copies concurrently.
> - * -e : Turn on errno logging.
> - * -i n : Execute test n times.
> - * -I x : Execute test for x seconds.
> - * -p x : Pause for x seconds between iterations.
> - * -t : Turn on syscall timing.
> + * Test for EFAULT error.
> *
> - * HISTORY
> - * 07/2001 Ported by Wayne Boyer
> - *
> - * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
> - * Modified.
> - * - #include <linux/mman.h> should not be included as per man page
> for - * mremap, #include <sys/mman.h> alone should do the job. But
> inorder - * to include definition of MREMAP_MAYMOVE defined in
> bits/mman.h - * (included by sys/mman.h) __USE_GNU needs to be
> defined. - * There may be a more elegant way of doing this...
> - *
> - *
> - * RESTRICTIONS:
> - * None.
> + * - mremap fail with the old address specified was not mapped.
> */
> -#define _GNU_SOURCE
> -#include <errno.h>
> -#include <unistd.h>
> -#include <fcntl.h>
> -#include <sys/mman.h>
>
> -#include "test.h"
> +#define _GNU_SOURCE
> +#include "tst_test.h"
>
> -char *TCID = "mremap03";
> -int TST_TOTAL = 1;
> static char *bad_addr;
> -static char *addr; /* addr of memory mapped region */
> -int memsize; /* memory mapped size */
> -int newsize; /* new size of virtual memory block */
> +static char *addr;
> +static int memsize;
> +static int newsize;
>
> -void setup(); /* Main setup function of test */
> -void cleanup(); /* cleanup function for the test */
> -
> -int main(int ac, char **av)
> +static void verify_mremap(void)
> {
> - int lc;
> -
> - tst_parse_opts(ac, av, NULL, NULL);
> -
> - setup();
> -
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> -
> - tst_count = 0;
> + errno = 0;
> + addr = mremap(bad_addr, memsize, newsize, MREMAP_MAYMOVE);
> + TST_ERR = errno;
We can simplify by using TESTPTR() macro.
>
> - /*
> - * Attempt to expand the existing mapped
> - * memory region (memsize) by newsize limits
> - * using mremap() should fail as specified old
> - * virtual address was not mapped.
> - */
> - errno = 0;
> - addr = mremap(bad_addr, memsize, newsize, MREMAP_MAYMOVE);
> - TEST_ERRNO = errno;
> -
> - /* Check for the return value of mremap() */
> - if (addr != MAP_FAILED) {
> - tst_resm(TFAIL,
> - "mremap returned invalid value, expected: -1");
> -
> - /* Unmap the mapped memory region */
> - if (munmap(addr, newsize) != 0) {
> - tst_brkm(TFAIL, cleanup, "munmap fails to "
> - "unmap the expanded memory region, "
> - " error=%d", errno);
> - }
> - continue;
> - }
> -
> - /* Check for the expected errno */
> - if (errno == EFAULT) {
> - tst_resm(TPASS, "mremap() Fails, 'old region not "
> - "mapped', errno %d", TEST_ERRNO);
> - } else {
> - tst_resm(TFAIL, "mremap() Fails, "
> - "'Unexpected errno %d", TEST_ERRNO);
> - }
> + if (addr != MAP_FAILED) {
> + tst_res(TFAIL | TTERRNO,
> + "mremap returned invalid value, expected: -1");
> }
>
> - cleanup();
> - tst_exit();
> -
> + if (errno == EFAULT) {
> + tst_res(TPASS | TTERRNO, "mremap() Failed, 'old region not "
> + "mapped' - errno %d", TST_ERR);
> + } else {
> + tst_res(TFAIL | TTERRNO, "mremap() Failed, "
> + "'Unexpected errno %d", TST_ERR);
> + }
> }
>
> -/*
> - * setup() - performs all ONE TIME setup for this test.
> - *
> - * Get system page size.
> - * Set the old address point some high address which is not mapped.
> - */
> void setup(void)
missing static
> {
> - int page_sz; /* system page size */
> -
> - tst_sig(FORK, DEF_HANDLER, cleanup);
> -
> - TEST_PAUSE;
> + int page_sz;
>
> - /* Get the system page size */
> - if ((page_sz = getpagesize()) < 0) {
> - tst_brkm(TFAIL, NULL,
> - "getpagesize() fails to get system page size");
> - }
> -
> - /* Get the size of virtual memory area to be mapped */
> + page_sz = SAFE_SYSCONF(_SC_PAGESIZE);
> memsize = (1000 * page_sz);
> -
> - /* Get the New size of virtual memory block after resize */
> newsize = (memsize * 2);
> -
> - /*
> - * Set the old virtual address point to some address
> - * which is not mapped.
> - */
> - bad_addr = tst_get_bad_addr(cleanup);
> + bad_addr = tst_get_bad_addr(NULL);
This would not count as a not-mapped address becasue tst_get_bad_addr()
creates a mapping with
bad_addr = mmap(0, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
which maps atleast one page.
But mremap() is still failing because we are passing a different address
range with old_size value as
memsize = (1000 * page_sz);
If we simply use "memsize = page_sz" here mremap() doesn't fail.
So I think, to use and address which is not mapped in the process's address
space, we can do a SAFE_MMAP() immediately followed by SAFE_MUNMAP() and
use that address for mremap() call.
> }
>
> -/*
> - * cleanup() - performs all ONE TIME cleanup for this test at
> - * completion or premature exit.
> - */
> -void cleanup(void)
> +static void cleanup(void)
> {
> -
> - /* Exit the program */
> -
> + if (addr != MAP_FAILED)
> + SAFE_MUNMAP(addr, newsize);
> }
> +
> +static struct tst_test test = {
> + .test_all = verify_mremap,
> + .setup = setup,
> + .cleanup = cleanup,
> +};
Regards,
Avinesh
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [LTP] [PATCH v2 3/3] mremap04: Convert to new API
2024-02-27 8:42 ` [LTP] [PATCH v2 3/3] mremap04: " Yang Xu via ltp
@ 2024-03-04 20:49 ` Avinesh Kumar
0 siblings, 0 replies; 7+ messages in thread
From: Avinesh Kumar @ 2024-03-04 20:49 UTC (permalink / raw)
To: Yang Xu; +Cc: ltp
Hi,
On Tuesday, February 27, 2024 9:42:44 AM CET Yang Xu via ltp wrote:
> Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
> ---
> testcases/kernel/syscalls/mremap/Makefile | 4 +-
> testcases/kernel/syscalls/mremap/mremap04.c | 269 ++++----------------
> 2 files changed, 53 insertions(+), 220 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/mremap/Makefile
> b/testcases/kernel/syscalls/mremap/Makefile index 190b7659d..f286dd9bf
> 100644
> --- a/testcases/kernel/syscalls/mremap/Makefile
> +++ b/testcases/kernel/syscalls/mremap/Makefile
> @@ -3,10 +3,10 @@
>
> top_srcdir ?= ../../../..
>
> -LTPLIBS = ltpipc
> +LTPLIBS = ltpnewipc
>
> include $(top_srcdir)/include/mk/testcases.mk
>
> -mremap04: LTPLDLIBS = -lltpipc
> +mremap04: LTPLDLIBS = -lltpnewipc
>
> include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/kernel/syscalls/mremap/mremap04.c
> b/testcases/kernel/syscalls/mremap/mremap04.c index 53902df73..8d819184e
> 100644
> --- a/testcases/kernel/syscalls/mremap/mremap04.c
> +++ b/testcases/kernel/syscalls/mremap/mremap04.c
> @@ -1,245 +1,78 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> /*
> - *
> - * Copyright (c) International Business Machines Corp., 2001
> - *
> - * This program is free software; you can redistribute it and/or modify
> - * it under the terms of the GNU General Public License as published by
> - * the Free Software Foundation; either version 2 of the License, or
> - * (at your option) any later version.
> - *
> - * This program 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 General Public License for more details.
> - *
> - * You should have received a copy of the GNU General Public License
> - * along with this program; if not, write to the Free Software
> - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
> 02110-1301 USA + * Copyright (c) International Business Machines Corp.,
> 2001
> + * Copyright (c) Linux Test Project, 2001-2024
> + * 07/2001 Ported by Wayne Boyer
> */
>
> -/*
> - * Test Name: mremap04
> - *
> - * Test Description:
> - * Verify that,
> - * mremap() fails when used to expand the existing virtual memory mapped
> - * region to the requested size, if the memory area cannot be expanded at
> - * the current virtual address and MREMAP_MAYMOVE flag not set. - *
> - * Expected Result:
> - * mremap() should return -1 and set errno to ENOMEM.
> - *
> - * Algorithm:
> - * Setup:
> - * Setup signal handling.
> - * Create temporary directory.
> - * Pause for SIGUSR1 if option specified.
> - *
> - * Test:
> - * Loop if the proper options are given.
> - * Execute system call
> - * Check return code, if system call failed (return=-1)
> - * if errno set == expected errno
> - * Issue sys call failed with expected return value and errno.
> - * Otherwise,
> - * Issue sys call failed with unexpected errno.
> - * Otherwise,
> - * Issue sys call returns unexpected value.
> - *
> - * Cleanup:
> - * Print errno log and/or timing stats if options given
> - * Delete the temporary directory(s)/file(s) created.
> - *
> - * Usage: <for command-line>
> - * mremap04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
> - * where, -c n : Run n copies concurrently.
> - * -e : Turn on errno logging.
> - * -i n : Execute test n times.
> - * -I x : Execute test for x seconds.
> - * -p x : Pause for x seconds between iterations.
> - * -t : Turn on syscall timing.
> - *
> - * HISTORY
> - * 07/2001 Ported by Wayne Boyer
> +/*\
> + * [Description]
> *
> - * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
> - * Modified.
> - * - #include <linux/mman.h> should not be included as per man page
> for - * mremap, #include <sys/mman.h> alone should do the job. But
> inorder - * to include definition of MREMAP_MAYMOVE defined in
> bits/mman.h - * (included by sys/mman.h) __USE_GNU needs to be
> defined. - * There may be a more elegant way of doing this...
> + * Test for ENOMEM error.
> *
> - * 26/02/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
> - * - Fix concurrency issue. Use a shm key from getipckey instead of
> - * a fixed hard-coded value.
> - *
> - * RESTRICTIONS:
> - * None.
> + * - mremap fail when the memory area cannot be expanded at the current
> virtual address + * and MREMAP_MAYMOVE flag not set
> */
> +
> #define _GNU_SOURCE
> -#include <errno.h>
> -#include <unistd.h>
> -#include <sys/mman.h>
> #include <sys/ipc.h>
> #include <sys/shm.h>
> +#include "tst_test.h"
> +#include "libnewipc.h"
> +#include "tst_safe_sysv_ipc.h"
>
> -#include "test.h"
> -
> -#define SHM_MODE (SHM_R | SHM_W) /* mode permissions of shared memory */
> +#define SHM_MODE (SHM_R | SHM_W)
>
> -char *TCID = "mremap04";
> -int TST_TOTAL = 1;
> -char *addr; /* addr of memory mapped region */
> -char *shmaddr; /* pointer to shared memory segment */
> -int shmid; /* shared memory identifier. */
> -int memsize; /* memory mapped size */
> -int newsize; /* new size of virtual memory block */
> +static char *addr;
> +static char *shmaddr;
> +static int shm_id;
> +static int memsize;
> +static int newsize;
>
> -void setup(); /* Main setup function of test */
> -void cleanup(); /* cleanup function for the test */
> -
> -extern int getipckey();
> -
> -int main(int ac, char **av)
> +static void verify_mremap(void)
> {
> - int lc;
> -
> - tst_parse_opts(ac, av, NULL, NULL);
> -
> - setup();
> -
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> -
> - tst_count = 0;
> -
> - /*
> - * Attempt to expand the existing shared
> - * memory region of newsize by newsize limits
> - * using mremap() should fail as specified
> - * memory area already locked and MREMAP_MAYMOVE
> - * flag unset.
> - */
> - errno = 0;
> - addr = mremap(shmaddr, memsize, newsize, 0);
> - TEST_ERRNO = errno;
> -
> - /* Check for the return value of mremap() */
> - if (addr != MAP_FAILED) {
> - tst_resm(TFAIL,
> - "mremap returned invalid value, expected: -1");
> -
> - /* Unmap the mapped memory region */
> - if (munmap(addr, newsize) != 0) {
> - tst_brkm(TFAIL, cleanup, "munmap failed to "
> - "unmap the expanded memory region, "
> - "error=%d", errno);
> - }
> - continue;
> - }
> + errno = 0;
> + addr = mremap(shmaddr, memsize, newsize, 0);
If mremap() call somehow passes, further test iterations would be broken as
there will be no mapping at shmaddr. So I think we need to move the code for
setting up shared memory mapping from setup() to main test function.
> + TST_ERR = errno;
We can use TESTPRT() macro.
>
> - if (TEST_ERRNO == ENOMEM) {
> - tst_resm(TPASS, "mremap() failed, "
> - "'MREMAP_MAYMOVE flag unset', "
> - "errno %d", TEST_ERRNO);
> - } else {
> - tst_resm(TFAIL, "mremap() failed, "
> - "Unexpected errno %d", TEST_ERRNO);
> - }
> + if (addr != MAP_FAILED) {
> + tst_res(TFAIL | TTERRNO,
> + "mremap returned invalid value, expected: -1");
we should return here if mremap() was successful.
> }
>
> - cleanup();
> - tst_exit();
> -
> + if (TST_ERR == ENOMEM) {
> + tst_res(TPASS | TTERRNO, "mremap() failed, "
> + "'MREMAP_MAYMOVE flag unset', "
> + "errno %d", TST_ERR);
tst_res() with TTERRNO will take care of printing the errno.
> + } else {
> + tst_res(TFAIL | TTERRNO, "mremap() failed, "
> + "Unexpected errno %d", TST_ERR);
> + }
> }
>
> -/*
> - * setup() - performs all ONE TIME setup for this test.
> - *
> - * Get system page size, Set the size of virtual memory area and the
> - * newsize after resize,
> - * Create a named shared memory segment SHMKEY of newsize and mode SHM_MODE
> - * by using shmget() which returns a shared memory identifier associated -
> * with the created shared memory segment.
> - * Call shmat() to attach the shared memory segment to the data segment of
> the - * calling process. The segment is attached at the first available
> address as - * selected by the system.
> - */
> -void setup(void)
> +static void setup(void)
> {
> key_t shmkey;
>
> - tst_sig(FORK, DEF_HANDLER, cleanup);
> -
> - TEST_PAUSE;
> -
> - tst_tmpdir();
> -
> - /* Get the system page size */
> - if ((memsize = getpagesize()) < 0) {
> - tst_brkm(TBROK, NULL,
> - "getpagesize() failed to get system page size");
> - }
> -
> - /* Get the New size of virtual memory block after resize */
> + memsize = SAFE_SYSCONF(_SC_PAGESIZE);
> newsize = (memsize * 2);
> -
> - /* get an IPC resource key */
> - shmkey = getipckey();
> -
> - /*
> - * Create a shared memory segment represented by SHMKEY of
> - * specified size 'newsize' and mode permissions 'SHM_MODE'.
> - */
> - shmid = shmget(shmkey, newsize, IPC_CREAT | SHM_MODE);
> - if (shmid == -1) {
> - tst_brkm(TBROK, NULL, "shmget() Failed to create a shared "
> - "memory, error:%d", errno);
> - }
> -
> - /*
> - * Attach the shared memory segment associated with the shared
> - * memory identifier specified by "shmid" to the data segment of
> - * the calling process at the first available address as selected
> - * by the system.
> - */
> - shmaddr = shmat(shmid, NULL, 0);
> - if (shmaddr == (void *)-1) {
> - tst_brkm(TBROK, cleanup, "shmat() Failed to attach shared "
> - "memory, error:%d", errno);
> - }
> + shmkey = GETIPCKEY();
> + shm_id = SAFE_SHMGET(shmkey, newsize, IPC_CREAT | SHM_MODE);
> + shmaddr = SAFE_SHMAT(shm_id, NULL, 0);
> }
>
> -/*
> - * cleanup() - performs all ONE TIME cleanup for this test at
> - * completion or premature exit.
> - * Detach the shared memory segment and remove the shared memory
> - * identifier associated with the shared memory.
> - */
> -void cleanup(void)
> +static void cleanup(void)
> {
> + SAFE_SHMDT(shmaddr);
> + SAFE_SHMCTL(shm_id, IPC_RMID, 0);
>
> - /*
> - * Detach the shared memory segment attached to
> - * the calling process's data segment
> - */
> - if (shmdt(shmaddr) < 0) {
> - tst_brkm(TFAIL, NULL, "shmdt() Failed to detach shared "
> - "memory, error:%d", errno);
> - }
> -
> - /*
> - * Remove the shared memory identifier associated with
> - * the shared memory segment and destroy the shared memory
> - * segment.
> - */
> - if (shmctl(shmid, IPC_RMID, 0) < 0) {
> - tst_brkm(TFAIL, NULL, "shmctl() Failed to remove shared "
> - "memory, error:%d", errno);
> - }
> -
> - tst_rmdir();
> -
> - /* Exit the program */
> -
> + if (addr != MAP_FAILED)
> + SAFE_MUNMAP(addr, newsize);
we need to do the resetting in main test function if mremap() was successful
so that test can run correctly when called in multiple loops.
> }
> +
> +static struct tst_test test = {
> + .setup = setup,
> + .cleanup = cleanup,
> + .needs_tmpdir = 1,
> + .test_all = verify_mremap,
> +};
Regards,
Avinesh
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2024-03-04 20:49 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-27 8:42 [LTP] [PATCH v2 1/3] mremap02: Convert to new API Yang Xu via ltp
2024-02-27 8:42 ` [LTP] [PATCH v2 2/3] mremap03: " Yang Xu via ltp
2024-03-04 19:49 ` Avinesh Kumar
2024-02-27 8:42 ` [LTP] [PATCH v2 3/3] mremap04: " Yang Xu via ltp
2024-03-04 20:49 ` Avinesh Kumar
2024-03-01 12:31 ` [LTP] [PATCH v2 1/3] mremap02: " Avinesh Kumar
2024-03-01 13:06 ` Martin Doucha
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox