]> git.ipfire.org Git - thirdparty/gcc.git/blame - libsanitizer/lsan/lsan_common_linux.cpp
[Ada] Improved support for aspect alignment in CCG
[thirdparty/gcc.git] / libsanitizer / lsan / lsan_common_linux.cpp
CommitLineData
b667dd70 1//=-- lsan_common_linux.cpp -----------------------------------------------===//
ef1b3fda 2//
b667dd70
ML
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
ef1b3fda
KS
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of LeakSanitizer.
b667dd70
ML
10// Implementation of common leak checking functionality. Linux/NetBSD-specific
11// code.
ef1b3fda
KS
12//
13//===----------------------------------------------------------------------===//
14
15#include "sanitizer_common/sanitizer_platform.h"
16#include "lsan_common.h"
17
b667dd70 18#if CAN_SANITIZE_LEAKS && (SANITIZER_LINUX || SANITIZER_NETBSD)
ef1b3fda
KS
19#include <link.h>
20
21#include "sanitizer_common/sanitizer_common.h"
dee5ea7a 22#include "sanitizer_common/sanitizer_flags.h"
eac97531 23#include "sanitizer_common/sanitizer_getauxval.h"
ef1b3fda
KS
24#include "sanitizer_common/sanitizer_linux.h"
25#include "sanitizer_common/sanitizer_stackdepot.h"
26
27namespace __lsan {
28
29static const char kLinkerName[] = "ld";
10189819
MO
30
31static char linker_placeholder[sizeof(LoadedModule)] ALIGNED(64);
696d846a 32static LoadedModule *linker = nullptr;
ef1b3fda 33
eac97531
ML
34static bool IsLinker(const LoadedModule& module) {
35#if SANITIZER_USE_GETAUXVAL
36 return module.base_address() == getauxval(AT_BASE);
37#else
38 return LibraryNameIs(module.full_name(), kLinkerName);
39#endif // SANITIZER_USE_GETAUXVAL
ef1b3fda
KS
40}
41
5d3805fc
JJ
42__attribute__((tls_model("initial-exec")))
43THREADLOCAL int disable_counter;
44bool DisabledInThisThread() { return disable_counter > 0; }
45void DisableInThisThread() { disable_counter++; }
46void EnableInThisThread() {
47 if (disable_counter == 0) {
48 DisableCounterUnderflow();
49 }
50 disable_counter--;
51}
52
ef1b3fda 53void InitializePlatformSpecificModules() {
10189819
MO
54 ListOfModules modules;
55 modules.init();
56 for (LoadedModule &module : modules) {
eac97531
ML
57 if (!IsLinker(module))
58 continue;
10189819
MO
59 if (linker == nullptr) {
60 linker = reinterpret_cast<LoadedModule *>(linker_placeholder);
61 *linker = module;
62 module = LoadedModule();
63 } else {
64 VReport(1, "LeakSanitizer: Multiple modules match \"%s\". "
eac97531
ML
65 "TLS and other allocations originating from linker might be "
66 "falsely reported as leaks.\n", kLinkerName);
10189819
MO
67 linker->clear();
68 linker = nullptr;
69 return;
70 }
ef1b3fda 71 }
5d3805fc 72 if (linker == nullptr) {
eac97531
ML
73 VReport(1, "LeakSanitizer: Dynamic linker not found. TLS and other "
74 "allocations originating from linker might be falsely reported "
75 "as leaks.\n");
5d3805fc 76 }
ef1b3fda
KS
77}
78
79static int ProcessGlobalRegionsCallback(struct dl_phdr_info *info, size_t size,
80 void *data) {
81 Frontier *frontier = reinterpret_cast<Frontier *>(data);
82 for (uptr j = 0; j < info->dlpi_phnum; j++) {
83 const ElfW(Phdr) *phdr = &(info->dlpi_phdr[j]);
84 // We're looking for .data and .bss sections, which reside in writeable,
85 // loadable segments.
86 if (!(phdr->p_flags & PF_W) || (phdr->p_type != PT_LOAD) ||
87 (phdr->p_memsz == 0))
88 continue;
89 uptr begin = info->dlpi_addr + phdr->p_vaddr;
90 uptr end = begin + phdr->p_memsz;
5d3805fc 91 ScanGlobalRange(begin, end, frontier);
ef1b3fda
KS
92 }
93 return 0;
94}
95
96// Scans global variables for heap pointers.
97void ProcessGlobalRegions(Frontier *frontier) {
dee5ea7a 98 if (!flags()->use_globals) return;
ef1b3fda
KS
99 dl_iterate_phdr(ProcessGlobalRegionsCallback, frontier);
100}
101
5d3805fc 102LoadedModule *GetLinker() { return linker; }
ef1b3fda 103
5d3805fc 104void ProcessPlatformSpecificAllocations(Frontier *frontier) {}
ef1b3fda 105
696d846a
MO
106struct DoStopTheWorldParam {
107 StopTheWorldCallback callback;
108 void *argument;
109};
110
5d3805fc
JJ
111// While calling Die() here is undefined behavior and can potentially
112// cause race conditions, it isn't possible to intercept exit on linux,
113// so we have no choice but to call Die() from the atexit handler.
114void HandleLeaks() {
115 if (common_flags()->exitcode) Die();
116}
117
3ca75cd5
ML
118static int LockStuffAndStopTheWorldCallback(struct dl_phdr_info *info,
119 size_t size, void *data) {
120 LockThreadRegistry();
121 LockAllocator();
696d846a
MO
122 DoStopTheWorldParam *param = reinterpret_cast<DoStopTheWorldParam *>(data);
123 StopTheWorld(param->callback, param->argument);
3ca75cd5
ML
124 UnlockAllocator();
125 UnlockThreadRegistry();
696d846a
MO
126 return 1;
127}
128
129// LSan calls dl_iterate_phdr() from the tracer task. This may deadlock: if one
130// of the threads is frozen while holding the libdl lock, the tracer will hang
131// in dl_iterate_phdr() forever.
132// Luckily, (a) the lock is reentrant and (b) libc can't distinguish between the
133// tracer task and the thread that spawned it. Thus, if we run the tracer task
134// while holding the libdl lock in the parent thread, we can safely reenter it
135// in the tracer. The solution is to run stoptheworld from a dl_iterate_phdr()
136// callback in the parent thread.
3c6331c2
ML
137void LockStuffAndStopTheWorld(StopTheWorldCallback callback,
138 CheckForLeaksParam *argument) {
696d846a 139 DoStopTheWorldParam param = {callback, argument};
3ca75cd5 140 dl_iterate_phdr(LockStuffAndStopTheWorldCallback, &param);
696d846a
MO
141}
142
143} // namespace __lsan
144
b667dd70 145#endif