1 //===-- AppleObjCRuntimeV2.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 <stdint.h>
10
11 #include <memory>
12 #include <string>
13 #include <vector>
14
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/DeclObjC.h"
17
18 #include "lldb/Host/OptionParser.h"
19 #include "lldb/Symbol/CompilerType.h"
20 #include "lldb/lldb-enumerations.h"
21
22 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
23 #include "lldb/Core/Debugger.h"
24 #include "lldb/Core/Module.h"
25 #include "lldb/Core/PluginManager.h"
26 #include "lldb/Core/Section.h"
27 #include "lldb/Core/ValueObjectConstResult.h"
28 #include "lldb/Core/ValueObjectVariable.h"
29 #include "lldb/Expression/DiagnosticManager.h"
30 #include "lldb/Expression/FunctionCaller.h"
31 #include "lldb/Expression/UtilityFunction.h"
32 #include "lldb/Interpreter/CommandObject.h"
33 #include "lldb/Interpreter/CommandObjectMultiword.h"
34 #include "lldb/Interpreter/CommandReturnObject.h"
35 #include "lldb/Interpreter/OptionArgParser.h"
36 #include "lldb/Interpreter/OptionValueBoolean.h"
37 #include "lldb/Symbol/ObjectFile.h"
38 #include "lldb/Symbol/Symbol.h"
39 #include "lldb/Symbol/TypeList.h"
40 #include "lldb/Symbol/VariableList.h"
41 #include "lldb/Target/ABI.h"
42 #include "lldb/Target/ExecutionContext.h"
43 #include "lldb/Target/Platform.h"
44 #include "lldb/Target/Process.h"
45 #include "lldb/Target/RegisterContext.h"
46 #include "lldb/Target/StackFrameRecognizer.h"
47 #include "lldb/Target/Target.h"
48 #include "lldb/Target/Thread.h"
49 #include "lldb/Utility/ConstString.h"
50 #include "lldb/Utility/Log.h"
51 #include "lldb/Utility/Scalar.h"
52 #include "lldb/Utility/Status.h"
53 #include "lldb/Utility/Stream.h"
54 #include "lldb/Utility/StreamString.h"
55 #include "lldb/Utility/Timer.h"
56
57 #include "AppleObjCClassDescriptorV2.h"
58 #include "AppleObjCDeclVendor.h"
59 #include "AppleObjCRuntimeV2.h"
60 #include "AppleObjCTrampolineHandler.h"
61 #include "AppleObjCTypeEncodingParser.h"
62
63 #include "clang/AST/ASTContext.h"
64 #include "clang/AST/DeclObjC.h"
65 #include "clang/Basic/TargetInfo.h"
66
67 #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
68
69 #include <vector>
70
71 using namespace lldb;
72 using namespace lldb_private;
73
74 char AppleObjCRuntimeV2::ID = 0;
75
76 static const char *g_get_dynamic_class_info_name =
77 "__lldb_apple_objc_v2_get_dynamic_class_info";
78 // Testing using the new C++11 raw string literals. If this breaks GCC then we
79 // will need to revert to the code above...
80 static const char *g_get_dynamic_class_info_body = R"(
81
82 extern "C"
83 {
84 size_t strlen(const char *);
85 char *strncpy (char * s1, const char * s2, size_t n);
86 int printf(const char * format, ...);
87 }
88 #define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__)
89
90 typedef struct _NXMapTable {
91 void *prototype;
92 unsigned num_classes;
93 unsigned num_buckets_minus_one;
94 void *buckets;
95 } NXMapTable;
96
97 #define NX_MAPNOTAKEY ((void *)(-1))
98
99 typedef struct BucketInfo
100 {
101 const char *name_ptr;
102 Class isa;
103 } BucketInfo;
104
105 struct ClassInfo
106 {
107 Class isa;
108 uint32_t hash;
109 } __attribute__((__packed__));
110
111 uint32_t
112 __lldb_apple_objc_v2_get_dynamic_class_info (void *gdb_objc_realized_classes_ptr,
113 void *class_infos_ptr,
114 uint32_t class_infos_byte_size,
115 uint32_t should_log)
116 {
117 DEBUG_PRINTF ("gdb_objc_realized_classes_ptr = %p\n", gdb_objc_realized_classes_ptr);
118 DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr);
119 DEBUG_PRINTF ("class_infos_byte_size = %u\n", class_infos_byte_size);
120 const NXMapTable *grc = (const NXMapTable *)gdb_objc_realized_classes_ptr;
121 if (grc)
122 {
123 const unsigned num_classes = grc->num_classes;
124 if (class_infos_ptr)
125 {
126 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
127 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
128 BucketInfo *buckets = (BucketInfo *)grc->buckets;
129
130 uint32_t idx = 0;
131 for (unsigned i=0; i<=grc->num_buckets_minus_one; ++i)
132 {
133 if (buckets[i].name_ptr != NX_MAPNOTAKEY)
134 {
135 if (idx < max_class_infos)
136 {
137 const char *s = buckets[i].name_ptr;
138 uint32_t h = 5381;
139 for (unsigned char c = *s; c; c = *++s)
140 h = ((h << 5) + h) + c;
141 class_infos[idx].hash = h;
142 class_infos[idx].isa = buckets[i].isa;
143 }
144 ++idx;
145 }
146 }
147 if (idx < max_class_infos)
148 {
149 class_infos[idx].isa = NULL;
150 class_infos[idx].hash = 0;
151 }
152 }
153 return num_classes;
154 }
155 return 0;
156 }
157
158 )";
159
160 // We'll substitute in class_getName or class_getNameRaw depending
161 // on which is present.
162 static const char *g_shared_cache_class_name_funcptr = R"(
163 extern "C"
164 {
165 const char *%s(void *objc_class);
166 const char *(*class_name_lookup_func)(void *) = %s;
167 }
168 )";
169
170 static const char *g_get_shared_cache_class_info_name =
171 "__lldb_apple_objc_v2_get_shared_cache_class_info";
172 // Testing using the new C++11 raw string literals. If this breaks GCC then we
173 // will need to revert to the code above...
174 static const char *g_get_shared_cache_class_info_body = R"(
175
176 extern "C"
177 {
178 size_t strlen(const char *);
179 char *strncpy (char * s1, const char * s2, size_t n);
180 int printf(const char * format, ...);
181 }
182
183 #define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__)
184
185
186 struct objc_classheader_t {
187 int32_t clsOffset;
188 int32_t hiOffset;
189 };
190
191 struct objc_clsopt_t {
192 uint32_t capacity;
193 uint32_t occupied;
194 uint32_t shift;
195 uint32_t mask;
196 uint32_t zero;
197 uint32_t unused;
198 uint64_t salt;
199 uint32_t scramble[256];
200 uint8_t tab[0]; // tab[mask+1]
201 // uint8_t checkbytes[capacity];
202 // int32_t offset[capacity];
203 // objc_classheader_t clsOffsets[capacity];
204 // uint32_t duplicateCount;
205 // objc_classheader_t duplicateOffsets[duplicateCount];
206 };
207
208 struct objc_opt_t {
209 uint32_t version;
210 int32_t selopt_offset;
211 int32_t headeropt_offset;
212 int32_t clsopt_offset;
213 };
214
215 struct objc_opt_v14_t {
216 uint32_t version;
217 uint32_t flags;
218 int32_t selopt_offset;
219 int32_t headeropt_offset;
220 int32_t clsopt_offset;
221 };
222
223 struct ClassInfo
224 {
225 Class isa;
226 uint32_t hash;
227 } __attribute__((__packed__));
228
229 uint32_t
230 __lldb_apple_objc_v2_get_shared_cache_class_info (void *objc_opt_ro_ptr,
231 void *class_infos_ptr,
232 uint32_t class_infos_byte_size,
233 uint32_t should_log)
234 {
235 uint32_t idx = 0;
236 DEBUG_PRINTF ("objc_opt_ro_ptr = %p\n", objc_opt_ro_ptr);
237 DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr);
238 DEBUG_PRINTF ("class_infos_byte_size = %u (%llu class infos)\n", class_infos_byte_size, (uint64_t)(class_infos_byte_size/sizeof(ClassInfo)));
239 if (objc_opt_ro_ptr)
240 {
241 const objc_opt_t *objc_opt = (objc_opt_t *)objc_opt_ro_ptr;
242 const objc_opt_v14_t* objc_opt_v14 = (objc_opt_v14_t*)objc_opt_ro_ptr;
243 const bool is_v14_format = objc_opt->version >= 14;
244 if (is_v14_format)
245 {
246 DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt_v14->version);
247 DEBUG_PRINTF ("objc_opt->flags = %u\n", objc_opt_v14->flags);
248 DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt_v14->selopt_offset);
249 DEBUG_PRINTF ("objc_opt->headeropt_offset = %d\n", objc_opt_v14->headeropt_offset);
250 DEBUG_PRINTF ("objc_opt->clsopt_offset = %d\n", objc_opt_v14->clsopt_offset);
251 }
252 else
253 {
254 DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt->version);
255 DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt->selopt_offset);
256 DEBUG_PRINTF ("objc_opt->headeropt_offset = %d\n", objc_opt->headeropt_offset);
257 DEBUG_PRINTF ("objc_opt->clsopt_offset = %d\n", objc_opt->clsopt_offset);
258 }
259 if (objc_opt->version == 12 || objc_opt->version == 13 || objc_opt->version == 14 || objc_opt->version == 15)
260 {
261 const objc_clsopt_t* clsopt = NULL;
262 if (is_v14_format)
263 clsopt = (const objc_clsopt_t*)((uint8_t *)objc_opt_v14 + objc_opt_v14->clsopt_offset);
264 else
265 clsopt = (const objc_clsopt_t*)((uint8_t *)objc_opt + objc_opt->clsopt_offset);
266 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
267 DEBUG_PRINTF("max_class_infos = %llu\n", (uint64_t)max_class_infos);
268 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
269 int32_t invalidEntryOffset = 0;
270 // this is safe to do because the version field order is invariant
271 if (objc_opt->version == 12)
272 invalidEntryOffset = 16;
273 const uint8_t *checkbytes = &clsopt->tab[clsopt->mask+1];
274 const int32_t *offsets = (const int32_t *)(checkbytes + clsopt->capacity);
275 const objc_classheader_t *classOffsets = (const objc_classheader_t *)(offsets + clsopt->capacity);
276 DEBUG_PRINTF ("clsopt->capacity = %u\n", clsopt->capacity);
277 DEBUG_PRINTF ("clsopt->mask = 0x%8.8x\n", clsopt->mask);
278 DEBUG_PRINTF ("classOffsets = %p\n", classOffsets);
279 DEBUG_PRINTF("invalidEntryOffset = %d\n", invalidEntryOffset);
280 for (uint32_t i=0; i<clsopt->capacity; ++i)
281 {
282 const int32_t clsOffset = classOffsets[i].clsOffset;
283 DEBUG_PRINTF("clsOffset[%u] = %u\n", i, clsOffset);
284 if (clsOffset & 1)
285 {
286 DEBUG_PRINTF("clsOffset & 1\n");
287 continue; // duplicate
288 }
289 else if (clsOffset == invalidEntryOffset)
290 {
291 DEBUG_PRINTF("clsOffset == invalidEntryOffset\n");
292 continue; // invalid offset
293 }
294
295 if (class_infos && idx < max_class_infos)
296 {
297 class_infos[idx].isa = (Class)((uint8_t *)clsopt + clsOffset);
298 const char *name = class_name_lookup_func (class_infos[idx].isa);
299 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name);
300 // Hash the class name so we don't have to read it
301 const char *s = name;
302 uint32_t h = 5381;
303 for (unsigned char c = *s; c; c = *++s)
304 {
305 // class_getName demangles swift names and the hash must
306 // be calculated on the mangled name. hash==0 means lldb
307 // will fetch the mangled name and compute the hash in
308 // ParseClassInfoArray.
309 if (c == '.')
310 {
311 h = 0;
312 break;
313 }
314 h = ((h << 5) + h) + c;
315 }
316 class_infos[idx].hash = h;
317 }
318 else
319 {
320 DEBUG_PRINTF("not(class_infos && idx < max_class_infos)\n");
321 }
322 ++idx;
323 }
324
325 const uint32_t *duplicate_count_ptr = (uint32_t *)&classOffsets[clsopt->capacity];
326 const uint32_t duplicate_count = *duplicate_count_ptr;
327 const objc_classheader_t *duplicateClassOffsets = (const objc_classheader_t *)(&duplicate_count_ptr[1]);
328 DEBUG_PRINTF ("duplicate_count = %u\n", duplicate_count);
329 DEBUG_PRINTF ("duplicateClassOffsets = %p\n", duplicateClassOffsets);
330 for (uint32_t i=0; i<duplicate_count; ++i)
331 {
332 const int32_t clsOffset = duplicateClassOffsets[i].clsOffset;
333 if (clsOffset & 1)
334 continue; // duplicate
335 else if (clsOffset == invalidEntryOffset)
336 continue; // invalid offset
337
338 if (class_infos && idx < max_class_infos)
339 {
340 class_infos[idx].isa = (Class)((uint8_t *)clsopt + clsOffset);
341 const char *name = class_name_lookup_func (class_infos[idx].isa);
342 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name);
343 // Hash the class name so we don't have to read it
344 const char *s = name;
345 uint32_t h = 5381;
346 for (unsigned char c = *s; c; c = *++s)
347 {
348 // class_getName demangles swift names and the hash must
349 // be calculated on the mangled name. hash==0 means lldb
350 // will fetch the mangled name and compute the hash in
351 // ParseClassInfoArray.
352 if (c == '.')
353 {
354 h = 0;
355 break;
356 }
357 h = ((h << 5) + h) + c;
358 }
359 class_infos[idx].hash = h;
360 }
361 ++idx;
362 }
363 }
364 DEBUG_PRINTF ("%u class_infos\n", idx);
365 DEBUG_PRINTF ("done\n");
366 }
367 return idx;
368 }
369
370
371 )";
372
373 static uint64_t
ExtractRuntimeGlobalSymbol(Process * process,ConstString name,const ModuleSP & module_sp,Status & error,bool read_value=true,uint8_t byte_size=0,uint64_t default_value=LLDB_INVALID_ADDRESS,SymbolType sym_type=lldb::eSymbolTypeData)374 ExtractRuntimeGlobalSymbol(Process *process, ConstString name,
375 const ModuleSP &module_sp, Status &error,
376 bool read_value = true, uint8_t byte_size = 0,
377 uint64_t default_value = LLDB_INVALID_ADDRESS,
378 SymbolType sym_type = lldb::eSymbolTypeData) {
379 if (!process) {
380 error.SetErrorString("no process");
381 return default_value;
382 }
383 if (!module_sp) {
384 error.SetErrorString("no module");
385 return default_value;
386 }
387 if (!byte_size)
388 byte_size = process->GetAddressByteSize();
389 const Symbol *symbol =
390 module_sp->FindFirstSymbolWithNameAndType(name, lldb::eSymbolTypeData);
391 if (symbol && symbol->ValueIsAddress()) {
392 lldb::addr_t symbol_load_addr =
393 symbol->GetAddressRef().GetLoadAddress(&process->GetTarget());
394 if (symbol_load_addr != LLDB_INVALID_ADDRESS) {
395 if (read_value)
396 return process->ReadUnsignedIntegerFromMemory(
397 symbol_load_addr, byte_size, default_value, error);
398 else
399 return symbol_load_addr;
400 } else {
401 error.SetErrorString("symbol address invalid");
402 return default_value;
403 }
404 } else {
405 error.SetErrorString("no symbol");
406 return default_value;
407 }
408 }
409
410 static void RegisterObjCExceptionRecognizer(Process *process);
411
AppleObjCRuntimeV2(Process * process,const ModuleSP & objc_module_sp)412 AppleObjCRuntimeV2::AppleObjCRuntimeV2(Process *process,
413 const ModuleSP &objc_module_sp)
414 : AppleObjCRuntime(process), m_get_class_info_code(),
415 m_get_class_info_args(LLDB_INVALID_ADDRESS),
416 m_get_class_info_args_mutex(), m_get_shared_cache_class_info_code(),
417 m_get_shared_cache_class_info_args(LLDB_INVALID_ADDRESS),
418 m_get_shared_cache_class_info_args_mutex(), m_decl_vendor_up(),
419 m_tagged_pointer_obfuscator(LLDB_INVALID_ADDRESS),
420 m_isa_hash_table_ptr(LLDB_INVALID_ADDRESS), m_hash_signature(),
421 m_has_object_getClass(false), m_loaded_objc_opt(false),
422 m_non_pointer_isa_cache_up(
423 NonPointerISACache::CreateInstance(*this, objc_module_sp)),
424 m_tagged_pointer_vendor_up(
425 TaggedPointerVendorV2::CreateInstance(*this, objc_module_sp)),
426 m_encoding_to_type_sp(), m_noclasses_warning_emitted(false),
427 m_CFBoolean_values() {
428 static const ConstString g_gdb_object_getClass("gdb_object_getClass");
429 m_has_object_getClass =
430 (objc_module_sp->FindFirstSymbolWithNameAndType(
431 g_gdb_object_getClass, eSymbolTypeCode) != nullptr);
432 RegisterObjCExceptionRecognizer(process);
433 }
434
GetDynamicTypeAndAddress(ValueObject & in_value,lldb::DynamicValueType use_dynamic,TypeAndOrName & class_type_or_name,Address & address,Value::ValueType & value_type)435 bool AppleObjCRuntimeV2::GetDynamicTypeAndAddress(
436 ValueObject &in_value, lldb::DynamicValueType use_dynamic,
437 TypeAndOrName &class_type_or_name, Address &address,
438 Value::ValueType &value_type) {
439 // We should never get here with a null process...
440 assert(m_process != nullptr);
441
442 // The Runtime is attached to a particular process, you shouldn't pass in a
443 // value from another process. Note, however, the process might be NULL (e.g.
444 // if the value was made with SBTarget::EvaluateExpression...) in which case
445 // it is sufficient if the target's match:
446
447 Process *process = in_value.GetProcessSP().get();
448 if (process)
449 assert(process == m_process);
450 else
451 assert(in_value.GetTargetSP().get() == m_process->CalculateTarget().get());
452
453 class_type_or_name.Clear();
454 value_type = Value::ValueType::eValueTypeScalar;
455
456 // Make sure we can have a dynamic value before starting...
457 if (CouldHaveDynamicValue(in_value)) {
458 // First job, pull out the address at 0 offset from the object That will
459 // be the ISA pointer.
460 ClassDescriptorSP objc_class_sp(GetNonKVOClassDescriptor(in_value));
461 if (objc_class_sp) {
462 const addr_t object_ptr = in_value.GetPointerValue();
463 address.SetRawAddress(object_ptr);
464
465 ConstString class_name(objc_class_sp->GetClassName());
466 class_type_or_name.SetName(class_name);
467 TypeSP type_sp(objc_class_sp->GetType());
468 if (type_sp)
469 class_type_or_name.SetTypeSP(type_sp);
470 else {
471 type_sp = LookupInCompleteClassCache(class_name);
472 if (type_sp) {
473 objc_class_sp->SetType(type_sp);
474 class_type_or_name.SetTypeSP(type_sp);
475 } else {
476 // try to go for a CompilerType at least
477 if (auto *vendor = GetDeclVendor()) {
478 auto types = vendor->FindTypes(class_name, /*max_matches*/ 1);
479 if (!types.empty())
480 class_type_or_name.SetCompilerType(types.front());
481 }
482 }
483 }
484 }
485 }
486 return !class_type_or_name.IsEmpty();
487 }
488
489 // Static Functions
CreateInstance(Process * process,LanguageType language)490 LanguageRuntime *AppleObjCRuntimeV2::CreateInstance(Process *process,
491 LanguageType language) {
492 // FIXME: This should be a MacOS or iOS process, and we need to look for the
493 // OBJC section to make
494 // sure we aren't using the V1 runtime.
495 if (language == eLanguageTypeObjC) {
496 ModuleSP objc_module_sp;
497
498 if (AppleObjCRuntime::GetObjCVersion(process, objc_module_sp) ==
499 ObjCRuntimeVersions::eAppleObjC_V2)
500 return new AppleObjCRuntimeV2(process, objc_module_sp);
501 else
502 return nullptr;
503 } else
504 return nullptr;
505 }
506
507 static constexpr OptionDefinition g_objc_classtable_dump_options[] = {
508 {LLDB_OPT_SET_ALL, false, "verbose", 'v', OptionParser::eNoArgument,
509 nullptr, {}, 0, eArgTypeNone,
510 "Print ivar and method information in detail"}};
511
512 class CommandObjectObjC_ClassTable_Dump : public CommandObjectParsed {
513 public:
514 class CommandOptions : public Options {
515 public:
CommandOptions()516 CommandOptions() : Options(), m_verbose(false, false) {}
517
518 ~CommandOptions() override = default;
519
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)520 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
521 ExecutionContext *execution_context) override {
522 Status error;
523 const int short_option = m_getopt_table[option_idx].val;
524 switch (short_option) {
525 case 'v':
526 m_verbose.SetCurrentValue(true);
527 m_verbose.SetOptionWasSet();
528 break;
529
530 default:
531 error.SetErrorStringWithFormat("unrecognized short option '%c'",
532 short_option);
533 break;
534 }
535
536 return error;
537 }
538
OptionParsingStarting(ExecutionContext * execution_context)539 void OptionParsingStarting(ExecutionContext *execution_context) override {
540 m_verbose.Clear();
541 }
542
GetDefinitions()543 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
544 return llvm::makeArrayRef(g_objc_classtable_dump_options);
545 }
546
547 OptionValueBoolean m_verbose;
548 };
549
CommandObjectObjC_ClassTable_Dump(CommandInterpreter & interpreter)550 CommandObjectObjC_ClassTable_Dump(CommandInterpreter &interpreter)
551 : CommandObjectParsed(
552 interpreter, "dump", "Dump information on Objective-C classes "
553 "known to the current process.",
554 "language objc class-table dump",
555 eCommandRequiresProcess | eCommandProcessMustBeLaunched |
556 eCommandProcessMustBePaused),
557 m_options() {
558 CommandArgumentEntry arg;
559 CommandArgumentData index_arg;
560
561 // Define the first (and only) variant of this arg.
562 index_arg.arg_type = eArgTypeRegularExpression;
563 index_arg.arg_repetition = eArgRepeatOptional;
564
565 // There is only one variant this argument could be; put it into the
566 // argument entry.
567 arg.push_back(index_arg);
568
569 // Push the data for the first argument into the m_arguments vector.
570 m_arguments.push_back(arg);
571 }
572
573 ~CommandObjectObjC_ClassTable_Dump() override = default;
574
GetOptions()575 Options *GetOptions() override { return &m_options; }
576
577 protected:
DoExecute(Args & command,CommandReturnObject & result)578 bool DoExecute(Args &command, CommandReturnObject &result) override {
579 std::unique_ptr<RegularExpression> regex_up;
580 switch (command.GetArgumentCount()) {
581 case 0:
582 break;
583 case 1: {
584 regex_up = std::make_unique<RegularExpression>(
585 llvm::StringRef::withNullAsEmpty(command.GetArgumentAtIndex(0)));
586 if (!regex_up->IsValid()) {
587 result.AppendError(
588 "invalid argument - please provide a valid regular expression");
589 result.SetStatus(lldb::eReturnStatusFailed);
590 return false;
591 }
592 break;
593 }
594 default: {
595 result.AppendError("please provide 0 or 1 arguments");
596 result.SetStatus(lldb::eReturnStatusFailed);
597 return false;
598 }
599 }
600
601 Process *process = m_exe_ctx.GetProcessPtr();
602 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
603 if (objc_runtime) {
604 auto iterators_pair = objc_runtime->GetDescriptorIteratorPair();
605 auto iterator = iterators_pair.first;
606 auto &std_out = result.GetOutputStream();
607 for (; iterator != iterators_pair.second; iterator++) {
608 if (iterator->second) {
609 const char *class_name =
610 iterator->second->GetClassName().AsCString("<unknown>");
611 if (regex_up && class_name &&
612 !regex_up->Execute(llvm::StringRef(class_name)))
613 continue;
614 std_out.Printf("isa = 0x%" PRIx64, iterator->first);
615 std_out.Printf(" name = %s", class_name);
616 std_out.Printf(" instance size = %" PRIu64,
617 iterator->second->GetInstanceSize());
618 std_out.Printf(" num ivars = %" PRIuPTR,
619 (uintptr_t)iterator->second->GetNumIVars());
620 if (auto superclass = iterator->second->GetSuperclass()) {
621 std_out.Printf(" superclass = %s",
622 superclass->GetClassName().AsCString("<unknown>"));
623 }
624 std_out.Printf("\n");
625 if (m_options.m_verbose) {
626 for (size_t i = 0; i < iterator->second->GetNumIVars(); i++) {
627 auto ivar = iterator->second->GetIVarAtIndex(i);
628 std_out.Printf(
629 " ivar name = %s type = %s size = %" PRIu64
630 " offset = %" PRId32 "\n",
631 ivar.m_name.AsCString("<unknown>"),
632 ivar.m_type.GetDisplayTypeName().AsCString("<unknown>"),
633 ivar.m_size, ivar.m_offset);
634 }
635 iterator->second->Describe(
636 nullptr,
637 [&std_out](const char *name, const char *type) -> bool {
638 std_out.Printf(" instance method name = %s type = %s\n",
639 name, type);
640 return false;
641 },
642 [&std_out](const char *name, const char *type) -> bool {
643 std_out.Printf(" class method name = %s type = %s\n", name,
644 type);
645 return false;
646 },
647 nullptr);
648 }
649 } else {
650 if (regex_up && !regex_up->Execute(llvm::StringRef()))
651 continue;
652 std_out.Printf("isa = 0x%" PRIx64 " has no associated class.\n",
653 iterator->first);
654 }
655 }
656 result.SetStatus(lldb::eReturnStatusSuccessFinishResult);
657 return true;
658 } else {
659 result.AppendError("current process has no Objective-C runtime loaded");
660 result.SetStatus(lldb::eReturnStatusFailed);
661 return false;
662 }
663 }
664
665 CommandOptions m_options;
666 };
667
668 class CommandObjectMultiwordObjC_TaggedPointer_Info
669 : public CommandObjectParsed {
670 public:
CommandObjectMultiwordObjC_TaggedPointer_Info(CommandInterpreter & interpreter)671 CommandObjectMultiwordObjC_TaggedPointer_Info(CommandInterpreter &interpreter)
672 : CommandObjectParsed(
673 interpreter, "info", "Dump information on a tagged pointer.",
674 "language objc tagged-pointer info",
675 eCommandRequiresProcess | eCommandProcessMustBeLaunched |
676 eCommandProcessMustBePaused) {
677 CommandArgumentEntry arg;
678 CommandArgumentData index_arg;
679
680 // Define the first (and only) variant of this arg.
681 index_arg.arg_type = eArgTypeAddress;
682 index_arg.arg_repetition = eArgRepeatPlus;
683
684 // There is only one variant this argument could be; put it into the
685 // argument entry.
686 arg.push_back(index_arg);
687
688 // Push the data for the first argument into the m_arguments vector.
689 m_arguments.push_back(arg);
690 }
691
692 ~CommandObjectMultiwordObjC_TaggedPointer_Info() override = default;
693
694 protected:
DoExecute(Args & command,CommandReturnObject & result)695 bool DoExecute(Args &command, CommandReturnObject &result) override {
696 if (command.GetArgumentCount() == 0) {
697 result.AppendError("this command requires arguments");
698 result.SetStatus(lldb::eReturnStatusFailed);
699 return false;
700 }
701
702 Process *process = m_exe_ctx.GetProcessPtr();
703 ExecutionContext exe_ctx(process);
704 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
705 if (objc_runtime) {
706 ObjCLanguageRuntime::TaggedPointerVendor *tagged_ptr_vendor =
707 objc_runtime->GetTaggedPointerVendor();
708 if (tagged_ptr_vendor) {
709 for (size_t i = 0; i < command.GetArgumentCount(); i++) {
710 const char *arg_str = command.GetArgumentAtIndex(i);
711 if (!arg_str)
712 continue;
713 Status error;
714 lldb::addr_t arg_addr = OptionArgParser::ToAddress(
715 &exe_ctx, arg_str, LLDB_INVALID_ADDRESS, &error);
716 if (arg_addr == 0 || arg_addr == LLDB_INVALID_ADDRESS || error.Fail())
717 continue;
718 auto descriptor_sp = tagged_ptr_vendor->GetClassDescriptor(arg_addr);
719 if (!descriptor_sp)
720 continue;
721 uint64_t info_bits = 0;
722 uint64_t value_bits = 0;
723 uint64_t payload = 0;
724 if (descriptor_sp->GetTaggedPointerInfo(&info_bits, &value_bits,
725 &payload)) {
726 result.GetOutputStream().Printf(
727 "0x%" PRIx64 " is tagged.\n\tpayload = 0x%" PRIx64
728 "\n\tvalue = 0x%" PRIx64 "\n\tinfo bits = 0x%" PRIx64
729 "\n\tclass = %s\n",
730 (uint64_t)arg_addr, payload, value_bits, info_bits,
731 descriptor_sp->GetClassName().AsCString("<unknown>"));
732 } else {
733 result.GetOutputStream().Printf("0x%" PRIx64 " is not tagged.\n",
734 (uint64_t)arg_addr);
735 }
736 }
737 } else {
738 result.AppendError("current process has no tagged pointer support");
739 result.SetStatus(lldb::eReturnStatusFailed);
740 return false;
741 }
742 result.SetStatus(lldb::eReturnStatusSuccessFinishResult);
743 return true;
744 } else {
745 result.AppendError("current process has no Objective-C runtime loaded");
746 result.SetStatus(lldb::eReturnStatusFailed);
747 return false;
748 }
749 }
750 };
751
752 class CommandObjectMultiwordObjC_ClassTable : public CommandObjectMultiword {
753 public:
CommandObjectMultiwordObjC_ClassTable(CommandInterpreter & interpreter)754 CommandObjectMultiwordObjC_ClassTable(CommandInterpreter &interpreter)
755 : CommandObjectMultiword(
756 interpreter, "class-table",
757 "Commands for operating on the Objective-C class table.",
758 "class-table <subcommand> [<subcommand-options>]") {
759 LoadSubCommand(
760 "dump",
761 CommandObjectSP(new CommandObjectObjC_ClassTable_Dump(interpreter)));
762 }
763
764 ~CommandObjectMultiwordObjC_ClassTable() override = default;
765 };
766
767 class CommandObjectMultiwordObjC_TaggedPointer : public CommandObjectMultiword {
768 public:
CommandObjectMultiwordObjC_TaggedPointer(CommandInterpreter & interpreter)769 CommandObjectMultiwordObjC_TaggedPointer(CommandInterpreter &interpreter)
770 : CommandObjectMultiword(
771 interpreter, "tagged-pointer",
772 "Commands for operating on Objective-C tagged pointers.",
773 "class-table <subcommand> [<subcommand-options>]") {
774 LoadSubCommand(
775 "info",
776 CommandObjectSP(
777 new CommandObjectMultiwordObjC_TaggedPointer_Info(interpreter)));
778 }
779
780 ~CommandObjectMultiwordObjC_TaggedPointer() override = default;
781 };
782
783 class CommandObjectMultiwordObjC : public CommandObjectMultiword {
784 public:
CommandObjectMultiwordObjC(CommandInterpreter & interpreter)785 CommandObjectMultiwordObjC(CommandInterpreter &interpreter)
786 : CommandObjectMultiword(
787 interpreter, "objc",
788 "Commands for operating on the Objective-C language runtime.",
789 "objc <subcommand> [<subcommand-options>]") {
790 LoadSubCommand("class-table",
791 CommandObjectSP(
792 new CommandObjectMultiwordObjC_ClassTable(interpreter)));
793 LoadSubCommand("tagged-pointer",
794 CommandObjectSP(new CommandObjectMultiwordObjC_TaggedPointer(
795 interpreter)));
796 }
797
798 ~CommandObjectMultiwordObjC() override = default;
799 };
800
Initialize()801 void AppleObjCRuntimeV2::Initialize() {
802 PluginManager::RegisterPlugin(
803 GetPluginNameStatic(), "Apple Objective-C Language Runtime - Version 2",
804 CreateInstance,
805 [](CommandInterpreter &interpreter) -> lldb::CommandObjectSP {
806 return CommandObjectSP(new CommandObjectMultiwordObjC(interpreter));
807 },
808 GetBreakpointExceptionPrecondition);
809 }
810
Terminate()811 void AppleObjCRuntimeV2::Terminate() {
812 PluginManager::UnregisterPlugin(CreateInstance);
813 }
814
GetPluginNameStatic()815 lldb_private::ConstString AppleObjCRuntimeV2::GetPluginNameStatic() {
816 static ConstString g_name("apple-objc-v2");
817 return g_name;
818 }
819
820 // PluginInterface protocol
GetPluginName()821 lldb_private::ConstString AppleObjCRuntimeV2::GetPluginName() {
822 return GetPluginNameStatic();
823 }
824
GetPluginVersion()825 uint32_t AppleObjCRuntimeV2::GetPluginVersion() { return 1; }
826
827 BreakpointResolverSP
CreateExceptionResolver(const BreakpointSP & bkpt,bool catch_bp,bool throw_bp)828 AppleObjCRuntimeV2::CreateExceptionResolver(const BreakpointSP &bkpt,
829 bool catch_bp, bool throw_bp) {
830 BreakpointResolverSP resolver_sp;
831
832 if (throw_bp)
833 resolver_sp = std::make_shared<BreakpointResolverName>(
834 bkpt, std::get<1>(GetExceptionThrowLocation()).AsCString(),
835 eFunctionNameTypeBase, eLanguageTypeUnknown, Breakpoint::Exact, 0,
836 eLazyBoolNo);
837 // FIXME: We don't do catch breakpoints for ObjC yet.
838 // Should there be some way for the runtime to specify what it can do in this
839 // regard?
840 return resolver_sp;
841 }
842
843 llvm::Expected<std::unique_ptr<UtilityFunction>>
CreateObjectChecker(std::string name,ExecutionContext & exe_ctx)844 AppleObjCRuntimeV2::CreateObjectChecker(std::string name,
845 ExecutionContext &exe_ctx) {
846 char check_function_code[2048];
847
848 int len = 0;
849 if (m_has_object_getClass) {
850 len = ::snprintf(check_function_code, sizeof(check_function_code), R"(
851 extern "C" void *gdb_object_getClass(void *);
852 extern "C" int printf(const char *format, ...);
853 extern "C" void
854 %s(void *$__lldb_arg_obj, void *$__lldb_arg_selector) {
855 if ($__lldb_arg_obj == (void *)0)
856 return; // nil is ok
857 if (!gdb_object_getClass($__lldb_arg_obj)) {
858 *((volatile int *)0) = 'ocgc';
859 } else if ($__lldb_arg_selector != (void *)0) {
860 signed char $responds = (signed char)
861 [(id)$__lldb_arg_obj respondsToSelector:
862 (void *) $__lldb_arg_selector];
863 if ($responds == (signed char) 0)
864 *((volatile int *)0) = 'ocgc';
865 }
866 })",
867 name.c_str());
868 } else {
869 len = ::snprintf(check_function_code, sizeof(check_function_code), R"(
870 extern "C" void *gdb_class_getClass(void *);
871 extern "C" int printf(const char *format, ...);
872 extern "C" void
873 %s(void *$__lldb_arg_obj, void *$__lldb_arg_selector) {
874 if ($__lldb_arg_obj == (void *)0)
875 return; // nil is ok
876 void **$isa_ptr = (void **)$__lldb_arg_obj;
877 if (*$isa_ptr == (void *)0 ||
878 !gdb_class_getClass(*$isa_ptr))
879 *((volatile int *)0) = 'ocgc';
880 else if ($__lldb_arg_selector != (void *)0) {
881 signed char $responds = (signed char)
882 [(id)$__lldb_arg_obj respondsToSelector:
883 (void *) $__lldb_arg_selector];
884 if ($responds == (signed char) 0)
885 *((volatile int *)0) = 'ocgc';
886 }
887 })",
888 name.c_str());
889 }
890
891 assert(len < (int)sizeof(check_function_code));
892 UNUSED_IF_ASSERT_DISABLED(len);
893
894 return GetTargetRef().CreateUtilityFunction(check_function_code, name,
895 eLanguageTypeC, exe_ctx);
896 }
897
GetByteOffsetForIvar(CompilerType & parent_ast_type,const char * ivar_name)898 size_t AppleObjCRuntimeV2::GetByteOffsetForIvar(CompilerType &parent_ast_type,
899 const char *ivar_name) {
900 uint32_t ivar_offset = LLDB_INVALID_IVAR_OFFSET;
901
902 ConstString class_name = parent_ast_type.GetTypeName();
903 if (!class_name.IsEmpty() && ivar_name && ivar_name[0]) {
904 // Make the objective C V2 mangled name for the ivar offset from the class
905 // name and ivar name
906 std::string buffer("OBJC_IVAR_$_");
907 buffer.append(class_name.AsCString());
908 buffer.push_back('.');
909 buffer.append(ivar_name);
910 ConstString ivar_const_str(buffer.c_str());
911
912 // Try to get the ivar offset address from the symbol table first using the
913 // name we created above
914 SymbolContextList sc_list;
915 Target &target = m_process->GetTarget();
916 target.GetImages().FindSymbolsWithNameAndType(ivar_const_str,
917 eSymbolTypeObjCIVar, sc_list);
918
919 addr_t ivar_offset_address = LLDB_INVALID_ADDRESS;
920
921 Status error;
922 SymbolContext ivar_offset_symbol;
923 if (sc_list.GetSize() == 1 &&
924 sc_list.GetContextAtIndex(0, ivar_offset_symbol)) {
925 if (ivar_offset_symbol.symbol)
926 ivar_offset_address =
927 ivar_offset_symbol.symbol->GetLoadAddress(&target);
928 }
929
930 // If we didn't get the ivar offset address from the symbol table, fall
931 // back to getting it from the runtime
932 if (ivar_offset_address == LLDB_INVALID_ADDRESS)
933 ivar_offset_address = LookupRuntimeSymbol(ivar_const_str);
934
935 if (ivar_offset_address != LLDB_INVALID_ADDRESS)
936 ivar_offset = m_process->ReadUnsignedIntegerFromMemory(
937 ivar_offset_address, 4, LLDB_INVALID_IVAR_OFFSET, error);
938 }
939 return ivar_offset;
940 }
941
942 // tagged pointers are special not-a-real-pointer values that contain both type
943 // and value information this routine attempts to check with as little
944 // computational effort as possible whether something could possibly be a
945 // tagged pointer - false positives are possible but false negatives shouldn't
IsTaggedPointer(addr_t ptr)946 bool AppleObjCRuntimeV2::IsTaggedPointer(addr_t ptr) {
947 if (!m_tagged_pointer_vendor_up)
948 return false;
949 return m_tagged_pointer_vendor_up->IsPossibleTaggedPointer(ptr);
950 }
951
952 class RemoteNXMapTable {
953 public:
RemoteNXMapTable()954 RemoteNXMapTable()
955 : m_count(0), m_num_buckets_minus_one(0),
956 m_buckets_ptr(LLDB_INVALID_ADDRESS), m_process(nullptr),
957 m_end_iterator(*this, -1), m_load_addr(LLDB_INVALID_ADDRESS),
958 m_map_pair_size(0), m_invalid_key(0) {}
959
Dump()960 void Dump() {
961 printf("RemoteNXMapTable.m_load_addr = 0x%" PRIx64 "\n", m_load_addr);
962 printf("RemoteNXMapTable.m_count = %u\n", m_count);
963 printf("RemoteNXMapTable.m_num_buckets_minus_one = %u\n",
964 m_num_buckets_minus_one);
965 printf("RemoteNXMapTable.m_buckets_ptr = 0x%" PRIX64 "\n", m_buckets_ptr);
966 }
967
ParseHeader(Process * process,lldb::addr_t load_addr)968 bool ParseHeader(Process *process, lldb::addr_t load_addr) {
969 m_process = process;
970 m_load_addr = load_addr;
971 m_map_pair_size = m_process->GetAddressByteSize() * 2;
972 m_invalid_key =
973 m_process->GetAddressByteSize() == 8 ? UINT64_MAX : UINT32_MAX;
974 Status err;
975
976 // This currently holds true for all platforms we support, but we might
977 // need to change this to use get the actually byte size of "unsigned" from
978 // the target AST...
979 const uint32_t unsigned_byte_size = sizeof(uint32_t);
980 // Skip the prototype as we don't need it (const struct
981 // +NXMapTablePrototype *prototype)
982
983 bool success = true;
984 if (load_addr == LLDB_INVALID_ADDRESS)
985 success = false;
986 else {
987 lldb::addr_t cursor = load_addr + m_process->GetAddressByteSize();
988
989 // unsigned count;
990 m_count = m_process->ReadUnsignedIntegerFromMemory(
991 cursor, unsigned_byte_size, 0, err);
992 if (m_count) {
993 cursor += unsigned_byte_size;
994
995 // unsigned nbBucketsMinusOne;
996 m_num_buckets_minus_one = m_process->ReadUnsignedIntegerFromMemory(
997 cursor, unsigned_byte_size, 0, err);
998 cursor += unsigned_byte_size;
999
1000 // void *buckets;
1001 m_buckets_ptr = m_process->ReadPointerFromMemory(cursor, err);
1002
1003 success = m_count > 0 && m_buckets_ptr != LLDB_INVALID_ADDRESS;
1004 }
1005 }
1006
1007 if (!success) {
1008 m_count = 0;
1009 m_num_buckets_minus_one = 0;
1010 m_buckets_ptr = LLDB_INVALID_ADDRESS;
1011 }
1012 return success;
1013 }
1014
1015 // const_iterator mimics NXMapState and its code comes from NXInitMapState
1016 // and NXNextMapState.
1017 typedef std::pair<ConstString, ObjCLanguageRuntime::ObjCISA> element;
1018
1019 friend class const_iterator;
1020 class const_iterator {
1021 public:
const_iterator(RemoteNXMapTable & parent,int index)1022 const_iterator(RemoteNXMapTable &parent, int index)
1023 : m_parent(parent), m_index(index) {
1024 AdvanceToValidIndex();
1025 }
1026
const_iterator(const const_iterator & rhs)1027 const_iterator(const const_iterator &rhs)
1028 : m_parent(rhs.m_parent), m_index(rhs.m_index) {
1029 // AdvanceToValidIndex() has been called by rhs already.
1030 }
1031
operator =(const const_iterator & rhs)1032 const_iterator &operator=(const const_iterator &rhs) {
1033 // AdvanceToValidIndex() has been called by rhs already.
1034 assert(&m_parent == &rhs.m_parent);
1035 m_index = rhs.m_index;
1036 return *this;
1037 }
1038
operator ==(const const_iterator & rhs) const1039 bool operator==(const const_iterator &rhs) const {
1040 if (&m_parent != &rhs.m_parent)
1041 return false;
1042 if (m_index != rhs.m_index)
1043 return false;
1044
1045 return true;
1046 }
1047
operator !=(const const_iterator & rhs) const1048 bool operator!=(const const_iterator &rhs) const {
1049 return !(operator==(rhs));
1050 }
1051
operator ++()1052 const_iterator &operator++() {
1053 AdvanceToValidIndex();
1054 return *this;
1055 }
1056
operator *() const1057 const element operator*() const {
1058 if (m_index == -1) {
1059 // TODO find a way to make this an error, but not an assert
1060 return element();
1061 }
1062
1063 lldb::addr_t pairs_ptr = m_parent.m_buckets_ptr;
1064 size_t map_pair_size = m_parent.m_map_pair_size;
1065 lldb::addr_t pair_ptr = pairs_ptr + (m_index * map_pair_size);
1066
1067 Status err;
1068
1069 lldb::addr_t key =
1070 m_parent.m_process->ReadPointerFromMemory(pair_ptr, err);
1071 if (!err.Success())
1072 return element();
1073 lldb::addr_t value = m_parent.m_process->ReadPointerFromMemory(
1074 pair_ptr + m_parent.m_process->GetAddressByteSize(), err);
1075 if (!err.Success())
1076 return element();
1077
1078 std::string key_string;
1079
1080 m_parent.m_process->ReadCStringFromMemory(key, key_string, err);
1081 if (!err.Success())
1082 return element();
1083
1084 return element(ConstString(key_string.c_str()),
1085 (ObjCLanguageRuntime::ObjCISA)value);
1086 }
1087
1088 private:
AdvanceToValidIndex()1089 void AdvanceToValidIndex() {
1090 if (m_index == -1)
1091 return;
1092
1093 const lldb::addr_t pairs_ptr = m_parent.m_buckets_ptr;
1094 const size_t map_pair_size = m_parent.m_map_pair_size;
1095 const lldb::addr_t invalid_key = m_parent.m_invalid_key;
1096 Status err;
1097
1098 while (m_index--) {
1099 lldb::addr_t pair_ptr = pairs_ptr + (m_index * map_pair_size);
1100 lldb::addr_t key =
1101 m_parent.m_process->ReadPointerFromMemory(pair_ptr, err);
1102
1103 if (!err.Success()) {
1104 m_index = -1;
1105 return;
1106 }
1107
1108 if (key != invalid_key)
1109 return;
1110 }
1111 }
1112 RemoteNXMapTable &m_parent;
1113 int m_index;
1114 };
1115
begin()1116 const_iterator begin() {
1117 return const_iterator(*this, m_num_buckets_minus_one + 1);
1118 }
1119
end()1120 const_iterator end() { return m_end_iterator; }
1121
GetCount() const1122 uint32_t GetCount() const { return m_count; }
1123
GetBucketCount() const1124 uint32_t GetBucketCount() const { return m_num_buckets_minus_one; }
1125
GetBucketDataPointer() const1126 lldb::addr_t GetBucketDataPointer() const { return m_buckets_ptr; }
1127
GetTableLoadAddress() const1128 lldb::addr_t GetTableLoadAddress() const { return m_load_addr; }
1129
1130 private:
1131 // contents of _NXMapTable struct
1132 uint32_t m_count;
1133 uint32_t m_num_buckets_minus_one;
1134 lldb::addr_t m_buckets_ptr;
1135 lldb_private::Process *m_process;
1136 const_iterator m_end_iterator;
1137 lldb::addr_t m_load_addr;
1138 size_t m_map_pair_size;
1139 lldb::addr_t m_invalid_key;
1140 };
1141
HashTableSignature()1142 AppleObjCRuntimeV2::HashTableSignature::HashTableSignature()
1143 : m_count(0), m_num_buckets(0), m_buckets_ptr(0) {}
1144
UpdateSignature(const RemoteNXMapTable & hash_table)1145 void AppleObjCRuntimeV2::HashTableSignature::UpdateSignature(
1146 const RemoteNXMapTable &hash_table) {
1147 m_count = hash_table.GetCount();
1148 m_num_buckets = hash_table.GetBucketCount();
1149 m_buckets_ptr = hash_table.GetBucketDataPointer();
1150 }
1151
NeedsUpdate(Process * process,AppleObjCRuntimeV2 * runtime,RemoteNXMapTable & hash_table)1152 bool AppleObjCRuntimeV2::HashTableSignature::NeedsUpdate(
1153 Process *process, AppleObjCRuntimeV2 *runtime,
1154 RemoteNXMapTable &hash_table) {
1155 if (!hash_table.ParseHeader(process, runtime->GetISAHashTablePointer())) {
1156 return false; // Failed to parse the header, no need to update anything
1157 }
1158
1159 // Check with out current signature and return true if the count, number of
1160 // buckets or the hash table address changes.
1161 if (m_count == hash_table.GetCount() &&
1162 m_num_buckets == hash_table.GetBucketCount() &&
1163 m_buckets_ptr == hash_table.GetBucketDataPointer()) {
1164 // Hash table hasn't changed
1165 return false;
1166 }
1167 // Hash table data has changed, we need to update
1168 return true;
1169 }
1170
1171 ObjCLanguageRuntime::ClassDescriptorSP
GetClassDescriptorFromISA(ObjCISA isa)1172 AppleObjCRuntimeV2::GetClassDescriptorFromISA(ObjCISA isa) {
1173 ObjCLanguageRuntime::ClassDescriptorSP class_descriptor_sp;
1174 if (m_non_pointer_isa_cache_up)
1175 class_descriptor_sp = m_non_pointer_isa_cache_up->GetClassDescriptor(isa);
1176 if (!class_descriptor_sp)
1177 class_descriptor_sp = ObjCLanguageRuntime::GetClassDescriptorFromISA(isa);
1178 return class_descriptor_sp;
1179 }
1180
1181 ObjCLanguageRuntime::ClassDescriptorSP
GetClassDescriptor(ValueObject & valobj)1182 AppleObjCRuntimeV2::GetClassDescriptor(ValueObject &valobj) {
1183 ClassDescriptorSP objc_class_sp;
1184 if (valobj.IsBaseClass()) {
1185 ValueObject *parent = valobj.GetParent();
1186 // if I am my own parent, bail out of here fast..
1187 if (parent && parent != &valobj) {
1188 ClassDescriptorSP parent_descriptor_sp = GetClassDescriptor(*parent);
1189 if (parent_descriptor_sp)
1190 return parent_descriptor_sp->GetSuperclass();
1191 }
1192 return nullptr;
1193 }
1194 // if we get an invalid VO (which might still happen when playing around with
1195 // pointers returned by the expression parser, don't consider this a valid
1196 // ObjC object)
1197 if (!valobj.GetCompilerType().IsValid())
1198 return objc_class_sp;
1199 addr_t isa_pointer = valobj.GetPointerValue();
1200
1201 // tagged pointer
1202 if (IsTaggedPointer(isa_pointer))
1203 return m_tagged_pointer_vendor_up->GetClassDescriptor(isa_pointer);
1204 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
1205
1206 Process *process = exe_ctx.GetProcessPtr();
1207 if (!process)
1208 return objc_class_sp;
1209
1210 Status error;
1211 ObjCISA isa = process->ReadPointerFromMemory(isa_pointer, error);
1212 if (isa == LLDB_INVALID_ADDRESS)
1213 return objc_class_sp;
1214
1215 objc_class_sp = GetClassDescriptorFromISA(isa);
1216 if (isa && !objc_class_sp) {
1217 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS |
1218 LIBLLDB_LOG_TYPES));
1219 LLDB_LOGF(log,
1220 "0x%" PRIx64
1221 ": AppleObjCRuntimeV2::GetClassDescriptor() ISA was "
1222 "not in class descriptor cache 0x%" PRIx64,
1223 isa_pointer, isa);
1224 }
1225 return objc_class_sp;
1226 }
1227
GetTaggedPointerObfuscator()1228 lldb::addr_t AppleObjCRuntimeV2::GetTaggedPointerObfuscator() {
1229 if (m_tagged_pointer_obfuscator != LLDB_INVALID_ADDRESS)
1230 return m_tagged_pointer_obfuscator;
1231
1232
1233 Process *process = GetProcess();
1234 ModuleSP objc_module_sp(GetObjCModule());
1235
1236 if (!objc_module_sp)
1237 return LLDB_INVALID_ADDRESS;
1238
1239 static ConstString g_gdb_objc_obfuscator("objc_debug_taggedpointer_obfuscator");
1240
1241 const Symbol *symbol = objc_module_sp->FindFirstSymbolWithNameAndType(
1242 g_gdb_objc_obfuscator, lldb::eSymbolTypeAny);
1243 if (symbol) {
1244 lldb::addr_t g_gdb_obj_obfuscator_ptr =
1245 symbol->GetLoadAddress(&process->GetTarget());
1246
1247 if (g_gdb_obj_obfuscator_ptr != LLDB_INVALID_ADDRESS) {
1248 Status error;
1249 m_tagged_pointer_obfuscator = process->ReadPointerFromMemory(
1250 g_gdb_obj_obfuscator_ptr, error);
1251 }
1252 }
1253 // If we don't have a correct value at this point, there must be no obfuscation.
1254 if (m_tagged_pointer_obfuscator == LLDB_INVALID_ADDRESS)
1255 m_tagged_pointer_obfuscator = 0;
1256
1257 return m_tagged_pointer_obfuscator;
1258 }
1259
GetISAHashTablePointer()1260 lldb::addr_t AppleObjCRuntimeV2::GetISAHashTablePointer() {
1261 if (m_isa_hash_table_ptr == LLDB_INVALID_ADDRESS) {
1262 Process *process = GetProcess();
1263
1264 ModuleSP objc_module_sp(GetObjCModule());
1265
1266 if (!objc_module_sp)
1267 return LLDB_INVALID_ADDRESS;
1268
1269 static ConstString g_gdb_objc_realized_classes("gdb_objc_realized_classes");
1270
1271 const Symbol *symbol = objc_module_sp->FindFirstSymbolWithNameAndType(
1272 g_gdb_objc_realized_classes, lldb::eSymbolTypeAny);
1273 if (symbol) {
1274 lldb::addr_t gdb_objc_realized_classes_ptr =
1275 symbol->GetLoadAddress(&process->GetTarget());
1276
1277 if (gdb_objc_realized_classes_ptr != LLDB_INVALID_ADDRESS) {
1278 Status error;
1279 m_isa_hash_table_ptr = process->ReadPointerFromMemory(
1280 gdb_objc_realized_classes_ptr, error);
1281 }
1282 }
1283 }
1284 return m_isa_hash_table_ptr;
1285 }
1286
1287 AppleObjCRuntimeV2::DescriptorMapUpdateResult
UpdateISAToDescriptorMapDynamic(RemoteNXMapTable & hash_table)1288 AppleObjCRuntimeV2::UpdateISAToDescriptorMapDynamic(
1289 RemoteNXMapTable &hash_table) {
1290 Process *process = GetProcess();
1291
1292 if (process == nullptr)
1293 return DescriptorMapUpdateResult::Fail();
1294
1295 uint32_t num_class_infos = 0;
1296
1297 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_TYPES));
1298
1299 ExecutionContext exe_ctx;
1300
1301 ThreadSP thread_sp = process->GetThreadList().GetExpressionExecutionThread();
1302
1303 if (!thread_sp)
1304 return DescriptorMapUpdateResult::Fail();
1305
1306 thread_sp->CalculateExecutionContext(exe_ctx);
1307 TypeSystemClang *ast =
1308 ScratchTypeSystemClang::GetForTarget(process->GetTarget());
1309
1310 if (!ast)
1311 return DescriptorMapUpdateResult::Fail();
1312
1313 Address function_address;
1314
1315 const uint32_t addr_size = process->GetAddressByteSize();
1316
1317 Status err;
1318
1319 // Read the total number of classes from the hash table
1320 const uint32_t num_classes = hash_table.GetCount();
1321 if (num_classes == 0) {
1322 LLDB_LOGF(log, "No dynamic classes found in gdb_objc_realized_classes.");
1323 return DescriptorMapUpdateResult::Success(0);
1324 }
1325
1326 // Make some types for our arguments
1327 CompilerType clang_uint32_t_type =
1328 ast->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
1329 CompilerType clang_void_pointer_type =
1330 ast->GetBasicType(eBasicTypeVoid).GetPointerType();
1331
1332 ValueList arguments;
1333 FunctionCaller *get_class_info_function = nullptr;
1334
1335 if (!m_get_class_info_code) {
1336 auto utility_fn_or_error = GetTargetRef().CreateUtilityFunction(
1337 g_get_dynamic_class_info_body, g_get_dynamic_class_info_name,
1338 eLanguageTypeC, exe_ctx);
1339 if (!utility_fn_or_error) {
1340 LLDB_LOG_ERROR(
1341 log, utility_fn_or_error.takeError(),
1342 "Failed to get utility function for implementation lookup: {0}");
1343 return DescriptorMapUpdateResult::Fail();
1344 }
1345 m_get_class_info_code = std::move(*utility_fn_or_error);
1346
1347 // Next make the runner function for our implementation utility function.
1348 Value value;
1349 value.SetValueType(Value::eValueTypeScalar);
1350 value.SetCompilerType(clang_void_pointer_type);
1351 arguments.PushValue(value);
1352 arguments.PushValue(value);
1353
1354 value.SetValueType(Value::eValueTypeScalar);
1355 value.SetCompilerType(clang_uint32_t_type);
1356 arguments.PushValue(value);
1357 arguments.PushValue(value);
1358
1359 Status error;
1360 get_class_info_function = m_get_class_info_code->MakeFunctionCaller(
1361 clang_uint32_t_type, arguments, thread_sp, error);
1362
1363 if (error.Fail()) {
1364 LLDB_LOGF(log,
1365 "Failed to make function caller for implementation lookup: %s.",
1366 error.AsCString());
1367 return DescriptorMapUpdateResult::Fail();
1368 }
1369 } else {
1370 get_class_info_function = m_get_class_info_code->GetFunctionCaller();
1371 if (!get_class_info_function) {
1372 LLDB_LOGF(log, "Failed to get implementation lookup function caller.");
1373 return DescriptorMapUpdateResult::Fail();
1374 }
1375 arguments = get_class_info_function->GetArgumentValues();
1376 }
1377
1378 DiagnosticManager diagnostics;
1379
1380 const uint32_t class_info_byte_size = addr_size + 4;
1381 const uint32_t class_infos_byte_size = num_classes * class_info_byte_size;
1382 lldb::addr_t class_infos_addr = process->AllocateMemory(
1383 class_infos_byte_size, ePermissionsReadable | ePermissionsWritable, err);
1384
1385 if (class_infos_addr == LLDB_INVALID_ADDRESS) {
1386 LLDB_LOGF(log,
1387 "unable to allocate %" PRIu32
1388 " bytes in process for shared cache read",
1389 class_infos_byte_size);
1390 return DescriptorMapUpdateResult::Fail();
1391 }
1392
1393 std::lock_guard<std::mutex> guard(m_get_class_info_args_mutex);
1394
1395 // Fill in our function argument values
1396 arguments.GetValueAtIndex(0)->GetScalar() = hash_table.GetTableLoadAddress();
1397 arguments.GetValueAtIndex(1)->GetScalar() = class_infos_addr;
1398 arguments.GetValueAtIndex(2)->GetScalar() = class_infos_byte_size;
1399
1400 // Only dump the runtime classes from the expression evaluation if the log is
1401 // verbose:
1402 Log *type_log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES);
1403 bool dump_log = type_log && type_log->GetVerbose();
1404
1405 arguments.GetValueAtIndex(3)->GetScalar() = dump_log ? 1 : 0;
1406
1407 bool success = false;
1408
1409 diagnostics.Clear();
1410
1411 // Write our function arguments into the process so we can run our function
1412 if (get_class_info_function->WriteFunctionArguments(
1413 exe_ctx, m_get_class_info_args, arguments, diagnostics)) {
1414 EvaluateExpressionOptions options;
1415 options.SetUnwindOnError(true);
1416 options.SetTryAllThreads(false);
1417 options.SetStopOthers(true);
1418 options.SetIgnoreBreakpoints(true);
1419 options.SetTimeout(process->GetUtilityExpressionTimeout());
1420 options.SetIsForUtilityExpr(true);
1421
1422 Value return_value;
1423 return_value.SetValueType(Value::eValueTypeScalar);
1424 return_value.SetCompilerType(clang_uint32_t_type);
1425 return_value.GetScalar() = 0;
1426
1427 diagnostics.Clear();
1428
1429 // Run the function
1430 ExpressionResults results = get_class_info_function->ExecuteFunction(
1431 exe_ctx, &m_get_class_info_args, options, diagnostics, return_value);
1432
1433 if (results == eExpressionCompleted) {
1434 // The result is the number of ClassInfo structures that were filled in
1435 num_class_infos = return_value.GetScalar().ULong();
1436 LLDB_LOGF(log, "Discovered %u ObjC classes\n", num_class_infos);
1437 if (num_class_infos > 0) {
1438 // Read the ClassInfo structures
1439 DataBufferHeap buffer(num_class_infos * class_info_byte_size, 0);
1440 if (process->ReadMemory(class_infos_addr, buffer.GetBytes(),
1441 buffer.GetByteSize(),
1442 err) == buffer.GetByteSize()) {
1443 DataExtractor class_infos_data(buffer.GetBytes(),
1444 buffer.GetByteSize(),
1445 process->GetByteOrder(), addr_size);
1446 ParseClassInfoArray(class_infos_data, num_class_infos);
1447 }
1448 }
1449 success = true;
1450 } else {
1451 if (log) {
1452 LLDB_LOGF(log, "Error evaluating our find class name function.");
1453 diagnostics.Dump(log);
1454 }
1455 }
1456 } else {
1457 if (log) {
1458 LLDB_LOGF(log, "Error writing function arguments.");
1459 diagnostics.Dump(log);
1460 }
1461 }
1462
1463 // Deallocate the memory we allocated for the ClassInfo array
1464 process->DeallocateMemory(class_infos_addr);
1465
1466 return DescriptorMapUpdateResult(success, num_class_infos);
1467 }
1468
ParseClassInfoArray(const DataExtractor & data,uint32_t num_class_infos)1469 uint32_t AppleObjCRuntimeV2::ParseClassInfoArray(const DataExtractor &data,
1470 uint32_t num_class_infos) {
1471 // Parses an array of "num_class_infos" packed ClassInfo structures:
1472 //
1473 // struct ClassInfo
1474 // {
1475 // Class isa;
1476 // uint32_t hash;
1477 // } __attribute__((__packed__));
1478
1479 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES));
1480 bool should_log = log && log->GetVerbose();
1481
1482 uint32_t num_parsed = 0;
1483
1484 // Iterate through all ClassInfo structures
1485 lldb::offset_t offset = 0;
1486 for (uint32_t i = 0; i < num_class_infos; ++i) {
1487 ObjCISA isa = data.GetAddress(&offset);
1488
1489 if (isa == 0) {
1490 if (should_log)
1491 LLDB_LOGF(
1492 log, "AppleObjCRuntimeV2 found NULL isa, ignoring this class info");
1493 continue;
1494 }
1495 // Check if we already know about this ISA, if we do, the info will never
1496 // change, so we can just skip it.
1497 if (ISAIsCached(isa)) {
1498 if (should_log)
1499 LLDB_LOGF(log,
1500 "AppleObjCRuntimeV2 found cached isa=0x%" PRIx64
1501 ", ignoring this class info",
1502 isa);
1503 offset += 4;
1504 } else {
1505 // Read the 32 bit hash for the class name
1506 const uint32_t name_hash = data.GetU32(&offset);
1507 ClassDescriptorSP descriptor_sp(
1508 new ClassDescriptorV2(*this, isa, nullptr));
1509
1510 // The code in g_get_shared_cache_class_info_body sets the value of the hash
1511 // to 0 to signal a demangled symbol. We use class_getName() in that code to
1512 // find the class name, but this returns a demangled name for Swift symbols.
1513 // For those symbols, recompute the hash here by reading their name from the
1514 // runtime.
1515 if (name_hash)
1516 AddClass(isa, descriptor_sp, name_hash);
1517 else
1518 AddClass(isa, descriptor_sp, descriptor_sp->GetClassName().AsCString(nullptr));
1519 num_parsed++;
1520 if (should_log)
1521 LLDB_LOGF(log,
1522 "AppleObjCRuntimeV2 added isa=0x%" PRIx64
1523 ", hash=0x%8.8x, name=%s",
1524 isa, name_hash,
1525 descriptor_sp->GetClassName().AsCString("<unknown>"));
1526 }
1527 }
1528 if (should_log)
1529 LLDB_LOGF(log, "AppleObjCRuntimeV2 parsed %" PRIu32 " class infos",
1530 num_parsed);
1531 return num_parsed;
1532 }
1533
1534 AppleObjCRuntimeV2::DescriptorMapUpdateResult
UpdateISAToDescriptorMapSharedCache()1535 AppleObjCRuntimeV2::UpdateISAToDescriptorMapSharedCache() {
1536 Process *process = GetProcess();
1537
1538 if (process == nullptr)
1539 return DescriptorMapUpdateResult::Fail();
1540
1541 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_TYPES));
1542
1543 ExecutionContext exe_ctx;
1544
1545 ThreadSP thread_sp = process->GetThreadList().GetExpressionExecutionThread();
1546
1547 if (!thread_sp)
1548 return DescriptorMapUpdateResult::Fail();
1549
1550 thread_sp->CalculateExecutionContext(exe_ctx);
1551 TypeSystemClang *ast =
1552 ScratchTypeSystemClang::GetForTarget(process->GetTarget());
1553
1554 if (!ast)
1555 return DescriptorMapUpdateResult::Fail();
1556
1557 Address function_address;
1558
1559 const uint32_t addr_size = process->GetAddressByteSize();
1560
1561 Status err;
1562
1563 uint32_t num_class_infos = 0;
1564
1565 const lldb::addr_t objc_opt_ptr = GetSharedCacheReadOnlyAddress();
1566
1567 if (objc_opt_ptr == LLDB_INVALID_ADDRESS)
1568 return DescriptorMapUpdateResult::Fail();
1569
1570 const uint32_t num_classes = 128 * 1024;
1571
1572 // Make some types for our arguments
1573 CompilerType clang_uint32_t_type =
1574 ast->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
1575 CompilerType clang_void_pointer_type =
1576 ast->GetBasicType(eBasicTypeVoid).GetPointerType();
1577
1578 ValueList arguments;
1579 FunctionCaller *get_shared_cache_class_info_function = nullptr;
1580
1581 if (!m_get_shared_cache_class_info_code) {
1582 Status error;
1583
1584 // If the inferior objc.dylib has the class_getNameRaw function,
1585 // use that in our jitted expression. Else fall back to the old
1586 // class_getName.
1587 static ConstString g_class_getName_symbol_name("class_getName");
1588 static ConstString g_class_getNameRaw_symbol_name("objc_debug_class_getNameRaw");
1589 ConstString class_name_getter_function_name = g_class_getName_symbol_name;
1590
1591 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
1592 if (objc_runtime) {
1593 const ModuleList &images = process->GetTarget().GetImages();
1594 std::lock_guard<std::recursive_mutex> guard(images.GetMutex());
1595 for (size_t i = 0; i < images.GetSize(); ++i) {
1596 lldb::ModuleSP mod_sp = images.GetModuleAtIndexUnlocked(i);
1597 if (objc_runtime->IsModuleObjCLibrary(mod_sp)) {
1598 const Symbol *symbol =
1599 mod_sp->FindFirstSymbolWithNameAndType(g_class_getNameRaw_symbol_name,
1600 lldb::eSymbolTypeCode);
1601 if (symbol &&
1602 (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) {
1603 class_name_getter_function_name = g_class_getNameRaw_symbol_name;
1604 }
1605 }
1606 }
1607 }
1608
1609 // Substitute in the correct class_getName / class_getNameRaw function name,
1610 // concatenate the two parts of our expression text. The format string
1611 // has two %s's, so provide the name twice.
1612 std::string shared_class_expression;
1613 llvm::raw_string_ostream(shared_class_expression) << llvm::format(
1614 g_shared_cache_class_name_funcptr,
1615 class_name_getter_function_name.AsCString(),
1616 class_name_getter_function_name.AsCString());
1617
1618 shared_class_expression += g_get_shared_cache_class_info_body;
1619
1620 auto utility_fn_or_error = exe_ctx.GetTargetRef().CreateUtilityFunction(
1621 std::move(shared_class_expression), g_get_shared_cache_class_info_name,
1622 eLanguageTypeC, exe_ctx);
1623 if (!utility_fn_or_error) {
1624 LLDB_LOG_ERROR(
1625 log, utility_fn_or_error.takeError(),
1626 "Failed to get utility function for implementation lookup: {0}");
1627 return DescriptorMapUpdateResult::Fail();
1628 }
1629
1630 m_get_shared_cache_class_info_code = std::move(*utility_fn_or_error);
1631
1632 // Next make the function caller for our implementation utility function.
1633 Value value;
1634 value.SetValueType(Value::eValueTypeScalar);
1635 value.SetCompilerType(clang_void_pointer_type);
1636 arguments.PushValue(value);
1637 arguments.PushValue(value);
1638
1639 value.SetValueType(Value::eValueTypeScalar);
1640 value.SetCompilerType(clang_uint32_t_type);
1641 arguments.PushValue(value);
1642 arguments.PushValue(value);
1643
1644 get_shared_cache_class_info_function =
1645 m_get_shared_cache_class_info_code->MakeFunctionCaller(
1646 clang_uint32_t_type, arguments, thread_sp, error);
1647
1648 if (get_shared_cache_class_info_function == nullptr)
1649 return DescriptorMapUpdateResult::Fail();
1650
1651 } else {
1652 get_shared_cache_class_info_function =
1653 m_get_shared_cache_class_info_code->GetFunctionCaller();
1654 if (get_shared_cache_class_info_function == nullptr)
1655 return DescriptorMapUpdateResult::Fail();
1656 arguments = get_shared_cache_class_info_function->GetArgumentValues();
1657 }
1658
1659 DiagnosticManager diagnostics;
1660
1661 const uint32_t class_info_byte_size = addr_size + 4;
1662 const uint32_t class_infos_byte_size = num_classes * class_info_byte_size;
1663 lldb::addr_t class_infos_addr = process->AllocateMemory(
1664 class_infos_byte_size, ePermissionsReadable | ePermissionsWritable, err);
1665
1666 if (class_infos_addr == LLDB_INVALID_ADDRESS) {
1667 LLDB_LOGF(log,
1668 "unable to allocate %" PRIu32
1669 " bytes in process for shared cache read",
1670 class_infos_byte_size);
1671 return DescriptorMapUpdateResult::Fail();
1672 }
1673
1674 std::lock_guard<std::mutex> guard(m_get_shared_cache_class_info_args_mutex);
1675
1676 // Fill in our function argument values
1677 arguments.GetValueAtIndex(0)->GetScalar() = objc_opt_ptr;
1678 arguments.GetValueAtIndex(1)->GetScalar() = class_infos_addr;
1679 arguments.GetValueAtIndex(2)->GetScalar() = class_infos_byte_size;
1680 // Only dump the runtime classes from the expression evaluation if the log is
1681 // verbose:
1682 Log *type_log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES);
1683 bool dump_log = type_log && type_log->GetVerbose();
1684
1685 arguments.GetValueAtIndex(3)->GetScalar() = dump_log ? 1 : 0;
1686
1687 bool success = false;
1688
1689 diagnostics.Clear();
1690
1691 // Write our function arguments into the process so we can run our function
1692 if (get_shared_cache_class_info_function->WriteFunctionArguments(
1693 exe_ctx, m_get_shared_cache_class_info_args, arguments,
1694 diagnostics)) {
1695 EvaluateExpressionOptions options;
1696 options.SetUnwindOnError(true);
1697 options.SetTryAllThreads(false);
1698 options.SetStopOthers(true);
1699 options.SetIgnoreBreakpoints(true);
1700 options.SetTimeout(process->GetUtilityExpressionTimeout());
1701 options.SetIsForUtilityExpr(true);
1702
1703 Value return_value;
1704 return_value.SetValueType(Value::eValueTypeScalar);
1705 return_value.SetCompilerType(clang_uint32_t_type);
1706 return_value.GetScalar() = 0;
1707
1708 diagnostics.Clear();
1709
1710 // Run the function
1711 ExpressionResults results =
1712 get_shared_cache_class_info_function->ExecuteFunction(
1713 exe_ctx, &m_get_shared_cache_class_info_args, options, diagnostics,
1714 return_value);
1715
1716 if (results == eExpressionCompleted) {
1717 // The result is the number of ClassInfo structures that were filled in
1718 num_class_infos = return_value.GetScalar().ULong();
1719 LLDB_LOGF(log, "Discovered %u ObjC classes in shared cache\n",
1720 num_class_infos);
1721 assert(num_class_infos <= num_classes);
1722 if (num_class_infos > 0) {
1723 if (num_class_infos > num_classes) {
1724 num_class_infos = num_classes;
1725
1726 success = false;
1727 } else {
1728 success = true;
1729 }
1730
1731 // Read the ClassInfo structures
1732 DataBufferHeap buffer(num_class_infos * class_info_byte_size, 0);
1733 if (process->ReadMemory(class_infos_addr, buffer.GetBytes(),
1734 buffer.GetByteSize(),
1735 err) == buffer.GetByteSize()) {
1736 DataExtractor class_infos_data(buffer.GetBytes(),
1737 buffer.GetByteSize(),
1738 process->GetByteOrder(), addr_size);
1739
1740 ParseClassInfoArray(class_infos_data, num_class_infos);
1741 }
1742 } else {
1743 success = true;
1744 }
1745 } else {
1746 if (log) {
1747 LLDB_LOGF(log, "Error evaluating our find class name function.");
1748 diagnostics.Dump(log);
1749 }
1750 }
1751 } else {
1752 if (log) {
1753 LLDB_LOGF(log, "Error writing function arguments.");
1754 diagnostics.Dump(log);
1755 }
1756 }
1757
1758 // Deallocate the memory we allocated for the ClassInfo array
1759 process->DeallocateMemory(class_infos_addr);
1760
1761 return DescriptorMapUpdateResult(success, num_class_infos);
1762 }
1763
UpdateISAToDescriptorMapFromMemory(RemoteNXMapTable & hash_table)1764 bool AppleObjCRuntimeV2::UpdateISAToDescriptorMapFromMemory(
1765 RemoteNXMapTable &hash_table) {
1766 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_TYPES));
1767
1768 Process *process = GetProcess();
1769
1770 if (process == nullptr)
1771 return false;
1772
1773 uint32_t num_map_table_isas = 0;
1774
1775 ModuleSP objc_module_sp(GetObjCModule());
1776
1777 if (objc_module_sp) {
1778 for (RemoteNXMapTable::element elt : hash_table) {
1779 ++num_map_table_isas;
1780
1781 if (ISAIsCached(elt.second))
1782 continue;
1783
1784 ClassDescriptorSP descriptor_sp = ClassDescriptorSP(
1785 new ClassDescriptorV2(*this, elt.second, elt.first.AsCString()));
1786
1787 if (log && log->GetVerbose())
1788 LLDB_LOGF(log,
1789 "AppleObjCRuntimeV2 added (ObjCISA)0x%" PRIx64
1790 " (%s) from dynamic table to isa->descriptor cache",
1791 elt.second, elt.first.AsCString());
1792
1793 AddClass(elt.second, descriptor_sp, elt.first.AsCString());
1794 }
1795 }
1796
1797 return num_map_table_isas > 0;
1798 }
1799
GetSharedCacheReadOnlyAddress()1800 lldb::addr_t AppleObjCRuntimeV2::GetSharedCacheReadOnlyAddress() {
1801 Process *process = GetProcess();
1802
1803 if (process) {
1804 ModuleSP objc_module_sp(GetObjCModule());
1805
1806 if (objc_module_sp) {
1807 ObjectFile *objc_object = objc_module_sp->GetObjectFile();
1808
1809 if (objc_object) {
1810 SectionList *section_list = objc_module_sp->GetSectionList();
1811
1812 if (section_list) {
1813 SectionSP text_segment_sp(
1814 section_list->FindSectionByName(ConstString("__TEXT")));
1815
1816 if (text_segment_sp) {
1817 SectionSP objc_opt_section_sp(
1818 text_segment_sp->GetChildren().FindSectionByName(
1819 ConstString("__objc_opt_ro")));
1820
1821 if (objc_opt_section_sp) {
1822 return objc_opt_section_sp->GetLoadBaseAddress(
1823 &process->GetTarget());
1824 }
1825 }
1826 }
1827 }
1828 }
1829 }
1830 return LLDB_INVALID_ADDRESS;
1831 }
1832
UpdateISAToDescriptorMapIfNeeded()1833 void AppleObjCRuntimeV2::UpdateISAToDescriptorMapIfNeeded() {
1834 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_TYPES));
1835
1836 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1837 Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
1838
1839 // Else we need to check with our process to see when the map was updated.
1840 Process *process = GetProcess();
1841
1842 if (process) {
1843 RemoteNXMapTable hash_table;
1844
1845 // Update the process stop ID that indicates the last time we updated the
1846 // map, whether it was successful or not.
1847 m_isa_to_descriptor_stop_id = process->GetStopID();
1848
1849 if (!m_hash_signature.NeedsUpdate(process, this, hash_table))
1850 return;
1851
1852 m_hash_signature.UpdateSignature(hash_table);
1853
1854 // Grab the dynamically loaded objc classes from the hash table in memory
1855 DescriptorMapUpdateResult dynamic_update_result =
1856 UpdateISAToDescriptorMapDynamic(hash_table);
1857
1858 // Now get the objc classes that are baked into the Objective-C runtime in
1859 // the shared cache, but only once per process as this data never changes
1860 if (!m_loaded_objc_opt) {
1861 // it is legitimately possible for the shared cache to be empty - in that
1862 // case, the dynamic hash table will contain all the class information we
1863 // need; the situation we're trying to detect is one where we aren't
1864 // seeing class information from the runtime - in order to detect that
1865 // vs. just the shared cache being empty or sparsely populated, we set an
1866 // arbitrary (very low) threshold for the number of classes that we want
1867 // to see in a "good" scenario - anything below that is suspicious
1868 // (Foundation alone has thousands of classes)
1869 const uint32_t num_classes_to_warn_at = 500;
1870
1871 DescriptorMapUpdateResult shared_cache_update_result =
1872 UpdateISAToDescriptorMapSharedCache();
1873
1874 LLDB_LOGF(log,
1875 "attempted to read objc class data - results: "
1876 "[dynamic_update]: ran: %s, count: %" PRIu32
1877 " [shared_cache_update]: ran: %s, count: %" PRIu32,
1878 dynamic_update_result.m_update_ran ? "yes" : "no",
1879 dynamic_update_result.m_num_found,
1880 shared_cache_update_result.m_update_ran ? "yes" : "no",
1881 shared_cache_update_result.m_num_found);
1882
1883 // warn if:
1884 // - we could not run either expression
1885 // - we found fewer than num_classes_to_warn_at classes total
1886 if ((!shared_cache_update_result.m_update_ran) ||
1887 (!dynamic_update_result.m_update_ran))
1888 WarnIfNoClassesCached(
1889 SharedCacheWarningReason::eExpressionExecutionFailure);
1890 else if (dynamic_update_result.m_num_found +
1891 shared_cache_update_result.m_num_found <
1892 num_classes_to_warn_at)
1893 WarnIfNoClassesCached(SharedCacheWarningReason::eNotEnoughClassesRead);
1894 else
1895 m_loaded_objc_opt = true;
1896 }
1897 } else {
1898 m_isa_to_descriptor_stop_id = UINT32_MAX;
1899 }
1900 }
1901
DoesProcessHaveSharedCache(Process & process)1902 static bool DoesProcessHaveSharedCache(Process &process) {
1903 PlatformSP platform_sp = process.GetTarget().GetPlatform();
1904 if (!platform_sp)
1905 return true; // this should not happen
1906
1907 ConstString platform_plugin_name = platform_sp->GetPluginName();
1908 if (platform_plugin_name) {
1909 llvm::StringRef platform_plugin_name_sr =
1910 platform_plugin_name.GetStringRef();
1911 if (platform_plugin_name_sr.endswith("-simulator"))
1912 return false;
1913 }
1914
1915 return true;
1916 }
1917
WarnIfNoClassesCached(SharedCacheWarningReason reason)1918 void AppleObjCRuntimeV2::WarnIfNoClassesCached(
1919 SharedCacheWarningReason reason) {
1920 if (m_noclasses_warning_emitted)
1921 return;
1922
1923 if (GetProcess() && !DoesProcessHaveSharedCache(*GetProcess())) {
1924 // Simulators do not have the objc_opt_ro class table so don't actually
1925 // complain to the user
1926 m_noclasses_warning_emitted = true;
1927 return;
1928 }
1929
1930 Debugger &debugger(GetProcess()->GetTarget().GetDebugger());
1931 if (auto stream = debugger.GetAsyncOutputStream()) {
1932 switch (reason) {
1933 case SharedCacheWarningReason::eNotEnoughClassesRead:
1934 stream->PutCString("warning: could not find Objective-C class data in "
1935 "the process. This may reduce the quality of type "
1936 "information available.\n");
1937 m_noclasses_warning_emitted = true;
1938 break;
1939 case SharedCacheWarningReason::eExpressionExecutionFailure:
1940 stream->PutCString("warning: could not execute support code to read "
1941 "Objective-C class data in the process. This may "
1942 "reduce the quality of type information available.\n");
1943 m_noclasses_warning_emitted = true;
1944 break;
1945 }
1946 }
1947 }
1948
GetDeclVendor()1949 DeclVendor *AppleObjCRuntimeV2::GetDeclVendor() {
1950 if (!m_decl_vendor_up)
1951 m_decl_vendor_up = std::make_unique<AppleObjCDeclVendor>(*this);
1952
1953 return m_decl_vendor_up.get();
1954 }
1955
LookupRuntimeSymbol(ConstString name)1956 lldb::addr_t AppleObjCRuntimeV2::LookupRuntimeSymbol(ConstString name) {
1957 lldb::addr_t ret = LLDB_INVALID_ADDRESS;
1958
1959 const char *name_cstr = name.AsCString();
1960
1961 if (name_cstr) {
1962 llvm::StringRef name_strref(name_cstr);
1963
1964 llvm::StringRef ivar_prefix("OBJC_IVAR_$_");
1965 llvm::StringRef class_prefix("OBJC_CLASS_$_");
1966
1967 if (name_strref.startswith(ivar_prefix)) {
1968 llvm::StringRef ivar_skipped_prefix =
1969 name_strref.substr(ivar_prefix.size());
1970 std::pair<llvm::StringRef, llvm::StringRef> class_and_ivar =
1971 ivar_skipped_prefix.split('.');
1972
1973 if (class_and_ivar.first.size() && class_and_ivar.second.size()) {
1974 const ConstString class_name_cs(class_and_ivar.first);
1975 ClassDescriptorSP descriptor =
1976 ObjCLanguageRuntime::GetClassDescriptorFromClassName(class_name_cs);
1977
1978 if (descriptor) {
1979 const ConstString ivar_name_cs(class_and_ivar.second);
1980 const char *ivar_name_cstr = ivar_name_cs.AsCString();
1981
1982 auto ivar_func = [&ret, ivar_name_cstr](
1983 const char *name, const char *type, lldb::addr_t offset_addr,
1984 uint64_t size) -> lldb::addr_t {
1985 if (!strcmp(name, ivar_name_cstr)) {
1986 ret = offset_addr;
1987 return true;
1988 }
1989 return false;
1990 };
1991
1992 descriptor->Describe(
1993 std::function<void(ObjCISA)>(nullptr),
1994 std::function<bool(const char *, const char *)>(nullptr),
1995 std::function<bool(const char *, const char *)>(nullptr),
1996 ivar_func);
1997 }
1998 }
1999 } else if (name_strref.startswith(class_prefix)) {
2000 llvm::StringRef class_skipped_prefix =
2001 name_strref.substr(class_prefix.size());
2002 const ConstString class_name_cs(class_skipped_prefix);
2003 ClassDescriptorSP descriptor =
2004 GetClassDescriptorFromClassName(class_name_cs);
2005
2006 if (descriptor)
2007 ret = descriptor->GetISA();
2008 }
2009 }
2010
2011 return ret;
2012 }
2013
2014 AppleObjCRuntimeV2::NonPointerISACache *
CreateInstance(AppleObjCRuntimeV2 & runtime,const lldb::ModuleSP & objc_module_sp)2015 AppleObjCRuntimeV2::NonPointerISACache::CreateInstance(
2016 AppleObjCRuntimeV2 &runtime, const lldb::ModuleSP &objc_module_sp) {
2017 Process *process(runtime.GetProcess());
2018
2019 Status error;
2020
2021 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES));
2022
2023 auto objc_debug_isa_magic_mask = ExtractRuntimeGlobalSymbol(
2024 process, ConstString("objc_debug_isa_magic_mask"), objc_module_sp, error);
2025 if (error.Fail())
2026 return nullptr;
2027
2028 auto objc_debug_isa_magic_value = ExtractRuntimeGlobalSymbol(
2029 process, ConstString("objc_debug_isa_magic_value"), objc_module_sp,
2030 error);
2031 if (error.Fail())
2032 return nullptr;
2033
2034 auto objc_debug_isa_class_mask = ExtractRuntimeGlobalSymbol(
2035 process, ConstString("objc_debug_isa_class_mask"), objc_module_sp, error);
2036 if (error.Fail())
2037 return nullptr;
2038
2039 if (log)
2040 log->PutCString("AOCRT::NPI: Found all the non-indexed ISA masks");
2041
2042 bool foundError = false;
2043 auto objc_debug_indexed_isa_magic_mask = ExtractRuntimeGlobalSymbol(
2044 process, ConstString("objc_debug_indexed_isa_magic_mask"), objc_module_sp,
2045 error);
2046 foundError |= error.Fail();
2047
2048 auto objc_debug_indexed_isa_magic_value = ExtractRuntimeGlobalSymbol(
2049 process, ConstString("objc_debug_indexed_isa_magic_value"),
2050 objc_module_sp, error);
2051 foundError |= error.Fail();
2052
2053 auto objc_debug_indexed_isa_index_mask = ExtractRuntimeGlobalSymbol(
2054 process, ConstString("objc_debug_indexed_isa_index_mask"), objc_module_sp,
2055 error);
2056 foundError |= error.Fail();
2057
2058 auto objc_debug_indexed_isa_index_shift = ExtractRuntimeGlobalSymbol(
2059 process, ConstString("objc_debug_indexed_isa_index_shift"),
2060 objc_module_sp, error);
2061 foundError |= error.Fail();
2062
2063 auto objc_indexed_classes =
2064 ExtractRuntimeGlobalSymbol(process, ConstString("objc_indexed_classes"),
2065 objc_module_sp, error, false);
2066 foundError |= error.Fail();
2067
2068 if (log)
2069 log->PutCString("AOCRT::NPI: Found all the indexed ISA masks");
2070
2071 // we might want to have some rules to outlaw these other values (e.g if the
2072 // mask is zero but the value is non-zero, ...)
2073
2074 return new NonPointerISACache(
2075 runtime, objc_module_sp, objc_debug_isa_class_mask,
2076 objc_debug_isa_magic_mask, objc_debug_isa_magic_value,
2077 objc_debug_indexed_isa_magic_mask, objc_debug_indexed_isa_magic_value,
2078 objc_debug_indexed_isa_index_mask, objc_debug_indexed_isa_index_shift,
2079 foundError ? 0 : objc_indexed_classes);
2080 }
2081
2082 AppleObjCRuntimeV2::TaggedPointerVendorV2 *
CreateInstance(AppleObjCRuntimeV2 & runtime,const lldb::ModuleSP & objc_module_sp)2083 AppleObjCRuntimeV2::TaggedPointerVendorV2::CreateInstance(
2084 AppleObjCRuntimeV2 &runtime, const lldb::ModuleSP &objc_module_sp) {
2085 Process *process(runtime.GetProcess());
2086
2087 Status error;
2088
2089 auto objc_debug_taggedpointer_mask = ExtractRuntimeGlobalSymbol(
2090 process, ConstString("objc_debug_taggedpointer_mask"), objc_module_sp,
2091 error);
2092 if (error.Fail())
2093 return new TaggedPointerVendorLegacy(runtime);
2094
2095 auto objc_debug_taggedpointer_slot_shift = ExtractRuntimeGlobalSymbol(
2096 process, ConstString("objc_debug_taggedpointer_slot_shift"),
2097 objc_module_sp, error, true, 4);
2098 if (error.Fail())
2099 return new TaggedPointerVendorLegacy(runtime);
2100
2101 auto objc_debug_taggedpointer_slot_mask = ExtractRuntimeGlobalSymbol(
2102 process, ConstString("objc_debug_taggedpointer_slot_mask"),
2103 objc_module_sp, error, true, 4);
2104 if (error.Fail())
2105 return new TaggedPointerVendorLegacy(runtime);
2106
2107 auto objc_debug_taggedpointer_payload_lshift = ExtractRuntimeGlobalSymbol(
2108 process, ConstString("objc_debug_taggedpointer_payload_lshift"),
2109 objc_module_sp, error, true, 4);
2110 if (error.Fail())
2111 return new TaggedPointerVendorLegacy(runtime);
2112
2113 auto objc_debug_taggedpointer_payload_rshift = ExtractRuntimeGlobalSymbol(
2114 process, ConstString("objc_debug_taggedpointer_payload_rshift"),
2115 objc_module_sp, error, true, 4);
2116 if (error.Fail())
2117 return new TaggedPointerVendorLegacy(runtime);
2118
2119 auto objc_debug_taggedpointer_classes = ExtractRuntimeGlobalSymbol(
2120 process, ConstString("objc_debug_taggedpointer_classes"), objc_module_sp,
2121 error, false);
2122 if (error.Fail())
2123 return new TaggedPointerVendorLegacy(runtime);
2124
2125 // try to detect the "extended tagged pointer" variables - if any are
2126 // missing, use the non-extended vendor
2127 do {
2128 auto objc_debug_taggedpointer_ext_mask = ExtractRuntimeGlobalSymbol(
2129 process, ConstString("objc_debug_taggedpointer_ext_mask"),
2130 objc_module_sp, error);
2131 if (error.Fail())
2132 break;
2133
2134 auto objc_debug_taggedpointer_ext_slot_shift = ExtractRuntimeGlobalSymbol(
2135 process, ConstString("objc_debug_taggedpointer_ext_slot_shift"),
2136 objc_module_sp, error, true, 4);
2137 if (error.Fail())
2138 break;
2139
2140 auto objc_debug_taggedpointer_ext_slot_mask = ExtractRuntimeGlobalSymbol(
2141 process, ConstString("objc_debug_taggedpointer_ext_slot_mask"),
2142 objc_module_sp, error, true, 4);
2143 if (error.Fail())
2144 break;
2145
2146 auto objc_debug_taggedpointer_ext_classes = ExtractRuntimeGlobalSymbol(
2147 process, ConstString("objc_debug_taggedpointer_ext_classes"),
2148 objc_module_sp, error, false);
2149 if (error.Fail())
2150 break;
2151
2152 auto objc_debug_taggedpointer_ext_payload_lshift =
2153 ExtractRuntimeGlobalSymbol(
2154 process, ConstString("objc_debug_taggedpointer_ext_payload_lshift"),
2155 objc_module_sp, error, true, 4);
2156 if (error.Fail())
2157 break;
2158
2159 auto objc_debug_taggedpointer_ext_payload_rshift =
2160 ExtractRuntimeGlobalSymbol(
2161 process, ConstString("objc_debug_taggedpointer_ext_payload_rshift"),
2162 objc_module_sp, error, true, 4);
2163 if (error.Fail())
2164 break;
2165
2166 return new TaggedPointerVendorExtended(
2167 runtime, objc_debug_taggedpointer_mask,
2168 objc_debug_taggedpointer_ext_mask, objc_debug_taggedpointer_slot_shift,
2169 objc_debug_taggedpointer_ext_slot_shift,
2170 objc_debug_taggedpointer_slot_mask,
2171 objc_debug_taggedpointer_ext_slot_mask,
2172 objc_debug_taggedpointer_payload_lshift,
2173 objc_debug_taggedpointer_payload_rshift,
2174 objc_debug_taggedpointer_ext_payload_lshift,
2175 objc_debug_taggedpointer_ext_payload_rshift,
2176 objc_debug_taggedpointer_classes, objc_debug_taggedpointer_ext_classes);
2177 } while (false);
2178
2179 // we might want to have some rules to outlaw these values (e.g if the
2180 // table's address is zero)
2181
2182 return new TaggedPointerVendorRuntimeAssisted(
2183 runtime, objc_debug_taggedpointer_mask,
2184 objc_debug_taggedpointer_slot_shift, objc_debug_taggedpointer_slot_mask,
2185 objc_debug_taggedpointer_payload_lshift,
2186 objc_debug_taggedpointer_payload_rshift,
2187 objc_debug_taggedpointer_classes);
2188 }
2189
IsPossibleTaggedPointer(lldb::addr_t ptr)2190 bool AppleObjCRuntimeV2::TaggedPointerVendorLegacy::IsPossibleTaggedPointer(
2191 lldb::addr_t ptr) {
2192 return (ptr & 1);
2193 }
2194
2195 ObjCLanguageRuntime::ClassDescriptorSP
GetClassDescriptor(lldb::addr_t ptr)2196 AppleObjCRuntimeV2::TaggedPointerVendorLegacy::GetClassDescriptor(
2197 lldb::addr_t ptr) {
2198 if (!IsPossibleTaggedPointer(ptr))
2199 return ObjCLanguageRuntime::ClassDescriptorSP();
2200
2201 uint32_t foundation_version = m_runtime.GetFoundationVersion();
2202
2203 if (foundation_version == LLDB_INVALID_MODULE_VERSION)
2204 return ObjCLanguageRuntime::ClassDescriptorSP();
2205
2206 uint64_t class_bits = (ptr & 0xE) >> 1;
2207 ConstString name;
2208
2209 static ConstString g_NSAtom("NSAtom");
2210 static ConstString g_NSNumber("NSNumber");
2211 static ConstString g_NSDateTS("NSDateTS");
2212 static ConstString g_NSManagedObject("NSManagedObject");
2213 static ConstString g_NSDate("NSDate");
2214
2215 if (foundation_version >= 900) {
2216 switch (class_bits) {
2217 case 0:
2218 name = g_NSAtom;
2219 break;
2220 case 3:
2221 name = g_NSNumber;
2222 break;
2223 case 4:
2224 name = g_NSDateTS;
2225 break;
2226 case 5:
2227 name = g_NSManagedObject;
2228 break;
2229 case 6:
2230 name = g_NSDate;
2231 break;
2232 default:
2233 return ObjCLanguageRuntime::ClassDescriptorSP();
2234 }
2235 } else {
2236 switch (class_bits) {
2237 case 1:
2238 name = g_NSNumber;
2239 break;
2240 case 5:
2241 name = g_NSManagedObject;
2242 break;
2243 case 6:
2244 name = g_NSDate;
2245 break;
2246 case 7:
2247 name = g_NSDateTS;
2248 break;
2249 default:
2250 return ObjCLanguageRuntime::ClassDescriptorSP();
2251 }
2252 }
2253
2254 lldb::addr_t unobfuscated = ptr ^ m_runtime.GetTaggedPointerObfuscator();
2255 return ClassDescriptorSP(new ClassDescriptorV2Tagged(name, unobfuscated));
2256 }
2257
2258 AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted::
TaggedPointerVendorRuntimeAssisted(AppleObjCRuntimeV2 & runtime,uint64_t objc_debug_taggedpointer_mask,uint32_t objc_debug_taggedpointer_slot_shift,uint32_t objc_debug_taggedpointer_slot_mask,uint32_t objc_debug_taggedpointer_payload_lshift,uint32_t objc_debug_taggedpointer_payload_rshift,lldb::addr_t objc_debug_taggedpointer_classes)2259 TaggedPointerVendorRuntimeAssisted(
2260 AppleObjCRuntimeV2 &runtime, uint64_t objc_debug_taggedpointer_mask,
2261 uint32_t objc_debug_taggedpointer_slot_shift,
2262 uint32_t objc_debug_taggedpointer_slot_mask,
2263 uint32_t objc_debug_taggedpointer_payload_lshift,
2264 uint32_t objc_debug_taggedpointer_payload_rshift,
2265 lldb::addr_t objc_debug_taggedpointer_classes)
2266 : TaggedPointerVendorV2(runtime), m_cache(),
2267 m_objc_debug_taggedpointer_mask(objc_debug_taggedpointer_mask),
2268 m_objc_debug_taggedpointer_slot_shift(
2269 objc_debug_taggedpointer_slot_shift),
2270 m_objc_debug_taggedpointer_slot_mask(objc_debug_taggedpointer_slot_mask),
2271 m_objc_debug_taggedpointer_payload_lshift(
2272 objc_debug_taggedpointer_payload_lshift),
2273 m_objc_debug_taggedpointer_payload_rshift(
2274 objc_debug_taggedpointer_payload_rshift),
2275 m_objc_debug_taggedpointer_classes(objc_debug_taggedpointer_classes) {}
2276
2277 bool AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted::
IsPossibleTaggedPointer(lldb::addr_t ptr)2278 IsPossibleTaggedPointer(lldb::addr_t ptr) {
2279 return (ptr & m_objc_debug_taggedpointer_mask) != 0;
2280 }
2281
2282 ObjCLanguageRuntime::ClassDescriptorSP
GetClassDescriptor(lldb::addr_t ptr)2283 AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted::GetClassDescriptor(
2284 lldb::addr_t ptr) {
2285 ClassDescriptorSP actual_class_descriptor_sp;
2286 uint64_t data_payload;
2287 uint64_t unobfuscated = (ptr) ^ m_runtime.GetTaggedPointerObfuscator();
2288
2289 if (!IsPossibleTaggedPointer(unobfuscated))
2290 return ObjCLanguageRuntime::ClassDescriptorSP();
2291
2292 uintptr_t slot = (ptr >> m_objc_debug_taggedpointer_slot_shift) &
2293 m_objc_debug_taggedpointer_slot_mask;
2294
2295 CacheIterator iterator = m_cache.find(slot), end = m_cache.end();
2296 if (iterator != end) {
2297 actual_class_descriptor_sp = iterator->second;
2298 } else {
2299 Process *process(m_runtime.GetProcess());
2300 uintptr_t slot_ptr = slot * process->GetAddressByteSize() +
2301 m_objc_debug_taggedpointer_classes;
2302 Status error;
2303 uintptr_t slot_data = process->ReadPointerFromMemory(slot_ptr, error);
2304 if (error.Fail() || slot_data == 0 ||
2305 slot_data == uintptr_t(LLDB_INVALID_ADDRESS))
2306 return nullptr;
2307 actual_class_descriptor_sp =
2308 m_runtime.GetClassDescriptorFromISA((ObjCISA)slot_data);
2309 if (!actual_class_descriptor_sp)
2310 return ObjCLanguageRuntime::ClassDescriptorSP();
2311 m_cache[slot] = actual_class_descriptor_sp;
2312 }
2313
2314 data_payload =
2315 (((uint64_t)unobfuscated << m_objc_debug_taggedpointer_payload_lshift) >>
2316 m_objc_debug_taggedpointer_payload_rshift);
2317
2318 return ClassDescriptorSP(
2319 new ClassDescriptorV2Tagged(actual_class_descriptor_sp, data_payload));
2320 }
2321
TaggedPointerVendorExtended(AppleObjCRuntimeV2 & runtime,uint64_t objc_debug_taggedpointer_mask,uint64_t objc_debug_taggedpointer_ext_mask,uint32_t objc_debug_taggedpointer_slot_shift,uint32_t objc_debug_taggedpointer_ext_slot_shift,uint32_t objc_debug_taggedpointer_slot_mask,uint32_t objc_debug_taggedpointer_ext_slot_mask,uint32_t objc_debug_taggedpointer_payload_lshift,uint32_t objc_debug_taggedpointer_payload_rshift,uint32_t objc_debug_taggedpointer_ext_payload_lshift,uint32_t objc_debug_taggedpointer_ext_payload_rshift,lldb::addr_t objc_debug_taggedpointer_classes,lldb::addr_t objc_debug_taggedpointer_ext_classes)2322 AppleObjCRuntimeV2::TaggedPointerVendorExtended::TaggedPointerVendorExtended(
2323 AppleObjCRuntimeV2 &runtime, uint64_t objc_debug_taggedpointer_mask,
2324 uint64_t objc_debug_taggedpointer_ext_mask,
2325 uint32_t objc_debug_taggedpointer_slot_shift,
2326 uint32_t objc_debug_taggedpointer_ext_slot_shift,
2327 uint32_t objc_debug_taggedpointer_slot_mask,
2328 uint32_t objc_debug_taggedpointer_ext_slot_mask,
2329 uint32_t objc_debug_taggedpointer_payload_lshift,
2330 uint32_t objc_debug_taggedpointer_payload_rshift,
2331 uint32_t objc_debug_taggedpointer_ext_payload_lshift,
2332 uint32_t objc_debug_taggedpointer_ext_payload_rshift,
2333 lldb::addr_t objc_debug_taggedpointer_classes,
2334 lldb::addr_t objc_debug_taggedpointer_ext_classes)
2335 : TaggedPointerVendorRuntimeAssisted(
2336 runtime, objc_debug_taggedpointer_mask,
2337 objc_debug_taggedpointer_slot_shift,
2338 objc_debug_taggedpointer_slot_mask,
2339 objc_debug_taggedpointer_payload_lshift,
2340 objc_debug_taggedpointer_payload_rshift,
2341 objc_debug_taggedpointer_classes),
2342 m_ext_cache(),
2343 m_objc_debug_taggedpointer_ext_mask(objc_debug_taggedpointer_ext_mask),
2344 m_objc_debug_taggedpointer_ext_slot_shift(
2345 objc_debug_taggedpointer_ext_slot_shift),
2346 m_objc_debug_taggedpointer_ext_slot_mask(
2347 objc_debug_taggedpointer_ext_slot_mask),
2348 m_objc_debug_taggedpointer_ext_payload_lshift(
2349 objc_debug_taggedpointer_ext_payload_lshift),
2350 m_objc_debug_taggedpointer_ext_payload_rshift(
2351 objc_debug_taggedpointer_ext_payload_rshift),
2352 m_objc_debug_taggedpointer_ext_classes(
2353 objc_debug_taggedpointer_ext_classes) {}
2354
2355 bool AppleObjCRuntimeV2::TaggedPointerVendorExtended::
IsPossibleExtendedTaggedPointer(lldb::addr_t ptr)2356 IsPossibleExtendedTaggedPointer(lldb::addr_t ptr) {
2357 if (!IsPossibleTaggedPointer(ptr))
2358 return false;
2359
2360 if (m_objc_debug_taggedpointer_ext_mask == 0)
2361 return false;
2362
2363 return ((ptr & m_objc_debug_taggedpointer_ext_mask) ==
2364 m_objc_debug_taggedpointer_ext_mask);
2365 }
2366
2367 ObjCLanguageRuntime::ClassDescriptorSP
GetClassDescriptor(lldb::addr_t ptr)2368 AppleObjCRuntimeV2::TaggedPointerVendorExtended::GetClassDescriptor(
2369 lldb::addr_t ptr) {
2370 ClassDescriptorSP actual_class_descriptor_sp;
2371 uint64_t data_payload;
2372 uint64_t unobfuscated = (ptr) ^ m_runtime.GetTaggedPointerObfuscator();
2373
2374 if (!IsPossibleTaggedPointer(unobfuscated))
2375 return ObjCLanguageRuntime::ClassDescriptorSP();
2376
2377 if (!IsPossibleExtendedTaggedPointer(unobfuscated))
2378 return this->TaggedPointerVendorRuntimeAssisted::GetClassDescriptor(ptr);
2379
2380 uintptr_t slot = (ptr >> m_objc_debug_taggedpointer_ext_slot_shift) &
2381 m_objc_debug_taggedpointer_ext_slot_mask;
2382
2383 CacheIterator iterator = m_ext_cache.find(slot), end = m_ext_cache.end();
2384 if (iterator != end) {
2385 actual_class_descriptor_sp = iterator->second;
2386 } else {
2387 Process *process(m_runtime.GetProcess());
2388 uintptr_t slot_ptr = slot * process->GetAddressByteSize() +
2389 m_objc_debug_taggedpointer_ext_classes;
2390 Status error;
2391 uintptr_t slot_data = process->ReadPointerFromMemory(slot_ptr, error);
2392 if (error.Fail() || slot_data == 0 ||
2393 slot_data == uintptr_t(LLDB_INVALID_ADDRESS))
2394 return nullptr;
2395 actual_class_descriptor_sp =
2396 m_runtime.GetClassDescriptorFromISA((ObjCISA)slot_data);
2397 if (!actual_class_descriptor_sp)
2398 return ObjCLanguageRuntime::ClassDescriptorSP();
2399 m_ext_cache[slot] = actual_class_descriptor_sp;
2400 }
2401
2402 data_payload =
2403 (((uint64_t)unobfuscated << m_objc_debug_taggedpointer_ext_payload_lshift) >>
2404 m_objc_debug_taggedpointer_ext_payload_rshift);
2405
2406 return ClassDescriptorSP(
2407 new ClassDescriptorV2Tagged(actual_class_descriptor_sp, data_payload));
2408 }
2409
NonPointerISACache(AppleObjCRuntimeV2 & runtime,const ModuleSP & objc_module_sp,uint64_t objc_debug_isa_class_mask,uint64_t objc_debug_isa_magic_mask,uint64_t objc_debug_isa_magic_value,uint64_t objc_debug_indexed_isa_magic_mask,uint64_t objc_debug_indexed_isa_magic_value,uint64_t objc_debug_indexed_isa_index_mask,uint64_t objc_debug_indexed_isa_index_shift,lldb::addr_t objc_indexed_classes)2410 AppleObjCRuntimeV2::NonPointerISACache::NonPointerISACache(
2411 AppleObjCRuntimeV2 &runtime, const ModuleSP &objc_module_sp,
2412 uint64_t objc_debug_isa_class_mask, uint64_t objc_debug_isa_magic_mask,
2413 uint64_t objc_debug_isa_magic_value,
2414 uint64_t objc_debug_indexed_isa_magic_mask,
2415 uint64_t objc_debug_indexed_isa_magic_value,
2416 uint64_t objc_debug_indexed_isa_index_mask,
2417 uint64_t objc_debug_indexed_isa_index_shift,
2418 lldb::addr_t objc_indexed_classes)
2419 : m_runtime(runtime), m_cache(), m_objc_module_wp(objc_module_sp),
2420 m_objc_debug_isa_class_mask(objc_debug_isa_class_mask),
2421 m_objc_debug_isa_magic_mask(objc_debug_isa_magic_mask),
2422 m_objc_debug_isa_magic_value(objc_debug_isa_magic_value),
2423 m_objc_debug_indexed_isa_magic_mask(objc_debug_indexed_isa_magic_mask),
2424 m_objc_debug_indexed_isa_magic_value(objc_debug_indexed_isa_magic_value),
2425 m_objc_debug_indexed_isa_index_mask(objc_debug_indexed_isa_index_mask),
2426 m_objc_debug_indexed_isa_index_shift(objc_debug_indexed_isa_index_shift),
2427 m_objc_indexed_classes(objc_indexed_classes), m_indexed_isa_cache() {}
2428
2429 ObjCLanguageRuntime::ClassDescriptorSP
GetClassDescriptor(ObjCISA isa)2430 AppleObjCRuntimeV2::NonPointerISACache::GetClassDescriptor(ObjCISA isa) {
2431 ObjCISA real_isa = 0;
2432 if (!EvaluateNonPointerISA(isa, real_isa))
2433 return ObjCLanguageRuntime::ClassDescriptorSP();
2434 auto cache_iter = m_cache.find(real_isa);
2435 if (cache_iter != m_cache.end())
2436 return cache_iter->second;
2437 auto descriptor_sp =
2438 m_runtime.ObjCLanguageRuntime::GetClassDescriptorFromISA(real_isa);
2439 if (descriptor_sp) // cache only positive matches since the table might grow
2440 m_cache[real_isa] = descriptor_sp;
2441 return descriptor_sp;
2442 }
2443
EvaluateNonPointerISA(ObjCISA isa,ObjCISA & ret_isa)2444 bool AppleObjCRuntimeV2::NonPointerISACache::EvaluateNonPointerISA(
2445 ObjCISA isa, ObjCISA &ret_isa) {
2446 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES));
2447
2448 LLDB_LOGF(log, "AOCRT::NPI Evaluate(isa = 0x%" PRIx64 ")", (uint64_t)isa);
2449
2450 if ((isa & ~m_objc_debug_isa_class_mask) == 0)
2451 return false;
2452
2453 // If all of the indexed ISA variables are set, then its possible that this
2454 // ISA is indexed, and we should first try to get its value using the index.
2455 // Note, we check these variables first as the ObjC runtime will set at least
2456 // one of their values to 0 if they aren't needed.
2457 if (m_objc_debug_indexed_isa_magic_mask &&
2458 m_objc_debug_indexed_isa_magic_value &&
2459 m_objc_debug_indexed_isa_index_mask &&
2460 m_objc_debug_indexed_isa_index_shift && m_objc_indexed_classes) {
2461 if ((isa & ~m_objc_debug_indexed_isa_index_mask) == 0)
2462 return false;
2463
2464 if ((isa & m_objc_debug_indexed_isa_magic_mask) ==
2465 m_objc_debug_indexed_isa_magic_value) {
2466 // Magic bits are correct, so try extract the index.
2467 uintptr_t index = (isa & m_objc_debug_indexed_isa_index_mask) >>
2468 m_objc_debug_indexed_isa_index_shift;
2469 // If the index is out of bounds of the length of the array then check if
2470 // the array has been updated. If that is the case then we should try
2471 // read the count again, and update the cache if the count has been
2472 // updated.
2473 if (index > m_indexed_isa_cache.size()) {
2474 LLDB_LOGF(log,
2475 "AOCRT::NPI (index = %" PRIu64
2476 ") exceeds cache (size = %" PRIu64 ")",
2477 (uint64_t)index, (uint64_t)m_indexed_isa_cache.size());
2478
2479 Process *process(m_runtime.GetProcess());
2480
2481 ModuleSP objc_module_sp(m_objc_module_wp.lock());
2482 if (!objc_module_sp)
2483 return false;
2484
2485 Status error;
2486 auto objc_indexed_classes_count = ExtractRuntimeGlobalSymbol(
2487 process, ConstString("objc_indexed_classes_count"), objc_module_sp,
2488 error);
2489 if (error.Fail())
2490 return false;
2491
2492 LLDB_LOGF(log, "AOCRT::NPI (new class count = %" PRIu64 ")",
2493 (uint64_t)objc_indexed_classes_count);
2494
2495 if (objc_indexed_classes_count > m_indexed_isa_cache.size()) {
2496 // Read the class entries we don't have. We should just read all of
2497 // them instead of just the one we need as then we can cache those we
2498 // may need later.
2499 auto num_new_classes =
2500 objc_indexed_classes_count - m_indexed_isa_cache.size();
2501 const uint32_t addr_size = process->GetAddressByteSize();
2502 DataBufferHeap buffer(num_new_classes * addr_size, 0);
2503
2504 lldb::addr_t last_read_class =
2505 m_objc_indexed_classes + (m_indexed_isa_cache.size() * addr_size);
2506 size_t bytes_read = process->ReadMemory(
2507 last_read_class, buffer.GetBytes(), buffer.GetByteSize(), error);
2508 if (error.Fail() || bytes_read != buffer.GetByteSize())
2509 return false;
2510
2511 LLDB_LOGF(log, "AOCRT::NPI (read new classes count = %" PRIu64 ")",
2512 (uint64_t)num_new_classes);
2513
2514 // Append the new entries to the existing cache.
2515 DataExtractor data(buffer.GetBytes(), buffer.GetByteSize(),
2516 process->GetByteOrder(),
2517 process->GetAddressByteSize());
2518
2519 lldb::offset_t offset = 0;
2520 for (unsigned i = 0; i != num_new_classes; ++i)
2521 m_indexed_isa_cache.push_back(data.GetAddress(&offset));
2522 }
2523 }
2524
2525 // If the index is still out of range then this isn't a pointer.
2526 if (index > m_indexed_isa_cache.size())
2527 return false;
2528
2529 LLDB_LOGF(log, "AOCRT::NPI Evaluate(ret_isa = 0x%" PRIx64 ")",
2530 (uint64_t)m_indexed_isa_cache[index]);
2531
2532 ret_isa = m_indexed_isa_cache[index];
2533 return (ret_isa != 0); // this is a pointer so 0 is not a valid value
2534 }
2535
2536 return false;
2537 }
2538
2539 // Definitely not an indexed ISA, so try to use a mask to extract the pointer
2540 // from the ISA.
2541 if ((isa & m_objc_debug_isa_magic_mask) == m_objc_debug_isa_magic_value) {
2542 ret_isa = isa & m_objc_debug_isa_class_mask;
2543 return (ret_isa != 0); // this is a pointer so 0 is not a valid value
2544 }
2545 return false;
2546 }
2547
GetEncodingToType()2548 ObjCLanguageRuntime::EncodingToTypeSP AppleObjCRuntimeV2::GetEncodingToType() {
2549 if (!m_encoding_to_type_sp)
2550 m_encoding_to_type_sp =
2551 std::make_shared<AppleObjCTypeEncodingParser>(*this);
2552 return m_encoding_to_type_sp;
2553 }
2554
2555 lldb_private::AppleObjCRuntime::ObjCISA
GetPointerISA(ObjCISA isa)2556 AppleObjCRuntimeV2::GetPointerISA(ObjCISA isa) {
2557 ObjCISA ret = isa;
2558
2559 if (m_non_pointer_isa_cache_up)
2560 m_non_pointer_isa_cache_up->EvaluateNonPointerISA(isa, ret);
2561
2562 return ret;
2563 }
2564
GetCFBooleanValuesIfNeeded()2565 bool AppleObjCRuntimeV2::GetCFBooleanValuesIfNeeded() {
2566 if (m_CFBoolean_values)
2567 return true;
2568
2569 static ConstString g_kCFBooleanFalse("__kCFBooleanFalse");
2570 static ConstString g_kCFBooleanTrue("__kCFBooleanTrue");
2571
2572 std::function<lldb::addr_t(ConstString)> get_symbol =
2573 [this](ConstString sym) -> lldb::addr_t {
2574 SymbolContextList sc_list;
2575 GetProcess()->GetTarget().GetImages().FindSymbolsWithNameAndType(
2576 sym, lldb::eSymbolTypeData, sc_list);
2577 if (sc_list.GetSize() == 1) {
2578 SymbolContext sc;
2579 sc_list.GetContextAtIndex(0, sc);
2580 if (sc.symbol)
2581 return sc.symbol->GetLoadAddress(&GetProcess()->GetTarget());
2582 }
2583
2584 return LLDB_INVALID_ADDRESS;
2585 };
2586
2587 lldb::addr_t false_addr = get_symbol(g_kCFBooleanFalse);
2588 lldb::addr_t true_addr = get_symbol(g_kCFBooleanTrue);
2589
2590 return (m_CFBoolean_values = {false_addr, true_addr}).operator bool();
2591 }
2592
GetValuesForGlobalCFBooleans(lldb::addr_t & cf_true,lldb::addr_t & cf_false)2593 void AppleObjCRuntimeV2::GetValuesForGlobalCFBooleans(lldb::addr_t &cf_true,
2594 lldb::addr_t &cf_false) {
2595 if (GetCFBooleanValuesIfNeeded()) {
2596 cf_true = m_CFBoolean_values->second;
2597 cf_false = m_CFBoolean_values->first;
2598 } else
2599 this->AppleObjCRuntime::GetValuesForGlobalCFBooleans(cf_true, cf_false);
2600 }
2601
2602 #pragma mark Frame recognizers
2603
2604 class ObjCExceptionRecognizedStackFrame : public RecognizedStackFrame {
2605 public:
ObjCExceptionRecognizedStackFrame(StackFrameSP frame_sp)2606 ObjCExceptionRecognizedStackFrame(StackFrameSP frame_sp) {
2607 ThreadSP thread_sp = frame_sp->GetThread();
2608 ProcessSP process_sp = thread_sp->GetProcess();
2609
2610 const lldb::ABISP &abi = process_sp->GetABI();
2611 if (!abi) return;
2612
2613 TypeSystemClang *clang_ast_context =
2614 ScratchTypeSystemClang::GetForTarget(process_sp->GetTarget());
2615 if (!clang_ast_context)
2616 return;
2617 CompilerType voidstar =
2618 clang_ast_context->GetBasicType(lldb::eBasicTypeVoid).GetPointerType();
2619
2620 ValueList args;
2621 Value input_value;
2622 input_value.SetCompilerType(voidstar);
2623 args.PushValue(input_value);
2624
2625 if (!abi->GetArgumentValues(*thread_sp, args)) return;
2626
2627 addr_t exception_addr = args.GetValueAtIndex(0)->GetScalar().ULongLong();
2628
2629 Value value(exception_addr);
2630 value.SetCompilerType(voidstar);
2631 exception = ValueObjectConstResult::Create(frame_sp.get(), value,
2632 ConstString("exception"));
2633 exception = ValueObjectRecognizerSynthesizedValue::Create(
2634 *exception, eValueTypeVariableArgument);
2635 exception = exception->GetDynamicValue(eDynamicDontRunTarget);
2636
2637 m_arguments = ValueObjectListSP(new ValueObjectList());
2638 m_arguments->Append(exception);
2639
2640 m_stop_desc = "hit Objective-C exception";
2641 }
2642
2643 ValueObjectSP exception;
2644
GetExceptionObject()2645 lldb::ValueObjectSP GetExceptionObject() override { return exception; }
2646 };
2647
2648 class ObjCExceptionThrowFrameRecognizer : public StackFrameRecognizer {
2649 lldb::RecognizedStackFrameSP
RecognizeFrame(lldb::StackFrameSP frame)2650 RecognizeFrame(lldb::StackFrameSP frame) override {
2651 return lldb::RecognizedStackFrameSP(
2652 new ObjCExceptionRecognizedStackFrame(frame));
2653 };
GetName()2654 std::string GetName() override {
2655 return "ObjC Exception Throw StackFrame Recognizer";
2656 }
2657 };
2658
RegisterObjCExceptionRecognizer(Process * process)2659 static void RegisterObjCExceptionRecognizer(Process *process) {
2660 FileSpec module;
2661 ConstString function;
2662 std::tie(module, function) = AppleObjCRuntime::GetExceptionThrowLocation();
2663 std::vector<ConstString> symbols = {function};
2664
2665 process->GetTarget().GetFrameRecognizerManager().AddRecognizer(
2666 StackFrameRecognizerSP(new ObjCExceptionThrowFrameRecognizer()),
2667 module.GetFilename(), symbols,
2668 /*first_instruction_only*/ true);
2669 }
2670