direct_mmap.h 5.7 KB

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