]>
Commit | Line | Data |
---|---|---|
158d5fa0 | 1 | /* BZ #18877 and #21270 mmap offset test. |
d3573f61 | 2 | |
04277e02 | 3 | Copyright (C) 2015-2019 Free Software Foundation, Inc. |
d3573f61 AZ |
4 | This file is part of the GNU C Library. |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
17 | License along with the GNU C Library; if not, see | |
18 | <http://www.gnu.org/licenses/>. */ | |
19 | ||
25cfd502 | 20 | #include <stdint.h> |
d3573f61 AZ |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
158d5fa0 | 25 | #include <errno.h> |
d3573f61 AZ |
26 | #include <sys/mman.h> |
27 | ||
158d5fa0 AZ |
28 | #include <support/check.h> |
29 | ||
30 | static int fd; | |
31 | static long int page_shift; | |
95511aab | 32 | static char fname[] = "/tmp/tst-mmap-offset-XXXXXX"; |
158d5fa0 AZ |
33 | |
34 | static void | |
35 | do_prepare (int argc, char **argv) | |
d3573f61 | 36 | { |
158d5fa0 AZ |
37 | fd = mkstemp64 (fname); |
38 | if (fd < 0) | |
39 | FAIL_EXIT1 ("mkstemp failed"); | |
40 | ||
41 | if (unlink (fname)) | |
42 | FAIL_EXIT1 ("unlink failed"); | |
43 | ||
44 | long sz = sysconf(_SC_PAGESIZE); | |
45 | if (sz == -1) | |
46 | sz = 4096L; | |
47 | page_shift = ffs (sz) - 1; | |
d3573f61 AZ |
48 | } |
49 | ||
158d5fa0 AZ |
50 | #define PREPARE do_prepare |
51 | ||
52 | ||
d3573f61 AZ |
53 | /* Check if negative offsets are handled correctly by mmap. */ |
54 | static int | |
158d5fa0 | 55 | do_test_bz18877 (void) |
d3573f61 AZ |
56 | { |
57 | const int prot = PROT_READ | PROT_WRITE; | |
58 | const int flags = MAP_SHARED; | |
59 | const unsigned long length = 0x10000; | |
60 | const unsigned long offset = 0xace00000; | |
61 | const unsigned long size = offset + length; | |
62 | void *addr; | |
d3573f61 AZ |
63 | |
64 | if (ftruncate64 (fd, size)) | |
158d5fa0 | 65 | FAIL_RET ("ftruncate64 failed"); |
d3573f61 AZ |
66 | |
67 | addr = mmap (NULL, length, prot, flags, fd, offset); | |
68 | if (MAP_FAILED == addr) | |
158d5fa0 | 69 | FAIL_RET ("mmap failed"); |
d3573f61 AZ |
70 | |
71 | /* This memcpy is likely to SIGBUS if mmap has messed up with offset. */ | |
72 | memcpy (addr, fname, sizeof (fname)); | |
73 | ||
74 | return 0; | |
75 | } | |
76 | ||
158d5fa0 AZ |
77 | /* Check if invalid offset are handled correctly by mmap. */ |
78 | static int | |
79 | do_test_bz21270 (void) | |
80 | { | |
81 | /* For architectures with sizeof (off_t) < sizeof (off64_t) mmap is | |
82 | implemented with __SYS_mmap2 syscall and the offset is represented in | |
83 | multiples of page size. For offset larger than | |
84 | '1 << (page_shift + 8 * sizeof (off_t))' (that is, 1<<44 on system with | |
85 | page size of 4096 bytes) the system call silently truncates the offset. | |
86 | For this case glibc mmap implementation returns EINVAL. */ | |
87 | const int prot = PROT_READ | PROT_WRITE; | |
88 | const int flags = MAP_SHARED; | |
89 | const int64_t offset = 1ULL << (page_shift + 8 * sizeof (uint32_t)); | |
90 | const size_t length = 4096; | |
91 | ||
92 | void *addr = mmap64 (NULL, length, prot, flags, fd, offset); | |
93 | if (sizeof (off_t) < sizeof (off64_t)) | |
94 | { | |
95 | if ((addr != MAP_FAILED) && (errno != EINVAL)) | |
96 | FAIL_RET ("mmap succeed"); | |
97 | } | |
98 | else | |
99 | { | |
100 | if (addr == MAP_FAILED) | |
101 | FAIL_RET ("mmap failed"); | |
102 | } | |
103 | ||
104 | return 0; | |
105 | } | |
106 | ||
107 | int | |
108 | do_test (void) | |
109 | { | |
110 | int ret = 0; | |
111 | ||
112 | ret += do_test_bz18877 (); | |
113 | ret += do_test_bz21270 (); | |
114 | ||
115 | return ret; | |
116 | } | |
117 | ||
118 | #include <support/test-driver.c> |