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.
71 
AddressIsReadable(const void * addr)72 bool AddressIsReadable(const void *addr) {
73   absl::base_internal::ErrnoSaver errno_saver;
74   // We test whether a byte is readable by using write().  Normally, this would
75   // be done via a cached file descriptor to /dev/null, but linux fails to
76   // check whether the byte is readable when the destination is /dev/null, so
77   // we use a cached pipe.  We store the pid of the process that created the
78   // pipe to handle the case where a process forks, and the child closes all
79   // the file descriptors and then calls this routine.  This is not perfect:
80   // the child could use the routine, then close all file descriptors and then
81   // use this routine again.  But the likely use of this routine is when
82   // crashing, to test the validity of pages when dumping the stack.  Beware
83   // that we may leak file descriptors, but we're unlikely to leak many.
84   int bytes_written;
85   int current_pid = getpid() & 0xffff;   // we use only the low order 16 bits
86   do {  // until we do not get EBADF trying to use file descriptors
87     int pid;
88     int read_fd;
89     int write_fd;
90     uint64_t local_pid_and_fds = pid_and_fds.load(std::memory_order_acquire);
91     Unpack(local_pid_and_fds, &pid, &read_fd, &write_fd);
92     while (current_pid != pid) {
93       int p[2];
94       // new pipe
95       if (pipe(p) != 0) {
96         ABSL_RAW_LOG(FATAL, "Failed to create pipe, errno=%d", errno);
97       }
98       fcntl(p[0], F_SETFD, FD_CLOEXEC);
99       fcntl(p[1], F_SETFD, FD_CLOEXEC);
100       uint64_t new_pid_and_fds = Pack(current_pid, p[0], p[1]);
101       if (pid_and_fds.compare_exchange_strong(
102               local_pid_and_fds, new_pid_and_fds, std::memory_order_release,
103               std::memory_order_relaxed)) {
104         local_pid_and_fds = new_pid_and_fds;  // fds exposed to other threads
105       } else {  // fds not exposed to other threads; we can close them.
106         close(p[0]);
107         close(p[1]);
108         local_pid_and_fds = pid_and_fds.load(std::memory_order_acquire);
109       }
110       Unpack(local_pid_and_fds, &pid, &read_fd, &write_fd);
111     }
112     errno = 0;
113     // Use syscall(SYS_write, ...) instead of write() to prevent ASAN
114     // and other checkers from complaining about accesses to arbitrary
115     // memory.
116     do {
117       bytes_written = syscall(SYS_write, write_fd, addr, 1);
118     } while (bytes_written == -1 && errno == EINTR);
119     if (bytes_written == 1) {   // remove the byte from the pipe
120       char c;
121       while (read(read_fd, &c, 1) == -1 && errno == EINTR) {
122       }
123     }
124     if (errno == EBADF) {  // Descriptors invalid.
125       // If pid_and_fds contains the problematic file descriptors we just used,
126       // this call will forget them, and the loop will try again.
127       pid_and_fds.compare_exchange_strong(local_pid_and_fds, 0,
128                                           std::memory_order_release,
129                                           std::memory_order_relaxed);
130     }
131   } while (errno == EBADF);
132   return bytes_written == 1;
133 }
134 
135 }  // namespace debugging_internal
136 ABSL_NAMESPACE_END
137 }  // namespace absl
138 
139 #endif
140