1/* 2 * Copyright (c) 2002, Intel Corporation. All rights reserved. 3 * This file is licensed under the GPL license. For the full content 4 * of this license, see the COPYING file at the top level of this 5 * source tree. 6 * 7 * MPR References within the address range starting at pa and 8 * continuing for len bytes to whole pages following the end 9 * of an object shall result in delivery of a SIGBUS signal. 10 * 11 * Test step: 12 * 1. Map a shared memory object of size 1/2 * page_size, 13 * with len = 2 * page_size 14 * 2. If Memory Protection option is supported, read the second page 15 * beyond the end of the object, should get SIGBUS. 16 */ 17 18#define _XOPEN_SOURCE 600 19 20#include <pthread.h> 21#include <stdio.h> 22#include <stdlib.h> 23#include <unistd.h> 24#include <sys/mman.h> 25#include <sys/types.h> 26#include <sys/stat.h> 27#include <sys/wait.h> 28#include <fcntl.h> 29#include <string.h> 30#include <errno.h> 31#include "posixtest.h" 32 33#define TNAME "mmap/11-3.c" 34 35 36void sigbus_handler (int signum) 37{ 38 printf("Test Pass: " TNAME " Trigger SIGBUS\n"); 39 exit(PTS_PASS); 40} 41 42int main() 43{ 44#ifndef _POSIX_MEMORY_PROTECTION 45 printf("_POSIX_MEMORY_PROTECTION is not defined\n"); 46 return PTS_UNTESTED; 47#endif 48 char tmpfname[256]; 49 long page_size; 50 long total_size; 51 52 void *pa = NULL; 53 void *addr = NULL; 54 size_t len; 55 int flag; 56 int fd; 57 off_t off = 0; 58 int prot; 59 60 char *ch = NULL; 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_%ld", 77 (long)getpid()); 78 /* Create shared object */ 79 shm_unlink(tmpfname); 80 fd = shm_open(tmpfname, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); 81 if(fd == -1) 82 { 83 printf(TNAME " Error at shm_open(): %s\n", strerror(errno)); 84 return PTS_UNRESOLVED; 85 } 86 shm_unlink(tmpfname); 87 if(ftruncate(fd, total_size) == -1) { 88 printf(TNAME " Error at ftruncate(): %s\n", strerror(errno)); 89 return PTS_UNRESOLVED; 90 } 91 92 prot = PROT_READ | PROT_WRITE; 93 flag = MAP_SHARED; 94 off = 0; 95 pa = mmap(addr, len, prot, flag, fd, off); 96 if (pa == MAP_FAILED) 97 { 98 printf("Test FAIL: " TNAME " Error at mmap(): %s\n", 99 strerror(errno)); 100 exit(PTS_FAIL); 101 } 102 103 ch = pa + page_size + 1 ; 104 105 /* This reference should trigger SIGBUS */ 106 *ch = 0; 107 108 /* wait for a while */ 109 sleep(1); 110 111 printf("Test Fail: " TNAME " Did not trigger SIGBUS, " 112 "while Memory Protection is enabled\n"); 113 exit(PTS_FAIL); 114} 115