1 /*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * Copyright (c) 2012, Cyril Hrubis <chrubis@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 #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/resource.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <errno.h>
31 #include "posixtest.h"
32
main(void)33 int main(void)
34 {
35 char tmpfname[256];
36 void *pa;
37 void *addr;
38 size_t len;
39 int fd;
40
41 /* Size of the shared memory object */
42 size_t shm_size;
43 struct rlimit rlim;
44 unsigned long page_size = sysconf(_SC_PAGE_SIZE);
45
46 shm_size = 2 * page_size;
47 snprintf(tmpfname, sizeof(tmpfname), "pts_mmap_24_2_%d", getpid());
48
49 /* Create shared object */
50 shm_unlink(tmpfname);
51 fd = shm_open(tmpfname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
52 if (fd == -1) {
53 printf("Error at shm_open(): %s\n", strerror(errno));
54 return PTS_UNRESOLVED;
55 }
56 shm_unlink(tmpfname);
57 if (ftruncate(fd, shm_size) == -1) {
58 printf("Error at ftruncate(): %s\n", strerror(errno));
59 return PTS_UNRESOLVED;
60 }
61
62 /* Currentlly total available memory of this process, in bytes */
63 if (getrlimit(RLIMIT_AS, &rlim) == -1) {
64 printf("Error at getrlimit(): %s\n", strerror(errno));
65 return PTS_UNRESOLVED;
66 }
67 printf("available memory: %lu\n", rlim.rlim_cur);
68
69 /* First mmap, just to get a legal addr for second mmap */
70 len = shm_size;
71 pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
72 if (pa == MAP_FAILED) {
73 printf("Error at first mmap(): %s\n", strerror(errno));
74 exit(PTS_FAIL);
75 }
76
77 len = rlim.rlim_cur;
78 addr = pa;
79 printf("addr: %lx, len: %lx\n", (unsigned long)addr,
80 (unsigned long)len);
81 /* Make sure addr and len is aligned to page size */
82 if ((unsigned long)addr % page_size) {
83 /* Upper boundary */
84 addr += page_size;
85 addr = (void *)((unsigned long)addr & ~(page_size - 1));
86 }
87 if (len % page_size) {
88 /* Lower boundary */
89 len &= ~(page_size - 1);
90 }
91 printf("addr: %lx, len: %lx\n", (unsigned long)addr,
92 (unsigned long)len);
93 pa = mmap(addr, len, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, fd,
94 0);
95 if (pa == MAP_FAILED && errno == ENOMEM) {
96 printf("Got ENOMEM: %s\nTest PASSED\n", strerror(errno));
97 exit(PTS_PASS);
98 }
99
100 if (pa == MAP_FAILED)
101 perror("Error at mmap()");
102 else
103 munmap(pa, len);
104 close(fd);
105 printf("Test Fail: Did not get ENOMEM as expected\n");
106 return PTS_FAIL;
107 }
108