Coverage Report

Created: 2026-09-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/debugging/internal/vdso_support.h
Line
Count
Source
1
//
2
// Copyright 2017 The Abseil Authors.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
//      https://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
//
16
17
// Allow dynamic symbol lookup in the kernel VDSO page.
18
//
19
// VDSO stands for "Virtual Dynamic Shared Object" -- a page of
20
// executable code, which looks like a shared library, but doesn't
21
// necessarily exist anywhere on disk, and which gets mmap()ed into
22
// every process by kernels which support VDSO, such as 2.6.x for 32-bit
23
// executables, and 2.6.24 and above for 64-bit executables.
24
//
25
// More details could be found here:
26
// http://www.trilithium.com/johan/2005/08/linux-gate/
27
//
28
// VDSOSupport -- a class representing kernel VDSO (if present).
29
//
30
// Example usage:
31
//  VDSOSupport vdso;
32
//  VDSOSupport::SymbolInfo info;
33
//  typedef (*FN)(unsigned *, void *, void *);
34
//  FN fn = nullptr;
35
//  if (vdso.LookupSymbol("__vdso_getcpu", "LINUX_2.6", STT_FUNC, &info)) {
36
//     fn = reinterpret_cast<FN>(info.address);
37
//  }
38
39
#ifndef ABSL_DEBUGGING_INTERNAL_VDSO_SUPPORT_H_
40
#define ABSL_DEBUGGING_INTERNAL_VDSO_SUPPORT_H_
41
42
#include <atomic>
43
44
#include "absl/base/attributes.h"
45
#include "absl/base/config.h"
46
#include "absl/debugging/internal/elf_mem_image.h"
47
48
#ifdef ABSL_HAVE_ELF_MEM_IMAGE
49
50
#ifdef ABSL_HAVE_VDSO_SUPPORT
51
#error ABSL_HAVE_VDSO_SUPPORT cannot be directly set
52
#else
53
#define ABSL_HAVE_VDSO_SUPPORT 1
54
#endif
55
56
namespace absl {
57
ABSL_NAMESPACE_BEGIN
58
namespace debugging_internal {
59
60
// NOTE: this class may be used from within tcmalloc, and can not
61
// use any memory allocation routines.
62
class VDSOSupport {
63
 public:
64
  VDSOSupport();
65
66
  typedef ElfMemImage::SymbolInfo SymbolInfo;
67
  typedef ElfMemImage::SymbolIterator SymbolIterator;
68
69
  // On PowerPC64 VDSO symbols can either be of type STT_FUNC or STT_NOTYPE
70
  // depending on how the kernel is built.  The kernel is normally built with
71
  // STT_NOTYPE type VDSO symbols.  Let's make things simpler first by using a
72
  // compile-time constant.
73
#ifdef __powerpc64__
74
  enum { kVDSOSymbolType = STT_NOTYPE };
75
#else
76
  enum { kVDSOSymbolType = STT_FUNC };
77
#endif
78
79
  // Answers whether we have a vdso at all.
80
0
  bool IsPresent() const { return image_.IsPresent(); }
81
82
  // Allow to iterate over all VDSO symbols.
83
0
  SymbolIterator begin() const { return image_.begin(); }
84
0
  SymbolIterator end() const { return image_.end(); }
85
86
  // Look up versioned dynamic symbol in the kernel VDSO.
87
  // Returns false if VDSO is not present, or doesn't contain given
88
  // symbol/version/type combination.
89
  // If info_out != nullptr, additional details are filled in.
90
  bool LookupSymbol(const char *name, const char *version,
91
                    int symbol_type, SymbolInfo *info_out) const;
92
93
  // Find info about symbol (if any) which overlaps given address.
94
  // Returns true if symbol was found; false if VDSO isn't present
95
  // or doesn't have a symbol overlapping given address.
96
  // If info_out != nullptr, additional details are filled in.
97
  bool LookupSymbolByAddress(const void *address, SymbolInfo *info_out) const;
98
99
  // Used only for testing. Replace real VDSO base with a mock.
100
  // Returns previous value of vdso_base_. After you are done testing,
101
  // you are expected to call SetBase() with previous value, in order to
102
  // reset state to the way it was.
103
  const void *SetBase(const void *s);
104
105
  // Computes vdso_base_ and returns it. Should be called as early as
106
  // possible; before any thread creation, chroot or setuid.
107
  static const void *Init();
108
109
 private:
110
  // image_ represents VDSO ELF image in memory.
111
  // image_.ehdr_ == nullptr implies there is no VDSO.
112
  ElfMemImage image_;
113
114
  // Cached value of auxv AT_SYSINFO_EHDR, computed once.
115
  // This is a tri-state:
116
  //   kInvalidBase   => value hasn't been determined yet.
117
  //              0   => there is no VDSO.
118
  //           else   => vma of VDSO Elf{32,64}_Ehdr.
119
  //
120
  // When testing with mock VDSO, low bit is set.
121
  // The low bit is always available because vdso_base_ is
122
  // page-aligned.
123
  static std::atomic<const void *> vdso_base_;
124
125
  // NOLINT on 'long' because these routines mimic kernel api.
126
  // The 'cache' parameter may be used by some versions of the kernel,
127
  // and should be nullptr or point to a static buffer containing at
128
  // least two 'long's.
129
  static long InitAndGetCPU(unsigned *cpu, void *cache,     // NOLINT 'long'.
130
                            void *unused);
131
  static long GetCPUViaSyscall(unsigned *cpu, void *cache,  // NOLINT 'long'.
132
                               void *unused);
133
  typedef long (*GetCpuFn)(unsigned *cpu, void *cache,      // NOLINT 'long'.
134
                           void *unused);
135
136
  // This function pointer may point to InitAndGetCPU,
137
  // GetCPUViaSyscall, or __vdso_getcpu at different stages of initialization.
138
  ABSL_CONST_INIT static std::atomic<GetCpuFn> getcpu_fn_;
139
140
  friend int GetCPU(void);  // Needs access to getcpu_fn_.
141
142
  VDSOSupport(const VDSOSupport&) = delete;
143
  VDSOSupport& operator=(const VDSOSupport&) = delete;
144
};
145
146
// Same as sched_getcpu() on later glibc versions.
147
// Return current CPU, using (fast) __vdso_getcpu@LINUX_2.6 if present,
148
// otherwise use syscall(SYS_getcpu,...).
149
// May return -1 with errno == ENOSYS if the kernel doesn't
150
// support SYS_getcpu.
151
int GetCPU();
152
153
}  // namespace debugging_internal
154
ABSL_NAMESPACE_END
155
}  // namespace absl
156
157
#endif  // ABSL_HAVE_ELF_MEM_IMAGE
158
159
#endif  // ABSL_DEBUGGING_INTERNAL_VDSO_SUPPORT_H_