Skip to content

Commit 0de70b5

Browse files
committed
[lldb] Emit a warning when UB is detected in an expression IR
In rare cases missing type information results in IRGen lowering values into SILUndef. This may be for a variable that isn't even used in the expression, so we just warn about it. This is reported as an out-of-band warning, because LLDB suppresses warnings on successful expressions. rdar://147879706 (cherry picked from commit 98e6dc9)
1 parent cacc2c9 commit 0de70b5

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "SwiftUserExpression.h"
2525

2626
#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
27+
#include "lldb/Core/Debugger.h"
2728
#include "lldb/Core/Module.h"
2829
#include "lldb/Core/ModuleList.h"
2930
#include "lldb/Core/ModuleSpec.h"
@@ -50,6 +51,7 @@
5051
#include "llvm/ADT/StringRef.h"
5152
#include "llvm/BinaryFormat/Dwarf.h"
5253
#include "llvm/IR/IRBuilder.h"
54+
#include "llvm/IR/IntrinsicInst.h"
5355
#include "llvm/IR/LLVMContext.h"
5456
#include "llvm/IR/Module.h"
5557
#include "llvm/IR/Verifier.h"
@@ -1697,6 +1699,17 @@ RedirectCallFromSinkToTrampolineFunction(llvm::Module &module,
16971699
return true;
16981700
}
16991701

1702+
static bool ContainsUB(llvm::Module &module) {
1703+
for (llvm::Function &function : module)
1704+
for (llvm::BasicBlock &bb : function)
1705+
for (llvm::Instruction &i : bb)
1706+
if (auto *memcpy = llvm::dyn_cast<llvm::MemCpyInst>(&i))
1707+
if (llvm::isa<llvm::UndefValue>(memcpy->getOperand(0)) ||
1708+
llvm::isa<llvm::UndefValue>(memcpy->getOperand(1)))
1709+
return true;
1710+
return false;
1711+
}
1712+
17001713
/// Add additional context that is otherwise hard to convey.
17011714
static void AnnotateDiagnostics(DiagnosticManager &diagnostic_manager) {
17021715
bool append_missing_library_note = false;
@@ -2086,6 +2099,17 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
20862099
m_module.reset(ContextAndModule.second);
20872100
}
20882101

2102+
// In rare cases missing type information results in IRGen lowering
2103+
// values into SILUndef. This may be for a variable that isn't even
2104+
// used in the expression, so just warn about it. This is reported
2105+
// as an out-of-band warning, because LLDB suppresses warnings on
2106+
// successful expressions.
2107+
if (m_sc.target_sp && m_module && ContainsUB(*m_module))
2108+
Debugger::ReportWarning(
2109+
"Expression contains undefined behavior. Expression results may be "
2110+
"incorrect. This may be due to incomplete Swift type information.",
2111+
m_sc.target_sp->GetDebugger().GetID());
2112+
20892113
// If IRGen failed without errors, the root cause may be a fatal
20902114
// Clang diagnostic.
20912115
using ErrorKind = SwiftASTContext::ScopedDiagnostics::ErrorKind;

0 commit comments

Comments
 (0)