19f2f44ceSEd Maste //===-- IRDynamicChecks.cpp -------------------------------------*- C++ -*-===// 2ac7ddfbfSEd Maste // 3ac7ddfbfSEd Maste // The LLVM Compiler Infrastructure 4ac7ddfbfSEd Maste // 5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source 6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details. 7ac7ddfbfSEd Maste // 8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===// 9ac7ddfbfSEd Maste 109f2f44ceSEd Maste // C Includes 119f2f44ceSEd Maste // C++ Includes 129f2f44ceSEd Maste // Other libraries and framework includes 13ac7ddfbfSEd Maste #include "llvm/Support/raw_ostream.h" 14ac7ddfbfSEd Maste #include "llvm/IR/Constants.h" 1512b93ac6SEd Maste #include "llvm/IR/DataLayout.h" 16ac7ddfbfSEd Maste #include "llvm/IR/Function.h" 17ac7ddfbfSEd Maste #include "llvm/IR/Instructions.h" 18ac7ddfbfSEd Maste #include "llvm/IR/Module.h" 19ac7ddfbfSEd Maste #include "llvm/IR/Value.h" 20ac7ddfbfSEd Maste 219f2f44ceSEd Maste // Project includes 229f2f44ceSEd Maste #include "lldb/Expression/IRDynamicChecks.h" 239f2f44ceSEd Maste 249f2f44ceSEd Maste #include "lldb/Core/ConstString.h" 259f2f44ceSEd Maste #include "lldb/Core/Log.h" 269f2f44ceSEd Maste #include "lldb/Expression/UtilityFunction.h" 279f2f44ceSEd Maste #include "lldb/Target/ExecutionContext.h" 289f2f44ceSEd Maste #include "lldb/Target/ObjCLanguageRuntime.h" 299f2f44ceSEd Maste #include "lldb/Target/Process.h" 309f2f44ceSEd Maste #include "lldb/Target/StackFrame.h" 319f2f44ceSEd Maste #include "lldb/Target/Target.h" 329f2f44ceSEd Maste 33ac7ddfbfSEd Maste using namespace llvm; 34ac7ddfbfSEd Maste using namespace lldb_private; 35ac7ddfbfSEd Maste 36ac7ddfbfSEd Maste static char ID; 37ac7ddfbfSEd Maste 38ac7ddfbfSEd Maste #define VALID_POINTER_CHECK_NAME "$__lldb_valid_pointer_check" 39ac7ddfbfSEd Maste #define VALID_OBJC_OBJECT_CHECK_NAME "$__lldb_objc_object_check" 40ac7ddfbfSEd Maste 41ac7ddfbfSEd Maste static const char g_valid_pointer_check_text[] = 42ac7ddfbfSEd Maste "extern \"C\" void\n" 43ac7ddfbfSEd Maste "$__lldb_valid_pointer_check (unsigned char *$__lldb_arg_ptr)\n" 44ac7ddfbfSEd Maste "{\n" 45ac7ddfbfSEd Maste " unsigned char $__lldb_local_val = *$__lldb_arg_ptr;\n" 46ac7ddfbfSEd Maste "}"; 47ac7ddfbfSEd Maste 489f2f44ceSEd Maste DynamicCheckerFunctions::DynamicCheckerFunctions() = default; 49ac7ddfbfSEd Maste 509f2f44ceSEd Maste DynamicCheckerFunctions::~DynamicCheckerFunctions() = default; 51ac7ddfbfSEd Maste 52ac7ddfbfSEd Maste bool 53ac7ddfbfSEd Maste DynamicCheckerFunctions::Install(Stream &error_stream, 54ac7ddfbfSEd Maste ExecutionContext &exe_ctx) 55ac7ddfbfSEd Maste { 569f2f44ceSEd Maste Error error; 579f2f44ceSEd Maste m_valid_pointer_check.reset(exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(g_valid_pointer_check_text, 589f2f44ceSEd Maste lldb::eLanguageTypeC, 599f2f44ceSEd Maste VALID_POINTER_CHECK_NAME, 609f2f44ceSEd Maste error)); 619f2f44ceSEd Maste if (error.Fail()) 629f2f44ceSEd Maste return false; 639f2f44ceSEd Maste 64ac7ddfbfSEd Maste if (!m_valid_pointer_check->Install(error_stream, exe_ctx)) 65ac7ddfbfSEd Maste return false; 66ac7ddfbfSEd Maste 67ac7ddfbfSEd Maste Process *process = exe_ctx.GetProcessPtr(); 68ac7ddfbfSEd Maste 69ac7ddfbfSEd Maste if (process) 70ac7ddfbfSEd Maste { 71ac7ddfbfSEd Maste ObjCLanguageRuntime *objc_language_runtime = process->GetObjCLanguageRuntime(); 72ac7ddfbfSEd Maste 73ac7ddfbfSEd Maste if (objc_language_runtime) 74ac7ddfbfSEd Maste { 75ac7ddfbfSEd Maste m_objc_object_check.reset(objc_language_runtime->CreateObjectChecker(VALID_OBJC_OBJECT_CHECK_NAME)); 76ac7ddfbfSEd Maste 77ac7ddfbfSEd Maste if (!m_objc_object_check->Install(error_stream, exe_ctx)) 78ac7ddfbfSEd Maste return false; 79ac7ddfbfSEd Maste } 80ac7ddfbfSEd Maste } 81ac7ddfbfSEd Maste 82ac7ddfbfSEd Maste return true; 83ac7ddfbfSEd Maste } 84ac7ddfbfSEd Maste 85ac7ddfbfSEd Maste bool 86ac7ddfbfSEd Maste DynamicCheckerFunctions::DoCheckersExplainStop (lldb::addr_t addr, Stream &message) 87ac7ddfbfSEd Maste { 88ac7ddfbfSEd Maste // FIXME: We have to get the checkers to know why they scotched the call in more detail, 89ac7ddfbfSEd Maste // so we can print a better message here. 909f2f44ceSEd Maste if (m_valid_pointer_check && m_valid_pointer_check->ContainsAddress(addr)) 91ac7ddfbfSEd Maste { 92ac7ddfbfSEd Maste message.Printf ("Attempted to dereference an invalid pointer."); 93ac7ddfbfSEd Maste return true; 94ac7ddfbfSEd Maste } 959f2f44ceSEd Maste else if (m_objc_object_check && m_objc_object_check->ContainsAddress(addr)) 96ac7ddfbfSEd Maste { 97ac7ddfbfSEd Maste message.Printf ("Attempted to dereference an invalid ObjC Object or send it an unrecognized selector"); 98ac7ddfbfSEd Maste return true; 99ac7ddfbfSEd Maste } 100ac7ddfbfSEd Maste return false; 101ac7ddfbfSEd Maste } 102ac7ddfbfSEd Maste 103ac7ddfbfSEd Maste static std::string 104ac7ddfbfSEd Maste PrintValue(llvm::Value *V, bool truncate = false) 105ac7ddfbfSEd Maste { 106ac7ddfbfSEd Maste std::string s; 107ac7ddfbfSEd Maste raw_string_ostream rso(s); 108ac7ddfbfSEd Maste V->print(rso); 109ac7ddfbfSEd Maste rso.flush(); 110ac7ddfbfSEd Maste if (truncate) 111ac7ddfbfSEd Maste s.resize(s.length() - 1); 112ac7ddfbfSEd Maste return s; 113ac7ddfbfSEd Maste } 114ac7ddfbfSEd Maste 115ac7ddfbfSEd Maste //---------------------------------------------------------------------- 116ac7ddfbfSEd Maste /// @class Instrumenter IRDynamicChecks.cpp 117ac7ddfbfSEd Maste /// @brief Finds and instruments individual LLVM IR instructions 118ac7ddfbfSEd Maste /// 119ac7ddfbfSEd Maste /// When instrumenting LLVM IR, it is frequently desirable to first search 120ac7ddfbfSEd Maste /// for instructions, and then later modify them. This way iterators 121ac7ddfbfSEd Maste /// remain intact, and multiple passes can look at the same code base without 122ac7ddfbfSEd Maste /// treading on each other's toes. 123ac7ddfbfSEd Maste /// 124ac7ddfbfSEd Maste /// The Instrumenter class implements this functionality. A client first 125ac7ddfbfSEd Maste /// calls Inspect on a function, which populates a list of instructions to 126ac7ddfbfSEd Maste /// be instrumented. Then, later, when all passes' Inspect functions have 127ac7ddfbfSEd Maste /// been called, the client calls Instrument, which adds the desired 128ac7ddfbfSEd Maste /// instrumentation. 129ac7ddfbfSEd Maste /// 130ac7ddfbfSEd Maste /// A subclass of Instrumenter must override InstrumentInstruction, which 131ac7ddfbfSEd Maste /// is responsible for adding whatever instrumentation is necessary. 132ac7ddfbfSEd Maste /// 133ac7ddfbfSEd Maste /// A subclass of Instrumenter may override: 134ac7ddfbfSEd Maste /// 135ac7ddfbfSEd Maste /// - InspectInstruction [default: does nothing] 136ac7ddfbfSEd Maste /// 137ac7ddfbfSEd Maste /// - InspectBasicBlock [default: iterates through the instructions in a 138ac7ddfbfSEd Maste /// basic block calling InspectInstruction] 139ac7ddfbfSEd Maste /// 140ac7ddfbfSEd Maste /// - InspectFunction [default: iterates through the basic blocks in a 141ac7ddfbfSEd Maste /// function calling InspectBasicBlock] 142ac7ddfbfSEd Maste //---------------------------------------------------------------------- 143ac7ddfbfSEd Maste class Instrumenter { 144ac7ddfbfSEd Maste public: 145ac7ddfbfSEd Maste //------------------------------------------------------------------ 146ac7ddfbfSEd Maste /// Constructor 147ac7ddfbfSEd Maste /// 148ac7ddfbfSEd Maste /// @param[in] module 149ac7ddfbfSEd Maste /// The module being instrumented. 150ac7ddfbfSEd Maste //------------------------------------------------------------------ 151ac7ddfbfSEd Maste Instrumenter (llvm::Module &module, 152ac7ddfbfSEd Maste DynamicCheckerFunctions &checker_functions) : 153ac7ddfbfSEd Maste m_module(module), 154ac7ddfbfSEd Maste m_checker_functions(checker_functions), 1559f2f44ceSEd Maste m_i8ptr_ty(nullptr), 1569f2f44ceSEd Maste m_intptr_ty(nullptr) 157ac7ddfbfSEd Maste { 158ac7ddfbfSEd Maste } 159ac7ddfbfSEd Maste 1609f2f44ceSEd Maste virtual ~Instrumenter() = default; 161ac7ddfbfSEd Maste 162ac7ddfbfSEd Maste //------------------------------------------------------------------ 163ac7ddfbfSEd Maste /// Inspect a function to find instructions to instrument 164ac7ddfbfSEd Maste /// 165ac7ddfbfSEd Maste /// @param[in] function 166ac7ddfbfSEd Maste /// The function to inspect. 167ac7ddfbfSEd Maste /// 168ac7ddfbfSEd Maste /// @return 169ac7ddfbfSEd Maste /// True on success; false on error. 170ac7ddfbfSEd Maste //------------------------------------------------------------------ 171ac7ddfbfSEd Maste bool Inspect (llvm::Function &function) 172ac7ddfbfSEd Maste { 173ac7ddfbfSEd Maste return InspectFunction(function); 174ac7ddfbfSEd Maste } 175ac7ddfbfSEd Maste 176ac7ddfbfSEd Maste //------------------------------------------------------------------ 177ac7ddfbfSEd Maste /// Instrument all the instructions found by Inspect() 178ac7ddfbfSEd Maste /// 179ac7ddfbfSEd Maste /// @return 180ac7ddfbfSEd Maste /// True on success; false on error. 181ac7ddfbfSEd Maste //------------------------------------------------------------------ 182ac7ddfbfSEd Maste bool Instrument () 183ac7ddfbfSEd Maste { 184ac7ddfbfSEd Maste for (InstIterator ii = m_to_instrument.begin(), last_ii = m_to_instrument.end(); 185ac7ddfbfSEd Maste ii != last_ii; 186ac7ddfbfSEd Maste ++ii) 187ac7ddfbfSEd Maste { 188ac7ddfbfSEd Maste if (!InstrumentInstruction(*ii)) 189ac7ddfbfSEd Maste return false; 190ac7ddfbfSEd Maste } 191ac7ddfbfSEd Maste 192ac7ddfbfSEd Maste return true; 193ac7ddfbfSEd Maste } 1949f2f44ceSEd Maste 195ac7ddfbfSEd Maste protected: 196ac7ddfbfSEd Maste //------------------------------------------------------------------ 197ac7ddfbfSEd Maste /// Add instrumentation to a single instruction 198ac7ddfbfSEd Maste /// 199ac7ddfbfSEd Maste /// @param[in] inst 200ac7ddfbfSEd Maste /// The instruction to be instrumented. 201ac7ddfbfSEd Maste /// 202ac7ddfbfSEd Maste /// @return 203ac7ddfbfSEd Maste /// True on success; false otherwise. 204ac7ddfbfSEd Maste //------------------------------------------------------------------ 205ac7ddfbfSEd Maste virtual bool InstrumentInstruction(llvm::Instruction *inst) = 0; 206ac7ddfbfSEd Maste 207ac7ddfbfSEd Maste //------------------------------------------------------------------ 208ac7ddfbfSEd Maste /// Register a single instruction to be instrumented 209ac7ddfbfSEd Maste /// 210ac7ddfbfSEd Maste /// @param[in] inst 211ac7ddfbfSEd Maste /// The instruction to be instrumented. 212ac7ddfbfSEd Maste //------------------------------------------------------------------ 213ac7ddfbfSEd Maste void RegisterInstruction(llvm::Instruction &i) 214ac7ddfbfSEd Maste { 215ac7ddfbfSEd Maste m_to_instrument.push_back(&i); 216ac7ddfbfSEd Maste } 217ac7ddfbfSEd Maste 218ac7ddfbfSEd Maste //------------------------------------------------------------------ 219ac7ddfbfSEd Maste /// Determine whether a single instruction is interesting to 220ac7ddfbfSEd Maste /// instrument, and, if so, call RegisterInstruction 221ac7ddfbfSEd Maste /// 222ac7ddfbfSEd Maste /// @param[in] i 223ac7ddfbfSEd Maste /// The instruction to be inspected. 224ac7ddfbfSEd Maste /// 225ac7ddfbfSEd Maste /// @return 226ac7ddfbfSEd Maste /// False if there was an error scanning; true otherwise. 227ac7ddfbfSEd Maste //------------------------------------------------------------------ 228ac7ddfbfSEd Maste virtual bool InspectInstruction(llvm::Instruction &i) 229ac7ddfbfSEd Maste { 230ac7ddfbfSEd Maste return true; 231ac7ddfbfSEd Maste } 232ac7ddfbfSEd Maste 233ac7ddfbfSEd Maste //------------------------------------------------------------------ 234ac7ddfbfSEd Maste /// Scan a basic block to see if any instructions are interesting 235ac7ddfbfSEd Maste /// 236ac7ddfbfSEd Maste /// @param[in] bb 237ac7ddfbfSEd Maste /// The basic block to be inspected. 238ac7ddfbfSEd Maste /// 239ac7ddfbfSEd Maste /// @return 240ac7ddfbfSEd Maste /// False if there was an error scanning; true otherwise. 241ac7ddfbfSEd Maste //------------------------------------------------------------------ 242ac7ddfbfSEd Maste virtual bool InspectBasicBlock(llvm::BasicBlock &bb) 243ac7ddfbfSEd Maste { 244ac7ddfbfSEd Maste for (llvm::BasicBlock::iterator ii = bb.begin(), last_ii = bb.end(); 245ac7ddfbfSEd Maste ii != last_ii; 246ac7ddfbfSEd Maste ++ii) 247ac7ddfbfSEd Maste { 248ac7ddfbfSEd Maste if (!InspectInstruction(*ii)) 249ac7ddfbfSEd Maste return false; 250ac7ddfbfSEd Maste } 251ac7ddfbfSEd Maste 252ac7ddfbfSEd Maste return true; 253ac7ddfbfSEd Maste } 254ac7ddfbfSEd Maste 255ac7ddfbfSEd Maste //------------------------------------------------------------------ 256ac7ddfbfSEd Maste /// Scan a function to see if any instructions are interesting 257ac7ddfbfSEd Maste /// 258ac7ddfbfSEd Maste /// @param[in] f 259ac7ddfbfSEd Maste /// The function to be inspected. 260ac7ddfbfSEd Maste /// 261ac7ddfbfSEd Maste /// @return 262ac7ddfbfSEd Maste /// False if there was an error scanning; true otherwise. 263ac7ddfbfSEd Maste //------------------------------------------------------------------ 264ac7ddfbfSEd Maste virtual bool InspectFunction(llvm::Function &f) 265ac7ddfbfSEd Maste { 266ac7ddfbfSEd Maste for (llvm::Function::iterator bbi = f.begin(), last_bbi = f.end(); 267ac7ddfbfSEd Maste bbi != last_bbi; 268ac7ddfbfSEd Maste ++bbi) 269ac7ddfbfSEd Maste { 270ac7ddfbfSEd Maste if (!InspectBasicBlock(*bbi)) 271ac7ddfbfSEd Maste return false; 272ac7ddfbfSEd Maste } 273ac7ddfbfSEd Maste 274ac7ddfbfSEd Maste return true; 275ac7ddfbfSEd Maste } 276ac7ddfbfSEd Maste 277ac7ddfbfSEd Maste //------------------------------------------------------------------ 278ac7ddfbfSEd Maste /// Build a function pointer for a function with signature 279ac7ddfbfSEd Maste /// void (*)(uint8_t*) with a given address 280ac7ddfbfSEd Maste /// 281ac7ddfbfSEd Maste /// @param[in] start_address 282ac7ddfbfSEd Maste /// The address of the function. 283ac7ddfbfSEd Maste /// 284ac7ddfbfSEd Maste /// @return 285ac7ddfbfSEd Maste /// The function pointer, for use in a CallInst. 286ac7ddfbfSEd Maste //------------------------------------------------------------------ 287ac7ddfbfSEd Maste llvm::Value *BuildPointerValidatorFunc(lldb::addr_t start_address) 288ac7ddfbfSEd Maste { 289ac7ddfbfSEd Maste llvm::Type *param_array[1]; 290ac7ddfbfSEd Maste 291ac7ddfbfSEd Maste param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy()); 292ac7ddfbfSEd Maste 293ac7ddfbfSEd Maste ArrayRef<llvm::Type*> params(param_array, 1); 294ac7ddfbfSEd Maste 295ac7ddfbfSEd Maste FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true); 296ac7ddfbfSEd Maste PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty); 29712b93ac6SEd Maste Constant *fun_addr_int = ConstantInt::get(GetIntptrTy(), start_address, false); 298ac7ddfbfSEd Maste return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty); 299ac7ddfbfSEd Maste } 300ac7ddfbfSEd Maste 301ac7ddfbfSEd Maste //------------------------------------------------------------------ 302ac7ddfbfSEd Maste /// Build a function pointer for a function with signature 303ac7ddfbfSEd Maste /// void (*)(uint8_t*, uint8_t*) with a given address 304ac7ddfbfSEd Maste /// 305ac7ddfbfSEd Maste /// @param[in] start_address 306ac7ddfbfSEd Maste /// The address of the function. 307ac7ddfbfSEd Maste /// 308ac7ddfbfSEd Maste /// @return 309ac7ddfbfSEd Maste /// The function pointer, for use in a CallInst. 310ac7ddfbfSEd Maste //------------------------------------------------------------------ 311ac7ddfbfSEd Maste llvm::Value *BuildObjectCheckerFunc(lldb::addr_t start_address) 312ac7ddfbfSEd Maste { 313ac7ddfbfSEd Maste llvm::Type *param_array[2]; 314ac7ddfbfSEd Maste 315ac7ddfbfSEd Maste param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy()); 316ac7ddfbfSEd Maste param_array[1] = const_cast<llvm::PointerType*>(GetI8PtrTy()); 317ac7ddfbfSEd Maste 318ac7ddfbfSEd Maste ArrayRef<llvm::Type*> params(param_array, 2); 319ac7ddfbfSEd Maste 320ac7ddfbfSEd Maste FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true); 321ac7ddfbfSEd Maste PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty); 32212b93ac6SEd Maste Constant *fun_addr_int = ConstantInt::get(GetIntptrTy(), start_address, false); 323ac7ddfbfSEd Maste return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty); 324ac7ddfbfSEd Maste } 325ac7ddfbfSEd Maste 326ac7ddfbfSEd Maste PointerType *GetI8PtrTy() 327ac7ddfbfSEd Maste { 328ac7ddfbfSEd Maste if (!m_i8ptr_ty) 329ac7ddfbfSEd Maste m_i8ptr_ty = llvm::Type::getInt8PtrTy(m_module.getContext()); 330ac7ddfbfSEd Maste 331ac7ddfbfSEd Maste return m_i8ptr_ty; 332ac7ddfbfSEd Maste } 333ac7ddfbfSEd Maste 33412b93ac6SEd Maste IntegerType *GetIntptrTy() 33512b93ac6SEd Maste { 33612b93ac6SEd Maste if (!m_intptr_ty) 33712b93ac6SEd Maste { 33812b93ac6SEd Maste llvm::DataLayout data_layout(&m_module); 33912b93ac6SEd Maste 34012b93ac6SEd Maste m_intptr_ty = llvm::Type::getIntNTy(m_module.getContext(), data_layout.getPointerSizeInBits()); 34112b93ac6SEd Maste } 34212b93ac6SEd Maste 34312b93ac6SEd Maste return m_intptr_ty; 34412b93ac6SEd Maste } 34512b93ac6SEd Maste 346ac7ddfbfSEd Maste typedef std::vector <llvm::Instruction *> InstVector; 347ac7ddfbfSEd Maste typedef InstVector::iterator InstIterator; 348ac7ddfbfSEd Maste 349ac7ddfbfSEd Maste InstVector m_to_instrument; ///< List of instructions the inspector found 350ac7ddfbfSEd Maste llvm::Module &m_module; ///< The module which is being instrumented 351ac7ddfbfSEd Maste DynamicCheckerFunctions &m_checker_functions; ///< The dynamic checker functions for the process 3529f2f44ceSEd Maste 353ac7ddfbfSEd Maste private: 354ac7ddfbfSEd Maste PointerType *m_i8ptr_ty; 35512b93ac6SEd Maste IntegerType *m_intptr_ty; 356ac7ddfbfSEd Maste }; 357ac7ddfbfSEd Maste 358ac7ddfbfSEd Maste class ValidPointerChecker : public Instrumenter 359ac7ddfbfSEd Maste { 360ac7ddfbfSEd Maste public: 361ac7ddfbfSEd Maste ValidPointerChecker (llvm::Module &module, 362ac7ddfbfSEd Maste DynamicCheckerFunctions &checker_functions) : 363ac7ddfbfSEd Maste Instrumenter(module, checker_functions), 3649f2f44ceSEd Maste m_valid_pointer_check_func(nullptr) 365ac7ddfbfSEd Maste { 366ac7ddfbfSEd Maste } 367ac7ddfbfSEd Maste 3689f2f44ceSEd Maste ~ValidPointerChecker() override = default; 3699f2f44ceSEd Maste 3709f2f44ceSEd Maste protected: 3719f2f44ceSEd Maste bool InstrumentInstruction(llvm::Instruction *inst) override 372ac7ddfbfSEd Maste { 373ac7ddfbfSEd Maste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 374ac7ddfbfSEd Maste 375ac7ddfbfSEd Maste if (log) 376ac7ddfbfSEd Maste log->Printf("Instrumenting load/store instruction: %s\n", 377ac7ddfbfSEd Maste PrintValue(inst).c_str()); 378ac7ddfbfSEd Maste 379ac7ddfbfSEd Maste if (!m_valid_pointer_check_func) 380ac7ddfbfSEd Maste m_valid_pointer_check_func = BuildPointerValidatorFunc(m_checker_functions.m_valid_pointer_check->StartAddress()); 381ac7ddfbfSEd Maste 3829f2f44ceSEd Maste llvm::Value *dereferenced_ptr = nullptr; 383ac7ddfbfSEd Maste 384ac7ddfbfSEd Maste if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst> (inst)) 385ac7ddfbfSEd Maste dereferenced_ptr = li->getPointerOperand(); 386ac7ddfbfSEd Maste else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst> (inst)) 387ac7ddfbfSEd Maste dereferenced_ptr = si->getPointerOperand(); 388ac7ddfbfSEd Maste else 389ac7ddfbfSEd Maste return false; 390ac7ddfbfSEd Maste 391ac7ddfbfSEd Maste // Insert an instruction to cast the loaded value to int8_t* 392ac7ddfbfSEd Maste 393ac7ddfbfSEd Maste BitCastInst *bit_cast = new BitCastInst(dereferenced_ptr, 394ac7ddfbfSEd Maste GetI8PtrTy(), 395ac7ddfbfSEd Maste "", 396ac7ddfbfSEd Maste inst); 397ac7ddfbfSEd Maste 398ac7ddfbfSEd Maste // Insert an instruction to call the helper with the result 399ac7ddfbfSEd Maste 400ac7ddfbfSEd Maste llvm::Value *arg_array[1]; 401ac7ddfbfSEd Maste 402ac7ddfbfSEd Maste arg_array[0] = bit_cast; 403ac7ddfbfSEd Maste 404ac7ddfbfSEd Maste llvm::ArrayRef<llvm::Value *> args(arg_array, 1); 405ac7ddfbfSEd Maste 406ac7ddfbfSEd Maste CallInst::Create(m_valid_pointer_check_func, 407ac7ddfbfSEd Maste args, 408ac7ddfbfSEd Maste "", 409ac7ddfbfSEd Maste inst); 410ac7ddfbfSEd Maste 411ac7ddfbfSEd Maste return true; 412ac7ddfbfSEd Maste } 413ac7ddfbfSEd Maste 4149f2f44ceSEd Maste bool InspectInstruction(llvm::Instruction &i) override 415ac7ddfbfSEd Maste { 416ac7ddfbfSEd Maste if (dyn_cast<llvm::LoadInst> (&i) || 417ac7ddfbfSEd Maste dyn_cast<llvm::StoreInst> (&i)) 418ac7ddfbfSEd Maste RegisterInstruction(i); 419ac7ddfbfSEd Maste 420ac7ddfbfSEd Maste return true; 421ac7ddfbfSEd Maste } 422ac7ddfbfSEd Maste 4239f2f44ceSEd Maste private: 424ac7ddfbfSEd Maste llvm::Value *m_valid_pointer_check_func; 425ac7ddfbfSEd Maste }; 426ac7ddfbfSEd Maste 427ac7ddfbfSEd Maste class ObjcObjectChecker : public Instrumenter 428ac7ddfbfSEd Maste { 429ac7ddfbfSEd Maste public: 430ac7ddfbfSEd Maste ObjcObjectChecker(llvm::Module &module, 431ac7ddfbfSEd Maste DynamicCheckerFunctions &checker_functions) : 432ac7ddfbfSEd Maste Instrumenter(module, checker_functions), 4339f2f44ceSEd Maste m_objc_object_check_func(nullptr) 434ac7ddfbfSEd Maste { 435ac7ddfbfSEd Maste } 436ac7ddfbfSEd Maste 4379f2f44ceSEd Maste ~ObjcObjectChecker() override = default; 438ac7ddfbfSEd Maste 439ac7ddfbfSEd Maste enum msgSend_type 440ac7ddfbfSEd Maste { 441ac7ddfbfSEd Maste eMsgSend = 0, 442ac7ddfbfSEd Maste eMsgSendSuper, 443ac7ddfbfSEd Maste eMsgSendSuper_stret, 444ac7ddfbfSEd Maste eMsgSend_fpret, 445ac7ddfbfSEd Maste eMsgSend_stret 446ac7ddfbfSEd Maste }; 447ac7ddfbfSEd Maste 448ac7ddfbfSEd Maste std::map <llvm::Instruction *, msgSend_type> msgSend_types; 449ac7ddfbfSEd Maste 4509f2f44ceSEd Maste protected: 4519f2f44ceSEd Maste bool InstrumentInstruction(llvm::Instruction *inst) override 452ac7ddfbfSEd Maste { 453ac7ddfbfSEd Maste CallInst *call_inst = dyn_cast<CallInst>(inst); 454ac7ddfbfSEd Maste 455ac7ddfbfSEd Maste if (!call_inst) 4569f2f44ceSEd Maste return false; // call_inst really shouldn't be nullptr, because otherwise InspectInstruction wouldn't have registered it 457ac7ddfbfSEd Maste 458ac7ddfbfSEd Maste if (!m_objc_object_check_func) 459ac7ddfbfSEd Maste m_objc_object_check_func = BuildObjectCheckerFunc(m_checker_functions.m_objc_object_check->StartAddress()); 460ac7ddfbfSEd Maste 461ac7ddfbfSEd Maste // id objc_msgSend(id theReceiver, SEL theSelector, ...) 462ac7ddfbfSEd Maste 463ac7ddfbfSEd Maste llvm::Value *target_object; 464ac7ddfbfSEd Maste llvm::Value *selector; 465ac7ddfbfSEd Maste 466ac7ddfbfSEd Maste switch (msgSend_types[inst]) 467ac7ddfbfSEd Maste { 468ac7ddfbfSEd Maste case eMsgSend: 469ac7ddfbfSEd Maste case eMsgSend_fpret: 470ac7ddfbfSEd Maste target_object = call_inst->getArgOperand(0); 471ac7ddfbfSEd Maste selector = call_inst->getArgOperand(1); 472ac7ddfbfSEd Maste break; 473ac7ddfbfSEd Maste case eMsgSend_stret: 474ac7ddfbfSEd Maste target_object = call_inst->getArgOperand(1); 475ac7ddfbfSEd Maste selector = call_inst->getArgOperand(2); 476ac7ddfbfSEd Maste break; 477ac7ddfbfSEd Maste case eMsgSendSuper: 478ac7ddfbfSEd Maste case eMsgSendSuper_stret: 479ac7ddfbfSEd Maste return true; 480ac7ddfbfSEd Maste } 481ac7ddfbfSEd Maste 482ac7ddfbfSEd Maste // These objects should always be valid according to Sean Calannan 483ac7ddfbfSEd Maste assert (target_object); 484ac7ddfbfSEd Maste assert (selector); 485ac7ddfbfSEd Maste 486ac7ddfbfSEd Maste // Insert an instruction to cast the receiver id to int8_t* 487ac7ddfbfSEd Maste 488ac7ddfbfSEd Maste BitCastInst *bit_cast = new BitCastInst(target_object, 489ac7ddfbfSEd Maste GetI8PtrTy(), 490ac7ddfbfSEd Maste "", 491ac7ddfbfSEd Maste inst); 492ac7ddfbfSEd Maste 493ac7ddfbfSEd Maste // Insert an instruction to call the helper with the result 494ac7ddfbfSEd Maste 495ac7ddfbfSEd Maste llvm::Value *arg_array[2]; 496ac7ddfbfSEd Maste 497ac7ddfbfSEd Maste arg_array[0] = bit_cast; 498ac7ddfbfSEd Maste arg_array[1] = selector; 499ac7ddfbfSEd Maste 500ac7ddfbfSEd Maste ArrayRef<llvm::Value*> args(arg_array, 2); 501ac7ddfbfSEd Maste 502ac7ddfbfSEd Maste CallInst::Create(m_objc_object_check_func, 503ac7ddfbfSEd Maste args, 504ac7ddfbfSEd Maste "", 505ac7ddfbfSEd Maste inst); 506ac7ddfbfSEd Maste 507ac7ddfbfSEd Maste return true; 508ac7ddfbfSEd Maste } 509ac7ddfbfSEd Maste 5109f2f44ceSEd Maste bool InspectInstruction(llvm::Instruction &i) override 511ac7ddfbfSEd Maste { 512ac7ddfbfSEd Maste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 513ac7ddfbfSEd Maste 514ac7ddfbfSEd Maste CallInst *call_inst = dyn_cast<CallInst>(&i); 515ac7ddfbfSEd Maste 516ac7ddfbfSEd Maste if (call_inst) 517ac7ddfbfSEd Maste { 518ac7ddfbfSEd Maste // This metadata is set by IRForTarget::MaybeHandleCall(). 519ac7ddfbfSEd Maste 520ac7ddfbfSEd Maste MDNode *metadata = call_inst->getMetadata("lldb.call.realName"); 521ac7ddfbfSEd Maste 522ac7ddfbfSEd Maste if (!metadata) 523ac7ddfbfSEd Maste return true; 524ac7ddfbfSEd Maste 525ac7ddfbfSEd Maste if (metadata->getNumOperands() != 1) 526ac7ddfbfSEd Maste { 527ac7ddfbfSEd Maste if (log) 5280127ef0fSEd Maste log->Printf("Function call metadata has %d operands for [%p] %s", 5290127ef0fSEd Maste metadata->getNumOperands(), 5300127ef0fSEd Maste static_cast<void*>(call_inst), 5310127ef0fSEd Maste PrintValue(call_inst).c_str()); 532ac7ddfbfSEd Maste return false; 533ac7ddfbfSEd Maste } 534ac7ddfbfSEd Maste 535ac7ddfbfSEd Maste MDString *real_name = dyn_cast<MDString>(metadata->getOperand(0)); 536ac7ddfbfSEd Maste 537ac7ddfbfSEd Maste if (!real_name) 538ac7ddfbfSEd Maste { 539ac7ddfbfSEd Maste if (log) 5400127ef0fSEd Maste log->Printf("Function call metadata is not an MDString for [%p] %s", 5410127ef0fSEd Maste static_cast<void*>(call_inst), 5420127ef0fSEd Maste PrintValue(call_inst).c_str()); 543ac7ddfbfSEd Maste return false; 544ac7ddfbfSEd Maste } 545ac7ddfbfSEd Maste 546ac7ddfbfSEd Maste std::string name_str = real_name->getString(); 547ac7ddfbfSEd Maste const char* name_cstr = name_str.c_str(); 548ac7ddfbfSEd Maste 549ac7ddfbfSEd Maste if (log) 550ac7ddfbfSEd Maste log->Printf("Found call to %s: %s\n", name_cstr, PrintValue(call_inst).c_str()); 551ac7ddfbfSEd Maste 552ac7ddfbfSEd Maste if (name_str.find("objc_msgSend") == std::string::npos) 553ac7ddfbfSEd Maste return true; 554ac7ddfbfSEd Maste 555ac7ddfbfSEd Maste if (!strcmp(name_cstr, "objc_msgSend")) 556ac7ddfbfSEd Maste { 557ac7ddfbfSEd Maste RegisterInstruction(i); 558ac7ddfbfSEd Maste msgSend_types[&i] = eMsgSend; 559ac7ddfbfSEd Maste return true; 560ac7ddfbfSEd Maste } 561ac7ddfbfSEd Maste 562ac7ddfbfSEd Maste if (!strcmp(name_cstr, "objc_msgSend_stret")) 563ac7ddfbfSEd Maste { 564ac7ddfbfSEd Maste RegisterInstruction(i); 565ac7ddfbfSEd Maste msgSend_types[&i] = eMsgSend_stret; 566ac7ddfbfSEd Maste return true; 567ac7ddfbfSEd Maste } 568ac7ddfbfSEd Maste 569ac7ddfbfSEd Maste if (!strcmp(name_cstr, "objc_msgSend_fpret")) 570ac7ddfbfSEd Maste { 571ac7ddfbfSEd Maste RegisterInstruction(i); 572ac7ddfbfSEd Maste msgSend_types[&i] = eMsgSend_fpret; 573ac7ddfbfSEd Maste return true; 574ac7ddfbfSEd Maste } 575ac7ddfbfSEd Maste 576ac7ddfbfSEd Maste if (!strcmp(name_cstr, "objc_msgSendSuper")) 577ac7ddfbfSEd Maste { 578ac7ddfbfSEd Maste RegisterInstruction(i); 579ac7ddfbfSEd Maste msgSend_types[&i] = eMsgSendSuper; 580ac7ddfbfSEd Maste return true; 581ac7ddfbfSEd Maste } 582ac7ddfbfSEd Maste 583ac7ddfbfSEd Maste if (!strcmp(name_cstr, "objc_msgSendSuper_stret")) 584ac7ddfbfSEd Maste { 585ac7ddfbfSEd Maste RegisterInstruction(i); 586ac7ddfbfSEd Maste msgSend_types[&i] = eMsgSendSuper_stret; 587ac7ddfbfSEd Maste return true; 588ac7ddfbfSEd Maste } 589ac7ddfbfSEd Maste 590ac7ddfbfSEd Maste if (log) 591ac7ddfbfSEd Maste log->Printf("Function name '%s' contains 'objc_msgSend' but is not handled", name_str.c_str()); 592ac7ddfbfSEd Maste 593ac7ddfbfSEd Maste return true; 594ac7ddfbfSEd Maste } 595ac7ddfbfSEd Maste 596ac7ddfbfSEd Maste return true; 597ac7ddfbfSEd Maste } 598ac7ddfbfSEd Maste 5999f2f44ceSEd Maste private: 600ac7ddfbfSEd Maste llvm::Value *m_objc_object_check_func; 601ac7ddfbfSEd Maste }; 602ac7ddfbfSEd Maste 603ac7ddfbfSEd Maste IRDynamicChecks::IRDynamicChecks(DynamicCheckerFunctions &checker_functions, 604ac7ddfbfSEd Maste const char *func_name) : 605ac7ddfbfSEd Maste ModulePass(ID), 606ac7ddfbfSEd Maste m_func_name(func_name), 607ac7ddfbfSEd Maste m_checker_functions(checker_functions) 608ac7ddfbfSEd Maste { 609ac7ddfbfSEd Maste } 610ac7ddfbfSEd Maste 6119f2f44ceSEd Maste IRDynamicChecks::~IRDynamicChecks() = default; 612ac7ddfbfSEd Maste 613ac7ddfbfSEd Maste bool 614ac7ddfbfSEd Maste IRDynamicChecks::runOnModule(llvm::Module &M) 615ac7ddfbfSEd Maste { 616ac7ddfbfSEd Maste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 617ac7ddfbfSEd Maste 618ac7ddfbfSEd Maste llvm::Function* function = M.getFunction(StringRef(m_func_name.c_str())); 619ac7ddfbfSEd Maste 620ac7ddfbfSEd Maste if (!function) 621ac7ddfbfSEd Maste { 622ac7ddfbfSEd Maste if (log) 623ac7ddfbfSEd Maste log->Printf("Couldn't find %s() in the module", m_func_name.c_str()); 624ac7ddfbfSEd Maste 625ac7ddfbfSEd Maste return false; 626ac7ddfbfSEd Maste } 627ac7ddfbfSEd Maste 6289f2f44ceSEd Maste if (m_checker_functions.m_valid_pointer_check) 629ac7ddfbfSEd Maste { 630ac7ddfbfSEd Maste ValidPointerChecker vpc(M, m_checker_functions); 631ac7ddfbfSEd Maste 632ac7ddfbfSEd Maste if (!vpc.Inspect(*function)) 633ac7ddfbfSEd Maste return false; 634ac7ddfbfSEd Maste 635ac7ddfbfSEd Maste if (!vpc.Instrument()) 636ac7ddfbfSEd Maste return false; 637ac7ddfbfSEd Maste } 638ac7ddfbfSEd Maste 6399f2f44ceSEd Maste if (m_checker_functions.m_objc_object_check) 640ac7ddfbfSEd Maste { 641ac7ddfbfSEd Maste ObjcObjectChecker ooc(M, m_checker_functions); 642ac7ddfbfSEd Maste 643ac7ddfbfSEd Maste if (!ooc.Inspect(*function)) 644ac7ddfbfSEd Maste return false; 645ac7ddfbfSEd Maste 646ac7ddfbfSEd Maste if (!ooc.Instrument()) 647ac7ddfbfSEd Maste return false; 648ac7ddfbfSEd Maste } 649ac7ddfbfSEd Maste 650ac7ddfbfSEd Maste if (log && log->GetVerbose()) 651ac7ddfbfSEd Maste { 652ac7ddfbfSEd Maste std::string s; 653ac7ddfbfSEd Maste raw_string_ostream oss(s); 654ac7ddfbfSEd Maste 6559f2f44ceSEd Maste M.print(oss, nullptr); 656ac7ddfbfSEd Maste 657ac7ddfbfSEd Maste oss.flush(); 658ac7ddfbfSEd Maste 659ac7ddfbfSEd Maste log->Printf ("Module after dynamic checks: \n%s", s.c_str()); 660ac7ddfbfSEd Maste } 661ac7ddfbfSEd Maste 662ac7ddfbfSEd Maste return true; 663ac7ddfbfSEd Maste } 664ac7ddfbfSEd Maste 665ac7ddfbfSEd Maste void 666ac7ddfbfSEd Maste IRDynamicChecks::assignPassManager(PMStack &PMS, 667ac7ddfbfSEd Maste PassManagerType T) 668ac7ddfbfSEd Maste { 669ac7ddfbfSEd Maste } 670ac7ddfbfSEd Maste 671ac7ddfbfSEd Maste PassManagerType 672ac7ddfbfSEd Maste IRDynamicChecks::getPotentialPassManagerType() const 673ac7ddfbfSEd Maste { 674ac7ddfbfSEd Maste return PMT_ModulePassManager; 675ac7ddfbfSEd Maste } 676