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 const size_t matching_symbols = 107 modules.FindSymbolsWithNameAndType(name, eSymbolTypeObjCClass, sc_list); 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 const uint32_t num_types = module_sp->FindTypes( 125 name, exact_match, max_matches, searched_symbol_files, types); 126 127 if (num_types) { 128 uint32_t i; 129 for (i = 0; i < num_types; ++i) { 130 TypeSP type_sp(types.GetTypeAtIndex(i)); 131 132 if (ClangASTContext::IsObjCObjectOrInterfaceType( 133 type_sp->GetForwardCompilerType())) { 134 if (type_sp->IsCompleteObjCClass()) { 135 m_complete_class_cache[name] = type_sp; 136 return type_sp; 137 } 138 } 139 } 140 } 141 } 142 m_negative_complete_class_cache.insert(name); 143 return TypeSP(); 144 } 145 146 size_t ObjCLanguageRuntime::GetByteOffsetForIvar(CompilerType &parent_qual_type, 147 const char *ivar_name) { 148 return LLDB_INVALID_IVAR_OFFSET; 149 } 150 151 bool ObjCLanguageRuntime::ClassDescriptor::IsPointerValid( 152 lldb::addr_t value, uint32_t ptr_size, bool allow_NULLs, bool allow_tagged, 153 bool check_version_specific) const { 154 if (!value) 155 return allow_NULLs; 156 if ((value % 2) == 1 && allow_tagged) 157 return true; 158 if ((value % ptr_size) == 0) 159 return (check_version_specific ? CheckPointer(value, ptr_size) : true); 160 else 161 return false; 162 } 163 164 ObjCLanguageRuntime::ObjCISA 165 ObjCLanguageRuntime::GetISA(ConstString name) { 166 ISAToDescriptorIterator pos = GetDescriptorIterator(name); 167 if (pos != m_isa_to_descriptor.end()) 168 return pos->first; 169 return 0; 170 } 171 172 ObjCLanguageRuntime::ISAToDescriptorIterator 173 ObjCLanguageRuntime::GetDescriptorIterator(ConstString name) { 174 ISAToDescriptorIterator end = m_isa_to_descriptor.end(); 175 176 if (name) { 177 UpdateISAToDescriptorMap(); 178 if (m_hash_to_isa_map.empty()) { 179 // No name hashes were provided, we need to just linearly power through 180 // the names and find a match 181 for (ISAToDescriptorIterator pos = m_isa_to_descriptor.begin(); 182 pos != end; ++pos) { 183 if (pos->second->GetClassName() == name) 184 return pos; 185 } 186 } else { 187 // Name hashes were provided, so use them to efficiently lookup name to 188 // isa/descriptor 189 const uint32_t name_hash = llvm::djbHash(name.GetStringRef()); 190 std::pair<HashToISAIterator, HashToISAIterator> range = 191 m_hash_to_isa_map.equal_range(name_hash); 192 for (HashToISAIterator range_pos = range.first; range_pos != range.second; 193 ++range_pos) { 194 ISAToDescriptorIterator pos = 195 m_isa_to_descriptor.find(range_pos->second); 196 if (pos != m_isa_to_descriptor.end()) { 197 if (pos->second->GetClassName() == name) 198 return pos; 199 } 200 } 201 } 202 } 203 return end; 204 } 205 206 std::pair<ObjCLanguageRuntime::ISAToDescriptorIterator, 207 ObjCLanguageRuntime::ISAToDescriptorIterator> 208 ObjCLanguageRuntime::GetDescriptorIteratorPair(bool update_if_needed) { 209 if (update_if_needed) 210 UpdateISAToDescriptorMapIfNeeded(); 211 212 return std::pair<ObjCLanguageRuntime::ISAToDescriptorIterator, 213 ObjCLanguageRuntime::ISAToDescriptorIterator>( 214 m_isa_to_descriptor.begin(), m_isa_to_descriptor.end()); 215 } 216 217 ObjCLanguageRuntime::ObjCISA 218 ObjCLanguageRuntime::GetParentClass(ObjCLanguageRuntime::ObjCISA isa) { 219 ClassDescriptorSP objc_class_sp(GetClassDescriptorFromISA(isa)); 220 if (objc_class_sp) { 221 ClassDescriptorSP objc_super_class_sp(objc_class_sp->GetSuperclass()); 222 if (objc_super_class_sp) 223 return objc_super_class_sp->GetISA(); 224 } 225 return 0; 226 } 227 228 ConstString 229 ObjCLanguageRuntime::GetActualTypeName(ObjCLanguageRuntime::ObjCISA isa) { 230 ClassDescriptorSP objc_class_sp(GetNonKVOClassDescriptor(isa)); 231 if (objc_class_sp) 232 return objc_class_sp->GetClassName(); 233 return ConstString(); 234 } 235 236 ObjCLanguageRuntime::ClassDescriptorSP 237 ObjCLanguageRuntime::GetClassDescriptorFromClassName( 238 ConstString class_name) { 239 ISAToDescriptorIterator pos = GetDescriptorIterator(class_name); 240 if (pos != m_isa_to_descriptor.end()) 241 return pos->second; 242 return ClassDescriptorSP(); 243 } 244 245 ObjCLanguageRuntime::ClassDescriptorSP 246 ObjCLanguageRuntime::GetClassDescriptor(ValueObject &valobj) { 247 ClassDescriptorSP objc_class_sp; 248 // if we get an invalid VO (which might still happen when playing around with 249 // pointers returned by the expression parser, don't consider this a valid 250 // ObjC object) 251 if (valobj.GetCompilerType().IsValid()) { 252 addr_t isa_pointer = valobj.GetPointerValue(); 253 if (isa_pointer != LLDB_INVALID_ADDRESS) { 254 ExecutionContext exe_ctx(valobj.GetExecutionContextRef()); 255 256 Process *process = exe_ctx.GetProcessPtr(); 257 if (process) { 258 Status error; 259 ObjCISA isa = process->ReadPointerFromMemory(isa_pointer, error); 260 if (isa != LLDB_INVALID_ADDRESS) 261 objc_class_sp = GetClassDescriptorFromISA(isa); 262 } 263 } 264 } 265 return objc_class_sp; 266 } 267 268 ObjCLanguageRuntime::ClassDescriptorSP 269 ObjCLanguageRuntime::GetNonKVOClassDescriptor(ValueObject &valobj) { 270 ObjCLanguageRuntime::ClassDescriptorSP objc_class_sp( 271 GetClassDescriptor(valobj)); 272 if (objc_class_sp) { 273 if (!objc_class_sp->IsKVO()) 274 return objc_class_sp; 275 276 ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass()); 277 if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid()) 278 return non_kvo_objc_class_sp; 279 } 280 return ClassDescriptorSP(); 281 } 282 283 ObjCLanguageRuntime::ClassDescriptorSP 284 ObjCLanguageRuntime::GetClassDescriptorFromISA(ObjCISA isa) { 285 if (isa) { 286 UpdateISAToDescriptorMap(); 287 ObjCLanguageRuntime::ISAToDescriptorIterator pos = 288 m_isa_to_descriptor.find(isa); 289 if (pos != m_isa_to_descriptor.end()) 290 return pos->second; 291 } 292 return ClassDescriptorSP(); 293 } 294 295 ObjCLanguageRuntime::ClassDescriptorSP 296 ObjCLanguageRuntime::GetNonKVOClassDescriptor(ObjCISA isa) { 297 if (isa) { 298 ClassDescriptorSP objc_class_sp = GetClassDescriptorFromISA(isa); 299 if (objc_class_sp && objc_class_sp->IsValid()) { 300 if (!objc_class_sp->IsKVO()) 301 return objc_class_sp; 302 303 ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass()); 304 if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid()) 305 return non_kvo_objc_class_sp; 306 } 307 } 308 return ClassDescriptorSP(); 309 } 310 311 CompilerType 312 ObjCLanguageRuntime::EncodingToType::RealizeType(const char *name, 313 bool for_expression) { 314 if (m_scratch_ast_ctx_up) 315 return RealizeType(*m_scratch_ast_ctx_up, name, for_expression); 316 return CompilerType(); 317 } 318 319 CompilerType ObjCLanguageRuntime::EncodingToType::RealizeType( 320 ClangASTContext &ast_ctx, const char *name, bool for_expression) { 321 clang::ASTContext *clang_ast = ast_ctx.getASTContext(); 322 if (!clang_ast) 323 return CompilerType(); 324 return RealizeType(*clang_ast, name, for_expression); 325 } 326 327 ObjCLanguageRuntime::EncodingToType::~EncodingToType() {} 328 329 ObjCLanguageRuntime::EncodingToTypeSP ObjCLanguageRuntime::GetEncodingToType() { 330 return nullptr; 331 } 332 333 bool ObjCLanguageRuntime::GetTypeBitSize(const CompilerType &compiler_type, 334 uint64_t &size) { 335 void *opaque_ptr = compiler_type.GetOpaqueQualType(); 336 size = m_type_size_cache.Lookup(opaque_ptr); 337 // an ObjC object will at least have an ISA, so 0 is definitely not OK 338 if (size > 0) 339 return true; 340 341 ClassDescriptorSP class_descriptor_sp = 342 GetClassDescriptorFromClassName(compiler_type.GetTypeName()); 343 if (!class_descriptor_sp) 344 return false; 345 346 int32_t max_offset = INT32_MIN; 347 uint64_t sizeof_max = 0; 348 bool found = false; 349 350 for (size_t idx = 0; idx < class_descriptor_sp->GetNumIVars(); idx++) { 351 const auto &ivar = class_descriptor_sp->GetIVarAtIndex(idx); 352 int32_t cur_offset = ivar.m_offset; 353 if (cur_offset > max_offset) { 354 max_offset = cur_offset; 355 sizeof_max = ivar.m_size; 356 found = true; 357 } 358 } 359 360 size = 8 * (max_offset + sizeof_max); 361 if (found) 362 m_type_size_cache.Insert(opaque_ptr, size); 363 364 return found; 365 } 366 367 lldb::BreakpointPreconditionSP 368 ObjCLanguageRuntime::GetBreakpointExceptionPrecondition(LanguageType language, 369 bool throw_bp) { 370 if (language != eLanguageTypeObjC) 371 return lldb::BreakpointPreconditionSP(); 372 if (!throw_bp) 373 return lldb::BreakpointPreconditionSP(); 374 BreakpointPreconditionSP precondition_sp( 375 new ObjCLanguageRuntime::ObjCExceptionPrecondition()); 376 return precondition_sp; 377 } 378 379 // Exception breakpoint Precondition class for ObjC: 380 void ObjCLanguageRuntime::ObjCExceptionPrecondition::AddClassName( 381 const char *class_name) { 382 m_class_names.insert(class_name); 383 } 384 385 ObjCLanguageRuntime::ObjCExceptionPrecondition::ObjCExceptionPrecondition() {} 386 387 bool ObjCLanguageRuntime::ObjCExceptionPrecondition::EvaluatePrecondition( 388 StoppointCallbackContext &context) { 389 return true; 390 } 391 392 void ObjCLanguageRuntime::ObjCExceptionPrecondition::GetDescription( 393 Stream &stream, lldb::DescriptionLevel level) {} 394 395 Status ObjCLanguageRuntime::ObjCExceptionPrecondition::ConfigurePrecondition( 396 Args &args) { 397 Status error; 398 if (args.GetArgumentCount() > 0) 399 error.SetErrorString( 400 "The ObjC Exception breakpoint doesn't support extra options."); 401 return error; 402 } 403 404 llvm::Optional<CompilerType> 405 ObjCLanguageRuntime::GetRuntimeType(CompilerType base_type) { 406 CompilerType class_type; 407 bool is_pointer_type = false; 408 409 if (ClangASTContext::IsObjCObjectPointerType(base_type, &class_type)) 410 is_pointer_type = true; 411 else if (ClangASTContext::IsObjCObjectOrInterfaceType(base_type)) 412 class_type = base_type; 413 else 414 return llvm::None; 415 416 if (!class_type) 417 return llvm::None; 418 419 ConstString class_name(class_type.GetConstTypeName()); 420 if (!class_name) 421 return llvm::None; 422 423 TypeSP complete_objc_class_type_sp = LookupInCompleteClassCache(class_name); 424 if (!complete_objc_class_type_sp) 425 return llvm::None; 426 427 CompilerType complete_class( 428 complete_objc_class_type_sp->GetFullCompilerType()); 429 if (complete_class.GetCompleteType()) { 430 if (is_pointer_type) 431 return complete_class.GetPointerType(); 432 else 433 return complete_class; 434 } 435 436 return llvm::None; 437 } 438