1818
1919#include " lldb/Expression/UtilityFunction.h"
2020#include " lldb/Target/ExecutionContext.h"
21+ #include " lldb/Target/Language.h"
2122#include " lldb/Target/Process.h"
2223#include " lldb/Target/StackFrame.h"
2324#include " lldb/Target/Target.h"
@@ -32,36 +33,27 @@ using namespace lldb_private;
3233
3334static char ID;
3435
35- #define VALID_POINTER_CHECK_NAME " _$__lldb_valid_pointer_check"
3636#define VALID_OBJC_OBJECT_CHECK_NAME " $__lldb_objc_object_check"
3737
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-
4538ClangDynamicCheckerFunctions::ClangDynamicCheckerFunctions ()
4639 : DynamicCheckerFunctions(DCF_Clang) {}
4740
4841ClangDynamicCheckerFunctions::~ClangDynamicCheckerFunctions () = default ;
4942
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-
43+ llvm::Error
44+ ClangDynamicCheckerFunctions::Install (DiagnosticManager &diagnostic_manager,
45+ ExecutionContext &exe_ctx) {
6046 if (Process *process = exe_ctx.GetProcessPtr ()) {
6147 ObjCLanguageRuntime *objc_language_runtime =
6248 ObjCLanguageRuntime::Get (*process);
6349
64- if (objc_language_runtime) {
50+ SourceLanguage lang = process->GetTarget ().GetLanguage ();
51+ if (!lang)
52+ if (auto *frame = exe_ctx.GetFramePtr ())
53+ lang = frame->GetLanguage ();
54+
55+ if (objc_language_runtime &&
56+ Language::LanguageIsObjC (lang.AsLanguageType ())) {
6557 Expected<std::unique_ptr<UtilityFunction>> checker_fn =
6658 objc_language_runtime->CreateObjectChecker (VALID_OBJC_OBJECT_CHECK_NAME, exe_ctx);
6759 if (!checker_fn)
@@ -78,11 +70,7 @@ bool ClangDynamicCheckerFunctions::DoCheckersExplainStop(lldb::addr_t addr,
7870 // FIXME: We have to get the checkers to know why they scotched the call in
7971 // more detail,
8072 // 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)) {
73+ if (m_objc_object_check && m_objc_object_check->ContainsAddress (addr)) {
8674 message.Printf (" Attempted to dereference an invalid ObjC Object or send it "
8775 " an unrecognized selector" );
8876 return true ;
@@ -224,29 +212,6 @@ class Instrumenter {
224212 return true ;
225213 }
226214
227- // / Build a function pointer for a function with signature void
228- // / (*)(uint8_t*) with a given address
229- // /
230- // / \param[in] start_address
231- // / The address of the function.
232- // /
233- // / \return
234- // / The function pointer, for use in a CallInst.
235- llvm::FunctionCallee BuildPointerValidatorFunc (lldb::addr_t start_address) {
236- llvm::Type *param_array[1 ];
237-
238- param_array[0 ] = const_cast <llvm::PointerType *>(GetI8PtrTy ());
239-
240- ArrayRef<llvm::Type *> params (param_array, 1 );
241-
242- FunctionType *fun_ty = FunctionType::get (
243- llvm::Type::getVoidTy (m_module.getContext ()), params, true );
244- PointerType *fun_ptr_ty = PointerType::getUnqual (fun_ty);
245- Constant *fun_addr_int =
246- ConstantInt::get (GetIntptrTy (), start_address, false );
247- return {fun_ty, ConstantExpr::getIntToPtr (fun_addr_int, fun_ptr_ty)};
248- }
249-
250215 // / Build a function pointer for a function with signature void
251216 // / (*)(uint8_t*, uint8_t*) with a given address
252217 // /
@@ -301,53 +266,6 @@ class Instrumenter {
301266 IntegerType *m_intptr_ty = nullptr ;
302267};
303268
304- class ValidPointerChecker : public Instrumenter {
305- public:
306- ValidPointerChecker (llvm::Module &module ,
307- std::shared_ptr<UtilityFunction> checker_function)
308- : Instrumenter(module , checker_function),
309- m_valid_pointer_check_func (nullptr ) {}
310-
311- ~ValidPointerChecker () override = default ;
312-
313- protected:
314- bool InstrumentInstruction (llvm::Instruction *inst) override {
315- Log *log = GetLog (LLDBLog::Expressions);
316-
317- LLDB_LOGF (log, " Instrumenting load/store instruction: %s\n " ,
318- PrintValue (inst).c_str ());
319-
320- if (!m_valid_pointer_check_func)
321- m_valid_pointer_check_func =
322- BuildPointerValidatorFunc (m_checker_function->StartAddress ());
323-
324- llvm::Value *dereferenced_ptr = nullptr ;
325-
326- if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst>(inst))
327- dereferenced_ptr = li->getPointerOperand ();
328- else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst>(inst))
329- dereferenced_ptr = si->getPointerOperand ();
330- else
331- return false ;
332-
333- // Insert an instruction to call the helper with the result
334- CallInst::Create (m_valid_pointer_check_func, dereferenced_ptr, " " ,
335- inst->getIterator ());
336-
337- return true ;
338- }
339-
340- bool InspectInstruction (llvm::Instruction &i) override {
341- if (isa<llvm::LoadInst>(&i) || isa<llvm::StoreInst>(&i))
342- RegisterInstruction (i);
343-
344- return true ;
345- }
346-
347- private:
348- llvm::FunctionCallee m_valid_pointer_check_func;
349- };
350-
351269class ObjcObjectChecker : public Instrumenter {
352270public:
353271 ObjcObjectChecker (llvm::Module &module ,
@@ -528,16 +446,6 @@ bool IRDynamicChecks::runOnModule(llvm::Module &M) {
528446 return false ;
529447 }
530448
531- if (m_checker_functions.m_valid_pointer_check ) {
532- ValidPointerChecker vpc (M, m_checker_functions.m_valid_pointer_check );
533-
534- if (!vpc.Inspect (*function))
535- return false ;
536-
537- if (!vpc.Instrument ())
538- return false ;
539- }
540-
541449 if (m_checker_functions.m_objc_object_check ) {
542450 ObjcObjectChecker ooc (M, m_checker_functions.m_objc_object_check );
543451
0 commit comments