vdso_support.cc 6.4 KB

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