* [LTP] [PATCH 2/5] syscalls/mmap09: Rewrite the test using new LTP API
2023-08-25 6:38 [LTP] [PATCH 1/5] syscalls/mmap08: Rewrite the test using new LTP API Avinesh Kumar
@ 2023-08-25 6:38 ` Avinesh Kumar
2023-09-01 8:27 ` Richard Palethorpe
2023-08-25 6:38 ` [LTP] [PATCH 3/5] syscalls/mmap13: Rewrite the test using new API Avinesh Kumar
` (3 subsequent siblings)
4 siblings, 1 reply; 20+ messages in thread
From: Avinesh Kumar @ 2023-08-25 6:38 UTC (permalink / raw)
To: ltp
Signed-off-by: Avinesh Kumar <akumar@suse.de>
---
testcases/kernel/syscalls/mmap/mmap09.c | 134 ++++++++----------------
1 file changed, 45 insertions(+), 89 deletions(-)
diff --git a/testcases/kernel/syscalls/mmap/mmap09.c b/testcases/kernel/syscalls/mmap/mmap09.c
index 4ab0da470..6f59baf4a 100644
--- a/testcases/kernel/syscalls/mmap/mmap09.c
+++ b/testcases/kernel/syscalls/mmap/mmap09.c
@@ -1,119 +1,75 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
- * Copyright (c) International Business Machines Corp., 2003
- *
- * 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
+ * 04/2003 Written by Paul Larson
+ * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
*/
-/*
- * Test Description:
- * Verify that truncating a mmaped file works correctly.
- *
- * Expected Result:
- * ftruncate should be allowed to increase, decrease, or zero the
- * size of a file that has been mmaped
+/*\
+ * [Description]
*
- * Test:
- * Use ftruncate to shrink the file while it is mapped
- * Use ftruncate to grow the file while it is mapped
- * Use ftruncate to zero the size of the file while it is mapped
- *
- * HISTORY
- * 04/2003 Written by Paul Larson
+ * Verify that, once we have a file mapping created using mmap(), we can
+ * successfully shrink, grow or zero the size of the mapped file using
+ * ftruncate.
*/
-#include <stdio.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <sys/mman.h>
-#include <sys/types.h>
-#include "test.h"
-#define mapsize (1 << 14)
-char *TCID = "mmap09";
-int TST_TOTAL = 3;
+#include <stdlib.h>
+#include "tst_test.h"
+#define mapsize (1 << 14)
+#define TEMPFILE "mmapfile"
static int fd;
-static char *maddr;
+static char *addr;
-static struct test_case_t {
+static struct tcase {
off_t newsize;
char *desc;
-} TC[] = {
- {mapsize - 8192, "ftruncate mmaped file to a smaller size"},
- {mapsize + 1024, "ftruncate mmaped file to a larger size"},
- {0, "ftruncate mmaped file to 0 size"},
+} tcases[] = {
+ {mapsize - 8192, "ftruncate mapped file to a smaller size"},
+ {mapsize + 1024, "ftruncate mapped file to a bigger size"},
+ {0, "ftruncate mapped file to zero size"}
};
-static void setup(void);
-static void cleanup(void);
-
-int main(int argc, char **argv)
+static void setup(void)
{
- int lc;
- int i;
-
- tst_parse_opts(argc, argv, NULL, NULL);
-
- setup();
+ fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- tst_count = 0;
- for (i = 0; i < TST_TOTAL; i++) {
- TEST(ftruncate(fd, TC[i].newsize));
+ SAFE_FTRUNCATE(fd, mapsize);
- if (TEST_RETURN == -1) {
- tst_resm(TFAIL | TTERRNO, "%s", TC[i].desc);
- } else {
- tst_resm(TPASS, "%s", TC[i].desc);
- }
- }
+ addr = mmap(0, mapsize, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
+ if (addr == MAP_FAILED)
+ tst_brk(TFAIL | TERRNO, "mmap() failed");
- }
+ memset(addr, 'A', mapsize);
- cleanup();
- tst_exit();
}
-static void setup(void)
+static void run(unsigned int i)
{
- tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
- TEST_PAUSE;
+ struct stat stat_buf;
+ struct tcase *tc = &tcases[i];
- tst_tmpdir();
+ TST_EXP_PASS(ftruncate(fd, tc->newsize), "%s", tc->desc);
- if ((fd = open("mmaptest", O_RDWR | O_CREAT, 0666)) < 0)
- tst_brkm(TFAIL | TERRNO, cleanup, "opening mmaptest failed");
+ SAFE_FSTAT(fd, &stat_buf);
+ TST_EXP_EQ_LI(stat_buf.st_size, tc->newsize);
- /* ftruncate the file to 16k */
- if (ftruncate(fd, mapsize) < 0)
- tst_brkm(TFAIL | TERRNO, cleanup, "ftruncate file failed");
-
- maddr = mmap(0, mapsize, PROT_READ | PROT_WRITE,
- MAP_FILE | MAP_SHARED, fd, 0);
- if (maddr == MAP_FAILED)
- tst_brkm(TFAIL | TERRNO, cleanup, "mmapping mmaptest failed");
-
- /* fill up the file with A's */
- memset(maddr, 'A', mapsize);
+ SAFE_FTRUNCATE(fd, mapsize);
}
static void cleanup(void)
{
- munmap(maddr, mapsize);
- close(fd);
- tst_rmdir();
+ if (fd > 0)
+ SAFE_CLOSE(fd);
+ if (addr)
+ SAFE_MUNMAP(addr, mapsize);
}
+
+static struct tst_test test = {
+ .setup = setup,
+ .cleanup = cleanup,
+ .test = run,
+ .tcnt = ARRAY_SIZE(tcases),
+ .needs_tmpdir = 1
+};
--
2.41.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [LTP] [PATCH 2/5] syscalls/mmap09: Rewrite the test using new LTP API
2023-08-25 6:38 ` [LTP] [PATCH 2/5] syscalls/mmap09: " Avinesh Kumar
@ 2023-09-01 8:27 ` Richard Palethorpe
2023-09-01 9:04 ` Cyril Hrubis
0 siblings, 1 reply; 20+ messages in thread
From: Richard Palethorpe @ 2023-09-01 8:27 UTC (permalink / raw)
To: Avinesh Kumar; +Cc: ltp
Hello,
Avinesh Kumar <akumar@suse.de> writes:
> Signed-off-by: Avinesh Kumar <akumar@suse.de>
> ---
> testcases/kernel/syscalls/mmap/mmap09.c | 134 ++++++++----------------
> 1 file changed, 45 insertions(+), 89 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/mmap/mmap09.c b/testcases/kernel/syscalls/mmap/mmap09.c
> index 4ab0da470..6f59baf4a 100644
> --- a/testcases/kernel/syscalls/mmap/mmap09.c
> +++ b/testcases/kernel/syscalls/mmap/mmap09.c
> @@ -1,119 +1,75 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> /*
> - * Copyright (c) International Business Machines Corp., 2003
> - *
> - * 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
> + * 04/2003 Written by Paul Larson
> + * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
> */
>
> -/*
> - * Test Description:
> - * Verify that truncating a mmaped file works correctly.
> - *
> - * Expected Result:
> - * ftruncate should be allowed to increase, decrease, or zero the
> - * size of a file that has been mmaped
> +/*\
> + * [Description]
> *
> - * Test:
> - * Use ftruncate to shrink the file while it is mapped
> - * Use ftruncate to grow the file while it is mapped
> - * Use ftruncate to zero the size of the file while it is mapped
> - *
> - * HISTORY
> - * 04/2003 Written by Paul Larson
> + * Verify that, once we have a file mapping created using mmap(), we can
> + * successfully shrink, grow or zero the size of the mapped file using
> + * ftruncate.
> */
> -#include <stdio.h>
> -#include <unistd.h>
> -#include <fcntl.h>
> -#include <errno.h>
> -#include <sys/mman.h>
> -#include <sys/types.h>
> -#include "test.h"
>
> -#define mapsize (1 << 14)
>
> -char *TCID = "mmap09";
> -int TST_TOTAL = 3;
> +#include <stdlib.h>
> +#include "tst_test.h"
>
> +#define mapsize (1 << 14)
> +#define TEMPFILE "mmapfile"
> static int fd;
> -static char *maddr;
> +static char *addr;
>
> -static struct test_case_t {
> +static struct tcase {
> off_t newsize;
> char *desc;
> -} TC[] = {
> - {mapsize - 8192, "ftruncate mmaped file to a smaller size"},
> - {mapsize + 1024, "ftruncate mmaped file to a larger size"},
> - {0, "ftruncate mmaped file to 0 size"},
> +} tcases[] = {
> + {mapsize - 8192, "ftruncate mapped file to a smaller size"},
> + {mapsize + 1024, "ftruncate mapped file to a bigger size"},
> + {0, "ftruncate mapped file to zero size"}
> };
>
> -static void setup(void);
> -static void cleanup(void);
> -
> -int main(int argc, char **argv)
> +static void setup(void)
> {
> - int lc;
> - int i;
> -
> - tst_parse_opts(argc, argv, NULL, NULL);
> -
> - setup();
> + fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
>
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> - tst_count = 0;
> - for (i = 0; i < TST_TOTAL; i++) {
> - TEST(ftruncate(fd, TC[i].newsize));
> + SAFE_FTRUNCATE(fd, mapsize);
>
> - if (TEST_RETURN == -1) {
> - tst_resm(TFAIL | TTERRNO, "%s", TC[i].desc);
> - } else {
> - tst_resm(TPASS, "%s", TC[i].desc);
> - }
> - }
> + addr = mmap(0, mapsize, PROT_READ | PROT_WRITE, MAP_FILE |
> MAP_SHARED, fd, 0);
Why don't we use SAFE_MMAP?
> + if (addr == MAP_FAILED)
> + tst_brk(TFAIL | TERRNO, "mmap() failed");
>
> - }
> + memset(addr, 'A', mapsize);
>
> - cleanup();
> - tst_exit();
> }
>
> -static void setup(void)
> +static void run(unsigned int i)
> {
> - tst_sig(NOFORK, DEF_HANDLER, cleanup);
> -
> - TEST_PAUSE;
> + struct stat stat_buf;
> + struct tcase *tc = &tcases[i];
>
> - tst_tmpdir();
> + TST_EXP_PASS(ftruncate(fd, tc->newsize), "%s", tc->desc);
>
> - if ((fd = open("mmaptest", O_RDWR | O_CREAT, 0666)) < 0)
> - tst_brkm(TFAIL | TERRNO, cleanup, "opening mmaptest failed");
> + SAFE_FSTAT(fd, &stat_buf);
> + TST_EXP_EQ_LI(stat_buf.st_size, tc->newsize);
>
> - /* ftruncate the file to 16k */
> - if (ftruncate(fd, mapsize) < 0)
> - tst_brkm(TFAIL | TERRNO, cleanup, "ftruncate file failed");
> -
> - maddr = mmap(0, mapsize, PROT_READ | PROT_WRITE,
> - MAP_FILE | MAP_SHARED, fd, 0);
> - if (maddr == MAP_FAILED)
> - tst_brkm(TFAIL | TERRNO, cleanup, "mmapping mmaptest failed");
> -
> - /* fill up the file with A's */
> - memset(maddr, 'A', mapsize);
> + SAFE_FTRUNCATE(fd, mapsize);
> }
>
> static void cleanup(void)
> {
> - munmap(maddr, mapsize);
> - close(fd);
> - tst_rmdir();
> + if (fd > 0)
> + SAFE_CLOSE(fd);
> + if (addr)
> + SAFE_MUNMAP(addr, mapsize);
> }
> +
> +static struct tst_test test = {
> + .setup = setup,
> + .cleanup = cleanup,
> + .test = run,
> + .tcnt = ARRAY_SIZE(tcases),
> + .needs_tmpdir = 1
Can we use all file systems?
The test is mapping a file and performing an operation on it. So this is
basically a file system test.
BTW this test seems weak. I don't know what truncating the file without
then trying to access the newly OOB memory achieves. However it's what
the original test did, so it's up to you if you want to change anything.
Setting to changes requested
--
Thank you,
Richard.
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 20+ messages in thread* Re: [LTP] [PATCH 2/5] syscalls/mmap09: Rewrite the test using new LTP API
2023-09-01 8:27 ` Richard Palethorpe
@ 2023-09-01 9:04 ` Cyril Hrubis
2023-09-01 9:11 ` Richard Palethorpe
0 siblings, 1 reply; 20+ messages in thread
From: Cyril Hrubis @ 2023-09-01 9:04 UTC (permalink / raw)
To: Richard Palethorpe; +Cc: ltp
Hi!
> > + addr = mmap(0, mapsize, PROT_READ | PROT_WRITE, MAP_FILE |
> > MAP_SHARED, fd, 0);
>
> Why don't we use SAFE_MMAP?
I guess mainly because that would produce TBROK instead of TFAIL.
> Can we use all file systems?
>
> The test is mapping a file and performing an operation on it. So this is
> basically a file system test.
>
> BTW this test seems weak. I don't know what truncating the file without
> then trying to access the newly OOB memory achieves. However it's what
> the original test did, so it's up to you if you want to change anything.
I would vote for adding additional checks like this. I suppose that you
will get SIGBUS when accessing pages beyond new file size, so we should
probably fork a child, let it touch the truncated part of the file, and
check that it was killed by SIGBUS.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [LTP] [PATCH 2/5] syscalls/mmap09: Rewrite the test using new LTP API
2023-09-01 9:04 ` Cyril Hrubis
@ 2023-09-01 9:11 ` Richard Palethorpe
0 siblings, 0 replies; 20+ messages in thread
From: Richard Palethorpe @ 2023-09-01 9:11 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: ltp
Hello,
Cyril Hrubis <chrubis@suse.cz> writes:
> Hi!
>> > + addr = mmap(0, mapsize, PROT_READ | PROT_WRITE, MAP_FILE |
>> > MAP_SHARED, fd, 0);
>>
>> Why don't we use SAFE_MMAP?
>
> I guess mainly because that would produce TBROK instead of TFAIL.
>
>> Can we use all file systems?
>>
>> The test is mapping a file and performing an operation on it. So this is
>> basically a file system test.
>>
>> BTW this test seems weak. I don't know what truncating the file without
>> then trying to access the newly OOB memory achieves. However it's what
>> the original test did, so it's up to you if you want to change anything.
>
> I would vote for adding additional checks like this. I suppose that you
> will get SIGBUS when accessing pages beyond new file size, so we should
> probably fork a child, let it touch the truncated part of the file, and
> check that it was killed by SIGBUS.
There is some overlap with mmap13 because that does check for
SIGBUS. Possibly these could be combined?
--
Thank you,
Richard.
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 20+ messages in thread
* [LTP] [PATCH 3/5] syscalls/mmap13: Rewrite the test using new API
2023-08-25 6:38 [LTP] [PATCH 1/5] syscalls/mmap08: Rewrite the test using new LTP API Avinesh Kumar
2023-08-25 6:38 ` [LTP] [PATCH 2/5] syscalls/mmap09: " Avinesh Kumar
@ 2023-08-25 6:38 ` Avinesh Kumar
2023-09-04 8:54 ` Richard Palethorpe
2023-08-25 6:38 ` [LTP] [PATCH 4/5] syscalls/mmap14: Rewrite test using new LTP API Avinesh Kumar
` (2 subsequent siblings)
4 siblings, 1 reply; 20+ messages in thread
From: Avinesh Kumar @ 2023-08-25 6:38 UTC (permalink / raw)
To: ltp
Signed-off-by: Avinesh Kumar <akumar@suse.de>
---
testcases/kernel/syscalls/mmap/mmap13.c | 174 +++++++++---------------
1 file changed, 65 insertions(+), 109 deletions(-)
diff --git a/testcases/kernel/syscalls/mmap/mmap13.c b/testcases/kernel/syscalls/mmap/mmap13.c
index c5a2058e9..5e18f24fe 100644
--- a/testcases/kernel/syscalls/mmap/mmap13.c
+++ b/testcases/kernel/syscalls/mmap/mmap13.c
@@ -1,142 +1,98 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2013 FNST, DAN LI <li.dan@cn.fujitsu.com>
+ * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
+ */
+
+/*\
+ * [Description]
*
- * 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.
+ * Verify that, mmap() call succeeds to create a file mapping with length
+ * argument greater than the file size but any attempt to reference the
+ * memory region which does not correspond to the file causes SIGBUS signal.
*
- * 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
- */
-
-/*
- * Test Description:
- * Verify error signal SIGBUS.
- * "Attempted access to a portion of the buffer that does not correspond
- * to the file."
+ * mmap(0, 8192, prot, MAP_FILE | MAP_SHARED, fd, 0);
*
- * Expected Result:
- * mmap() should succeed returning the address of the mapped region,
- * and an attempt to access the memory which does not correspond to the file
- * should rise the signal SIGBUS.
+ * byte offset: 0 2047 2048 4095 4096 8191 8192
+ * +------------+------------+------------------------+
+ * Memory | | remainder | |
+ * region | | of page(0s)| |
+ * +------------+------------+------------------------+
+ * |<---------->|<---------->|<---------------------->|<--------------->
+ * | accessible,| accessible, references references
+ * | mapped | not mapped yield SIGBUS yield SIGSEGV
+ * | to file | to file
+ * | |
+ * +------------+
+ * Mapped file | |
+ * (2048 bytes) | |
+ * +------------+
+ * file offset: 0 2047
*/
-#include <stdio.h>
+
#include <stdlib.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
-#include <signal.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
#include <setjmp.h>
-
-#include "test.h"
+#include "tst_test.h"
#define TEMPFILE "mmapfile"
-
-char *TCID = "mmap13";
-int TST_TOTAL = 1;
-
static size_t page_sz;
static char *addr;
-static int fildes;
+static int fd;
static volatile sig_atomic_t pass;
static sigjmp_buf env;
-static void setup(void);
-static void cleanup(void);
-static void sig_handler(int sig);
-
-int main(int argc, char *argv[])
+static void sig_handler(int sig)
{
- int lc;
- char *ch;
-
- tst_parse_opts(argc, argv, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- tst_count = 0;
-
- addr = mmap(NULL, page_sz * 2, PROT_READ | PROT_WRITE,
- MAP_FILE | MAP_SHARED, fildes, 0);
-
- if (addr == MAP_FAILED) {
- tst_resm(TFAIL | TERRNO, "mmap() failed on %s",
- TEMPFILE);
- continue;
- }
-
- if (sigsetjmp(env, 1) == 0) {
- ch = addr + page_sz + 1;
- *ch = 0;
- }
-
- if (pass)
- tst_resm(TPASS, "Got SIGBUS "
- "as expected");
- else
- tst_resm(TFAIL, "Invalid access not "
- "rise SIGBUS");
-
- if (munmap(addr, page_sz * 2) != 0)
- tst_brkm(TFAIL | TERRNO, cleanup,
- "failed to unmap the mmapped pages");
-
- pass = 0;
- }
-
- cleanup();
- tst_exit();
+ if (sig == SIGBUS) {
+ pass = 1;
+ siglongjmp(env, 1);
+ } else
+ tst_brk(TBROK, "received an unexpected signal");
}
static void setup(void)
{
- tst_sig(NOFORK, sig_handler, cleanup);
-
- TEST_PAUSE;
+ SAFE_SIGNAL(SIGBUS, sig_handler);
page_sz = getpagesize();
- tst_tmpdir();
-
- fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0766);
- if (fildes < 0)
- tst_brkm(TFAIL | TERRNO, cleanup, "opening %s failed",
- TEMPFILE);
-
- if (ftruncate(fildes, page_sz / 2) == -1)
- tst_brkm(TFAIL | TERRNO, cleanup, "ftruncate %s failed",
- TEMPFILE);
+ fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
+ SAFE_FTRUNCATE(fd, page_sz / 2);
}
-/*
- * This function gets executed when the test process receives
- * the signal SIGBUS while trying to access the memory which
- * does not correspond to the file.
- */
-static void sig_handler(int sig)
+static void run(void)
{
- if (sig == SIGBUS) {
- pass = 1;
- siglongjmp(env, 1);
- } else {
- tst_brkm(TBROK, cleanup, "received an unexpected signal");
+ char *ch;
+
+ addr = mmap(0, page_sz * 2, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
+ if (addr == MAP_FAILED) {
+ tst_res(TFAIL | TERRNO, "mmap() of %s failed", TEMPFILE);
+ return;
}
+
+ if (sigsetjmp(env, 1) == 0) {
+ ch = addr + page_sz + 1;
+ *ch = 0;
+ }
+
+ if (pass)
+ tst_res(TPASS, "Received SIGBUS signal as expected");
+ else
+ tst_res(TFAIL, "SIGBUS signal not received");
+
+ SAFE_MUNMAP(addr, page_sz * 2);
}
static void cleanup(void)
{
- close(fildes);
- tst_rmdir();
+ if (fd > 0)
+ SAFE_CLOSE(fd);
}
+
+static struct tst_test test = {
+ .setup = setup,
+ .cleanup = cleanup,
+ .test_all = run,
+ .needs_tmpdir = 1
+};
--
2.41.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [LTP] [PATCH 3/5] syscalls/mmap13: Rewrite the test using new API
2023-08-25 6:38 ` [LTP] [PATCH 3/5] syscalls/mmap13: Rewrite the test using new API Avinesh Kumar
@ 2023-09-04 8:54 ` Richard Palethorpe
0 siblings, 0 replies; 20+ messages in thread
From: Richard Palethorpe @ 2023-09-04 8:54 UTC (permalink / raw)
To: Avinesh Kumar; +Cc: ltp
Hello,
Avinesh Kumar <akumar@suse.de> writes:
> Signed-off-by: Avinesh Kumar <akumar@suse.de>
> ---
> testcases/kernel/syscalls/mmap/mmap13.c | 174 +++++++++---------------
> 1 file changed, 65 insertions(+), 109 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/mmap/mmap13.c b/testcases/kernel/syscalls/mmap/mmap13.c
> index c5a2058e9..5e18f24fe 100644
> --- a/testcases/kernel/syscalls/mmap/mmap13.c
> +++ b/testcases/kernel/syscalls/mmap/mmap13.c
> @@ -1,142 +1,98 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> /*
> * Copyright (c) 2013 FNST, DAN LI <li.dan@cn.fujitsu.com>
> + * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
> + */
> +
> +/*\
> + * [Description]
> *
> - * 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.
> + * Verify that, mmap() call succeeds to create a file mapping with length
> + * argument greater than the file size but any attempt to reference the
> + * memory region which does not correspond to the file causes SIGBUS signal.
> *
> - * 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
> - */
> -
> -/*
> - * Test Description:
> - * Verify error signal SIGBUS.
> - * "Attempted access to a portion of the buffer that does not correspond
> - * to the file."
> + * mmap(0, 8192, prot, MAP_FILE | MAP_SHARED, fd, 0);
> *
> - * Expected Result:
> - * mmap() should succeed returning the address of the mapped region,
> - * and an attempt to access the memory which does not correspond to the file
> - * should rise the signal SIGBUS.
> + * byte offset: 0 2047 2048 4095 4096 8191 8192
> + * +------------+------------+------------------------+
> + * Memory | | remainder | |
> + * region | | of page(0s)| |
> + * +------------+------------+------------------------+
> + * |<---------->|<---------->|<---------------------->|<--------------->
> + * | accessible,| accessible, references references
> + * | mapped | not mapped yield SIGBUS yield SIGSEGV
> + * | to file | to file
> + * | |
> + * +------------+
> + * Mapped file | |
> + * (2048 bytes) | |
> + * +------------+
> + * file offset: 0 2047
Nice diagram! The byte offsets are only valid for 4Kb pages though.
> */
> -#include <stdio.h>
> +
> #include <stdlib.h>
> -#include <sys/types.h>
> -#include <errno.h>
> -#include <unistd.h>
> -#include <fcntl.h>
> -#include <string.h>
> -#include <signal.h>
> -#include <sys/stat.h>
> -#include <sys/mman.h>
> #include <setjmp.h>
> -
> -#include "test.h"
> +#include "tst_test.h"
>
> #define TEMPFILE "mmapfile"
> -
> -char *TCID = "mmap13";
> -int TST_TOTAL = 1;
> -
> static size_t page_sz;
> static char *addr;
> -static int fildes;
> +static int fd;
> static volatile sig_atomic_t pass;
> static sigjmp_buf env;
>
> -static void setup(void);
> -static void cleanup(void);
> -static void sig_handler(int sig);
> -
> -int main(int argc, char *argv[])
> +static void sig_handler(int sig)
> {
> - int lc;
> - char *ch;
> -
> - tst_parse_opts(argc, argv, NULL, NULL);
> -
> - setup();
> -
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> - tst_count = 0;
> -
> - addr = mmap(NULL, page_sz * 2, PROT_READ | PROT_WRITE,
> - MAP_FILE | MAP_SHARED, fildes, 0);
> -
> - if (addr == MAP_FAILED) {
> - tst_resm(TFAIL | TERRNO, "mmap() failed on %s",
> - TEMPFILE);
> - continue;
> - }
> -
> - if (sigsetjmp(env, 1) == 0) {
> - ch = addr + page_sz + 1;
> - *ch = 0;
> - }
> -
> - if (pass)
> - tst_resm(TPASS, "Got SIGBUS "
> - "as expected");
> - else
> - tst_resm(TFAIL, "Invalid access not "
> - "rise SIGBUS");
> -
> - if (munmap(addr, page_sz * 2) != 0)
> - tst_brkm(TFAIL | TERRNO, cleanup,
> - "failed to unmap the mmapped pages");
> -
> - pass = 0;
> - }
> -
> - cleanup();
> - tst_exit();
> + if (sig == SIGBUS) {
> + pass = 1;
> + siglongjmp(env, 1);
> + } else
> + tst_brk(TBROK, "received an unexpected signal");
To be safe we should exit the signal handler ASAP. It's better to save
the signal details and check them after returning.
The rest looks good, but setting to changes requested.
--
Thank you,
Richard.
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 20+ messages in thread
* [LTP] [PATCH 4/5] syscalls/mmap14: Rewrite test using new LTP API
2023-08-25 6:38 [LTP] [PATCH 1/5] syscalls/mmap08: Rewrite the test using new LTP API Avinesh Kumar
2023-08-25 6:38 ` [LTP] [PATCH 2/5] syscalls/mmap09: " Avinesh Kumar
2023-08-25 6:38 ` [LTP] [PATCH 3/5] syscalls/mmap13: Rewrite the test using new API Avinesh Kumar
@ 2023-08-25 6:38 ` Avinesh Kumar
2023-09-01 9:23 ` Richard Palethorpe
2023-09-05 13:28 ` [LTP] [PATCH v2] " Avinesh Kumar
2023-08-25 6:38 ` [LTP] [PATCH 5/5] syscalls/mmap15: " Avinesh Kumar
2023-08-30 12:51 ` [LTP] [PATCH 1/5] syscalls/mmap08: Rewrite the " Cyril Hrubis
4 siblings, 2 replies; 20+ messages in thread
From: Avinesh Kumar @ 2023-08-25 6:38 UTC (permalink / raw)
To: ltp
Signed-off-by: Avinesh Kumar <akumar@suse.de>
---
testcases/kernel/syscalls/mmap/mmap14.c | 135 +++++++-----------------
1 file changed, 40 insertions(+), 95 deletions(-)
diff --git a/testcases/kernel/syscalls/mmap/mmap14.c b/testcases/kernel/syscalls/mmap/mmap14.c
index 31632601b..2bc63ae58 100644
--- a/testcases/kernel/syscalls/mmap/mmap14.c
+++ b/testcases/kernel/syscalls/mmap/mmap14.c
@@ -1,124 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2013 FNST, DAN LI <li.dan@cn.fujitsu.com>
- *
- * 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) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
*/
-/*
- * Test Description:
- * Verify MAP_LOCKED works fine.
- * "Lock the pages of the mapped region into memory in the manner of mlock(2)."
+/*\
+ * [Description]
*
- * Expected Result:
- * mmap() should succeed returning the address of the mapped region,
- * and this region should be locked into memory.
+ * Verify that, mmap() call with MAP_LOCKED flag successfully locks
+ * the mapped pages into memory.
*/
-#include <stdio.h>
-#include <sys/mman.h>
-#include "test.h"
+#include <stdio.h>
+#include "tst_test.h"
-#define TEMPFILE "mmapfile"
-#define MMAPSIZE (1UL<<20)
+#define MMAPSIZE (1UL<<16)
#define LINELEN 256
-char *TCID = "mmap14";
-int TST_TOTAL = 1;
-
static char *addr;
-
static void getvmlck(unsigned int *lock_sz);
-static void setup(void);
-static void cleanup(void);
-
-int main(int argc, char *argv[])
-{
- int lc;
- unsigned int sz_before;
- unsigned int sz_after;
- unsigned int sz_ch;
-
- tst_parse_opts(argc, argv, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
-
- tst_count = 0;
-
- getvmlck(&sz_before);
-
- addr = mmap(NULL, MMAPSIZE, PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_LOCKED | MAP_ANONYMOUS,
- -1, 0);
-
- if (addr == MAP_FAILED) {
- tst_resm(TFAIL | TERRNO, "mmap of %s failed", TEMPFILE);
- continue;
- }
-
- getvmlck(&sz_after);
-
- sz_ch = sz_after - sz_before;
- if (sz_ch == MMAPSIZE / 1024) {
- tst_resm(TPASS, "Functionality of mmap() "
- "successful");
- } else {
- tst_resm(TFAIL, "Expected %luK locked, "
- "get %uK locked",
- MMAPSIZE / 1024, sz_ch);
- }
-
- if (munmap(addr, MMAPSIZE) != 0)
- tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
- }
-
- cleanup();
- tst_exit();
-}
void getvmlck(unsigned int *lock_sz)
{
- int ret;
char line[LINELEN];
- FILE *fstatus = NULL;
+ FILE *fp = NULL;
- fstatus = fopen("/proc/self/status", "r");
- if (fstatus == NULL)
- tst_brkm(TFAIL | TERRNO, NULL, "Open dev status failed");
+ fp = fopen("/proc/self/status", "r");
+ if (fp == NULL)
+ tst_brk(TFAIL | TERRNO, "could not open status file");
- while (fgets(line, LINELEN, fstatus) != NULL)
+ while (fgets(line, LINELEN, fp) != NULL) {
if (strstr(line, "VmLck") != NULL)
break;
+ }
- ret = sscanf(line, "%*[^0-9]%d%*[^0-9]", lock_sz);
- if (ret != 1)
- tst_brkm(TFAIL | TERRNO, NULL, "Get lock size failed");
+ if (sscanf(line, "%*[^0-9]%d%*[^0-9]", lock_sz) != 1)
+ tst_brk(TFAIL | TERRNO, "Get lock size failed");
- fclose(fstatus);
+ fclose(fp);
}
-static void setup(void)
+static void run(void)
{
- tst_require_root();
+ unsigned int sz_before, sz_after, sz_diff;
- tst_sig(FORK, DEF_HANDLER, cleanup);
+ getvmlck(&sz_before);
- TEST_PAUSE;
-}
+ addr = mmap(0, MMAPSIZE, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_LOCKED | MAP_ANONYMOUS, -1, 0);
-static void cleanup(void)
-{
+ if (addr != MAP_FAILED)
+ tst_res(TPASS, "mmap() with MAP_LOCKED flag passed");
+ else {
+ tst_res(TFAIL | TERRNO, "mmap() failed");
+ return;
+ }
+
+ getvmlck(&sz_after);
+ sz_diff = sz_after - sz_before;
+ TST_EXP_EQ_LU(MMAPSIZE / 1024, sz_diff);
+
+ SAFE_MUNMAP(addr, MMAPSIZE);
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .needs_tmpdir = 1
+};
--
2.41.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [LTP] [PATCH 4/5] syscalls/mmap14: Rewrite test using new LTP API
2023-08-25 6:38 ` [LTP] [PATCH 4/5] syscalls/mmap14: Rewrite test using new LTP API Avinesh Kumar
@ 2023-09-01 9:23 ` Richard Palethorpe
2023-09-05 13:28 ` [LTP] [PATCH v2] " Avinesh Kumar
1 sibling, 0 replies; 20+ messages in thread
From: Richard Palethorpe @ 2023-09-01 9:23 UTC (permalink / raw)
To: Avinesh Kumar; +Cc: ltp
Hello,
Avinesh Kumar <akumar@suse.de> writes:
> Signed-off-by: Avinesh Kumar <akumar@suse.de>
> ---
> testcases/kernel/syscalls/mmap/mmap14.c | 135 +++++++-----------------
> 1 file changed, 40 insertions(+), 95 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/mmap/mmap14.c b/testcases/kernel/syscalls/mmap/mmap14.c
> index 31632601b..2bc63ae58 100644
> --- a/testcases/kernel/syscalls/mmap/mmap14.c
> +++ b/testcases/kernel/syscalls/mmap/mmap14.c
> @@ -1,124 +1,69 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> /*
> * Copyright (c) 2013 FNST, DAN LI <li.dan@cn.fujitsu.com>
> - *
> - * 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) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
> */
>
> -/*
> - * Test Description:
> - * Verify MAP_LOCKED works fine.
> - * "Lock the pages of the mapped region into memory in the manner of mlock(2)."
> +/*\
> + * [Description]
> *
> - * Expected Result:
> - * mmap() should succeed returning the address of the mapped region,
> - * and this region should be locked into memory.
> + * Verify that, mmap() call with MAP_LOCKED flag successfully locks
> + * the mapped pages into memory.
> */
> -#include <stdio.h>
> -#include <sys/mman.h>
>
> -#include "test.h"
> +#include <stdio.h>
> +#include "tst_test.h"
>
> -#define TEMPFILE "mmapfile"
> -#define MMAPSIZE (1UL<<20)
> +#define MMAPSIZE (1UL<<16)
> #define LINELEN 256
>
> -char *TCID = "mmap14";
> -int TST_TOTAL = 1;
> -
> static char *addr;
> -
> static void getvmlck(unsigned int *lock_sz);
> -static void setup(void);
> -static void cleanup(void);
> -
> -int main(int argc, char *argv[])
> -{
> - int lc;
> - unsigned int sz_before;
> - unsigned int sz_after;
> - unsigned int sz_ch;
> -
> - tst_parse_opts(argc, argv, NULL, NULL);
> -
> - setup();
> -
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> -
> - tst_count = 0;
> -
> - getvmlck(&sz_before);
> -
> - addr = mmap(NULL, MMAPSIZE, PROT_READ | PROT_WRITE,
> - MAP_PRIVATE | MAP_LOCKED | MAP_ANONYMOUS,
> - -1, 0);
> -
> - if (addr == MAP_FAILED) {
> - tst_resm(TFAIL | TERRNO, "mmap of %s failed", TEMPFILE);
> - continue;
> - }
> -
> - getvmlck(&sz_after);
> -
> - sz_ch = sz_after - sz_before;
> - if (sz_ch == MMAPSIZE / 1024) {
> - tst_resm(TPASS, "Functionality of mmap() "
> - "successful");
> - } else {
> - tst_resm(TFAIL, "Expected %luK locked, "
> - "get %uK locked",
> - MMAPSIZE / 1024, sz_ch);
> - }
> -
> - if (munmap(addr, MMAPSIZE) != 0)
> - tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
> - }
> -
> - cleanup();
> - tst_exit();
> -}
>
> void getvmlck(unsigned int *lock_sz)
> {
> - int ret;
> char line[LINELEN];
> - FILE *fstatus = NULL;
> + FILE *fp = NULL;
>
> - fstatus = fopen("/proc/self/status", "r");
> - if (fstatus == NULL)
> - tst_brkm(TFAIL | TERRNO, NULL, "Open dev status failed");
> + fp = fopen("/proc/self/status", "r");
> + if (fp == NULL)
> + tst_brk(TFAIL | TERRNO, "could not open status file");
>
> - while (fgets(line, LINELEN, fstatus) != NULL)
> + while (fgets(line, LINELEN, fp) != NULL) {
> if (strstr(line, "VmLck") != NULL)
> break;
> + }
>
> - ret = sscanf(line, "%*[^0-9]%d%*[^0-9]", lock_sz);
> - if (ret != 1)
> - tst_brkm(TFAIL | TERRNO, NULL, "Get lock size failed");
> + if (sscanf(line, "%*[^0-9]%d%*[^0-9]", lock_sz) != 1)
> + tst_brk(TFAIL | TERRNO, "Get lock size failed");
>
> - fclose(fstatus);
> + fclose(fp);
> }
>
> -static void setup(void)
> +static void run(void)
> {
> - tst_require_root();
> + unsigned int sz_before, sz_after, sz_diff;
>
> - tst_sig(FORK, DEF_HANDLER, cleanup);
> + getvmlck(&sz_before);
>
> - TEST_PAUSE;
> -}
> + addr = mmap(0, MMAPSIZE, PROT_READ | PROT_WRITE,
> + MAP_PRIVATE | MAP_LOCKED | MAP_ANONYMOUS, -1, 0);
>
> -static void cleanup(void)
> -{
> + if (addr != MAP_FAILED)
> + tst_res(TPASS, "mmap() with MAP_LOCKED flag passed");
> + else {
> + tst_res(TFAIL | TERRNO, "mmap() failed");
> + return;
> + }
> +
> + getvmlck(&sz_after);
> + sz_diff = sz_after - sz_before;
> + TST_EXP_EQ_LU(MMAPSIZE / 1024, sz_diff);
> +
> + SAFE_MUNMAP(addr, MMAPSIZE);
> }
> +
> +static struct tst_test test = {
> + .test_all = run,
> + .needs_tmpdir = 1
I suppose that if RLIMIT_MEMLOCK < MMAPSIZE then the test will fail.
The original test required root, but if we are inside a user namespace
then this is not enough:
nsjail -u 0 --rlimit_memlock 0 --chroot / -- /home/rich/kernel/ltp/testcases/kernel/syscalls/mmap/mmap14
[I][2023-09-01T10:39:53+0100] Mode: STANDALONE_ONCE
[I][2023-09-01T10:39:53+0100] Jail parameters: hostname:'NSJAIL', chroot:'/', process:'/home/rich/kernel/ltp/testcases/kernel/syscalls/mmap/mmap14', bind:[::]:0, max_conns:0, max_conns_per_ip:0, time_limit:0, personality:0, daemonize:false, clone_newnet:true, clone_newuser:true, clone_newns:true, clone_newpid:true, clone_newipc:true, clone_newuts:true, clone_newcgroup:true, clone_newtime:false, keep_caps:false, disable_no_new_privs:false, max_cpus:0
[I][2023-09-01T10:39:53+0100] Mount: '/' -> '/' flags:MS_RDONLY|MS_BIND|MS_REC|MS_PRIVATE type:'' options:'' dir:true
[I][2023-09-01T10:39:53+0100] Mount: '/proc' flags:MS_RDONLY type:'proc' options:'' dir:true
[I][2023-09-01T10:39:53+0100] Uid map: inside_uid:0 outside_uid:1000 count:1 newuidmap:false
[I][2023-09-01T10:39:53+0100] Gid map: inside_gid:100 outside_gid:100 count:1 newgidmap:false
[I][2023-09-01T10:39:53+0100] Executing '/home/rich/kernel/ltp/testcases/kernel/syscalls/mmap/mmap14' for '[STANDALONE MODE]'
mmap14 1 TFAIL : mmap14.c:68: mmap of mmapfile failed: errno=EPERM(1): Operation not permitted
[I][2023-09-01T10:39:53+0100] pid=15754 ([STANDALONE MODE]) exited with status: 1, (PIDs left: 0)
That is the original test. It sees we are UID 0 so thinks it can run,
but actually can't.
Probably the best thing to do is check the rlimit and ignore what user
we are. If we are real root (i.e. in the first user namespace) then the
rlimit should not matter, but checking what namespace is not something
we have done before AFAIK. So it's a seperate discussion.
> +};
> --
> 2.41.0
--
Thank you,
Richard.
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 20+ messages in thread* [LTP] [PATCH v2] syscalls/mmap14: Rewrite test using new LTP API
2023-08-25 6:38 ` [LTP] [PATCH 4/5] syscalls/mmap14: Rewrite test using new LTP API Avinesh Kumar
2023-09-01 9:23 ` Richard Palethorpe
@ 2023-09-05 13:28 ` Avinesh Kumar
2023-10-09 10:59 ` Richard Palethorpe
1 sibling, 1 reply; 20+ messages in thread
From: Avinesh Kumar @ 2023-09-05 13:28 UTC (permalink / raw)
To: ltp
Signed-off-by: Avinesh Kumar <akumar@suse.de>
---
Changes in v2:
- Check RLIMIT_MEMLOCK before trying to map locked pages
testcases/kernel/syscalls/mmap/mmap14.c | 138 ++++++++----------------
1 file changed, 46 insertions(+), 92 deletions(-)
diff --git a/testcases/kernel/syscalls/mmap/mmap14.c b/testcases/kernel/syscalls/mmap/mmap14.c
index 31632601b..fba07ef3c 100644
--- a/testcases/kernel/syscalls/mmap/mmap14.c
+++ b/testcases/kernel/syscalls/mmap/mmap14.c
@@ -1,124 +1,78 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2013 FNST, DAN LI <li.dan@cn.fujitsu.com>
- *
- * 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) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
*/
-/*
- * Test Description:
- * Verify MAP_LOCKED works fine.
- * "Lock the pages of the mapped region into memory in the manner of mlock(2)."
+/*\
+ * [Description]
*
- * Expected Result:
- * mmap() should succeed returning the address of the mapped region,
- * and this region should be locked into memory.
+ * Verify that, mmap() call with MAP_LOCKED flag successfully locks
+ * the mapped pages into memory.
*/
-#include <stdio.h>
-#include <sys/mman.h>
-#include "test.h"
+#include <stdio.h>
+#include "tst_test.h"
-#define TEMPFILE "mmapfile"
-#define MMAPSIZE (1UL<<20)
+#define MMAPSIZE (1UL<<21)
#define LINELEN 256
-char *TCID = "mmap14";
-int TST_TOTAL = 1;
-
static char *addr;
-
static void getvmlck(unsigned int *lock_sz);
-static void setup(void);
-static void cleanup(void);
+static struct rlimit rlim;
-int main(int argc, char *argv[])
+static void setup(void)
{
- int lc;
- unsigned int sz_before;
- unsigned int sz_after;
- unsigned int sz_ch;
-
- tst_parse_opts(argc, argv, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
-
- tst_count = 0;
-
- getvmlck(&sz_before);
-
- addr = mmap(NULL, MMAPSIZE, PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_LOCKED | MAP_ANONYMOUS,
- -1, 0);
-
- if (addr == MAP_FAILED) {
- tst_resm(TFAIL | TERRNO, "mmap of %s failed", TEMPFILE);
- continue;
- }
-
- getvmlck(&sz_after);
-
- sz_ch = sz_after - sz_before;
- if (sz_ch == MMAPSIZE / 1024) {
- tst_resm(TPASS, "Functionality of mmap() "
- "successful");
- } else {
- tst_resm(TFAIL, "Expected %luK locked, "
- "get %uK locked",
- MMAPSIZE / 1024, sz_ch);
- }
-
- if (munmap(addr, MMAPSIZE) != 0)
- tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
- }
-
- cleanup();
- tst_exit();
+ SAFE_GETRLIMIT(RLIMIT_MEMLOCK, &rlim);
}
void getvmlck(unsigned int *lock_sz)
{
- int ret;
char line[LINELEN];
- FILE *fstatus = NULL;
+ FILE *fp = NULL;
- fstatus = fopen("/proc/self/status", "r");
- if (fstatus == NULL)
- tst_brkm(TFAIL | TERRNO, NULL, "Open dev status failed");
+ fp = fopen("/proc/self/status", "r");
+ if (fp == NULL)
+ tst_brk(TFAIL | TERRNO, "could not open status file");
- while (fgets(line, LINELEN, fstatus) != NULL)
+ while (fgets(line, LINELEN, fp) != NULL) {
if (strstr(line, "VmLck") != NULL)
break;
+ }
- ret = sscanf(line, "%*[^0-9]%d%*[^0-9]", lock_sz);
- if (ret != 1)
- tst_brkm(TFAIL | TERRNO, NULL, "Get lock size failed");
+ if (sscanf(line, "%*[^0-9]%d%*[^0-9]", lock_sz) != 1)
+ tst_brk(TFAIL | TERRNO, "Getting locked memory size failed");
- fclose(fstatus);
+ fclose(fp);
}
-static void setup(void)
+static void run(void)
{
- tst_require_root();
+ unsigned int sz_before, sz_after, sz_diff;
- tst_sig(FORK, DEF_HANDLER, cleanup);
+ getvmlck(&sz_before);
- TEST_PAUSE;
-}
+ if (((sz_before * 1024) + MMAPSIZE) > rlim.rlim_cur)
+ tst_brk(TBROK, "Trying to exceed RLIMIT_MEMLOCK limit");
-static void cleanup(void)
-{
+ addr = mmap(NULL, MMAPSIZE, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_LOCKED | MAP_ANONYMOUS, -1, 0);
+
+ if (addr != MAP_FAILED) {
+ tst_res(TPASS, "mmap() with MAP_LOCKED flag passed");
+ } else {
+ tst_res(TFAIL | TERRNO, "mmap() failed");
+ return;
+ }
+
+ getvmlck(&sz_after);
+ sz_diff = sz_after - sz_before;
+ TST_EXP_EQ_LU(MMAPSIZE / 1024, sz_diff);
+
+ SAFE_MUNMAP(addr, MMAPSIZE);
}
+
+static struct tst_test test = {
+ .setup = setup,
+ .test_all = run
+};
--
2.41.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [LTP] [PATCH v2] syscalls/mmap14: Rewrite test using new LTP API
2023-09-05 13:28 ` [LTP] [PATCH v2] " Avinesh Kumar
@ 2023-10-09 10:59 ` Richard Palethorpe
2023-10-31 15:39 ` Cyril Hrubis
0 siblings, 1 reply; 20+ messages in thread
From: Richard Palethorpe @ 2023-10-09 10:59 UTC (permalink / raw)
To: Avinesh Kumar; +Cc: ltp
Hello,
Avinesh Kumar <akumar@suse.de> writes:
>
> -static void setup(void)
> +static void run(void)
> {
> - tst_require_root();
> + unsigned int sz_before, sz_after, sz_diff;
>
> - tst_sig(FORK, DEF_HANDLER, cleanup);
> + getvmlck(&sz_before);
>
> - TEST_PAUSE;
> -}
> + if (((sz_before * 1024) + MMAPSIZE) > rlim.rlim_cur)
> + tst_brk(TBROK, "Trying to exceed RLIMIT_MEMLOCK limit");
Should this be TCONF not TBROK?
--
Thank you,
Richard.
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 20+ messages in thread* Re: [LTP] [PATCH v2] syscalls/mmap14: Rewrite test using new LTP API
2023-10-09 10:59 ` Richard Palethorpe
@ 2023-10-31 15:39 ` Cyril Hrubis
0 siblings, 0 replies; 20+ messages in thread
From: Cyril Hrubis @ 2023-10-31 15:39 UTC (permalink / raw)
To: Richard Palethorpe; +Cc: ltp
Hi!
> > -static void setup(void)
> > +static void run(void)
> > {
> > - tst_require_root();
> > + unsigned int sz_before, sz_after, sz_diff;
> >
> > - tst_sig(FORK, DEF_HANDLER, cleanup);
> > + getvmlck(&sz_before);
> >
> > - TEST_PAUSE;
> > -}
> > + if (((sz_before * 1024) + MMAPSIZE) > rlim.rlim_cur)
> > + tst_brk(TBROK, "Trying to exceed RLIMIT_MEMLOCK limit");
>
> Should this be TCONF not TBROK?
TCONF also this should be done in the test setup()
And optionally we may atempt to raise the limit as long as rlim_max >=
than the size we need we can easily raise the rlim_cur.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 20+ messages in thread
* [LTP] [PATCH 5/5] syscalls/mmap15: Rewrite test using new LTP API
2023-08-25 6:38 [LTP] [PATCH 1/5] syscalls/mmap08: Rewrite the test using new LTP API Avinesh Kumar
` (2 preceding siblings ...)
2023-08-25 6:38 ` [LTP] [PATCH 4/5] syscalls/mmap14: Rewrite test using new LTP API Avinesh Kumar
@ 2023-08-25 6:38 ` Avinesh Kumar
2023-09-04 9:23 ` Richard Palethorpe
2023-08-30 12:51 ` [LTP] [PATCH 1/5] syscalls/mmap08: Rewrite the " Cyril Hrubis
4 siblings, 1 reply; 20+ messages in thread
From: Avinesh Kumar @ 2023-08-25 6:38 UTC (permalink / raw)
To: ltp
Signed-off-by: Avinesh Kumar <akumar@suse.de>
---
testcases/kernel/syscalls/mmap/mmap15.c | 117 +++++++-----------------
1 file changed, 35 insertions(+), 82 deletions(-)
diff --git a/testcases/kernel/syscalls/mmap/mmap15.c b/testcases/kernel/syscalls/mmap/mmap15.c
index 443a37eb8..643ed3a8b 100644
--- a/testcases/kernel/syscalls/mmap/mmap15.c
+++ b/testcases/kernel/syscalls/mmap/mmap15.c
@@ -1,113 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) International Business Machines Corp., 2004
* Written by Robbie Williamson
- *
- * 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) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
*/
-/*
- * Test Description: Test that a normal page cannot be mapped into a high
- * memory region.
+/*\
+ * [Description]
+ *
+ * Verify that, a normal page cannot be mapped into a high memory region,
+ * and mmap() call fails with either ENOMEM or EINVAL errno.
*/
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/mount.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/abisize.h"
-
-char *TCID = "mmap15";
-int TST_TOTAL = 1;
+#include "tst_test.h"
#ifdef __ia64__
-# define HIGH_ADDR (void *)(0xa000000000000000UL)
+# define HIGH_ADDR ((void *)(0xa000000000000000UL))
#else
-# define HIGH_ADDR (void *)(-page_size)
+# define HIGH_ADDR ((void *)(-page_size))
#endif
+#define TEMPFILE "mmapfile"
static long page_size;
+static int fd;
-static void setup(void);
-static void cleanup(void);
-
-int main(int ac, char **av)
+static void run(void)
{
- int lc, fd;
- void *addr;
-
#ifdef TST_ABI32
- tst_brkm(TCONF, NULL, "This test is only for 64bit");
+ tst_brkm(TCONF, NULL, "Test is not applicable for 32-bit systems.");
#endif
- tst_parse_opts(ac, av, NULL, NULL);
+ fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
- setup();
+ TESTPTR(mmap(HIGH_ADDR, page_size, PROT_READ, MAP_SHARED | MAP_FIXED, fd, 0));
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- tst_count = 0;
+ if (TST_RET_PTR != MAP_FAILED) {
+ tst_res(TFAIL, "mmap() into high mem region succeeded unexpectedly");
+ SAFE_MUNMAP(TST_RET_PTR, page_size);
+ return;
+ } else if (TST_RET_PTR == MAP_FAILED && (TST_ERR == ENOMEM || TST_ERR == EINVAL))
+ tst_res(TPASS | TERRNO, "mmap() failed with expected errno");
+ else
+ tst_res(TFAIL | TERRNO, "mmap() failed with unexpected errno");
- fd = SAFE_OPEN(cleanup, "testfile", O_RDWR | O_CREAT, 0666);
-
- /* Attempt to mmap into highmem addr, should get ENOMEM */
- addr = mmap(HIGH_ADDR, page_size, PROT_READ,
- MAP_SHARED | MAP_FIXED, fd, 0);
- if (addr != MAP_FAILED) {
- tst_resm(TFAIL, "mmap into high region "
- "succeeded unexpectedly");
- munmap(addr, page_size);
- close(fd);
- continue;
- }
-
- if (errno != ENOMEM && errno != EINVAL) {
- tst_resm(TFAIL | TERRNO, "mmap into high region "
- "failed unexpectedly");
- } else {
- tst_resm(TPASS | TERRNO, "mmap into high region "
- "failed as expected");
- }
-
- SAFE_CLOSE(cleanup, fd);
- }
-
- cleanup();
- tst_exit();
+ SAFE_CLOSE(fd);
}
static void setup(void)
{
- tst_require_root();
-
- tst_tmpdir();
-
page_size = getpagesize();
-
- TEST_PAUSE;
}
static void cleanup(void)
{
- tst_rmdir();
+ if (fd > 0)
+ SAFE_CLOSE(fd);
}
+
+static struct tst_test test = {
+ .setup = setup,
+ .cleanup = cleanup,
+ .test_all = run,
+ .needs_root = 1,
+ .needs_tmpdir = 1
+};
--
2.41.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [LTP] [PATCH 5/5] syscalls/mmap15: Rewrite test using new LTP API
2023-08-25 6:38 ` [LTP] [PATCH 5/5] syscalls/mmap15: " Avinesh Kumar
@ 2023-09-04 9:23 ` Richard Palethorpe
2023-09-05 16:01 ` [LTP] [PATCH v2] " Avinesh Kumar
0 siblings, 1 reply; 20+ messages in thread
From: Richard Palethorpe @ 2023-09-04 9:23 UTC (permalink / raw)
To: Avinesh Kumar; +Cc: ltp
Hello,
Avinesh Kumar <akumar@suse.de> writes:
> Signed-off-by: Avinesh Kumar <akumar@suse.de>
> ---
> testcases/kernel/syscalls/mmap/mmap15.c | 117 +++++++-----------------
> 1 file changed, 35 insertions(+), 82 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/mmap/mmap15.c b/testcases/kernel/syscalls/mmap/mmap15.c
> index 443a37eb8..643ed3a8b 100644
> --- a/testcases/kernel/syscalls/mmap/mmap15.c
> +++ b/testcases/kernel/syscalls/mmap/mmap15.c
> @@ -1,113 +1,66 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> /*
> * Copyright (c) International Business Machines Corp., 2004
> * Written by Robbie Williamson
> - *
> - * 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) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
> */
>
> -/*
> - * Test Description: Test that a normal page cannot be mapped into a high
> - * memory region.
> +/*\
> + * [Description]
> + *
> + * Verify that, a normal page cannot be mapped into a high memory region,
> + * and mmap() call fails with either ENOMEM or EINVAL errno.
> */
>
> -#include <sys/types.h>
> -#include <sys/mman.h>
> -#include <sys/mount.h>
> -#include <sys/stat.h>
> -#include <errno.h>
> -#include <fcntl.h>
> -#include <signal.h>
> -#include <stdint.h>
> -#include <stdio.h>
> -#include <stdlib.h>
> -#include <string.h>
> -#include <unistd.h>
> -#include "test.h"
> -#include "safe_macros.h"
> -#include "lapi/abisize.h"
> -
> -char *TCID = "mmap15";
> -int TST_TOTAL = 1;
> +#include "tst_test.h"
>
> #ifdef __ia64__
> -# define HIGH_ADDR (void *)(0xa000000000000000UL)
> +# define HIGH_ADDR ((void *)(0xa000000000000000UL))
> #else
> -# define HIGH_ADDR (void *)(-page_size)
> +# define HIGH_ADDR ((void *)(-page_size))
> #endif
>
> +#define TEMPFILE "mmapfile"
> static long page_size;
> +static int fd;
>
> -static void setup(void);
> -static void cleanup(void);
> -
> -int main(int ac, char **av)
> +static void run(void)
> {
> - int lc, fd;
> - void *addr;
> -
> #ifdef TST_ABI32
> - tst_brkm(TCONF, NULL, "This test is only for 64bit");
> + tst_brkm(TCONF, NULL, "Test is not applicable for 32-bit
> systems.");
This is still the old API. I suggest compiling with -m32 set to check
TCONF works as expected.
--
Thank you,
Richard.
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 20+ messages in thread* [LTP] [PATCH v2] syscalls/mmap15: Rewrite test using new LTP API
2023-09-04 9:23 ` Richard Palethorpe
@ 2023-09-05 16:01 ` Avinesh Kumar
2023-10-31 15:31 ` Cyril Hrubis
0 siblings, 1 reply; 20+ messages in thread
From: Avinesh Kumar @ 2023-09-05 16:01 UTC (permalink / raw)
To: ltp
Signed-off-by: Avinesh Kumar <akumar@suse.de>
---
testcases/kernel/syscalls/mmap/mmap15.c | 116 +++++++-----------------
1 file changed, 35 insertions(+), 81 deletions(-)
diff --git a/testcases/kernel/syscalls/mmap/mmap15.c b/testcases/kernel/syscalls/mmap/mmap15.c
index 443a37eb8..763b2ecbf 100644
--- a/testcases/kernel/syscalls/mmap/mmap15.c
+++ b/testcases/kernel/syscalls/mmap/mmap15.c
@@ -1,113 +1,67 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) International Business Machines Corp., 2004
* Written by Robbie Williamson
- *
- * 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) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
*/
-/*
- * Test Description: Test that a normal page cannot be mapped into a high
- * memory region.
+/*\
+ * [Description]
+ *
+ * Verify that, a normal page cannot be mapped into a high memory region,
+ * and mmap() call fails with either ENOMEM or EINVAL errno.
*/
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/mount.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/abisize.h"
-
-char *TCID = "mmap15";
-int TST_TOTAL = 1;
+#include "tst_test.h"
#ifdef __ia64__
-# define HIGH_ADDR (void *)(0xa000000000000000UL)
+# define HIGH_ADDR ((void *)(0xa000000000000000UL))
#else
-# define HIGH_ADDR (void *)(-page_size)
+# define HIGH_ADDR ((void *)(-page_size))
#endif
+#define TEMPFILE "mmapfile"
static long page_size;
+static int fd;
-static void setup(void);
-static void cleanup(void);
-
-int main(int ac, char **av)
+static void run(void)
{
- int lc, fd;
- void *addr;
-
#ifdef TST_ABI32
- tst_brkm(TCONF, NULL, "This test is only for 64bit");
+ tst_brk(TCONF, "Test is not applicable for 32-bit systems.");
#endif
- tst_parse_opts(ac, av, NULL, NULL);
+ fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
- setup();
+ TESTPTR(mmap(HIGH_ADDR, page_size, PROT_READ, MAP_SHARED | MAP_FIXED, fd, 0));
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- tst_count = 0;
-
- fd = SAFE_OPEN(cleanup, "testfile", O_RDWR | O_CREAT, 0666);
-
- /* Attempt to mmap into highmem addr, should get ENOMEM */
- addr = mmap(HIGH_ADDR, page_size, PROT_READ,
- MAP_SHARED | MAP_FIXED, fd, 0);
- if (addr != MAP_FAILED) {
- tst_resm(TFAIL, "mmap into high region "
- "succeeded unexpectedly");
- munmap(addr, page_size);
- close(fd);
- continue;
- }
-
- if (errno != ENOMEM && errno != EINVAL) {
- tst_resm(TFAIL | TERRNO, "mmap into high region "
- "failed unexpectedly");
- } else {
- tst_resm(TPASS | TERRNO, "mmap into high region "
- "failed as expected");
- }
-
- SAFE_CLOSE(cleanup, fd);
+ if (TST_RET_PTR != MAP_FAILED) {
+ tst_res(TFAIL, "mmap() into high mem region succeeded unexpectedly");
+ SAFE_MUNMAP(TST_RET_PTR, page_size);
+ return;
+ } else if (TST_RET_PTR == MAP_FAILED && (TST_ERR == ENOMEM || TST_ERR == EINVAL)) {
+ tst_res(TPASS | TERRNO, "mmap() failed with expected errno");
+ } else {
+ tst_res(TFAIL | TERRNO, "mmap() failed with unexpected errno");
}
- cleanup();
- tst_exit();
+ SAFE_CLOSE(fd);
}
static void setup(void)
{
- tst_require_root();
-
- tst_tmpdir();
-
page_size = getpagesize();
-
- TEST_PAUSE;
}
static void cleanup(void)
{
- tst_rmdir();
+ if (fd > 0)
+ SAFE_CLOSE(fd);
}
+
+static struct tst_test test = {
+ .setup = setup,
+ .cleanup = cleanup,
+ .test_all = run,
+ .needs_root = 1,
+ .needs_tmpdir = 1
+};
--
2.41.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [LTP] [PATCH v2] syscalls/mmap15: Rewrite test using new LTP API
2023-09-05 16:01 ` [LTP] [PATCH v2] " Avinesh Kumar
@ 2023-10-31 15:31 ` Cyril Hrubis
2023-12-11 20:49 ` [LTP] [PATCH v3] " Avinesh Kumar
0 siblings, 1 reply; 20+ messages in thread
From: Cyril Hrubis @ 2023-10-31 15:31 UTC (permalink / raw)
To: Avinesh Kumar; +Cc: ltp
Hi!
> + if (TST_RET_PTR != MAP_FAILED) {
> + tst_res(TFAIL, "mmap() into high mem region succeeded unexpectedly");
> + SAFE_MUNMAP(TST_RET_PTR, page_size);
> + return;
> + } else if (TST_RET_PTR == MAP_FAILED && (TST_ERR == ENOMEM || TST_ERR == EINVAL)) {
There is no reason for an else branch if we do return in the previous
one.
Also we do return so TST_RET_PTR is always MAP_FAILED here, no need to
test it again.
> + tst_res(TPASS | TERRNO, "mmap() failed with expected errno");
> + } else {
> + tst_res(TFAIL | TERRNO, "mmap() failed with unexpected errno");
> }
>
> - cleanup();
> - tst_exit();
> + SAFE_CLOSE(fd);
> }
>
> static void setup(void)
> {
> - tst_require_root();
> -
> - tst_tmpdir();
> -
> page_size = getpagesize();
> -
> - TEST_PAUSE;
> }
>
> static void cleanup(void)
> {
> - tst_rmdir();
> + if (fd > 0)
> + SAFE_CLOSE(fd);
> }
> +
> +static struct tst_test test = {
> + .setup = setup,
> + .cleanup = cleanup,
> + .test_all = run,
> + .needs_root = 1,
Do we really need root?
> + .needs_tmpdir = 1
> +};
> --
> 2.41.0
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 20+ messages in thread* [LTP] [PATCH v3] syscalls/mmap15: Rewrite test using new LTP API
2023-10-31 15:31 ` Cyril Hrubis
@ 2023-12-11 20:49 ` Avinesh Kumar
2024-01-09 17:15 ` Petr Vorel
0 siblings, 1 reply; 20+ messages in thread
From: Avinesh Kumar @ 2023-12-11 20:49 UTC (permalink / raw)
To: ltp
Signed-off-by: Avinesh Kumar <akumar@suse.de>
---
testcases/kernel/syscalls/mmap/mmap15.c | 115 +++++++-----------------
1 file changed, 34 insertions(+), 81 deletions(-)
diff --git a/testcases/kernel/syscalls/mmap/mmap15.c b/testcases/kernel/syscalls/mmap/mmap15.c
index 443a37eb8..42005a9ec 100644
--- a/testcases/kernel/syscalls/mmap/mmap15.c
+++ b/testcases/kernel/syscalls/mmap/mmap15.c
@@ -1,113 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) International Business Machines Corp., 2004
* Written by Robbie Williamson
- *
- * 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) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
*/
-/*
- * Test Description: Test that a normal page cannot be mapped into a high
- * memory region.
+/*\
+ * [Description]
+ *
+ * Verify that, a normal page cannot be mapped into a high memory region,
+ * and mmap() call fails with either ENOMEM or EINVAL errno.
*/
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/mount.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/abisize.h"
-
-char *TCID = "mmap15";
-int TST_TOTAL = 1;
+#include "tst_test.h"
#ifdef __ia64__
-# define HIGH_ADDR (void *)(0xa000000000000000UL)
+# define HIGH_ADDR ((void *)(0xa000000000000000UL))
#else
-# define HIGH_ADDR (void *)(-page_size)
+# define HIGH_ADDR ((void *)(-page_size))
#endif
+#define TEMPFILE "mmapfile"
static long page_size;
+static int fd;
-static void setup(void);
-static void cleanup(void);
-
-int main(int ac, char **av)
+static void run(void)
{
- int lc, fd;
- void *addr;
-
#ifdef TST_ABI32
- tst_brkm(TCONF, NULL, "This test is only for 64bit");
+ tst_brk(TCONF, "Test is not applicable for 32-bit systems.");
#endif
- tst_parse_opts(ac, av, NULL, NULL);
+ fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
- setup();
+ TESTPTR(mmap(HIGH_ADDR, page_size, PROT_READ, MAP_SHARED | MAP_FIXED, fd, 0));
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- tst_count = 0;
-
- fd = SAFE_OPEN(cleanup, "testfile", O_RDWR | O_CREAT, 0666);
-
- /* Attempt to mmap into highmem addr, should get ENOMEM */
- addr = mmap(HIGH_ADDR, page_size, PROT_READ,
- MAP_SHARED | MAP_FIXED, fd, 0);
- if (addr != MAP_FAILED) {
- tst_resm(TFAIL, "mmap into high region "
- "succeeded unexpectedly");
- munmap(addr, page_size);
- close(fd);
- continue;
- }
-
- if (errno != ENOMEM && errno != EINVAL) {
- tst_resm(TFAIL | TERRNO, "mmap into high region "
- "failed unexpectedly");
- } else {
- tst_resm(TPASS | TERRNO, "mmap into high region "
- "failed as expected");
- }
-
- SAFE_CLOSE(cleanup, fd);
+ if (TST_RET_PTR != MAP_FAILED) {
+ tst_res(TFAIL, "mmap() into high mem region succeeded unexpectedly");
+ SAFE_MUNMAP(TST_RET_PTR, page_size);
+ return;
}
+ if (TST_ERR == ENOMEM || TST_ERR == EINVAL)
+ tst_res(TPASS | TERRNO, "mmap() failed with expected errno");
+ else
+ tst_res(TFAIL | TERRNO, "mmap() failed with unexpected errno");
- cleanup();
- tst_exit();
+ SAFE_CLOSE(fd);
}
static void setup(void)
{
- tst_require_root();
-
- tst_tmpdir();
-
page_size = getpagesize();
-
- TEST_PAUSE;
}
static void cleanup(void)
{
- tst_rmdir();
+ if (fd > 0)
+ SAFE_CLOSE(fd);
}
+
+static struct tst_test test = {
+ .setup = setup,
+ .cleanup = cleanup,
+ .test_all = run,
+ .needs_tmpdir = 1
+};
--
2.43.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 20+ messages in thread* Re: [LTP] [PATCH v3] syscalls/mmap15: Rewrite test using new LTP API
2023-12-11 20:49 ` [LTP] [PATCH v3] " Avinesh Kumar
@ 2024-01-09 17:15 ` Petr Vorel
2024-01-10 9:41 ` Avinesh Kumar
0 siblings, 1 reply; 20+ messages in thread
From: Petr Vorel @ 2024-01-09 17:15 UTC (permalink / raw)
To: Avinesh Kumar; +Cc: ltp
Hi Avinesh,
I'm sorry v3 come too late, although Cyril pointed out not needed needs_root and
else if => if change, in the end v2 was merged.
Thus I fixed this as a separate change.
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [LTP] [PATCH v3] syscalls/mmap15: Rewrite test using new LTP API
2024-01-09 17:15 ` Petr Vorel
@ 2024-01-10 9:41 ` Avinesh Kumar
0 siblings, 0 replies; 20+ messages in thread
From: Avinesh Kumar @ 2024-01-10 9:41 UTC (permalink / raw)
To: Petr Vorel; +Cc: ltp
On Tuesday, January 9, 2024 6:15:02 PM CET Petr Vorel wrote:
> Hi Avinesh,
>
> I'm sorry v3 come too late, although Cyril pointed out not needed needs_root
> and else if => if change, in the end v2 was merged.
>
> Thus I fixed this as a separate change.
>
> Kind regards,
> Petr
Hi Petr,
Thank you! I didn't see a reply with merged. Maybe I should have checked the
version on master branch before sending the revision.
--
Avinesh
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [LTP] [PATCH 1/5] syscalls/mmap08: Rewrite the test using new LTP API
2023-08-25 6:38 [LTP] [PATCH 1/5] syscalls/mmap08: Rewrite the test using new LTP API Avinesh Kumar
` (3 preceding siblings ...)
2023-08-25 6:38 ` [LTP] [PATCH 5/5] syscalls/mmap15: " Avinesh Kumar
@ 2023-08-30 12:51 ` Cyril Hrubis
4 siblings, 0 replies; 20+ messages in thread
From: Cyril Hrubis @ 2023-08-30 12:51 UTC (permalink / raw)
To: Avinesh Kumar; +Cc: ltp
Hi!
Pushed with a minor changes, thanks.
Apart from changes similar to the previous tests I've also removed the
whole part that writes the file. There is no point in writing the file
if we are closing the fd before we pass it to the mmap() since the fd is
no longer associated with the file content. So now the test just open
and closes file and then uses that fd as an invalid one.
Full diff:
--- a/testcases/kernel/syscalls/mmap/mmap08.c
+++ b/testcases/kernel/syscalls/mmap/mmap08.c
@@ -21,30 +21,22 @@ static int fd;
static void setup(void)
{
- char *buf;
-
- page_sz = getpagesize();
-
- buf = SAFE_CALLOC(page_sz, sizeof(char));
- memset(buf, 'A', page_sz);
-
fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
- SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, page_sz);
- free(buf);
SAFE_CLOSE(fd);
}
static void run(void)
{
- TESTPTR(mmap(0, page_sz, PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0));
+ TESTPTR(mmap(NULL, page_sz, PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0));
if (TST_RET_PTR != MAP_FAILED) {
tst_res(TFAIL, "mmap() passed unexpectedly");
SAFE_MUNMAP(TST_RET_PTR, page_sz);
- } else if (TST_ERR == EBADF)
+ } else if (TST_ERR == EBADF) {
tst_res(TPASS, "mmap() failed with EBADF");
- else
+ } else {
tst_res(TFAIL | TERRNO, "mmap() failed with an invalid errno");
+ }
}
static void cleanup(void)
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 20+ messages in thread