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 #if !defined(__linux__) || defined(__ANDROID__)
21
22 namespace absl {
23 ABSL_NAMESPACE_BEGIN
24 namespace debugging_internal {
25
26 // On platforms other than Linux, just return true.
AddressIsReadable(const void *)27 bool AddressIsReadable(const void* /* addr */) { return true; }
28
29 } // namespace debugging_internal
30 ABSL_NAMESPACE_END
31 } // namespace absl
32
33 #else
34
35 #include <fcntl.h>
36 #include <sys/syscall.h>
37 #include <unistd.h>
38
39 #include <atomic>
40 #include <cerrno>
41 #include <cstdint>
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 // Pack a pid and two file descriptors into a 64-bit word,
51 // using 16, 24, and 24 bits for each respectively.
Pack(uint64_t pid,uint64_t read_fd,uint64_t write_fd)52 static uint64_t Pack(uint64_t pid, uint64_t read_fd, uint64_t write_fd) {
53 ABSL_RAW_CHECK((read_fd >> 24) == 0 && (write_fd >> 24) == 0,
54 "fd out of range");
55 return (pid << 48) | ((read_fd & 0xffffff) << 24) | (write_fd & 0xffffff);
56 }
57
58 // Unpack x into a pid and two file descriptors, where x was created with
59 // Pack().
Unpack(uint64_t x,int * pid,int * read_fd,int * write_fd)60 static void Unpack(uint64_t x, int *pid, int *read_fd, int *write_fd) {
61 *pid = x >> 48;
62 *read_fd = (x >> 24) & 0xffffff;
63 *write_fd = x & 0xffffff;
64 }
65
66 // Return whether the byte at *addr is readable, without faulting.
67 // Save and restores errno. Returns true on systems where
68 // unimplemented.
69 // This is a namespace-scoped variable for correct zero-initialization.
70 static std::atomic<uint64_t> pid_and_fds; // initially 0, an invalid pid.
AddressIsReadable(const void * addr)71 bool AddressIsReadable(const void *addr) {
72 absl::base_internal::ErrnoSaver errno_saver;
73 // We test whether a byte is readable by using write(). Normally, this would
74 // be done via a cached file descriptor to /dev/null, but linux fails to
75 // check whether the byte is readable when the destination is /dev/null, so
76 // we use a cached pipe. We store the pid of the process that created the
77 // pipe to handle the case where a process forks, and the child closes all
78 // the file descriptors and then calls this routine. This is not perfect:
79 // the child could use the routine, then close all file descriptors and then
80 // use this routine again. But the likely use of this routine is when
81 // crashing, to test the validity of pages when dumping the stack. Beware
82 // that we may leak file descriptors, but we're unlikely to leak many.
83 int bytes_written;
84 int current_pid = getpid() & 0xffff; // we use only the low order 16 bits
85 do { // until we do not get EBADF trying to use file descriptors
86 int pid;
87 int read_fd;
88 int write_fd;
89 uint64_t local_pid_and_fds = pid_and_fds.load(std::memory_order_relaxed);
90 Unpack(local_pid_and_fds, &pid, &read_fd, &write_fd);
91 while (current_pid != pid) {
92 int p[2];
93 // new pipe
94 if (pipe(p) != 0) {
95 ABSL_RAW_LOG(FATAL, "Failed to create pipe, errno=%d", errno);
96 }
97 fcntl(p[0], F_SETFD, FD_CLOEXEC);
98 fcntl(p[1], F_SETFD, FD_CLOEXEC);
99 uint64_t new_pid_and_fds = Pack(current_pid, p[0], p[1]);
100 if (pid_and_fds.compare_exchange_strong(
101 local_pid_and_fds, new_pid_and_fds, std::memory_order_relaxed,
102 std::memory_order_relaxed)) {
103 local_pid_and_fds = new_pid_and_fds; // fds exposed to other threads
104 } else { // fds not exposed to other threads; we can close them.
105 close(p[0]);
106 close(p[1]);
107 local_pid_and_fds = pid_and_fds.load(std::memory_order_relaxed);
108 }
109 Unpack(local_pid_and_fds, &pid, &read_fd, &write_fd);
110 }
111 errno = 0;
112 // Use syscall(SYS_write, ...) instead of write() to prevent ASAN
113 // and other checkers from complaining about accesses to arbitrary
114 // memory.
115 do {
116 bytes_written = syscall(SYS_write, write_fd, addr, 1);
117 } while (bytes_written == -1 && errno == EINTR);
118 if (bytes_written == 1) { // remove the byte from the pipe
119 char c;
120 while (read(read_fd, &c, 1) == -1 && errno == EINTR) {
121 }
122 }
123 if (errno == EBADF) { // Descriptors invalid.
124 // If pid_and_fds contains the problematic file descriptors we just used,
125 // this call will forget them, and the loop will try again.
126 pid_and_fds.compare_exchange_strong(local_pid_and_fds, 0,
127 std::memory_order_relaxed,
128 std::memory_order_relaxed);
129 }
130 } while (errno == EBADF);
131 return bytes_written == 1;
132 }
133
134 } // namespace debugging_internal
135 ABSL_NAMESPACE_END
136 } // namespace absl
137
138 #endif
139