From e11da312c0c6f61bfac5ae7484ebb357a6b8615f Mon Sep 17 00:00:00 2001 From: Michael Buch Date: Tue, 17 Jun 2025 09:52:30 +0100 Subject: [PATCH 1/2] [lldb][Expression] Remove IR pointer checker Currently when jitting expressions, LLDB scans the IR instructions of the `$__lldb_expr` and will insert a call to a utility function for each load/store instruction. The purpose of the utility funciton is to dereference the load/store operand. If that operand was an invalid pointer the utility function would trap and LLDB asks the IR checker whether it was responsible for the trap, in which case it prints out an error message saying the expression dereferenced an invalid pointer. This is a lot of setup for not much gain. In fact, creating/running this utility expression shows up as ~TBD% of the expression evaluation time. And the error message we get out of it is arguably less useful than if we hadn't instrumented the IR. It was also untested. Before: ``` (lldb) expr int a = *returns_invalid_ptr() error: Execution was interrupted, reason: Attempted to dereference an invalid pointer.. The process has been returned to the state before expression evaluation. ``` After: ``` (lldb) expr int a = *returns_invalid_ptr() error: Expression execution was interrupted: EXC_BAD_ACCESS (code=1, address=0x5). The process has been returned to the state before expression evaluation. ``` This patch removes this IR checker. --- .../Clang/IRDynamicChecks.cpp | 107 +----------------- .../ExpressionParser/Clang/IRDynamicChecks.h | 1 - 2 files changed, 4 insertions(+), 104 deletions(-) diff --git a/lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp b/lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp index c7c292a8a7e42..6ef5d3f5be6d9 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp +++ b/lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp @@ -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> 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); @@ -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; @@ -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(GetI8PtrTy()); - - ArrayRef 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 /// @@ -300,53 +258,6 @@ class Instrumenter { IntegerType *m_intptr_ty = nullptr; }; -class ValidPointerChecker : public Instrumenter { -public: - ValidPointerChecker(llvm::Module &module, - std::shared_ptr 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(inst)) - dereferenced_ptr = li->getPointerOperand(); - else if (llvm::StoreInst *si = dyn_cast(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(&i) || isa(&i)) - RegisterInstruction(i); - - return true; - } - -private: - llvm::FunctionCallee m_valid_pointer_check_func; -}; - class ObjcObjectChecker : public Instrumenter { public: ObjcObjectChecker(llvm::Module &module, @@ -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); diff --git a/lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h b/lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h index ff20c1f08be0c..f67229afc2152 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h +++ b/lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h @@ -53,7 +53,6 @@ class ClangDynamicCheckerFunctions bool DoCheckersExplainStop(lldb::addr_t addr, Stream &message) override; - std::shared_ptr m_valid_pointer_check; std::shared_ptr m_objc_object_check; }; From 57c00f1fe8a37fb1584c316304709770532815fe Mon Sep 17 00:00:00 2001 From: Michael Buch Date: Tue, 17 Jun 2025 14:47:11 +0100 Subject: [PATCH 2/2] fixup! adapt TestDAP_save_core.py test --- lldb/test/API/tools/lldb-dap/save-core/TestDAP_save_core.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lldb/test/API/tools/lldb-dap/save-core/TestDAP_save_core.py b/lldb/test/API/tools/lldb-dap/save-core/TestDAP_save_core.py index 4045dd8fb6569..77c1e47914a39 100644 --- a/lldb/test/API/tools/lldb-dap/save-core/TestDAP_save_core.py +++ b/lldb/test/API/tools/lldb-dap/save-core/TestDAP_save_core.py @@ -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")