address_is_readable.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 // base::AddressIsReadable() probes an address to see whether it is readable,
00016 // without faulting.
00017 
00018 #include "absl/debugging/internal/address_is_readable.h"
00019 
00020 #if !defined(__linux__) || defined(__ANDROID__)
00021 
00022 namespace absl {
00023 namespace debugging_internal {
00024 
00025 // On platforms other than Linux, just return true.
00026 bool AddressIsReadable(const void* /* addr */) { return true; }
00027 
00028 }  // namespace debugging_internal
00029 }  // namespace absl
00030 
00031 #else
00032 
00033 #include <fcntl.h>
00034 #include <sys/syscall.h>
00035 #include <unistd.h>
00036 #include <atomic>
00037 #include <cerrno>
00038 #include <cstdint>
00039 
00040 #include "absl/base/internal/raw_logging.h"
00041 
00042 namespace absl {
00043 namespace debugging_internal {
00044 
00045 // Pack a pid and two file descriptors into a 64-bit word,
00046 // using 16, 24, and 24 bits for each respectively.
00047 static uint64_t Pack(uint64_t pid, uint64_t read_fd, uint64_t write_fd) {
00048   ABSL_RAW_CHECK((read_fd >> 24) == 0 && (write_fd >> 24) == 0,
00049                  "fd out of range");
00050   return (pid << 48) | ((read_fd & 0xffffff) << 24) | (write_fd & 0xffffff);
00051 }
00052 
00053 // Unpack x into a pid and two file descriptors, where x was created with
00054 // Pack().
00055 static void Unpack(uint64_t x, int *pid, int *read_fd, int *write_fd) {
00056   *pid = x >> 48;
00057   *read_fd = (x >> 24) & 0xffffff;
00058   *write_fd = x & 0xffffff;
00059 }
00060 
00061 // Return whether the byte at *addr is readable, without faulting.
00062 // Save and restores errno.   Returns true on systems where
00063 // unimplemented.
00064 // This is a namespace-scoped variable for correct zero-initialization.
00065 static std::atomic<uint64_t> pid_and_fds;  // initially 0, an invalid pid.
00066 bool AddressIsReadable(const void *addr) {
00067   int save_errno = errno;
00068   // We test whether a byte is readable by using write().  Normally, this would
00069   // be done via a cached file descriptor to /dev/null, but linux fails to
00070   // check whether the byte is readable when the destination is /dev/null, so
00071   // we use a cached pipe.  We store the pid of the process that created the
00072   // pipe to handle the case where a process forks, and the child closes all
00073   // the file descriptors and then calls this routine.  This is not perfect:
00074   // the child could use the routine, then close all file descriptors and then
00075   // use this routine again.  But the likely use of this routine is when
00076   // crashing, to test the validity of pages when dumping the stack.  Beware
00077   // that we may leak file descriptors, but we're unlikely to leak many.
00078   int bytes_written;
00079   int current_pid = getpid() & 0xffff;   // we use only the low order 16 bits
00080   do {  // until we do not get EBADF trying to use file descriptors
00081     int pid;
00082     int read_fd;
00083     int write_fd;
00084     uint64_t local_pid_and_fds = pid_and_fds.load(std::memory_order_relaxed);
00085     Unpack(local_pid_and_fds, &pid, &read_fd, &write_fd);
00086     while (current_pid != pid) {
00087       int p[2];
00088       // new pipe
00089       if (pipe(p) != 0) {
00090         ABSL_RAW_LOG(FATAL, "Failed to create pipe, errno=%d", errno);
00091       }
00092       fcntl(p[0], F_SETFD, FD_CLOEXEC);
00093       fcntl(p[1], F_SETFD, FD_CLOEXEC);
00094       uint64_t new_pid_and_fds = Pack(current_pid, p[0], p[1]);
00095       if (pid_and_fds.compare_exchange_strong(
00096               local_pid_and_fds, new_pid_and_fds, std::memory_order_relaxed,
00097               std::memory_order_relaxed)) {
00098         local_pid_and_fds = new_pid_and_fds;  // fds exposed to other threads
00099       } else {  // fds not exposed to other threads; we can close them.
00100         close(p[0]);
00101         close(p[1]);
00102         local_pid_and_fds = pid_and_fds.load(std::memory_order_relaxed);
00103       }
00104       Unpack(local_pid_and_fds, &pid, &read_fd, &write_fd);
00105     }
00106     errno = 0;
00107     // Use syscall(SYS_write, ...) instead of write() to prevent ASAN
00108     // and other checkers from complaining about accesses to arbitrary
00109     // memory.
00110     do {
00111       bytes_written = syscall(SYS_write, write_fd, addr, 1);
00112     } while (bytes_written == -1 && errno == EINTR);
00113     if (bytes_written == 1) {   // remove the byte from the pipe
00114       char c;
00115       while (read(read_fd, &c, 1) == -1 && errno == EINTR) {
00116       }
00117     }
00118     if (errno == EBADF) {  // Descriptors invalid.
00119       // If pid_and_fds contains the problematic file descriptors we just used,
00120       // this call will forget them, and the loop will try again.
00121       pid_and_fds.compare_exchange_strong(local_pid_and_fds, 0,
00122                                           std::memory_order_relaxed,
00123                                           std::memory_order_relaxed);
00124     }
00125   } while (errno == EBADF);
00126   errno = save_errno;
00127   return bytes_written == 1;
00128 }
00129 
00130 }  // namespace debugging_internal
00131 }  // namespace absl
00132 
00133 #endif


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14