Home | History | Annotate | Download | only in mmap
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Copyright (c) 2012, Cyril Hrubis <chrubis (at) suse.cz>
      4  *
      5  * This file is licensed under the GPL license.  For the full content
      6  * of this license, see the COPYING file at the top level of this
      7  * source tree.
      8  *
      9  * MPR References within the address range starting at pa and
     10  * continuing for len bytes to whole pages following the end
     11  * of an object shall result in delivery of a SIGBUS signal.
     12  *
     13  * Test Steps:
     14  * 1. Map a shared memory object of size 1/2 * page_size,
     15  *    with len = 2 * page_size
     16  * 2. If Memory Protection option is supported, read the second page
     17  *    beyond the end of the object, should get SIGBUS.
     18  */
     19 
     20 #define _XOPEN_SOURCE 600
     21 
     22 #include <sys/mman.h>
     23 #include <sys/types.h>
     24 #include <sys/stat.h>
     25 #include <sys/wait.h>
     26 #include <errno.h>
     27 #include <fcntl.h>
     28 #include <signal.h>
     29 #include <stdio.h>
     30 #include <stdlib.h>
     31 #include <string.h>
     32 #include <unistd.h>
     33 #include "posixtest.h"
     34 
     35 #define WRITE(str) write(STDOUT_FILENO, str, sizeof(str) - 1)
     36 
     37 void sigbus_handler(int signum)
     38 {
     39 	if (signum == SIGBUS) {
     40 		WRITE("SIGBUS triggered\n");
     41 		WRITE("Test PASSED\n");
     42 		_exit(PTS_PASS);
     43 	}
     44 }
     45 
     46 int main(void)
     47 {
     48 #ifndef _POSIX_MEMORY_PROTECTION
     49 	printf("_POSIX_MEMORY_PROTECTION is not defined\n");
     50 	return PTS_UNTESTED;
     51 #endif
     52 	char tmpfname[256];
     53 	long page_size;
     54 	long total_size;
     55 
     56 	void *pa;
     57 	size_t len;
     58 	int fd;
     59 
     60 	char *ch;
     61 
     62 	struct sigaction sa;
     63 
     64 	page_size = sysconf(_SC_PAGE_SIZE);
     65 
     66 	/* Size of the shared memory object to be mapped */
     67 	total_size = page_size / 2;
     68 
     69 	/* mmap will create a partial page */
     70 	len = page_size * 2;
     71 
     72 	sigfillset(&sa.sa_mask);
     73 	sa.sa_handler = sigbus_handler;
     74 	sigaction(SIGBUS, &sa, NULL);
     75 
     76 	snprintf(tmpfname, sizeof(tmpfname), "/pts_mmap_11_3_%d", getpid());
     77 	/* Create shared object */
     78 	shm_unlink(tmpfname);
     79 	fd = shm_open(tmpfname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
     80 	if (fd == -1) {
     81 		printf("Error at shm_open(): %s\n", strerror(errno));
     82 		return PTS_UNRESOLVED;
     83 	}
     84 	shm_unlink(tmpfname);
     85 	if (ftruncate(fd, total_size) == -1) {
     86 		printf("Error at ftruncate(): %s\n", strerror(errno));
     87 		return PTS_UNRESOLVED;
     88 	}
     89 
     90 	pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
     91 	if (pa == MAP_FAILED) {
     92 		printf("Error at mmap(): %s\n", strerror(errno));
     93 		return PTS_FAIL;
     94 	}
     95 
     96 	ch = pa + page_size + 1;
     97 
     98 	/* This reference should trigger SIGBUS */
     99 	*ch = 0;
    100 
    101 	/* wait for a while */
    102 	sleep(1);
    103 
    104 	printf("Test FAILED: SIGBUS not triggered, "
    105 	       "while Memory Protection is enabled\n");
    106 	return PTS_FAIL;
    107 }
    108