Skip to content

Commit a3f2b3d

Browse files
authored
[lldb] Guard more manual validation with GetSwiftValidateTypeSystem check (#6748)
To identify cases where Swift ASTContexts are being unnecessarily created, it helps to avoid these validation-only checks. Check `GetSwiftValidateTypeSystem` before validating. For more context, see #6655.
1 parent f5f1126 commit a3f2b3d

File tree

1 file changed

+50
-41
lines changed

1 file changed

+50
-41
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,8 @@ llvm::Optional<uint64_t> SwiftLanguageRuntimeImpl::GetMemberVariableOffset(
955955
offset = GetMemberVariableOffsetRemoteMirrors(instance_type, instance,
956956
member_name, error);
957957
#ifndef NDEBUG
958-
{
958+
if (ModuleList::GetGlobalModuleListProperties()
959+
.GetSwiftValidateTypeSystem()) {
959960
// Convert to an AST type, if necessary.
960961
if (auto ts = instance_type.GetTypeSystem()
961962
.dyn_cast_or_null<TypeSystemSwiftTypeRef>())
@@ -1990,35 +1991,39 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Class(
19901991
#ifndef NDEBUG
19911992
// Dynamic type resolution in RemoteAST might pull in other Swift modules, so
19921993
// use the scratch context where such operations are legal and safe.
1993-
llvm::Optional<SwiftScratchContextReader> maybe_scratch_ctx =
1994-
in_value.GetSwiftScratchContext();
1995-
if (!maybe_scratch_ctx)
1996-
return false;
1997-
auto scratch_ctx = maybe_scratch_ctx->get();
1998-
if (!scratch_ctx)
1999-
return false;
2000-
SwiftASTContext *swift_ast_ctx = scratch_ctx->GetSwiftASTContext();
2001-
if (!swift_ast_ctx)
2002-
return true;
1994+
if (ModuleList::GetGlobalModuleListProperties()
1995+
.GetSwiftValidateTypeSystem()) {
1996+
llvm::Optional<SwiftScratchContextReader> maybe_scratch_ctx =
1997+
in_value.GetSwiftScratchContext();
1998+
if (!maybe_scratch_ctx)
1999+
return false;
2000+
auto scratch_ctx = maybe_scratch_ctx->get();
2001+
if (!scratch_ctx)
2002+
return false;
2003+
SwiftASTContext *swift_ast_ctx = scratch_ctx->GetSwiftASTContext();
2004+
if (!swift_ast_ctx)
2005+
return true;
20032006

2004-
auto &remote_ast = GetRemoteASTContext(*swift_ast_ctx);
2005-
auto remote_ast_metadata_address = remote_ast.getHeapMetadataForObject(
2006-
swift::remote::RemoteAddress(instance_ptr));
2007-
if (remote_ast_metadata_address) {
2008-
auto instance_type = remote_ast.getTypeForRemoteTypeMetadata(
2009-
remote_ast_metadata_address.getValue(),
2010-
/*skipArtificial=*/true);
2011-
if (instance_type) {
2012-
auto ref_type = ToCompilerType(instance_type.getValue());
2013-
ConstString a = ref_type.GetMangledTypeName();
2014-
ConstString b = class_type_or_name.GetCompilerType().GetMangledTypeName();
2015-
if (a != b)
2016-
llvm::dbgs() << "RemoteAST and runtime diverge " << a << " != " << b
2017-
<< "\n";
2018-
} else {
2019-
if (log) {
2020-
log->Printf("could not get type metadata: %s\n",
2021-
instance_type.getFailure().render().c_str());
2007+
auto &remote_ast = GetRemoteASTContext(*swift_ast_ctx);
2008+
auto remote_ast_metadata_address = remote_ast.getHeapMetadataForObject(
2009+
swift::remote::RemoteAddress(instance_ptr));
2010+
if (remote_ast_metadata_address) {
2011+
auto instance_type = remote_ast.getTypeForRemoteTypeMetadata(
2012+
remote_ast_metadata_address.getValue(),
2013+
/*skipArtificial=*/true);
2014+
if (instance_type) {
2015+
auto ref_type = ToCompilerType(instance_type.getValue());
2016+
ConstString a = ref_type.GetMangledTypeName();
2017+
ConstString b =
2018+
class_type_or_name.GetCompilerType().GetMangledTypeName();
2019+
if (a != b)
2020+
llvm::dbgs() << "RemoteAST and runtime diverge " << a << " != " << b
2021+
<< "\n";
2022+
} else {
2023+
if (log) {
2024+
log->Printf("could not get type metadata: %s\n",
2025+
instance_type.getFailure().render().c_str());
2026+
}
20222027
}
20232028
}
20242029
}
@@ -2212,18 +2217,22 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Protocol(
22122217
address.SetRawAddress(out_address.getAddressData());
22132218

22142219
#ifndef NDEBUG
2215-
auto reference_pair = remote_ast_impl(use_local_buffer, existential_address);
2216-
assert(pair.hasValue() >= reference_pair.hasValue() &&
2217-
"RemoteAST and runtime diverge");
2218-
2219-
if (reference_pair) {
2220-
CompilerType ref_type = std::get<CompilerType>(*reference_pair);
2221-
Address ref_address = std::get<Address>(*reference_pair);
2222-
ConstString a = class_type_or_name.GetCompilerType().GetMangledTypeName();
2223-
ConstString b = ref_type.GetMangledTypeName();
2224-
if (a != b)
2225-
llvm::dbgs() << "RemoteAST and runtime diverge " << a << " != " << b
2226-
<< "\n";
2220+
if (ModuleList::GetGlobalModuleListProperties()
2221+
.GetSwiftValidateTypeSystem()) {
2222+
auto reference_pair =
2223+
remote_ast_impl(use_local_buffer, existential_address);
2224+
assert(pair.hasValue() >= reference_pair.hasValue() &&
2225+
"RemoteAST and runtime diverge");
2226+
2227+
if (reference_pair) {
2228+
CompilerType ref_type = std::get<CompilerType>(*reference_pair);
2229+
Address ref_address = std::get<Address>(*reference_pair);
2230+
ConstString a = class_type_or_name.GetCompilerType().GetMangledTypeName();
2231+
ConstString b = ref_type.GetMangledTypeName();
2232+
if (a != b)
2233+
llvm::dbgs() << "RemoteAST and runtime diverge " << a << " != " << b
2234+
<< "\n";
2235+
}
22272236
}
22282237
#endif
22292238
return true;

0 commit comments

Comments
 (0)