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