syscalls/mmap08: Rewrite the test using new LTP API
PATCH 1/5 syscalls/mmap08: Rewrite the test using new LTP API
Usage of SAFE_CALLOC is based on the patch sent earlier[1]
[1] https://lore.kernel.org/ltp/[email protected]/
--- testcases/kernel/syscalls/mmap/mmap08.c | 159 ++++++------------------ 1 file changed, 39 insertions(+), 120 deletions(-)
testcases/kernel/syscalls/mmap/mmap08.c +39 -120
@@ -1,142 +1,61 @@ +// 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 +* 07/2001 Ported by Wayne Boyer +* Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]> */ -/* -* Test Description: -* Verify that mmap() fails to map a file creating a mapped region -* when the file specified by file descriptor is not valid. +/*\ +* [Description] * -* Expected Result: -* mmap() should fail returning -1 and errno should get set to EBADF. -* -* HISTORY -* 07/2001 Ported by Wayne Boyer +* verify that, mmap() calls fails with errno EBADF when a file mapping +* is requested but the fd is not a valid file descriptor. */ -#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 "test.h" - -#define TEMPFILE "mmapfile" -char *TCID ="mmap08" ; -int TST_TOTAL =1 ; +#include <stdlib.h> +#include "tst_test.h" +#define TEMPFILE "mmapfile"static size_t page_sz; -static char *addr; -static int fildes; - -static void setup (void ); -static void cleanup (void ); - -int main (int ac,char **av) -{ -int lc; - -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - - tst_count =0 ; - -/* -* Call mmap to map the temporary file 'TEMPFILE' -* which is already closed. so, fildes is not valid. -*/ - errno =0 ; - addr =mmap (0 , page_sz,PROT_WRITE , -MAP_FILE |MAP_SHARED , fildes,0 ); -TEST_ERRNO = errno; - -/* Check for the return value of mmap() */ -if (addr !=MAP_FAILED ) { -tst_resm (TFAIL ,"mmap() didn't fail (%p != %p)" , - addr,MAP_FAILED ); -/* Unmap the mapped memory */ -if (munmap (addr, page_sz) !=0 ) { -tst_brkm (TBROK , cleanup,"munmap() failed" ); - } -continue ; - } -if (TEST_ERRNO ==EBADF ) { -tst_resm (TPASS ,"mmap failed with EBADF" ); - }else { -tst_resm (TFAIL |TERRNO , -"mmap failed with an invalid errno" ); - } - } - -cleanup (); -tst_exit (); -} +static int fd;static void setup (void ) { -char *tst_buff; - -tst_sig (NOFORK ,DEF_HANDLER , cleanup); - -TEST_PAUSE ; +char *buf; page_sz =getpagesize (); -if ((tst_buff =calloc (page_sz,sizeof (char ))) ==NULL ) { -tst_brkm (TFAIL ,NULL , -"calloc() failed to allocate space for tst_buff" ); - } + buf =SAFE_CALLOC (page_sz,sizeof (char )); +memset (buf,'A' , page_sz); -/* Fill the test buffer with the known data */ -memset (tst_buff,'A' , page_sz); - -tst_tmpdir (); - -/* Creat a temporary file used for mapping */ -if ((fildes =open (TEMPFILE ,O_WRONLY |O_CREAT ,0666 )) <0 ) { -free (tst_buff); -tst_brkm (TFAIL , cleanup,"opening %s failed" ,TEMPFILE ); - } - -/* Write test buffer contents into temporary file */ -if (write (fildes, tst_buff, page_sz) != (int )page_sz) { -free (tst_buff); -tst_brkm (TFAIL , cleanup,"writing to %s failed" ,TEMPFILE ); - } - -/* Free the memory allocated for test buffer */ -free (tst_buff); + fd =SAFE_OPEN (TEMPFILE ,O_RDWR |O_CREAT ,0666 ); +SAFE_WRITE (SAFE_WRITE_ALL , fd, buf, page_sz); +free (buf); +SAFE_CLOSE (fd); +} -/* Close the temporary file opened for writing */ -if (close (fildes) <0 ) { -tst_brkm (TFAIL , cleanup,"closing %s failed" ,TEMPFILE ); - } +static void run (void ) +{ +TESTPTR (mmap (0 , 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 ) +tst_res (TPASS ,"mmap() failed with EBADF" ); +else +tst_res (TFAIL |TERRNO ,"mmap() failed with an invalid errno" ); }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 +};
Signature · 5 lines
2.41.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 2/5 syscalls/mmap09: Rewrite the test using new LTP API
--- testcases/kernel/syscalls/mmap/mmap09.c | 134 ++++++++---------------- 1 file changed, 45 insertions(+), 89 deletions(-)
testcases/kernel/syscalls/mmap/mmap09.c +45 -89
@@ -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 <[email protected]> */ -/* -* 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 +};
Signature · 5 lines
2.41.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/5] syscalls/mmap09: Rewrite the test using new LTP API
Hello,
Quoted 106 lines · Avinesh Kumar
Signed-off-by Avinesh Kumar <[email protected]>--- testcases/kernel/syscalls/mmap/mmap09.c | 134 ++++++++---------------- 1 file changed, 45 insertions(+), 89 deletions(-)testcases/kernel/syscalls/mmap/mmap09.c +21
-59@@ -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 <[email protected]> */ -/* -* 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));
testcases/kernel/syscalls/mmap/mmap09.c line 41
Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap09.c +2
-6+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?
Quoted 48 lines
testcases/kernel/syscalls/mmap/mmap09.c +12
-24+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);
testcases/kernel/syscalls/mmap/mmap09.c line 74
Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap09.c +9
-0+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
Signature · 5 lines
Thank you, Richard. -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/5] syscalls/mmap09: Rewrite the test using new LTP API
Hi!
Quoted 4 lines
Quoted 2 lines
(quoted diff) +1
-0+ 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.
Quoted 8 lines
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.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/5] syscalls/mmap09: Rewrite the test using new LTP API
Hello,
Quoted 13 lines · Cyril Hrubis
Hi!
Quoted 4 lines
Quoted 2 lines
(quoted diff) +1
-0+ 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.
Quoted 5 lines
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.
Quoted 8 lines
Quoted 3 lines
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?
Signature · 5 lines
Thank you, Richard. -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 3/5 syscalls/mmap13: Rewrite the test using new API
In reply to Avinesh Kumar--- testcases/kernel/syscalls/mmap/mmap13.c | 174 +++++++++--------------- 1 file changed, 65 insertions(+), 109 deletions(-)
testcases/kernel/syscalls/mmap/mmap13.c +65 -109
@@ -1,142 +1,98 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) 2013 FNST, DAN LI <[email protected]> +* Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]> +*/ + +/*\ +* [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 +};
Signature · 5 lines
2.41.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 3/5] syscalls/mmap13: Rewrite the test using new API
Hello,
Quoted 53 lines · Avinesh Kumar
Signed-off-by Avinesh Kumar <[email protected]>--- testcases/kernel/syscalls/mmap/mmap13.c | 174 +++++++++--------------- 1 file changed, 65 insertions(+), 109 deletions(-)testcases/kernel/syscalls/mmap/mmap13.c +15
-22@@ -1,142 +1,98 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) 2013 FNST, DAN LI <[email protected]> +* Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]> +*/ + +/*\ +* [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)| | +* +------------+------------+------------------------+
testcases/kernel/syscalls/mmap/mmap13.c line 31
Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap13.c +10
-0+* |<---------->|<---------->|<---------------------->|<---------------> +* | 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.
Quoted 71 lines
testcases/kernel/syscalls/mmap/mmap13.c +4
-56*/ -#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" ); -
testcases/kernel/syscalls/mmap/mmap13.c line 51
Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap13.c +5
-5- 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.
Signature · 5 lines
Thank you, Richard. -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 4/5 syscalls/mmap14: Rewrite test using new LTP API
In reply to Avinesh Kumar--- testcases/kernel/syscalls/mmap/mmap14.c | 135 +++++++----------------- 1 file changed, 40 insertions(+), 95 deletions(-)
testcases/kernel/syscalls/mmap/mmap14.c +40 -95
@@ -1,124 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) 2013 FNST, DAN LI <[email protected]> -* -* 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 <[email protected]> */ -/* -* 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 +};
Signature · 5 lines
2.41.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 4/5] syscalls/mmap14: Rewrite test using new LTP API
Hello,
Quoted 163 lines · Avinesh Kumar
Signed-off-by Avinesh Kumar <[email protected]>--- testcases/kernel/syscalls/mmap/mmap14.c | 135 +++++++----------------- 1 file changed, 40 insertions(+), 95 deletions(-)testcases/kernel/syscalls/mmap/mmap14.c +30
-95@@ -1,124 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) 2013 FNST, DAN LI <[email protected]> -* -* 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 <[email protected]> */ -/* -* 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 ; + } +
testcases/kernel/syscalls/mmap/mmap14.c line 68
Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap14.c +9
-0+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.
Quoted 3 lines
testcases/kernel/syscalls/mmap/mmap14.c +1
-0+};Signature · 1 line
2.41.0
Signature · 5 lines
Thank you, Richard. -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 1/1 syscalls/mmap14: Rewrite test using new LTP API
In reply to Avinesh Kumar---
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(-)
testcases/kernel/syscalls/mmap/mmap14.c +46 -92
@@ -1,124 +1,78 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) 2013 FNST, DAN LI <[email protected]> -* -* 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 <[email protected]> */ -/* -* 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 +};
Signature · 5 lines
2.41.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2] syscalls/mmap14: Rewrite test using new LTP API
Hello,
Quoted 4 lines · Avinesh Kumar
testcases/kernel/syscalls/mmap/mmap14.c +1
-1-static void setup (void ) +static void run (void ) {
testcases/kernel/syscalls/mmap/mmap14.c line 56
Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap14.c +4
-4-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?
Signature · 5 lines
Thank you, Richard. -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2] syscalls/mmap14: Rewrite test using new LTP API
Hi!
Quoted 9 lines
Quoted 9 lines
testcases/kernel/syscalls/mmap/mmap14.c +3
-3-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);
Quoted 6 lines
testcases/kernel/syscalls/mmap/mmap14.c line 56
Quoted 4 lines
testcases/kernel/syscalls/mmap/mmap14.c +2
-2-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.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 5/5 syscalls/mmap15: Rewrite test using new LTP API
In reply to Avinesh Kumar--- testcases/kernel/syscalls/mmap/mmap15.c | 117 +++++++----------------- 1 file changed, 35 insertions(+), 82 deletions(-)
testcases/kernel/syscalls/mmap/mmap15.c +35 -82
@@ -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 <[email protected]> */ -/* -* 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 +};
Signature · 5 lines
2.41.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 5/5] syscalls/mmap15: Rewrite test using new LTP API
Hello,
Quoted 76 lines · Avinesh Kumar
Signed-off-by Avinesh Kumar <[email protected]>--- testcases/kernel/syscalls/mmap/mmap15.c | 117 +++++++----------------- 1 file changed, 35 insertions(+), 82 deletions(-)testcases/kernel/syscalls/mmap/mmap15.c +12
-40@@ -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 <[email protected]> */ -/* -* 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 ); -
testcases/kernel/syscalls/mmap/mmap15.c line 31
Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap15.c +2
-5-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.
Signature · 5 lines
Thank you, Richard. -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 1/1 syscalls/mmap15: Rewrite test using new LTP API
--- testcases/kernel/syscalls/mmap/mmap15.c | 116 +++++++----------------- 1 file changed, 35 insertions(+), 81 deletions(-)
testcases/kernel/syscalls/mmap/mmap15.c +35 -81
@@ -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 <[email protected]> */ -/* -* 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 +};
Signature · 5 lines
2.41.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2] syscalls/mmap15: Rewrite test using new LTP API
Hi!
testcases/kernel/syscalls/mmap/mmap15.c line 41
Quoted 5 lines
testcases/kernel/syscalls/mmap/mmap15.c +5
-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 )) {
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.
Quoted 23 lines
testcases/kernel/syscalls/mmap/mmap15.c +4
-8+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 ) {
testcases/kernel/syscalls/mmap/mmap15.c line 65
Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap15.c +8
-1-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?
Quoted 8 lines
testcases/kernel/syscalls/mmap/mmap15.c +2
-0+ .needs_tmpdir = 1 +};Signature · 5 lines
2.41.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 1/1 syscalls/mmap15: Rewrite test using new LTP API
--- testcases/kernel/syscalls/mmap/mmap15.c | 115 +++++++----------------- 1 file changed, 34 insertions(+), 81 deletions(-)
testcases/kernel/syscalls/mmap/mmap15.c +34 -81
@@ -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 <[email protected]> */ -/* -* 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 +};
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v3] syscalls/mmap15: Rewrite test using new LTP API
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
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v3] syscalls/mmap15: Rewrite test using new LTP API
Quoted 9 lines · CET Petr Vorel
Hi Avinesh,
I'm sorry v3 come too late, although Cyril pointed out not needed
needs_rootand 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
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/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:
testcases/kernel/syscalls/mmap/mmap08.c +4 -12
@@ -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 )
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp