blob: b78196855890ee73916dac370e758caa74538bec [file] [log] [blame]
/*
* 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
*/
/*
* DESCRIPTION
* Testcase to check the error conditions for mprotect(2)
*
* ALGORITHM
* test1:
* Invoke mprotect() with an address of 0. Check if error
* is set to ENOMEM.
* test2:
* Invoke mprotect() with an address that is not a multiple
* of PAGESIZE. EINVAL
* test3:
* Mmap a file with only read permission (PROT_READ).
* Try to set write permission (PROT_WRITE) using mprotect(2).
* Check that error is set to EACCES.
*
* HISTORY
* 07/2001 Ported by Wayne Boyer
* 03/2002 Paul Larson: case 1 should expect ENOMEM not EFAULT
*/
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include "test.h"
#include "usctest.h"
#ifndef PAGESIZE
#define PAGESIZE 4096
#endif
static void cleanup(void);
static void setup(void);
static void setup1(void);
static void setup2(void);
static void setup3(void);
char *TCID = "mprotect01";
int TST_TOTAL = 3;
static void *addr1, *addr2, *addr3;
static int fd;
static int exp_enos[] = { ENOMEM, EINVAL, EACCES, 0 };
struct test_case_t {
void **addr;
int len;
int prot;
int error;
void (*setupfunc) ();
} TC[] = {
#ifdef __ia64__
/* Check for ENOMEM passing memory that cannot be accessed. */
{&addr1, 1024, PROT_READ, ENOMEM, setup1},
#else
/* Check for ENOMEM passing memory that cannot be accessed. */
{&addr1, 1024, PROT_READ, ENOMEM, NULL},
#endif
/*
* Check for EINVAL by passing a pointer which is not a
* multiple of PAGESIZE.
*/
{&addr2, 1024, PROT_READ, EINVAL, setup2},
/*
* Check for EACCES by trying to mark a section of memory
* which has been mmap'ed as read-only, as PROT_WRITE
*/
{&addr3, PAGESIZE, PROT_WRITE, EACCES, setup3}
};
int main(int ac, char **av)
{
int lc;
int i;
char *msg;
if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
setup();
/* set up the expected errnos */
TEST_EXP_ENOS(exp_enos);
for (lc = 0; TEST_LOOPING(lc); lc++) {
tst_count = 0;
for (i = 0; i < TST_TOTAL; i++) {
if (TC[i].setupfunc != NULL)
TC[i].setupfunc();
TEST(mprotect(*(TC[i].addr), TC[i].len, TC[i].prot));
if (TEST_RETURN != -1) {
tst_resm(TFAIL, "call succeeded unexpectedly");
continue;
}
TEST_ERROR_LOG(TEST_ERRNO);
if (TEST_ERRNO == TC[i].error) {
tst_resm(TPASS, "expected failure - "
"errno = %d : %s", TEST_ERRNO,
strerror(TEST_ERRNO));
} else {
tst_resm(TFAIL, "unexpected error - %d : %s - "
"expected %d", TEST_ERRNO,
strerror(TEST_ERRNO), TC[i].error);
}
}
close(fd);
}
cleanup();
tst_exit();
}
static void setup1(void)
{
TC[0].len = getpagesize() + 1;
}
static void setup2(void)
{
addr2 = malloc(PAGESIZE);
if (addr2 == NULL)
tst_brkm(TINFO, cleanup, "malloc failed");
addr2++; /* Ensure addr2 is not page aligned */
}
static void setup3(void)
{
fd = open("/etc/passwd", O_RDONLY);
if (fd < 0)
tst_brkm(TBROK, cleanup, "open failed");
/*
* mmap the PAGESIZE bytes as read only.
*/
addr3 = mmap(0, PAGESIZE, PROT_READ, MAP_SHARED, fd, 0);
if (addr3 < 0)
tst_brkm(TBROK, cleanup, "mmap failed");
}
static void setup(void)
{
tst_sig(FORK, DEF_HANDLER, cleanup);
TEST_PAUSE;
}
static void cleanup(void)
{
TEST_CLEANUP;
}