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