Skip to content

[lldb][Expression] Remove IR pointer checker #144483

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 2 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
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
107 changes: 4 additions & 103 deletions lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,16 @@ using namespace lldb_private;

static char ID;

#define VALID_POINTER_CHECK_NAME "_$__lldb_valid_pointer_check"
#define VALID_OBJC_OBJECT_CHECK_NAME "$__lldb_objc_object_check"

static const char g_valid_pointer_check_text[] =
"extern \"C\" void\n"
"_$__lldb_valid_pointer_check (unsigned char *$__lldb_arg_ptr)\n"
"{\n"
" unsigned char $__lldb_local_val = *$__lldb_arg_ptr;\n"
"}";

ClangDynamicCheckerFunctions::ClangDynamicCheckerFunctions()
: DynamicCheckerFunctions(DCF_Clang) {}

ClangDynamicCheckerFunctions::~ClangDynamicCheckerFunctions() = default;

llvm::Error ClangDynamicCheckerFunctions::Install(
DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx) {
Expected<std::unique_ptr<UtilityFunction>> utility_fn =
exe_ctx.GetTargetRef().CreateUtilityFunction(
g_valid_pointer_check_text, VALID_POINTER_CHECK_NAME,
lldb::eLanguageTypeC, exe_ctx);
if (!utility_fn)
return utility_fn.takeError();
m_valid_pointer_check = std::move(*utility_fn);

llvm::Error
ClangDynamicCheckerFunctions::Install(DiagnosticManager &diagnostic_manager,
ExecutionContext &exe_ctx) {
if (Process *process = exe_ctx.GetProcessPtr()) {
ObjCLanguageRuntime *objc_language_runtime =
ObjCLanguageRuntime::Get(*process);
Expand All @@ -78,11 +63,7 @@ bool ClangDynamicCheckerFunctions::DoCheckersExplainStop(lldb::addr_t addr,
// FIXME: We have to get the checkers to know why they scotched the call in
// more detail,
// so we can print a better message here.
if (m_valid_pointer_check && m_valid_pointer_check->ContainsAddress(addr)) {
message.Printf("Attempted to dereference an invalid pointer.");
return true;
} else if (m_objc_object_check &&
m_objc_object_check->ContainsAddress(addr)) {
if (m_objc_object_check && m_objc_object_check->ContainsAddress(addr)) {
message.Printf("Attempted to dereference an invalid ObjC Object or send it "
"an unrecognized selector");
return true;
Expand Down Expand Up @@ -223,29 +204,6 @@ class Instrumenter {
return true;
}

/// Build a function pointer for a function with signature void
/// (*)(uint8_t*) with a given address
///
/// \param[in] start_address
/// The address of the function.
///
/// \return
/// The function pointer, for use in a CallInst.
llvm::FunctionCallee BuildPointerValidatorFunc(lldb::addr_t start_address) {
llvm::Type *param_array[1];

param_array[0] = const_cast<llvm::PointerType *>(GetI8PtrTy());

ArrayRef<llvm::Type *> params(param_array, 1);

FunctionType *fun_ty = FunctionType::get(
llvm::Type::getVoidTy(m_module.getContext()), params, true);
PointerType *fun_ptr_ty = PointerType::getUnqual(m_module.getContext());
Constant *fun_addr_int =
ConstantInt::get(GetIntptrTy(), start_address, false);
return {fun_ty, ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty)};
}

/// Build a function pointer for a function with signature void
/// (*)(uint8_t*, uint8_t*) with a given address
///
Expand Down Expand Up @@ -300,53 +258,6 @@ class Instrumenter {
IntegerType *m_intptr_ty = nullptr;
};

class ValidPointerChecker : public Instrumenter {
public:
ValidPointerChecker(llvm::Module &module,
std::shared_ptr<UtilityFunction> checker_function)
: Instrumenter(module, checker_function),
m_valid_pointer_check_func(nullptr) {}

~ValidPointerChecker() override = default;

protected:
bool InstrumentInstruction(llvm::Instruction *inst) override {
Log *log = GetLog(LLDBLog::Expressions);

LLDB_LOGF(log, "Instrumenting load/store instruction: %s\n",
PrintValue(inst).c_str());

if (!m_valid_pointer_check_func)
m_valid_pointer_check_func =
BuildPointerValidatorFunc(m_checker_function->StartAddress());

llvm::Value *dereferenced_ptr = nullptr;

if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst>(inst))
dereferenced_ptr = li->getPointerOperand();
else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst>(inst))
dereferenced_ptr = si->getPointerOperand();
else
return false;

// Insert an instruction to call the helper with the result
CallInst::Create(m_valid_pointer_check_func, dereferenced_ptr, "",
inst->getIterator());

return true;
}

bool InspectInstruction(llvm::Instruction &i) override {
if (isa<llvm::LoadInst>(&i) || isa<llvm::StoreInst>(&i))
RegisterInstruction(i);

return true;
}

private:
llvm::FunctionCallee m_valid_pointer_check_func;
};

class ObjcObjectChecker : public Instrumenter {
public:
ObjcObjectChecker(llvm::Module &module,
Expand Down Expand Up @@ -527,16 +438,6 @@ bool IRDynamicChecks::runOnModule(llvm::Module &M) {
return false;
}

if (m_checker_functions.m_valid_pointer_check) {
ValidPointerChecker vpc(M, m_checker_functions.m_valid_pointer_check);

if (!vpc.Inspect(*function))
return false;

if (!vpc.Instrument())
return false;
}

if (m_checker_functions.m_objc_object_check) {
ObjcObjectChecker ooc(M, m_checker_functions.m_objc_object_check);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class ClangDynamicCheckerFunctions

bool DoCheckersExplainStop(lldb::addr_t addr, Stream &message) override;

std::shared_ptr<UtilityFunction> m_valid_pointer_check;
std::shared_ptr<UtilityFunction> m_objc_object_check;
};

Expand Down
6 changes: 0 additions & 6 deletions lldb/test/API/tools/lldb-dap/save-core/TestDAP_save_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ def test_save_core(self):
# Getting dap stack trace may trigger __lldb_caller_function JIT module to be created.
self.get_stackFrames(startFrame=0)

# Evaluating an expression that cause "_$__lldb_valid_pointer_check" JIT module to be created.
expression = 'printf("this is a test")'
self.dap_server.request_evaluate(expression, context="watch")

# Verify "_$__lldb_valid_pointer_check" JIT module is created.
modules = self.dap_server.get_modules()
self.assertTrue(modules["_$__lldb_valid_pointer_check"])
thread_count = len(self.dap_server.get_threads())

core_stack = self.getBuildArtifact("core.stack.dmp")
Expand Down
Loading