Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 1 | //===-- IRDynamicChecks.cpp -------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Expression/IRDynamicChecks.h" |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 11 | |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 12 | #include "lldb/Core/ConstString.h" |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Log.h" |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 14 | #include "lldb/Expression/ClangUtilityFunction.h" |
| 15 | #include "lldb/Target/ExecutionContext.h" |
Sean Callanan | 14a97ff | 2010-11-04 01:51:38 +0000 | [diff] [blame] | 16 | #include "lldb/Target/ObjCLanguageRuntime.h" |
| 17 | #include "lldb/Target/Process.h" |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 18 | #include "lldb/Target/StackFrame.h" |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 19 | |
| 20 | #include "llvm/Support/raw_ostream.h" |
Sean Callanan | 2a8c338 | 2011-04-14 02:01:31 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 22 | #include "llvm/Function.h" |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 23 | #include "llvm/Instructions.h" |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
| 25 | #include "llvm/Value.h" |
| 26 | |
| 27 | using namespace llvm; |
| 28 | using namespace lldb_private; |
| 29 | |
| 30 | static char ID; |
| 31 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 32 | #define VALID_POINTER_CHECK_NAME "$__lldb_valid_pointer_check" |
| 33 | #define VALID_OBJC_OBJECT_CHECK_NAME "$__lldb_objc_object_check" |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 34 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 35 | static const char g_valid_pointer_check_text[] = |
| 36 | "extern \"C\" void\n" |
Greg Clayton | 21f5fe1 | 2010-10-16 21:09:32 +0000 | [diff] [blame] | 37 | "$__lldb_valid_pointer_check (unsigned char *$__lldb_arg_ptr)\n" |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 38 | "{\n" |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 39 | " unsigned char $__lldb_local_val = *$__lldb_arg_ptr;\n" |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 40 | "}"; |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 41 | |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 42 | DynamicCheckerFunctions::DynamicCheckerFunctions () |
| 43 | { |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | DynamicCheckerFunctions::~DynamicCheckerFunctions () |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | bool |
| 51 | DynamicCheckerFunctions::Install(Stream &error_stream, |
| 52 | ExecutionContext &exe_ctx) |
| 53 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 54 | m_valid_pointer_check.reset(new ClangUtilityFunction(g_valid_pointer_check_text, |
| 55 | VALID_POINTER_CHECK_NAME)); |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 56 | if (!m_valid_pointer_check->Install(error_stream, exe_ctx)) |
| 57 | return false; |
Sean Callanan | 14a97ff | 2010-11-04 01:51:38 +0000 | [diff] [blame] | 58 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 59 | Process *process = exe_ctx.GetProcessPtr(); |
| 60 | |
| 61 | if (process) |
Sean Callanan | 14a97ff | 2010-11-04 01:51:38 +0000 | [diff] [blame] | 62 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 63 | ObjCLanguageRuntime *objc_language_runtime = process->GetObjCLanguageRuntime(); |
Sean Callanan | 14a97ff | 2010-11-04 01:51:38 +0000 | [diff] [blame] | 64 | |
| 65 | if (objc_language_runtime) |
| 66 | { |
| 67 | m_objc_object_check.reset(objc_language_runtime->CreateObjectChecker(VALID_OBJC_OBJECT_CHECK_NAME)); |
| 68 | |
| 69 | if (!m_objc_object_check->Install(error_stream, exe_ctx)) |
| 70 | return false; |
| 71 | } |
| 72 | } |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 77 | static std::string |
| 78 | PrintValue(llvm::Value *V, bool truncate = false) |
| 79 | { |
| 80 | std::string s; |
| 81 | raw_string_ostream rso(s); |
| 82 | V->print(rso); |
| 83 | rso.flush(); |
| 84 | if (truncate) |
| 85 | s.resize(s.length() - 1); |
| 86 | return s; |
| 87 | } |
| 88 | |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 89 | //---------------------------------------------------------------------- |
| 90 | /// @class Instrumenter IRDynamicChecks.cpp |
| 91 | /// @brief Finds and instruments individual LLVM IR instructions |
| 92 | /// |
| 93 | /// When instrumenting LLVM IR, it is frequently desirable to first search |
| 94 | /// for instructions, and then later modify them. This way iterators |
| 95 | /// remain intact, and multiple passes can look at the same code base without |
| 96 | /// treading on each other's toes. |
| 97 | /// |
| 98 | /// The Instrumenter class implements this functionality. A client first |
| 99 | /// calls Inspect on a function, which populates a list of instructions to |
| 100 | /// be instrumented. Then, later, when all passes' Inspect functions have |
| 101 | /// been called, the client calls Instrument, which adds the desired |
| 102 | /// instrumentation. |
| 103 | /// |
| 104 | /// A subclass of Instrumenter must override InstrumentInstruction, which |
| 105 | /// is responsible for adding whatever instrumentation is necessary. |
| 106 | /// |
| 107 | /// A subclass of Instrumenter may override: |
| 108 | /// |
| 109 | /// - InspectInstruction [default: does nothing] |
| 110 | /// |
| 111 | /// - InspectBasicBlock [default: iterates through the instructions in a |
| 112 | /// basic block calling InspectInstruction] |
| 113 | /// |
| 114 | /// - InspectFunction [default: iterates through the basic blocks in a |
| 115 | /// function calling InspectBasicBlock] |
| 116 | //---------------------------------------------------------------------- |
| 117 | class Instrumenter { |
| 118 | public: |
| 119 | //------------------------------------------------------------------ |
| 120 | /// Constructor |
| 121 | /// |
| 122 | /// @param[in] module |
| 123 | /// The module being instrumented. |
| 124 | //------------------------------------------------------------------ |
| 125 | Instrumenter (llvm::Module &module, |
| 126 | DynamicCheckerFunctions &checker_functions) : |
| 127 | m_module(module), |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 128 | m_checker_functions(checker_functions), |
| 129 | m_i8ptr_ty(NULL) |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 130 | { |
| 131 | } |
| 132 | |
Greg Clayton | bdcb6ab | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 133 | virtual~Instrumenter () |
| 134 | { |
| 135 | } |
| 136 | |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 137 | //------------------------------------------------------------------ |
| 138 | /// Inspect a function to find instructions to instrument |
| 139 | /// |
| 140 | /// @param[in] function |
| 141 | /// The function to inspect. |
| 142 | /// |
| 143 | /// @return |
| 144 | /// True on success; false on error. |
| 145 | //------------------------------------------------------------------ |
| 146 | bool Inspect (llvm::Function &function) |
| 147 | { |
| 148 | return InspectFunction(function); |
| 149 | } |
| 150 | |
| 151 | //------------------------------------------------------------------ |
| 152 | /// Instrument all the instructions found by Inspect() |
| 153 | /// |
| 154 | /// @return |
| 155 | /// True on success; false on error. |
| 156 | //------------------------------------------------------------------ |
| 157 | bool Instrument () |
| 158 | { |
| 159 | for (InstIterator ii = m_to_instrument.begin(), last_ii = m_to_instrument.end(); |
| 160 | ii != last_ii; |
| 161 | ++ii) |
| 162 | { |
| 163 | if (!InstrumentInstruction(*ii)) |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | protected: |
| 170 | //------------------------------------------------------------------ |
| 171 | /// Add instrumentation to a single instruction |
| 172 | /// |
| 173 | /// @param[in] inst |
| 174 | /// The instruction to be instrumented. |
| 175 | /// |
| 176 | /// @return |
| 177 | /// True on success; false otherwise. |
| 178 | //------------------------------------------------------------------ |
| 179 | virtual bool InstrumentInstruction(llvm::Instruction *inst) = 0; |
| 180 | |
| 181 | //------------------------------------------------------------------ |
| 182 | /// Register a single instruction to be instrumented |
| 183 | /// |
| 184 | /// @param[in] inst |
| 185 | /// The instruction to be instrumented. |
| 186 | //------------------------------------------------------------------ |
| 187 | void RegisterInstruction(llvm::Instruction &i) |
| 188 | { |
| 189 | m_to_instrument.push_back(&i); |
| 190 | } |
| 191 | |
| 192 | //------------------------------------------------------------------ |
| 193 | /// Determine whether a single instruction is interesting to |
| 194 | /// instrument, and, if so, call RegisterInstruction |
| 195 | /// |
| 196 | /// @param[in] i |
| 197 | /// The instruction to be inspected. |
| 198 | /// |
| 199 | /// @return |
| 200 | /// False if there was an error scanning; true otherwise. |
| 201 | //------------------------------------------------------------------ |
| 202 | virtual bool InspectInstruction(llvm::Instruction &i) |
| 203 | { |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | //------------------------------------------------------------------ |
| 208 | /// Scan a basic block to see if any instructions are interesting |
| 209 | /// |
| 210 | /// @param[in] bb |
| 211 | /// The basic block to be inspected. |
| 212 | /// |
| 213 | /// @return |
| 214 | /// False if there was an error scanning; true otherwise. |
| 215 | //------------------------------------------------------------------ |
| 216 | virtual bool InspectBasicBlock(llvm::BasicBlock &bb) |
| 217 | { |
| 218 | for (llvm::BasicBlock::iterator ii = bb.begin(), last_ii = bb.end(); |
| 219 | ii != last_ii; |
| 220 | ++ii) |
| 221 | { |
| 222 | if (!InspectInstruction(*ii)) |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | //------------------------------------------------------------------ |
| 230 | /// Scan a function to see if any instructions are interesting |
| 231 | /// |
| 232 | /// @param[in] f |
| 233 | /// The function to be inspected. |
| 234 | /// |
| 235 | /// @return |
| 236 | /// False if there was an error scanning; true otherwise. |
| 237 | //------------------------------------------------------------------ |
| 238 | virtual bool InspectFunction(llvm::Function &f) |
| 239 | { |
| 240 | for (llvm::Function::iterator bbi = f.begin(), last_bbi = f.end(); |
| 241 | bbi != last_bbi; |
| 242 | ++bbi) |
| 243 | { |
| 244 | if (!InspectBasicBlock(*bbi)) |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | return true; |
| 249 | } |
| 250 | |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 251 | //------------------------------------------------------------------ |
| 252 | /// Build a function pointer for a function with signature |
| 253 | /// void (*)(uint8_t*) with a given address |
| 254 | /// |
| 255 | /// @param[in] start_address |
| 256 | /// The address of the function. |
| 257 | /// |
| 258 | /// @return |
| 259 | /// The function pointer, for use in a CallInst. |
| 260 | //------------------------------------------------------------------ |
| 261 | llvm::Value *BuildPointerValidatorFunc(lldb::addr_t start_address) |
| 262 | { |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 263 | IntegerType *intptr_ty = llvm::Type::getIntNTy(m_module.getContext(), |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 264 | (m_module.getPointerSize() == llvm::Module::Pointer64) ? 64 : 32); |
| 265 | |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 266 | llvm::Type *param_array[1]; |
| 267 | |
| 268 | param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy()); |
| 269 | |
| 270 | ArrayRef<llvm::Type*> params(param_array, 1); |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 271 | |
| 272 | FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true); |
| 273 | PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty); |
| 274 | Constant *fun_addr_int = ConstantInt::get(intptr_ty, start_address, false); |
| 275 | return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty); |
| 276 | } |
| 277 | |
Sean Callanan | 20b8a96 | 2011-10-27 00:02:05 +0000 | [diff] [blame] | 278 | //------------------------------------------------------------------ |
| 279 | /// Build a function pointer for a function with signature |
| 280 | /// void (*)(uint8_t*, uint8_t*) with a given address |
| 281 | /// |
| 282 | /// @param[in] start_address |
| 283 | /// The address of the function. |
| 284 | /// |
| 285 | /// @return |
| 286 | /// The function pointer, for use in a CallInst. |
| 287 | //------------------------------------------------------------------ |
| 288 | llvm::Value *BuildObjectCheckerFunc(lldb::addr_t start_address) |
| 289 | { |
| 290 | IntegerType *intptr_ty = llvm::Type::getIntNTy(m_module.getContext(), |
| 291 | (m_module.getPointerSize() == llvm::Module::Pointer64) ? 64 : 32); |
| 292 | |
| 293 | llvm::Type *param_array[2]; |
| 294 | |
| 295 | param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy()); |
| 296 | param_array[1] = const_cast<llvm::PointerType*>(GetI8PtrTy()); |
| 297 | |
| 298 | ArrayRef<llvm::Type*> params(param_array, 2); |
| 299 | |
| 300 | FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true); |
| 301 | PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty); |
| 302 | Constant *fun_addr_int = ConstantInt::get(intptr_ty, start_address, false); |
| 303 | return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty); |
| 304 | } |
| 305 | |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 306 | PointerType *GetI8PtrTy() |
Sean Callanan | 14a97ff | 2010-11-04 01:51:38 +0000 | [diff] [blame] | 307 | { |
| 308 | if (!m_i8ptr_ty) |
| 309 | m_i8ptr_ty = llvm::Type::getInt8PtrTy(m_module.getContext()); |
| 310 | |
| 311 | return m_i8ptr_ty; |
| 312 | } |
| 313 | |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 314 | typedef std::vector <llvm::Instruction *> InstVector; |
| 315 | typedef InstVector::iterator InstIterator; |
| 316 | |
| 317 | InstVector m_to_instrument; ///< List of instructions the inspector found |
| 318 | llvm::Module &m_module; ///< The module which is being instrumented |
| 319 | DynamicCheckerFunctions &m_checker_functions; ///< The dynamic checker functions for the process |
Sean Callanan | 14a97ff | 2010-11-04 01:51:38 +0000 | [diff] [blame] | 320 | private: |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 321 | PointerType *m_i8ptr_ty; |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | class ValidPointerChecker : public Instrumenter |
| 325 | { |
| 326 | public: |
Greg Clayton | bdcb6ab | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 327 | ValidPointerChecker (llvm::Module &module, |
| 328 | DynamicCheckerFunctions &checker_functions) : |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 329 | Instrumenter(module, checker_functions), |
| 330 | m_valid_pointer_check_func(NULL) |
| 331 | { |
| 332 | } |
Greg Clayton | bdcb6ab | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 333 | |
| 334 | virtual ~ValidPointerChecker () |
| 335 | { |
| 336 | } |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 337 | private: |
| 338 | bool InstrumentInstruction(llvm::Instruction *inst) |
| 339 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 340 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 341 | |
Enrico Granata | 4c3fb4b | 2011-07-19 18:03:25 +0000 | [diff] [blame] | 342 | if (log) |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 343 | log->Printf("Instrumenting load/store instruction: %s\n", |
| 344 | PrintValue(inst).c_str()); |
| 345 | |
| 346 | if (!m_valid_pointer_check_func) |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 347 | m_valid_pointer_check_func = BuildPointerValidatorFunc(m_checker_functions.m_valid_pointer_check->StartAddress()); |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 348 | |
Sean Callanan | 58baaad | 2011-07-08 00:39:14 +0000 | [diff] [blame] | 349 | llvm::Value *dereferenced_ptr = NULL; |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 350 | |
| 351 | if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst> (inst)) |
| 352 | dereferenced_ptr = li->getPointerOperand(); |
| 353 | else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst> (inst)) |
| 354 | dereferenced_ptr = si->getPointerOperand(); |
| 355 | else |
| 356 | return false; |
| 357 | |
| 358 | // Insert an instruction to cast the loaded value to int8_t* |
| 359 | |
| 360 | BitCastInst *bit_cast = new BitCastInst(dereferenced_ptr, |
Sean Callanan | 14a97ff | 2010-11-04 01:51:38 +0000 | [diff] [blame] | 361 | GetI8PtrTy(), |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 362 | "", |
| 363 | inst); |
| 364 | |
| 365 | // Insert an instruction to call the helper with the result |
| 366 | |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 367 | llvm::Value *arg_array[1]; |
| 368 | |
| 369 | arg_array[0] = bit_cast; |
| 370 | |
| 371 | llvm::ArrayRef<llvm::Value *> args(arg_array, 1); |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 372 | |
| 373 | CallInst::Create(m_valid_pointer_check_func, |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 374 | args, |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 375 | "", |
| 376 | inst); |
| 377 | |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | bool InspectInstruction(llvm::Instruction &i) |
| 382 | { |
| 383 | if (dyn_cast<llvm::LoadInst> (&i) || |
| 384 | dyn_cast<llvm::StoreInst> (&i)) |
| 385 | RegisterInstruction(i); |
| 386 | |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | llvm::Value *m_valid_pointer_check_func; |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 391 | }; |
| 392 | |
| 393 | class ObjcObjectChecker : public Instrumenter |
| 394 | { |
| 395 | public: |
| 396 | ObjcObjectChecker(llvm::Module &module, |
| 397 | DynamicCheckerFunctions &checker_functions) : |
| 398 | Instrumenter(module, checker_functions), |
| 399 | m_objc_object_check_func(NULL) |
| 400 | { |
| 401 | } |
Greg Clayton | bdcb6ab | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 402 | |
| 403 | virtual |
| 404 | ~ObjcObjectChecker () |
| 405 | { |
| 406 | } |
| 407 | |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 408 | private: |
| 409 | bool InstrumentInstruction(llvm::Instruction *inst) |
| 410 | { |
| 411 | CallInst *call_inst = dyn_cast<CallInst>(inst); |
| 412 | |
| 413 | if (!call_inst) |
Sean Callanan | 14a97ff | 2010-11-04 01:51:38 +0000 | [diff] [blame] | 414 | return false; // call_inst really shouldn't be NULL, because otherwise InspectInstruction wouldn't have registered it |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 415 | |
| 416 | if (!m_objc_object_check_func) |
Sean Callanan | 20b8a96 | 2011-10-27 00:02:05 +0000 | [diff] [blame] | 417 | m_objc_object_check_func = BuildObjectCheckerFunc(m_checker_functions.m_objc_object_check->StartAddress()); |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 418 | |
| 419 | // id objc_msgSend(id theReceiver, SEL theSelector, ...) |
| 420 | |
Sean Callanan | 20b8a96 | 2011-10-27 00:02:05 +0000 | [diff] [blame] | 421 | llvm::Value *target_object = call_inst->getArgOperand(0);; |
| 422 | llvm::Value *selector = call_inst->getArgOperand(1); |
| 423 | |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 424 | // Insert an instruction to cast the receiver id to int8_t* |
| 425 | |
| 426 | BitCastInst *bit_cast = new BitCastInst(target_object, |
Sean Callanan | 14a97ff | 2010-11-04 01:51:38 +0000 | [diff] [blame] | 427 | GetI8PtrTy(), |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 428 | "", |
| 429 | inst); |
| 430 | |
| 431 | // Insert an instruction to call the helper with the result |
| 432 | |
Sean Callanan | 20b8a96 | 2011-10-27 00:02:05 +0000 | [diff] [blame] | 433 | llvm::Value *arg_array[2]; |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 434 | |
| 435 | arg_array[0] = bit_cast; |
Sean Callanan | 20b8a96 | 2011-10-27 00:02:05 +0000 | [diff] [blame] | 436 | arg_array[1] = selector; |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 437 | |
Sean Callanan | 715f6b0 | 2011-10-31 22:11:40 +0000 | [diff] [blame^] | 438 | ArrayRef<llvm::Value*> args(arg_array, 2); |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 439 | |
| 440 | CallInst::Create(m_objc_object_check_func, |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 441 | args, |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 442 | "", |
| 443 | inst); |
| 444 | |
| 445 | return true; |
| 446 | } |
| 447 | |
| 448 | bool InspectInstruction(llvm::Instruction &i) |
| 449 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 450 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 451 | |
| 452 | CallInst *call_inst = dyn_cast<CallInst>(&i); |
| 453 | |
| 454 | if (call_inst) |
| 455 | { |
| 456 | // This metadata is set by IRForTarget::MaybeHandleCall(). |
| 457 | |
| 458 | MDNode *metadata = call_inst->getMetadata("lldb.call.realName"); |
| 459 | |
| 460 | if (!metadata) |
| 461 | return true; |
| 462 | |
| 463 | if (metadata->getNumOperands() != 1) |
| 464 | { |
| 465 | if (log) |
| 466 | log->Printf("Function call metadata has %d operands for [%p] %s", metadata->getNumOperands(), call_inst, PrintValue(call_inst).c_str()); |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | ConstantArray *real_name = dyn_cast<ConstantArray>(metadata->getOperand(0)); |
| 471 | |
| 472 | if (!real_name) |
| 473 | { |
| 474 | if (log) |
| 475 | log->Printf("Function call metadata is not a ConstantArray for [%p] %s", call_inst, PrintValue(call_inst).c_str()); |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | if (!real_name->isString()) |
| 480 | { |
| 481 | if (log) |
| 482 | log->Printf("Function call metadata is not a string for [%p] %s", call_inst, PrintValue(call_inst).c_str()); |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | if (log) |
| 487 | log->Printf("Found call to %s: %s\n", real_name->getAsString().c_str(), PrintValue(call_inst).c_str()); |
| 488 | |
| 489 | if (real_name->getAsString().find("objc_msgSend") != std::string::npos) |
| 490 | RegisterInstruction(i); |
| 491 | } |
| 492 | |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | llvm::Value *m_objc_object_check_func; |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 497 | }; |
| 498 | |
| 499 | IRDynamicChecks::IRDynamicChecks(DynamicCheckerFunctions &checker_functions, |
| 500 | const char *func_name) : |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 501 | ModulePass(ID), |
Stephen Wilson | dbeb3e1 | 2011-04-11 19:41:40 +0000 | [diff] [blame] | 502 | m_func_name(func_name), |
| 503 | m_checker_functions(checker_functions) |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 504 | { |
| 505 | } |
| 506 | |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 507 | IRDynamicChecks::~IRDynamicChecks() |
| 508 | { |
| 509 | } |
| 510 | |
| 511 | bool |
| 512 | IRDynamicChecks::runOnModule(llvm::Module &M) |
| 513 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 514 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 515 | |
| 516 | llvm::Function* function = M.getFunction(StringRef(m_func_name.c_str())); |
| 517 | |
| 518 | if (!function) |
| 519 | { |
| 520 | if (log) |
| 521 | log->Printf("Couldn't find %s() in the module", m_func_name.c_str()); |
| 522 | |
| 523 | return false; |
| 524 | } |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 525 | |
| 526 | ValidPointerChecker vpc(M, m_checker_functions); |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 527 | |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 528 | if (!vpc.Inspect(*function)) |
| 529 | return false; |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 530 | |
Sean Callanan | 0526233 | 2010-09-02 00:37:32 +0000 | [diff] [blame] | 531 | if (!vpc.Instrument()) |
| 532 | return false; |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 533 | |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 534 | ObjcObjectChecker ooc(M, m_checker_functions); |
| 535 | |
| 536 | if (!ooc.Inspect(*function)) |
| 537 | return false; |
| 538 | |
| 539 | if (!ooc.Instrument()) |
| 540 | return false; |
Jim Ingham | bbce712 | 2011-01-22 01:25:40 +0000 | [diff] [blame] | 541 | |
| 542 | if (log && log->GetVerbose()) |
Sean Callanan | 65af734 | 2010-09-08 20:04:08 +0000 | [diff] [blame] | 543 | { |
| 544 | std::string s; |
| 545 | raw_string_ostream oss(s); |
| 546 | |
| 547 | M.print(oss, NULL); |
| 548 | |
| 549 | oss.flush(); |
| 550 | |
Jim Ingham | bbce712 | 2011-01-22 01:25:40 +0000 | [diff] [blame] | 551 | log->Printf ("Module after dynamic checks: \n%s", s.c_str()); |
Sean Callanan | 65af734 | 2010-09-08 20:04:08 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 554 | return true; |
| 555 | } |
| 556 | |
| 557 | void |
| 558 | IRDynamicChecks::assignPassManager(PMStack &PMS, |
| 559 | PassManagerType T) |
| 560 | { |
| 561 | } |
| 562 | |
| 563 | PassManagerType |
| 564 | IRDynamicChecks::getPotentialPassManagerType() const |
| 565 | { |
| 566 | return PMT_ModulePassManager; |
| 567 | } |