• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if 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 #ifdef __BIONIC__
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__
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,off64_t offset)74 inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd,
75                         off64_t offset) noexcept {
76 #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
77     (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) ||                   \
78     (defined(__PPC__) && !defined(__PPC64__)) ||                             \
79     (defined(__riscv) && __riscv_xlen == 32) ||                              \
80     (defined(__s390__) && !defined(__s390x__))
81   // On these architectures, implement mmap with mmap2.
82   static int pagesize = 0;
83   if (pagesize == 0) {
84 #if defined(__wasm__) || defined(__asmjs__)
85     pagesize = getpagesize();
86 #else
87     pagesize = sysconf(_SC_PAGESIZE);
88 #endif
89   }
90   if (offset < 0 || offset % pagesize != 0) {
91     errno = EINVAL;
92     return MAP_FAILED;
93   }
94 #ifdef __BIONIC__
95   // SYS_mmap2 has problems on Android API level <= 16.
96   // Workaround by invoking __mmap2() instead.
97   return __mmap2(start, length, prot, flags, fd, offset / pagesize);
98 #else
99   return reinterpret_cast<void*>(
100       syscall(SYS_mmap2, start, length, prot, flags, fd,
101               static_cast<off_t>(offset / pagesize)));
102 #endif
103 #elif defined(__s390x__)
104   // On s390x, mmap() arguments are passed in memory.
105   unsigned long buf[6] = {reinterpret_cast<unsigned long>(start),  // NOLINT
106                           static_cast<unsigned long>(length),      // NOLINT
107                           static_cast<unsigned long>(prot),        // NOLINT
108                           static_cast<unsigned long>(flags),       // NOLINT
109                           static_cast<unsigned long>(fd),          // NOLINT
110                           static_cast<unsigned long>(offset)};     // NOLINT
111   return reinterpret_cast<void*>(syscall(SYS_mmap, buf));
112 #elif defined(__x86_64__)
113 // The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
114 // We need to explicitly cast to an unsigned 64 bit type to avoid implicit
115 // sign extension.  We can't cast pointers directly because those are
116 // 32 bits, and gcc will dump ugly warnings about casting from a pointer
117 // to an integer of a different size. We also need to make sure __off64_t
118 // isn't truncated to 32-bits under x32.
119 #define MMAP_SYSCALL_ARG(x) ((uint64_t)(uintptr_t)(x))
120   return reinterpret_cast<void*>(
121       syscall(SYS_mmap, MMAP_SYSCALL_ARG(start), MMAP_SYSCALL_ARG(length),
122               MMAP_SYSCALL_ARG(prot), MMAP_SYSCALL_ARG(flags),
123               MMAP_SYSCALL_ARG(fd), static_cast<uint64_t>(offset)));
124 #undef MMAP_SYSCALL_ARG
125 #else  // Remaining 64-bit aritectures.
126   static_assert(sizeof(unsigned long) == 8, "Platform is not 64-bit");
127   return reinterpret_cast<void*>(
128       syscall(SYS_mmap, start, length, prot, flags, fd, offset));
129 #endif
130 }
131 
DirectMunmap(void * start,size_t length)132 inline int DirectMunmap(void* start, size_t length) {
133   return static_cast<int>(syscall(SYS_munmap, start, length));
134 }
135 
136 }  // namespace base_internal
137 ABSL_NAMESPACE_END
138 }  // namespace absl
139 
140 #else  // !__linux__
141 
142 // For non-linux platforms where we have mmap, just dispatch directly to the
143 // actual mmap()/munmap() methods.
144 
145 namespace absl {
146 ABSL_NAMESPACE_BEGIN
147 namespace base_internal {
148 
DirectMmap(void * start,size_t length,int prot,int flags,int fd,off_t offset)149 inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd,
150                         off_t offset) {
151   return mmap(start, length, prot, flags, fd, offset);
152 }
153 
DirectMunmap(void * start,size_t length)154 inline int DirectMunmap(void* start, size_t length) {
155   return munmap(start, length);
156 }
157 
158 }  // namespace base_internal
159 ABSL_NAMESPACE_END
160 }  // namespace absl
161 
162 #endif  // __linux__
163 
164 #endif  // ABSL_HAVE_MMAP
165 
166 #endif  // ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
167