abseil-cpp/absl/base/internal/direct_mmap.h
Go to the documentation of this file.
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 {
70 namespace base_internal {
71 
72 // Platform specific logic extracted from
73 // https://chromium.googlesource.com/linux-syscall-support/+/master/linux_syscall_support.h
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(__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, offset / pagesize);
101 #else
102  return reinterpret_cast<void*>(
103  syscall(SYS_mmap2, start, length, prot, flags, fd,
104  static_cast<off_t>(offset / pagesize)));
105 #endif
106 #elif defined(__s390x__)
107  // On s390x, mmap() arguments are passed in memory.
108  unsigned long buf[6] = {reinterpret_cast<unsigned long>(start), // NOLINT
109  static_cast<unsigned long>(length), // NOLINT
110  static_cast<unsigned long>(prot), // NOLINT
111  static_cast<unsigned long>(flags), // NOLINT
112  static_cast<unsigned long>(fd), // NOLINT
113  static_cast<unsigned long>(offset)}; // NOLINT
114  return reinterpret_cast<void*>(syscall(SYS_mmap, buf));
115 #elif defined(__x86_64__)
116 // The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
117 // We need to explicitly cast to an unsigned 64 bit type to avoid implicit
118 // sign extension. We can't cast pointers directly because those are
119 // 32 bits, and gcc will dump ugly warnings about casting from a pointer
120 // to an integer of a different size. We also need to make sure __off64_t
121 // isn't truncated to 32-bits under x32.
122 #define MMAP_SYSCALL_ARG(x) ((uint64_t)(uintptr_t)(x))
123  return reinterpret_cast<void*>(
124  syscall(SYS_mmap, MMAP_SYSCALL_ARG(start), MMAP_SYSCALL_ARG(length),
125  MMAP_SYSCALL_ARG(prot), MMAP_SYSCALL_ARG(flags),
126  MMAP_SYSCALL_ARG(fd), static_cast<uint64_t>(offset)));
127 #undef MMAP_SYSCALL_ARG
128 #else // Remaining 64-bit aritectures.
129  static_assert(sizeof(unsigned long) == 8, "Platform is not 64-bit");
130  return reinterpret_cast<void*>(
131  syscall(SYS_mmap, start, length, prot, flags, fd, offset));
132 #endif
133 }
134 
135 inline int DirectMunmap(void* start, size_t length) {
136  return static_cast<int>(syscall(SYS_munmap, start, length));
137 }
138 
139 } // namespace base_internal
141 } // namespace absl
142 
143 #else // !__linux__
144 
145 // For non-linux platforms where we have mmap, just dispatch directly to the
146 // actual mmap()/munmap() methods.
147 
148 namespace absl {
150 namespace base_internal {
151 
152 inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd,
153  off_t offset) {
154  return mmap(start, length, prot, flags, fd, offset);
155 }
156 
157 inline int DirectMunmap(void* start, size_t length) {
158  return munmap(start, length);
159 }
160 
161 } // namespace base_internal
163 } // namespace absl
164 
165 #endif // __linux__
166 
167 #endif // ABSL_HAVE_MMAP
168 
169 #endif // ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
start
static uint64_t start
Definition: benchmark-pound.c:74
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
syscall
const char * syscall
Definition: third_party/libuv/src/win/internal.h:270
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
offset
voidpf uLong offset
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:142


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:16