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