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 "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
10#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
11
12#include "lldb/Core/Debugger.h"
13#include "lldb/Core/DebuggerEvents.h"
14#include "lldb/Core/Module.h"
15#include "lldb/Core/PluginManager.h"
16#include "lldb/Core/Section.h"
17#include "lldb/Expression/DiagnosticManager.h"
18#include "lldb/Expression/FunctionCaller.h"
19#include "lldb/Expression/UtilityFunction.h"
20#include "lldb/Host/OptionParser.h"
21#include "lldb/Interpreter/CommandObject.h"
22#include "lldb/Interpreter/CommandObjectMultiword.h"
23#include "lldb/Interpreter/CommandReturnObject.h"
24#include "lldb/Interpreter/OptionArgParser.h"
25#include "lldb/Interpreter/OptionValueBoolean.h"
26#include "lldb/Symbol/CompilerType.h"
27#include "lldb/Symbol/ObjectFile.h"
28#include "lldb/Symbol/Symbol.h"
29#include "lldb/Symbol/TypeList.h"
30#include "lldb/Symbol/VariableList.h"
31#include "lldb/Target/ABI.h"
32#include "lldb/Target/DynamicLoader.h"
33#include "lldb/Target/ExecutionContext.h"
34#include "lldb/Target/LanguageRuntime.h"
35#include "lldb/Target/Platform.h"
36#include "lldb/Target/Process.h"
37#include "lldb/Target/RegisterContext.h"
38#include "lldb/Target/StackFrameRecognizer.h"
39#include "lldb/Target/Target.h"
40#include "lldb/Target/Thread.h"
41#include "lldb/Utility/ConstString.h"
42#include "lldb/Utility/LLDBLog.h"
43#include "lldb/Utility/Log.h"
44#include "lldb/Utility/Scalar.h"
45#include "lldb/Utility/Status.h"
46#include "lldb/Utility/Stream.h"
47#include "lldb/Utility/StreamString.h"
48#include "lldb/Utility/Timer.h"
49#include "lldb/ValueObject/ValueObjectConstResult.h"
50#include "lldb/ValueObject/ValueObjectVariable.h"
51#include "lldb/lldb-enumerations.h"
52
53#include "AppleObjCClassDescriptorV2.h"
54#include "AppleObjCDeclVendor.h"
55#include "AppleObjCRuntimeV2.h"
56#include "AppleObjCTrampolineHandler.h"
57#include "AppleObjCTypeEncodingParser.h"
58
59#include "clang/AST/ASTContext.h"
60#include "clang/AST/DeclObjC.h"
61#include "clang/Basic/TargetInfo.h"
62#include "llvm/ADT/ScopeExit.h"
63
64#include <cstdint>
65#include <memory>
66#include <string>
67#include <vector>
68
69using namespace lldb;
70using namespace lldb_private;
71
72char AppleObjCRuntimeV2::ID = 0;
73
74static const char *g_get_dynamic_class_info_name =
75 "__lldb_apple_objc_v2_get_dynamic_class_info";
76
77static const char *g_get_dynamic_class_info_body = R"(
78
79extern "C"
80{
81 size_t strlen(const char *);
82 char *strncpy (char * s1, const char * s2, size_t n);
83 int printf(const char * format, ...);
84}
85#define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__)
86
87typedef struct _NXMapTable {
88 void *prototype;
89 unsigned num_classes;
90 unsigned num_buckets_minus_one;
91 void *buckets;
92} NXMapTable;
93
94#define NX_MAPNOTAKEY ((void *)(-1))
95
96typedef struct BucketInfo
97{
98 const char *name_ptr;
99 Class isa;
100} BucketInfo;
101
102struct ClassInfo
103{
104 Class isa;
105 uint32_t hash;
106} __attribute__((__packed__));
107
108uint32_t
109__lldb_apple_objc_v2_get_dynamic_class_info (void *gdb_objc_realized_classes_ptr,
110 void *class_infos_ptr,
111 uint32_t class_infos_byte_size,
112 uint32_t should_log)
113{
114 DEBUG_PRINTF ("gdb_objc_realized_classes_ptr = %p\n", gdb_objc_realized_classes_ptr);
115 DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr);
116 DEBUG_PRINTF ("class_infos_byte_size = %u\n", class_infos_byte_size);
117 const NXMapTable *grc = (const NXMapTable *)gdb_objc_realized_classes_ptr;
118 if (grc)
119 {
120 const unsigned num_classes = grc->num_classes;
121 DEBUG_PRINTF ("num_classes = %u\n", grc->num_classes);
122 if (class_infos_ptr)
123 {
124 const unsigned num_buckets_minus_one = grc->num_buckets_minus_one;
125 DEBUG_PRINTF ("num_buckets_minus_one = %u\n", num_buckets_minus_one);
126
127 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
128 DEBUG_PRINTF ("max_class_infos = %u\n", max_class_infos);
129
130 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
131 BucketInfo *buckets = (BucketInfo *)grc->buckets;
132
133 uint32_t idx = 0;
134 for (unsigned i=0; i<=num_buckets_minus_one; ++i)
135 {
136 if (buckets[i].name_ptr != NX_MAPNOTAKEY)
137 {
138 if (idx < max_class_infos)
139 {
140 const char *s = buckets[i].name_ptr;
141 uint32_t h = 5381;
142 for (unsigned char c = *s; c; c = *++s)
143 h = ((h << 5) + h) + c;
144 class_infos[idx].hash = h;
145 class_infos[idx].isa = buckets[i].isa;
146 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, buckets[i].name_ptr);
147 }
148 ++idx;
149 }
150 }
151 if (idx < max_class_infos)
152 {
153 class_infos[idx].isa = NULL;
154 class_infos[idx].hash = 0;
155 }
156 }
157 return num_classes;
158 }
159 return 0;
160}
161
162)";
163
164static const char *g_get_dynamic_class_info2_name =
165 "__lldb_apple_objc_v2_get_dynamic_class_info2";
166
167static const char *g_get_dynamic_class_info2_body = R"(
168
169extern "C" {
170 int printf(const char * format, ...);
171 void free(void *ptr);
172 Class* objc_copyRealizedClassList_nolock(unsigned int *outCount);
173 const char* objc_debug_class_getNameRaw(Class cls);
174}
175
176#define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__)
177
178struct ClassInfo
179{
180 Class isa;
181 uint32_t hash;
182} __attribute__((__packed__));
183
184uint32_t
185__lldb_apple_objc_v2_get_dynamic_class_info2(void *gdb_objc_realized_classes_ptr,
186 void *class_infos_ptr,
187 uint32_t class_infos_byte_size,
188 uint32_t should_log)
189{
190 DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr);
191 DEBUG_PRINTF ("class_infos_byte_size = %u\n", class_infos_byte_size);
192
193 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
194 DEBUG_PRINTF ("max_class_infos = %u\n", max_class_infos);
195
196 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
197
198 uint32_t count = 0;
199 Class* realized_class_list = objc_copyRealizedClassList_nolock(&count);
200 DEBUG_PRINTF ("count = %u\n", count);
201
202 uint32_t idx = 0;
203 for (uint32_t i=0; i<count; ++i)
204 {
205 if (idx < max_class_infos)
206 {
207 Class isa = realized_class_list[i];
208 const char *name_ptr = objc_debug_class_getNameRaw(isa);
209 if (!name_ptr)
210 continue;
211 const char *s = name_ptr;
212 uint32_t h = 5381;
213 for (unsigned char c = *s; c; c = *++s)
214 h = ((h << 5) + h) + c;
215 class_infos[idx].hash = h;
216 class_infos[idx].isa = isa;
217 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name_ptr);
218 }
219 idx++;
220 }
221
222 if (idx < max_class_infos)
223 {
224 class_infos[idx].isa = NULL;
225 class_infos[idx].hash = 0;
226 }
227
228 free(realized_class_list);
229 return count;
230}
231)";
232
233static const char *g_get_dynamic_class_info3_name =
234 "__lldb_apple_objc_v2_get_dynamic_class_info3";
235
236static const char *g_get_dynamic_class_info3_body = R"(
237
238extern "C" {
239 int printf(const char * format, ...);
240 void free(void *ptr);
241 size_t objc_getRealizedClassList_trylock(Class *buffer, size_t len);
242 const char* objc_debug_class_getNameRaw(Class cls);
243 const char* class_getName(Class cls);
244}
245
246#define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__)
247
248struct ClassInfo
249{
250 Class isa;
251 uint32_t hash;
252} __attribute__((__packed__));
253
254uint32_t
255__lldb_apple_objc_v2_get_dynamic_class_info3(void *gdb_objc_realized_classes_ptr,
256 void *class_infos_ptr,
257 uint32_t class_infos_byte_size,
258 void *class_buffer,
259 uint32_t class_buffer_len,
260 uint32_t should_log)
261{
262 DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr);
263 DEBUG_PRINTF ("class_infos_byte_size = %u\n", class_infos_byte_size);
264
265 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
266 DEBUG_PRINTF ("max_class_infos = %u\n", max_class_infos);
267
268 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
269
270 Class *realized_class_list = (Class*)class_buffer;
271
272 uint32_t count = objc_getRealizedClassList_trylock(realized_class_list,
273 class_buffer_len);
274 DEBUG_PRINTF ("count = %u\n", count);
275
276 uint32_t idx = 0;
277 for (uint32_t i=0; i<count; ++i)
278 {
279 if (idx < max_class_infos)
280 {
281 Class isa = realized_class_list[i];
282 const char *name_ptr = objc_debug_class_getNameRaw(isa);
283 if (!name_ptr) {
284 class_getName(isa); // Realize name of lazy classes.
285 name_ptr = objc_debug_class_getNameRaw(isa);
286 }
287 if (!name_ptr)
288 continue;
289 const char *s = name_ptr;
290 uint32_t h = 5381;
291 for (unsigned char c = *s; c; c = *++s)
292 h = ((h << 5) + h) + c;
293 class_infos[idx].hash = h;
294 class_infos[idx].isa = isa;
295 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name_ptr);
296 }
297 idx++;
298 }
299
300 if (idx < max_class_infos)
301 {
302 class_infos[idx].isa = NULL;
303 class_infos[idx].hash = 0;
304 }
305
306 return count;
307}
308)";
309
310// We'll substitute in class_getName or class_getNameRaw depending
311// on which is present.
312static const char *g_shared_cache_class_name_funcptr = R"(
313extern "C"
314{
315 const char *%s(void *objc_class);
316 const char *(*class_name_lookup_func)(void *) = %s;
317}
318)";
319
320static const char *g_get_shared_cache_class_info_name =
321 "__lldb_apple_objc_v2_get_shared_cache_class_info";
322
323static const char *g_get_shared_cache_class_info_body = R"(
324
325extern "C"
326{
327 size_t strlen(const char *);
328 char *strncpy (char * s1, const char * s2, size_t n);
329 int printf(const char * format, ...);
330}
331
332#define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__)
333
334
335struct objc_classheader_t {
336 int32_t clsOffset;
337 int32_t hiOffset;
338};
339
340struct objc_classheader_v16_t {
341 uint64_t isDuplicate : 1,
342 objectCacheOffset : 47, // Offset from the shared cache base
343 dylibObjCIndex : 16;
344};
345
346struct objc_clsopt_t {
347 uint32_t capacity;
348 uint32_t occupied;
349 uint32_t shift;
350 uint32_t mask;
351 uint32_t zero;
352 uint32_t unused;
353 uint64_t salt;
354 uint32_t scramble[256];
355 uint8_t tab[0]; // tab[mask+1]
356 // uint8_t checkbytes[capacity];
357 // int32_t offset[capacity];
358 // objc_classheader_t clsOffsets[capacity];
359 // uint32_t duplicateCount;
360 // objc_classheader_t duplicateOffsets[duplicateCount];
361};
362
363struct objc_clsopt_v16_t {
364 uint32_t version;
365 uint32_t capacity;
366 uint32_t occupied;
367 uint32_t shift;
368 uint32_t mask;
369 uint32_t zero;
370 uint64_t salt;
371 uint32_t scramble[256];
372 uint8_t tab[0]; // tab[mask+1]
373 // uint8_t checkbytes[capacity];
374 // int32_t offset[capacity];
375 // objc_classheader_t clsOffsets[capacity];
376 // uint32_t duplicateCount;
377 // objc_classheader_t duplicateOffsets[duplicateCount];
378};
379
380struct objc_opt_t {
381 uint32_t version;
382 int32_t selopt_offset;
383 int32_t headeropt_offset;
384 int32_t clsopt_offset;
385};
386
387struct objc_opt_v14_t {
388 uint32_t version;
389 uint32_t flags;
390 int32_t selopt_offset;
391 int32_t headeropt_offset;
392 int32_t clsopt_offset;
393};
394
395struct objc_opt_v16_t {
396 uint32_t version;
397 uint32_t flags;
398 int32_t selopt_offset;
399 int32_t headeropt_ro_offset;
400 int32_t unused_clsopt_offset;
401 int32_t unused_protocolopt_offset;
402 int32_t headeropt_rw_offset;
403 int32_t unused_protocolopt2_offset;
404 int32_t largeSharedCachesClassOffset;
405 int32_t largeSharedCachesProtocolOffset;
406 uint64_t relativeMethodSelectorBaseAddressCacheOffset;
407};
408
409struct ClassInfo
410{
411 Class isa;
412 uint32_t hash;
413} __attribute__((__packed__));
414
415uint32_t
416__lldb_apple_objc_v2_get_shared_cache_class_info (void *objc_opt_ro_ptr,
417 void *shared_cache_base_ptr,
418 void *class_infos_ptr,
419 uint64_t *relative_selector_offset,
420 uint32_t class_infos_byte_size,
421 uint32_t should_log)
422{
423 *relative_selector_offset = 0;
424 uint32_t idx = 0;
425 DEBUG_PRINTF ("objc_opt_ro_ptr = %p\n", objc_opt_ro_ptr);
426 DEBUG_PRINTF ("shared_cache_base_ptr = %p\n", shared_cache_base_ptr);
427 DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr);
428 DEBUG_PRINTF ("class_infos_byte_size = %u (%llu class infos)\n", class_infos_byte_size, (uint64_t)(class_infos_byte_size/sizeof(ClassInfo)));
429 if (objc_opt_ro_ptr)
430 {
431 const objc_opt_t *objc_opt = (objc_opt_t *)objc_opt_ro_ptr;
432 const objc_opt_v14_t* objc_opt_v14 = (objc_opt_v14_t*)objc_opt_ro_ptr;
433 const objc_opt_v16_t* objc_opt_v16 = (objc_opt_v16_t*)objc_opt_ro_ptr;
434 if (objc_opt->version >= 16)
435 {
436 *relative_selector_offset = objc_opt_v16->relativeMethodSelectorBaseAddressCacheOffset;
437 DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt_v16->version);
438 DEBUG_PRINTF ("objc_opt->flags = %u\n", objc_opt_v16->flags);
439 DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt_v16->selopt_offset);
440 DEBUG_PRINTF ("objc_opt->headeropt_ro_offset = %d\n", objc_opt_v16->headeropt_ro_offset);
441 DEBUG_PRINTF ("objc_opt->relativeMethodSelectorBaseAddressCacheOffset = %d\n", *relative_selector_offset);
442 }
443 else if (objc_opt->version >= 14)
444 {
445 DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt_v14->version);
446 DEBUG_PRINTF ("objc_opt->flags = %u\n", objc_opt_v14->flags);
447 DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt_v14->selopt_offset);
448 DEBUG_PRINTF ("objc_opt->headeropt_offset = %d\n", objc_opt_v14->headeropt_offset);
449 DEBUG_PRINTF ("objc_opt->clsopt_offset = %d\n", objc_opt_v14->clsopt_offset);
450 }
451 else
452 {
453 DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt->version);
454 DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt->selopt_offset);
455 DEBUG_PRINTF ("objc_opt->headeropt_offset = %d\n", objc_opt->headeropt_offset);
456 DEBUG_PRINTF ("objc_opt->clsopt_offset = %d\n", objc_opt->clsopt_offset);
457 }
458
459 if (objc_opt->version == 16)
460 {
461 int32_t large_offset = objc_opt_v16->largeSharedCachesClassOffset;
462 const objc_clsopt_v16_t* clsopt = (const objc_clsopt_v16_t*)((uint8_t *)objc_opt + large_offset);
463 // Work around a bug in some version shared cache builder where the offset overflows 2GiB (rdar://146432183).
464 uint32_t unsigned_offset = (uint32_t)large_offset;
465 if (unsigned_offset > 0x7fffffff && unsigned_offset < 0x82000000) {
466 clsopt = (const objc_clsopt_v16_t*)((uint8_t *)objc_opt + unsigned_offset);
467 DEBUG_PRINTF("warning: applying largeSharedCachesClassOffset overflow workaround!\n");
468 }
469 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
470
471 DEBUG_PRINTF("max_class_infos = %llu\n", (uint64_t)max_class_infos);
472
473 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
474
475 const uint8_t *checkbytes = &clsopt->tab[clsopt->mask+1];
476 const int32_t *offsets = (const int32_t *)(checkbytes + clsopt->capacity);
477 const objc_classheader_v16_t *classOffsets = (const objc_classheader_v16_t *)(offsets + clsopt->capacity);
478
479 DEBUG_PRINTF ("clsopt->capacity = %u\n", clsopt->capacity);
480 DEBUG_PRINTF ("clsopt->mask = 0x%8.8x\n", clsopt->mask);
481 DEBUG_PRINTF ("classOffsets = %p\n", classOffsets);
482
483 for (uint32_t i=0; i<clsopt->capacity; ++i)
484 {
485 const uint64_t objectCacheOffset = classOffsets[i].objectCacheOffset;
486 DEBUG_PRINTF("objectCacheOffset[%u] = %u\n", i, objectCacheOffset);
487
488 if (classOffsets[i].isDuplicate) {
489 DEBUG_PRINTF("isDuplicate = true\n");
490 continue; // duplicate
491 }
492
493 if (objectCacheOffset == 0) {
494 DEBUG_PRINTF("objectCacheOffset == invalidEntryOffset\n");
495 continue; // invalid offset
496 }
497
498 if (class_infos && idx < max_class_infos)
499 {
500 class_infos[idx].isa = (Class)((uint8_t *)shared_cache_base_ptr + objectCacheOffset);
501
502 // Lookup the class name.
503 const char *name = class_name_lookup_func(class_infos[idx].isa);
504 DEBUG_PRINTF("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name);
505
506 // Hash the class name so we don't have to read it.
507 const char *s = name;
508 uint32_t h = 5381;
509 for (unsigned char c = *s; c; c = *++s)
510 {
511 // class_getName demangles swift names and the hash must
512 // be calculated on the mangled name. hash==0 means lldb
513 // will fetch the mangled name and compute the hash in
514 // ParseClassInfoArray.
515 if (c == '.')
516 {
517 h = 0;
518 break;
519 }
520 h = ((h << 5) + h) + c;
521 }
522 class_infos[idx].hash = h;
523 }
524 else
525 {
526 DEBUG_PRINTF("not(class_infos && idx < max_class_infos)\n");
527 }
528 ++idx;
529 }
530
531 const uint32_t *duplicate_count_ptr = (uint32_t *)&classOffsets[clsopt->capacity];
532 const uint32_t duplicate_count = *duplicate_count_ptr;
533 const objc_classheader_v16_t *duplicateClassOffsets = (const objc_classheader_v16_t *)(&duplicate_count_ptr[1]);
534
535 DEBUG_PRINTF ("duplicate_count = %u\n", duplicate_count);
536 DEBUG_PRINTF ("duplicateClassOffsets = %p\n", duplicateClassOffsets);
537
538 for (uint32_t i=0; i<duplicate_count; ++i)
539 {
540 const uint64_t objectCacheOffset = classOffsets[i].objectCacheOffset;
541 DEBUG_PRINTF("objectCacheOffset[%u] = %u\n", i, objectCacheOffset);
542
543 if (classOffsets[i].isDuplicate) {
544 DEBUG_PRINTF("isDuplicate = true\n");
545 continue; // duplicate
546 }
547
548 if (objectCacheOffset == 0) {
549 DEBUG_PRINTF("objectCacheOffset == invalidEntryOffset\n");
550 continue; // invalid offset
551 }
552
553 if (class_infos && idx < max_class_infos)
554 {
555 class_infos[idx].isa = (Class)((uint8_t *)shared_cache_base_ptr + objectCacheOffset);
556
557 // Lookup the class name.
558 const char *name = class_name_lookup_func(class_infos[idx].isa);
559 DEBUG_PRINTF("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name);
560
561 // Hash the class name so we don't have to read it.
562 const char *s = name;
563 uint32_t h = 5381;
564 for (unsigned char c = *s; c; c = *++s)
565 {
566 // class_getName demangles swift names and the hash must
567 // be calculated on the mangled name. hash==0 means lldb
568 // will fetch the mangled name and compute the hash in
569 // ParseClassInfoArray.
570 if (c == '.')
571 {
572 h = 0;
573 break;
574 }
575 h = ((h << 5) + h) + c;
576 }
577 class_infos[idx].hash = h;
578 }
579 }
580 }
581 else if (objc_opt->version >= 12 && objc_opt->version <= 15)
582 {
583 const objc_clsopt_t* clsopt = NULL;
584 if (objc_opt->version >= 14)
585 clsopt = (const objc_clsopt_t*)((uint8_t *)objc_opt_v14 + objc_opt_v14->clsopt_offset);
586 else
587 clsopt = (const objc_clsopt_t*)((uint8_t *)objc_opt + objc_opt->clsopt_offset);
588 const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo);
589 DEBUG_PRINTF("max_class_infos = %llu\n", (uint64_t)max_class_infos);
590 ClassInfo *class_infos = (ClassInfo *)class_infos_ptr;
591 int32_t invalidEntryOffset = 0;
592 // this is safe to do because the version field order is invariant
593 if (objc_opt->version == 12)
594 invalidEntryOffset = 16;
595 const uint8_t *checkbytes = &clsopt->tab[clsopt->mask+1];
596 const int32_t *offsets = (const int32_t *)(checkbytes + clsopt->capacity);
597 const objc_classheader_t *classOffsets = (const objc_classheader_t *)(offsets + clsopt->capacity);
598 DEBUG_PRINTF ("clsopt->capacity = %u\n", clsopt->capacity);
599 DEBUG_PRINTF ("clsopt->mask = 0x%8.8x\n", clsopt->mask);
600 DEBUG_PRINTF ("classOffsets = %p\n", classOffsets);
601 DEBUG_PRINTF("invalidEntryOffset = %d\n", invalidEntryOffset);
602 for (uint32_t i=0; i<clsopt->capacity; ++i)
603 {
604 const int32_t clsOffset = classOffsets[i].clsOffset;
605 DEBUG_PRINTF("clsOffset[%u] = %u\n", i, clsOffset);
606 if (clsOffset & 1)
607 {
608 DEBUG_PRINTF("clsOffset & 1\n");
609 continue; // duplicate
610 }
611 else if (clsOffset == invalidEntryOffset)
612 {
613 DEBUG_PRINTF("clsOffset == invalidEntryOffset\n");
614 continue; // invalid offset
615 }
616
617 if (class_infos && idx < max_class_infos)
618 {
619 class_infos[idx].isa = (Class)((uint8_t *)clsopt + clsOffset);
620 const char *name = class_name_lookup_func (class_infos[idx].isa);
621 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name);
622 // Hash the class name so we don't have to read it
623 const char *s = name;
624 uint32_t h = 5381;
625 for (unsigned char c = *s; c; c = *++s)
626 {
627 // class_getName demangles swift names and the hash must
628 // be calculated on the mangled name. hash==0 means lldb
629 // will fetch the mangled name and compute the hash in
630 // ParseClassInfoArray.
631 if (c == '.')
632 {
633 h = 0;
634 break;
635 }
636 h = ((h << 5) + h) + c;
637 }
638 class_infos[idx].hash = h;
639 }
640 else
641 {
642 DEBUG_PRINTF("not(class_infos && idx < max_class_infos)\n");
643 }
644 ++idx;
645 }
646
647 const uint32_t *duplicate_count_ptr = (uint32_t *)&classOffsets[clsopt->capacity];
648 const uint32_t duplicate_count = *duplicate_count_ptr;
649 const objc_classheader_t *duplicateClassOffsets = (const objc_classheader_t *)(&duplicate_count_ptr[1]);
650 DEBUG_PRINTF ("duplicate_count = %u\n", duplicate_count);
651 DEBUG_PRINTF ("duplicateClassOffsets = %p\n", duplicateClassOffsets);
652 for (uint32_t i=0; i<duplicate_count; ++i)
653 {
654 const int32_t clsOffset = duplicateClassOffsets[i].clsOffset;
655 if (clsOffset & 1)
656 continue; // duplicate
657 else if (clsOffset == invalidEntryOffset)
658 continue; // invalid offset
659
660 if (class_infos && idx < max_class_infos)
661 {
662 class_infos[idx].isa = (Class)((uint8_t *)clsopt + clsOffset);
663 const char *name = class_name_lookup_func (class_infos[idx].isa);
664 DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name);
665 // Hash the class name so we don't have to read it
666 const char *s = name;
667 uint32_t h = 5381;
668 for (unsigned char c = *s; c; c = *++s)
669 {
670 // class_getName demangles swift names and the hash must
671 // be calculated on the mangled name. hash==0 means lldb
672 // will fetch the mangled name and compute the hash in
673 // ParseClassInfoArray.
674 if (c == '.')
675 {
676 h = 0;
677 break;
678 }
679 h = ((h << 5) + h) + c;
680 }
681 class_infos[idx].hash = h;
682 }
683 ++idx;
684 }
685 }
686 DEBUG_PRINTF ("%u class_infos\n", idx);
687 DEBUG_PRINTF ("done\n");
688 }
689 return idx;
690}
691
692
693)";
694
695static uint64_t
696ExtractRuntimeGlobalSymbol(Process *process, ConstString name,
697 const ModuleSP &module_sp, Status &error,
698 bool read_value = true, uint8_t byte_size = 0,
699 uint64_t default_value = LLDB_INVALID_ADDRESS,
700 SymbolType sym_type = lldb::eSymbolTypeData) {
701 if (!process) {
702 error = Status::FromErrorString(str: "no process");
703 return default_value;
704 }
705
706 if (!module_sp) {
707 error = Status::FromErrorString(str: "no module");
708 return default_value;
709 }
710
711 if (!byte_size)
712 byte_size = process->GetAddressByteSize();
713 const Symbol *symbol =
714 module_sp->FindFirstSymbolWithNameAndType(name, symbol_type: lldb::eSymbolTypeData);
715
716 if (!symbol || !symbol->ValueIsAddress()) {
717 error = Status::FromErrorString(str: "no symbol");
718 return default_value;
719 }
720
721 lldb::addr_t symbol_load_addr =
722 symbol->GetAddressRef().GetLoadAddress(target: &process->GetTarget());
723 if (symbol_load_addr == LLDB_INVALID_ADDRESS) {
724 error = Status::FromErrorString(str: "symbol address invalid");
725 return default_value;
726 }
727
728 if (read_value)
729 return process->ReadUnsignedIntegerFromMemory(load_addr: symbol_load_addr, byte_size,
730 fail_value: default_value, error);
731 return symbol_load_addr;
732}
733
734static void RegisterObjCExceptionRecognizer(Process *process);
735
736AppleObjCRuntimeV2::AppleObjCRuntimeV2(Process *process,
737 const ModuleSP &objc_module_sp)
738 : AppleObjCRuntime(process), m_objc_module_sp(objc_module_sp),
739 m_dynamic_class_info_extractor(*this),
740 m_shared_cache_class_info_extractor(*this), m_decl_vendor_up(),
741 m_tagged_pointer_obfuscator(LLDB_INVALID_ADDRESS),
742 m_isa_hash_table_ptr(LLDB_INVALID_ADDRESS),
743 m_relative_selector_base(LLDB_INVALID_ADDRESS), m_hash_signature(),
744 m_has_object_getClass(false), m_has_objc_copyRealizedClassList(false),
745 m_has_objc_getRealizedClassList_trylock(false), m_loaded_objc_opt(false),
746 m_non_pointer_isa_cache_up(),
747 m_tagged_pointer_vendor_up(
748 TaggedPointerVendorV2::CreateInstance(runtime&: *this, objc_module_sp)),
749 m_encoding_to_type_sp(), m_CFBoolean_values(),
750 m_realized_class_generation_count(0) {
751 static const ConstString g_gdb_object_getClass("gdb_object_getClass");
752 m_has_object_getClass = HasSymbol(Name: g_gdb_object_getClass);
753 static const ConstString g_objc_copyRealizedClassList(
754 "_ZL33objc_copyRealizedClassList_nolockPj");
755 static const ConstString g_objc_getRealizedClassList_trylock(
756 "_objc_getRealizedClassList_trylock");
757 m_has_objc_copyRealizedClassList = HasSymbol(Name: g_objc_copyRealizedClassList);
758 m_has_objc_getRealizedClassList_trylock =
759 HasSymbol(Name: g_objc_getRealizedClassList_trylock);
760 WarnIfNoExpandedSharedCache();
761 RegisterObjCExceptionRecognizer(process);
762}
763
764LanguageRuntime *
765AppleObjCRuntimeV2::GetPreferredLanguageRuntime(ValueObject &in_value) {
766 if (auto process_sp = in_value.GetProcessSP()) {
767 assert(process_sp.get() == m_process);
768 if (auto descriptor_sp = GetNonKVOClassDescriptor(in_value)) {
769 LanguageType impl_lang = descriptor_sp->GetImplementationLanguage();
770 if (impl_lang != eLanguageTypeUnknown)
771 return process_sp->GetLanguageRuntime(language: impl_lang);
772 }
773 }
774 return nullptr;
775}
776
777bool AppleObjCRuntimeV2::GetDynamicTypeAndAddress(
778 ValueObject &in_value, lldb::DynamicValueType use_dynamic,
779 TypeAndOrName &class_type_or_name, Address &address,
780 Value::ValueType &value_type, llvm::ArrayRef<uint8_t> &local_buffer) {
781 // We should never get here with a null process...
782 assert(m_process != nullptr);
783
784 // The Runtime is attached to a particular process, you shouldn't pass in a
785 // value from another process. Note, however, the process might be NULL (e.g.
786 // if the value was made with SBTarget::EvaluateExpression...) in which case
787 // it is sufficient if the target's match:
788
789 Process *process = in_value.GetProcessSP().get();
790 if (process)
791 assert(process == m_process);
792 else
793 assert(in_value.GetTargetSP().get() == m_process->CalculateTarget().get());
794
795 class_type_or_name.Clear();
796 value_type = Value::ValueType::Scalar;
797
798 // Make sure we can have a dynamic value before starting...
799 if (CouldHaveDynamicValue(in_value)) {
800 // First job, pull out the address at 0 offset from the object That will
801 // be the ISA pointer.
802 ClassDescriptorSP objc_class_sp(GetNonKVOClassDescriptor(in_value));
803 if (objc_class_sp) {
804 const addr_t object_ptr = in_value.GetPointerValue().address;
805 address.SetRawAddress(object_ptr);
806
807 ConstString class_name(objc_class_sp->GetClassName());
808 class_type_or_name.SetName(class_name);
809 TypeSP type_sp(objc_class_sp->GetType());
810 if (type_sp)
811 class_type_or_name.SetTypeSP(type_sp);
812 else {
813 type_sp = LookupInCompleteClassCache(name&: class_name);
814 if (type_sp) {
815 objc_class_sp->SetType(type_sp);
816 class_type_or_name.SetTypeSP(type_sp);
817 } else {
818 // try to go for a CompilerType at least
819 if (auto *vendor = GetDeclVendor()) {
820 auto types = vendor->FindTypes(name: class_name, /*max_matches*/ 1);
821 if (!types.empty())
822 class_type_or_name.SetCompilerType(types.front());
823 }
824 }
825 }
826 }
827 }
828 return !class_type_or_name.IsEmpty();
829}
830
831// Static Functions
832LanguageRuntime *AppleObjCRuntimeV2::CreateInstance(Process *process,
833 LanguageType language) {
834 // FIXME: This should be a MacOS or iOS process, and we need to look for the
835 // OBJC section to make
836 // sure we aren't using the V1 runtime.
837 if (language == eLanguageTypeObjC) {
838 ModuleSP objc_module_sp;
839
840 if (AppleObjCRuntime::GetObjCVersion(process, objc_module_sp) ==
841 ObjCRuntimeVersions::eAppleObjC_V2)
842 return new AppleObjCRuntimeV2(process, objc_module_sp);
843 return nullptr;
844 }
845 return nullptr;
846}
847
848static constexpr OptionDefinition g_objc_classtable_dump_options[] = {
849 {LLDB_OPT_SET_ALL,
850 .required: false,
851 .long_option: "verbose",
852 .short_option: 'v',
853 .option_has_arg: OptionParser::eNoArgument,
854 .validator: nullptr,
855 .enum_values: {},
856 .completion_type: 0,
857 .argument_type: eArgTypeNone,
858 .usage_text: "Print ivar and method information in detail"}};
859
860class CommandObjectObjC_ClassTable_Dump : public CommandObjectParsed {
861public:
862 class CommandOptions : public Options {
863 public:
864 CommandOptions() : Options(), m_verbose(false, false) {}
865
866 ~CommandOptions() override = default;
867
868 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
869 ExecutionContext *execution_context) override {
870 Status error;
871 const int short_option = m_getopt_table[option_idx].val;
872 switch (short_option) {
873 case 'v':
874 m_verbose.SetCurrentValue(true);
875 m_verbose.SetOptionWasSet();
876 break;
877
878 default:
879 error = Status::FromErrorStringWithFormat(
880 format: "unrecognized short option '%c'", short_option);
881 break;
882 }
883
884 return error;
885 }
886
887 void OptionParsingStarting(ExecutionContext *execution_context) override {
888 m_verbose.Clear();
889 }
890
891 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
892 return llvm::ArrayRef(g_objc_classtable_dump_options);
893 }
894
895 OptionValueBoolean m_verbose;
896 };
897
898 CommandObjectObjC_ClassTable_Dump(CommandInterpreter &interpreter)
899 : CommandObjectParsed(interpreter, "dump",
900 "Dump information on Objective-C classes "
901 "known to the current process.",
902 "language objc class-table dump",
903 eCommandRequiresProcess |
904 eCommandProcessMustBeLaunched |
905 eCommandProcessMustBePaused),
906 m_options() {
907 AddSimpleArgumentList(arg_type: eArgTypeRegularExpression, repetition_type: eArgRepeatOptional);
908 }
909
910 ~CommandObjectObjC_ClassTable_Dump() override = default;
911
912 Options *GetOptions() override { return &m_options; }
913
914protected:
915 void DoExecute(Args &command, CommandReturnObject &result) override {
916 std::unique_ptr<RegularExpression> regex_up;
917 switch (command.GetArgumentCount()) {
918 case 0:
919 break;
920 case 1: {
921 regex_up =
922 std::make_unique<RegularExpression>(args: command.GetArgumentAtIndex(idx: 0));
923 if (!regex_up->IsValid()) {
924 result.AppendError(
925 in_string: "invalid argument - please provide a valid regular expression");
926 result.SetStatus(lldb::eReturnStatusFailed);
927 return;
928 }
929 break;
930 }
931 default: {
932 result.AppendError(in_string: "please provide 0 or 1 arguments");
933 result.SetStatus(lldb::eReturnStatusFailed);
934 return;
935 }
936 }
937
938 Process *process = m_exe_ctx.GetProcessPtr();
939 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(process&: *process);
940 if (objc_runtime) {
941 auto iterators_pair = objc_runtime->GetDescriptorIteratorPair();
942 auto iterator = iterators_pair.first;
943 auto &std_out = result.GetOutputStream();
944 for (; iterator != iterators_pair.second; iterator++) {
945 if (iterator->second) {
946 const char *class_name =
947 iterator->second->GetClassName().AsCString(value_if_empty: "<unknown>");
948 if (regex_up && class_name &&
949 !regex_up->Execute(string: llvm::StringRef(class_name)))
950 continue;
951 std_out.Printf(format: "isa = 0x%" PRIx64, iterator->first);
952 std_out.Printf(format: " name = %s", class_name);
953 std_out.Printf(format: " instance size = %" PRIu64,
954 iterator->second->GetInstanceSize());
955 std_out.Printf(format: " num ivars = %" PRIuPTR,
956 (uintptr_t)iterator->second->GetNumIVars());
957 if (auto superclass = iterator->second->GetSuperclass()) {
958 std_out.Printf(format: " superclass = %s",
959 superclass->GetClassName().AsCString(value_if_empty: "<unknown>"));
960 }
961 std_out.Printf(format: "\n");
962 if (m_options.m_verbose) {
963 for (size_t i = 0; i < iterator->second->GetNumIVars(); i++) {
964 auto ivar = iterator->second->GetIVarAtIndex(idx: i);
965 std_out.Printf(
966 format: " ivar name = %s type = %s size = %" PRIu64
967 " offset = %" PRId32 "\n",
968 ivar.m_name.AsCString(value_if_empty: "<unknown>"),
969 ivar.m_type.GetDisplayTypeName().AsCString(value_if_empty: "<unknown>"),
970 ivar.m_size, ivar.m_offset);
971 }
972
973 iterator->second->Describe(
974 superclass_func: nullptr,
975 instance_method_func: [&std_out](const char *name, const char *type) -> bool {
976 std_out.Printf(format: " instance method name = %s type = %s\n",
977 name, type);
978 return false;
979 },
980 class_method_func: [&std_out](const char *name, const char *type) -> bool {
981 std_out.Printf(format: " class method name = %s type = %s\n", name,
982 type);
983 return false;
984 },
985 ivar_func: nullptr);
986 }
987 } else {
988 if (regex_up && !regex_up->Execute(string: llvm::StringRef()))
989 continue;
990 std_out.Printf(format: "isa = 0x%" PRIx64 " has no associated class.\n",
991 iterator->first);
992 }
993 }
994 result.SetStatus(lldb::eReturnStatusSuccessFinishResult);
995 return;
996 }
997 result.AppendError(in_string: "current process has no Objective-C runtime loaded");
998 result.SetStatus(lldb::eReturnStatusFailed);
999 }
1000
1001 CommandOptions m_options;
1002};
1003
1004class CommandObjectMultiwordObjC_TaggedPointer_Info
1005 : public CommandObjectParsed {
1006public:
1007 CommandObjectMultiwordObjC_TaggedPointer_Info(CommandInterpreter &interpreter)
1008 : CommandObjectParsed(
1009 interpreter, "info", "Dump information on a tagged pointer.",
1010 "language objc tagged-pointer info",
1011 eCommandRequiresProcess | eCommandProcessMustBeLaunched |
1012 eCommandProcessMustBePaused) {
1013 AddSimpleArgumentList(arg_type: eArgTypeAddress, repetition_type: eArgRepeatPlus);
1014 }
1015
1016 ~CommandObjectMultiwordObjC_TaggedPointer_Info() override = default;
1017
1018protected:
1019 void DoExecute(Args &command, CommandReturnObject &result) override {
1020 if (command.GetArgumentCount() == 0) {
1021 result.AppendError(in_string: "this command requires arguments");
1022 result.SetStatus(lldb::eReturnStatusFailed);
1023 return;
1024 }
1025
1026 Process *process = m_exe_ctx.GetProcessPtr();
1027 ExecutionContext exe_ctx(process);
1028
1029 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(process&: *process);
1030 if (!objc_runtime) {
1031 result.AppendError(in_string: "current process has no Objective-C runtime loaded");
1032 result.SetStatus(lldb::eReturnStatusFailed);
1033 return;
1034 }
1035
1036 ObjCLanguageRuntime::TaggedPointerVendor *tagged_ptr_vendor =
1037 objc_runtime->GetTaggedPointerVendor();
1038 if (!tagged_ptr_vendor) {
1039 result.AppendError(in_string: "current process has no tagged pointer support");
1040 result.SetStatus(lldb::eReturnStatusFailed);
1041 return;
1042 }
1043
1044 for (size_t i = 0; i < command.GetArgumentCount(); i++) {
1045 const char *arg_str = command.GetArgumentAtIndex(idx: i);
1046 if (!arg_str)
1047 continue;
1048
1049 Status error;
1050 lldb::addr_t arg_addr = OptionArgParser::ToRawAddress(
1051 exe_ctx: &exe_ctx, s: arg_str, LLDB_INVALID_ADDRESS, error_ptr: &error);
1052 if (arg_addr == 0 || arg_addr == LLDB_INVALID_ADDRESS || error.Fail()) {
1053 result.AppendErrorWithFormatv(
1054 format: "could not convert '{0}' to a valid address\n", args&: arg_str);
1055 result.SetStatus(lldb::eReturnStatusFailed);
1056 return;
1057 }
1058
1059 if (!tagged_ptr_vendor->IsPossibleTaggedPointer(ptr: arg_addr)) {
1060 result.GetOutputStream().Format(format: "{0:x16} is not tagged\n", args&: arg_addr);
1061 continue;
1062 }
1063
1064 auto descriptor_sp = tagged_ptr_vendor->GetClassDescriptor(ptr: arg_addr);
1065 if (!descriptor_sp) {
1066 result.AppendErrorWithFormatv(
1067 format: "could not get class descriptor for {0:x16}\n", args&: arg_addr);
1068 result.SetStatus(lldb::eReturnStatusFailed);
1069 return;
1070 }
1071
1072 uint64_t info_bits = 0;
1073 uint64_t value_bits = 0;
1074 uint64_t payload = 0;
1075 if (descriptor_sp->GetTaggedPointerInfo(info_bits: &info_bits, value_bits: &value_bits,
1076 payload: &payload)) {
1077 result.GetOutputStream().Format(
1078 format: "{0:x} is tagged\n"
1079 "\tpayload = {1:x16}\n"
1080 "\tvalue = {2:x16}\n"
1081 "\tinfo bits = {3:x16}\n"
1082 "\tclass = {4}\n",
1083 args&: arg_addr, args&: payload, args&: value_bits, args&: info_bits,
1084 args: descriptor_sp->GetClassName().AsCString(value_if_empty: "<unknown>"));
1085 } else {
1086 result.GetOutputStream().Format(format: "{0:x16} is not tagged\n", args&: arg_addr);
1087 }
1088 }
1089
1090 result.SetStatus(lldb::eReturnStatusSuccessFinishResult);
1091 }
1092};
1093
1094class CommandObjectMultiwordObjC_ClassTable : public CommandObjectMultiword {
1095public:
1096 CommandObjectMultiwordObjC_ClassTable(CommandInterpreter &interpreter)
1097 : CommandObjectMultiword(
1098 interpreter, "class-table",
1099 "Commands for operating on the Objective-C class table.",
1100 "class-table <subcommand> [<subcommand-options>]") {
1101 LoadSubCommand(
1102 cmd_name: "dump",
1103 command_obj: CommandObjectSP(new CommandObjectObjC_ClassTable_Dump(interpreter)));
1104 }
1105
1106 ~CommandObjectMultiwordObjC_ClassTable() override = default;
1107};
1108
1109class CommandObjectMultiwordObjC_TaggedPointer : public CommandObjectMultiword {
1110public:
1111 CommandObjectMultiwordObjC_TaggedPointer(CommandInterpreter &interpreter)
1112 : CommandObjectMultiword(
1113 interpreter, "tagged-pointer",
1114 "Commands for operating on Objective-C tagged pointers.",
1115 "tagged-pointer <subcommand> [<subcommand-options>]") {
1116 LoadSubCommand(
1117 cmd_name: "info",
1118 command_obj: CommandObjectSP(
1119 new CommandObjectMultiwordObjC_TaggedPointer_Info(interpreter)));
1120 }
1121
1122 ~CommandObjectMultiwordObjC_TaggedPointer() override = default;
1123};
1124
1125class CommandObjectMultiwordObjC : public CommandObjectMultiword {
1126public:
1127 CommandObjectMultiwordObjC(CommandInterpreter &interpreter)
1128 : CommandObjectMultiword(
1129 interpreter, "objc",
1130 "Commands for operating on the Objective-C language runtime.",
1131 "objc <subcommand> [<subcommand-options>]") {
1132 LoadSubCommand(cmd_name: "class-table",
1133 command_obj: CommandObjectSP(
1134 new CommandObjectMultiwordObjC_ClassTable(interpreter)));
1135 LoadSubCommand(cmd_name: "tagged-pointer",
1136 command_obj: CommandObjectSP(new CommandObjectMultiwordObjC_TaggedPointer(
1137 interpreter)));
1138 }
1139
1140 ~CommandObjectMultiwordObjC() override = default;
1141};
1142
1143void AppleObjCRuntimeV2::Initialize() {
1144 PluginManager::RegisterPlugin(
1145 name: GetPluginNameStatic(), description: "Apple Objective-C Language Runtime - Version 2",
1146 create_callback: CreateInstance,
1147 command_callback: [](CommandInterpreter &interpreter) -> lldb::CommandObjectSP {
1148 return CommandObjectSP(new CommandObjectMultiwordObjC(interpreter));
1149 },
1150 precondition_callback: GetBreakpointExceptionPrecondition);
1151}
1152
1153void AppleObjCRuntimeV2::Terminate() {
1154 PluginManager::UnregisterPlugin(create_callback: CreateInstance);
1155}
1156
1157BreakpointResolverSP
1158AppleObjCRuntimeV2::CreateExceptionResolver(const BreakpointSP &bkpt,
1159 bool catch_bp, bool throw_bp) {
1160 BreakpointResolverSP resolver_sp;
1161
1162 if (throw_bp)
1163 resolver_sp = std::make_shared<BreakpointResolverName>(
1164 args: bkpt, args: std::get<1>(t: GetExceptionThrowLocation()).AsCString(),
1165 args: eFunctionNameTypeBase, args: eLanguageTypeUnknown, args: Breakpoint::Exact, args: 0,
1166 args: eLazyBoolNo);
1167 // FIXME: We don't do catch breakpoints for ObjC yet.
1168 // Should there be some way for the runtime to specify what it can do in this
1169 // regard?
1170 return resolver_sp;
1171}
1172
1173llvm::Expected<std::unique_ptr<UtilityFunction>>
1174AppleObjCRuntimeV2::CreateObjectChecker(std::string name,
1175 ExecutionContext &exe_ctx) {
1176 char check_function_code[2048];
1177
1178 int len = 0;
1179 if (m_has_object_getClass) {
1180 len = ::snprintf(s: check_function_code, maxlen: sizeof(check_function_code), format: R"(
1181 extern "C" void *gdb_object_getClass(void *);
1182 extern "C" int printf(const char *format, ...);
1183 extern "C" void
1184 %s(void *$__lldb_arg_obj, void *$__lldb_arg_selector) {
1185 if ($__lldb_arg_obj == (void *)0)
1186 return; // nil is ok
1187 if (!gdb_object_getClass($__lldb_arg_obj)) {
1188 *((volatile int *)0) = 'ocgc';
1189 } else if ($__lldb_arg_selector != (void *)0) {
1190 signed char $responds = (signed char)
1191 [(id)$__lldb_arg_obj respondsToSelector:
1192 (void *) $__lldb_arg_selector];
1193 if ($responds == (signed char) 0)
1194 *((volatile int *)0) = 'ocgc';
1195 }
1196 })",
1197 name.c_str());
1198 } else {
1199 len = ::snprintf(s: check_function_code, maxlen: sizeof(check_function_code), format: R"(
1200 extern "C" void *gdb_class_getClass(void *);
1201 extern "C" int printf(const char *format, ...);
1202 extern "C" void
1203 %s(void *$__lldb_arg_obj, void *$__lldb_arg_selector) {
1204 if ($__lldb_arg_obj == (void *)0)
1205 return; // nil is ok
1206 void **$isa_ptr = (void **)$__lldb_arg_obj;
1207 if (*$isa_ptr == (void *)0 ||
1208 !gdb_class_getClass(*$isa_ptr))
1209 *((volatile int *)0) = 'ocgc';
1210 else if ($__lldb_arg_selector != (void *)0) {
1211 signed char $responds = (signed char)
1212 [(id)$__lldb_arg_obj respondsToSelector:
1213 (void *) $__lldb_arg_selector];
1214 if ($responds == (signed char) 0)
1215 *((volatile int *)0) = 'ocgc';
1216 }
1217 })",
1218 name.c_str());
1219 }
1220
1221 assert(len < (int)sizeof(check_function_code));
1222 UNUSED_IF_ASSERT_DISABLED(len);
1223
1224 return GetTargetRef().CreateUtilityFunction(expression: check_function_code, name,
1225 language: eLanguageTypeC, exe_ctx);
1226}
1227
1228size_t AppleObjCRuntimeV2::GetByteOffsetForIvar(CompilerType &parent_ast_type,
1229 const char *ivar_name) {
1230 uint32_t ivar_offset = LLDB_INVALID_IVAR_OFFSET;
1231
1232 ConstString class_name = parent_ast_type.GetTypeName();
1233 if (!class_name.IsEmpty() && ivar_name && ivar_name[0]) {
1234 // Make the objective C V2 mangled name for the ivar offset from the class
1235 // name and ivar name
1236 std::string buffer("OBJC_IVAR_$_");
1237 buffer.append(s: class_name.AsCString());
1238 buffer.push_back(c: '.');
1239 buffer.append(s: ivar_name);
1240 ConstString ivar_const_str(buffer.c_str());
1241
1242 // Try to get the ivar offset address from the symbol table first using the
1243 // name we created above
1244 SymbolContextList sc_list;
1245 Target &target = m_process->GetTarget();
1246 target.GetImages().FindSymbolsWithNameAndType(name: ivar_const_str,
1247 symbol_type: eSymbolTypeObjCIVar, sc_list);
1248
1249 addr_t ivar_offset_address = LLDB_INVALID_ADDRESS;
1250
1251 Status error;
1252 SymbolContext ivar_offset_symbol;
1253 if (sc_list.GetSize() == 1 &&
1254 sc_list.GetContextAtIndex(idx: 0, sc&: ivar_offset_symbol)) {
1255 if (ivar_offset_symbol.symbol)
1256 ivar_offset_address =
1257 ivar_offset_symbol.symbol->GetLoadAddress(target: &target);
1258 }
1259
1260 // If we didn't get the ivar offset address from the symbol table, fall
1261 // back to getting it from the runtime
1262 if (ivar_offset_address == LLDB_INVALID_ADDRESS)
1263 ivar_offset_address = LookupRuntimeSymbol(name: ivar_const_str);
1264
1265 if (ivar_offset_address != LLDB_INVALID_ADDRESS)
1266 ivar_offset = m_process->ReadUnsignedIntegerFromMemory(
1267 load_addr: ivar_offset_address, byte_size: 4, LLDB_INVALID_IVAR_OFFSET, error);
1268 }
1269 return ivar_offset;
1270}
1271
1272// tagged pointers are special not-a-real-pointer values that contain both type
1273// and value information this routine attempts to check with as little
1274// computational effort as possible whether something could possibly be a
1275// tagged pointer - false positives are possible but false negatives shouldn't
1276bool AppleObjCRuntimeV2::IsTaggedPointer(addr_t ptr) {
1277 if (!m_tagged_pointer_vendor_up)
1278 return false;
1279 return m_tagged_pointer_vendor_up->IsPossibleTaggedPointer(ptr);
1280}
1281
1282class RemoteNXMapTable {
1283public:
1284 RemoteNXMapTable() : m_end_iterator(*this, -1) {}
1285
1286 void Dump() {
1287 printf(format: "RemoteNXMapTable.m_load_addr = 0x%" PRIx64 "\n", m_load_addr);
1288 printf(format: "RemoteNXMapTable.m_count = %u\n", m_count);
1289 printf(format: "RemoteNXMapTable.m_num_buckets_minus_one = %u\n",
1290 m_num_buckets_minus_one);
1291 printf(format: "RemoteNXMapTable.m_buckets_ptr = 0x%" PRIX64 "\n", m_buckets_ptr);
1292 }
1293
1294 bool ParseHeader(Process *process, lldb::addr_t load_addr) {
1295 m_process = process;
1296 m_load_addr = load_addr;
1297 m_map_pair_size = m_process->GetAddressByteSize() * 2;
1298 m_invalid_key =
1299 m_process->GetAddressByteSize() == 8 ? UINT64_MAX : UINT32_MAX;
1300 Status err;
1301
1302 // This currently holds true for all platforms we support, but we might
1303 // need to change this to use get the actually byte size of "unsigned" from
1304 // the target AST...
1305 const uint32_t unsigned_byte_size = sizeof(uint32_t);
1306 // Skip the prototype as we don't need it (const struct
1307 // +NXMapTablePrototype *prototype)
1308
1309 bool success = true;
1310 if (load_addr == LLDB_INVALID_ADDRESS)
1311 success = false;
1312 else {
1313 lldb::addr_t cursor = load_addr + m_process->GetAddressByteSize();
1314
1315 // unsigned count;
1316 m_count = m_process->ReadUnsignedIntegerFromMemory(
1317 load_addr: cursor, byte_size: unsigned_byte_size, fail_value: 0, error&: err);
1318 if (m_count) {
1319 cursor += unsigned_byte_size;
1320
1321 // unsigned nbBucketsMinusOne;
1322 m_num_buckets_minus_one = m_process->ReadUnsignedIntegerFromMemory(
1323 load_addr: cursor, byte_size: unsigned_byte_size, fail_value: 0, error&: err);
1324 cursor += unsigned_byte_size;
1325
1326 // void *buckets;
1327 m_buckets_ptr = m_process->ReadPointerFromMemory(vm_addr: cursor, error&: err);
1328
1329 success = m_count > 0 && m_buckets_ptr != LLDB_INVALID_ADDRESS;
1330 }
1331 }
1332
1333 if (!success) {
1334 m_count = 0;
1335 m_num_buckets_minus_one = 0;
1336 m_buckets_ptr = LLDB_INVALID_ADDRESS;
1337 }
1338 return success;
1339 }
1340
1341 // const_iterator mimics NXMapState and its code comes from NXInitMapState
1342 // and NXNextMapState.
1343 typedef std::pair<ConstString, ObjCLanguageRuntime::ObjCISA> element;
1344
1345 friend class const_iterator;
1346 class const_iterator {
1347 public:
1348 const_iterator(RemoteNXMapTable &parent, int index)
1349 : m_parent(parent), m_index(index) {
1350 AdvanceToValidIndex();
1351 }
1352
1353 const_iterator(const const_iterator &rhs)
1354 : m_parent(rhs.m_parent), m_index(rhs.m_index) {
1355 // AdvanceToValidIndex() has been called by rhs already.
1356 }
1357
1358 const_iterator &operator=(const const_iterator &rhs) {
1359 // AdvanceToValidIndex() has been called by rhs already.
1360 assert(&m_parent == &rhs.m_parent);
1361 m_index = rhs.m_index;
1362 return *this;
1363 }
1364
1365 bool operator==(const const_iterator &rhs) const {
1366 if (&m_parent != &rhs.m_parent)
1367 return false;
1368 if (m_index != rhs.m_index)
1369 return false;
1370
1371 return true;
1372 }
1373
1374 bool operator!=(const const_iterator &rhs) const {
1375 return !(operator==(rhs));
1376 }
1377
1378 const_iterator &operator++() {
1379 AdvanceToValidIndex();
1380 return *this;
1381 }
1382
1383 element operator*() const {
1384 if (m_index == -1) {
1385 // TODO find a way to make this an error, but not an assert
1386 return element();
1387 }
1388
1389 lldb::addr_t pairs_ptr = m_parent.m_buckets_ptr;
1390 size_t map_pair_size = m_parent.m_map_pair_size;
1391 lldb::addr_t pair_ptr = pairs_ptr + (m_index * map_pair_size);
1392
1393 Status err;
1394
1395 lldb::addr_t key =
1396 m_parent.m_process->ReadPointerFromMemory(vm_addr: pair_ptr, error&: err);
1397 if (!err.Success())
1398 return element();
1399 lldb::addr_t value = m_parent.m_process->ReadPointerFromMemory(
1400 vm_addr: pair_ptr + m_parent.m_process->GetAddressByteSize(), error&: err);
1401 if (!err.Success())
1402 return element();
1403
1404 std::string key_string;
1405
1406 m_parent.m_process->ReadCStringFromMemory(vm_addr: key, out_str&: key_string, error&: err);
1407 if (!err.Success())
1408 return element();
1409
1410 return element(ConstString(key_string.c_str()),
1411 (ObjCLanguageRuntime::ObjCISA)value);
1412 }
1413
1414 private:
1415 void AdvanceToValidIndex() {
1416 if (m_index == -1)
1417 return;
1418
1419 const lldb::addr_t pairs_ptr = m_parent.m_buckets_ptr;
1420 const size_t map_pair_size = m_parent.m_map_pair_size;
1421 const lldb::addr_t invalid_key = m_parent.m_invalid_key;
1422 Status err;
1423
1424 while (m_index--) {
1425 lldb::addr_t pair_ptr = pairs_ptr + (m_index * map_pair_size);
1426 lldb::addr_t key =
1427 m_parent.m_process->ReadPointerFromMemory(vm_addr: pair_ptr, error&: err);
1428
1429 if (!err.Success()) {
1430 m_index = -1;
1431 return;
1432 }
1433
1434 if (key != invalid_key)
1435 return;
1436 }
1437 }
1438 RemoteNXMapTable &m_parent;
1439 int m_index;
1440 };
1441
1442 const_iterator begin() {
1443 return const_iterator(*this, m_num_buckets_minus_one + 1);
1444 }
1445
1446 const_iterator end() { return m_end_iterator; }
1447
1448 uint32_t GetCount() const { return m_count; }
1449
1450 uint32_t GetBucketCount() const { return m_num_buckets_minus_one; }
1451
1452 lldb::addr_t GetBucketDataPointer() const { return m_buckets_ptr; }
1453
1454 lldb::addr_t GetTableLoadAddress() const { return m_load_addr; }
1455
1456private:
1457 // contents of _NXMapTable struct
1458 uint32_t m_count = 0;
1459 uint32_t m_num_buckets_minus_one = 0;
1460 lldb::addr_t m_buckets_ptr = LLDB_INVALID_ADDRESS;
1461 lldb_private::Process *m_process = nullptr;
1462 const_iterator m_end_iterator;
1463 lldb::addr_t m_load_addr = LLDB_INVALID_ADDRESS;
1464 size_t m_map_pair_size = 0;
1465 lldb::addr_t m_invalid_key = 0;
1466};
1467
1468AppleObjCRuntimeV2::HashTableSignature::HashTableSignature() = default;
1469
1470void AppleObjCRuntimeV2::HashTableSignature::UpdateSignature(
1471 const RemoteNXMapTable &hash_table) {
1472 m_count = hash_table.GetCount();
1473 m_num_buckets = hash_table.GetBucketCount();
1474 m_buckets_ptr = hash_table.GetBucketDataPointer();
1475}
1476
1477bool AppleObjCRuntimeV2::HashTableSignature::NeedsUpdate(
1478 Process *process, AppleObjCRuntimeV2 *runtime,
1479 RemoteNXMapTable &hash_table) {
1480 if (!hash_table.ParseHeader(process, load_addr: runtime->GetISAHashTablePointer())) {
1481 return false; // Failed to parse the header, no need to update anything
1482 }
1483
1484 // Check with out current signature and return true if the count, number of
1485 // buckets or the hash table address changes.
1486 if (m_count == hash_table.GetCount() &&
1487 m_num_buckets == hash_table.GetBucketCount() &&
1488 m_buckets_ptr == hash_table.GetBucketDataPointer()) {
1489 // Hash table hasn't changed
1490 return false;
1491 }
1492 // Hash table data has changed, we need to update
1493 return true;
1494}
1495
1496ObjCLanguageRuntime::ClassDescriptorSP
1497AppleObjCRuntimeV2::GetClassDescriptorFromISA(ObjCISA isa) {
1498 ObjCLanguageRuntime::ClassDescriptorSP class_descriptor_sp;
1499 if (auto *non_pointer_isa_cache = GetNonPointerIsaCache())
1500 class_descriptor_sp = non_pointer_isa_cache->GetClassDescriptor(isa);
1501 if (!class_descriptor_sp)
1502 class_descriptor_sp = ObjCLanguageRuntime::GetClassDescriptorFromISA(isa);
1503 return class_descriptor_sp;
1504}
1505
1506ObjCLanguageRuntime::ClassDescriptorSP
1507AppleObjCRuntimeV2::GetClassDescriptor(ValueObject &valobj) {
1508 ValueObjectSet seen;
1509 return GetClassDescriptorImpl(valobj, seen);
1510}
1511
1512ObjCLanguageRuntime::ClassDescriptorSP
1513AppleObjCRuntimeV2::GetClassDescriptorImpl(ValueObject &valobj,
1514 ValueObjectSet &seen) {
1515 seen.insert(Ptr: &valobj);
1516
1517 ClassDescriptorSP objc_class_sp;
1518 if (valobj.IsBaseClass()) {
1519 ValueObject *parent = valobj.GetParent();
1520 // Fail if there's a cycle in our parent chain.
1521 if (!parent || seen.count(Ptr: parent))
1522 return nullptr;
1523 if (ClassDescriptorSP parent_descriptor_sp =
1524 GetClassDescriptorImpl(valobj&: *parent, seen))
1525 return parent_descriptor_sp->GetSuperclass();
1526 return nullptr;
1527 }
1528 // if we get an invalid VO (which might still happen when playing around with
1529 // pointers returned by the expression parser, don't consider this a valid
1530 // ObjC object)
1531 if (!valobj.GetCompilerType().IsValid())
1532 return objc_class_sp;
1533 addr_t isa_pointer = valobj.GetPointerValue().address;
1534
1535 // tagged pointer
1536 if (IsTaggedPointer(ptr: isa_pointer))
1537 return m_tagged_pointer_vendor_up->GetClassDescriptor(ptr: isa_pointer);
1538 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
1539
1540 Process *process = exe_ctx.GetProcessPtr();
1541 if (!process)
1542 return objc_class_sp;
1543
1544 Status error;
1545 ObjCISA isa = process->ReadPointerFromMemory(vm_addr: isa_pointer, error);
1546 if (isa == LLDB_INVALID_ADDRESS)
1547 return objc_class_sp;
1548
1549 objc_class_sp = GetClassDescriptorFromISA(isa);
1550 if (!objc_class_sp) {
1551 if (ABISP abi_sp = process->GetABI())
1552 isa = abi_sp->FixCodeAddress(pc: isa);
1553 objc_class_sp = GetClassDescriptorFromISA(isa);
1554 }
1555
1556 if (isa && !objc_class_sp) {
1557 Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types);
1558 LLDB_LOGF(log,
1559 "0x%" PRIx64 ": AppleObjCRuntimeV2::GetClassDescriptor() ISA was "
1560 "not in class descriptor cache 0x%" PRIx64,
1561 isa_pointer, isa);
1562 }
1563 return objc_class_sp;
1564}
1565
1566lldb::addr_t AppleObjCRuntimeV2::GetTaggedPointerObfuscator() {
1567 if (m_tagged_pointer_obfuscator != LLDB_INVALID_ADDRESS)
1568 return m_tagged_pointer_obfuscator;
1569
1570 Process *process = GetProcess();
1571 ModuleSP objc_module_sp(GetObjCModule());
1572
1573 if (!objc_module_sp)
1574 return LLDB_INVALID_ADDRESS;
1575
1576 static ConstString g_gdb_objc_obfuscator(
1577 "objc_debug_taggedpointer_obfuscator");
1578
1579 const Symbol *symbol = objc_module_sp->FindFirstSymbolWithNameAndType(
1580 name: g_gdb_objc_obfuscator, symbol_type: lldb::eSymbolTypeAny);
1581 if (symbol) {
1582 lldb::addr_t g_gdb_obj_obfuscator_ptr =
1583 symbol->GetLoadAddress(target: &process->GetTarget());
1584
1585 if (g_gdb_obj_obfuscator_ptr != LLDB_INVALID_ADDRESS) {
1586 Status error;
1587 m_tagged_pointer_obfuscator =
1588 process->ReadPointerFromMemory(vm_addr: g_gdb_obj_obfuscator_ptr, error);
1589 }
1590 }
1591 // If we don't have a correct value at this point, there must be no
1592 // obfuscation.
1593 if (m_tagged_pointer_obfuscator == LLDB_INVALID_ADDRESS)
1594 m_tagged_pointer_obfuscator = 0;
1595
1596 return m_tagged_pointer_obfuscator;
1597}
1598
1599lldb::addr_t AppleObjCRuntimeV2::GetISAHashTablePointer() {
1600 if (m_isa_hash_table_ptr == LLDB_INVALID_ADDRESS) {
1601 Process *process = GetProcess();
1602
1603 ModuleSP objc_module_sp(GetObjCModule());
1604
1605 if (!objc_module_sp)
1606 return LLDB_INVALID_ADDRESS;
1607
1608 static ConstString g_gdb_objc_realized_classes("gdb_objc_realized_classes");
1609
1610 const Symbol *symbol = objc_module_sp->FindFirstSymbolWithNameAndType(
1611 name: g_gdb_objc_realized_classes, symbol_type: lldb::eSymbolTypeAny);
1612 if (symbol) {
1613 lldb::addr_t gdb_objc_realized_classes_ptr =
1614 symbol->GetLoadAddress(target: &process->GetTarget());
1615
1616 if (gdb_objc_realized_classes_ptr != LLDB_INVALID_ADDRESS) {
1617 Status error;
1618 m_isa_hash_table_ptr = process->ReadPointerFromMemory(
1619 vm_addr: gdb_objc_realized_classes_ptr, error);
1620 }
1621 }
1622 }
1623 return m_isa_hash_table_ptr;
1624}
1625
1626std::unique_ptr<AppleObjCRuntimeV2::SharedCacheImageHeaders>
1627AppleObjCRuntimeV2::SharedCacheImageHeaders::CreateSharedCacheImageHeaders(
1628 AppleObjCRuntimeV2 &runtime) {
1629 Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types);
1630 Process *process = runtime.GetProcess();
1631 ModuleSP objc_module_sp(runtime.GetObjCModule());
1632 if (!objc_module_sp || !process)
1633 return nullptr;
1634
1635 const Symbol *symbol = objc_module_sp->FindFirstSymbolWithNameAndType(
1636 name: ConstString("objc_debug_headerInfoRWs"), symbol_type: lldb::eSymbolTypeAny);
1637 if (!symbol) {
1638 LLDB_LOG(log, "Symbol 'objc_debug_headerInfoRWs' unavailable. Some "
1639 "information concerning the shared cache may be unavailable");
1640 return nullptr;
1641 }
1642
1643 lldb::addr_t objc_debug_headerInfoRWs_addr =
1644 symbol->GetLoadAddress(target: &process->GetTarget());
1645 if (objc_debug_headerInfoRWs_addr == LLDB_INVALID_ADDRESS) {
1646 LLDB_LOG(log, "Symbol 'objc_debug_headerInfoRWs' was found but we were "
1647 "unable to get its load address");
1648 return nullptr;
1649 }
1650
1651 Status error;
1652 lldb::addr_t objc_debug_headerInfoRWs_ptr =
1653 process->ReadPointerFromMemory(vm_addr: objc_debug_headerInfoRWs_addr, error);
1654 if (error.Fail()) {
1655 LLDB_LOG(log,
1656 "Failed to read address of 'objc_debug_headerInfoRWs' at {0:x}",
1657 objc_debug_headerInfoRWs_addr);
1658 return nullptr;
1659 }
1660
1661 const size_t metadata_size =
1662 sizeof(uint32_t) + sizeof(uint32_t); // count + entsize
1663 DataBufferHeap metadata_buffer(metadata_size, '\0');
1664 process->ReadMemory(vm_addr: objc_debug_headerInfoRWs_ptr, buf: metadata_buffer.GetBytes(),
1665 size: metadata_size, error);
1666 if (error.Fail()) {
1667 LLDB_LOG(log,
1668 "Unable to read metadata for 'objc_debug_headerInfoRWs' at {0:x}",
1669 objc_debug_headerInfoRWs_ptr);
1670 return nullptr;
1671 }
1672
1673 DataExtractor metadata_extractor(metadata_buffer.GetBytes(), metadata_size,
1674 process->GetByteOrder(),
1675 process->GetAddressByteSize());
1676 lldb::offset_t cursor = 0;
1677 uint32_t count = metadata_extractor.GetU32_unchecked(offset_ptr: &cursor);
1678 uint32_t entsize = metadata_extractor.GetU32_unchecked(offset_ptr: &cursor);
1679 if (count == 0 || entsize == 0) {
1680 LLDB_LOG(log,
1681 "'objc_debug_headerInfoRWs' had count {0} with entsize {1}. These "
1682 "should both be non-zero.",
1683 count, entsize);
1684 return nullptr;
1685 }
1686
1687 std::unique_ptr<SharedCacheImageHeaders> shared_cache_image_headers(
1688 new SharedCacheImageHeaders(runtime, objc_debug_headerInfoRWs_ptr, count,
1689 entsize));
1690 if (auto Err = shared_cache_image_headers->UpdateIfNeeded()) {
1691 LLDB_LOG_ERROR(log, std::move(Err),
1692 "Failed to update SharedCacheImageHeaders: {0}");
1693 return nullptr;
1694 }
1695
1696 return shared_cache_image_headers;
1697}
1698
1699llvm::Error AppleObjCRuntimeV2::SharedCacheImageHeaders::UpdateIfNeeded() {
1700 if (!m_needs_update)
1701 return llvm::Error::success();
1702
1703 Process *process = m_runtime.GetProcess();
1704 constexpr lldb::addr_t metadata_size =
1705 sizeof(uint32_t) + sizeof(uint32_t); // count + entsize
1706
1707 Status error;
1708 const lldb::addr_t first_header_addr = m_headerInfoRWs_ptr + metadata_size;
1709 DataBufferHeap header_buffer(m_entsize, '\0');
1710 lldb::offset_t cursor = 0;
1711 for (uint32_t i = 0; i < m_count; i++) {
1712 const lldb::addr_t header_addr = first_header_addr + (i * m_entsize);
1713 process->ReadMemory(vm_addr: header_addr, buf: header_buffer.GetBytes(), size: m_entsize,
1714 error);
1715 if (error.Fail())
1716 return llvm::createStringError(EC: llvm::inconvertibleErrorCode(),
1717 S: "Failed to read memory from inferior when "
1718 "populating SharedCacheImageHeaders");
1719
1720 DataExtractor header_extractor(header_buffer.GetBytes(), m_entsize,
1721 process->GetByteOrder(),
1722 process->GetAddressByteSize());
1723 cursor = 0;
1724 bool is_loaded = false;
1725 if (m_entsize == 4) {
1726 uint32_t header = header_extractor.GetU32_unchecked(offset_ptr: &cursor);
1727 if (header & 1)
1728 is_loaded = true;
1729 } else {
1730 uint64_t header = header_extractor.GetU64_unchecked(offset_ptr: &cursor);
1731 if (header & 1)
1732 is_loaded = true;
1733 }
1734
1735 if (is_loaded)
1736 m_loaded_images.set(i);
1737 else
1738 m_loaded_images.reset(Idx: i);
1739 }
1740 m_needs_update = false;
1741 m_version++;
1742 return llvm::Error::success();
1743}
1744
1745bool AppleObjCRuntimeV2::SharedCacheImageHeaders::IsImageLoaded(
1746 uint16_t image_index) {
1747 if (image_index >= m_count)
1748 return false;
1749 if (auto Err = UpdateIfNeeded()) {
1750 Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types);
1751 LLDB_LOG_ERROR(log, std::move(Err),
1752 "Failed to update SharedCacheImageHeaders: {0}");
1753 }
1754 return m_loaded_images.test(Idx: image_index);
1755}
1756
1757uint64_t AppleObjCRuntimeV2::SharedCacheImageHeaders::GetVersion() {
1758 if (auto Err = UpdateIfNeeded()) {
1759 Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types);
1760 LLDB_LOG_ERROR(log, std::move(Err),
1761 "Failed to update SharedCacheImageHeaders: {0}");
1762 }
1763 return m_version;
1764}
1765
1766std::unique_ptr<UtilityFunction>
1767AppleObjCRuntimeV2::DynamicClassInfoExtractor::GetClassInfoUtilityFunctionImpl(
1768 ExecutionContext &exe_ctx, Helper helper, std::string code,
1769 std::string name) {
1770 Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types);
1771
1772 LLDB_LOG(log, "Creating utility function {0}", name);
1773
1774 TypeSystemClangSP scratch_ts_sp =
1775 ScratchTypeSystemClang::GetForTarget(target&: exe_ctx.GetTargetRef());
1776 if (!scratch_ts_sp)
1777 return {};
1778
1779 auto utility_fn_or_error = exe_ctx.GetTargetRef().CreateUtilityFunction(
1780 expression: std::move(code), name: std::move(name), language: eLanguageTypeC, exe_ctx);
1781 if (!utility_fn_or_error) {
1782 LLDB_LOG_ERROR(
1783 log, utility_fn_or_error.takeError(),
1784 "Failed to get utility function for dynamic info extractor: {0}");
1785 return {};
1786 }
1787
1788 // Make some types for our arguments.
1789 CompilerType clang_uint32_t_type =
1790 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(encoding: eEncodingUint, bit_size: 32);
1791 CompilerType clang_void_pointer_type =
1792 scratch_ts_sp->GetBasicType(type: eBasicTypeVoid).GetPointerType();
1793
1794 // Make the runner function for our implementation utility function.
1795 ValueList arguments;
1796 Value value;
1797 value.SetValueType(Value::ValueType::Scalar);
1798 value.SetCompilerType(clang_void_pointer_type);
1799 arguments.PushValue(value);
1800 arguments.PushValue(value);
1801 value.SetValueType(Value::ValueType::Scalar);
1802 value.SetCompilerType(clang_uint32_t_type);
1803 arguments.PushValue(value);
1804
1805 // objc_getRealizedClassList_trylock takes an additional buffer and length.
1806 if (helper == Helper::objc_getRealizedClassList_trylock) {
1807 value.SetCompilerType(clang_void_pointer_type);
1808 arguments.PushValue(value);
1809 value.SetCompilerType(clang_uint32_t_type);
1810 arguments.PushValue(value);
1811 }
1812
1813 arguments.PushValue(value);
1814
1815 std::unique_ptr<UtilityFunction> utility_fn = std::move(*utility_fn_or_error);
1816
1817 Status error;
1818 utility_fn->MakeFunctionCaller(return_type: clang_uint32_t_type, arg_value_list: arguments,
1819 compilation_thread: exe_ctx.GetThreadSP(), error);
1820
1821 if (error.Fail()) {
1822 LLDB_LOG(log,
1823 "Failed to make function caller for implementation lookup: {0}.",
1824 error.AsCString());
1825 return {};
1826 }
1827
1828 return utility_fn;
1829}
1830
1831UtilityFunction *
1832AppleObjCRuntimeV2::DynamicClassInfoExtractor::GetClassInfoUtilityFunction(
1833 ExecutionContext &exe_ctx, Helper helper) {
1834 switch (helper) {
1835 case gdb_objc_realized_classes: {
1836 if (!m_gdb_objc_realized_classes_helper.utility_function)
1837 m_gdb_objc_realized_classes_helper.utility_function =
1838 GetClassInfoUtilityFunctionImpl(exe_ctx, helper,
1839 code: g_get_dynamic_class_info_body,
1840 name: g_get_dynamic_class_info_name);
1841 return m_gdb_objc_realized_classes_helper.utility_function.get();
1842 }
1843 case objc_copyRealizedClassList: {
1844 if (!m_objc_copyRealizedClassList_helper.utility_function)
1845 m_objc_copyRealizedClassList_helper.utility_function =
1846 GetClassInfoUtilityFunctionImpl(exe_ctx, helper,
1847 code: g_get_dynamic_class_info2_body,
1848 name: g_get_dynamic_class_info2_name);
1849 return m_objc_copyRealizedClassList_helper.utility_function.get();
1850 }
1851 case objc_getRealizedClassList_trylock: {
1852 if (!m_objc_getRealizedClassList_trylock_helper.utility_function)
1853 m_objc_getRealizedClassList_trylock_helper.utility_function =
1854 GetClassInfoUtilityFunctionImpl(exe_ctx, helper,
1855 code: g_get_dynamic_class_info3_body,
1856 name: g_get_dynamic_class_info3_name);
1857 return m_objc_getRealizedClassList_trylock_helper.utility_function.get();
1858 }
1859 }
1860 llvm_unreachable("Unexpected helper");
1861}
1862
1863lldb::addr_t &
1864AppleObjCRuntimeV2::DynamicClassInfoExtractor::GetClassInfoArgs(Helper helper) {
1865 switch (helper) {
1866 case gdb_objc_realized_classes:
1867 return m_gdb_objc_realized_classes_helper.args;
1868 case objc_copyRealizedClassList:
1869 return m_objc_copyRealizedClassList_helper.args;
1870 case objc_getRealizedClassList_trylock:
1871 return m_objc_getRealizedClassList_trylock_helper.args;
1872 }
1873 llvm_unreachable("Unexpected helper");
1874}
1875
1876AppleObjCRuntimeV2::DynamicClassInfoExtractor::Helper
1877AppleObjCRuntimeV2::DynamicClassInfoExtractor::ComputeHelper(
1878 ExecutionContext &exe_ctx) const {
1879 if (!m_runtime.m_has_objc_copyRealizedClassList &&
1880 !m_runtime.m_has_objc_getRealizedClassList_trylock)
1881 return DynamicClassInfoExtractor::gdb_objc_realized_classes;
1882
1883 if (Process *process = m_runtime.GetProcess()) {
1884 if (DynamicLoader *loader = process->GetDynamicLoader()) {
1885 if (loader->IsFullyInitialized()) {
1886 switch (exe_ctx.GetTargetRef().GetDynamicClassInfoHelper()) {
1887 case eDynamicClassInfoHelperAuto:
1888 [[fallthrough]];
1889 case eDynamicClassInfoHelperGetRealizedClassList:
1890 if (m_runtime.m_has_objc_getRealizedClassList_trylock)
1891 return DynamicClassInfoExtractor::objc_getRealizedClassList_trylock;
1892 [[fallthrough]];
1893 case eDynamicClassInfoHelperCopyRealizedClassList:
1894 if (m_runtime.m_has_objc_copyRealizedClassList)
1895 return DynamicClassInfoExtractor::objc_copyRealizedClassList;
1896 [[fallthrough]];
1897 case eDynamicClassInfoHelperRealizedClassesStruct:
1898 return DynamicClassInfoExtractor::gdb_objc_realized_classes;
1899 }
1900 }
1901 }
1902 }
1903
1904 return DynamicClassInfoExtractor::gdb_objc_realized_classes;
1905}
1906
1907std::unique_ptr<UtilityFunction>
1908AppleObjCRuntimeV2::SharedCacheClassInfoExtractor::
1909 GetClassInfoUtilityFunctionImpl(ExecutionContext &exe_ctx) {
1910 Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types);
1911
1912 LLDB_LOG(log, "Creating utility function {0}",
1913 g_get_shared_cache_class_info_name);
1914
1915 TypeSystemClangSP scratch_ts_sp =
1916 ScratchTypeSystemClang::GetForTarget(target&: exe_ctx.GetTargetRef());
1917 if (!scratch_ts_sp)
1918 return {};
1919
1920 // If the inferior objc.dylib has the class_getNameRaw function, use that in
1921 // our jitted expression. Else fall back to the old class_getName.
1922 static ConstString g_class_getName_symbol_name("class_getName");
1923 static ConstString g_class_getNameRaw_symbol_name(
1924 "objc_debug_class_getNameRaw");
1925
1926 ConstString class_name_getter_function_name =
1927 m_runtime.HasSymbol(Name: g_class_getNameRaw_symbol_name)
1928 ? g_class_getNameRaw_symbol_name
1929 : g_class_getName_symbol_name;
1930
1931 // Substitute in the correct class_getName / class_getNameRaw function name,
1932 // concatenate the two parts of our expression text. The format string has
1933 // two %s's, so provide the name twice.
1934 std::string shared_class_expression;
1935 llvm::raw_string_ostream(shared_class_expression)
1936 << llvm::format(Fmt: g_shared_cache_class_name_funcptr,
1937 Vals: class_name_getter_function_name.AsCString(),
1938 Vals: class_name_getter_function_name.AsCString());
1939
1940 shared_class_expression += g_get_shared_cache_class_info_body;
1941
1942 auto utility_fn_or_error = exe_ctx.GetTargetRef().CreateUtilityFunction(
1943 expression: std::move(shared_class_expression), name: g_get_shared_cache_class_info_name,
1944 language: eLanguageTypeC, exe_ctx);
1945
1946 if (!utility_fn_or_error) {
1947 LLDB_LOG_ERROR(
1948 log, utility_fn_or_error.takeError(),
1949 "Failed to get utility function for shared class info extractor: {0}");
1950 return nullptr;
1951 }
1952
1953 // Make some types for our arguments.
1954 CompilerType clang_uint32_t_type =
1955 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(encoding: eEncodingUint, bit_size: 32);
1956 CompilerType clang_void_pointer_type =
1957 scratch_ts_sp->GetBasicType(type: eBasicTypeVoid).GetPointerType();
1958 CompilerType clang_uint64_t_pointer_type =
1959 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(encoding: eEncodingUint, bit_size: 64)
1960 .GetPointerType();
1961
1962 // Next make the function caller for our implementation utility function.
1963 ValueList arguments;
1964 Value value;
1965 value.SetValueType(Value::ValueType::Scalar);
1966 value.SetCompilerType(clang_void_pointer_type);
1967 arguments.PushValue(value);
1968 arguments.PushValue(value);
1969 arguments.PushValue(value);
1970
1971 value.SetValueType(Value::ValueType::Scalar);
1972 value.SetCompilerType(clang_uint64_t_pointer_type);
1973 arguments.PushValue(value);
1974
1975 value.SetValueType(Value::ValueType::Scalar);
1976 value.SetCompilerType(clang_uint32_t_type);
1977 arguments.PushValue(value);
1978 arguments.PushValue(value);
1979
1980 std::unique_ptr<UtilityFunction> utility_fn = std::move(*utility_fn_or_error);
1981
1982 Status error;
1983 utility_fn->MakeFunctionCaller(return_type: clang_uint32_t_type, arg_value_list: arguments,
1984 compilation_thread: exe_ctx.GetThreadSP(), error);
1985
1986 if (error.Fail()) {
1987 LLDB_LOG(log,
1988 "Failed to make function caller for implementation lookup: {0}.",
1989 error.AsCString());
1990 return {};
1991 }
1992
1993 return utility_fn;
1994}
1995
1996UtilityFunction *
1997AppleObjCRuntimeV2::SharedCacheClassInfoExtractor::GetClassInfoUtilityFunction(
1998 ExecutionContext &exe_ctx) {
1999 if (!m_utility_function)
2000 m_utility_function = GetClassInfoUtilityFunctionImpl(exe_ctx);
2001 return m_utility_function.get();
2002}
2003
2004AppleObjCRuntimeV2::DescriptorMapUpdateResult
2005AppleObjCRuntimeV2::DynamicClassInfoExtractor::UpdateISAToDescriptorMap(
2006 RemoteNXMapTable &hash_table) {
2007 Process *process = m_runtime.GetProcess();
2008 if (process == nullptr)
2009 return DescriptorMapUpdateResult::Fail();
2010
2011 uint32_t num_class_infos = 0;
2012
2013 Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types);
2014
2015 ExecutionContext exe_ctx;
2016
2017 ThreadSP thread_sp = process->GetThreadList().GetExpressionExecutionThread();
2018
2019 if (!thread_sp)
2020 return DescriptorMapUpdateResult::Fail();
2021
2022 if (!thread_sp->SafeToCallFunctions())
2023 return DescriptorMapUpdateResult::Retry();
2024
2025 thread_sp->CalculateExecutionContext(exe_ctx);
2026 TypeSystemClangSP scratch_ts_sp =
2027 ScratchTypeSystemClang::GetForTarget(target&: process->GetTarget());
2028
2029 if (!scratch_ts_sp)
2030 return DescriptorMapUpdateResult::Fail();
2031
2032 Address function_address;
2033
2034 const uint32_t addr_size = process->GetAddressByteSize();
2035
2036 Status err;
2037
2038 // Compute which helper we're going to use for this update.
2039 const DynamicClassInfoExtractor::Helper helper = ComputeHelper(exe_ctx);
2040
2041 // Read the total number of classes from the hash table
2042 const uint32_t num_classes =
2043 helper == DynamicClassInfoExtractor::gdb_objc_realized_classes
2044 ? hash_table.GetCount()
2045 : m_runtime.m_realized_class_generation_count;
2046 if (num_classes == 0) {
2047 LLDB_LOGF(log, "No dynamic classes found.");
2048 return DescriptorMapUpdateResult::Success(found: 0);
2049 }
2050
2051 UtilityFunction *get_class_info_code =
2052 GetClassInfoUtilityFunction(exe_ctx, helper);
2053 if (!get_class_info_code) {
2054 // The callee will have already logged a useful error message.
2055 return DescriptorMapUpdateResult::Fail();
2056 }
2057
2058 FunctionCaller *get_class_info_function =
2059 get_class_info_code->GetFunctionCaller();
2060
2061 if (!get_class_info_function) {
2062 LLDB_LOGF(log, "Failed to get implementation lookup function caller.");
2063 return DescriptorMapUpdateResult::Fail();
2064 }
2065
2066 ValueList arguments = get_class_info_function->GetArgumentValues();
2067
2068 DiagnosticManager diagnostics;
2069
2070 const uint32_t class_info_byte_size = addr_size + 4;
2071 const uint32_t class_infos_byte_size = num_classes * class_info_byte_size;
2072 lldb::addr_t class_infos_addr = process->AllocateMemory(
2073 size: class_infos_byte_size, permissions: ePermissionsReadable | ePermissionsWritable, error&: err);
2074
2075 if (class_infos_addr == LLDB_INVALID_ADDRESS) {
2076 LLDB_LOGF(log,
2077 "unable to allocate %" PRIu32
2078 " bytes in process for shared cache read",
2079 class_infos_byte_size);
2080 return DescriptorMapUpdateResult::Fail();
2081 }
2082
2083 auto deallocate_class_infos = llvm::make_scope_exit(F: [&] {
2084 // Deallocate the memory we allocated for the ClassInfo array
2085 if (class_infos_addr != LLDB_INVALID_ADDRESS)
2086 process->DeallocateMemory(ptr: class_infos_addr);
2087 });
2088
2089 lldb::addr_t class_buffer_addr = LLDB_INVALID_ADDRESS;
2090 const uint32_t class_byte_size = addr_size;
2091 const uint32_t class_buffer_len = num_classes;
2092 const uint32_t class_buffer_byte_size = class_buffer_len * class_byte_size;
2093 if (helper == Helper::objc_getRealizedClassList_trylock) {
2094 class_buffer_addr = process->AllocateMemory(
2095 size: class_buffer_byte_size, permissions: ePermissionsReadable | ePermissionsWritable,
2096 error&: err);
2097 if (class_buffer_addr == LLDB_INVALID_ADDRESS) {
2098 LLDB_LOGF(log,
2099 "unable to allocate %" PRIu32
2100 " bytes in process for shared cache read",
2101 class_buffer_byte_size);
2102 return DescriptorMapUpdateResult::Fail();
2103 }
2104 }
2105
2106 auto deallocate_class_buffer = llvm::make_scope_exit(F: [&] {
2107 // Deallocate the memory we allocated for the Class array
2108 if (class_buffer_addr != LLDB_INVALID_ADDRESS)
2109 process->DeallocateMemory(ptr: class_buffer_addr);
2110 });
2111
2112 std::lock_guard<std::mutex> guard(m_mutex);
2113
2114 // Fill in our function argument values
2115 uint32_t index = 0;
2116 arguments.GetValueAtIndex(idx: index++)->GetScalar() =
2117 hash_table.GetTableLoadAddress();
2118 arguments.GetValueAtIndex(idx: index++)->GetScalar() = class_infos_addr;
2119 arguments.GetValueAtIndex(idx: index++)->GetScalar() = class_infos_byte_size;
2120
2121 if (class_buffer_addr != LLDB_INVALID_ADDRESS) {
2122 arguments.GetValueAtIndex(idx: index++)->GetScalar() = class_buffer_addr;
2123 arguments.GetValueAtIndex(idx: index++)->GetScalar() = class_buffer_byte_size;
2124 }
2125
2126 // Only dump the runtime classes from the expression evaluation if the log is
2127 // verbose:
2128 Log *type_log = GetLog(mask: LLDBLog::Types);
2129 bool dump_log = type_log && type_log->GetVerbose();
2130
2131 arguments.GetValueAtIndex(idx: index++)->GetScalar() = dump_log ? 1 : 0;
2132
2133 bool success = false;
2134
2135 diagnostics.Clear();
2136
2137 // Write our function arguments into the process so we can run our function
2138 if (get_class_info_function->WriteFunctionArguments(
2139 exe_ctx, args_addr_ref&: GetClassInfoArgs(helper), arg_values&: arguments, diagnostic_manager&: diagnostics)) {
2140 EvaluateExpressionOptions options;
2141 options.SetUnwindOnError(true);
2142 options.SetTryAllThreads(false);
2143 options.SetStopOthers(true);
2144 options.SetIgnoreBreakpoints(true);
2145 options.SetTimeout(process->GetUtilityExpressionTimeout());
2146 options.SetIsForUtilityExpr(true);
2147
2148 CompilerType clang_uint32_t_type =
2149 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(encoding: eEncodingUint, bit_size: 32);
2150
2151 Value return_value;
2152 return_value.SetValueType(Value::ValueType::Scalar);
2153 return_value.SetCompilerType(clang_uint32_t_type);
2154 return_value.GetScalar() = 0;
2155
2156 diagnostics.Clear();
2157
2158 // Run the function
2159 ExpressionResults results = get_class_info_function->ExecuteFunction(
2160 exe_ctx, args_addr_ptr: &GetClassInfoArgs(helper), options, diagnostic_manager&: diagnostics, results&: return_value);
2161
2162 if (results == eExpressionCompleted) {
2163 // The result is the number of ClassInfo structures that were filled in
2164 num_class_infos = return_value.GetScalar().ULong();
2165 LLDB_LOG(log, "Discovered {0} Objective-C classes", num_class_infos);
2166 if (num_class_infos > 0) {
2167 // Read the ClassInfo structures
2168 DataBufferHeap buffer(num_class_infos * class_info_byte_size, 0);
2169 if (process->ReadMemory(vm_addr: class_infos_addr, buf: buffer.GetBytes(),
2170 size: buffer.GetByteSize(),
2171 error&: err) == buffer.GetByteSize()) {
2172 DataExtractor class_infos_data(buffer.GetBytes(),
2173 buffer.GetByteSize(),
2174 process->GetByteOrder(), addr_size);
2175 m_runtime.ParseClassInfoArray(data: class_infos_data, num_class_infos);
2176 }
2177 }
2178 success = true;
2179 } else {
2180 if (log) {
2181 LLDB_LOGF(log, "Error evaluating our find class name function.");
2182 diagnostics.Dump(log);
2183 }
2184 }
2185 } else {
2186 if (log) {
2187 LLDB_LOGF(log, "Error writing function arguments.");
2188 diagnostics.Dump(log);
2189 }
2190 }
2191
2192 return DescriptorMapUpdateResult(success, false, num_class_infos);
2193}
2194
2195uint32_t AppleObjCRuntimeV2::ParseClassInfoArray(const DataExtractor &data,
2196 uint32_t num_class_infos) {
2197 // Parses an array of "num_class_infos" packed ClassInfo structures:
2198 //
2199 // struct ClassInfo
2200 // {
2201 // Class isa;
2202 // uint32_t hash;
2203 // } __attribute__((__packed__));
2204
2205 Log *log = GetLog(mask: LLDBLog::Types);
2206 bool should_log = log && log->GetVerbose();
2207
2208 uint32_t num_parsed = 0;
2209
2210 // Iterate through all ClassInfo structures
2211 lldb::offset_t offset = 0;
2212 for (uint32_t i = 0; i < num_class_infos; ++i) {
2213 ObjCISA isa = data.GetAddress(offset_ptr: &offset);
2214
2215 if (isa == 0) {
2216 if (should_log)
2217 LLDB_LOGF(
2218 log, "AppleObjCRuntimeV2 found NULL isa, ignoring this class info");
2219 continue;
2220 }
2221 // Check if we already know about this ISA, if we do, the info will never
2222 // change, so we can just skip it.
2223 if (ISAIsCached(isa)) {
2224 if (should_log)
2225 LLDB_LOGF(log,
2226 "AppleObjCRuntimeV2 found cached isa=0x%" PRIx64
2227 ", ignoring this class info",
2228 isa);
2229 offset += 4;
2230 } else {
2231 // Read the 32 bit hash for the class name
2232 const uint32_t name_hash = data.GetU32(offset_ptr: &offset);
2233 ClassDescriptorSP descriptor_sp(
2234 new ClassDescriptorV2(*this, isa, nullptr));
2235
2236 // The code in g_get_shared_cache_class_info_body sets the value of the
2237 // hash to 0 to signal a demangled symbol. We use class_getName() in that
2238 // code to find the class name, but this returns a demangled name for
2239 // Swift symbols. For those symbols, recompute the hash here by reading
2240 // their name from the runtime.
2241 if (name_hash)
2242 AddClass(isa, descriptor_sp, class_name_hash: name_hash);
2243 else
2244 AddClass(isa, descriptor_sp,
2245 class_name: descriptor_sp->GetClassName().AsCString(value_if_empty: nullptr));
2246 num_parsed++;
2247 if (should_log)
2248 LLDB_LOGF(log,
2249 "AppleObjCRuntimeV2 added isa=0x%" PRIx64
2250 ", hash=0x%8.8x, name=%s",
2251 isa, name_hash,
2252 descriptor_sp->GetClassName().AsCString("<unknown>"));
2253 }
2254 }
2255 if (should_log)
2256 LLDB_LOGF(log, "AppleObjCRuntimeV2 parsed %" PRIu32 " class infos",
2257 num_parsed);
2258 return num_parsed;
2259}
2260
2261bool AppleObjCRuntimeV2::HasSymbol(ConstString Name) {
2262 if (!m_objc_module_sp)
2263 return false;
2264 if (const Symbol *symbol = m_objc_module_sp->FindFirstSymbolWithNameAndType(
2265 name: Name, symbol_type: lldb::eSymbolTypeCode)) {
2266 if (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())
2267 return true;
2268 }
2269 return false;
2270}
2271
2272AppleObjCRuntimeV2::DescriptorMapUpdateResult
2273AppleObjCRuntimeV2::SharedCacheClassInfoExtractor::UpdateISAToDescriptorMap() {
2274 Process *process = m_runtime.GetProcess();
2275 if (process == nullptr)
2276 return DescriptorMapUpdateResult::Fail();
2277
2278 Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types);
2279
2280 ExecutionContext exe_ctx;
2281
2282 ThreadSP thread_sp = process->GetThreadList().GetExpressionExecutionThread();
2283
2284 if (!thread_sp)
2285 return DescriptorMapUpdateResult::Fail();
2286
2287 if (!thread_sp->SafeToCallFunctions())
2288 return DescriptorMapUpdateResult::Retry();
2289
2290 thread_sp->CalculateExecutionContext(exe_ctx);
2291 TypeSystemClangSP scratch_ts_sp =
2292 ScratchTypeSystemClang::GetForTarget(target&: process->GetTarget());
2293
2294 if (!scratch_ts_sp)
2295 return DescriptorMapUpdateResult::Fail();
2296
2297 Address function_address;
2298
2299 const uint32_t addr_size = process->GetAddressByteSize();
2300
2301 Status err;
2302
2303 uint32_t num_class_infos = 0;
2304
2305 const lldb::addr_t objc_opt_ptr = m_runtime.GetSharedCacheReadOnlyAddress();
2306 const lldb::addr_t shared_cache_base_addr =
2307 m_runtime.GetSharedCacheBaseAddress();
2308
2309 if (objc_opt_ptr == LLDB_INVALID_ADDRESS ||
2310 shared_cache_base_addr == LLDB_INVALID_ADDRESS)
2311 return DescriptorMapUpdateResult::Fail();
2312
2313 // The number of entries to pre-allocate room for.
2314 // Each entry is (addrsize + 4) bytes
2315 // FIXME: It is not sustainable to continue incrementing this value every time
2316 // the shared cache grows. This is because it requires allocating memory in
2317 // the inferior process and some inferior processes have small memory limits.
2318 const uint32_t max_num_classes = 212992;
2319
2320 UtilityFunction *get_class_info_code = GetClassInfoUtilityFunction(exe_ctx);
2321 if (!get_class_info_code) {
2322 // The callee will have already logged a useful error message.
2323 return DescriptorMapUpdateResult::Fail();
2324 }
2325
2326 FunctionCaller *get_shared_cache_class_info_function =
2327 get_class_info_code->GetFunctionCaller();
2328
2329 if (!get_shared_cache_class_info_function) {
2330 LLDB_LOGF(log, "Failed to get implementation lookup function caller.");
2331 return DescriptorMapUpdateResult::Fail();
2332 }
2333
2334 ValueList arguments =
2335 get_shared_cache_class_info_function->GetArgumentValues();
2336
2337 DiagnosticManager diagnostics;
2338
2339 const uint32_t class_info_byte_size = addr_size + 4;
2340 const uint32_t class_infos_byte_size = max_num_classes * class_info_byte_size;
2341 lldb::addr_t class_infos_addr = process->AllocateMemory(
2342 size: class_infos_byte_size, permissions: ePermissionsReadable | ePermissionsWritable, error&: err);
2343 const uint32_t relative_selector_offset_addr_size = 64;
2344 lldb::addr_t relative_selector_offset_addr =
2345 process->AllocateMemory(size: relative_selector_offset_addr_size,
2346 permissions: ePermissionsReadable | ePermissionsWritable, error&: err);
2347
2348 if (class_infos_addr == LLDB_INVALID_ADDRESS) {
2349 LLDB_LOGF(log,
2350 "unable to allocate %" PRIu32
2351 " bytes in process for shared cache read",
2352 class_infos_byte_size);
2353 return DescriptorMapUpdateResult::Fail();
2354 }
2355
2356 std::lock_guard<std::mutex> guard(m_mutex);
2357
2358 // Fill in our function argument values
2359 arguments.GetValueAtIndex(idx: 0)->GetScalar() = objc_opt_ptr;
2360 arguments.GetValueAtIndex(idx: 1)->GetScalar() = shared_cache_base_addr;
2361 arguments.GetValueAtIndex(idx: 2)->GetScalar() = class_infos_addr;
2362 arguments.GetValueAtIndex(idx: 3)->GetScalar() = relative_selector_offset_addr;
2363 arguments.GetValueAtIndex(idx: 4)->GetScalar() = class_infos_byte_size;
2364 // Only dump the runtime classes from the expression evaluation if the log is
2365 // verbose:
2366 Log *type_log = GetLog(mask: LLDBLog::Types);
2367 bool dump_log = type_log && type_log->GetVerbose();
2368
2369 arguments.GetValueAtIndex(idx: 5)->GetScalar() = dump_log ? 1 : 0;
2370
2371 bool success = false;
2372
2373 diagnostics.Clear();
2374
2375 // Write our function arguments into the process so we can run our function
2376 if (get_shared_cache_class_info_function->WriteFunctionArguments(
2377 exe_ctx, args_addr_ref&: m_args, arg_values&: arguments, diagnostic_manager&: diagnostics)) {
2378 EvaluateExpressionOptions options;
2379 options.SetUnwindOnError(true);
2380 options.SetTryAllThreads(false);
2381 options.SetStopOthers(true);
2382 options.SetIgnoreBreakpoints(true);
2383 options.SetTimeout(process->GetUtilityExpressionTimeout());
2384 options.SetIsForUtilityExpr(true);
2385
2386 CompilerType clang_uint32_t_type =
2387 scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(encoding: eEncodingUint, bit_size: 32);
2388
2389 Value return_value;
2390 return_value.SetValueType(Value::ValueType::Scalar);
2391 return_value.SetCompilerType(clang_uint32_t_type);
2392 return_value.GetScalar() = 0;
2393
2394 diagnostics.Clear();
2395
2396 // Run the function
2397 ExpressionResults results =
2398 get_shared_cache_class_info_function->ExecuteFunction(
2399 exe_ctx, args_addr_ptr: &m_args, options, diagnostic_manager&: diagnostics, results&: return_value);
2400
2401 if (results == eExpressionCompleted) {
2402 // The result is the number of ClassInfo structures that were filled in
2403 num_class_infos = return_value.GetScalar().ULong();
2404 LLDB_LOG(log, "Discovered {0} Objective-C classes in the shared cache",
2405 num_class_infos);
2406 // Assert if there were more classes than we pre-allocated
2407 // room for.
2408 assert(num_class_infos <= max_num_classes);
2409 if (num_class_infos > 0) {
2410 if (num_class_infos > max_num_classes) {
2411 num_class_infos = max_num_classes;
2412
2413 success = false;
2414 } else {
2415 success = true;
2416 }
2417
2418 // Read the relative selector offset.
2419 DataBufferHeap relative_selector_offset_buffer(64, 0);
2420 if (process->ReadMemory(vm_addr: relative_selector_offset_addr,
2421 buf: relative_selector_offset_buffer.GetBytes(),
2422 size: relative_selector_offset_buffer.GetByteSize(),
2423 error&: err) ==
2424 relative_selector_offset_buffer.GetByteSize()) {
2425 DataExtractor relative_selector_offset_data(
2426 relative_selector_offset_buffer.GetBytes(),
2427 relative_selector_offset_buffer.GetByteSize(),
2428 process->GetByteOrder(), addr_size);
2429 lldb::offset_t offset = 0;
2430 uint64_t relative_selector_offset =
2431 relative_selector_offset_data.GetU64(offset_ptr: &offset);
2432 if (relative_selector_offset > 0) {
2433 // The offset is relative to the objc_opt struct.
2434 m_runtime.SetRelativeSelectorBaseAddr(objc_opt_ptr +
2435 relative_selector_offset);
2436 }
2437 }
2438
2439 // Read the ClassInfo structures
2440 DataBufferHeap class_infos_buffer(
2441 num_class_infos * class_info_byte_size, 0);
2442 if (process->ReadMemory(vm_addr: class_infos_addr, buf: class_infos_buffer.GetBytes(),
2443 size: class_infos_buffer.GetByteSize(),
2444 error&: err) == class_infos_buffer.GetByteSize()) {
2445 DataExtractor class_infos_data(class_infos_buffer.GetBytes(),
2446 class_infos_buffer.GetByteSize(),
2447 process->GetByteOrder(), addr_size);
2448
2449 m_runtime.ParseClassInfoArray(data: class_infos_data, num_class_infos);
2450 }
2451 } else {
2452 success = true;
2453 }
2454 } else {
2455 if (log) {
2456 LLDB_LOGF(log, "Error evaluating our find class name function.");
2457 diagnostics.Dump(log);
2458 }
2459 }
2460 } else {
2461 if (log) {
2462 LLDB_LOGF(log, "Error writing function arguments.");
2463 diagnostics.Dump(log);
2464 }
2465 }
2466
2467 // Deallocate the memory we allocated for the ClassInfo array
2468 process->DeallocateMemory(ptr: class_infos_addr);
2469
2470 return DescriptorMapUpdateResult(success, false, num_class_infos);
2471}
2472
2473lldb::addr_t AppleObjCRuntimeV2::GetSharedCacheReadOnlyAddress() {
2474 Process *process = GetProcess();
2475
2476 if (process) {
2477 ModuleSP objc_module_sp(GetObjCModule());
2478
2479 if (objc_module_sp) {
2480 ObjectFile *objc_object = objc_module_sp->GetObjectFile();
2481
2482 if (objc_object) {
2483 SectionList *section_list = objc_module_sp->GetSectionList();
2484
2485 if (section_list) {
2486 SectionSP text_segment_sp(
2487 section_list->FindSectionByName(section_dstr: ConstString("__TEXT")));
2488
2489 if (text_segment_sp) {
2490 SectionSP objc_opt_section_sp(
2491 text_segment_sp->GetChildren().FindSectionByName(
2492 section_dstr: ConstString("__objc_opt_ro")));
2493
2494 if (objc_opt_section_sp) {
2495 return objc_opt_section_sp->GetLoadBaseAddress(
2496 target: &process->GetTarget());
2497 }
2498 }
2499 }
2500 }
2501 }
2502 }
2503 return LLDB_INVALID_ADDRESS;
2504}
2505
2506lldb::addr_t AppleObjCRuntimeV2::GetSharedCacheBaseAddress() {
2507 StructuredData::ObjectSP info = m_process->GetSharedCacheInfo();
2508 if (!info)
2509 return LLDB_INVALID_ADDRESS;
2510
2511 StructuredData::Dictionary *info_dict = info->GetAsDictionary();
2512 if (!info_dict)
2513 return LLDB_INVALID_ADDRESS;
2514
2515 StructuredData::ObjectSP value =
2516 info_dict->GetValueForKey(key: "shared_cache_base_address");
2517 if (!value)
2518 return LLDB_INVALID_ADDRESS;
2519
2520 return value->GetUnsignedIntegerValue(LLDB_INVALID_ADDRESS);
2521}
2522
2523void AppleObjCRuntimeV2::UpdateISAToDescriptorMapIfNeeded() {
2524 LLDB_SCOPED_TIMER();
2525
2526 Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types);
2527
2528 // Else we need to check with our process to see when the map was updated.
2529 Process *process = GetProcess();
2530
2531 if (process) {
2532 RemoteNXMapTable hash_table;
2533
2534 // Update the process stop ID that indicates the last time we updated the
2535 // map, whether it was successful or not.
2536 m_isa_to_descriptor_stop_id = process->GetStopID();
2537
2538 // Ask the runtime is the realized class generation count changed. Unlike
2539 // the hash table, this accounts for lazily named classes.
2540 const bool class_count_changed = RealizedClassGenerationCountChanged();
2541
2542 if (!m_hash_signature.NeedsUpdate(process, runtime: this, hash_table) &&
2543 !class_count_changed)
2544 return;
2545
2546 m_hash_signature.UpdateSignature(hash_table);
2547
2548 // Grab the dynamically loaded Objective-C classes from memory.
2549 DescriptorMapUpdateResult dynamic_update_result =
2550 m_dynamic_class_info_extractor.UpdateISAToDescriptorMap(hash_table);
2551
2552 // Now get the objc classes that are baked into the Objective-C runtime in
2553 // the shared cache, but only once per process as this data never changes
2554 if (!m_loaded_objc_opt) {
2555 // it is legitimately possible for the shared cache to be empty - in that
2556 // case, the dynamic hash table will contain all the class information we
2557 // need; the situation we're trying to detect is one where we aren't
2558 // seeing class information from the runtime - in order to detect that
2559 // vs. just the shared cache being empty or sparsely populated, we set an
2560 // arbitrary (very low) threshold for the number of classes that we want
2561 // to see in a "good" scenario - anything below that is suspicious
2562 // (Foundation alone has thousands of classes)
2563 const uint32_t num_classes_to_warn_at = 500;
2564
2565 DescriptorMapUpdateResult shared_cache_update_result =
2566 m_shared_cache_class_info_extractor.UpdateISAToDescriptorMap();
2567
2568 LLDB_LOGF(log,
2569 "attempted to read objc class data - results: "
2570 "[dynamic_update]: ran: %s, retry: %s, count: %" PRIu32
2571 " [shared_cache_update]: ran: %s, retry: %s, count: %" PRIu32,
2572 dynamic_update_result.m_update_ran ? "yes" : "no",
2573 dynamic_update_result.m_retry_update ? "yes" : "no",
2574 dynamic_update_result.m_num_found,
2575 shared_cache_update_result.m_update_ran ? "yes" : "no",
2576 shared_cache_update_result.m_retry_update ? "yes" : "no",
2577 shared_cache_update_result.m_num_found);
2578
2579 // warn if:
2580 // - we could not run either expression
2581 // - we found fewer than num_classes_to_warn_at classes total
2582 if (dynamic_update_result.m_retry_update ||
2583 shared_cache_update_result.m_retry_update)
2584 WarnIfNoClassesCached(reason: SharedCacheWarningReason::eExpressionUnableToRun);
2585 else if ((!shared_cache_update_result.m_update_ran) ||
2586 (!dynamic_update_result.m_update_ran))
2587 WarnIfNoClassesCached(
2588 reason: SharedCacheWarningReason::eExpressionExecutionFailure);
2589 else if (dynamic_update_result.m_num_found +
2590 shared_cache_update_result.m_num_found <
2591 num_classes_to_warn_at)
2592 WarnIfNoClassesCached(reason: SharedCacheWarningReason::eNotEnoughClassesRead);
2593 else
2594 m_loaded_objc_opt = true;
2595 }
2596 } else {
2597 m_isa_to_descriptor_stop_id = UINT32_MAX;
2598 }
2599}
2600
2601bool AppleObjCRuntimeV2::RealizedClassGenerationCountChanged() {
2602 Process *process = GetProcess();
2603 if (!process)
2604 return false;
2605
2606 Status error;
2607 uint64_t objc_debug_realized_class_generation_count =
2608 ExtractRuntimeGlobalSymbol(
2609 process, name: ConstString("objc_debug_realized_class_generation_count"),
2610 module_sp: GetObjCModule(), error);
2611 if (error.Fail())
2612 return false;
2613
2614 if (m_realized_class_generation_count ==
2615 objc_debug_realized_class_generation_count)
2616 return false;
2617
2618 Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types);
2619 LLDB_LOG(log,
2620 "objc_debug_realized_class_generation_count changed from {0} to {1}",
2621 m_realized_class_generation_count,
2622 objc_debug_realized_class_generation_count);
2623
2624 m_realized_class_generation_count =
2625 objc_debug_realized_class_generation_count;
2626
2627 return true;
2628}
2629
2630static bool DoesProcessHaveSharedCache(Process &process) {
2631 PlatformSP platform_sp = process.GetTarget().GetPlatform();
2632 if (!platform_sp)
2633 return true; // this should not happen
2634
2635 llvm::StringRef platform_plugin_name_sr = platform_sp->GetPluginName();
2636 if (platform_plugin_name_sr.ends_with(Suffix: "-simulator"))
2637 return false;
2638
2639 return true;
2640}
2641
2642void AppleObjCRuntimeV2::WarnIfNoClassesCached(
2643 SharedCacheWarningReason reason) {
2644 if (GetProcess() && !DoesProcessHaveSharedCache(process&: *GetProcess())) {
2645 // Simulators do not have the objc_opt_ro class table so don't actually
2646 // complain to the user
2647 return;
2648 }
2649
2650 Debugger &debugger(GetProcess()->GetTarget().GetDebugger());
2651 switch (reason) {
2652 case SharedCacheWarningReason::eNotEnoughClassesRead:
2653 Debugger::ReportWarning(message: "could not find Objective-C class data in "
2654 "the process. This may reduce the quality of type "
2655 "information available.\n",
2656 debugger_id: debugger.GetID(), once: &m_no_classes_cached_warning);
2657 break;
2658 case SharedCacheWarningReason::eExpressionExecutionFailure:
2659 Debugger::ReportWarning(
2660 message: "could not execute support code to read "
2661 "Objective-C class data in the process. This may "
2662 "reduce the quality of type information available.\n",
2663 debugger_id: debugger.GetID(), once: &m_no_classes_cached_warning);
2664 break;
2665 case SharedCacheWarningReason::eExpressionUnableToRun:
2666 Debugger::ReportWarning(
2667 message: "could not execute support code to read Objective-C class data because "
2668 "it's not yet safe to do so, and will be retried later.\n",
2669 debugger_id: debugger.GetID(), once: nullptr);
2670 break;
2671 }
2672}
2673
2674void AppleObjCRuntimeV2::WarnIfNoExpandedSharedCache() {
2675 if (!m_objc_module_sp)
2676 return;
2677
2678 ObjectFile *object_file = m_objc_module_sp->GetObjectFile();
2679 if (!object_file)
2680 return;
2681
2682 if (!object_file->IsInMemory())
2683 return;
2684
2685 if (!GetProcess()->IsLiveDebugSession())
2686 return;
2687
2688 Target &target = GetProcess()->GetTarget();
2689 Debugger &debugger = target.GetDebugger();
2690
2691 std::string buffer;
2692 llvm::raw_string_ostream os(buffer);
2693
2694 os << "libobjc.A.dylib is being read from process memory. This "
2695 "indicates that LLDB could not ";
2696 if (PlatformSP platform_sp = target.GetPlatform()) {
2697 if (platform_sp->IsHost()) {
2698 os << "read from the host's in-memory shared cache";
2699 } else {
2700 os << "find the on-disk shared cache for this device";
2701 }
2702 } else {
2703 os << "read from the shared cache";
2704 }
2705 os << ". This will likely reduce debugging performance.\n";
2706
2707 Debugger::ReportWarning(message: buffer, debugger_id: debugger.GetID(),
2708 once: &m_no_expanded_cache_warning);
2709}
2710
2711DeclVendor *AppleObjCRuntimeV2::GetDeclVendor() {
2712 if (!m_decl_vendor_up)
2713 m_decl_vendor_up = std::make_unique<AppleObjCDeclVendor>(args&: *this);
2714
2715 return m_decl_vendor_up.get();
2716}
2717
2718lldb::addr_t AppleObjCRuntimeV2::LookupRuntimeSymbol(ConstString name) {
2719 lldb::addr_t ret = LLDB_INVALID_ADDRESS;
2720
2721 const char *name_cstr = name.AsCString();
2722
2723 if (name_cstr) {
2724 llvm::StringRef name_strref(name_cstr);
2725
2726 llvm::StringRef ivar_prefix("OBJC_IVAR_$_");
2727 llvm::StringRef class_prefix("OBJC_CLASS_$_");
2728
2729 if (name_strref.starts_with(Prefix: ivar_prefix)) {
2730 llvm::StringRef ivar_skipped_prefix =
2731 name_strref.substr(Start: ivar_prefix.size());
2732 std::pair<llvm::StringRef, llvm::StringRef> class_and_ivar =
2733 ivar_skipped_prefix.split(Separator: '.');
2734
2735 if (!class_and_ivar.first.empty() && !class_and_ivar.second.empty()) {
2736 const ConstString class_name_cs(class_and_ivar.first);
2737 ClassDescriptorSP descriptor =
2738 ObjCLanguageRuntime::GetClassDescriptorFromClassName(class_name: class_name_cs);
2739
2740 if (descriptor) {
2741 const ConstString ivar_name_cs(class_and_ivar.second);
2742 const char *ivar_name_cstr = ivar_name_cs.AsCString();
2743
2744 auto ivar_func = [&ret,
2745 ivar_name_cstr](const char *name, const char *type,
2746 lldb::addr_t offset_addr,
2747 uint64_t size) -> lldb::addr_t {
2748 if (!strcmp(s1: name, s2: ivar_name_cstr)) {
2749 ret = offset_addr;
2750 return true;
2751 }
2752 return false;
2753 };
2754
2755 descriptor->Describe(
2756 superclass_func: std::function<void(ObjCISA)>(nullptr),
2757 instance_method_func: std::function<bool(const char *, const char *)>(nullptr),
2758 class_method_func: std::function<bool(const char *, const char *)>(nullptr),
2759 ivar_func);
2760 }
2761 }
2762 } else if (name_strref.starts_with(Prefix: class_prefix)) {
2763 llvm::StringRef class_skipped_prefix =
2764 name_strref.substr(Start: class_prefix.size());
2765 const ConstString class_name_cs(class_skipped_prefix);
2766 ClassDescriptorSP descriptor =
2767 GetClassDescriptorFromClassName(class_name: class_name_cs);
2768
2769 if (descriptor)
2770 ret = descriptor->GetISA();
2771 }
2772 }
2773
2774 return ret;
2775}
2776
2777AppleObjCRuntimeV2::NonPointerISACache *
2778AppleObjCRuntimeV2::NonPointerISACache::CreateInstance(
2779 AppleObjCRuntimeV2 &runtime, const lldb::ModuleSP &objc_module_sp) {
2780 Process *process(runtime.GetProcess());
2781
2782 Status error;
2783
2784 Log *log = GetLog(mask: LLDBLog::Types);
2785
2786 auto objc_debug_isa_magic_mask = ExtractRuntimeGlobalSymbol(
2787 process, name: ConstString("objc_debug_isa_magic_mask"), module_sp: objc_module_sp, error);
2788 if (error.Fail())
2789 return nullptr;
2790
2791 auto objc_debug_isa_magic_value = ExtractRuntimeGlobalSymbol(
2792 process, name: ConstString("objc_debug_isa_magic_value"), module_sp: objc_module_sp,
2793 error);
2794 if (error.Fail())
2795 return nullptr;
2796
2797 auto objc_debug_isa_class_mask = ExtractRuntimeGlobalSymbol(
2798 process, name: ConstString("objc_debug_isa_class_mask"), module_sp: objc_module_sp, error);
2799 if (error.Fail())
2800 return nullptr;
2801
2802 if (log)
2803 log->PutCString(cstr: "AOCRT::NPI: Found all the non-indexed ISA masks");
2804
2805 bool foundError = false;
2806 auto objc_debug_indexed_isa_magic_mask = ExtractRuntimeGlobalSymbol(
2807 process, name: ConstString("objc_debug_indexed_isa_magic_mask"), module_sp: objc_module_sp,
2808 error);
2809 foundError |= error.Fail();
2810
2811 auto objc_debug_indexed_isa_magic_value = ExtractRuntimeGlobalSymbol(
2812 process, name: ConstString("objc_debug_indexed_isa_magic_value"),
2813 module_sp: objc_module_sp, error);
2814 foundError |= error.Fail();
2815
2816 auto objc_debug_indexed_isa_index_mask = ExtractRuntimeGlobalSymbol(
2817 process, name: ConstString("objc_debug_indexed_isa_index_mask"), module_sp: objc_module_sp,
2818 error);
2819 foundError |= error.Fail();
2820
2821 auto objc_debug_indexed_isa_index_shift = ExtractRuntimeGlobalSymbol(
2822 process, name: ConstString("objc_debug_indexed_isa_index_shift"),
2823 module_sp: objc_module_sp, error);
2824 foundError |= error.Fail();
2825
2826 auto objc_indexed_classes =
2827 ExtractRuntimeGlobalSymbol(process, name: ConstString("objc_indexed_classes"),
2828 module_sp: objc_module_sp, error, read_value: false);
2829 foundError |= error.Fail();
2830
2831 if (log)
2832 log->PutCString(cstr: "AOCRT::NPI: Found all the indexed ISA masks");
2833
2834 // we might want to have some rules to outlaw these other values (e.g if the
2835 // mask is zero but the value is non-zero, ...)
2836
2837 return new NonPointerISACache(
2838 runtime, objc_module_sp, objc_debug_isa_class_mask,
2839 objc_debug_isa_magic_mask, objc_debug_isa_magic_value,
2840 objc_debug_indexed_isa_magic_mask, objc_debug_indexed_isa_magic_value,
2841 objc_debug_indexed_isa_index_mask, objc_debug_indexed_isa_index_shift,
2842 foundError ? 0 : objc_indexed_classes);
2843}
2844
2845AppleObjCRuntimeV2::TaggedPointerVendorV2 *
2846AppleObjCRuntimeV2::TaggedPointerVendorV2::CreateInstance(
2847 AppleObjCRuntimeV2 &runtime, const lldb::ModuleSP &objc_module_sp) {
2848 Process *process(runtime.GetProcess());
2849
2850 Status error;
2851
2852 auto objc_debug_taggedpointer_mask = ExtractRuntimeGlobalSymbol(
2853 process, name: ConstString("objc_debug_taggedpointer_mask"), module_sp: objc_module_sp,
2854 error);
2855 if (error.Fail())
2856 return new TaggedPointerVendorLegacy(runtime);
2857
2858 auto objc_debug_taggedpointer_slot_shift = ExtractRuntimeGlobalSymbol(
2859 process, name: ConstString("objc_debug_taggedpointer_slot_shift"),
2860 module_sp: objc_module_sp, error, read_value: true, byte_size: 4);
2861 if (error.Fail())
2862 return new TaggedPointerVendorLegacy(runtime);
2863
2864 auto objc_debug_taggedpointer_slot_mask = ExtractRuntimeGlobalSymbol(
2865 process, name: ConstString("objc_debug_taggedpointer_slot_mask"),
2866 module_sp: objc_module_sp, error, read_value: true, byte_size: 4);
2867 if (error.Fail())
2868 return new TaggedPointerVendorLegacy(runtime);
2869
2870 auto objc_debug_taggedpointer_payload_lshift = ExtractRuntimeGlobalSymbol(
2871 process, name: ConstString("objc_debug_taggedpointer_payload_lshift"),
2872 module_sp: objc_module_sp, error, read_value: true, byte_size: 4);
2873 if (error.Fail())
2874 return new TaggedPointerVendorLegacy(runtime);
2875
2876 auto objc_debug_taggedpointer_payload_rshift = ExtractRuntimeGlobalSymbol(
2877 process, name: ConstString("objc_debug_taggedpointer_payload_rshift"),
2878 module_sp: objc_module_sp, error, read_value: true, byte_size: 4);
2879 if (error.Fail())
2880 return new TaggedPointerVendorLegacy(runtime);
2881
2882 auto objc_debug_taggedpointer_classes = ExtractRuntimeGlobalSymbol(
2883 process, name: ConstString("objc_debug_taggedpointer_classes"), module_sp: objc_module_sp,
2884 error, read_value: false);
2885 if (error.Fail())
2886 return new TaggedPointerVendorLegacy(runtime);
2887
2888 // try to detect the "extended tagged pointer" variables - if any are
2889 // missing, use the non-extended vendor
2890 do {
2891 auto objc_debug_taggedpointer_ext_mask = ExtractRuntimeGlobalSymbol(
2892 process, name: ConstString("objc_debug_taggedpointer_ext_mask"),
2893 module_sp: objc_module_sp, error);
2894 if (error.Fail())
2895 break;
2896
2897 auto objc_debug_taggedpointer_ext_slot_shift = ExtractRuntimeGlobalSymbol(
2898 process, name: ConstString("objc_debug_taggedpointer_ext_slot_shift"),
2899 module_sp: objc_module_sp, error, read_value: true, byte_size: 4);
2900 if (error.Fail())
2901 break;
2902
2903 auto objc_debug_taggedpointer_ext_slot_mask = ExtractRuntimeGlobalSymbol(
2904 process, name: ConstString("objc_debug_taggedpointer_ext_slot_mask"),
2905 module_sp: objc_module_sp, error, read_value: true, byte_size: 4);
2906 if (error.Fail())
2907 break;
2908
2909 auto objc_debug_taggedpointer_ext_classes = ExtractRuntimeGlobalSymbol(
2910 process, name: ConstString("objc_debug_taggedpointer_ext_classes"),
2911 module_sp: objc_module_sp, error, read_value: false);
2912 if (error.Fail())
2913 break;
2914
2915 auto objc_debug_taggedpointer_ext_payload_lshift =
2916 ExtractRuntimeGlobalSymbol(
2917 process, name: ConstString("objc_debug_taggedpointer_ext_payload_lshift"),
2918 module_sp: objc_module_sp, error, read_value: true, byte_size: 4);
2919 if (error.Fail())
2920 break;
2921
2922 auto objc_debug_taggedpointer_ext_payload_rshift =
2923 ExtractRuntimeGlobalSymbol(
2924 process, name: ConstString("objc_debug_taggedpointer_ext_payload_rshift"),
2925 module_sp: objc_module_sp, error, read_value: true, byte_size: 4);
2926 if (error.Fail())
2927 break;
2928
2929 return new TaggedPointerVendorExtended(
2930 runtime, objc_debug_taggedpointer_mask,
2931 objc_debug_taggedpointer_ext_mask, objc_debug_taggedpointer_slot_shift,
2932 objc_debug_taggedpointer_ext_slot_shift,
2933 objc_debug_taggedpointer_slot_mask,
2934 objc_debug_taggedpointer_ext_slot_mask,
2935 objc_debug_taggedpointer_payload_lshift,
2936 objc_debug_taggedpointer_payload_rshift,
2937 objc_debug_taggedpointer_ext_payload_lshift,
2938 objc_debug_taggedpointer_ext_payload_rshift,
2939 objc_debug_taggedpointer_classes, objc_debug_taggedpointer_ext_classes);
2940 } while (false);
2941
2942 // we might want to have some rules to outlaw these values (e.g if the
2943 // table's address is zero)
2944
2945 return new TaggedPointerVendorRuntimeAssisted(
2946 runtime, objc_debug_taggedpointer_mask,
2947 objc_debug_taggedpointer_slot_shift, objc_debug_taggedpointer_slot_mask,
2948 objc_debug_taggedpointer_payload_lshift,
2949 objc_debug_taggedpointer_payload_rshift,
2950 objc_debug_taggedpointer_classes);
2951}
2952
2953bool AppleObjCRuntimeV2::TaggedPointerVendorLegacy::IsPossibleTaggedPointer(
2954 lldb::addr_t ptr) {
2955 return (ptr & 1);
2956}
2957
2958ObjCLanguageRuntime::ClassDescriptorSP
2959AppleObjCRuntimeV2::TaggedPointerVendorLegacy::GetClassDescriptor(
2960 lldb::addr_t ptr) {
2961 if (!IsPossibleTaggedPointer(ptr))
2962 return ObjCLanguageRuntime::ClassDescriptorSP();
2963
2964 uint32_t foundation_version = m_runtime.GetFoundationVersion();
2965
2966 if (foundation_version == LLDB_INVALID_MODULE_VERSION)
2967 return ObjCLanguageRuntime::ClassDescriptorSP();
2968
2969 uint64_t class_bits = (ptr & 0xE) >> 1;
2970 ConstString name;
2971
2972 static ConstString g_NSAtom("NSAtom");
2973 static ConstString g_NSNumber("NSNumber");
2974 static ConstString g_NSDateTS("NSDateTS");
2975 static ConstString g_NSManagedObject("NSManagedObject");
2976 static ConstString g_NSDate("NSDate");
2977
2978 if (foundation_version >= 900) {
2979 switch (class_bits) {
2980 case 0:
2981 name = g_NSAtom;
2982 break;
2983 case 3:
2984 name = g_NSNumber;
2985 break;
2986 case 4:
2987 name = g_NSDateTS;
2988 break;
2989 case 5:
2990 name = g_NSManagedObject;
2991 break;
2992 case 6:
2993 name = g_NSDate;
2994 break;
2995 default:
2996 return ObjCLanguageRuntime::ClassDescriptorSP();
2997 }
2998 } else {
2999 switch (class_bits) {
3000 case 1:
3001 name = g_NSNumber;
3002 break;
3003 case 5:
3004 name = g_NSManagedObject;
3005 break;
3006 case 6:
3007 name = g_NSDate;
3008 break;
3009 case 7:
3010 name = g_NSDateTS;
3011 break;
3012 default:
3013 return ObjCLanguageRuntime::ClassDescriptorSP();
3014 }
3015 }
3016
3017 lldb::addr_t unobfuscated = ptr ^ m_runtime.GetTaggedPointerObfuscator();
3018 return ClassDescriptorSP(new ClassDescriptorV2Tagged(name, unobfuscated));
3019}
3020
3021AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted::
3022 TaggedPointerVendorRuntimeAssisted(
3023 AppleObjCRuntimeV2 &runtime, uint64_t objc_debug_taggedpointer_mask,
3024 uint32_t objc_debug_taggedpointer_slot_shift,
3025 uint32_t objc_debug_taggedpointer_slot_mask,
3026 uint32_t objc_debug_taggedpointer_payload_lshift,
3027 uint32_t objc_debug_taggedpointer_payload_rshift,
3028 lldb::addr_t objc_debug_taggedpointer_classes)
3029 : TaggedPointerVendorV2(runtime), m_cache(),
3030 m_objc_debug_taggedpointer_mask(objc_debug_taggedpointer_mask),
3031 m_objc_debug_taggedpointer_slot_shift(
3032 objc_debug_taggedpointer_slot_shift),
3033 m_objc_debug_taggedpointer_slot_mask(objc_debug_taggedpointer_slot_mask),
3034 m_objc_debug_taggedpointer_payload_lshift(
3035 objc_debug_taggedpointer_payload_lshift),
3036 m_objc_debug_taggedpointer_payload_rshift(
3037 objc_debug_taggedpointer_payload_rshift),
3038 m_objc_debug_taggedpointer_classes(objc_debug_taggedpointer_classes) {}
3039
3040bool AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted::
3041 IsPossibleTaggedPointer(lldb::addr_t ptr) {
3042 return (ptr & m_objc_debug_taggedpointer_mask) != 0;
3043}
3044
3045ObjCLanguageRuntime::ClassDescriptorSP
3046AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted::GetClassDescriptor(
3047 lldb::addr_t ptr) {
3048 ClassDescriptorSP actual_class_descriptor_sp;
3049 uint64_t unobfuscated = (ptr) ^ m_runtime.GetTaggedPointerObfuscator();
3050
3051 if (!IsPossibleTaggedPointer(ptr: unobfuscated))
3052 return ObjCLanguageRuntime::ClassDescriptorSP();
3053
3054 uintptr_t slot = (ptr >> m_objc_debug_taggedpointer_slot_shift) &
3055 m_objc_debug_taggedpointer_slot_mask;
3056
3057 CacheIterator iterator = m_cache.find(x: slot), end = m_cache.end();
3058 if (iterator != end) {
3059 actual_class_descriptor_sp = iterator->second;
3060 } else {
3061 Process *process(m_runtime.GetProcess());
3062 uintptr_t slot_ptr = slot * process->GetAddressByteSize() +
3063 m_objc_debug_taggedpointer_classes;
3064 Status error;
3065 uintptr_t slot_data = process->ReadPointerFromMemory(vm_addr: slot_ptr, error);
3066 if (error.Fail() || slot_data == 0 ||
3067 slot_data == uintptr_t(LLDB_INVALID_ADDRESS))
3068 return nullptr;
3069 actual_class_descriptor_sp =
3070 m_runtime.GetClassDescriptorFromISA(isa: (ObjCISA)slot_data);
3071 if (!actual_class_descriptor_sp) {
3072 if (ABISP abi_sp = process->GetABI()) {
3073 ObjCISA fixed_isa = abi_sp->FixCodeAddress(pc: (ObjCISA)slot_data);
3074 actual_class_descriptor_sp =
3075 m_runtime.GetClassDescriptorFromISA(isa: fixed_isa);
3076 }
3077 }
3078 if (!actual_class_descriptor_sp)
3079 return ObjCLanguageRuntime::ClassDescriptorSP();
3080 m_cache[slot] = actual_class_descriptor_sp;
3081 }
3082
3083 uint64_t data_payload =
3084 ((unobfuscated << m_objc_debug_taggedpointer_payload_lshift) >>
3085 m_objc_debug_taggedpointer_payload_rshift);
3086 int64_t data_payload_signed =
3087 ((int64_t)(unobfuscated << m_objc_debug_taggedpointer_payload_lshift) >>
3088 m_objc_debug_taggedpointer_payload_rshift);
3089 return ClassDescriptorSP(new ClassDescriptorV2Tagged(
3090 actual_class_descriptor_sp, data_payload, data_payload_signed));
3091}
3092
3093AppleObjCRuntimeV2::TaggedPointerVendorExtended::TaggedPointerVendorExtended(
3094 AppleObjCRuntimeV2 &runtime, uint64_t objc_debug_taggedpointer_mask,
3095 uint64_t objc_debug_taggedpointer_ext_mask,
3096 uint32_t objc_debug_taggedpointer_slot_shift,
3097 uint32_t objc_debug_taggedpointer_ext_slot_shift,
3098 uint32_t objc_debug_taggedpointer_slot_mask,
3099 uint32_t objc_debug_taggedpointer_ext_slot_mask,
3100 uint32_t objc_debug_taggedpointer_payload_lshift,
3101 uint32_t objc_debug_taggedpointer_payload_rshift,
3102 uint32_t objc_debug_taggedpointer_ext_payload_lshift,
3103 uint32_t objc_debug_taggedpointer_ext_payload_rshift,
3104 lldb::addr_t objc_debug_taggedpointer_classes,
3105 lldb::addr_t objc_debug_taggedpointer_ext_classes)
3106 : TaggedPointerVendorRuntimeAssisted(
3107 runtime, objc_debug_taggedpointer_mask,
3108 objc_debug_taggedpointer_slot_shift,
3109 objc_debug_taggedpointer_slot_mask,
3110 objc_debug_taggedpointer_payload_lshift,
3111 objc_debug_taggedpointer_payload_rshift,
3112 objc_debug_taggedpointer_classes),
3113 m_ext_cache(),
3114 m_objc_debug_taggedpointer_ext_mask(objc_debug_taggedpointer_ext_mask),
3115 m_objc_debug_taggedpointer_ext_slot_shift(
3116 objc_debug_taggedpointer_ext_slot_shift),
3117 m_objc_debug_taggedpointer_ext_slot_mask(
3118 objc_debug_taggedpointer_ext_slot_mask),
3119 m_objc_debug_taggedpointer_ext_payload_lshift(
3120 objc_debug_taggedpointer_ext_payload_lshift),
3121 m_objc_debug_taggedpointer_ext_payload_rshift(
3122 objc_debug_taggedpointer_ext_payload_rshift),
3123 m_objc_debug_taggedpointer_ext_classes(
3124 objc_debug_taggedpointer_ext_classes) {}
3125
3126bool AppleObjCRuntimeV2::TaggedPointerVendorExtended::
3127 IsPossibleExtendedTaggedPointer(lldb::addr_t ptr) {
3128 if (!IsPossibleTaggedPointer(ptr))
3129 return false;
3130
3131 if (m_objc_debug_taggedpointer_ext_mask == 0)
3132 return false;
3133
3134 return ((ptr & m_objc_debug_taggedpointer_ext_mask) ==
3135 m_objc_debug_taggedpointer_ext_mask);
3136}
3137
3138ObjCLanguageRuntime::ClassDescriptorSP
3139AppleObjCRuntimeV2::TaggedPointerVendorExtended::GetClassDescriptor(
3140 lldb::addr_t ptr) {
3141 ClassDescriptorSP actual_class_descriptor_sp;
3142 uint64_t unobfuscated = (ptr) ^ m_runtime.GetTaggedPointerObfuscator();
3143
3144 if (!IsPossibleTaggedPointer(ptr: unobfuscated))
3145 return ObjCLanguageRuntime::ClassDescriptorSP();
3146
3147 if (!IsPossibleExtendedTaggedPointer(ptr: unobfuscated))
3148 return this->TaggedPointerVendorRuntimeAssisted::GetClassDescriptor(ptr);
3149
3150 uintptr_t slot = (ptr >> m_objc_debug_taggedpointer_ext_slot_shift) &
3151 m_objc_debug_taggedpointer_ext_slot_mask;
3152
3153 CacheIterator iterator = m_ext_cache.find(x: slot), end = m_ext_cache.end();
3154 if (iterator != end) {
3155 actual_class_descriptor_sp = iterator->second;
3156 } else {
3157 Process *process(m_runtime.GetProcess());
3158 uintptr_t slot_ptr = slot * process->GetAddressByteSize() +
3159 m_objc_debug_taggedpointer_ext_classes;
3160 Status error;
3161 uintptr_t slot_data = process->ReadPointerFromMemory(vm_addr: slot_ptr, error);
3162 if (error.Fail() || slot_data == 0 ||
3163 slot_data == uintptr_t(LLDB_INVALID_ADDRESS))
3164 return nullptr;
3165 actual_class_descriptor_sp =
3166 m_runtime.GetClassDescriptorFromISA(isa: (ObjCISA)slot_data);
3167 if (!actual_class_descriptor_sp)
3168 return ObjCLanguageRuntime::ClassDescriptorSP();
3169 m_ext_cache[slot] = actual_class_descriptor_sp;
3170 }
3171
3172 uint64_t data_payload = (((uint64_t)unobfuscated
3173 << m_objc_debug_taggedpointer_ext_payload_lshift) >>
3174 m_objc_debug_taggedpointer_ext_payload_rshift);
3175 int64_t data_payload_signed =
3176 ((int64_t)((uint64_t)unobfuscated
3177 << m_objc_debug_taggedpointer_ext_payload_lshift) >>
3178 m_objc_debug_taggedpointer_ext_payload_rshift);
3179
3180 return ClassDescriptorSP(new ClassDescriptorV2Tagged(
3181 actual_class_descriptor_sp, data_payload, data_payload_signed));
3182}
3183
3184AppleObjCRuntimeV2::NonPointerISACache::NonPointerISACache(
3185 AppleObjCRuntimeV2 &runtime, const ModuleSP &objc_module_sp,
3186 uint64_t objc_debug_isa_class_mask, uint64_t objc_debug_isa_magic_mask,
3187 uint64_t objc_debug_isa_magic_value,
3188 uint64_t objc_debug_indexed_isa_magic_mask,
3189 uint64_t objc_debug_indexed_isa_magic_value,
3190 uint64_t objc_debug_indexed_isa_index_mask,
3191 uint64_t objc_debug_indexed_isa_index_shift,
3192 lldb::addr_t objc_indexed_classes)
3193 : m_runtime(runtime), m_cache(), m_objc_module_wp(objc_module_sp),
3194 m_objc_debug_isa_class_mask(objc_debug_isa_class_mask),
3195 m_objc_debug_isa_magic_mask(objc_debug_isa_magic_mask),
3196 m_objc_debug_isa_magic_value(objc_debug_isa_magic_value),
3197 m_objc_debug_indexed_isa_magic_mask(objc_debug_indexed_isa_magic_mask),
3198 m_objc_debug_indexed_isa_magic_value(objc_debug_indexed_isa_magic_value),
3199 m_objc_debug_indexed_isa_index_mask(objc_debug_indexed_isa_index_mask),
3200 m_objc_debug_indexed_isa_index_shift(objc_debug_indexed_isa_index_shift),
3201 m_objc_indexed_classes(objc_indexed_classes), m_indexed_isa_cache() {}
3202
3203ObjCLanguageRuntime::ClassDescriptorSP
3204AppleObjCRuntimeV2::NonPointerISACache::GetClassDescriptor(ObjCISA isa) {
3205 ObjCISA real_isa = 0;
3206 if (!EvaluateNonPointerISA(isa, ret_isa&: real_isa))
3207 return ObjCLanguageRuntime::ClassDescriptorSP();
3208 auto cache_iter = m_cache.find(x: real_isa);
3209 if (cache_iter != m_cache.end())
3210 return cache_iter->second;
3211 auto descriptor_sp =
3212 m_runtime.ObjCLanguageRuntime::GetClassDescriptorFromISA(isa: real_isa);
3213 if (descriptor_sp) // cache only positive matches since the table might grow
3214 m_cache[real_isa] = descriptor_sp;
3215 return descriptor_sp;
3216}
3217
3218bool AppleObjCRuntimeV2::NonPointerISACache::EvaluateNonPointerISA(
3219 ObjCISA isa, ObjCISA &ret_isa) {
3220 Log *log = GetLog(mask: LLDBLog::Types);
3221
3222 LLDB_LOGF(log, "AOCRT::NPI Evaluate(isa = 0x%" PRIx64 ")", (uint64_t)isa);
3223
3224 if ((isa & ~m_objc_debug_isa_class_mask) == 0)
3225 return false;
3226
3227 // If all of the indexed ISA variables are set, then its possible that this
3228 // ISA is indexed, and we should first try to get its value using the index.
3229 // Note, we check these variables first as the ObjC runtime will set at least
3230 // one of their values to 0 if they aren't needed.
3231 if (m_objc_debug_indexed_isa_magic_mask &&
3232 m_objc_debug_indexed_isa_magic_value &&
3233 m_objc_debug_indexed_isa_index_mask &&
3234 m_objc_debug_indexed_isa_index_shift && m_objc_indexed_classes) {
3235 if ((isa & ~m_objc_debug_indexed_isa_index_mask) == 0)
3236 return false;
3237
3238 if ((isa & m_objc_debug_indexed_isa_magic_mask) ==
3239 m_objc_debug_indexed_isa_magic_value) {
3240 // Magic bits are correct, so try extract the index.
3241 uintptr_t index = (isa & m_objc_debug_indexed_isa_index_mask) >>
3242 m_objc_debug_indexed_isa_index_shift;
3243 // If the index is out of bounds of the length of the array then check if
3244 // the array has been updated. If that is the case then we should try
3245 // read the count again, and update the cache if the count has been
3246 // updated.
3247 if (index > m_indexed_isa_cache.size()) {
3248 LLDB_LOGF(log,
3249 "AOCRT::NPI (index = %" PRIu64
3250 ") exceeds cache (size = %" PRIu64 ")",
3251 (uint64_t)index, (uint64_t)m_indexed_isa_cache.size());
3252
3253 Process *process(m_runtime.GetProcess());
3254
3255 ModuleSP objc_module_sp(m_objc_module_wp.lock());
3256 if (!objc_module_sp)
3257 return false;
3258
3259 Status error;
3260 auto objc_indexed_classes_count = ExtractRuntimeGlobalSymbol(
3261 process, name: ConstString("objc_indexed_classes_count"), module_sp: objc_module_sp,
3262 error);
3263 if (error.Fail())
3264 return false;
3265
3266 LLDB_LOGF(log, "AOCRT::NPI (new class count = %" PRIu64 ")",
3267 (uint64_t)objc_indexed_classes_count);
3268
3269 if (objc_indexed_classes_count > m_indexed_isa_cache.size()) {
3270 // Read the class entries we don't have. We should just read all of
3271 // them instead of just the one we need as then we can cache those we
3272 // may need later.
3273 auto num_new_classes =
3274 objc_indexed_classes_count - m_indexed_isa_cache.size();
3275 const uint32_t addr_size = process->GetAddressByteSize();
3276 DataBufferHeap buffer(num_new_classes * addr_size, 0);
3277
3278 lldb::addr_t last_read_class =
3279 m_objc_indexed_classes + (m_indexed_isa_cache.size() * addr_size);
3280 size_t bytes_read = process->ReadMemory(
3281 vm_addr: last_read_class, buf: buffer.GetBytes(), size: buffer.GetByteSize(), error);
3282 if (error.Fail() || bytes_read != buffer.GetByteSize())
3283 return false;
3284
3285 LLDB_LOGF(log, "AOCRT::NPI (read new classes count = %" PRIu64 ")",
3286 (uint64_t)num_new_classes);
3287
3288 // Append the new entries to the existing cache.
3289 DataExtractor data(buffer.GetBytes(), buffer.GetByteSize(),
3290 process->GetByteOrder(),
3291 process->GetAddressByteSize());
3292
3293 lldb::offset_t offset = 0;
3294 for (unsigned i = 0; i != num_new_classes; ++i)
3295 m_indexed_isa_cache.push_back(x: data.GetAddress(offset_ptr: &offset));
3296 }
3297 }
3298
3299 // If the index is still out of range then this isn't a pointer.
3300 if (index >= m_indexed_isa_cache.size())
3301 return false;
3302
3303 LLDB_LOGF(log, "AOCRT::NPI Evaluate(ret_isa = 0x%" PRIx64 ")",
3304 (uint64_t)m_indexed_isa_cache[index]);
3305
3306 ret_isa = m_indexed_isa_cache[index];
3307 return (ret_isa != 0); // this is a pointer so 0 is not a valid value
3308 }
3309
3310 return false;
3311 }
3312
3313 // Definitely not an indexed ISA, so try to use a mask to extract the pointer
3314 // from the ISA.
3315 if ((isa & m_objc_debug_isa_magic_mask) == m_objc_debug_isa_magic_value) {
3316 ret_isa = isa & m_objc_debug_isa_class_mask;
3317 return (ret_isa != 0); // this is a pointer so 0 is not a valid value
3318 }
3319 return false;
3320}
3321
3322ObjCLanguageRuntime::EncodingToTypeSP AppleObjCRuntimeV2::GetEncodingToType() {
3323 if (!m_encoding_to_type_sp)
3324 m_encoding_to_type_sp =
3325 std::make_shared<AppleObjCTypeEncodingParser>(args&: *this);
3326 return m_encoding_to_type_sp;
3327}
3328
3329lldb_private::AppleObjCRuntime::ObjCISA
3330AppleObjCRuntimeV2::GetPointerISA(ObjCISA isa) {
3331 ObjCISA ret = isa;
3332
3333 if (auto *non_pointer_isa_cache = GetNonPointerIsaCache())
3334 non_pointer_isa_cache->EvaluateNonPointerISA(isa, ret_isa&: ret);
3335
3336 return ret;
3337}
3338
3339bool AppleObjCRuntimeV2::GetCFBooleanValuesIfNeeded() {
3340 if (m_CFBoolean_values)
3341 return true;
3342
3343 static ConstString g_dunder_kCFBooleanFalse("__kCFBooleanFalse");
3344 static ConstString g_dunder_kCFBooleanTrue("__kCFBooleanTrue");
3345 static ConstString g_kCFBooleanFalse("kCFBooleanFalse");
3346 static ConstString g_kCFBooleanTrue("kCFBooleanTrue");
3347
3348 std::function<lldb::addr_t(ConstString, ConstString)> get_symbol =
3349 [this](ConstString sym, ConstString real_sym) -> lldb::addr_t {
3350 SymbolContextList sc_list;
3351 GetProcess()->GetTarget().GetImages().FindSymbolsWithNameAndType(
3352 name: sym, symbol_type: lldb::eSymbolTypeData, sc_list);
3353 if (sc_list.GetSize() == 1) {
3354 SymbolContext sc;
3355 sc_list.GetContextAtIndex(idx: 0, sc);
3356 if (sc.symbol)
3357 return sc.symbol->GetLoadAddress(target: &GetProcess()->GetTarget());
3358 }
3359 GetProcess()->GetTarget().GetImages().FindSymbolsWithNameAndType(
3360 name: real_sym, symbol_type: lldb::eSymbolTypeData, sc_list);
3361 if (sc_list.GetSize() != 1)
3362 return LLDB_INVALID_ADDRESS;
3363
3364 SymbolContext sc;
3365 sc_list.GetContextAtIndex(idx: 0, sc);
3366 if (!sc.symbol)
3367 return LLDB_INVALID_ADDRESS;
3368
3369 lldb::addr_t addr = sc.symbol->GetLoadAddress(target: &GetProcess()->GetTarget());
3370 Status error;
3371 addr = GetProcess()->ReadPointerFromMemory(vm_addr: addr, error);
3372 if (error.Fail())
3373 return LLDB_INVALID_ADDRESS;
3374 return addr;
3375 };
3376
3377 lldb::addr_t false_addr = get_symbol(g_dunder_kCFBooleanFalse, g_kCFBooleanFalse);
3378 lldb::addr_t true_addr = get_symbol(g_dunder_kCFBooleanTrue, g_kCFBooleanTrue);
3379
3380 return (m_CFBoolean_values = {false_addr, true_addr}).operator bool();
3381}
3382
3383void AppleObjCRuntimeV2::GetValuesForGlobalCFBooleans(lldb::addr_t &cf_true,
3384 lldb::addr_t &cf_false) {
3385 if (GetCFBooleanValuesIfNeeded()) {
3386 cf_true = m_CFBoolean_values->second;
3387 cf_false = m_CFBoolean_values->first;
3388 } else
3389 this->AppleObjCRuntime::GetValuesForGlobalCFBooleans(cf_true, cf_false);
3390}
3391
3392void AppleObjCRuntimeV2::ModulesDidLoad(const ModuleList &module_list) {
3393 AppleObjCRuntime::ModulesDidLoad(module_list);
3394 if (HasReadObjCLibrary() && m_shared_cache_image_headers_up)
3395 m_shared_cache_image_headers_up->SetNeedsUpdate();
3396}
3397
3398bool AppleObjCRuntimeV2::IsSharedCacheImageLoaded(uint16_t image_index) {
3399 if (!m_shared_cache_image_headers_up) {
3400 m_shared_cache_image_headers_up =
3401 SharedCacheImageHeaders::CreateSharedCacheImageHeaders(runtime&: *this);
3402 }
3403 if (m_shared_cache_image_headers_up)
3404 return m_shared_cache_image_headers_up->IsImageLoaded(image_index);
3405
3406 return false;
3407}
3408
3409std::optional<uint64_t> AppleObjCRuntimeV2::GetSharedCacheImageHeaderVersion() {
3410 if (!m_shared_cache_image_headers_up) {
3411 m_shared_cache_image_headers_up =
3412 SharedCacheImageHeaders::CreateSharedCacheImageHeaders(runtime&: *this);
3413 }
3414 if (m_shared_cache_image_headers_up)
3415 return m_shared_cache_image_headers_up->GetVersion();
3416
3417 return std::nullopt;
3418}
3419
3420StructuredData::ObjectSP
3421AppleObjCRuntimeV2::GetLanguageSpecificData(SymbolContext sc) {
3422 auto dict_up = std::make_unique<StructuredData::Dictionary>();
3423 dict_up->AddItem(key: "Objective-C runtime version",
3424 value_sp: std::make_unique<StructuredData::UnsignedInteger>(args: 2));
3425 return dict_up;
3426}
3427
3428#pragma mark Frame recognizers
3429
3430class ObjCExceptionRecognizedStackFrame : public RecognizedStackFrame {
3431public:
3432 ObjCExceptionRecognizedStackFrame(StackFrameSP frame_sp) {
3433 ThreadSP thread_sp = frame_sp->GetThread();
3434 ProcessSP process_sp = thread_sp->GetProcess();
3435
3436 const lldb::ABISP &abi = process_sp->GetABI();
3437 if (!abi)
3438 return;
3439
3440 TypeSystemClangSP scratch_ts_sp =
3441 ScratchTypeSystemClang::GetForTarget(target&: process_sp->GetTarget());
3442 if (!scratch_ts_sp)
3443 return;
3444 CompilerType voidstar =
3445 scratch_ts_sp->GetBasicType(type: lldb::eBasicTypeVoid).GetPointerType();
3446
3447 ValueList args;
3448 Value input_value;
3449 input_value.SetCompilerType(voidstar);
3450 args.PushValue(value: input_value);
3451
3452 if (!abi->GetArgumentValues(thread&: *thread_sp, values&: args))
3453 return;
3454
3455 addr_t exception_addr = args.GetValueAtIndex(idx: 0)->GetScalar().ULongLong();
3456
3457 Value value(exception_addr);
3458 value.SetCompilerType(voidstar);
3459 exception = ValueObjectConstResult::Create(exe_scope: frame_sp.get(), value,
3460 name: ConstString("exception"));
3461 exception = ValueObjectRecognizerSynthesizedValue::Create(
3462 parent&: *exception, type: eValueTypeVariableArgument);
3463 exception = exception->GetDynamicValue(valueType: eDynamicDontRunTarget);
3464
3465 m_arguments = ValueObjectListSP(new ValueObjectList());
3466 m_arguments->Append(val_obj_sp: exception);
3467
3468 m_stop_desc = "hit Objective-C exception";
3469 }
3470
3471 ValueObjectSP exception;
3472
3473 lldb::ValueObjectSP GetExceptionObject() override { return exception; }
3474};
3475
3476class ObjCExceptionThrowFrameRecognizer : public StackFrameRecognizer {
3477 lldb::RecognizedStackFrameSP
3478 RecognizeFrame(lldb::StackFrameSP frame) override {
3479 return lldb::RecognizedStackFrameSP(
3480 new ObjCExceptionRecognizedStackFrame(frame));
3481 };
3482 std::string GetName() override {
3483 return "ObjC Exception Throw StackFrame Recognizer";
3484 }
3485};
3486
3487static void RegisterObjCExceptionRecognizer(Process *process) {
3488 FileSpec module;
3489 ConstString function;
3490 std::tie(args&: module, args&: function) = AppleObjCRuntime::GetExceptionThrowLocation();
3491 std::vector<ConstString> symbols = {function};
3492
3493 process->GetTarget().GetFrameRecognizerManager().AddRecognizer(
3494 recognizer: StackFrameRecognizerSP(new ObjCExceptionThrowFrameRecognizer()),
3495 module: module.GetFilename(), symbols, symbol_mangling: Mangled::NamePreference::ePreferDemangled,
3496 /*first_instruction_only*/ true);
3497}
3498

source code of lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp