Coverage Report

Created: 2023-09-30 09:22

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