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, and the range [addr,addr+len)
11 * exceeds that allowed for the address space of a process;
12 * or, if MAP_FIXED was not specified and
13 * there is insufficient room in the address space to effect the mapping.
14 *
15 * Test Steps:
16 * 1. In a very long loop, keep mapping a shared memory object,
17 * until there this insufficient room in the address space;
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 <stdint.h>
33 #include "posixtest.h"
34
main(void)35 int main(void)
36 {
37 char tmpfname[256];
38 void *pa;
39 size_t len;
40 int fd;
41
42 /* Size of the shared memory object */
43 size_t shm_size = 1024;
44
45 size_t mapped_size = 0;
46
47 snprintf(tmpfname, sizeof(tmpfname), "pts_mmap_25_1_%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
58 if (ftruncate(fd, shm_size) == -1) {
59 printf("Error at ftruncate(): %s\n", strerror(errno));
60 return PTS_UNRESOLVED;
61 }
62
63 len = shm_size;
64
65 mapped_size = 0;
66 while (mapped_size < SIZE_MAX) {
67 pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
68 if (pa == MAP_FAILED && errno == ENOMEM) {
69 printf("Total mapped size is %lu bytes\n",
70 (unsigned long)mapped_size);
71 printf("Test PASSED\n");
72 return PTS_PASS;
73 }
74
75 mapped_size += shm_size;
76 if (pa == MAP_FAILED)
77 perror("Error at mmap()");
78 }
79
80 close(fd);
81 printf("Test FAILED: Did not get ENOMEM as expected\n");
82 return PTS_FAIL;
83 }
84