• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 <stdint.h>
32 #include "posixtest.h"
33 
main(void)34 int main(void)
35 {
36 	char tmpfname[256];
37 	void *pa;
38 	size_t len;
39 	int fd;
40 
41 	/* Size of the shared memory object */
42 	size_t shm_size = 1024;
43 
44 	size_t mapped_size = 0;
45 
46 	snprintf(tmpfname, sizeof(tmpfname), "pts_mmap_25_1_%d", getpid());
47 
48 	/* Create shared object */
49 	shm_unlink(tmpfname);
50 	fd = shm_open(tmpfname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
51 	if (fd == -1) {
52 		printf("Error at shm_open(): %s\n", strerror(errno));
53 		return PTS_UNRESOLVED;
54 	}
55 	shm_unlink(tmpfname);
56 
57 	if (ftruncate(fd, shm_size) == -1) {
58 		printf("Error at ftruncate(): %s\n", strerror(errno));
59 		return PTS_UNRESOLVED;
60 	}
61 
62 	len = shm_size;
63 
64 	mapped_size = 0;
65 	while (mapped_size < SIZE_MAX) {
66 		pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
67 		if (pa == MAP_FAILED && errno == ENOMEM) {
68 			printf("Total mapped size is %lu bytes\n",
69 			       (unsigned long)mapped_size);
70 			printf("Test PASSED\n");
71 			return PTS_PASS;
72 		}
73 
74 		mapped_size += shm_size;
75 		if (pa == MAP_FAILED)
76 			perror("Error at mmap()");
77 	}
78 
79 	close(fd);
80 	printf("Test FAILED: Did not get ENOMEM as expected\n");
81 	return PTS_FAIL;
82 }
83