Home | History | Annotate | Download | only in mremap
      1 /*
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2001
      4  *
      5  *   This program is free software;  you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation; either version 2 of the License, or
      8  *   (at your option) any later version.
      9  *
     10  *   This program is distributed in the hope that it will be useful,
     11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13  *   the GNU General Public License for more details.
     14  *
     15  *   You should have received a copy of the GNU General Public License
     16  *   along with this program;  if not, write to the Free Software
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /*
     21  * Test Name: mremap02
     22  *
     23  * Test Description:
     24  *  Verify that,
     25  *   mremap() fails when used to expand the existing virtual memory mapped
     26  *   region to the requested size, if the virtual memory area previously
     27  *   mapped was not page aligned or invalid argument specified.
     28  *
     29  * Expected Result:
     30  *  mremap() should return -1 and set errno to EINVAL.
     31  *
     32  * Algorithm:
     33  *  Setup:
     34  *   Setup signal handling.
     35  *   Pause for SIGUSR1 if option specified.
     36  *
     37  *  Test:
     38  *   Loop if the proper options are given.
     39  *   Execute system call
     40  *   Check return code, if system call failed (return=-1)
     41  *	if errno set == expected errno
     42  *		Issue sys call fails with expected return value and errno.
     43  *	Otherwise,
     44  *		Issue sys call fails with unexpected errno.
     45  *   Otherwise,
     46  *	Issue sys call returns unexpected value.
     47  *
     48  *  Cleanup:
     49  *   Print errno log and/or timing stats if options given
     50  *
     51  * Usage:  <for command-line>
     52  *  mremap02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
     53  *     where,  -c n : Run n copies concurrently.
     54  *	       -e   : Turn on errno logging.
     55  *	       -i n : Execute test n times.
     56  *	       -I x : Execute test for x seconds.
     57  *	       -p x : Pause for x seconds between iterations.
     58  *	       -t   : Turn on syscall timing.
     59  *
     60  * HISTORY
     61  *	07/2001 Ported by Wayne Boyer
     62  *
     63  *      11/09/2001 Manoj Iyer (manjo (at) austin.ibm.com)
     64  *      Modified.
     65  *      - #include <linux/mman.h> should not be included as per man page for
     66  *        mremap, #include <sys/mman.h> alone should do the job. But inorder
     67  *        to include definition of MREMAP_MAYMOVE defined in bits/mman.h
     68  *        (included by sys/mman.h) __USE_GNU needs to be defined.
     69  *        There may be a more elegant way of doing this...
     70  *
     71  *
     72  * RESTRICTIONS:
     73  *  None.
     74  */
     75 #include <errno.h>
     76 #include <unistd.h>
     77 #include <fcntl.h>
     78 #define __USE_GNU
     79 #include <sys/mman.h>
     80 #undef __USE_GNU
     81 
     82 #include "test.h"
     83 
     84 char *TCID = "mremap02";
     85 int TST_TOTAL = 1;
     86 char *addr;			/* addr of memory mapped region */
     87 int memsize;			/* memory mapped size */
     88 int newsize;			/* new size of virtual memory block */
     89 
     90 void setup();			/* Main setup function of test */
     91 void cleanup();			/* cleanup function for the test */
     92 
     93 int main(int ac, char **av)
     94 {
     95 	int lc;
     96 
     97 	tst_parse_opts(ac, av, NULL, NULL);
     98 
     99 	setup();
    100 
    101 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    102 
    103 		tst_count = 0;
    104 
    105 		/*
    106 		 * Attempt to expand the existing mapped
    107 		 * memory region (memsize) by newsize limits using
    108 		 * mremap() should fail as old virtual address is not
    109 		 * page aligned.
    110 		 */
    111 		errno = 0;
    112 		addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE);
    113 		TEST_ERRNO = errno;
    114 
    115 		/* Check for the return value of mremap() */
    116 		if (addr != MAP_FAILED) {
    117 			tst_resm(TFAIL,
    118 				 "mremap returned invalid value, expected: -1");
    119 
    120 			/* Unmap the mapped memory region */
    121 			if (munmap(addr, newsize) != 0) {
    122 				tst_brkm(TBROK, cleanup, "munmap fails to "
    123 					 "unmap the expanded memory region, "
    124 					 "error=%d", errno);
    125 			}
    126 			continue;
    127 		}
    128 
    129 		if (errno == EINVAL) {
    130 			tst_resm(TPASS, "mremap() Failed, 'invalid argument "
    131 				 "specified' - errno %d", TEST_ERRNO);
    132 		} else {
    133 			tst_resm(TFAIL, "mremap() Failed, "
    134 				 "'Unexpected errno %d", TEST_ERRNO);
    135 		}
    136 	}
    137 
    138 	cleanup();
    139 	tst_exit();
    140 
    141 }
    142 
    143 /*
    144  * setup() - performs all ONE TIME setup for this test.
    145  *
    146  * Get system page size, Set the size of virtual memory area and the
    147  * new size after resize, Set the virtual memory address such that it
    148  * is not aligned.
    149  */
    150 void setup(void)
    151 {
    152 
    153 	tst_sig(FORK, DEF_HANDLER, cleanup);
    154 
    155 	TEST_PAUSE;
    156 
    157 	/* Get the system page size */
    158 	if ((memsize = getpagesize()) < 0) {
    159 		tst_brkm(TFAIL, NULL,
    160 			 "getpagesize() fails to get system page size");
    161 	}
    162 
    163 	/* Get the New size of virtual memory block after resize */
    164 	newsize = (memsize * 2);
    165 
    166 	/* Set the old virtual memory address */
    167 	addr = (char *)(addr + (memsize - 1));
    168 }
    169 
    170 /*
    171  * cleanup() - performs all ONE TIME cleanup for this test at
    172  *             completion or premature exit.
    173  */
    174 void cleanup(void)
    175 {
    176 
    177 	/* Exit the program */
    178 
    179 }
    180