Coverage Report

Created: 2026-09-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/debugging/internal/address_is_readable.cc
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
// base::AddressIsReadable() probes an address to see whether it is readable,
16
// without faulting.
17
18
#include "absl/debugging/internal/address_is_readable.h"
19
20
#include <cerrno>
21
22
#include "absl/base/config.h"
23
24
#if !defined(__linux__) || defined(__ANDROID__)
25
26
namespace absl {
27
ABSL_NAMESPACE_BEGIN
28
namespace debugging_internal {
29
30
// On platforms other than Linux, just return true.
31
bool AddressIsReadable(const void* /* addr */) { return true; }
32
33
}  // namespace debugging_internal
34
ABSL_NAMESPACE_END
35
}  // namespace absl
36
37
#else  // __linux__ && !__ANDROID__
38
39
#include <stdint.h>
40
#include <syscall.h>
41
#include <unistd.h>
42
43
#include "absl/base/internal/errno_saver.h"
44
#include "absl/base/internal/raw_logging.h"
45
46
namespace absl {
47
ABSL_NAMESPACE_BEGIN
48
namespace debugging_internal {
49
50
// NOTE: be extra careful about adding any interposable function calls here
51
// (such as open(), read(), etc.). These symbols may be interposed and will get
52
// invoked in contexts they don't expect.
53
//
54
// NOTE: any new system calls here may also require sandbox reconfiguration.
55
//
56
0
bool AddressIsReadable(const void *addr) {
57
  // rt_sigprocmask below checks 8 contiguous bytes. If addr resides in the
58
  // last 7 bytes of a page (unaligned), rt_sigprocmask would additionally
59
  // check the readability of the next page, which is not desired. Align
60
  // address on 8-byte boundary to check only the current page.
61
0
  const uintptr_t u_addr = reinterpret_cast<uintptr_t>(addr) & ~uintptr_t{7};
62
0
  addr = reinterpret_cast<const void *>(u_addr);
63
64
  // rt_sigprocmask below will succeed for this input.
65
0
  if (addr == nullptr) return false;
66
67
0
  absl::base_internal::ErrnoSaver errno_saver;
68
69
  // Here we probe with some syscall which
70
  // - accepts an 8-byte region of user memory as input
71
  // - tests for EFAULT before other validation
72
  // - has no problematic side-effects
73
  //
74
  // rt_sigprocmask(2) works for this.  It copies sizeof(kernel_sigset_t)==8
75
  // bytes from the address into the kernel memory before any validation.
76
  //
77
  // The call can never succeed, since the `how` parameter is not one of
78
  // SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK.
79
  //
80
  // This strategy depends on Linux implementation details,
81
  // so we rely on the test to alert us if it stops working.
82
  //
83
  // Some discarded past approaches:
84
  // - msync() doesn't reject PROT_NONE regions
85
  // - write() on /dev/null doesn't return EFAULT
86
  // - write() on a pipe requires creating it and draining the writes
87
  // - connect() works but is problematic for sandboxes and needs a valid
88
  //   file descriptor
89
  //
90
  // This can never succeed (invalid first argument to sigprocmask).
91
0
  ABSL_RAW_CHECK(syscall(SYS_rt_sigprocmask, ~0, addr, nullptr,
92
0
                         /*sizeof(kernel_sigset_t)*/ 8) == -1,
93
0
                 "unexpected success");
94
0
  ABSL_RAW_CHECK(errno == EFAULT || errno == EINVAL, "unexpected errno");
95
0
  return errno != EFAULT;
96
0
}
97
98
}  // namespace debugging_internal
99
ABSL_NAMESPACE_END
100
}  // namespace absl
101
102
#endif  // __linux__ && !__ANDROID__