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 #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 /* The overflow does not happen when 32bit binary runs on 64bit kernel */
55 #ifdef __linux__
56 struct utsname buf;
57
58 if (!uname(&buf) && strstr(buf.machine, "64")) {
59 printf("UNSUPPORTED: Looks like we run on 64bit kernel (%s)\n",
60 buf.machine);
61 return PTS_UNSUPPORTED;
62 }
63
64 #endif /* __linux__ */
65
66 long page_size = sysconf(_SC_PAGE_SIZE);
67
68 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_31_1_%d", getpid());
69 unlink(tmpfname);
70 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
71 if (fd == -1) {
72 printf("Error at open(): %s\n", strerror(errno));
73 return PTS_UNRESOLVED;
74 }
75 unlink(tmpfname);
76
77 /* Set lenght to maximal multiple of page size */
78 len = ~((size_t) 0) & (~(page_size - 1));
79
80 /*
81 * Now we need offset that fits into 32 bit
82 * value when divided by page size but is big
83 * enough so that offset + PAGE_ALIGN(len) / page_size
84 * overflows 32 bits.
85 */
86 off = ((off_t) ~ ((size_t) 0)) * page_size;
87 off &= ~(page_size - 1);
88
89 printf("off: %llx, len: %llx\n", (unsigned long long)off,
90 (unsigned long long)len);
91
92 pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, off);
93 if (pa == MAP_FAILED && errno == EOVERFLOW) {
94 printf("Got EOVERFLOW\n");
95 printf("Test PASSED\n");
96 return PTS_PASS;
97 }
98
99 if (pa == MAP_FAILED)
100 perror("Test FAILED: expect EOVERFLOW but get other error");
101 else
102 printf("Test FAILED: Expect EOVERFLOW but got no error\n");
103
104 close(fd);
105 munmap(pa, len);
106 return PTS_FAIL;
107 }
108