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 * The mmap() function shall fail if: 10 * [ENOMEM] MAP_FIXED was specified, 11 * and the range [addr,addr+len) exceeds that allowed 12 * for the address space of a process; or, if MAP_FIXED was not specified and 13 * there is insufficient room in the address space to effect the mapping. 14 * 15 * Test Step: 16 * 1. Map a shared memory object, with size exceeding the value get from 17 * rlim_cur of resource RLIMIT_AS, setting MAP_FIXED; 18 * 3. Should get ENOMEM. 19 */ 20 21 #define _XOPEN_SOURCE 600 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <unistd.h> 25 #include <sys/mman.h> 26 #include <sys/types.h> 27 #include <sys/stat.h> 28 #include <sys/resource.h> 29 #include <fcntl.h> 30 #include <string.h> 31 #include <errno.h> 32 #include "posixtest.h" 33 34 int main(void) 35 { 36 char tmpfname[256]; 37 void *pa; 38 void *addr; 39 size_t len; 40 int fd; 41 42 /* Size of the shared memory object */ 43 size_t shm_size; 44 struct rlimit rlim; 45 unsigned long page_size = sysconf(_SC_PAGE_SIZE); 46 47 shm_size = 2 * page_size; 48 snprintf(tmpfname, sizeof(tmpfname), "pts_mmap_24_2_%d", getpid()); 49 50 /* Create shared object */ 51 shm_unlink(tmpfname); 52 fd = shm_open(tmpfname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); 53 if (fd == -1) { 54 printf("Error at shm_open(): %s\n", strerror(errno)); 55 return PTS_UNRESOLVED; 56 } 57 shm_unlink(tmpfname); 58 if (ftruncate(fd, shm_size) == -1) { 59 printf("Error at ftruncate(): %s\n", strerror(errno)); 60 return PTS_UNRESOLVED; 61 } 62 63 /* Currentlly total available memory of this process, in bytes */ 64 if (getrlimit(RLIMIT_AS, &rlim) == -1) { 65 printf("Error at getrlimit(): %s\n", strerror(errno)); 66 return PTS_UNRESOLVED; 67 } 68 printf("available memory: %lu\n", rlim.rlim_cur); 69 70 /* First mmap, just to get a legal addr for second mmap */ 71 len = shm_size; 72 pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 73 if (pa == MAP_FAILED) { 74 printf("Error at first mmap(): %s\n", strerror(errno)); 75 exit(PTS_FAIL); 76 } 77 78 len = rlim.rlim_cur; 79 addr = pa; 80 printf("addr: %lx, len: %lx\n", (unsigned long)addr, 81 (unsigned long)len); 82 /* Make sure addr and len is aligned to page size */ 83 if ((unsigned long)addr % page_size) { 84 /* Upper boundary */ 85 addr += page_size; 86 addr = (void *)((unsigned long)addr & ~(page_size - 1)); 87 } 88 if (len % page_size) { 89 /* Lower boundary */ 90 len &= ~(page_size - 1); 91 } 92 printf("addr: %lx, len: %lx\n", (unsigned long)addr, 93 (unsigned long)len); 94 pa = mmap(addr, len, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, fd, 95 0); 96 if (pa == MAP_FAILED && errno == ENOMEM) { 97 printf("Got ENOMEM: %s\nTest PASSED\n", strerror(errno)); 98 exit(PTS_PASS); 99 } 100 101 if (pa == MAP_FAILED) 102 perror("Error at mmap()"); 103 else 104 munmap(pa, len); 105 close(fd); 106 printf("Test Fail: Did not get ENOMEM as expected\n"); 107 return PTS_FAIL; 108 } 109