]> git.ipfire.org Git - thirdparty/glibc.git/blob - posix/tst-mmap-offset.c
Consolidate Linux mmap implementation (BZ#21270)
[thirdparty/glibc.git] / posix / tst-mmap-offset.c
1 /* BZ #18877 and #21270 mmap offset test.
2
3 Copyright (C) 2015-2017 Free Software Foundation, Inc.
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
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <sys/mman.h>
26
27 #include <support/check.h>
28
29 static int fd;
30 static long int page_shift;
31 static char fname[] = "tst-mmap-offset-XXXXXX";
32
33 static void
34 do_prepare (int argc, char **argv)
35 {
36 fd = mkstemp64 (fname);
37 if (fd < 0)
38 FAIL_EXIT1 ("mkstemp failed");
39
40 if (unlink (fname))
41 FAIL_EXIT1 ("unlink failed");
42
43 long sz = sysconf(_SC_PAGESIZE);
44 if (sz == -1)
45 sz = 4096L;
46 page_shift = ffs (sz) - 1;
47 }
48
49 #define PREPARE do_prepare
50
51
52 /* Check if negative offsets are handled correctly by mmap. */
53 static int
54 do_test_bz18877 (void)
55 {
56 const int prot = PROT_READ | PROT_WRITE;
57 const int flags = MAP_SHARED;
58 const unsigned long length = 0x10000;
59 const unsigned long offset = 0xace00000;
60 const unsigned long size = offset + length;
61 void *addr;
62
63 if (ftruncate64 (fd, size))
64 FAIL_RET ("ftruncate64 failed");
65
66 addr = mmap (NULL, length, prot, flags, fd, offset);
67 if (MAP_FAILED == addr)
68 FAIL_RET ("mmap failed");
69
70 /* This memcpy is likely to SIGBUS if mmap has messed up with offset. */
71 memcpy (addr, fname, sizeof (fname));
72
73 return 0;
74 }
75
76 /* Check if invalid offset are handled correctly by mmap. */
77 static int
78 do_test_bz21270 (void)
79 {
80 /* For architectures with sizeof (off_t) < sizeof (off64_t) mmap is
81 implemented with __SYS_mmap2 syscall and the offset is represented in
82 multiples of page size. For offset larger than
83 '1 << (page_shift + 8 * sizeof (off_t))' (that is, 1<<44 on system with
84 page size of 4096 bytes) the system call silently truncates the offset.
85 For this case glibc mmap implementation returns EINVAL. */
86 const int prot = PROT_READ | PROT_WRITE;
87 const int flags = MAP_SHARED;
88 const int64_t offset = 1ULL << (page_shift + 8 * sizeof (uint32_t));
89 const size_t length = 4096;
90
91 void *addr = mmap64 (NULL, length, prot, flags, fd, offset);
92 if (sizeof (off_t) < sizeof (off64_t))
93 {
94 if ((addr != MAP_FAILED) && (errno != EINVAL))
95 FAIL_RET ("mmap succeed");
96 }
97 else
98 {
99 if (addr == MAP_FAILED)
100 FAIL_RET ("mmap failed");
101 }
102
103 return 0;
104 }
105
106 int
107 do_test (void)
108 {
109 int ret = 0;
110
111 ret += do_test_bz18877 ();
112 ret += do_test_bz21270 ();
113
114 return ret;
115 }
116
117 #include <support/test-driver.c>