Coverage Report

Created: 2022-01-18 06:27

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