Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- DynamicLoaderMacOS.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 "lldb/Breakpoint/StoppointCallbackContext.h"
10
#include "lldb/Core/Debugger.h"
11
#include "lldb/Core/Module.h"
12
#include "lldb/Core/PluginManager.h"
13
#include "lldb/Core/Section.h"
14
#include "lldb/Symbol/ObjectFile.h"
15
#include "lldb/Symbol/SymbolVendor.h"
16
#include "lldb/Target/ABI.h"
17
#include "lldb/Target/SectionLoadList.h"
18
#include "lldb/Target/StackFrame.h"
19
#include "lldb/Target/Target.h"
20
#include "lldb/Target/Thread.h"
21
#include "lldb/Utility/LLDBLog.h"
22
#include "lldb/Utility/Log.h"
23
#include "lldb/Utility/State.h"
24
25
#include "DynamicLoaderDarwin.h"
26
#include "DynamicLoaderMacOS.h"
27
28
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
29
30
using namespace lldb;
31
using namespace lldb_private;
32
33
// Create an instance of this class. This function is filled into the plugin
34
// info class that gets handed out by the plugin factory and allows the lldb to
35
// instantiate an instance of this class.
36
DynamicLoader *DynamicLoaderMacOS::CreateInstance(Process *process,
37
2.74k
                                                  bool force) {
38
2.74k
  bool create = force;
39
2.74k
  if (!create) {
40
2.74k
    create = true;
41
2.74k
    Module *exe_module = process->GetTarget().GetExecutableModulePointer();
42
2.74k
    if (exe_module) {
43
2.68k
      ObjectFile *object_file = exe_module->GetObjectFile();
44
2.68k
      if (object_file) {
45
2.68k
        create = (object_file->GetStrata() == ObjectFile::eStrataUser);
46
2.68k
      }
47
2.68k
    }
48
49
2.74k
    if (create) {
50
2.74k
      const llvm::Triple &triple_ref =
51
2.74k
          process->GetTarget().GetArchitecture().GetTriple();
52
2.74k
      switch (triple_ref.getOS()) {
53
0
      case llvm::Triple::Darwin:
54
2.67k
      case llvm::Triple::MacOSX:
55
2.67k
      case llvm::Triple::IOS:
56
2.67k
      case llvm::Triple::TvOS:
57
2.67k
      case llvm::Triple::WatchOS:
58
      // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS:
59
2.67k
        create = triple_ref.getVendor() == llvm::Triple::Apple;
60
2.67k
        break;
61
66
      default:
62
66
        create = false;
63
66
        break;
64
2.74k
      }
65
2.74k
    }
66
2.74k
  }
67
68
2.74k
  if (!UseDYLDSPI(process)) {
69
66
    create = false;
70
66
  }
71
72
2.74k
  if (create)
73
2.67k
    return new DynamicLoaderMacOS(process);
74
67
  return nullptr;
75
2.74k
}
76
77
// Constructor
78
DynamicLoaderMacOS::DynamicLoaderMacOS(Process *process)
79
    : DynamicLoaderDarwin(process), m_image_infos_stop_id(UINT32_MAX),
80
      m_break_id(LLDB_INVALID_BREAK_ID),
81
      m_dyld_handover_break_id(LLDB_INVALID_BREAK_ID), m_mutex(),
82
2.67k
      m_maybe_image_infos_address(LLDB_INVALID_ADDRESS) {}
83
84
// Destructor
85
2.67k
DynamicLoaderMacOS::~DynamicLoaderMacOS() {
86
2.67k
  if (LLDB_BREAK_ID_IS_VALID(m_break_id))
87
2.67k
    m_process->GetTarget().RemoveBreakpointByID(m_break_id);
88
2.67k
  if (LLDB_BREAK_ID_IS_VALID(m_dyld_handover_break_id))
89
0
    m_process->GetTarget().RemoveBreakpointByID(m_dyld_handover_break_id);
90
2.67k
}
91
92
6
bool DynamicLoaderMacOS::ProcessDidExec() {
93
6
  std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex());
94
6
  bool did_exec = false;
95
6
  if (m_process) {
96
    // If we are stopped after an exec, we will have only one thread...
97
6
    if (m_process->GetThreadList().GetSize() == 1) {
98
      // Maybe we still have an image infos address around?  If so see
99
      // if that has changed, and if so we have exec'ed.
100
6
      if (m_maybe_image_infos_address != LLDB_INVALID_ADDRESS) {
101
6
        lldb::addr_t image_infos_address = m_process->GetImageInfoAddress();
102
6
        if (image_infos_address != m_maybe_image_infos_address) {
103
          // We don't really have to reset this here, since we are going to
104
          // call DoInitialImageFetch right away to handle the exec.  But in
105
          // case anybody looks at it in the meantime, it can't hurt.
106
0
          m_maybe_image_infos_address = image_infos_address;
107
0
          did_exec = true;
108
0
        }
109
6
      }
110
111
6
      if (!did_exec) {
112
        // See if we are stopped at '_dyld_start'
113
6
        ThreadSP thread_sp(m_process->GetThreadList().GetThreadAtIndex(0));
114
6
        if (thread_sp) {
115
6
          lldb::StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0));
116
6
          if (frame_sp) {
117
6
            const Symbol *symbol =
118
6
                frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
119
6
            if (symbol) {
120
6
              if (symbol->GetName() == "_dyld_start")
121
0
                did_exec = true;
122
6
            }
123
6
          }
124
6
        }
125
6
      }
126
6
    }
127
6
  }
128
129
6
  if (did_exec) {
130
0
    m_libpthread_module_wp.reset();
131
0
    m_pthread_getspecific_addr.Clear();
132
0
  }
133
6
  return did_exec;
134
6
}
135
136
// Clear out the state of this class.
137
0
void DynamicLoaderMacOS::DoClear() {
138
0
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
139
140
0
  if (LLDB_BREAK_ID_IS_VALID(m_break_id))
141
0
    m_process->GetTarget().RemoveBreakpointByID(m_break_id);
142
0
  if (LLDB_BREAK_ID_IS_VALID(m_dyld_handover_break_id))
143
0
    m_process->GetTarget().RemoveBreakpointByID(m_dyld_handover_break_id);
144
145
0
  m_break_id = LLDB_INVALID_BREAK_ID;
146
0
  m_dyld_handover_break_id = LLDB_INVALID_BREAK_ID;
147
0
}
148
149
// Check if we have found DYLD yet
150
0
bool DynamicLoaderMacOS::DidSetNotificationBreakpoint() {
151
0
  return LLDB_BREAK_ID_IS_VALID(m_break_id);
152
0
}
153
154
0
void DynamicLoaderMacOS::ClearNotificationBreakpoint() {
155
0
  if (LLDB_BREAK_ID_IS_VALID(m_break_id)) {
156
0
    m_process->GetTarget().RemoveBreakpointByID(m_break_id);
157
0
    m_break_id = LLDB_INVALID_BREAK_ID;
158
0
  }
159
0
}
160
161
// Try and figure out where dyld is by first asking the Process if it knows
162
// (which currently calls down in the lldb::Process to get the DYLD info
163
// (available on SnowLeopard only). If that fails, then check in the default
164
// addresses.
165
2.67k
void DynamicLoaderMacOS::DoInitialImageFetch() {
166
2.67k
  Log *log = GetLog(LLDBLog::DynamicLoader);
167
168
  // Remove any binaries we pre-loaded in the Target before
169
  // launching/attaching. If the same binaries are present in the process,
170
  // we'll get them from the shared module cache, we won't need to re-load them
171
  // from disk.
172
2.67k
  UnloadAllImages();
173
174
2.67k
  StructuredData::ObjectSP all_image_info_json_sp(
175
2.67k
      m_process->GetLoadedDynamicLibrariesInfos());
176
2.67k
  ImageInfo::collection image_infos;
177
2.67k
  if (all_image_info_json_sp.get() &&
178
2.67k
      
all_image_info_json_sp->GetAsDictionary()2.67k
&&
179
2.67k
      
all_image_info_json_sp->GetAsDictionary()->HasKey("images")2.67k
&&
180
2.67k
      all_image_info_json_sp->GetAsDictionary()
181
2.67k
          ->GetValueForKey("images")
182
2.67k
          ->GetAsArray()) {
183
2.67k
    if (JSONImageInformationIntoImageInfo(all_image_info_json_sp,
184
2.67k
                                          image_infos)) {
185
2.67k
      LLDB_LOGF(log, "Initial module fetch:  Adding %" PRId64 " modules.\n",
186
2.67k
                (uint64_t)image_infos.size());
187
188
2.67k
      UpdateSpecialBinariesFromNewImageInfos(image_infos);
189
2.67k
      AddModulesUsingImageInfos(image_infos);
190
2.67k
    }
191
2.67k
  }
192
193
2.67k
  m_dyld_image_infos_stop_id = m_process->GetStopID();
194
2.67k
  m_maybe_image_infos_address = m_process->GetImageInfoAddress();
195
2.67k
}
196
197
0
bool DynamicLoaderMacOS::NeedToDoInitialImageFetch() { return true; }
198
199
// Static callback function that gets called when our DYLD notification
200
// breakpoint gets hit. We update all of our image infos and then let our super
201
// class DynamicLoader class decide if we should stop or not (based on global
202
// preference).
203
bool DynamicLoaderMacOS::NotifyBreakpointHit(void *baton,
204
                                             StoppointCallbackContext *context,
205
                                             lldb::user_id_t break_id,
206
2.72k
                                             lldb::user_id_t break_loc_id) {
207
  // Let the event know that the images have changed
208
  // DYLD passes three arguments to the notification breakpoint.
209
  // Arg1: enum dyld_notify_mode mode - 0 = adding, 1 = removing, 2 = remove
210
  // all Arg2: unsigned long icount        - Number of shared libraries
211
  // added/removed Arg3: uint64_t mach_headers[]     - Array of load addresses
212
  // of binaries added/removed
213
214
2.72k
  DynamicLoaderMacOS *dyld_instance = (DynamicLoaderMacOS *)baton;
215
216
2.72k
  ExecutionContext exe_ctx(context->exe_ctx_ref);
217
2.72k
  Process *process = exe_ctx.GetProcessPtr();
218
219
  // This is a sanity check just in case this dyld_instance is an old dyld
220
  // plugin's breakpoint still lying around.
221
2.72k
  if (process != dyld_instance->m_process)
222
0
    return false;
223
224
2.72k
  if (dyld_instance->m_image_infos_stop_id != UINT32_MAX &&
225
2.72k
      
process->GetStopID() < dyld_instance->m_image_infos_stop_id0
) {
226
0
    return false;
227
0
  }
228
229
2.72k
  const lldb::ABISP &abi = process->GetABI();
230
2.72k
  if (abi) {
231
    // Build up the value array to store the three arguments given above, then
232
    // get the values from the ABI:
233
234
2.72k
    TypeSystemClang *clang_ast_context =
235
2.72k
        ScratchTypeSystemClang::GetForTarget(process->GetTarget());
236
2.72k
    if (!clang_ast_context)
237
0
      return false;
238
239
2.72k
    ValueList argument_values;
240
241
2.72k
    Value mode_value;    // enum dyld_notify_mode { dyld_notify_adding=0,
242
                         // dyld_notify_removing=1, dyld_notify_remove_all=2 };
243
2.72k
    Value count_value;   // unsigned long count
244
2.72k
    Value headers_value; // uint64_t machHeaders[] (aka void*)
245
246
2.72k
    CompilerType clang_void_ptr_type =
247
2.72k
        clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
248
2.72k
    CompilerType clang_uint32_type =
249
2.72k
        clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
250
2.72k
            lldb::eEncodingUint, 32);
251
2.72k
    CompilerType clang_uint64_type =
252
2.72k
        clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
253
2.72k
            lldb::eEncodingUint, 32);
254
255
2.72k
    mode_value.SetValueType(Value::ValueType::Scalar);
256
2.72k
    mode_value.SetCompilerType(clang_uint32_type);
257
258
2.72k
    if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 4) {
259
0
      count_value.SetValueType(Value::ValueType::Scalar);
260
0
      count_value.SetCompilerType(clang_uint32_type);
261
2.72k
    } else {
262
2.72k
      count_value.SetValueType(Value::ValueType::Scalar);
263
2.72k
      count_value.SetCompilerType(clang_uint64_type);
264
2.72k
    }
265
266
2.72k
    headers_value.SetValueType(Value::ValueType::Scalar);
267
2.72k
    headers_value.SetCompilerType(clang_void_ptr_type);
268
269
2.72k
    argument_values.PushValue(mode_value);
270
2.72k
    argument_values.PushValue(count_value);
271
2.72k
    argument_values.PushValue(headers_value);
272
273
2.72k
    if (abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values)) {
274
2.72k
      uint32_t dyld_mode =
275
2.72k
          argument_values.GetValueAtIndex(0)->GetScalar().UInt(-1);
276
2.72k
      if (dyld_mode != static_cast<uint32_t>(-1)) {
277
        // Okay the mode was right, now get the number of elements, and the
278
        // array of new elements...
279
2.72k
        uint32_t image_infos_count =
280
2.72k
            argument_values.GetValueAtIndex(1)->GetScalar().UInt(-1);
281
2.72k
        if (image_infos_count != static_cast<uint32_t>(-1)) {
282
2.72k
          addr_t header_array =
283
2.72k
              argument_values.GetValueAtIndex(2)->GetScalar().ULongLong(-1);
284
2.72k
          if (header_array != static_cast<uint64_t>(-1)) {
285
2.72k
            std::vector<addr_t> image_load_addresses;
286
135k
            for (uint64_t i = 0; i < image_infos_count; 
i++133k
) {
287
133k
              Status error;
288
133k
              addr_t addr = process->ReadUnsignedIntegerFromMemory(
289
133k
                  header_array + (8 * i), 8, LLDB_INVALID_ADDRESS, error);
290
133k
              if (
addr != 133k
LLDB_INVALID_ADDRESS133k
) {
291
133k
                image_load_addresses.push_back(addr);
292
133k
              }
293
133k
            }
294
2.72k
            if (dyld_mode == 0) {
295
              // dyld_notify_adding
296
2.69k
              if (process->GetTarget().GetImages().GetSize() == 0) {
297
                // When all images have been removed, we're doing the
298
                // dyld handover from a launch-dyld to a shared-cache-dyld,
299
                // and we've just hit our one-shot address breakpoint in
300
                // the sc-dyld.  Note that the image addresses passed to
301
                // this function are inferior sizeof(void*) not uint64_t's
302
                // like our normal notification, so don't even look at
303
                // image_load_addresses.
304
305
0
                dyld_instance->ClearDYLDHandoverBreakpoint();
306
307
0
                dyld_instance->DoInitialImageFetch();
308
0
                dyld_instance->SetNotificationBreakpoint();
309
2.69k
              } else {
310
2.69k
                dyld_instance->AddBinaries(image_load_addresses);
311
2.69k
              }
312
2.69k
            } else 
if (27
dyld_mode == 127
) {
313
              // dyld_notify_removing
314
27
              dyld_instance->UnloadImages(image_load_addresses);
315
27
            } else 
if (0
dyld_mode == 20
) {
316
              // dyld_notify_remove_all
317
0
              dyld_instance->UnloadAllImages();
318
0
            } else if (dyld_mode == 3 && image_infos_count == 1) {
319
              // dyld_image_dyld_moved
320
321
0
              dyld_instance->ClearNotificationBreakpoint();
322
0
              dyld_instance->UnloadAllImages();
323
0
              dyld_instance->ClearDYLDModule();
324
0
              process->GetTarget().GetImages().Clear();
325
0
              process->GetTarget().GetSectionLoadList().Clear();
326
327
0
              addr_t all_image_infos = process->GetImageInfoAddress();
328
0
              int addr_size =
329
0
                  process->GetTarget().GetArchitecture().GetAddressByteSize();
330
0
              addr_t notification_location = all_image_infos + 4 + // version
331
0
                                             4 +        // infoArrayCount
332
0
                                             addr_size; // infoArray
333
0
              Status error;
334
0
              addr_t notification_addr =
335
0
                  process->ReadPointerFromMemory(notification_location, error);
336
0
              if (ABISP abi_sp = process->GetABI())
337
0
                notification_addr = abi_sp->FixCodeAddress(notification_addr);
338
339
0
              dyld_instance->SetDYLDHandoverBreakpoint(notification_addr);
340
0
            }
341
2.72k
          }
342
2.72k
        }
343
2.72k
      }
344
2.72k
    }
345
2.72k
  } else {
346
0
    Target &target = process->GetTarget();
347
0
    Debugger::ReportWarning(
348
0
        "no ABI plugin located for triple " +
349
0
            target.GetArchitecture().GetTriple().getTriple() +
350
0
            ": shared libraries will not be registered",
351
0
        target.GetDebugger().GetID());
352
0
  }
353
354
  // Return true to stop the target, false to just let the target run
355
2.72k
  return dyld_instance->GetStopWhenImagesChange();
356
2.72k
}
357
358
void DynamicLoaderMacOS::AddBinaries(
359
2.69k
    const std::vector<lldb::addr_t> &load_addresses) {
360
2.69k
  Log *log = GetLog(LLDBLog::DynamicLoader);
361
2.69k
  ImageInfo::collection image_infos;
362
363
2.69k
  LLDB_LOGF(log, "Adding %" PRId64 " modules.",
364
2.69k
            (uint64_t)load_addresses.size());
365
2.69k
  StructuredData::ObjectSP binaries_info_sp =
366
2.69k
      m_process->GetLoadedDynamicLibrariesInfos(load_addresses);
367
2.69k
  if (binaries_info_sp.get() && binaries_info_sp->GetAsDictionary() &&
368
2.69k
      binaries_info_sp->GetAsDictionary()->HasKey("images") &&
369
2.69k
      binaries_info_sp->GetAsDictionary()
370
2.69k
          ->GetValueForKey("images")
371
2.69k
          ->GetAsArray() &&
372
2.69k
      binaries_info_sp->GetAsDictionary()
373
2.69k
              ->GetValueForKey("images")
374
2.69k
              ->GetAsArray()
375
2.69k
              ->GetSize() == load_addresses.size()) {
376
2.69k
    if (JSONImageInformationIntoImageInfo(binaries_info_sp, image_infos)) {
377
2.69k
      UpdateSpecialBinariesFromNewImageInfos(image_infos);
378
2.69k
      AddModulesUsingImageInfos(image_infos);
379
2.69k
    }
380
2.69k
    m_dyld_image_infos_stop_id = m_process->GetStopID();
381
2.69k
  }
382
2.69k
}
383
384
// Dump the _dyld_all_image_infos members and all current image infos that we
385
// have parsed to the file handle provided.
386
0
void DynamicLoaderMacOS::PutToLog(Log *log) const {
387
0
  if (log == nullptr)
388
0
    return;
389
0
}
390
391
2.67k
bool DynamicLoaderMacOS::SetNotificationBreakpoint() {
392
2.67k
  if (m_break_id == LLDB_INVALID_BREAK_ID) {
393
2.67k
    ModuleSP dyld_sp(GetDYLDModule());
394
2.67k
    if (dyld_sp) {
395
2.67k
      bool internal = true;
396
2.67k
      bool hardware = false;
397
2.67k
      LazyBool skip_prologue = eLazyBoolNo;
398
2.67k
      FileSpecList *source_files = nullptr;
399
2.67k
      FileSpecList dyld_filelist;
400
2.67k
      dyld_filelist.Append(dyld_sp->GetFileSpec());
401
402
2.67k
      Breakpoint *breakpoint =
403
2.67k
          m_process->GetTarget()
404
2.67k
              .CreateBreakpoint(&dyld_filelist, source_files,
405
2.67k
                                "_dyld_debugger_notification",
406
2.67k
                                eFunctionNameTypeFull, eLanguageTypeC, 0,
407
2.67k
                                skip_prologue, internal, hardware)
408
2.67k
              .get();
409
2.67k
      breakpoint->SetCallback(DynamicLoaderMacOS::NotifyBreakpointHit, this,
410
2.67k
                              true);
411
2.67k
      breakpoint->SetBreakpointKind("shared-library-event");
412
2.67k
      m_break_id = breakpoint->GetID();
413
2.67k
    }
414
2.67k
  }
415
2.67k
  return m_break_id != LLDB_INVALID_BREAK_ID;
416
2.67k
}
417
418
bool DynamicLoaderMacOS::SetDYLDHandoverBreakpoint(
419
0
    addr_t notification_address) {
420
0
  if (m_dyld_handover_break_id == LLDB_INVALID_BREAK_ID) {
421
0
    BreakpointSP dyld_handover_bp = m_process->GetTarget().CreateBreakpoint(
422
0
        notification_address, true, false);
423
0
    dyld_handover_bp->SetCallback(DynamicLoaderMacOS::NotifyBreakpointHit, this,
424
0
                                  true);
425
0
    dyld_handover_bp->SetOneShot(true);
426
0
    m_dyld_handover_break_id = dyld_handover_bp->GetID();
427
0
    return true;
428
0
  }
429
0
  return false;
430
0
}
431
432
0
void DynamicLoaderMacOS::ClearDYLDHandoverBreakpoint() {
433
0
  if (LLDB_BREAK_ID_IS_VALID(m_dyld_handover_break_id))
434
0
    m_process->GetTarget().RemoveBreakpointByID(m_dyld_handover_break_id);
435
0
  m_dyld_handover_break_id = LLDB_INVALID_BREAK_ID;
436
0
}
437
438
addr_t
439
5
DynamicLoaderMacOS::GetDyldLockVariableAddressFromModule(Module *module) {
440
5
  SymbolContext sc;
441
5
  Target &target = m_process->GetTarget();
442
5
  if (Symtab *symtab = module->GetSymtab()) {
443
5
    std::vector<uint32_t> match_indexes;
444
5
    ConstString g_symbol_name("_dyld_global_lock_held");
445
5
    uint32_t num_matches = 0;
446
5
    num_matches =
447
5
        symtab->AppendSymbolIndexesWithName(g_symbol_name, match_indexes);
448
5
    if (num_matches == 1) {
449
5
      Symbol *symbol = symtab->SymbolAtIndex(match_indexes[0]);
450
5
      if (symbol &&
451
5
          (symbol->ValueIsAddress() || 
symbol->GetAddressRef().IsValid()0
)) {
452
5
        return symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
453
5
      }
454
5
    }
455
5
  }
456
0
  return LLDB_INVALID_ADDRESS;
457
5
}
458
459
//  Look for this symbol:
460
//
461
//  int __attribute__((visibility("hidden")))           _dyld_global_lock_held =
462
//  0;
463
//
464
//  in libdyld.dylib.
465
5
Status DynamicLoaderMacOS::CanLoadImage() {
466
5
  Status error;
467
5
  addr_t symbol_address = LLDB_INVALID_ADDRESS;
468
5
  ConstString g_libdyld_name("libdyld.dylib");
469
5
  Target &target = m_process->GetTarget();
470
5
  const ModuleList &target_modules = target.GetImages();
471
5
  std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
472
473
  // Find any modules named "libdyld.dylib" and look for the symbol there first
474
59
  for (ModuleSP module_sp : target.GetImages().ModulesNoLocking()) {
475
59
    if (module_sp) {
476
59
      if (module_sp->GetFileSpec().GetFilename() == g_libdyld_name) {
477
5
        symbol_address = GetDyldLockVariableAddressFromModule(module_sp.get());
478
5
        if (symbol_address != LLDB_INVALID_ADDRESS)
479
5
          break;
480
5
      }
481
59
    }
482
59
  }
483
484
  // Search through all modules looking for the symbol in them
485
5
  if (symbol_address == LLDB_INVALID_ADDRESS) {
486
0
    for (ModuleSP module_sp : target.GetImages().Modules()) {
487
0
      if (module_sp) {
488
0
        addr_t symbol_address =
489
0
            GetDyldLockVariableAddressFromModule(module_sp.get());
490
0
        if (symbol_address != LLDB_INVALID_ADDRESS)
491
0
          break;
492
0
      }
493
0
    }
494
0
  }
495
496
  // Default assumption is that it is OK to load images. Only say that we
497
  // cannot load images if we find the symbol in libdyld and it indicates that
498
  // we cannot.
499
500
5
  if (symbol_address != LLDB_INVALID_ADDRESS) {
501
5
    {
502
5
      int lock_held =
503
5
          m_process->ReadUnsignedIntegerFromMemory(symbol_address, 4, 0, error);
504
5
      if (lock_held != 0) {
505
0
        error.SetErrorString("dyld lock held - unsafe to load images.");
506
0
      }
507
5
    }
508
5
  } else {
509
    // If we were unable to find _dyld_global_lock_held in any modules, or it
510
    // is not loaded into memory yet, we may be at process startup (sitting  at
511
    // _dyld_start) - so we should not allow dlopen calls. But if we found more
512
    // than one module then we are clearly past _dyld_start so in that case
513
    // we'll default to "it's safe".
514
0
    if (target.GetImages().GetSize() <= 1)
515
0
      error.SetErrorString("could not find the dyld library or "
516
0
                           "the dyld lock symbol");
517
0
  }
518
5
  return error;
519
5
}
520
521
bool DynamicLoaderMacOS::GetSharedCacheInformation(
522
    lldb::addr_t &base_address, UUID &uuid, LazyBool &using_shared_cache,
523
0
    LazyBool &private_shared_cache) {
524
0
  base_address = LLDB_INVALID_ADDRESS;
525
0
  uuid.Clear();
526
0
  using_shared_cache = eLazyBoolCalculate;
527
0
  private_shared_cache = eLazyBoolCalculate;
528
529
0
  if (m_process) {
530
0
    StructuredData::ObjectSP info = m_process->GetSharedCacheInfo();
531
0
    StructuredData::Dictionary *info_dict = nullptr;
532
0
    if (info.get() && info->GetAsDictionary()) {
533
0
      info_dict = info->GetAsDictionary();
534
0
    }
535
536
    // {"shared_cache_base_address":140735683125248,"shared_cache_uuid
537
    // ":"DDB8D70C-
538
    // C9A2-3561-B2C8-BE48A4F33F96","no_shared_cache":false,"shared_cache_private_cache":false}
539
540
0
    if (info_dict && info_dict->HasKey("shared_cache_uuid") &&
541
0
        info_dict->HasKey("no_shared_cache") &&
542
0
        info_dict->HasKey("shared_cache_base_address")) {
543
0
      base_address = info_dict->GetValueForKey("shared_cache_base_address")
544
0
                         ->GetIntegerValue(LLDB_INVALID_ADDRESS);
545
0
      std::string uuid_str = std::string(
546
0
          info_dict->GetValueForKey("shared_cache_uuid")->GetStringValue());
547
0
      if (!uuid_str.empty())
548
0
        uuid.SetFromStringRef(uuid_str);
549
0
      if (!info_dict->GetValueForKey("no_shared_cache")->GetBooleanValue())
550
0
        using_shared_cache = eLazyBoolYes;
551
0
      else
552
0
        using_shared_cache = eLazyBoolNo;
553
0
      if (info_dict->GetValueForKey("shared_cache_private_cache")
554
0
              ->GetBooleanValue())
555
0
        private_shared_cache = eLazyBoolYes;
556
0
      else
557
0
        private_shared_cache = eLazyBoolNo;
558
559
0
      return true;
560
0
    }
561
0
  }
562
0
  return false;
563
0
}
564
565
3.61k
void DynamicLoaderMacOS::Initialize() {
566
3.61k
  PluginManager::RegisterPlugin(GetPluginNameStatic(),
567
3.61k
                                GetPluginDescriptionStatic(), CreateInstance);
568
3.61k
}
569
570
3.60k
void DynamicLoaderMacOS::Terminate() {
571
3.60k
  PluginManager::UnregisterPlugin(CreateInstance);
572
3.60k
}
573
574
3.61k
llvm::StringRef DynamicLoaderMacOS::GetPluginDescriptionStatic() {
575
3.61k
  return "Dynamic loader plug-in that watches for shared library loads/unloads "
576
3.61k
         "in MacOSX user processes.";
577
3.61k
}