Skip to content

Commit d2ed2d2

Browse files
Merge pull request #10350 from adrian-prantl/147879706
[lldb] Emit a warning when UB is detected in an expression IR
2 parents 98f84e5 + 98e6dc9 commit d2ed2d2

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"
@@ -1700,6 +1702,17 @@ RedirectCallFromSinkToTrampolineFunction(llvm::Module &module,
17001702
return true;
17011703
}
17021704

1705+
static bool ContainsUB(llvm::Module &module) {
1706+
for (llvm::Function &function : module)
1707+
for (llvm::BasicBlock &bb : function)
1708+
for (llvm::Instruction &i : bb)
1709+
if (auto *memcpy = llvm::dyn_cast<llvm::MemCpyInst>(&i))
1710+
if (llvm::isa<llvm::UndefValue>(memcpy->getOperand(0)) ||
1711+
llvm::isa<llvm::UndefValue>(memcpy->getOperand(1)))
1712+
return true;
1713+
return false;
1714+
}
1715+
17031716
/// Add additional context that is otherwise hard to convey.
17041717
static void AnnotateDiagnostics(DiagnosticManager &diagnostic_manager) {
17051718
bool append_missing_library_note = false;
@@ -2089,6 +2102,17 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
20892102
m_module.reset(ContextAndModule.second);
20902103
}
20912104

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

0 commit comments

Comments
 (0)