Skip to content

[lldb] Emit a warning when UB is detected in an expression IR #10350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "SwiftUserExpression.h"

#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleList.h"
#include "lldb/Core/ModuleSpec.h"
Expand All @@ -50,6 +51,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
Expand Down Expand Up @@ -1700,6 +1702,17 @@ RedirectCallFromSinkToTrampolineFunction(llvm::Module &module,
return true;
}

static bool ContainsUB(llvm::Module &module) {
for (llvm::Function &function : module)
for (llvm::BasicBlock &bb : function)
for (llvm::Instruction &i : bb)
if (auto *memcpy = llvm::dyn_cast<llvm::MemCpyInst>(&i))
if (llvm::isa<llvm::UndefValue>(memcpy->getOperand(0)) ||
llvm::isa<llvm::UndefValue>(memcpy->getOperand(1)))
return true;
return false;
}

/// Add additional context that is otherwise hard to convey.
static void AnnotateDiagnostics(DiagnosticManager &diagnostic_manager) {
bool append_missing_library_note = false;
Expand Down Expand Up @@ -2089,6 +2102,17 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
m_module.reset(ContextAndModule.second);
}

// 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 just warn about it. This is reported
// as an out-of-band warning, because LLDB suppresses warnings on
// successful expressions.
if (m_sc.target_sp && m_module && ContainsUB(*m_module))
Debugger::ReportWarning(
"Expression contains undefined behavior. Expression results may be "
"incorrect. This may be due to incomplete Swift type information.",
m_sc.target_sp->GetDebugger().GetID());

// If IRGen failed without errors, the root cause may be a fatal
// Clang diagnostic.
using ErrorKind = SwiftASTContext::ScopedDiagnostics::ErrorKind;
Expand Down