1 //===-- ObjCLanguageRuntime.cpp ---------------------------------*- 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 #include "clang/AST/Type.h" 9 10 #include "ObjCLanguageRuntime.h" 11 12 #include "lldb/Core/MappedHash.h" 13 #include "lldb/Core/Module.h" 14 #include "lldb/Core/PluginManager.h" 15 #include "lldb/Core/ValueObject.h" 16 #include "lldb/Symbol/ClangASTContext.h" 17 #include "lldb/Symbol/SymbolContext.h" 18 #include "lldb/Symbol/SymbolFile.h" 19 #include "lldb/Symbol/Type.h" 20 #include "lldb/Symbol/TypeList.h" 21 #include "lldb/Symbol/Variable.h" 22 #include "lldb/Target/Target.h" 23 #include "lldb/Utility/Log.h" 24 #include "lldb/Utility/Timer.h" 25 26 #include "llvm/ADT/StringRef.h" 27 #include "llvm/Support/DJB.h" 28 29 using namespace lldb; 30 using namespace lldb_private; 31 32 char ObjCLanguageRuntime::ID = 0; 33 34 // Destructor 35 ObjCLanguageRuntime::~ObjCLanguageRuntime() {} 36 37 ObjCLanguageRuntime::ObjCLanguageRuntime(Process *process) 38 : LanguageRuntime(process), m_impl_cache(), 39 m_has_new_literals_and_indexing(eLazyBoolCalculate), 40 m_isa_to_descriptor(), m_hash_to_isa_map(), m_type_size_cache(), 41 m_isa_to_descriptor_stop_id(UINT32_MAX), m_complete_class_cache(), 42 m_negative_complete_class_cache() {} 43 44 bool ObjCLanguageRuntime::IsWhitelistedRuntimeValue(ConstString name) { 45 static ConstString g_self = ConstString("self"); 46 static ConstString g_cmd = ConstString("_cmd"); 47 return name == g_self || name == g_cmd; 48 } 49 50 bool ObjCLanguageRuntime::AddClass(ObjCISA isa, 51 const ClassDescriptorSP &descriptor_sp, 52 const char *class_name) { 53 if (isa != 0) { 54 m_isa_to_descriptor[isa] = descriptor_sp; 55 // class_name is assumed to be valid 56 m_hash_to_isa_map.insert(std::make_pair(llvm::djbHash(class_name), isa)); 57 return true; 58 } 59 return false; 60 } 61 62 void ObjCLanguageRuntime::AddToMethodCache(lldb::addr_t class_addr, 63 lldb::addr_t selector, 64 lldb::addr_t impl_addr) { 65 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 66 if (log) { 67 LLDB_LOGF(log, 68 "Caching: class 0x%" PRIx64 " selector 0x%" PRIx64 69 " implementation 0x%" PRIx64 ".", 70 class_addr, selector, impl_addr); 71 } 72 m_impl_cache.insert(std::pair<ClassAndSel, lldb::addr_t>( 73 ClassAndSel(class_addr, selector), impl_addr)); 74 } 75 76 lldb::addr_t ObjCLanguageRuntime::LookupInMethodCache(lldb::addr_t class_addr, 77 lldb::addr_t selector) { 78 MsgImplMap::iterator pos, end = m_impl_cache.end(); 79 pos = m_impl_cache.find(ClassAndSel(class_addr, selector)); 80 if (pos != end) 81 return (*pos).second; 82 return LLDB_INVALID_ADDRESS; 83 } 84 85 lldb::TypeSP 86 ObjCLanguageRuntime::LookupInCompleteClassCache(ConstString &name) { 87 CompleteClassMap::iterator complete_class_iter = 88 m_complete_class_cache.find(name); 89 90 if (complete_class_iter != m_complete_class_cache.end()) { 91 // Check the weak pointer to make sure the type hasn't been unloaded 92 TypeSP complete_type_sp(complete_class_iter->second.lock()); 93 94 if (complete_type_sp) 95 return complete_type_sp; 96 else 97 m_complete_class_cache.erase(name); 98 } 99 100 if (m_negative_complete_class_cache.count(name) > 0) 101 return TypeSP(); 102 103 const ModuleList &modules = m_process->GetTarget().GetImages(); 104 105 SymbolContextList sc_list; 106 modules.FindSymbolsWithNameAndType(name, eSymbolTypeObjCClass, sc_list); 107 const size_t matching_symbols = sc_list.GetSize(); 108 109 if (matching_symbols) { 110 SymbolContext sc; 111 112 sc_list.GetContextAtIndex(0, sc); 113 114 ModuleSP module_sp(sc.module_sp); 115 116 if (!module_sp) 117 return TypeSP(); 118 119 const bool exact_match = true; 120 const uint32_t max_matches = UINT32_MAX; 121 TypeList types; 122 123 llvm::DenseSet<SymbolFile *> searched_symbol_files; 124 module_sp->FindTypes(name, exact_match, max_matches, searched_symbol_files, 125 types); 126 127 for (uint32_t i = 0; i < types.GetSize(); ++i) { 128 TypeSP type_sp(types.GetTypeAtIndex(i)); 129 130 if (ClangASTContext::IsObjCObjectOrInterfaceType( 131 type_sp->GetForwardCompilerType())) { 132 if (type_sp->IsCompleteObjCClass()) { 133 m_complete_class_cache[name] = type_sp; 134 return type_sp; 135 } 136 } 137 } 138 } 139 m_negative_complete_class_cache.insert(name); 140 return TypeSP(); 141 } 142 143 size_t ObjCLanguageRuntime::GetByteOffsetForIvar(CompilerType &parent_qual_type, 144 const char *ivar_name) { 145 return LLDB_INVALID_IVAR_OFFSET; 146 } 147 148 bool ObjCLanguageRuntime::ClassDescriptor::IsPointerValid( 149 lldb::addr_t value, uint32_t ptr_size, bool allow_NULLs, bool allow_tagged, 150 bool check_version_specific) const { 151 if (!value) 152 return allow_NULLs; 153 if ((value % 2) == 1 && allow_tagged) 154 return true; 155 if ((value % ptr_size) == 0) 156 return (check_version_specific ? CheckPointer(value, ptr_size) : true); 157 else 158 return false; 159 } 160 161 ObjCLanguageRuntime::ObjCISA 162 ObjCLanguageRuntime::GetISA(ConstString name) { 163 ISAToDescriptorIterator pos = GetDescriptorIterator(name); 164 if (pos != m_isa_to_descriptor.end()) 165 return pos->first; 166 return 0; 167 } 168 169 ObjCLanguageRuntime::ISAToDescriptorIterator 170 ObjCLanguageRuntime::GetDescriptorIterator(ConstString name) { 171 ISAToDescriptorIterator end = m_isa_to_descriptor.end(); 172 173 if (name) { 174 UpdateISAToDescriptorMap(); 175 if (m_hash_to_isa_map.empty()) { 176 // No name hashes were provided, we need to just linearly power through 177 // the names and find a match 178 for (ISAToDescriptorIterator pos = m_isa_to_descriptor.begin(); 179 pos != end; ++pos) { 180 if (pos->second->GetClassName() == name) 181 return pos; 182 } 183 } else { 184 // Name hashes were provided, so use them to efficiently lookup name to 185 // isa/descriptor 186 const uint32_t name_hash = llvm::djbHash(name.GetStringRef()); 187 std::pair<HashToISAIterator, HashToISAIterator> range = 188 m_hash_to_isa_map.equal_range(name_hash); 189 for (HashToISAIterator range_pos = range.first; range_pos != range.second; 190 ++range_pos) { 191 ISAToDescriptorIterator pos = 192 m_isa_to_descriptor.find(range_pos->second); 193 if (pos != m_isa_to_descriptor.end()) { 194 if (pos->second->GetClassName() == name) 195 return pos; 196 } 197 } 198 } 199 } 200 return end; 201 } 202 203 std::pair<ObjCLanguageRuntime::ISAToDescriptorIterator, 204 ObjCLanguageRuntime::ISAToDescriptorIterator> 205 ObjCLanguageRuntime::GetDescriptorIteratorPair(bool update_if_needed) { 206 if (update_if_needed) 207 UpdateISAToDescriptorMapIfNeeded(); 208 209 return std::pair<ObjCLanguageRuntime::ISAToDescriptorIterator, 210 ObjCLanguageRuntime::ISAToDescriptorIterator>( 211 m_isa_to_descriptor.begin(), m_isa_to_descriptor.end()); 212 } 213 214 ObjCLanguageRuntime::ObjCISA 215 ObjCLanguageRuntime::GetParentClass(ObjCLanguageRuntime::ObjCISA isa) { 216 ClassDescriptorSP objc_class_sp(GetClassDescriptorFromISA(isa)); 217 if (objc_class_sp) { 218 ClassDescriptorSP objc_super_class_sp(objc_class_sp->GetSuperclass()); 219 if (objc_super_class_sp) 220 return objc_super_class_sp->GetISA(); 221 } 222 return 0; 223 } 224 225 ConstString 226 ObjCLanguageRuntime::GetActualTypeName(ObjCLanguageRuntime::ObjCISA isa) { 227 ClassDescriptorSP objc_class_sp(GetNonKVOClassDescriptor(isa)); 228 if (objc_class_sp) 229 return objc_class_sp->GetClassName(); 230 return ConstString(); 231 } 232 233 ObjCLanguageRuntime::ClassDescriptorSP 234 ObjCLanguageRuntime::GetClassDescriptorFromClassName( 235 ConstString class_name) { 236 ISAToDescriptorIterator pos = GetDescriptorIterator(class_name); 237 if (pos != m_isa_to_descriptor.end()) 238 return pos->second; 239 return ClassDescriptorSP(); 240 } 241 242 ObjCLanguageRuntime::ClassDescriptorSP 243 ObjCLanguageRuntime::GetClassDescriptor(ValueObject &valobj) { 244 ClassDescriptorSP objc_class_sp; 245 // if we get an invalid VO (which might still happen when playing around with 246 // pointers returned by the expression parser, don't consider this a valid 247 // ObjC object) 248 if (valobj.GetCompilerType().IsValid()) { 249 addr_t isa_pointer = valobj.GetPointerValue(); 250 if (isa_pointer != LLDB_INVALID_ADDRESS) { 251 ExecutionContext exe_ctx(valobj.GetExecutionContextRef()); 252 253 Process *process = exe_ctx.GetProcessPtr(); 254 if (process) { 255 Status error; 256 ObjCISA isa = process->ReadPointerFromMemory(isa_pointer, error); 257 if (isa != LLDB_INVALID_ADDRESS) 258 objc_class_sp = GetClassDescriptorFromISA(isa); 259 } 260 } 261 } 262 return objc_class_sp; 263 } 264 265 ObjCLanguageRuntime::ClassDescriptorSP 266 ObjCLanguageRuntime::GetNonKVOClassDescriptor(ValueObject &valobj) { 267 ObjCLanguageRuntime::ClassDescriptorSP objc_class_sp( 268 GetClassDescriptor(valobj)); 269 if (objc_class_sp) { 270 if (!objc_class_sp->IsKVO()) 271 return objc_class_sp; 272 273 ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass()); 274 if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid()) 275 return non_kvo_objc_class_sp; 276 } 277 return ClassDescriptorSP(); 278 } 279 280 ObjCLanguageRuntime::ClassDescriptorSP 281 ObjCLanguageRuntime::GetClassDescriptorFromISA(ObjCISA isa) { 282 if (isa) { 283 UpdateISAToDescriptorMap(); 284 ObjCLanguageRuntime::ISAToDescriptorIterator pos = 285 m_isa_to_descriptor.find(isa); 286 if (pos != m_isa_to_descriptor.end()) 287 return pos->second; 288 } 289 return ClassDescriptorSP(); 290 } 291 292 ObjCLanguageRuntime::ClassDescriptorSP 293 ObjCLanguageRuntime::GetNonKVOClassDescriptor(ObjCISA isa) { 294 if (isa) { 295 ClassDescriptorSP objc_class_sp = GetClassDescriptorFromISA(isa); 296 if (objc_class_sp && objc_class_sp->IsValid()) { 297 if (!objc_class_sp->IsKVO()) 298 return objc_class_sp; 299 300 ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass()); 301 if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid()) 302 return non_kvo_objc_class_sp; 303 } 304 } 305 return ClassDescriptorSP(); 306 } 307 308 CompilerType 309 ObjCLanguageRuntime::EncodingToType::RealizeType(const char *name, 310 bool for_expression) { 311 if (m_scratch_ast_ctx_up) 312 return RealizeType(*m_scratch_ast_ctx_up, name, for_expression); 313 return CompilerType(); 314 } 315 316 ObjCLanguageRuntime::EncodingToType::~EncodingToType() {} 317 318 ObjCLanguageRuntime::EncodingToTypeSP ObjCLanguageRuntime::GetEncodingToType() { 319 return nullptr; 320 } 321 322 bool ObjCLanguageRuntime::GetTypeBitSize(const CompilerType &compiler_type, 323 uint64_t &size) { 324 void *opaque_ptr = compiler_type.GetOpaqueQualType(); 325 size = m_type_size_cache.Lookup(opaque_ptr); 326 // an ObjC object will at least have an ISA, so 0 is definitely not OK 327 if (size > 0) 328 return true; 329 330 ClassDescriptorSP class_descriptor_sp = 331 GetClassDescriptorFromClassName(compiler_type.GetTypeName()); 332 if (!class_descriptor_sp) 333 return false; 334 335 int32_t max_offset = INT32_MIN; 336 uint64_t sizeof_max = 0; 337 bool found = false; 338 339 for (size_t idx = 0; idx < class_descriptor_sp->GetNumIVars(); idx++) { 340 const auto &ivar = class_descriptor_sp->GetIVarAtIndex(idx); 341 int32_t cur_offset = ivar.m_offset; 342 if (cur_offset > max_offset) { 343 max_offset = cur_offset; 344 sizeof_max = ivar.m_size; 345 found = true; 346 } 347 } 348 349 size = 8 * (max_offset + sizeof_max); 350 if (found) 351 m_type_size_cache.Insert(opaque_ptr, size); 352 353 return found; 354 } 355 356 lldb::BreakpointPreconditionSP 357 ObjCLanguageRuntime::GetBreakpointExceptionPrecondition(LanguageType language, 358 bool throw_bp) { 359 if (language != eLanguageTypeObjC) 360 return lldb::BreakpointPreconditionSP(); 361 if (!throw_bp) 362 return lldb::BreakpointPreconditionSP(); 363 BreakpointPreconditionSP precondition_sp( 364 new ObjCLanguageRuntime::ObjCExceptionPrecondition()); 365 return precondition_sp; 366 } 367 368 // Exception breakpoint Precondition class for ObjC: 369 void ObjCLanguageRuntime::ObjCExceptionPrecondition::AddClassName( 370 const char *class_name) { 371 m_class_names.insert(class_name); 372 } 373 374 ObjCLanguageRuntime::ObjCExceptionPrecondition::ObjCExceptionPrecondition() {} 375 376 bool ObjCLanguageRuntime::ObjCExceptionPrecondition::EvaluatePrecondition( 377 StoppointCallbackContext &context) { 378 return true; 379 } 380 381 void ObjCLanguageRuntime::ObjCExceptionPrecondition::GetDescription( 382 Stream &stream, lldb::DescriptionLevel level) {} 383 384 Status ObjCLanguageRuntime::ObjCExceptionPrecondition::ConfigurePrecondition( 385 Args &args) { 386 Status error; 387 if (args.GetArgumentCount() > 0) 388 error.SetErrorString( 389 "The ObjC Exception breakpoint doesn't support extra options."); 390 return error; 391 } 392 393 llvm::Optional<CompilerType> 394 ObjCLanguageRuntime::GetRuntimeType(CompilerType base_type) { 395 CompilerType class_type; 396 bool is_pointer_type = false; 397 398 if (ClangASTContext::IsObjCObjectPointerType(base_type, &class_type)) 399 is_pointer_type = true; 400 else if (ClangASTContext::IsObjCObjectOrInterfaceType(base_type)) 401 class_type = base_type; 402 else 403 return llvm::None; 404 405 if (!class_type) 406 return llvm::None; 407 408 ConstString class_name(class_type.GetConstTypeName()); 409 if (!class_name) 410 return llvm::None; 411 412 TypeSP complete_objc_class_type_sp = LookupInCompleteClassCache(class_name); 413 if (!complete_objc_class_type_sp) 414 return llvm::None; 415 416 CompilerType complete_class( 417 complete_objc_class_type_sp->GetFullCompilerType()); 418 if (complete_class.GetCompleteType()) { 419 if (is_pointer_type) 420 return complete_class.GetPointerType(); 421 else 422 return complete_class; 423 } 424 425 return llvm::None; 426 } 427