From 9bbf396032ad37a321a8be203838b5723596e2dd Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Thu, 7 Jan 2021 13:44:45 -0800 Subject: [PATCH 1/4] Make assertion failures easier to diagnose by splitting the assertion up. --- .../Swift/TypeSystemSwiftTypeRef.cpp | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp index e102828da5b68..919714ff2a880 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp +++ b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp @@ -2342,20 +2342,19 @@ CompilerType TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex( llvm::StringRef suffix(ast_child_name); if (suffix.consume_front("__ObjC.")) ast_child_name = suffix.str(); + assert((llvm::StringRef(child_name).contains('.') || + Equivalent(child_name, ast_child_name))); + assert((Equivalent(llvm::Optional(child_byte_size), + llvm::Optional(ast_child_byte_size)) || + ast_language_flags)); + assert(Equivalent(llvm::Optional(child_byte_offset), + llvm::Optional(ast_child_byte_offset))); assert( - (llvm::StringRef(child_name).contains('.') || - Equivalent(child_name, ast_child_name)) && - (Equivalent(llvm::Optional(child_byte_size), - llvm::Optional(ast_child_byte_size)) || - ast_language_flags) && - Equivalent(llvm::Optional(child_byte_offset), - llvm::Optional(ast_child_byte_offset)) && - Equivalent(child_bitfield_bit_offset, ast_child_bitfield_bit_offset) && - Equivalent(child_bitfield_bit_size, ast_child_bitfield_bit_size) && - Equivalent(child_is_base_class, ast_child_is_base_class) && - Equivalent(child_is_deref_of_parent, ast_child_is_deref_of_parent) && - Equivalent(language_flags, ast_language_flags) && - "TypeSystemSwiftTypeRef diverges from SwiftASTContext"); + Equivalent(child_bitfield_bit_offset, ast_child_bitfield_bit_offset)); + assert(Equivalent(child_bitfield_bit_size, ast_child_bitfield_bit_size)); + assert(Equivalent(child_is_base_class, ast_child_is_base_class)); + assert(Equivalent(child_is_deref_of_parent, ast_child_is_deref_of_parent)); + assert(Equivalent(language_flags, ast_language_flags)); }); #endif VALIDATE_AND_RETURN( From 0627fa31628e7dc06cc48e373a2685989360e048 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Thu, 7 Jan 2021 13:46:03 -0800 Subject: [PATCH 2/4] Add defensive early exits. --- .../ExpressionParser/Swift/SwiftExpressionParser.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp b/lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp index 754c6a13b9e7a..7321f7662746a 100644 --- a/lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp +++ b/lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp @@ -550,6 +550,8 @@ static void AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp, auto *stack_frame = stack_frame_sp.get(); imported_self_type = swift_runtime->BindGenericTypeParameters( *stack_frame, imported_self_type); + if (!imported_self_type) + return; { auto *swift_type_system = llvm::dyn_cast_or_null( @@ -561,6 +563,8 @@ static void AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp, // extend the referent: imported_self_type = swift_type_system->GetReferentType( imported_self_type.GetOpaqueQualType()); + if (!imported_self_type) + return; } { @@ -573,6 +577,8 @@ static void AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp, // and we have to grab the instance type: imported_self_type = swift_type_system->GetInstanceType( imported_self_type.GetOpaqueQualType()); + if (!imported_self_type) + return; } Flags imported_self_type_flags(imported_self_type.GetTypeInfo()); From c9ba5e27b8025c03ea414b46cb6fbd04b4f2dd3d Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Thu, 7 Jan 2021 13:48:05 -0800 Subject: [PATCH 3/4] Rename parameter to better show intent --- .../Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp index 919714ff2a880..1032d2aec772a 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp +++ b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp @@ -2176,12 +2176,12 @@ CompilerType TypeSystemSwiftTypeRef::GetFieldAtIndex( static swift::Demangle::NodePointer GetClangTypeTypeNode(TypeSystemSwiftTypeRef &ts, swift::Demangle::Demangler &dem, CompilerType clang_type, - SwiftASTContext *swift_ast_context) { + SwiftASTContext *module_holder) { assert(llvm::isa(clang_type.GetTypeSystem()) && "expected a clang type"); using namespace swift::Demangle; NodePointer type = dem.createNode(Node::Kind::Type); - type->addChild(GetClangTypeNode(clang_type, dem, swift_ast_context), dem); + type->addChild(GetClangTypeNode(clang_type, dem, module_holder), dem); return type; } From e27eeeb1d554f41a20515e62227903557f542edd Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Thu, 7 Jan 2021 15:50:14 -0800 Subject: [PATCH 4/4] Relax overly strict cast. LLDBTypeInfoProvider only requires a TypeSystemSwift. --- .../SwiftLanguageRuntimeDynamicTypeResolution.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lldb/source/Target/SwiftLanguageRuntimeDynamicTypeResolution.cpp b/lldb/source/Target/SwiftLanguageRuntimeDynamicTypeResolution.cpp index a8c8028df6fec..c278a17546abe 100644 --- a/lldb/source/Target/SwiftLanguageRuntimeDynamicTypeResolution.cpp +++ b/lldb/source/Target/SwiftLanguageRuntimeDynamicTypeResolution.cpp @@ -1485,6 +1485,11 @@ bool SwiftLanguageRuntimeImpl::ForEachSuperClassType( auto *reflection_ctx = GetReflectionContext(); if (!reflection_ctx) return false; + CompilerType instance_type = instance.GetCompilerType(); + auto *ts = + llvm::dyn_cast_or_null(instance_type.GetTypeSystem()); + if (!ts) + return false; lldb::addr_t pointer = instance.GetPointerValue(); // Maybe this belongs into GetPointerValue, but on the other hand it @@ -1494,7 +1499,7 @@ bool SwiftLanguageRuntimeImpl::ForEachSuperClassType( // libReflection cannot tell up how many bits to strip from // multi-payload enum values. auto addr_deref = - FixupPointerValue(pointer, instance.GetCompilerType()); + FixupPointerValue(pointer, instance_type); pointer = addr_deref.first; if (addr_deref.second) { // This is a reference storage object. @@ -1504,11 +1509,6 @@ bool SwiftLanguageRuntimeImpl::ForEachSuperClassType( return false; } - auto *ts = llvm::dyn_cast_or_null( - instance.GetCompilerType().GetTypeSystem()); - if (!ts) - return false; - auto md_ptr = reflection_ctx->readMetadataFromInstance(pointer); if (!md_ptr) return false;