• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- ObjCLanguageRuntime.h -----------------------------------*- C++ -*-===//
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 #ifndef LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_OBJC_OBJCLANGUAGERUNTIME_H
10 #define LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_OBJC_OBJCLANGUAGERUNTIME_H
11 
12 #include <functional>
13 #include <map>
14 #include <memory>
15 #include <unordered_set>
16 
17 #include "llvm/Support/Casting.h"
18 
19 #include "lldb/Breakpoint/BreakpointPrecondition.h"
20 #include "lldb/Core/PluginInterface.h"
21 #include "lldb/Core/ThreadSafeDenseMap.h"
22 #include "lldb/Symbol/CompilerType.h"
23 #include "lldb/Symbol/Type.h"
24 #include "lldb/Target/LanguageRuntime.h"
25 #include "lldb/lldb-private.h"
26 
27 class CommandObjectObjC_ClassTable_Dump;
28 
29 namespace lldb_private {
30 
31 class TypeSystemClang;
32 class UtilityFunction;
33 
34 class ObjCLanguageRuntime : public LanguageRuntime {
35 public:
36   enum class ObjCRuntimeVersions {
37     eObjC_VersionUnknown = 0,
38     eAppleObjC_V1 = 1,
39     eAppleObjC_V2 = 2
40   };
41 
42   typedef lldb::addr_t ObjCISA;
43 
44   class ClassDescriptor;
45   typedef std::shared_ptr<ClassDescriptor> ClassDescriptorSP;
46 
47   // the information that we want to support retrieving from an ObjC class this
48   // needs to be pure virtual since there are at least 2 different
49   // implementations of the runtime, and more might come
50   class ClassDescriptor {
51   public:
ClassDescriptor()52     ClassDescriptor()
53         : m_is_kvo(eLazyBoolCalculate), m_is_cf(eLazyBoolCalculate),
54           m_type_wp() {}
55 
56     virtual ~ClassDescriptor() = default;
57 
58     virtual ConstString GetClassName() = 0;
59 
60     virtual ClassDescriptorSP GetSuperclass() = 0;
61 
62     virtual ClassDescriptorSP GetMetaclass() const = 0;
63 
64     // virtual if any implementation has some other version-specific rules but
65     // for the known v1/v2 this is all that needs to be done
IsKVO()66     virtual bool IsKVO() {
67       if (m_is_kvo == eLazyBoolCalculate) {
68         const char *class_name = GetClassName().AsCString();
69         if (class_name && *class_name)
70           m_is_kvo =
71               (LazyBool)(strstr(class_name, "NSKVONotifying_") == class_name);
72       }
73       return (m_is_kvo == eLazyBoolYes);
74     }
75 
76     // virtual if any implementation has some other version-specific rules but
77     // for the known v1/v2 this is all that needs to be done
IsCFType()78     virtual bool IsCFType() {
79       if (m_is_cf == eLazyBoolCalculate) {
80         const char *class_name = GetClassName().AsCString();
81         if (class_name && *class_name)
82           m_is_cf = (LazyBool)(strcmp(class_name, "__NSCFType") == 0 ||
83                                strcmp(class_name, "NSCFType") == 0);
84       }
85       return (m_is_cf == eLazyBoolYes);
86     }
87 
88     virtual bool IsValid() = 0;
89 
90     virtual bool GetTaggedPointerInfo(uint64_t *info_bits = nullptr,
91                                       uint64_t *value_bits = nullptr,
92                                       uint64_t *payload = nullptr) = 0;
93 
94     virtual uint64_t GetInstanceSize() = 0;
95 
96     // use to implement version-specific additional constraints on pointers
CheckPointer(lldb::addr_t value,uint32_t ptr_size)97     virtual bool CheckPointer(lldb::addr_t value, uint32_t ptr_size) const {
98       return true;
99     }
100 
101     virtual ObjCISA GetISA() = 0;
102 
103     // This should return true iff the interface could be completed
104     virtual bool
Describe(std::function<void (ObjCISA)> const & superclass_func,std::function<bool (const char *,const char *)> const & instance_method_func,std::function<bool (const char *,const char *)> const & class_method_func,std::function<bool (const char *,const char *,lldb::addr_t,uint64_t)> const & ivar_func)105     Describe(std::function<void(ObjCISA)> const &superclass_func,
106              std::function<bool(const char *, const char *)> const
107                  &instance_method_func,
108              std::function<bool(const char *, const char *)> const
109                  &class_method_func,
110              std::function<bool(const char *, const char *, lldb::addr_t,
111                                 uint64_t)> const &ivar_func) const {
112       return false;
113     }
114 
GetType()115     lldb::TypeSP GetType() { return m_type_wp.lock(); }
116 
SetType(const lldb::TypeSP & type_sp)117     void SetType(const lldb::TypeSP &type_sp) { m_type_wp = type_sp; }
118 
119     struct iVarDescriptor {
120       ConstString m_name;
121       CompilerType m_type;
122       uint64_t m_size;
123       int32_t m_offset;
124     };
125 
GetNumIVars()126     virtual size_t GetNumIVars() { return 0; }
127 
GetIVarAtIndex(size_t idx)128     virtual iVarDescriptor GetIVarAtIndex(size_t idx) {
129       return iVarDescriptor();
130     }
131 
132   protected:
133     bool IsPointerValid(lldb::addr_t value, uint32_t ptr_size,
134                         bool allow_NULLs = false, bool allow_tagged = false,
135                         bool check_version_specific = false) const;
136 
137   private:
138     LazyBool m_is_kvo;
139     LazyBool m_is_cf;
140     lldb::TypeWP m_type_wp;
141   };
142 
143   class EncodingToType {
144   public:
145     virtual ~EncodingToType();
146 
147     virtual CompilerType RealizeType(TypeSystemClang &ast_ctx, const char *name,
148                                      bool for_expression) = 0;
149     virtual CompilerType RealizeType(const char *name, bool for_expression);
150 
151   protected:
152     std::unique_ptr<TypeSystemClang> m_scratch_ast_ctx_up;
153   };
154 
155   class ObjCExceptionPrecondition : public BreakpointPrecondition {
156   public:
157     ObjCExceptionPrecondition();
158 
159     ~ObjCExceptionPrecondition() override = default;
160 
161     bool EvaluatePrecondition(StoppointCallbackContext &context) override;
162     void GetDescription(Stream &stream, lldb::DescriptionLevel level) override;
163     Status ConfigurePrecondition(Args &args) override;
164 
165   protected:
166     void AddClassName(const char *class_name);
167 
168   private:
169     std::unordered_set<std::string> m_class_names;
170   };
171 
172   static lldb::BreakpointPreconditionSP
173   GetBreakpointExceptionPrecondition(lldb::LanguageType language,
174                                      bool throw_bp);
175 
176   class TaggedPointerVendor {
177   public:
178     virtual ~TaggedPointerVendor() = default;
179 
180     virtual bool IsPossibleTaggedPointer(lldb::addr_t ptr) = 0;
181 
182     virtual ObjCLanguageRuntime::ClassDescriptorSP
183     GetClassDescriptor(lldb::addr_t ptr) = 0;
184 
185   protected:
186     TaggedPointerVendor() = default;
187 
188   private:
189     TaggedPointerVendor(const TaggedPointerVendor &) = delete;
190     const TaggedPointerVendor &operator=(const TaggedPointerVendor &) = delete;
191   };
192 
193   ~ObjCLanguageRuntime() override;
194 
195   static char ID;
196 
isA(const void * ClassID)197   bool isA(const void *ClassID) const override {
198     return ClassID == &ID || LanguageRuntime::isA(ClassID);
199   }
200 
classof(const LanguageRuntime * runtime)201   static bool classof(const LanguageRuntime *runtime) {
202     return runtime->isA(&ID);
203   }
204 
Get(Process & process)205   static ObjCLanguageRuntime *Get(Process &process) {
206     return llvm::cast_or_null<ObjCLanguageRuntime>(
207         process.GetLanguageRuntime(lldb::eLanguageTypeObjC));
208   }
209 
GetTaggedPointerVendor()210   virtual TaggedPointerVendor *GetTaggedPointerVendor() { return nullptr; }
211 
212   typedef std::shared_ptr<EncodingToType> EncodingToTypeSP;
213 
214   virtual EncodingToTypeSP GetEncodingToType();
215 
216   virtual ClassDescriptorSP GetClassDescriptor(ValueObject &in_value);
217 
218   ClassDescriptorSP GetNonKVOClassDescriptor(ValueObject &in_value);
219 
220   virtual ClassDescriptorSP
221   GetClassDescriptorFromClassName(ConstString class_name);
222 
223   virtual ClassDescriptorSP GetClassDescriptorFromISA(ObjCISA isa);
224 
225   ClassDescriptorSP GetNonKVOClassDescriptor(ObjCISA isa);
226 
GetLanguageType()227   lldb::LanguageType GetLanguageType() const override {
228     return lldb::eLanguageTypeObjC;
229   }
230 
231   virtual bool IsModuleObjCLibrary(const lldb::ModuleSP &module_sp) = 0;
232 
233   virtual bool ReadObjCLibrary(const lldb::ModuleSP &module_sp) = 0;
234 
235   virtual bool HasReadObjCLibrary() = 0;
236 
237   lldb::addr_t LookupInMethodCache(lldb::addr_t class_addr, lldb::addr_t sel);
238 
239   void AddToMethodCache(lldb::addr_t class_addr, lldb::addr_t sel,
240                         lldb::addr_t impl_addr);
241 
242   TypeAndOrName LookupInClassNameCache(lldb::addr_t class_addr);
243 
244   void AddToClassNameCache(lldb::addr_t class_addr, const char *name,
245                            lldb::TypeSP type_sp);
246 
247   void AddToClassNameCache(lldb::addr_t class_addr,
248                            const TypeAndOrName &class_or_type_name);
249 
250   lldb::TypeSP LookupInCompleteClassCache(ConstString &name);
251 
252   llvm::Optional<CompilerType> GetRuntimeType(CompilerType base_type) override;
253 
254   virtual llvm::Expected<std::unique_ptr<UtilityFunction>>
255   CreateObjectChecker(std::string name, ExecutionContext &exe_ctx) = 0;
256 
GetRuntimeVersion()257   virtual ObjCRuntimeVersions GetRuntimeVersion() const {
258     return ObjCRuntimeVersions::eObjC_VersionUnknown;
259   }
260 
IsValidISA(ObjCISA isa)261   bool IsValidISA(ObjCISA isa) {
262     UpdateISAToDescriptorMap();
263     return m_isa_to_descriptor.count(isa) > 0;
264   }
265 
266   virtual void UpdateISAToDescriptorMapIfNeeded() = 0;
267 
UpdateISAToDescriptorMap()268   void UpdateISAToDescriptorMap() {
269     if (m_process && m_process->GetStopID() != m_isa_to_descriptor_stop_id) {
270       UpdateISAToDescriptorMapIfNeeded();
271     }
272   }
273 
274   virtual ObjCISA GetISA(ConstString name);
275 
276   virtual ObjCISA GetParentClass(ObjCISA isa);
277 
278   // Finds the byte offset of the child_type ivar in parent_type.  If it can't
279   // find the offset, returns LLDB_INVALID_IVAR_OFFSET.
280 
281   virtual size_t GetByteOffsetForIvar(CompilerType &parent_qual_type,
282                                       const char *ivar_name);
283 
HasNewLiteralsAndIndexing()284   bool HasNewLiteralsAndIndexing() {
285     if (m_has_new_literals_and_indexing == eLazyBoolCalculate) {
286       if (CalculateHasNewLiteralsAndIndexing())
287         m_has_new_literals_and_indexing = eLazyBoolYes;
288       else
289         m_has_new_literals_and_indexing = eLazyBoolNo;
290     }
291 
292     return (m_has_new_literals_and_indexing == eLazyBoolYes);
293   }
294 
SymbolsDidLoad(const ModuleList & module_list)295   void SymbolsDidLoad(const ModuleList &module_list) override {
296     m_negative_complete_class_cache.clear();
297   }
298 
299   bool GetTypeBitSize(const CompilerType &compiler_type,
300                       uint64_t &size) override;
301 
302   /// Check whether the name is "self" or "_cmd" and should show up in
303   /// "frame variable".
304   bool IsAllowedRuntimeValue(ConstString name) override;
305 
306 protected:
307   // Classes that inherit from ObjCLanguageRuntime can see and modify these
308   ObjCLanguageRuntime(Process *process);
309 
CalculateHasNewLiteralsAndIndexing()310   virtual bool CalculateHasNewLiteralsAndIndexing() { return false; }
311 
ISAIsCached(ObjCISA isa)312   bool ISAIsCached(ObjCISA isa) const {
313     return m_isa_to_descriptor.find(isa) != m_isa_to_descriptor.end();
314   }
315 
AddClass(ObjCISA isa,const ClassDescriptorSP & descriptor_sp)316   bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp) {
317     if (isa != 0) {
318       m_isa_to_descriptor[isa] = descriptor_sp;
319       return true;
320     }
321     return false;
322   }
323 
324   bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp,
325                 const char *class_name);
326 
AddClass(ObjCISA isa,const ClassDescriptorSP & descriptor_sp,uint32_t class_name_hash)327   bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp,
328                 uint32_t class_name_hash) {
329     if (isa != 0) {
330       m_isa_to_descriptor[isa] = descriptor_sp;
331       m_hash_to_isa_map.insert(std::make_pair(class_name_hash, isa));
332       return true;
333     }
334     return false;
335   }
336 
337 private:
338   // We keep a map of <Class,Selector>->Implementation so we don't have to call
339   // the resolver function over and over.
340 
341   // FIXME: We need to watch for the loading of Protocols, and flush the cache
342   // for any
343   // class that we see so changed.
344 
345   struct ClassAndSel {
ClassAndSelClassAndSel346     ClassAndSel() {
347       sel_addr = LLDB_INVALID_ADDRESS;
348       class_addr = LLDB_INVALID_ADDRESS;
349     }
350 
ClassAndSelClassAndSel351     ClassAndSel(lldb::addr_t in_sel_addr, lldb::addr_t in_class_addr)
352         : class_addr(in_class_addr), sel_addr(in_sel_addr) {}
353 
354     bool operator==(const ClassAndSel &rhs) {
355       if (class_addr == rhs.class_addr && sel_addr == rhs.sel_addr)
356         return true;
357       else
358         return false;
359     }
360 
361     bool operator<(const ClassAndSel &rhs) const {
362       if (class_addr < rhs.class_addr)
363         return true;
364       else if (class_addr > rhs.class_addr)
365         return false;
366       else {
367         if (sel_addr < rhs.sel_addr)
368           return true;
369         else
370           return false;
371       }
372     }
373 
374     lldb::addr_t class_addr;
375     lldb::addr_t sel_addr;
376   };
377 
378   typedef std::map<ClassAndSel, lldb::addr_t> MsgImplMap;
379   typedef std::map<ObjCISA, ClassDescriptorSP> ISAToDescriptorMap;
380   typedef std::multimap<uint32_t, ObjCISA> HashToISAMap;
381   typedef ISAToDescriptorMap::iterator ISAToDescriptorIterator;
382   typedef HashToISAMap::iterator HashToISAIterator;
383   typedef ThreadSafeDenseMap<void *, uint64_t> TypeSizeCache;
384 
385   MsgImplMap m_impl_cache;
386   LazyBool m_has_new_literals_and_indexing;
387   ISAToDescriptorMap m_isa_to_descriptor;
388   HashToISAMap m_hash_to_isa_map;
389   TypeSizeCache m_type_size_cache;
390 
391 protected:
392   uint32_t m_isa_to_descriptor_stop_id;
393 
394   typedef std::map<ConstString, lldb::TypeWP> CompleteClassMap;
395   CompleteClassMap m_complete_class_cache;
396 
397   struct ConstStringSetHelpers {
operatorConstStringSetHelpers398     size_t operator()(ConstString arg) const // for hashing
399     {
400       return (size_t)arg.GetCString();
401     }
operatorConstStringSetHelpers402     bool operator()(ConstString arg1,
403                     ConstString arg2) const // for equality
404     {
405       return arg1.operator==(arg2);
406     }
407   };
408   typedef std::unordered_set<ConstString, ConstStringSetHelpers,
409                              ConstStringSetHelpers>
410       CompleteClassSet;
411   CompleteClassSet m_negative_complete_class_cache;
412 
413   ISAToDescriptorIterator GetDescriptorIterator(ConstString name);
414 
415   friend class ::CommandObjectObjC_ClassTable_Dump;
416 
417   std::pair<ISAToDescriptorIterator, ISAToDescriptorIterator>
418   GetDescriptorIteratorPair(bool update_if_needed = true);
419 
420   void ReadObjCLibraryIfNeeded(const ModuleList &module_list);
421 
422   ObjCLanguageRuntime(const ObjCLanguageRuntime &) = delete;
423   const ObjCLanguageRuntime &operator=(const ObjCLanguageRuntime &) = delete;
424 };
425 
426 } // namespace lldb_private
427 
428 #endif // LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_OBJC_OBJCLANGUAGERUNTIME_H
429