Coverage Report

Created: 2026-09-14 06:45

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