Skip to content

Commit 6030975

Browse files
committed
[lldb] Make swift::ASTContext accesses thread safe
swift::ASTContext is not thread safe by default, wrap SwiftASTContext's swift::ASTContext in a LockGuarded. rdar://121409294
1 parent 48a92fa commit 6030975

File tree

7 files changed

+95
-69
lines changed

7 files changed

+95
-69
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ static void ResolveSpecialNames(
792792

793793
/// Initialize the SwiftASTContext and return the wrapped
794794
/// swift::ASTContext when successful.
795-
static swift::ASTContext *
795+
static ThreadSafeASTContext
796796
SetupASTContext(SwiftASTContextForExpressions &swift_ast_context,
797797
DiagnosticManager &diagnostic_manager,
798798
std::function<bool()> disable_objc_runtime, bool repl,
@@ -806,27 +806,27 @@ SetupASTContext(SwiftASTContextForExpressions &swift_ast_context,
806806
diagnostic_manager.PutString(eSeverityInfo,
807807
"Couldn't initialize Swift expression "
808808
"evaluator due to previous errors.");
809-
return nullptr;
809+
return ThreadSafeASTContext();
810810
}
811811

812812
if (swift_ast_context.HasFatalErrors()) {
813813
diagnostic_manager.PutString(eSeverityError,
814814
"The AST context is in a fatal error state.");
815-
return nullptr;
815+
return ThreadSafeASTContext();
816816
}
817817

818-
swift::ASTContext *ast_context = swift_ast_context.GetASTContext();
818+
ThreadSafeASTContext ast_context = swift_ast_context.GetASTContext();
819819
if (!ast_context) {
820820
diagnostic_manager.PutString(
821821
eSeverityError,
822822
"Couldn't initialize the AST context. Please check your settings.");
823-
return nullptr;
823+
return ThreadSafeASTContext();
824824
}
825825

826826
if (swift_ast_context.HasFatalErrors()) {
827827
diagnostic_manager.PutString(eSeverityError,
828828
"The AST context is in a fatal error state.");
829-
return nullptr;
829+
return ThreadSafeASTContext();
830830
}
831831

832832
// TODO: Find a way to get contraint-solver output sent to a stream
@@ -1126,7 +1126,7 @@ char ModuleImportError::ID = 0;
11261126
/// This holds the result of ParseAndImport.
11271127
struct ParsedExpression {
11281128
std::unique_ptr<SwiftASTManipulator> code_manipulator;
1129-
swift::ASTContext &ast_context;
1129+
ThreadSafeASTContext ast_context;
11301130
swift::ModuleDecl &module;
11311131
LLDBNameLookup &external_lookup;
11321132
swift::SourceFile &source_file;
@@ -1293,7 +1293,7 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
12931293
return !ObjCLanguageRuntime::Get(*process_sp);
12941294
};
12951295

1296-
swift::ASTContext *ast_context =
1296+
ThreadSafeASTContext ast_context =
12971297
SetupASTContext(swift_ast_context, diagnostic_manager,
12981298
should_disable_objc_runtime, repl, playground);
12991299
if (!ast_context)
@@ -1352,7 +1352,7 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
13521352
importInfo.AdditionalImports.emplace_back(attributed_import);
13531353

13541354
auto module_id = ast_context->getIdentifier(expr_name_buf);
1355-
auto &module = *swift::ModuleDecl::create(module_id, *ast_context,
1355+
auto &module = *swift::ModuleDecl::create(module_id, **ast_context,
13561356
importInfo);
13571357

13581358
swift::SourceFileKind source_file_kind = swift::SourceFileKind::Library;
@@ -1362,7 +1362,7 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
13621362

13631363
// Create the source file. Note, we disable delayed parsing for the
13641364
// swift expression parser.
1365-
swift::SourceFile *source_file = new (*ast_context)
1365+
swift::SourceFile *source_file = new (**ast_context)
13661366
swift::SourceFile(module, source_file_kind, buffer_id,
13671367
swift::SourceFile::ParsingFlags::DisableDelayedBodies);
13681368
module.addFile(*source_file);
@@ -1520,8 +1520,13 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
15201520
swift::verify(*source_file);
15211521

15221522
ParsedExpression result = {
1523-
std::move(code_manipulator), *ast_context, module, *external_lookup,
1524-
*source_file, std::move(main_filename), /*buffer_id*/0,
1523+
std::move(code_manipulator),
1524+
std::move(ast_context),
1525+
module,
1526+
*external_lookup,
1527+
*source_file,
1528+
std::move(main_filename),
1529+
/*buffer_id*/ 0,
15251530
};
15261531
return std::move(result);
15271532
}
@@ -2140,7 +2145,7 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
21402145
// part of the parse from the staging area in the external lookup
21412146
// object into the SwiftPersistentExpressionState.
21422147
swift::ModuleDecl *module = &parsed_expr->module;
2143-
parsed_expr->ast_context.addLoadedModule(module);
2148+
parsed_expr->ast_context->addLoadedModule(module);
21442149
m_swift_ast_ctx.CacheModule(module);
21452150
if (m_sc.target_sp) {
21462151
auto *persistent_state =

lldb/source/Plugins/ExpressionParser/Swift/SwiftREPL.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ void SwiftREPL::CompleteCode(const std::string &current_code,
574574
SwiftASTContextForExpressions *swift_ast = m_swift_ast;
575575

576576
if (swift_ast) {
577-
swift::ASTContext *ast = swift_ast->GetASTContext();
577+
ThreadSafeASTContext ast = swift_ast->GetASTContext();
578578
swift::REPLCompletions completions;
579579
SourceModule completion_module_info;
580580
completion_module_info.path.push_back(ConstString("repl"));
@@ -587,7 +587,7 @@ void SwiftREPL::CompleteCode(const std::string &current_code,
587587
repl_module = swift_ast->CreateModule(completion_module_info, error,
588588
importInfo);
589589
std::optional<unsigned> bufferID;
590-
swift::SourceFile *repl_source_file = new (*ast) swift::SourceFile(
590+
swift::SourceFile *repl_source_file = new (**ast) swift::SourceFile(
591591
*repl_module, swift::SourceFileKind::Main, bufferID);
592592
repl_module->addFile(*repl_source_file);
593593
swift::performImportResolution(*repl_source_file);

lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static llvm::Error AddVariableInfo(
378378
ss.flush();
379379
log->Printf("Adding injected self: type (%p) context(%p) is: %s",
380380
static_cast<void *>(swift_type.getPointer()),
381-
static_cast<void *>(ast_context.GetASTContext()), s.c_str());
381+
static_cast<void *>(*ast_context.GetASTContext()), s.c_str());
382382
}
383383
// A one-off clone of variable_sp with the type replaced by target_type.
384384
auto patched_variable_sp = std::make_shared<lldb_private::Variable>(

lldb/source/Plugins/InstrumentationRuntime/MainThreadChecker/InstrumentationRuntimeMainThreadChecker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ static std::string TranslateObjCNameToSwiftName(std::string className,
150150
}
151151
};
152152

153-
MyConsumer consumer(swift::ObjCSelector(*ctx->GetASTContext(), numArguments,
153+
ThreadSafeASTContext ast_ctx = ctx->GetASTContext();
154+
MyConsumer consumer(swift::ObjCSelector(**ast_ctx, numArguments,
154155
selectorIdentifiers));
155156
// FIXME(mracek): Switch to a new API that translates the Clang class name
156157
// to Swift class name, once this API exists. Now we assume they are the same.

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,18 @@ swift::remoteAST::RemoteASTContext &
5959
SwiftLanguageRuntimeImpl::GetRemoteASTContext(SwiftASTContext &swift_ast_ctx) {
6060
// If we already have a remote AST context for this AST context,
6161
// return it.
62-
auto known = m_remote_ast_contexts.find(swift_ast_ctx.GetASTContext());
62+
ThreadSafeASTContext ast_ctx = swift_ast_ctx.GetASTContext();
63+
auto known = m_remote_ast_contexts.find(*ast_ctx);
6364
if (known != m_remote_ast_contexts.end())
6465
return *known->second;
6566

6667
// Initialize a new remote AST context.
6768
(void)GetReflectionContext();
6869
auto remote_ast_up = std::make_unique<swift::remoteAST::RemoteASTContext>(
69-
*swift_ast_ctx.GetASTContext(), GetMemoryReader());
70+
**ast_ctx, GetMemoryReader());
7071
auto &remote_ast = *remote_ast_up;
7172
m_remote_ast_contexts.insert(
72-
{swift_ast_ctx.GetASTContext(), std::move(remote_ast_up)});
73+
{*ast_ctx, std::move(remote_ast_up)});
7374
return remote_ast;
7475
}
7576

@@ -521,7 +522,8 @@ SwiftLanguageRuntimeImpl::GetMetadataPromise(const SymbolContext *sc,
521522
if (addr == 0 || addr == LLDB_INVALID_ADDRESS)
522523
return nullptr;
523524

524-
auto key = std::make_pair(swift_ast_ctx->GetASTContext(), addr);
525+
ThreadSafeASTContext ast_ctx = swift_ast_ctx->GetASTContext();
526+
auto key = std::make_pair(*ast_ctx, addr);
525527
auto iter = m_promises_map.find(key);
526528
if (iter != m_promises_map.end())
527529
return iter->second;

0 commit comments

Comments
 (0)