vdso_support.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2008, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // ---
  30. // Author: Paul Pluzhnikov
  31. //
  32. // Allow dynamic symbol lookup in the kernel VDSO page.
  33. //
  34. // VDSOSupport -- a class representing kernel VDSO (if present).
  35. //
  36. #include "base/vdso_support.h"
  37. #ifdef HAVE_VDSO_SUPPORT // defined in vdso_support.h
  38. #include <fcntl.h>
  39. #include <stddef.h> // for ptrdiff_t
  40. #include "base/atomicops.h" // for MemoryBarrier
  41. #include "base/linux_syscall_support.h"
  42. #include "base/logging.h"
  43. #include "base/dynamic_annotations.h"
  44. #include "base/basictypes.h" // for COMPILE_ASSERT
  45. using base::subtle::MemoryBarrier;
  46. #ifndef AT_SYSINFO_EHDR
  47. #define AT_SYSINFO_EHDR 33
  48. #endif
  49. namespace base {
  50. const void *VDSOSupport::vdso_base_ = ElfMemImage::kInvalidBase;
  51. VDSOSupport::VDSOSupport()
  52. // If vdso_base_ is still set to kInvalidBase, we got here
  53. // before VDSOSupport::Init has been called. Call it now.
  54. : image_(vdso_base_ == ElfMemImage::kInvalidBase ? Init() : vdso_base_) {
  55. }
  56. // NOTE: we can't use GoogleOnceInit() below, because we can be
  57. // called by tcmalloc, and none of the *once* stuff may be functional yet.
  58. //
  59. // In addition, we hope that the VDSOSupportHelper constructor
  60. // causes this code to run before there are any threads, and before
  61. // InitGoogle() has executed any chroot or setuid calls.
  62. //
  63. // Finally, even if there is a race here, it is harmless, because
  64. // the operation should be idempotent.
  65. const void *VDSOSupport::Init() {
  66. if (vdso_base_ == ElfMemImage::kInvalidBase) {
  67. // Valgrind zaps AT_SYSINFO_EHDR and friends from the auxv[]
  68. // on stack, and so glibc works as if VDSO was not present.
  69. // But going directly to kernel via /proc/self/auxv below bypasses
  70. // Valgrind zapping. So we check for Valgrind separately.
  71. if (RunningOnValgrind()) {
  72. vdso_base_ = NULL;
  73. return NULL;
  74. }
  75. int fd = open("/proc/self/auxv", O_RDONLY);
  76. if (fd == -1) {
  77. // Kernel too old to have a VDSO.
  78. vdso_base_ = NULL;
  79. return NULL;
  80. }
  81. ElfW(auxv_t) aux;
  82. while (read(fd, &aux, sizeof(aux)) == sizeof(aux)) {
  83. if (aux.a_type == AT_SYSINFO_EHDR) {
  84. COMPILE_ASSERT(sizeof(vdso_base_) == sizeof(aux.a_un.a_val),
  85. unexpected_sizeof_pointer_NE_sizeof_a_val);
  86. vdso_base_ = reinterpret_cast<void *>(aux.a_un.a_val);
  87. break;
  88. }
  89. }
  90. close(fd);
  91. if (vdso_base_ == ElfMemImage::kInvalidBase) {
  92. // Didn't find AT_SYSINFO_EHDR in auxv[].
  93. vdso_base_ = NULL;
  94. }
  95. }
  96. return vdso_base_;
  97. }
  98. const void *VDSOSupport::SetBase(const void *base) {
  99. CHECK(base != ElfMemImage::kInvalidBase);
  100. const void *old_base = vdso_base_;
  101. vdso_base_ = base;
  102. image_.Init(base);
  103. return old_base;
  104. }
  105. bool VDSOSupport::LookupSymbol(const char *name,
  106. const char *version,
  107. int type,
  108. SymbolInfo *info) const {
  109. return image_.LookupSymbol(name, version, type, info);
  110. }
  111. bool VDSOSupport::LookupSymbolByAddress(const void *address,
  112. SymbolInfo *info_out) const {
  113. return image_.LookupSymbolByAddress(address, info_out);
  114. }
  115. // We need to make sure VDSOSupport::Init() is called before
  116. // the main() runs, since it might do something like setuid or
  117. // chroot. If VDSOSupport
  118. // is used in any global constructor, this will happen, since
  119. // VDSOSupport's constructor calls Init. But if not, we need to
  120. // ensure it here, with a global constructor of our own. This
  121. // is an allowed exception to the normal rule against non-trivial
  122. // global constructors.
  123. static class VDSOInitHelper {
  124. public:
  125. VDSOInitHelper() { VDSOSupport::Init(); }
  126. } vdso_init_helper;
  127. }
  128. #endif // HAVE_VDSO_SUPPORT