Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:00:50

0001 // Copyright 2017 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // Functions for directly invoking mmap() via syscall, avoiding the case where
0016 // mmap() has been locally overridden.
0017 
0018 #ifndef ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
0019 #define ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
0020 
0021 #include "absl/base/config.h"
0022 
0023 #ifdef ABSL_HAVE_MMAP
0024 
0025 #include <sys/mman.h>
0026 
0027 #ifdef __linux__
0028 
0029 #include <sys/types.h>
0030 #ifdef __BIONIC__
0031 #include <sys/syscall.h>
0032 #else
0033 #include <syscall.h>
0034 #endif
0035 
0036 #include <linux/unistd.h>
0037 #include <unistd.h>
0038 #include <cerrno>
0039 #include <cstdarg>
0040 #include <cstdint>
0041 
0042 #ifdef __mips__
0043 // Include definitions of the ABI currently in use.
0044 #if defined(__BIONIC__) || !defined(__GLIBC__)
0045 // Android doesn't have sgidefs.h, but does have asm/sgidefs.h, which has the
0046 // definitions we need.
0047 #include <asm/sgidefs.h>
0048 #else
0049 #include <sgidefs.h>
0050 #endif  // __BIONIC__ || !__GLIBC__
0051 #endif  // __mips__
0052 
0053 // SYS_mmap and SYS_munmap are not defined in Android.
0054 #ifdef __BIONIC__
0055 extern "C" void* __mmap2(void*, size_t, int, int, int, size_t);
0056 #if defined(__NR_mmap) && !defined(SYS_mmap)
0057 #define SYS_mmap __NR_mmap
0058 #endif
0059 #ifndef SYS_munmap
0060 #define SYS_munmap __NR_munmap
0061 #endif
0062 #endif  // __BIONIC__
0063 
0064 #if defined(__NR_mmap2) && !defined(SYS_mmap2)
0065 #define SYS_mmap2 __NR_mmap2
0066 #endif
0067 
0068 namespace absl {
0069 ABSL_NAMESPACE_BEGIN
0070 namespace base_internal {
0071 
0072 // Platform specific logic extracted from
0073 // https://chromium.googlesource.com/linux-syscall-support/+/master/linux_syscall_support.h
0074 inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd,
0075                         off_t offset) noexcept {
0076 #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
0077     defined(__m68k__) || defined(__sh__) ||                                  \
0078     (defined(__hppa__) && !defined(__LP64__)) ||                             \
0079     (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) ||                   \
0080     (defined(__PPC__) && !defined(__PPC64__)) ||                             \
0081     (defined(__riscv) && __riscv_xlen == 32) ||                              \
0082     (defined(__s390__) && !defined(__s390x__)) ||                            \
0083     (defined(__sparc__) && !defined(__arch64__))
0084   // On these architectures, implement mmap with mmap2.
0085   static int pagesize = 0;
0086   if (pagesize == 0) {
0087 #if defined(__wasm__) || defined(__asmjs__)
0088     pagesize = getpagesize();
0089 #else
0090     pagesize = sysconf(_SC_PAGESIZE);
0091 #endif
0092   }
0093   if (offset < 0 || offset % pagesize != 0) {
0094     errno = EINVAL;
0095     return MAP_FAILED;
0096   }
0097 #ifdef __BIONIC__
0098   // SYS_mmap2 has problems on Android API level <= 16.
0099   // Workaround by invoking __mmap2() instead.
0100   return __mmap2(start, length, prot, flags, fd,
0101                  static_cast<size_t>(offset / pagesize));
0102 #else
0103   return reinterpret_cast<void*>(
0104       syscall(SYS_mmap2, start, length, prot, flags, fd,
0105               static_cast<unsigned long>(offset / pagesize)));  // NOLINT
0106 #endif
0107 #elif defined(__s390x__)
0108   // On s390x, mmap() arguments are passed in memory.
0109   unsigned long buf[6] = {reinterpret_cast<unsigned long>(start),  // NOLINT
0110                           static_cast<unsigned long>(length),      // NOLINT
0111                           static_cast<unsigned long>(prot),        // NOLINT
0112                           static_cast<unsigned long>(flags),       // NOLINT
0113                           static_cast<unsigned long>(fd),          // NOLINT
0114                           static_cast<unsigned long>(offset)};     // NOLINT
0115   return reinterpret_cast<void*>(syscall(SYS_mmap, buf));
0116 #elif defined(__x86_64__)
0117 // The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
0118 // We need to explicitly cast to an unsigned 64 bit type to avoid implicit
0119 // sign extension.  We can't cast pointers directly because those are
0120 // 32 bits, and gcc will dump ugly warnings about casting from a pointer
0121 // to an integer of a different size. We also need to make sure __off64_t
0122 // isn't truncated to 32-bits under x32.
0123 #define MMAP_SYSCALL_ARG(x) ((uint64_t)(uintptr_t)(x))
0124   return reinterpret_cast<void*>(
0125       syscall(SYS_mmap, MMAP_SYSCALL_ARG(start), MMAP_SYSCALL_ARG(length),
0126               MMAP_SYSCALL_ARG(prot), MMAP_SYSCALL_ARG(flags),
0127               MMAP_SYSCALL_ARG(fd), static_cast<uint64_t>(offset)));
0128 #undef MMAP_SYSCALL_ARG
0129 #else  // Remaining 64-bit aritectures.
0130   static_assert(sizeof(unsigned long) == 8, "Platform is not 64-bit");
0131   return reinterpret_cast<void*>(
0132       syscall(SYS_mmap, start, length, prot, flags, fd, offset));
0133 #endif
0134 }
0135 
0136 inline int DirectMunmap(void* start, size_t length) {
0137   return static_cast<int>(syscall(SYS_munmap, start, length));
0138 }
0139 
0140 }  // namespace base_internal
0141 ABSL_NAMESPACE_END
0142 }  // namespace absl
0143 
0144 #else  // !__linux__
0145 
0146 // For non-linux platforms where we have mmap, just dispatch directly to the
0147 // actual mmap()/munmap() methods.
0148 
0149 namespace absl {
0150 ABSL_NAMESPACE_BEGIN
0151 namespace base_internal {
0152 
0153 inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd,
0154                         off_t offset) {
0155   return mmap(start, length, prot, flags, fd, offset);
0156 }
0157 
0158 inline int DirectMunmap(void* start, size_t length) {
0159   return munmap(start, length);
0160 }
0161 
0162 }  // namespace base_internal
0163 ABSL_NAMESPACE_END
0164 }  // namespace absl
0165 
0166 #endif  // __linux__
0167 
0168 #endif  // ABSL_HAVE_MMAP
0169 
0170 #endif  // ABSL_BASE_INTERNAL_DIRECT_MMAP_H_