diff options
Diffstat (limited to 'contrib/llvm/tools/lldb/source/Expression/IRDynamicChecks.cpp')
-rw-r--r-- | contrib/llvm/tools/lldb/source/Expression/IRDynamicChecks.cpp | 933 |
1 files changed, 434 insertions, 499 deletions
diff --git a/contrib/llvm/tools/lldb/source/Expression/IRDynamicChecks.cpp b/contrib/llvm/tools/lldb/source/Expression/IRDynamicChecks.cpp index 62bcfe0..b337ea2 100644 --- a/contrib/llvm/tools/lldb/source/Expression/IRDynamicChecks.cpp +++ b/contrib/llvm/tools/lldb/source/Expression/IRDynamicChecks.cpp @@ -10,13 +10,13 @@ // C Includes // C++ Includes // Other libraries and framework includes -#include "llvm/Support/raw_ostream.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/Function.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Module.h" #include "llvm/IR/Value.h" +#include "llvm/Support/raw_ostream.h" // Project includes #include "lldb/Expression/IRDynamicChecks.h" @@ -39,76 +39,72 @@ static char ID; #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" -"}"; + "extern \"C\" void\n" + "_$__lldb_valid_pointer_check (unsigned char *$__lldb_arg_ptr)\n" + "{\n" + " unsigned char $__lldb_local_val = *$__lldb_arg_ptr;\n" + "}"; DynamicCheckerFunctions::DynamicCheckerFunctions() = default; DynamicCheckerFunctions::~DynamicCheckerFunctions() = default; -bool -DynamicCheckerFunctions::Install(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx) -{ - Error error; - m_valid_pointer_check.reset(exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(g_valid_pointer_check_text, - lldb::eLanguageTypeC, - VALID_POINTER_CHECK_NAME, - error)); - if (error.Fail()) - return false; +bool DynamicCheckerFunctions::Install(DiagnosticManager &diagnostic_manager, + ExecutionContext &exe_ctx) { + Error error; + m_valid_pointer_check.reset( + exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage( + g_valid_pointer_check_text, lldb::eLanguageTypeC, + VALID_POINTER_CHECK_NAME, error)); + if (error.Fail()) + return false; - if (!m_valid_pointer_check->Install(diagnostic_manager, exe_ctx)) - return false; + if (!m_valid_pointer_check->Install(diagnostic_manager, exe_ctx)) + return false; - Process *process = exe_ctx.GetProcessPtr(); + Process *process = exe_ctx.GetProcessPtr(); - if (process) - { - ObjCLanguageRuntime *objc_language_runtime = process->GetObjCLanguageRuntime(); + if (process) { + ObjCLanguageRuntime *objc_language_runtime = + process->GetObjCLanguageRuntime(); - if (objc_language_runtime) - { - m_objc_object_check.reset(objc_language_runtime->CreateObjectChecker(VALID_OBJC_OBJECT_CHECK_NAME)); + if (objc_language_runtime) { + m_objc_object_check.reset(objc_language_runtime->CreateObjectChecker( + VALID_OBJC_OBJECT_CHECK_NAME)); - if (!m_objc_object_check->Install(diagnostic_manager, exe_ctx)) - return false; - } + if (!m_objc_object_check->Install(diagnostic_manager, exe_ctx)) + return false; } + } - return true; + return true; } -bool -DynamicCheckerFunctions::DoCheckersExplainStop (lldb::addr_t addr, Stream &message) -{ - // 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)) - { - message.Printf ("Attempted to dereference an invalid ObjC Object or send it an unrecognized selector"); - return true; - } - return false; +bool DynamicCheckerFunctions::DoCheckersExplainStop(lldb::addr_t addr, + Stream &message) { + // 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)) { + message.Printf("Attempted to dereference an invalid ObjC Object or send it " + "an unrecognized selector"); + return true; + } + return false; } -static std::string -PrintValue(llvm::Value *V, bool truncate = false) -{ - std::string s; - raw_string_ostream rso(s); - V->print(rso); - rso.flush(); - if (truncate) - s.resize(s.length() - 1); - return s; +static std::string PrintValue(llvm::Value *V, bool truncate = false) { + std::string s; + raw_string_ostream rso(s); + V->print(rso); + rso.flush(); + if (truncate) + s.resize(s.length() - 1); + return s; } //---------------------------------------------------------------------- @@ -141,537 +137,476 @@ PrintValue(llvm::Value *V, bool truncate = false) //---------------------------------------------------------------------- class Instrumenter { public: - //------------------------------------------------------------------ - /// Constructor - /// - /// @param[in] module - /// The module being instrumented. - //------------------------------------------------------------------ - Instrumenter (llvm::Module &module, - DynamicCheckerFunctions &checker_functions) : - m_module(module), - m_checker_functions(checker_functions), - m_i8ptr_ty(nullptr), - m_intptr_ty(nullptr) - { - } - - virtual ~Instrumenter() = default; - - //------------------------------------------------------------------ - /// Inspect a function to find instructions to instrument - /// - /// @param[in] function - /// The function to inspect. - /// - /// @return - /// True on success; false on error. - //------------------------------------------------------------------ - bool Inspect (llvm::Function &function) - { - return InspectFunction(function); + //------------------------------------------------------------------ + /// Constructor + /// + /// @param[in] module + /// The module being instrumented. + //------------------------------------------------------------------ + Instrumenter(llvm::Module &module, DynamicCheckerFunctions &checker_functions) + : m_module(module), m_checker_functions(checker_functions), + m_i8ptr_ty(nullptr), m_intptr_ty(nullptr) {} + + virtual ~Instrumenter() = default; + + //------------------------------------------------------------------ + /// Inspect a function to find instructions to instrument + /// + /// @param[in] function + /// The function to inspect. + /// + /// @return + /// True on success; false on error. + //------------------------------------------------------------------ + bool Inspect(llvm::Function &function) { return InspectFunction(function); } + + //------------------------------------------------------------------ + /// Instrument all the instructions found by Inspect() + /// + /// @return + /// True on success; false on error. + //------------------------------------------------------------------ + bool Instrument() { + for (InstIterator ii = m_to_instrument.begin(), + last_ii = m_to_instrument.end(); + ii != last_ii; ++ii) { + if (!InstrumentInstruction(*ii)) + return false; } - //------------------------------------------------------------------ - /// Instrument all the instructions found by Inspect() - /// - /// @return - /// True on success; false on error. - //------------------------------------------------------------------ - bool Instrument () - { - for (InstIterator ii = m_to_instrument.begin(), last_ii = m_to_instrument.end(); - ii != last_ii; - ++ii) - { - if (!InstrumentInstruction(*ii)) - return false; - } - - return true; - } + return true; + } protected: - //------------------------------------------------------------------ - /// Add instrumentation to a single instruction - /// - /// @param[in] inst - /// The instruction to be instrumented. - /// - /// @return - /// True on success; false otherwise. - //------------------------------------------------------------------ - virtual bool InstrumentInstruction(llvm::Instruction *inst) = 0; - - //------------------------------------------------------------------ - /// Register a single instruction to be instrumented - /// - /// @param[in] inst - /// The instruction to be instrumented. - //------------------------------------------------------------------ - void RegisterInstruction(llvm::Instruction &i) - { - m_to_instrument.push_back(&i); + //------------------------------------------------------------------ + /// Add instrumentation to a single instruction + /// + /// @param[in] inst + /// The instruction to be instrumented. + /// + /// @return + /// True on success; false otherwise. + //------------------------------------------------------------------ + virtual bool InstrumentInstruction(llvm::Instruction *inst) = 0; + + //------------------------------------------------------------------ + /// Register a single instruction to be instrumented + /// + /// @param[in] inst + /// The instruction to be instrumented. + //------------------------------------------------------------------ + void RegisterInstruction(llvm::Instruction &i) { + m_to_instrument.push_back(&i); + } + + //------------------------------------------------------------------ + /// Determine whether a single instruction is interesting to + /// instrument, and, if so, call RegisterInstruction + /// + /// @param[in] i + /// The instruction to be inspected. + /// + /// @return + /// False if there was an error scanning; true otherwise. + //------------------------------------------------------------------ + virtual bool InspectInstruction(llvm::Instruction &i) { return true; } + + //------------------------------------------------------------------ + /// Scan a basic block to see if any instructions are interesting + /// + /// @param[in] bb + /// The basic block to be inspected. + /// + /// @return + /// False if there was an error scanning; true otherwise. + //------------------------------------------------------------------ + virtual bool InspectBasicBlock(llvm::BasicBlock &bb) { + for (llvm::BasicBlock::iterator ii = bb.begin(), last_ii = bb.end(); + ii != last_ii; ++ii) { + if (!InspectInstruction(*ii)) + return false; } - //------------------------------------------------------------------ - /// Determine whether a single instruction is interesting to - /// instrument, and, if so, call RegisterInstruction - /// - /// @param[in] i - /// The instruction to be inspected. - /// - /// @return - /// False if there was an error scanning; true otherwise. - //------------------------------------------------------------------ - virtual bool InspectInstruction(llvm::Instruction &i) - { - return true; + return true; + } + + //------------------------------------------------------------------ + /// Scan a function to see if any instructions are interesting + /// + /// @param[in] f + /// The function to be inspected. + /// + /// @return + /// False if there was an error scanning; true otherwise. + //------------------------------------------------------------------ + virtual bool InspectFunction(llvm::Function &f) { + for (llvm::Function::iterator bbi = f.begin(), last_bbi = f.end(); + bbi != last_bbi; ++bbi) { + if (!InspectBasicBlock(*bbi)) + return false; } - //------------------------------------------------------------------ - /// Scan a basic block to see if any instructions are interesting - /// - /// @param[in] bb - /// The basic block to be inspected. - /// - /// @return - /// False if there was an error scanning; true otherwise. - //------------------------------------------------------------------ - virtual bool InspectBasicBlock(llvm::BasicBlock &bb) - { - for (llvm::BasicBlock::iterator ii = bb.begin(), last_ii = bb.end(); - ii != last_ii; - ++ii) - { - if (!InspectInstruction(*ii)) - return false; - } - - return true; + 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::Value *BuildPointerValidatorFunc(lldb::addr_t start_address) { + llvm::Type *param_array[1]; + + param_array[0] = const_cast<llvm::PointerType *>(GetI8PtrTy()); + + ArrayRef<llvm::Type *> params(param_array, 1); + + FunctionType *fun_ty = FunctionType::get( + llvm::Type::getVoidTy(m_module.getContext()), params, true); + PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty); + Constant *fun_addr_int = + ConstantInt::get(GetIntptrTy(), start_address, false); + return 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 + /// + /// @param[in] start_address + /// The address of the function. + /// + /// @return + /// The function pointer, for use in a CallInst. + //------------------------------------------------------------------ + llvm::Value *BuildObjectCheckerFunc(lldb::addr_t start_address) { + llvm::Type *param_array[2]; + + param_array[0] = const_cast<llvm::PointerType *>(GetI8PtrTy()); + param_array[1] = const_cast<llvm::PointerType *>(GetI8PtrTy()); + + ArrayRef<llvm::Type *> params(param_array, 2); + + FunctionType *fun_ty = FunctionType::get( + llvm::Type::getVoidTy(m_module.getContext()), params, true); + PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty); + Constant *fun_addr_int = + ConstantInt::get(GetIntptrTy(), start_address, false); + return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty); + } + + PointerType *GetI8PtrTy() { + if (!m_i8ptr_ty) + m_i8ptr_ty = llvm::Type::getInt8PtrTy(m_module.getContext()); + + return m_i8ptr_ty; + } + + IntegerType *GetIntptrTy() { + if (!m_intptr_ty) { + llvm::DataLayout data_layout(&m_module); + + m_intptr_ty = llvm::Type::getIntNTy(m_module.getContext(), + data_layout.getPointerSizeInBits()); } - //------------------------------------------------------------------ - /// Scan a function to see if any instructions are interesting - /// - /// @param[in] f - /// The function to be inspected. - /// - /// @return - /// False if there was an error scanning; true otherwise. - //------------------------------------------------------------------ - virtual bool InspectFunction(llvm::Function &f) - { - for (llvm::Function::iterator bbi = f.begin(), last_bbi = f.end(); - bbi != last_bbi; - ++bbi) - { - if (!InspectBasicBlock(*bbi)) - return false; - } + return m_intptr_ty; + } - return true; - } + typedef std::vector<llvm::Instruction *> InstVector; + typedef InstVector::iterator InstIterator; - //------------------------------------------------------------------ - /// 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::Value *BuildPointerValidatorFunc(lldb::addr_t start_address) - { - llvm::Type *param_array[1]; - - param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy()); - - ArrayRef<llvm::Type*> params(param_array, 1); - - FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true); - PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty); - Constant *fun_addr_int = ConstantInt::get(GetIntptrTy(), start_address, false); - return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty); - } + InstVector m_to_instrument; ///< List of instructions the inspector found + llvm::Module &m_module; ///< The module which is being instrumented + DynamicCheckerFunctions + &m_checker_functions; ///< The dynamic checker functions for the process - //------------------------------------------------------------------ - /// Build a function pointer for a function with signature - /// void (*)(uint8_t*, 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::Value *BuildObjectCheckerFunc(lldb::addr_t start_address) - { - llvm::Type *param_array[2]; - - param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy()); - param_array[1] = const_cast<llvm::PointerType*>(GetI8PtrTy()); - - ArrayRef<llvm::Type*> params(param_array, 2); - - FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true); - PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty); - Constant *fun_addr_int = ConstantInt::get(GetIntptrTy(), start_address, false); - return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty); - } +private: + PointerType *m_i8ptr_ty; + IntegerType *m_intptr_ty; +}; - PointerType *GetI8PtrTy() - { - if (!m_i8ptr_ty) - m_i8ptr_ty = llvm::Type::getInt8PtrTy(m_module.getContext()); +class ValidPointerChecker : public Instrumenter { +public: + ValidPointerChecker(llvm::Module &module, + DynamicCheckerFunctions &checker_functions) + : Instrumenter(module, checker_functions), + m_valid_pointer_check_func(nullptr) {} - return m_i8ptr_ty; - } + ~ValidPointerChecker() override = default; - IntegerType *GetIntptrTy() - { - if (!m_intptr_ty) - { - llvm::DataLayout data_layout(&m_module); +protected: + bool InstrumentInstruction(llvm::Instruction *inst) override { + Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); - m_intptr_ty = llvm::Type::getIntNTy(m_module.getContext(), data_layout.getPointerSizeInBits()); - } + if (log) + log->Printf("Instrumenting load/store instruction: %s\n", + PrintValue(inst).c_str()); - return m_intptr_ty; - } + if (!m_valid_pointer_check_func) + m_valid_pointer_check_func = BuildPointerValidatorFunc( + m_checker_functions.m_valid_pointer_check->StartAddress()); - typedef std::vector <llvm::Instruction *> InstVector; - typedef InstVector::iterator InstIterator; + llvm::Value *dereferenced_ptr = nullptr; - InstVector m_to_instrument; ///< List of instructions the inspector found - llvm::Module &m_module; ///< The module which is being instrumented - DynamicCheckerFunctions &m_checker_functions; ///< The dynamic checker functions for the process + if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst>(inst)) + dereferenced_ptr = li->getPointerOperand(); + else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst>(inst)) + dereferenced_ptr = si->getPointerOperand(); + else + return false; -private: - PointerType *m_i8ptr_ty; - IntegerType *m_intptr_ty; -}; + // Insert an instruction to cast the loaded value to int8_t* -class ValidPointerChecker : public Instrumenter -{ -public: - ValidPointerChecker (llvm::Module &module, - DynamicCheckerFunctions &checker_functions) : - Instrumenter(module, checker_functions), - m_valid_pointer_check_func(nullptr) - { - } + BitCastInst *bit_cast = + new BitCastInst(dereferenced_ptr, GetI8PtrTy(), "", inst); - ~ValidPointerChecker() override = default; + // Insert an instruction to call the helper with the result -protected: - bool InstrumentInstruction(llvm::Instruction *inst) override - { - Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); + llvm::Value *arg_array[1]; - if (log) - log->Printf("Instrumenting load/store instruction: %s\n", - PrintValue(inst).c_str()); + arg_array[0] = bit_cast; - if (!m_valid_pointer_check_func) - m_valid_pointer_check_func = BuildPointerValidatorFunc(m_checker_functions.m_valid_pointer_check->StartAddress()); + llvm::ArrayRef<llvm::Value *> args(arg_array, 1); - llvm::Value *dereferenced_ptr = nullptr; + CallInst::Create(m_valid_pointer_check_func, args, "", inst); - if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst> (inst)) - dereferenced_ptr = li->getPointerOperand(); - else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst> (inst)) - dereferenced_ptr = si->getPointerOperand(); - else - return false; + return true; + } - // Insert an instruction to cast the loaded value to int8_t* + bool InspectInstruction(llvm::Instruction &i) override { + if (dyn_cast<llvm::LoadInst>(&i) || dyn_cast<llvm::StoreInst>(&i)) + RegisterInstruction(i); - BitCastInst *bit_cast = new BitCastInst(dereferenced_ptr, - GetI8PtrTy(), - "", - inst); + return true; + } - // Insert an instruction to call the helper with the result +private: + llvm::Value *m_valid_pointer_check_func; +}; - llvm::Value *arg_array[1]; +class ObjcObjectChecker : public Instrumenter { +public: + ObjcObjectChecker(llvm::Module &module, + DynamicCheckerFunctions &checker_functions) + : Instrumenter(module, checker_functions), + m_objc_object_check_func(nullptr) {} - arg_array[0] = bit_cast; + ~ObjcObjectChecker() override = default; - llvm::ArrayRef<llvm::Value *> args(arg_array, 1); + enum msgSend_type { + eMsgSend = 0, + eMsgSendSuper, + eMsgSendSuper_stret, + eMsgSend_fpret, + eMsgSend_stret + }; - CallInst::Create(m_valid_pointer_check_func, - args, - "", - inst); + std::map<llvm::Instruction *, msgSend_type> msgSend_types; - return true; +protected: + bool InstrumentInstruction(llvm::Instruction *inst) override { + CallInst *call_inst = dyn_cast<CallInst>(inst); + + if (!call_inst) + return false; // call_inst really shouldn't be nullptr, because otherwise + // InspectInstruction wouldn't have registered it + + if (!m_objc_object_check_func) + m_objc_object_check_func = BuildObjectCheckerFunc( + m_checker_functions.m_objc_object_check->StartAddress()); + + // id objc_msgSend(id theReceiver, SEL theSelector, ...) + + llvm::Value *target_object; + llvm::Value *selector; + + switch (msgSend_types[inst]) { + case eMsgSend: + case eMsgSend_fpret: + target_object = call_inst->getArgOperand(0); + selector = call_inst->getArgOperand(1); + break; + case eMsgSend_stret: + target_object = call_inst->getArgOperand(1); + selector = call_inst->getArgOperand(2); + break; + case eMsgSendSuper: + case eMsgSendSuper_stret: + return true; } - bool InspectInstruction(llvm::Instruction &i) override - { - if (dyn_cast<llvm::LoadInst> (&i) || - dyn_cast<llvm::StoreInst> (&i)) - RegisterInstruction(i); + // These objects should always be valid according to Sean Calannan + assert(target_object); + assert(selector); - return true; - } + // Insert an instruction to cast the receiver id to int8_t* -private: - llvm::Value *m_valid_pointer_check_func; -}; + BitCastInst *bit_cast = + new BitCastInst(target_object, GetI8PtrTy(), "", inst); -class ObjcObjectChecker : public Instrumenter -{ -public: - ObjcObjectChecker(llvm::Module &module, - DynamicCheckerFunctions &checker_functions) : - Instrumenter(module, checker_functions), - m_objc_object_check_func(nullptr) - { - } + // Insert an instruction to call the helper with the result - ~ObjcObjectChecker() override = default; + llvm::Value *arg_array[2]; - enum msgSend_type - { - eMsgSend = 0, - eMsgSendSuper, - eMsgSendSuper_stret, - eMsgSend_fpret, - eMsgSend_stret - }; + arg_array[0] = bit_cast; + arg_array[1] = selector; - std::map <llvm::Instruction *, msgSend_type> msgSend_types; + ArrayRef<llvm::Value *> args(arg_array, 2); -protected: - bool InstrumentInstruction(llvm::Instruction *inst) override - { - CallInst *call_inst = dyn_cast<CallInst>(inst); + CallInst::Create(m_objc_object_check_func, args, "", inst); - if (!call_inst) - return false; // call_inst really shouldn't be nullptr, because otherwise InspectInstruction wouldn't have registered it + return true; + } - if (!m_objc_object_check_func) - m_objc_object_check_func = BuildObjectCheckerFunc(m_checker_functions.m_objc_object_check->StartAddress()); + static llvm::Function *GetFunction(llvm::Value *value) { + if (llvm::Function *function = llvm::dyn_cast<llvm::Function>(value)) { + return function; + } - // id objc_msgSend(id theReceiver, SEL theSelector, ...) + if (llvm::ConstantExpr *const_expr = + llvm::dyn_cast<llvm::ConstantExpr>(value)) { + switch (const_expr->getOpcode()) { + default: + return nullptr; + case llvm::Instruction::BitCast: + return GetFunction(const_expr->getOperand(0)); + } + } - llvm::Value *target_object; - llvm::Value *selector; + return nullptr; + } - switch (msgSend_types[inst]) - { - case eMsgSend: - case eMsgSend_fpret: - target_object = call_inst->getArgOperand(0); - selector = call_inst->getArgOperand(1); - break; - case eMsgSend_stret: - target_object = call_inst->getArgOperand(1); - selector = call_inst->getArgOperand(2); - break; - case eMsgSendSuper: - case eMsgSendSuper_stret: - return true; - } + static llvm::Function *GetCalledFunction(llvm::CallInst *inst) { + return GetFunction(inst->getCalledValue()); + } - // These objects should always be valid according to Sean Calannan - assert (target_object); - assert (selector); + bool InspectInstruction(llvm::Instruction &i) override { + Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); - // Insert an instruction to cast the receiver id to int8_t* + CallInst *call_inst = dyn_cast<CallInst>(&i); - BitCastInst *bit_cast = new BitCastInst(target_object, - GetI8PtrTy(), - "", - inst); + if (call_inst) { + const llvm::Function *called_function = GetCalledFunction(call_inst); - // Insert an instruction to call the helper with the result + if (!called_function) + return true; - llvm::Value *arg_array[2]; + std::string name_str = called_function->getName().str(); + const char *name_cstr = name_str.c_str(); - arg_array[0] = bit_cast; - arg_array[1] = selector; + if (log) + log->Printf("Found call to %s: %s\n", name_cstr, + PrintValue(call_inst).c_str()); - ArrayRef<llvm::Value*> args(arg_array, 2); + if (name_str.find("objc_msgSend") == std::string::npos) + return true; - CallInst::Create(m_objc_object_check_func, - args, - "", - inst); + if (!strcmp(name_cstr, "objc_msgSend")) { + RegisterInstruction(i); + msgSend_types[&i] = eMsgSend; + return true; + } + if (!strcmp(name_cstr, "objc_msgSend_stret")) { + RegisterInstruction(i); + msgSend_types[&i] = eMsgSend_stret; return true; - } - - static llvm::Function *GetFunction(llvm::Value *value) - { - if (llvm::Function *function = llvm::dyn_cast<llvm::Function>(value)) - { - return function; - } - - if (llvm::ConstantExpr *const_expr = llvm::dyn_cast<llvm::ConstantExpr>(value)) - { - switch (const_expr->getOpcode()) - { - default: - return nullptr; - case llvm::Instruction::BitCast: - return GetFunction(const_expr->getOperand(0)); - } - } - - return nullptr; - } - - static llvm::Function *GetCalledFunction(llvm::CallInst *inst) - { - return GetFunction(inst->getCalledValue()); - } + } - bool InspectInstruction(llvm::Instruction &i) override - { - Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); - - CallInst *call_inst = dyn_cast<CallInst>(&i); - - if (call_inst) - { - const llvm::Function *called_function = GetCalledFunction(call_inst); - - if (!called_function) - return true; - - std::string name_str = called_function->getName().str(); - const char* name_cstr = name_str.c_str(); - - if (log) - log->Printf("Found call to %s: %s\n", name_cstr, PrintValue(call_inst).c_str()); - - if (name_str.find("objc_msgSend") == std::string::npos) - return true; - - if (!strcmp(name_cstr, "objc_msgSend")) - { - RegisterInstruction(i); - msgSend_types[&i] = eMsgSend; - return true; - } - - if (!strcmp(name_cstr, "objc_msgSend_stret")) - { - RegisterInstruction(i); - msgSend_types[&i] = eMsgSend_stret; - return true; - } - - if (!strcmp(name_cstr, "objc_msgSend_fpret")) - { - RegisterInstruction(i); - msgSend_types[&i] = eMsgSend_fpret; - return true; - } - - if (!strcmp(name_cstr, "objc_msgSendSuper")) - { - RegisterInstruction(i); - msgSend_types[&i] = eMsgSendSuper; - return true; - } - - if (!strcmp(name_cstr, "objc_msgSendSuper_stret")) - { - RegisterInstruction(i); - msgSend_types[&i] = eMsgSendSuper_stret; - return true; - } - - if (log) - log->Printf("Function name '%s' contains 'objc_msgSend' but is not handled", name_str.c_str()); - - return true; - } + if (!strcmp(name_cstr, "objc_msgSend_fpret")) { + RegisterInstruction(i); + msgSend_types[&i] = eMsgSend_fpret; + return true; + } + if (!strcmp(name_cstr, "objc_msgSendSuper")) { + RegisterInstruction(i); + msgSend_types[&i] = eMsgSendSuper; return true; + } + + if (!strcmp(name_cstr, "objc_msgSendSuper_stret")) { + RegisterInstruction(i); + msgSend_types[&i] = eMsgSendSuper_stret; + return true; + } + + if (log) + log->Printf( + "Function name '%s' contains 'objc_msgSend' but is not handled", + name_str.c_str()); + + return true; } + return true; + } + private: - llvm::Value *m_objc_object_check_func; + llvm::Value *m_objc_object_check_func; }; IRDynamicChecks::IRDynamicChecks(DynamicCheckerFunctions &checker_functions, - const char *func_name) : - ModulePass(ID), - m_func_name(func_name), - m_checker_functions(checker_functions) -{ -} + const char *func_name) + : ModulePass(ID), m_func_name(func_name), + m_checker_functions(checker_functions) {} IRDynamicChecks::~IRDynamicChecks() = default; -bool -IRDynamicChecks::runOnModule(llvm::Module &M) -{ - Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); +bool IRDynamicChecks::runOnModule(llvm::Module &M) { + Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); - llvm::Function* function = M.getFunction(StringRef(m_func_name.c_str())); + llvm::Function *function = M.getFunction(StringRef(m_func_name)); - if (!function) - { - if (log) - log->Printf("Couldn't find %s() in the module", m_func_name.c_str()); + if (!function) { + if (log) + log->Printf("Couldn't find %s() in the module", m_func_name.c_str()); - return false; - } + return false; + } - if (m_checker_functions.m_valid_pointer_check) - { - ValidPointerChecker vpc(M, m_checker_functions); + if (m_checker_functions.m_valid_pointer_check) { + ValidPointerChecker vpc(M, m_checker_functions); - if (!vpc.Inspect(*function)) - return false; + if (!vpc.Inspect(*function)) + return false; - if (!vpc.Instrument()) - return false; - } + if (!vpc.Instrument()) + return false; + } - if (m_checker_functions.m_objc_object_check) - { - ObjcObjectChecker ooc(M, m_checker_functions); + if (m_checker_functions.m_objc_object_check) { + ObjcObjectChecker ooc(M, m_checker_functions); - if (!ooc.Inspect(*function)) - return false; + if (!ooc.Inspect(*function)) + return false; - if (!ooc.Instrument()) - return false; - } + if (!ooc.Instrument()) + return false; + } - if (log && log->GetVerbose()) - { - std::string s; - raw_string_ostream oss(s); + if (log && log->GetVerbose()) { + std::string s; + raw_string_ostream oss(s); - M.print(oss, nullptr); + M.print(oss, nullptr); - oss.flush(); + oss.flush(); - log->Printf ("Module after dynamic checks: \n%s", s.c_str()); - } + log->Printf("Module after dynamic checks: \n%s", s.c_str()); + } - return true; + return true; } -void -IRDynamicChecks::assignPassManager(PMStack &PMS, - PassManagerType T) -{ -} +void IRDynamicChecks::assignPassManager(PMStack &PMS, PassManagerType T) {} -PassManagerType -IRDynamicChecks::getPotentialPassManagerType() const -{ - return PMT_ModulePassManager; +PassManagerType IRDynamicChecks::getPotentialPassManagerType() const { + return PMT_ModulePassManager; } |