• 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  * [EOVERFLOW] The file is a regular file and the value of off
11  * plus len exceeds the offset maximum established in the open
12  * file description associated with fildes.
13  *
14  * Note: This error condition came to the standard with large
15  *       file extension and cannot be triggered without it.
16  *
17  *       So in order to trigger this we need 32 bit architecture
18  *       and largefile support turned on.
19  */
20 
21 #define _XOPEN_SOURCE 600
22 
23 /* Turn on large file support, has no effect on 64 bit archs */
24 #define _FILE_OFFSET_BITS 64
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <limits.h>
30 #include <sys/mman.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/wait.h>
34 #include <fcntl.h>
35 #include <string.h>
36 #include <errno.h>
37 #include "posixtest.h"
38 
main(void)39 int main(void)
40 {
41 	char tmpfname[256];
42 
43 	void *pa;
44 	size_t len;
45 	int fd;
46 	off_t off = 0;
47 
48 	/* check for 64 bit arch */
49 	if (sizeof(void *) == 8) {
50 		printf("USUPPORTED: Cannot be tested on 64 bit architecture\n");
51 		return PTS_UNSUPPORTED;
52 	}
53 
54 	long page_size = sysconf(_SC_PAGE_SIZE);
55 
56 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_31_1_%d", getpid());
57 	unlink(tmpfname);
58 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
59 	if (fd == -1) {
60 		printf("Error at open(): %s\n", strerror(errno));
61 		return PTS_UNRESOLVED;
62 	}
63 	unlink(tmpfname);
64 
65 	/* Set lenght to maximal multiple of page size */
66 	len = ~((size_t) 0) & (~(page_size - 1));
67 
68 	/*
69 	 * Now we need offset that fits into 32 bit
70 	 * value when divided by page size but is big
71 	 * enough so that offset + PAGE_ALIGN(len) / page_size
72 	 * overflows 32 bits.
73 	 */
74 	off = ((off_t) ~ ((size_t) 0)) * page_size;
75 	off &= ~(page_size - 1);
76 
77 	printf("off: %llx, len: %llx\n", (unsigned long long)off,
78 	       (unsigned long long)len);
79 
80 	pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, off);
81 	if (pa == MAP_FAILED && errno == EOVERFLOW) {
82 		printf("Got EOVERFLOW\n");
83 		printf("Test PASSED\n");
84 		return PTS_PASS;
85 	}
86 
87 	if (pa == MAP_FAILED)
88 		perror("Test FAILED: expect EOVERFLOW but get other error");
89 	else
90 		printf("Test FAILED: Expect EOVERFLOW but got no error\n");
91 
92 	close(fd);
93 	munmap(pa, len);
94 	return PTS_FAIL;
95 }
96