1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Functions for directly invoking mmap() via syscall, avoiding the case where
16 // mmap() has been locally overridden.
17
18 #ifndef ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
19 #define ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
20
21 #include "absl/base/config.h"
22
23 #ifdef ABSL_HAVE_MMAP
24
25 #include <sys/mman.h>
26
27 #ifdef __linux__
28
29 #include <sys/types.h>
30 #ifdef __BIONIC__
31 #include <sys/syscall.h>
32 #else
33 #include <syscall.h>
34 #endif
35
36 #include <linux/unistd.h>
37 #include <unistd.h>
38 #include <cerrno>
39 #include <cstdarg>
40 #include <cstdint>
41
42 #ifdef __mips__
43 // Include definitions of the ABI currently in use.
44 #if defined(__BIONIC__) || !defined(__GLIBC__)
45 // Android doesn't have sgidefs.h, but does have asm/sgidefs.h, which has the
46 // definitions we need.
47 #include <asm/sgidefs.h>
48 #else
49 #include <sgidefs.h>
50 #endif // __BIONIC__ || !__GLIBC__
51 #endif // __mips__
52
53 // SYS_mmap and SYS_munmap are not defined in Android.
54 #ifdef __BIONIC__
55 extern "C" void* __mmap2(void*, size_t, int, int, int, size_t);
56 #if defined(__NR_mmap) && !defined(SYS_mmap)
57 #define SYS_mmap __NR_mmap
58 #endif
59 #ifndef SYS_munmap
60 #define SYS_munmap __NR_munmap
61 #endif
62 #endif // __BIONIC__
63
64 #if defined(__NR_mmap2) && !defined(SYS_mmap2)
65 #define SYS_mmap2 __NR_mmap2
66 #endif
67
68 namespace absl {
69 ABSL_NAMESPACE_BEGIN
70 namespace base_internal {
71
72 // Platform specific logic extracted from
73 // https://chromium.googlesource.com/linux-syscall-support/+/master/linux_syscall_support.h
DirectMmap(void * start,size_t length,int prot,int flags,int fd,off_t offset)74 inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd,
75 off_t offset) noexcept {
76 #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
77 defined(__m68k__) || defined(__sh__) || \
78 (defined(__hppa__) && !defined(__LP64__)) || \
79 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
80 (defined(__PPC__) && !defined(__PPC64__)) || \
81 (defined(__riscv) && __riscv_xlen == 32) || \
82 (defined(__s390__) && !defined(__s390x__)) || \
83 (defined(__sparc__) && !defined(__arch64__))
84 // On these architectures, implement mmap with mmap2.
85 static int pagesize = 0;
86 if (pagesize == 0) {
87 #if defined(__wasm__) || defined(__asmjs__)
88 pagesize = getpagesize();
89 #else
90 pagesize = sysconf(_SC_PAGESIZE);
91 #endif
92 }
93 if (offset < 0 || offset % pagesize != 0) {
94 errno = EINVAL;
95 return MAP_FAILED;
96 }
97 #ifdef __BIONIC__
98 // SYS_mmap2 has problems on Android API level <= 16.
99 // Workaround by invoking __mmap2() instead.
100 return __mmap2(start, length, prot, flags, fd,
101 static_cast<size_t>(offset / pagesize));
102 #else
103 return reinterpret_cast<void*>(
104 syscall(SYS_mmap2, start, length, prot, flags, fd,
105 static_cast<unsigned long>(offset / pagesize))); // NOLINT
106 #endif
107 #elif defined(__s390x__)
108 // On s390x, mmap() arguments are passed in memory.
109 unsigned long buf[6] = {reinterpret_cast<unsigned long>(start), // NOLINT
110 static_cast<unsigned long>(length), // NOLINT
111 static_cast<unsigned long>(prot), // NOLINT
112 static_cast<unsigned long>(flags), // NOLINT
113 static_cast<unsigned long>(fd), // NOLINT
114 static_cast<unsigned long>(offset)}; // NOLINT
115 return reinterpret_cast<void*>(syscall(SYS_mmap, buf));
116 #elif defined(__x86_64__)
117 // The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
118 // We need to explicitly cast to an unsigned 64 bit type to avoid implicit
119 // sign extension. We can't cast pointers directly because those are
120 // 32 bits, and gcc will dump ugly warnings about casting from a pointer
121 // to an integer of a different size. We also need to make sure __off64_t
122 // isn't truncated to 32-bits under x32.
123 #define MMAP_SYSCALL_ARG(x) ((uint64_t)(uintptr_t)(x))
124 return reinterpret_cast<void*>(
125 syscall(SYS_mmap, MMAP_SYSCALL_ARG(start), MMAP_SYSCALL_ARG(length),
126 MMAP_SYSCALL_ARG(prot), MMAP_SYSCALL_ARG(flags),
127 MMAP_SYSCALL_ARG(fd), static_cast<uint64_t>(offset)));
128 #undef MMAP_SYSCALL_ARG
129 #else // Remaining 64-bit aritectures.
130 static_assert(sizeof(unsigned long) == 8, "Platform is not 64-bit");
131 return reinterpret_cast<void*>(
132 syscall(SYS_mmap, start, length, prot, flags, fd, offset));
133 #endif
134 }
135
DirectMunmap(void * start,size_t length)136 inline int DirectMunmap(void* start, size_t length) {
137 return static_cast<int>(syscall(SYS_munmap, start, length));
138 }
139
140 } // namespace base_internal
141 ABSL_NAMESPACE_END
142 } // namespace absl
143
144 #else // !__linux__
145
146 // For non-linux platforms where we have mmap, just dispatch directly to the
147 // actual mmap()/munmap() methods.
148
149 namespace absl {
150 ABSL_NAMESPACE_BEGIN
151 namespace base_internal {
152
DirectMmap(void * start,size_t length,int prot,int flags,int fd,off_t offset)153 inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd,
154 off_t offset) {
155 return mmap(start, length, prot, flags, fd, offset);
156 }
157
DirectMunmap(void * start,size_t length)158 inline int DirectMunmap(void* start, size_t length) {
159 return munmap(start, length);
160 }
161
162 } // namespace base_internal
163 ABSL_NAMESPACE_END
164 } // namespace absl
165
166 #endif // __linux__
167
168 #endif // ABSL_HAVE_MMAP
169
170 #endif // ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
171