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