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
22 /* Turn on large file support, has no effect on 64 bit archs */
23 #define _FILE_OFFSET_BITS 64
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <limits.h>
29 #include <sys/mman.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/wait.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <sys/utsname.h>
37
38 #include "posixtest.h"
39 #include "tempfile.h"
40
main(void)41 int main(void)
42 {
43 char tmpfname[PATH_MAX];
44
45 void *pa;
46 size_t len;
47 int fd;
48 off_t off = 0;
49
50 /* check for 64 bit arch */
51 if (sizeof(void *) == 8) {
52 printf("UNSUPPORTED: Cannot be tested on 64 bit architecture\n");
53 return PTS_UNSUPPORTED;
54 }
55
56 /* The overflow does not happen when 32bit binary runs on 64bit kernel */
57 #ifdef __linux__
58 struct utsname buf;
59
60 if (!uname(&buf) && strstr(buf.machine, "64")) {
61 printf("UNSUPPORTED: Looks like we run on 64bit kernel (%s)\n",
62 buf.machine);
63 return PTS_UNSUPPORTED;
64 }
65
66 #endif /* __linux__ */
67
68 long page_size = sysconf(_SC_PAGE_SIZE);
69
70 PTS_GET_TMP_FILENAME(tmpfname, "pts_mmap_31_1");
71 unlink(tmpfname);
72 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
73 if (fd == -1) {
74 printf("Error at open(): %s\n", strerror(errno));
75 return PTS_UNRESOLVED;
76 }
77 unlink(tmpfname);
78
79 /* Set lenght to maximal multiple of page size */
80 len = ~((size_t) 0) & (~(page_size - 1));
81
82 /*
83 * Now we need offset that fits into 32 bit
84 * value when divided by page size but is big
85 * enough so that offset + PAGE_ALIGN(len) / page_size
86 * overflows 32 bits.
87 */
88 off = ((off_t) ~ ((size_t) 0)) * page_size;
89 off &= ~(page_size - 1);
90
91 printf("off: %llx, len: %llx\n", (unsigned long long)off,
92 (unsigned long long)len);
93
94 pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, off);
95 if (pa == MAP_FAILED && errno == EOVERFLOW) {
96 printf("Got EOVERFLOW\n");
97 printf("Test PASSED\n");
98 return PTS_PASS;
99 }
100
101 if (pa == MAP_FAILED)
102 perror("Test FAILED: expect EOVERFLOW but get other error");
103 else
104 printf("Test FAILED: Expect EOVERFLOW but got no error\n");
105
106 close(fd);
107 munmap(pa, len);
108 return PTS_FAIL;
109 }
110