vdso_support.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. // Allow dynamic symbol lookup in the kernel VDSO page.
  15. //
  16. // VDSOSupport -- a class representing kernel VDSO (if present).
  17. #include "absl/debugging/internal/vdso_support.h"
  18. #ifdef ABSL_HAVE_VDSO_SUPPORT // defined in vdso_support.h
  19. #if !defined(__has_include)
  20. #define __has_include(header) 0
  21. #endif
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #if __has_include(<syscall.h>)
  25. #include <syscall.h>
  26. #elif __has_include(<sys/syscall.h>)
  27. #include <sys/syscall.h>
  28. #endif
  29. #include <unistd.h>
  30. #if !defined(__UCLIBC__) && defined(__GLIBC__) && \
  31. (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
  32. #define ABSL_HAVE_GETAUXVAL
  33. #endif
  34. #ifdef ABSL_HAVE_GETAUXVAL
  35. #include <sys/auxv.h>
  36. #endif
  37. #include "absl/base/dynamic_annotations.h"
  38. #include "absl/base/internal/raw_logging.h"
  39. #include "absl/base/port.h"
  40. #ifndef AT_SYSINFO_EHDR
  41. #define AT_SYSINFO_EHDR 33 // for crosstoolv10
  42. #endif
  43. #if defined(__NetBSD__)
  44. using Elf32_auxv_t = Aux32Info;
  45. using Elf64_auxv_t = Aux64Info;
  46. #endif
  47. #if defined(__FreeBSD__)
  48. #if defined(__ELF_WORD_SIZE) && __ELF_WORD_SIZE == 64
  49. using Elf64_auxv_t = Elf64_Auxinfo;
  50. #endif
  51. using Elf32_auxv_t = Elf32_Auxinfo;
  52. #endif
  53. namespace absl {
  54. ABSL_NAMESPACE_BEGIN
  55. namespace debugging_internal {
  56. ABSL_CONST_INIT
  57. std::atomic<const void *> VDSOSupport::vdso_base_(
  58. debugging_internal::ElfMemImage::kInvalidBase);
  59. ABSL_CONST_INIT std::atomic<VDSOSupport::GetCpuFn> VDSOSupport::getcpu_fn_(
  60. &InitAndGetCPU);
  61. VDSOSupport::VDSOSupport()
  62. // If vdso_base_ is still set to kInvalidBase, we got here
  63. // before VDSOSupport::Init has been called. Call it now.
  64. : image_(vdso_base_.load(std::memory_order_relaxed) ==
  65. debugging_internal::ElfMemImage::kInvalidBase
  66. ? Init()
  67. : vdso_base_.load(std::memory_order_relaxed)) {}
  68. // NOTE: we can't use GoogleOnceInit() below, because we can be
  69. // called by tcmalloc, and none of the *once* stuff may be functional yet.
  70. //
  71. // In addition, we hope that the VDSOSupportHelper constructor
  72. // causes this code to run before there are any threads, and before
  73. // InitGoogle() has executed any chroot or setuid calls.
  74. //
  75. // Finally, even if there is a race here, it is harmless, because
  76. // the operation should be idempotent.
  77. const void *VDSOSupport::Init() {
  78. const auto kInvalidBase = debugging_internal::ElfMemImage::kInvalidBase;
  79. #ifdef ABSL_HAVE_GETAUXVAL
  80. if (vdso_base_.load(std::memory_order_relaxed) == kInvalidBase) {
  81. errno = 0;
  82. const void *const sysinfo_ehdr =
  83. reinterpret_cast<const void *>(getauxval(AT_SYSINFO_EHDR));
  84. if (errno == 0) {
  85. vdso_base_.store(sysinfo_ehdr, std::memory_order_relaxed);
  86. }
  87. }
  88. #endif // ABSL_HAVE_GETAUXVAL
  89. if (vdso_base_.load(std::memory_order_relaxed) == kInvalidBase) {
  90. int fd = open("/proc/self/auxv", O_RDONLY);
  91. if (fd == -1) {
  92. // Kernel too old to have a VDSO.
  93. vdso_base_.store(nullptr, std::memory_order_relaxed);
  94. getcpu_fn_.store(&GetCPUViaSyscall, std::memory_order_relaxed);
  95. return nullptr;
  96. }
  97. ElfW(auxv_t) aux;
  98. while (read(fd, &aux, sizeof(aux)) == sizeof(aux)) {
  99. if (aux.a_type == AT_SYSINFO_EHDR) {
  100. #if defined(__NetBSD__)
  101. vdso_base_.store(reinterpret_cast<void *>(aux.a_v),
  102. std::memory_order_relaxed);
  103. #else
  104. vdso_base_.store(reinterpret_cast<void *>(aux.a_un.a_val),
  105. std::memory_order_relaxed);
  106. #endif
  107. break;
  108. }
  109. }
  110. close(fd);
  111. if (vdso_base_.load(std::memory_order_relaxed) == kInvalidBase) {
  112. // Didn't find AT_SYSINFO_EHDR in auxv[].
  113. vdso_base_.store(nullptr, std::memory_order_relaxed);
  114. }
  115. }
  116. GetCpuFn fn = &GetCPUViaSyscall; // default if VDSO not present.
  117. if (vdso_base_.load(std::memory_order_relaxed)) {
  118. VDSOSupport vdso;
  119. SymbolInfo info;
  120. if (vdso.LookupSymbol("__vdso_getcpu", "LINUX_2.6", STT_FUNC, &info)) {
  121. fn = reinterpret_cast<GetCpuFn>(const_cast<void *>(info.address));
  122. }
  123. }
  124. // Subtle: this code runs outside of any locks; prevent compiler
  125. // from assigning to getcpu_fn_ more than once.
  126. getcpu_fn_.store(fn, std::memory_order_relaxed);
  127. return vdso_base_.load(std::memory_order_relaxed);
  128. }
  129. const void *VDSOSupport::SetBase(const void *base) {
  130. ABSL_RAW_CHECK(base != debugging_internal::ElfMemImage::kInvalidBase,
  131. "internal error");
  132. const void *old_base = vdso_base_.load(std::memory_order_relaxed);
  133. vdso_base_.store(base, std::memory_order_relaxed);
  134. image_.Init(base);
  135. // Also reset getcpu_fn_, so GetCPU could be tested with simulated VDSO.
  136. getcpu_fn_.store(&InitAndGetCPU, std::memory_order_relaxed);
  137. return old_base;
  138. }
  139. bool VDSOSupport::LookupSymbol(const char *name,
  140. const char *version,
  141. int type,
  142. SymbolInfo *info) const {
  143. return image_.LookupSymbol(name, version, type, info);
  144. }
  145. bool VDSOSupport::LookupSymbolByAddress(const void *address,
  146. SymbolInfo *info_out) const {
  147. return image_.LookupSymbolByAddress(address, info_out);
  148. }
  149. // NOLINT on 'long' because this routine mimics kernel api.
  150. long VDSOSupport::GetCPUViaSyscall(unsigned *cpu, // NOLINT(runtime/int)
  151. void *, void *) {
  152. #ifdef SYS_getcpu
  153. return syscall(SYS_getcpu, cpu, nullptr, nullptr);
  154. #else
  155. // x86_64 never implemented sys_getcpu(), except as a VDSO call.
  156. static_cast<void>(cpu); // Avoid an unused argument compiler warning.
  157. errno = ENOSYS;
  158. return -1;
  159. #endif
  160. }
  161. // Use fast __vdso_getcpu if available.
  162. long VDSOSupport::InitAndGetCPU(unsigned *cpu, // NOLINT(runtime/int)
  163. void *x, void *y) {
  164. Init();
  165. GetCpuFn fn = getcpu_fn_.load(std::memory_order_relaxed);
  166. ABSL_RAW_CHECK(fn != &InitAndGetCPU, "Init() did not set getcpu_fn_");
  167. return (*fn)(cpu, x, y);
  168. }
  169. // This function must be very fast, and may be called from very
  170. // low level (e.g. tcmalloc). Hence I avoid things like
  171. // GoogleOnceInit() and ::operator new.
  172. ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
  173. int GetCPU() {
  174. unsigned cpu;
  175. long ret_code = // NOLINT(runtime/int)
  176. (*VDSOSupport::getcpu_fn_)(&cpu, nullptr, nullptr);
  177. return ret_code == 0 ? static_cast<int>(cpu) : static_cast<int>(ret_code);
  178. }
  179. } // namespace debugging_internal
  180. ABSL_NAMESPACE_END
  181. } // namespace absl
  182. #endif // ABSL_HAVE_VDSO_SUPPORT