Skip to content

Commit 0a7b0c8

Browse files
authored
[lldb][Expression] Remove IR pointer checker (#144483)
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 ~2% of the expression evaluation time (though we cache them for subsequent expressions). 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.
1 parent 35f6d91 commit 0a7b0c8

File tree

3 files changed

+4
-110
lines changed

3 files changed

+4
-110
lines changed

lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp

Lines changed: 4 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,16 @@ using namespace lldb_private;
3232

3333
static char ID;
3434

35-
#define VALID_POINTER_CHECK_NAME "_$__lldb_valid_pointer_check"
3635
#define VALID_OBJC_OBJECT_CHECK_NAME "$__lldb_objc_object_check"
3736

38-
static const char g_valid_pointer_check_text[] =
39-
"extern \"C\" void\n"
40-
"_$__lldb_valid_pointer_check (unsigned char *$__lldb_arg_ptr)\n"
41-
"{\n"
42-
" unsigned char $__lldb_local_val = *$__lldb_arg_ptr;\n"
43-
"}";
44-
4537
ClangDynamicCheckerFunctions::ClangDynamicCheckerFunctions()
4638
: DynamicCheckerFunctions(DCF_Clang) {}
4739

4840
ClangDynamicCheckerFunctions::~ClangDynamicCheckerFunctions() = default;
4941

50-
llvm::Error ClangDynamicCheckerFunctions::Install(
51-
DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx) {
52-
Expected<std::unique_ptr<UtilityFunction>> utility_fn =
53-
exe_ctx.GetTargetRef().CreateUtilityFunction(
54-
g_valid_pointer_check_text, VALID_POINTER_CHECK_NAME,
55-
lldb::eLanguageTypeC, exe_ctx);
56-
if (!utility_fn)
57-
return utility_fn.takeError();
58-
m_valid_pointer_check = std::move(*utility_fn);
59-
42+
llvm::Error
43+
ClangDynamicCheckerFunctions::Install(DiagnosticManager &diagnostic_manager,
44+
ExecutionContext &exe_ctx) {
6045
if (Process *process = exe_ctx.GetProcessPtr()) {
6146
ObjCLanguageRuntime *objc_language_runtime =
6247
ObjCLanguageRuntime::Get(*process);
@@ -78,11 +63,7 @@ bool ClangDynamicCheckerFunctions::DoCheckersExplainStop(lldb::addr_t addr,
7863
// FIXME: We have to get the checkers to know why they scotched the call in
7964
// more detail,
8065
// so we can print a better message here.
81-
if (m_valid_pointer_check && m_valid_pointer_check->ContainsAddress(addr)) {
82-
message.Printf("Attempted to dereference an invalid pointer.");
83-
return true;
84-
} else if (m_objc_object_check &&
85-
m_objc_object_check->ContainsAddress(addr)) {
66+
if (m_objc_object_check && m_objc_object_check->ContainsAddress(addr)) {
8667
message.Printf("Attempted to dereference an invalid ObjC Object or send it "
8768
"an unrecognized selector");
8869
return true;
@@ -223,29 +204,6 @@ class Instrumenter {
223204
return true;
224205
}
225206

226-
/// Build a function pointer for a function with signature void
227-
/// (*)(uint8_t*) with a given address
228-
///
229-
/// \param[in] start_address
230-
/// The address of the function.
231-
///
232-
/// \return
233-
/// The function pointer, for use in a CallInst.
234-
llvm::FunctionCallee BuildPointerValidatorFunc(lldb::addr_t start_address) {
235-
llvm::Type *param_array[1];
236-
237-
param_array[0] = const_cast<llvm::PointerType *>(GetI8PtrTy());
238-
239-
ArrayRef<llvm::Type *> params(param_array, 1);
240-
241-
FunctionType *fun_ty = FunctionType::get(
242-
llvm::Type::getVoidTy(m_module.getContext()), params, true);
243-
PointerType *fun_ptr_ty = PointerType::getUnqual(m_module.getContext());
244-
Constant *fun_addr_int =
245-
ConstantInt::get(GetIntptrTy(), start_address, false);
246-
return {fun_ty, ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty)};
247-
}
248-
249207
/// Build a function pointer for a function with signature void
250208
/// (*)(uint8_t*, uint8_t*) with a given address
251209
///
@@ -300,53 +258,6 @@ class Instrumenter {
300258
IntegerType *m_intptr_ty = nullptr;
301259
};
302260

303-
class ValidPointerChecker : public Instrumenter {
304-
public:
305-
ValidPointerChecker(llvm::Module &module,
306-
std::shared_ptr<UtilityFunction> checker_function)
307-
: Instrumenter(module, checker_function),
308-
m_valid_pointer_check_func(nullptr) {}
309-
310-
~ValidPointerChecker() override = default;
311-
312-
protected:
313-
bool InstrumentInstruction(llvm::Instruction *inst) override {
314-
Log *log = GetLog(LLDBLog::Expressions);
315-
316-
LLDB_LOGF(log, "Instrumenting load/store instruction: %s\n",
317-
PrintValue(inst).c_str());
318-
319-
if (!m_valid_pointer_check_func)
320-
m_valid_pointer_check_func =
321-
BuildPointerValidatorFunc(m_checker_function->StartAddress());
322-
323-
llvm::Value *dereferenced_ptr = nullptr;
324-
325-
if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst>(inst))
326-
dereferenced_ptr = li->getPointerOperand();
327-
else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst>(inst))
328-
dereferenced_ptr = si->getPointerOperand();
329-
else
330-
return false;
331-
332-
// Insert an instruction to call the helper with the result
333-
CallInst::Create(m_valid_pointer_check_func, dereferenced_ptr, "",
334-
inst->getIterator());
335-
336-
return true;
337-
}
338-
339-
bool InspectInstruction(llvm::Instruction &i) override {
340-
if (isa<llvm::LoadInst>(&i) || isa<llvm::StoreInst>(&i))
341-
RegisterInstruction(i);
342-
343-
return true;
344-
}
345-
346-
private:
347-
llvm::FunctionCallee m_valid_pointer_check_func;
348-
};
349-
350261
class ObjcObjectChecker : public Instrumenter {
351262
public:
352263
ObjcObjectChecker(llvm::Module &module,
@@ -527,16 +438,6 @@ bool IRDynamicChecks::runOnModule(llvm::Module &M) {
527438
return false;
528439
}
529440

530-
if (m_checker_functions.m_valid_pointer_check) {
531-
ValidPointerChecker vpc(M, m_checker_functions.m_valid_pointer_check);
532-
533-
if (!vpc.Inspect(*function))
534-
return false;
535-
536-
if (!vpc.Instrument())
537-
return false;
538-
}
539-
540441
if (m_checker_functions.m_objc_object_check) {
541442
ObjcObjectChecker ooc(M, m_checker_functions.m_objc_object_check);
542443

lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class ClangDynamicCheckerFunctions
5353

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

56-
std::shared_ptr<UtilityFunction> m_valid_pointer_check;
5756
std::shared_ptr<UtilityFunction> m_objc_object_check;
5857
};
5958

lldb/test/API/tools/lldb-dap/save-core/TestDAP_save_core.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,7 @@ def test_save_core(self):
3232
# Getting dap stack trace may trigger __lldb_caller_function JIT module to be created.
3333
self.get_stackFrames(startFrame=0)
3434

35-
# Evaluating an expression that cause "_$__lldb_valid_pointer_check" JIT module to be created.
36-
expression = 'printf("this is a test")'
37-
self.dap_server.request_evaluate(expression, context="watch")
38-
39-
# Verify "_$__lldb_valid_pointer_check" JIT module is created.
4035
modules = self.dap_server.get_modules()
41-
self.assertTrue(modules["_$__lldb_valid_pointer_check"])
4236
thread_count = len(self.dap_server.get_threads())
4337

4438
core_stack = self.getBuildArtifact("core.stack.dmp")

0 commit comments

Comments
 (0)