You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

direct_mmap.h 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #ifndef ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
  18. #define ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
  19. #include "absl/base/config.h"
  20. #ifdef ABSL_HAVE_MMAP
  21. #include <sys/mman.h>
  22. #ifdef __linux__
  23. #include <sys/types.h>
  24. #ifdef __BIONIC__
  25. #include <sys/syscall.h>
  26. #else
  27. #include <syscall.h>
  28. #endif
  29. #include <linux/unistd.h>
  30. #include <unistd.h>
  31. #include <cerrno>
  32. #include <cstdarg>
  33. #include <cstdint>
  34. #ifdef __mips__
  35. // Include definitions of the ABI currently in use.
  36. #if defined(__BIONIC__) || !defined(__GLIBC__)
  37. // Android doesn't have sgidefs.h, but does have asm/sgidefs.h, which has the
  38. // definitions we need.
  39. #include <asm/sgidefs.h>
  40. #else
  41. #include <sgidefs.h>
  42. #endif // __BIONIC__ || !__GLIBC__
  43. #endif // __mips__
  44. // SYS_mmap and SYS_munmap are not defined in Android.
  45. #ifdef __BIONIC__
  46. extern "C" void* __mmap2(void*, size_t, int, int, int, size_t);
  47. #if defined(__NR_mmap) && !defined(SYS_mmap)
  48. #define SYS_mmap __NR_mmap
  49. #endif
  50. #ifndef SYS_munmap
  51. #define SYS_munmap __NR_munmap
  52. #endif
  53. #endif // __BIONIC__
  54. #if defined(__NR_mmap2) && !defined(SYS_mmap2)
  55. #define SYS_mmap2 __NR_mmap2
  56. #endif
  57. namespace absl
  58. {
  59. ABSL_NAMESPACE_BEGIN
  60. namespace base_internal
  61. {
  62. // Platform specific logic extracted from
  63. // https://chromium.googlesource.com/linux-syscall-support/+/master/linux_syscall_support.h
  64. inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd, off64_t offset) noexcept
  65. {
  66. #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
  67. defined(__m68k__) || defined(__sh__) || \
  68. (defined(__hppa__) && !defined(__LP64__)) || \
  69. (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
  70. (defined(__PPC__) && !defined(__PPC64__)) || \
  71. (defined(__riscv) && __riscv_xlen == 32) || \
  72. (defined(__s390__) && !defined(__s390x__)) || \
  73. (defined(__sparc__) && !defined(__arch64__))
  74. // On these architectures, implement mmap with mmap2.
  75. static int pagesize = 0;
  76. if (pagesize == 0)
  77. {
  78. #if defined(__wasm__) || defined(__asmjs__)
  79. pagesize = getpagesize();
  80. #else
  81. pagesize = sysconf(_SC_PAGESIZE);
  82. #endif
  83. }
  84. if (offset < 0 || offset % pagesize != 0)
  85. {
  86. errno = EINVAL;
  87. return MAP_FAILED;
  88. }
  89. #ifdef __BIONIC__
  90. // SYS_mmap2 has problems on Android API level <= 16.
  91. // Workaround by invoking __mmap2() instead.
  92. return __mmap2(start, length, prot, flags, fd, offset / pagesize);
  93. #else
  94. return reinterpret_cast<void*>(
  95. syscall(SYS_mmap2, start, length, prot, flags, fd, static_cast<off_t>(offset / pagesize))
  96. );
  97. #endif
  98. #elif defined(__s390x__)
  99. // On s390x, mmap() arguments are passed in memory.
  100. unsigned long buf[6] = {reinterpret_cast<unsigned long>(start), // NOLINT
  101. static_cast<unsigned long>(length), // NOLINT
  102. static_cast<unsigned long>(prot), // NOLINT
  103. static_cast<unsigned long>(flags), // NOLINT
  104. static_cast<unsigned long>(fd), // NOLINT
  105. static_cast<unsigned long>(offset)}; // NOLINT
  106. return reinterpret_cast<void*>(syscall(SYS_mmap, buf));
  107. #elif defined(__x86_64__)
  108. // The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
  109. // We need to explicitly cast to an unsigned 64 bit type to avoid implicit
  110. // sign extension. We can't cast pointers directly because those are
  111. // 32 bits, and gcc will dump ugly warnings about casting from a pointer
  112. // to an integer of a different size. We also need to make sure __off64_t
  113. // isn't truncated to 32-bits under x32.
  114. #define MMAP_SYSCALL_ARG(x) ((uint64_t)(uintptr_t)(x))
  115. return reinterpret_cast<void*>(
  116. syscall(SYS_mmap, MMAP_SYSCALL_ARG(start), MMAP_SYSCALL_ARG(length), MMAP_SYSCALL_ARG(prot), MMAP_SYSCALL_ARG(flags), MMAP_SYSCALL_ARG(fd), static_cast<uint64_t>(offset))
  117. );
  118. #undef MMAP_SYSCALL_ARG
  119. #else // Remaining 64-bit aritectures.
  120. static_assert(sizeof(unsigned long) == 8, "Platform is not 64-bit");
  121. return reinterpret_cast<void*>(
  122. syscall(SYS_mmap, start, length, prot, flags, fd, offset)
  123. );
  124. #endif
  125. }
  126. inline int DirectMunmap(void* start, size_t length)
  127. {
  128. return static_cast<int>(syscall(SYS_munmap, start, length));
  129. }
  130. } // namespace base_internal
  131. ABSL_NAMESPACE_END
  132. } // namespace absl
  133. #else // !__linux__
  134. // For non-linux platforms where we have mmap, just dispatch directly to the
  135. // actual mmap()/munmap() methods.
  136. namespace absl
  137. {
  138. ABSL_NAMESPACE_BEGIN
  139. namespace base_internal
  140. {
  141. inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd, off_t offset)
  142. {
  143. return mmap(start, length, prot, flags, fd, offset);
  144. }
  145. inline int DirectMunmap(void* start, size_t length)
  146. {
  147. return munmap(start, length);
  148. }
  149. } // namespace base_internal
  150. ABSL_NAMESPACE_END
  151. } // namespace absl
  152. #endif // __linux__
  153. #endif // ABSL_HAVE_MMAP
  154. #endif // ABSL_BASE_INTERNAL_DIRECT_MMAP_H_