From 2396d0b73d608a0581497851eecd9fa9101895ab Mon Sep 17 00:00:00 2001 From: Augusto Noronha Date: Wed, 3 Dec 2025 16:48:34 -0800 Subject: [PATCH] [lldb] Call llvm::verifyModule during expression evaluation Since we can't turn on automatic module verification (due to it crashing the process if verification fails), call llvm::verifyModule every time after we create or mutate it. rdar://154876866 --- .../Swift/SwiftExpressionParser.cpp | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp b/lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp index cd42b0a3da8fd..70703da666c52 100644 --- a/lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp +++ b/lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp @@ -1759,6 +1759,19 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager, AnnotateDiagnostics(diagnostic_manager); }; + auto verify = [&](llvm::Module &module) { + std::string Error; + llvm::raw_string_ostream MsgsOS(Error); + if (llvm::verifyModule(module, &MsgsOS)) { + LLDB_LOG(log, "IRGeneration failed with error: {0}", Error); + diagnostic_manager.AddDiagnostic( + "The expression could not be compiled", + eSeverityError, eDiagnosticOriginLLDB); + return parse_result_failure; + } + return ParseResult::success; + }; + // In the case of playgrounds, we turn all rewriting functionality off. const bool repl = m_options.GetREPLEnabled(); const bool playground = m_options.GetPlaygroundTransformEnabled(); @@ -2088,11 +2101,16 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager, std::move(sil_module), "lldb_module", swift::PrimarySpecificPaths("", parsed_expr->main_filename), llvm::ArrayRef(), llvm::ArrayRef()); - if (GenModule) { + auto parse_result = verify(*GenModule.getModule()); + if (parse_result != ParseResult::success) + return parse_result; swift::performLLVMOptimizations( IRGenOpts, m_swift_ast_ctx.GetDiagnosticEngine(), nullptr, GenModule.getModule(), GenModule.getTargetMachine(), nullptr); + parse_result = verify(*GenModule.getModule()); + if (parse_result != ParseResult::success) + return parse_result; } auto ContextAndModule = std::move(GenModule).release(); m_llvm_context.reset(ContextAndModule.first); @@ -2177,12 +2195,9 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager, std::lock_guard global_context_locker( IRExecutionUnit::GetLLVMGlobalContextMutex()); - bool has_errors = LLVMVerifyModule((LLVMOpaqueModule *)m_module.get(), - LLVMReturnStatusAction, nullptr); - if (has_errors) { - diagnostic_manager.PutString(eSeverityInfo, "LLVM verification error"); - return parse_result_failure; - } + ParseResult parse_result = verify(*m_module.get()); + if (parse_result != ParseResult::success) + return parse_result; } if (expr_diagnostics->HasErrors()) {