Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- DynamicLoaderWindowsDYLD.cpp --------------------------------------===//
2
//
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
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "DynamicLoaderWindowsDYLD.h"
10
11
#include "lldb/Core/Module.h"
12
#include "lldb/Core/PluginManager.h"
13
#include "lldb/Target/ExecutionContext.h"
14
#include "lldb/Target/Platform.h"
15
#include "lldb/Target/Process.h"
16
#include "lldb/Target/RegisterContext.h"
17
#include "lldb/Target/Target.h"
18
#include "lldb/Target/ThreadPlanStepInstruction.h"
19
#include "lldb/Utility/LLDBLog.h"
20
#include "lldb/Utility/Log.h"
21
22
#include "llvm/ADT/Triple.h"
23
24
using namespace lldb;
25
using namespace lldb_private;
26
27
LLDB_PLUGIN_DEFINE(DynamicLoaderWindowsDYLD)
28
29
DynamicLoaderWindowsDYLD::DynamicLoaderWindowsDYLD(Process *process)
30
0
    : DynamicLoader(process) {}
31
32
0
DynamicLoaderWindowsDYLD::~DynamicLoaderWindowsDYLD() = default;
33
34
3.61k
void DynamicLoaderWindowsDYLD::Initialize() {
35
3.61k
  PluginManager::RegisterPlugin(GetPluginNameStatic(),
36
3.61k
                                GetPluginDescriptionStatic(), CreateInstance);
37
3.61k
}
38
39
3.60k
void DynamicLoaderWindowsDYLD::Terminate() {}
40
41
3.61k
llvm::StringRef DynamicLoaderWindowsDYLD::GetPluginDescriptionStatic() {
42
3.61k
  return "Dynamic loader plug-in that watches for shared library "
43
3.61k
         "loads/unloads in Windows processes.";
44
3.61k
}
45
46
DynamicLoader *DynamicLoaderWindowsDYLD::CreateInstance(Process *process,
47
3
                                                        bool force) {
48
3
  bool should_create = force;
49
3
  if (!should_create) {
50
3
    const llvm::Triple &triple_ref =
51
3
        process->GetTarget().GetArchitecture().GetTriple();
52
3
    if (triple_ref.getOS() == llvm::Triple::Win32)
53
0
      should_create = true;
54
3
  }
55
56
3
  if (should_create)
57
0
    return new DynamicLoaderWindowsDYLD(process);
58
59
3
  return nullptr;
60
3
}
61
62
void DynamicLoaderWindowsDYLD::OnLoadModule(lldb::ModuleSP module_sp,
63
                                            const ModuleSpec module_spec,
64
0
                                            lldb::addr_t module_addr) {
65
66
  // Resolve the module unless we already have one.
67
0
  if (!module_sp) {
68
0
    Status error;
69
0
    module_sp = m_process->GetTarget().GetOrCreateModule(module_spec, 
70
0
                                             true /* notify */, &error);
71
0
    if (error.Fail())
72
0
      return;
73
0
  }
74
75
0
  m_loaded_modules[module_sp] = module_addr;
76
0
  UpdateLoadedSectionsCommon(module_sp, module_addr, false);
77
0
  ModuleList module_list;
78
0
  module_list.Append(module_sp);
79
0
  m_process->GetTarget().ModulesDidLoad(module_list);
80
0
}
81
82
0
void DynamicLoaderWindowsDYLD::OnUnloadModule(lldb::addr_t module_addr) {
83
0
  Address resolved_addr;
84
0
  if (!m_process->GetTarget().ResolveLoadAddress(module_addr, resolved_addr))
85
0
    return;
86
87
0
  ModuleSP module_sp = resolved_addr.GetModule();
88
0
  if (module_sp) {
89
0
    m_loaded_modules.erase(module_sp);
90
0
    UnloadSectionsCommon(module_sp);
91
0
    ModuleList module_list;
92
0
    module_list.Append(module_sp);
93
0
    m_process->GetTarget().ModulesDidUnload(module_list, false);
94
0
  }
95
0
}
96
97
0
lldb::addr_t DynamicLoaderWindowsDYLD::GetLoadAddress(ModuleSP executable) {
98
  // First, see if the load address is already cached.
99
0
  auto it = m_loaded_modules.find(executable);
100
0
  if (it != m_loaded_modules.end() && it->second != LLDB_INVALID_ADDRESS)
101
0
    return it->second;
102
103
0
  lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
104
105
  // Second, try to get it through the process plugins.  For a remote process,
106
  // the remote platform will be responsible for providing it.
107
0
  FileSpec file_spec(executable->GetPlatformFileSpec());
108
0
  bool is_loaded = false;
109
0
  Status status =
110
0
      m_process->GetFileLoadAddress(file_spec, is_loaded, load_addr);
111
  // Servers other than lldb server could respond with a bogus address.
112
0
  if (status.Success() && is_loaded && load_addr != LLDB_INVALID_ADDRESS) {
113
0
    m_loaded_modules[executable] = load_addr;
114
0
    return load_addr;
115
0
  }
116
117
0
  return LLDB_INVALID_ADDRESS;
118
0
}
119
120
0
void DynamicLoaderWindowsDYLD::DidAttach() {
121
0
  Log *log = GetLog(LLDBLog::DynamicLoader);
122
0
  LLDB_LOGF(log, "DynamicLoaderWindowsDYLD::%s()", __FUNCTION__);
123
124
0
  ModuleSP executable = GetTargetExecutable();
125
126
0
  if (!executable.get())
127
0
    return;
128
129
  // Try to fetch the load address of the file from the process, since there
130
  // could be randomization of the load address.
131
0
  lldb::addr_t load_addr = GetLoadAddress(executable);
132
0
  if (load_addr == LLDB_INVALID_ADDRESS)
133
0
    return;
134
135
  // Request the process base address.
136
0
  lldb::addr_t image_base = m_process->GetImageInfoAddress();
137
0
  if (image_base == load_addr)
138
0
    return;
139
140
  // Rebase the process's modules if there is a mismatch.
141
0
  UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false);
142
143
0
  ModuleList module_list;
144
0
  module_list.Append(executable);
145
0
  m_process->GetTarget().ModulesDidLoad(module_list);
146
0
  auto error = m_process->LoadModules();
147
0
  LLDB_LOG_ERROR(log, std::move(error), "failed to load modules: {0}");
148
0
}
149
150
0
void DynamicLoaderWindowsDYLD::DidLaunch() {
151
0
  Log *log = GetLog(LLDBLog::DynamicLoader);
152
0
  LLDB_LOGF(log, "DynamicLoaderWindowsDYLD::%s()", __FUNCTION__);
153
154
0
  ModuleSP executable = GetTargetExecutable();
155
0
  if (!executable.get())
156
0
    return;
157
158
0
  lldb::addr_t load_addr = GetLoadAddress(executable);
159
0
  if (load_addr != LLDB_INVALID_ADDRESS) {
160
    // Update the loaded sections so that the breakpoints can be resolved.
161
0
    UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false);
162
163
0
    ModuleList module_list;
164
0
    module_list.Append(executable);
165
0
    m_process->GetTarget().ModulesDidLoad(module_list);
166
0
    auto error = m_process->LoadModules();
167
0
    LLDB_LOG_ERROR(log, std::move(error), "failed to load modules: {0}");
168
0
  }
169
0
}
170
171
0
Status DynamicLoaderWindowsDYLD::CanLoadImage() { return Status(); }
172
173
ThreadPlanSP
174
DynamicLoaderWindowsDYLD::GetStepThroughTrampolinePlan(Thread &thread,
175
0
                                                       bool stop) {
176
0
  auto arch = m_process->GetTarget().GetArchitecture();
177
0
  if (arch.GetMachine() != llvm::Triple::x86) {
178
0
    return ThreadPlanSP();
179
0
  }
180
181
0
  uint64_t pc = thread.GetRegisterContext()->GetPC();
182
  // Max size of an instruction in x86 is 15 bytes.
183
0
  AddressRange range(pc, 2 * 15);
184
185
0
  DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
186
0
      arch, nullptr, nullptr, m_process->GetTarget(), range);
187
0
  if (!disassembler_sp) {
188
0
    return ThreadPlanSP();
189
0
  }
190
191
0
  InstructionList *insn_list = &disassembler_sp->GetInstructionList();
192
0
  if (insn_list == nullptr) {
193
0
    return ThreadPlanSP();
194
0
  }
195
196
  // First instruction in a x86 Windows trampoline is going to be an indirect
197
  // jump through the IAT and the next one will be a nop (usually there for
198
  // alignment purposes). e.g.:
199
  //     0x70ff4cfc <+956>: jmpl   *0x7100c2a8
200
  //     0x70ff4d02 <+962>: nop
201
202
0
  auto first_insn = insn_list->GetInstructionAtIndex(0);
203
0
  auto second_insn = insn_list->GetInstructionAtIndex(1);
204
205
0
  ExecutionContext exe_ctx(m_process->GetTarget());
206
0
  if (first_insn == nullptr || second_insn == nullptr ||
207
0
      strcmp(first_insn->GetMnemonic(&exe_ctx), "jmpl") != 0 ||
208
0
      strcmp(second_insn->GetMnemonic(&exe_ctx), "nop") != 0) {
209
0
    return ThreadPlanSP();
210
0
  }
211
212
0
  assert(first_insn->DoesBranch() && !second_insn->DoesBranch());
213
214
0
  return ThreadPlanSP(new ThreadPlanStepInstruction(
215
0
      thread, false, false, eVoteNoOpinion, eVoteNoOpinion));
216
0
}