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