Skip to content

[lldb] Avoid crashing when trying reconstruct types with errors #10119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,10 @@ void *TypeSystemSwiftTypeRef::ReconstructType(opaque_compiler_type_t type,
if (m_dangerous_types.count(key))
return nullptr;

// This can crash SwiftASTContext.
if (ContainsError(type))
return nullptr;

auto swift_ast_context = GetSwiftASTContext(GetSymbolContext(exe_ctx));
if (!swift_ast_context || swift_ast_context->HasFatalErrors())
return nullptr;
Expand Down Expand Up @@ -2895,11 +2899,25 @@ bool TypeSystemSwiftTypeRef::IsExpressionEvaluatorDefined(
const auto *mangled_name = AsMangledName(type);
Demangler dem;
NodePointer node = GetDemangledType(dem, mangled_name);
return swift_demangle::FindIf(node, [](NodePointer node) -> NodePointer {
return swift_demangle::FindIf(node, [](NodePointer node) -> bool {
if (node->getKind() == Node::Kind::Module &&
node->getText().starts_with("__lldb_expr"))
return node;
return nullptr;
return true;
return false;
});
}

bool TypeSystemSwiftTypeRef::ContainsError(
lldb::opaque_compiler_type_t type) {
using namespace swift::Demangle;
const auto *mangled_name = AsMangledName(type);
Demangler dem;
NodePointer node = GetDemangledType(dem, mangled_name);
return swift_demangle::FindIf(node, [](NodePointer node) -> bool {
// This node is an in-band error, not a Swift.Error type, which is a protocol.
if (node->getKind() == Node::Kind::ErrorType)
return true;
return false;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
swift::Mangle::ManglingFlavor
GetManglingFlavor(ExecutionContext *exe_ctx = nullptr);

/// Returns true if this type contains an error node anywhere.
bool ContainsError(lldb::opaque_compiler_type_t type);

protected:
/// Determine whether the fallback is enabled via setting.
bool UseSwiftASTContextFallback(const char *func_name,
Expand Down
12 changes: 12 additions & 0 deletions lldb/unittests/Symbol/TestTypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,3 +1013,15 @@ TEST_F(TestTypeSystemSwiftTypeRef, GenericSignature) {
ASSERT_FALSE(maybe_signature.has_value());
}
}

TEST_F(TestTypeSystemSwiftTypeRef, Error) {
using namespace swift::Demangle;
Demangler dem;
NodeBuilder b(dem);
{
NodePointer n = b.GlobalType(b.Node(Node::Kind::ErrorType, "Fatal Error"));
CompilerType t = GetCompilerType(b.Mangle(n));
lldb::opaque_compiler_type_t opaque = t.GetOpaqueQualType();
ASSERT_TRUE(m_swift_ts->ContainsError(opaque));
}
}