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