1 //===-- AppleObjCClassDescriptorV2.cpp -----------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #include "AppleObjCClassDescriptorV2.h"
12 
13 #include "lldb/Core/Log.h"
14 #include "lldb/Expression/FunctionCaller.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 bool ClassDescriptorV2::Read_objc_class(
20     Process *process, std::unique_ptr<objc_class_t> &objc_class) const {
21   objc_class.reset(new objc_class_t);
22 
23   bool ret = objc_class->Read(process, m_objc_class_ptr);
24 
25   if (!ret)
26     objc_class.reset();
27 
28   return ret;
29 }
30 
31 static lldb::addr_t GetClassDataMask(Process *process) {
32   switch (process->GetAddressByteSize()) {
33   case 4:
34     return 0xfffffffcUL;
35   case 8:
36     return 0x00007ffffffffff8UL;
37   default:
38     break;
39   }
40 
41   return LLDB_INVALID_ADDRESS;
42 }
43 
44 bool ClassDescriptorV2::objc_class_t::Read(Process *process,
45                                            lldb::addr_t addr) {
46   size_t ptr_size = process->GetAddressByteSize();
47 
48   size_t objc_class_size = ptr_size    // uintptr_t isa;
49                            + ptr_size  // Class superclass;
50                            + ptr_size  // void *cache;
51                            + ptr_size  // IMP *vtable;
52                            + ptr_size; // uintptr_t data_NEVER_USE;
53 
54   DataBufferHeap objc_class_buf(objc_class_size, '\0');
55   Error error;
56 
57   process->ReadMemory(addr, objc_class_buf.GetBytes(), objc_class_size, error);
58   if (error.Fail()) {
59     return false;
60   }
61 
62   DataExtractor extractor(objc_class_buf.GetBytes(), objc_class_size,
63                           process->GetByteOrder(),
64                           process->GetAddressByteSize());
65 
66   lldb::offset_t cursor = 0;
67 
68   m_isa = extractor.GetAddress_unchecked(&cursor);        // uintptr_t isa;
69   m_superclass = extractor.GetAddress_unchecked(&cursor); // Class superclass;
70   m_cache_ptr = extractor.GetAddress_unchecked(&cursor);  // void *cache;
71   m_vtable_ptr = extractor.GetAddress_unchecked(&cursor); // IMP *vtable;
72   lldb::addr_t data_NEVER_USE =
73       extractor.GetAddress_unchecked(&cursor); // uintptr_t data_NEVER_USE;
74 
75   m_flags = (uint8_t)(data_NEVER_USE & (lldb::addr_t)3);
76   m_data_ptr = data_NEVER_USE & GetClassDataMask(process);
77 
78   return true;
79 }
80 
81 bool ClassDescriptorV2::class_rw_t::Read(Process *process, lldb::addr_t addr) {
82   size_t ptr_size = process->GetAddressByteSize();
83 
84   size_t size = sizeof(uint32_t)   // uint32_t flags;
85                 + sizeof(uint32_t) // uint32_t version;
86                 + ptr_size         // const class_ro_t *ro;
87                 + ptr_size         // union { method_list_t **method_lists;
88                                    // method_list_t *method_list; };
89                 + ptr_size         // struct chained_property_list *properties;
90                 + ptr_size         // const protocol_list_t **protocols;
91                 + ptr_size         // Class firstSubclass;
92                 + ptr_size;        // Class nextSiblingClass;
93 
94   DataBufferHeap buffer(size, '\0');
95   Error error;
96 
97   process->ReadMemory(addr, buffer.GetBytes(), size, error);
98   if (error.Fail()) {
99     return false;
100   }
101 
102   DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
103                           process->GetAddressByteSize());
104 
105   lldb::offset_t cursor = 0;
106 
107   m_flags = extractor.GetU32_unchecked(&cursor);
108   m_version = extractor.GetU32_unchecked(&cursor);
109   m_ro_ptr = extractor.GetAddress_unchecked(&cursor);
110   m_method_list_ptr = extractor.GetAddress_unchecked(&cursor);
111   m_properties_ptr = extractor.GetAddress_unchecked(&cursor);
112   m_firstSubclass = extractor.GetAddress_unchecked(&cursor);
113   m_nextSiblingClass = extractor.GetAddress_unchecked(&cursor);
114 
115   return true;
116 }
117 
118 bool ClassDescriptorV2::class_ro_t::Read(Process *process, lldb::addr_t addr) {
119   size_t ptr_size = process->GetAddressByteSize();
120 
121   size_t size = sizeof(uint32_t)   // uint32_t flags;
122                 + sizeof(uint32_t) // uint32_t instanceStart;
123                 + sizeof(uint32_t) // uint32_t instanceSize;
124                 + (ptr_size == 8 ? sizeof(uint32_t)
125                                  : 0) // uint32_t reserved; // __LP64__ only
126                 + ptr_size            // const uint8_t *ivarLayout;
127                 + ptr_size            // const char *name;
128                 + ptr_size            // const method_list_t *baseMethods;
129                 + ptr_size            // const protocol_list_t *baseProtocols;
130                 + ptr_size            // const ivar_list_t *ivars;
131                 + ptr_size            // const uint8_t *weakIvarLayout;
132                 + ptr_size;           // const property_list_t *baseProperties;
133 
134   DataBufferHeap buffer(size, '\0');
135   Error error;
136 
137   process->ReadMemory(addr, buffer.GetBytes(), size, error);
138   if (error.Fail()) {
139     return false;
140   }
141 
142   DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
143                           process->GetAddressByteSize());
144 
145   lldb::offset_t cursor = 0;
146 
147   m_flags = extractor.GetU32_unchecked(&cursor);
148   m_instanceStart = extractor.GetU32_unchecked(&cursor);
149   m_instanceSize = extractor.GetU32_unchecked(&cursor);
150   if (ptr_size == 8)
151     m_reserved = extractor.GetU32_unchecked(&cursor);
152   else
153     m_reserved = 0;
154   m_ivarLayout_ptr = extractor.GetAddress_unchecked(&cursor);
155   m_name_ptr = extractor.GetAddress_unchecked(&cursor);
156   m_baseMethods_ptr = extractor.GetAddress_unchecked(&cursor);
157   m_baseProtocols_ptr = extractor.GetAddress_unchecked(&cursor);
158   m_ivars_ptr = extractor.GetAddress_unchecked(&cursor);
159   m_weakIvarLayout_ptr = extractor.GetAddress_unchecked(&cursor);
160   m_baseProperties_ptr = extractor.GetAddress_unchecked(&cursor);
161 
162   DataBufferHeap name_buf(1024, '\0');
163 
164   process->ReadCStringFromMemory(m_name_ptr, (char *)name_buf.GetBytes(),
165                                  name_buf.GetByteSize(), error);
166 
167   if (error.Fail()) {
168     return false;
169   }
170 
171   m_name.assign((char *)name_buf.GetBytes());
172 
173   return true;
174 }
175 
176 bool ClassDescriptorV2::Read_class_row(
177     Process *process, const objc_class_t &objc_class,
178     std::unique_ptr<class_ro_t> &class_ro,
179     std::unique_ptr<class_rw_t> &class_rw) const {
180   class_ro.reset();
181   class_rw.reset();
182 
183   Error error;
184   uint32_t class_row_t_flags = process->ReadUnsignedIntegerFromMemory(
185       objc_class.m_data_ptr, sizeof(uint32_t), 0, error);
186   if (!error.Success())
187     return false;
188 
189   if (class_row_t_flags & RW_REALIZED) {
190     class_rw.reset(new class_rw_t);
191 
192     if (!class_rw->Read(process, objc_class.m_data_ptr)) {
193       class_rw.reset();
194       return false;
195     }
196 
197     class_ro.reset(new class_ro_t);
198 
199     if (!class_ro->Read(process, class_rw->m_ro_ptr)) {
200       class_rw.reset();
201       class_ro.reset();
202       return false;
203     }
204   } else {
205     class_ro.reset(new class_ro_t);
206 
207     if (!class_ro->Read(process, objc_class.m_data_ptr)) {
208       class_ro.reset();
209       return false;
210     }
211   }
212 
213   return true;
214 }
215 
216 bool ClassDescriptorV2::method_list_t::Read(Process *process,
217                                             lldb::addr_t addr) {
218   size_t size = sizeof(uint32_t)    // uint32_t entsize_NEVER_USE;
219                 + sizeof(uint32_t); // uint32_t count;
220 
221   DataBufferHeap buffer(size, '\0');
222   Error error;
223 
224   process->ReadMemory(addr, buffer.GetBytes(), size, error);
225   if (error.Fail()) {
226     return false;
227   }
228 
229   DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
230                           process->GetAddressByteSize());
231 
232   lldb::offset_t cursor = 0;
233 
234   m_entsize = extractor.GetU32_unchecked(&cursor) & ~(uint32_t)3;
235   m_count = extractor.GetU32_unchecked(&cursor);
236   m_first_ptr = addr + cursor;
237 
238   return true;
239 }
240 
241 bool ClassDescriptorV2::method_t::Read(Process *process, lldb::addr_t addr) {
242   size_t size = GetSize(process);
243 
244   DataBufferHeap buffer(size, '\0');
245   Error error;
246 
247   process->ReadMemory(addr, buffer.GetBytes(), size, error);
248   if (error.Fail()) {
249     return false;
250   }
251 
252   DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
253                           process->GetAddressByteSize());
254 
255   lldb::offset_t cursor = 0;
256 
257   m_name_ptr = extractor.GetAddress_unchecked(&cursor);
258   m_types_ptr = extractor.GetAddress_unchecked(&cursor);
259   m_imp_ptr = extractor.GetAddress_unchecked(&cursor);
260 
261   process->ReadCStringFromMemory(m_name_ptr, m_name, error);
262   if (error.Fail()) {
263     return false;
264   }
265 
266   process->ReadCStringFromMemory(m_types_ptr, m_types, error);
267   if (error.Fail()) {
268     return false;
269   }
270 
271   return true;
272 }
273 
274 bool ClassDescriptorV2::ivar_list_t::Read(Process *process, lldb::addr_t addr) {
275   size_t size = sizeof(uint32_t)    // uint32_t entsize;
276                 + sizeof(uint32_t); // uint32_t count;
277 
278   DataBufferHeap buffer(size, '\0');
279   Error error;
280 
281   process->ReadMemory(addr, buffer.GetBytes(), size, error);
282   if (error.Fail()) {
283     return false;
284   }
285 
286   DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
287                           process->GetAddressByteSize());
288 
289   lldb::offset_t cursor = 0;
290 
291   m_entsize = extractor.GetU32_unchecked(&cursor);
292   m_count = extractor.GetU32_unchecked(&cursor);
293   m_first_ptr = addr + cursor;
294 
295   return true;
296 }
297 
298 bool ClassDescriptorV2::ivar_t::Read(Process *process, lldb::addr_t addr) {
299   size_t size = GetSize(process);
300 
301   DataBufferHeap buffer(size, '\0');
302   Error error;
303 
304   process->ReadMemory(addr, buffer.GetBytes(), size, error);
305   if (error.Fail()) {
306     return false;
307   }
308 
309   DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
310                           process->GetAddressByteSize());
311 
312   lldb::offset_t cursor = 0;
313 
314   m_offset_ptr = extractor.GetAddress_unchecked(&cursor);
315   m_name_ptr = extractor.GetAddress_unchecked(&cursor);
316   m_type_ptr = extractor.GetAddress_unchecked(&cursor);
317   m_alignment = extractor.GetU32_unchecked(&cursor);
318   m_size = extractor.GetU32_unchecked(&cursor);
319 
320   process->ReadCStringFromMemory(m_name_ptr, m_name, error);
321   if (error.Fail()) {
322     return false;
323   }
324 
325   process->ReadCStringFromMemory(m_type_ptr, m_type, error);
326   if (error.Fail()) {
327     return false;
328   }
329 
330   return true;
331 }
332 
333 bool ClassDescriptorV2::Describe(
334     std::function<void(ObjCLanguageRuntime::ObjCISA)> const &superclass_func,
335     std::function<bool(const char *, const char *)> const &instance_method_func,
336     std::function<bool(const char *, const char *)> const &class_method_func,
337     std::function<bool(const char *, const char *, lldb::addr_t,
338                        uint64_t)> const &ivar_func) const {
339   lldb_private::Process *process = m_runtime.GetProcess();
340 
341   std::unique_ptr<objc_class_t> objc_class;
342   std::unique_ptr<class_ro_t> class_ro;
343   std::unique_ptr<class_rw_t> class_rw;
344 
345   if (!Read_objc_class(process, objc_class))
346     return 0;
347   if (!Read_class_row(process, *objc_class, class_ro, class_rw))
348     return 0;
349 
350   static ConstString NSObject_name("NSObject");
351 
352   if (m_name != NSObject_name && superclass_func)
353     superclass_func(objc_class->m_superclass);
354 
355   if (instance_method_func) {
356     std::unique_ptr<method_list_t> base_method_list;
357 
358     base_method_list.reset(new method_list_t);
359     if (!base_method_list->Read(process, class_ro->m_baseMethods_ptr))
360       return false;
361 
362     if (base_method_list->m_entsize != method_t::GetSize(process))
363       return false;
364 
365     std::unique_ptr<method_t> method;
366     method.reset(new method_t);
367 
368     for (uint32_t i = 0, e = base_method_list->m_count; i < e; ++i) {
369       method->Read(process, base_method_list->m_first_ptr +
370                                 (i * base_method_list->m_entsize));
371 
372       if (instance_method_func(method->m_name.c_str(), method->m_types.c_str()))
373         break;
374     }
375   }
376 
377   if (class_method_func) {
378     AppleObjCRuntime::ClassDescriptorSP metaclass(GetMetaclass());
379 
380     // We don't care about the metaclass's superclass, or its class methods.
381     // Its instance methods are
382     // our class methods.
383 
384     if (metaclass) {
385       metaclass->Describe(
386           std::function<void(ObjCLanguageRuntime::ObjCISA)>(nullptr),
387           class_method_func,
388           std::function<bool(const char *, const char *)>(nullptr),
389           std::function<bool(const char *, const char *, lldb::addr_t,
390                              uint64_t)>(nullptr));
391     }
392   }
393 
394   if (ivar_func) {
395     if (class_ro->m_ivars_ptr != 0) {
396       ivar_list_t ivar_list;
397       if (!ivar_list.Read(process, class_ro->m_ivars_ptr))
398         return false;
399 
400       if (ivar_list.m_entsize != ivar_t::GetSize(process))
401         return false;
402 
403       ivar_t ivar;
404 
405       for (uint32_t i = 0, e = ivar_list.m_count; i < e; ++i) {
406         ivar.Read(process, ivar_list.m_first_ptr + (i * ivar_list.m_entsize));
407 
408         if (ivar_func(ivar.m_name.c_str(), ivar.m_type.c_str(),
409                       ivar.m_offset_ptr, ivar.m_size))
410           break;
411       }
412     }
413   }
414 
415   return true;
416 }
417 
418 ConstString ClassDescriptorV2::GetClassName() {
419   if (!m_name) {
420     lldb_private::Process *process = m_runtime.GetProcess();
421 
422     if (process) {
423       std::unique_ptr<objc_class_t> objc_class;
424       std::unique_ptr<class_ro_t> class_ro;
425       std::unique_ptr<class_rw_t> class_rw;
426 
427       if (!Read_objc_class(process, objc_class))
428         return m_name;
429       if (!Read_class_row(process, *objc_class, class_ro, class_rw))
430         return m_name;
431 
432       m_name = ConstString(class_ro->m_name.c_str());
433     }
434   }
435   return m_name;
436 }
437 
438 ObjCLanguageRuntime::ClassDescriptorSP ClassDescriptorV2::GetSuperclass() {
439   lldb_private::Process *process = m_runtime.GetProcess();
440 
441   if (!process)
442     return ObjCLanguageRuntime::ClassDescriptorSP();
443 
444   std::unique_ptr<objc_class_t> objc_class;
445 
446   if (!Read_objc_class(process, objc_class))
447     return ObjCLanguageRuntime::ClassDescriptorSP();
448 
449   return m_runtime.ObjCLanguageRuntime::GetClassDescriptorFromISA(
450       objc_class->m_superclass);
451 }
452 
453 ObjCLanguageRuntime::ClassDescriptorSP ClassDescriptorV2::GetMetaclass() const {
454   lldb_private::Process *process = m_runtime.GetProcess();
455 
456   if (!process)
457     return ObjCLanguageRuntime::ClassDescriptorSP();
458 
459   std::unique_ptr<objc_class_t> objc_class;
460 
461   if (!Read_objc_class(process, objc_class))
462     return ObjCLanguageRuntime::ClassDescriptorSP();
463 
464   lldb::addr_t candidate_isa = m_runtime.GetPointerISA(objc_class->m_isa);
465 
466   return ObjCLanguageRuntime::ClassDescriptorSP(
467       new ClassDescriptorV2(m_runtime, candidate_isa, nullptr));
468 }
469 
470 uint64_t ClassDescriptorV2::GetInstanceSize() {
471   lldb_private::Process *process = m_runtime.GetProcess();
472 
473   if (process) {
474     std::unique_ptr<objc_class_t> objc_class;
475     std::unique_ptr<class_ro_t> class_ro;
476     std::unique_ptr<class_rw_t> class_rw;
477 
478     if (!Read_objc_class(process, objc_class))
479       return 0;
480     if (!Read_class_row(process, *objc_class, class_ro, class_rw))
481       return 0;
482 
483     return class_ro->m_instanceSize;
484   }
485 
486   return 0;
487 }
488 
489 ClassDescriptorV2::iVarsStorage::iVarsStorage()
490     : m_filled(false), m_ivars(), m_mutex() {}
491 
492 size_t ClassDescriptorV2::iVarsStorage::size() { return m_ivars.size(); }
493 
494 ClassDescriptorV2::iVarDescriptor &ClassDescriptorV2::iVarsStorage::
495 operator[](size_t idx) {
496   return m_ivars[idx];
497 }
498 
499 void ClassDescriptorV2::iVarsStorage::fill(AppleObjCRuntimeV2 &runtime,
500                                            ClassDescriptorV2 &descriptor) {
501   if (m_filled)
502     return;
503   std::lock_guard<std::recursive_mutex> guard(m_mutex);
504   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES | LIBLLDB_LOG_VERBOSE));
505   if (log)
506     log->Printf("[ClassDescriptorV2::iVarsStorage::fill] class_name = %s",
507                 descriptor.GetClassName().AsCString("<unknown"));
508   m_filled = true;
509   ObjCLanguageRuntime::EncodingToTypeSP encoding_to_type_sp(
510       runtime.GetEncodingToType());
511   Process *process(runtime.GetProcess());
512   if (!encoding_to_type_sp)
513     return;
514   descriptor.Describe(nullptr, nullptr, nullptr, [this, process,
515                                                   encoding_to_type_sp,
516                                                   log](const char *name,
517                                                        const char *type,
518                                                        lldb::addr_t offset_ptr,
519                                                        uint64_t size) -> bool {
520     const bool for_expression = false;
521     const bool stop_loop = false;
522     if (log)
523       log->Printf("[ClassDescriptorV2::iVarsStorage::fill] name = %s, encoding "
524                   "= %s, offset_ptr = %" PRIx64 ", size = %" PRIu64,
525                   name, type, offset_ptr, size);
526     CompilerType ivar_type =
527         encoding_to_type_sp->RealizeType(type, for_expression);
528     if (ivar_type) {
529       if (log)
530         log->Printf("[ClassDescriptorV2::iVarsStorage::fill] name = %s, "
531                     "encoding = %s, offset_ptr = %" PRIx64 ", size = %" PRIu64
532                     " , type_size = %" PRIu64,
533                     name, type, offset_ptr, size,
534                     ivar_type.GetByteSize(nullptr));
535       Scalar offset_scalar;
536       Error error;
537       const int offset_ptr_size = 4;
538       const bool is_signed = false;
539       size_t read = process->ReadScalarIntegerFromMemory(
540           offset_ptr, offset_ptr_size, is_signed, offset_scalar, error);
541       if (error.Success() && 4 == read) {
542         if (log)
543           log->Printf(
544               "[ClassDescriptorV2::iVarsStorage::fill] offset_ptr = %" PRIx64
545               " --> %" PRIu32,
546               offset_ptr, offset_scalar.SInt());
547         m_ivars.push_back(
548             {ConstString(name), ivar_type, size, offset_scalar.SInt()});
549       } else if (log)
550         log->Printf(
551             "[ClassDescriptorV2::iVarsStorage::fill] offset_ptr = %" PRIx64
552             " --> read fail, read = %zu",
553             offset_ptr, read);
554     }
555     return stop_loop;
556   });
557 }
558 
559 void ClassDescriptorV2::GetIVarInformation() {
560   m_ivars_storage.fill(m_runtime, *this);
561 }
562