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