address_is_readable.cc 5.2 KB

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