blob: 1b9e362cfadae586d8efd589068931b317e2af58 [file] [log] [blame]
Sean Callananf18d91c2010-09-01 00:58:00 +00001//===-- 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 Callananf18d91c2010-09-01 00:58:00 +000011
Sean Callanane8a59a82010-09-13 21:34:21 +000012#include "lldb/Core/ConstString.h"
Sean Callananf18d91c2010-09-01 00:58:00 +000013#include "lldb/Core/Log.h"
Sean Callanane8a59a82010-09-13 21:34:21 +000014#include "lldb/Expression/ClangUtilityFunction.h"
15#include "lldb/Target/ExecutionContext.h"
Sean Callanan14a97ff2010-11-04 01:51:38 +000016#include "lldb/Target/ObjCLanguageRuntime.h"
17#include "lldb/Target/Process.h"
Sean Callanane8a59a82010-09-13 21:34:21 +000018#include "lldb/Target/StackFrame.h"
Sean Callananf18d91c2010-09-01 00:58:00 +000019
20#include "llvm/Support/raw_ostream.h"
Chandler Carrutha80c70c2013-01-02 12:20:07 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/Instructions.h"
24#include "llvm/IR/Module.h"
25#include "llvm/IR/Value.h"
Sean Callananf18d91c2010-09-01 00:58:00 +000026
27using namespace llvm;
28using namespace lldb_private;
29
30static char ID;
31
Greg Clayton8de27c72010-10-15 22:48:33 +000032#define VALID_POINTER_CHECK_NAME "$__lldb_valid_pointer_check"
33#define VALID_OBJC_OBJECT_CHECK_NAME "$__lldb_objc_object_check"
Sean Callanane8a59a82010-09-13 21:34:21 +000034
Greg Clayton8de27c72010-10-15 22:48:33 +000035static const char g_valid_pointer_check_text[] =
36"extern \"C\" void\n"
Greg Clayton21f5fe12010-10-16 21:09:32 +000037"$__lldb_valid_pointer_check (unsigned char *$__lldb_arg_ptr)\n"
Greg Clayton8de27c72010-10-15 22:48:33 +000038"{\n"
Greg Clayton24b48ff2010-10-17 22:03:32 +000039" unsigned char $__lldb_local_val = *$__lldb_arg_ptr;\n"
Greg Clayton8de27c72010-10-15 22:48:33 +000040"}";
Sean Callanane8a59a82010-09-13 21:34:21 +000041
Sean Callananf18d91c2010-09-01 00:58:00 +000042DynamicCheckerFunctions::DynamicCheckerFunctions ()
43{
Sean Callananf18d91c2010-09-01 00:58:00 +000044}
45
46DynamicCheckerFunctions::~DynamicCheckerFunctions ()
47{
48}
49
50bool
51DynamicCheckerFunctions::Install(Stream &error_stream,
52 ExecutionContext &exe_ctx)
53{
Greg Clayton8de27c72010-10-15 22:48:33 +000054 m_valid_pointer_check.reset(new ClangUtilityFunction(g_valid_pointer_check_text,
55 VALID_POINTER_CHECK_NAME));
Sean Callananf18d91c2010-09-01 00:58:00 +000056 if (!m_valid_pointer_check->Install(error_stream, exe_ctx))
57 return false;
Sean Callanan14a97ff2010-11-04 01:51:38 +000058
Greg Clayton567e7f32011-09-22 04:58:26 +000059 Process *process = exe_ctx.GetProcessPtr();
60
61 if (process)
Sean Callanan14a97ff2010-11-04 01:51:38 +000062 {
Greg Clayton567e7f32011-09-22 04:58:26 +000063 ObjCLanguageRuntime *objc_language_runtime = process->GetObjCLanguageRuntime();
Sean Callanan14a97ff2010-11-04 01:51:38 +000064
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 Callananf18d91c2010-09-01 00:58:00 +000073
74 return true;
75}
76
Jim Inghamba560cc2011-11-01 02:46:54 +000077bool
78DynamicCheckerFunctions::DoCheckersExplainStop (lldb::addr_t addr, Stream &message)
79{
80 // FIXME: We have to get the checkers to know why they scotched the call in more detail,
81 // so we can print a better message here.
82 if (m_valid_pointer_check.get() != NULL && m_valid_pointer_check->ContainsAddress(addr))
83 {
84 message.Printf ("Attempted to dereference an invalid pointer.");
85 return true;
86 }
87 else if (m_objc_object_check.get() != NULL && m_objc_object_check->ContainsAddress(addr))
88 {
89 message.Printf ("Attempted to dereference an invalid ObjC Object or send it an unrecognized selector");
90 return true;
91 }
92 return false;
93}
94
95
Sean Callananf18d91c2010-09-01 00:58:00 +000096static std::string
97PrintValue(llvm::Value *V, bool truncate = false)
98{
99 std::string s;
100 raw_string_ostream rso(s);
101 V->print(rso);
102 rso.flush();
103 if (truncate)
104 s.resize(s.length() - 1);
105 return s;
106}
107
Sean Callanan05262332010-09-02 00:37:32 +0000108//----------------------------------------------------------------------
109/// @class Instrumenter IRDynamicChecks.cpp
110/// @brief Finds and instruments individual LLVM IR instructions
111///
112/// When instrumenting LLVM IR, it is frequently desirable to first search
113/// for instructions, and then later modify them. This way iterators
114/// remain intact, and multiple passes can look at the same code base without
115/// treading on each other's toes.
116///
117/// The Instrumenter class implements this functionality. A client first
118/// calls Inspect on a function, which populates a list of instructions to
119/// be instrumented. Then, later, when all passes' Inspect functions have
120/// been called, the client calls Instrument, which adds the desired
121/// instrumentation.
122///
123/// A subclass of Instrumenter must override InstrumentInstruction, which
124/// is responsible for adding whatever instrumentation is necessary.
125///
126/// A subclass of Instrumenter may override:
127///
128/// - InspectInstruction [default: does nothing]
129///
130/// - InspectBasicBlock [default: iterates through the instructions in a
131/// basic block calling InspectInstruction]
132///
133/// - InspectFunction [default: iterates through the basic blocks in a
134/// function calling InspectBasicBlock]
135//----------------------------------------------------------------------
136class Instrumenter {
137public:
138 //------------------------------------------------------------------
139 /// Constructor
140 ///
141 /// @param[in] module
142 /// The module being instrumented.
143 //------------------------------------------------------------------
144 Instrumenter (llvm::Module &module,
145 DynamicCheckerFunctions &checker_functions) :
146 m_module(module),
Sean Callanane8a59a82010-09-13 21:34:21 +0000147 m_checker_functions(checker_functions),
148 m_i8ptr_ty(NULL)
Sean Callanan05262332010-09-02 00:37:32 +0000149 {
150 }
151
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000152 virtual~Instrumenter ()
153 {
154 }
155
Sean Callanan05262332010-09-02 00:37:32 +0000156 //------------------------------------------------------------------
157 /// Inspect a function to find instructions to instrument
158 ///
159 /// @param[in] function
160 /// The function to inspect.
161 ///
162 /// @return
163 /// True on success; false on error.
164 //------------------------------------------------------------------
165 bool Inspect (llvm::Function &function)
166 {
167 return InspectFunction(function);
168 }
169
170 //------------------------------------------------------------------
171 /// Instrument all the instructions found by Inspect()
172 ///
173 /// @return
174 /// True on success; false on error.
175 //------------------------------------------------------------------
176 bool Instrument ()
177 {
178 for (InstIterator ii = m_to_instrument.begin(), last_ii = m_to_instrument.end();
179 ii != last_ii;
180 ++ii)
181 {
182 if (!InstrumentInstruction(*ii))
183 return false;
184 }
185
186 return true;
187 }
188protected:
189 //------------------------------------------------------------------
190 /// Add instrumentation to a single instruction
191 ///
192 /// @param[in] inst
193 /// The instruction to be instrumented.
194 ///
195 /// @return
196 /// True on success; false otherwise.
197 //------------------------------------------------------------------
198 virtual bool InstrumentInstruction(llvm::Instruction *inst) = 0;
199
200 //------------------------------------------------------------------
201 /// Register a single instruction to be instrumented
202 ///
203 /// @param[in] inst
204 /// The instruction to be instrumented.
205 //------------------------------------------------------------------
206 void RegisterInstruction(llvm::Instruction &i)
207 {
208 m_to_instrument.push_back(&i);
209 }
210
211 //------------------------------------------------------------------
212 /// Determine whether a single instruction is interesting to
213 /// instrument, and, if so, call RegisterInstruction
214 ///
215 /// @param[in] i
216 /// The instruction to be inspected.
217 ///
218 /// @return
219 /// False if there was an error scanning; true otherwise.
220 //------------------------------------------------------------------
221 virtual bool InspectInstruction(llvm::Instruction &i)
222 {
223 return true;
224 }
225
226 //------------------------------------------------------------------
227 /// Scan a basic block to see if any instructions are interesting
228 ///
229 /// @param[in] bb
230 /// The basic block to be inspected.
231 ///
232 /// @return
233 /// False if there was an error scanning; true otherwise.
234 //------------------------------------------------------------------
235 virtual bool InspectBasicBlock(llvm::BasicBlock &bb)
236 {
237 for (llvm::BasicBlock::iterator ii = bb.begin(), last_ii = bb.end();
238 ii != last_ii;
239 ++ii)
240 {
241 if (!InspectInstruction(*ii))
242 return false;
243 }
244
245 return true;
246 }
247
248 //------------------------------------------------------------------
249 /// Scan a function to see if any instructions are interesting
250 ///
251 /// @param[in] f
252 /// The function to be inspected.
253 ///
254 /// @return
255 /// False if there was an error scanning; true otherwise.
256 //------------------------------------------------------------------
257 virtual bool InspectFunction(llvm::Function &f)
258 {
259 for (llvm::Function::iterator bbi = f.begin(), last_bbi = f.end();
260 bbi != last_bbi;
261 ++bbi)
262 {
263 if (!InspectBasicBlock(*bbi))
264 return false;
265 }
266
267 return true;
268 }
269
Sean Callanane8a59a82010-09-13 21:34:21 +0000270 //------------------------------------------------------------------
271 /// Build a function pointer for a function with signature
272 /// void (*)(uint8_t*) with a given address
273 ///
274 /// @param[in] start_address
275 /// The address of the function.
276 ///
277 /// @return
278 /// The function pointer, for use in a CallInst.
279 //------------------------------------------------------------------
280 llvm::Value *BuildPointerValidatorFunc(lldb::addr_t start_address)
281 {
Sean Callanan9b6898f2011-07-30 02:42:06 +0000282 IntegerType *intptr_ty = llvm::Type::getIntNTy(m_module.getContext(),
Sean Callanan4fbe61b2012-10-11 22:00:52 +0000283 (m_module.getPointerSize() == llvm::Module::Pointer64) ? 64 : 32);
Sean Callanane8a59a82010-09-13 21:34:21 +0000284
Sean Callanan9b6898f2011-07-30 02:42:06 +0000285 llvm::Type *param_array[1];
286
287 param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy());
288
289 ArrayRef<llvm::Type*> params(param_array, 1);
Sean Callanane8a59a82010-09-13 21:34:21 +0000290
291 FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true);
292 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
293 Constant *fun_addr_int = ConstantInt::get(intptr_ty, start_address, false);
294 return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
295 }
296
Sean Callanan20b8a962011-10-27 00:02:05 +0000297 //------------------------------------------------------------------
298 /// Build a function pointer for a function with signature
299 /// void (*)(uint8_t*, uint8_t*) with a given address
300 ///
301 /// @param[in] start_address
302 /// The address of the function.
303 ///
304 /// @return
305 /// The function pointer, for use in a CallInst.
306 //------------------------------------------------------------------
307 llvm::Value *BuildObjectCheckerFunc(lldb::addr_t start_address)
308 {
309 IntegerType *intptr_ty = llvm::Type::getIntNTy(m_module.getContext(),
Sean Callanan4fbe61b2012-10-11 22:00:52 +0000310 (m_module.getPointerSize() == llvm::Module::Pointer64) ? 64 : 32);
Sean Callanan20b8a962011-10-27 00:02:05 +0000311
312 llvm::Type *param_array[2];
313
314 param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy());
315 param_array[1] = const_cast<llvm::PointerType*>(GetI8PtrTy());
316
317 ArrayRef<llvm::Type*> params(param_array, 2);
318
319 FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true);
320 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
321 Constant *fun_addr_int = ConstantInt::get(intptr_ty, start_address, false);
322 return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
323 }
324
Sean Callanan9b6898f2011-07-30 02:42:06 +0000325 PointerType *GetI8PtrTy()
Sean Callanan14a97ff2010-11-04 01:51:38 +0000326 {
327 if (!m_i8ptr_ty)
328 m_i8ptr_ty = llvm::Type::getInt8PtrTy(m_module.getContext());
329
330 return m_i8ptr_ty;
331 }
332
Sean Callanan05262332010-09-02 00:37:32 +0000333 typedef std::vector <llvm::Instruction *> InstVector;
334 typedef InstVector::iterator InstIterator;
335
336 InstVector m_to_instrument; ///< List of instructions the inspector found
337 llvm::Module &m_module; ///< The module which is being instrumented
338 DynamicCheckerFunctions &m_checker_functions; ///< The dynamic checker functions for the process
Sean Callanan14a97ff2010-11-04 01:51:38 +0000339private:
Sean Callanan9b6898f2011-07-30 02:42:06 +0000340 PointerType *m_i8ptr_ty;
Sean Callanan05262332010-09-02 00:37:32 +0000341};
342
343class ValidPointerChecker : public Instrumenter
344{
345public:
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000346 ValidPointerChecker (llvm::Module &module,
347 DynamicCheckerFunctions &checker_functions) :
Sean Callanan05262332010-09-02 00:37:32 +0000348 Instrumenter(module, checker_functions),
349 m_valid_pointer_check_func(NULL)
350 {
351 }
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000352
353 virtual ~ValidPointerChecker ()
354 {
355 }
Sean Callanan05262332010-09-02 00:37:32 +0000356private:
357 bool InstrumentInstruction(llvm::Instruction *inst)
358 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000359 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan05262332010-09-02 00:37:32 +0000360
Enrico Granata4c3fb4b2011-07-19 18:03:25 +0000361 if (log)
Sean Callanan05262332010-09-02 00:37:32 +0000362 log->Printf("Instrumenting load/store instruction: %s\n",
363 PrintValue(inst).c_str());
364
365 if (!m_valid_pointer_check_func)
Sean Callanane8a59a82010-09-13 21:34:21 +0000366 m_valid_pointer_check_func = BuildPointerValidatorFunc(m_checker_functions.m_valid_pointer_check->StartAddress());
Sean Callanan05262332010-09-02 00:37:32 +0000367
Sean Callanan58baaad2011-07-08 00:39:14 +0000368 llvm::Value *dereferenced_ptr = NULL;
Sean Callanan05262332010-09-02 00:37:32 +0000369
370 if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst> (inst))
371 dereferenced_ptr = li->getPointerOperand();
372 else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst> (inst))
373 dereferenced_ptr = si->getPointerOperand();
374 else
375 return false;
376
377 // Insert an instruction to cast the loaded value to int8_t*
378
379 BitCastInst *bit_cast = new BitCastInst(dereferenced_ptr,
Sean Callanan14a97ff2010-11-04 01:51:38 +0000380 GetI8PtrTy(),
Sean Callanan05262332010-09-02 00:37:32 +0000381 "",
382 inst);
383
384 // Insert an instruction to call the helper with the result
385
Sean Callanan9b6898f2011-07-30 02:42:06 +0000386 llvm::Value *arg_array[1];
387
388 arg_array[0] = bit_cast;
389
390 llvm::ArrayRef<llvm::Value *> args(arg_array, 1);
Sean Callanan05262332010-09-02 00:37:32 +0000391
392 CallInst::Create(m_valid_pointer_check_func,
Sean Callanan9b6898f2011-07-30 02:42:06 +0000393 args,
Sean Callanan05262332010-09-02 00:37:32 +0000394 "",
395 inst);
396
397 return true;
398 }
399
400 bool InspectInstruction(llvm::Instruction &i)
401 {
402 if (dyn_cast<llvm::LoadInst> (&i) ||
403 dyn_cast<llvm::StoreInst> (&i))
404 RegisterInstruction(i);
405
406 return true;
407 }
408
409 llvm::Value *m_valid_pointer_check_func;
Sean Callanane8a59a82010-09-13 21:34:21 +0000410};
411
412class ObjcObjectChecker : public Instrumenter
413{
414public:
415 ObjcObjectChecker(llvm::Module &module,
416 DynamicCheckerFunctions &checker_functions) :
417 Instrumenter(module, checker_functions),
418 m_objc_object_check_func(NULL)
419 {
420 }
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000421
422 virtual
423 ~ObjcObjectChecker ()
424 {
425 }
Sean Callanan7befa302011-11-16 00:20:50 +0000426
427 enum msgSend_type
428 {
429 eMsgSend = 0,
430 eMsgSendSuper,
431 eMsgSendSuper_stret,
432 eMsgSend_fpret,
433 eMsgSend_stret
434 };
435
436 std::map <llvm::Instruction *, msgSend_type> msgSend_types;
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000437
Sean Callanane8a59a82010-09-13 21:34:21 +0000438private:
439 bool InstrumentInstruction(llvm::Instruction *inst)
440 {
441 CallInst *call_inst = dyn_cast<CallInst>(inst);
442
443 if (!call_inst)
Sean Callanan14a97ff2010-11-04 01:51:38 +0000444 return false; // call_inst really shouldn't be NULL, because otherwise InspectInstruction wouldn't have registered it
Sean Callanane8a59a82010-09-13 21:34:21 +0000445
446 if (!m_objc_object_check_func)
Sean Callanan20b8a962011-10-27 00:02:05 +0000447 m_objc_object_check_func = BuildObjectCheckerFunc(m_checker_functions.m_objc_object_check->StartAddress());
Sean Callanane8a59a82010-09-13 21:34:21 +0000448
449 // id objc_msgSend(id theReceiver, SEL theSelector, ...)
450
Sean Callanan7befa302011-11-16 00:20:50 +0000451 llvm::Value *target_object;
452 llvm::Value *selector;
453
454 switch (msgSend_types[inst])
455 {
456 case eMsgSend:
457 case eMsgSend_fpret:
458 target_object = call_inst->getArgOperand(0);
459 selector = call_inst->getArgOperand(1);
460 break;
461 case eMsgSend_stret:
462 target_object = call_inst->getArgOperand(1);
463 selector = call_inst->getArgOperand(2);
Greg Clayton4a379b12012-07-17 03:23:13 +0000464 break;
Sean Callanan7befa302011-11-16 00:20:50 +0000465 case eMsgSendSuper:
466 case eMsgSendSuper_stret:
467 return true;
468 }
Greg Clayton4a379b12012-07-17 03:23:13 +0000469
470 // These objects should always be valid according to Sean Calannan
471 assert (target_object);
472 assert (selector);
473
Sean Callanane8a59a82010-09-13 21:34:21 +0000474 // Insert an instruction to cast the receiver id to int8_t*
475
476 BitCastInst *bit_cast = new BitCastInst(target_object,
Sean Callanan14a97ff2010-11-04 01:51:38 +0000477 GetI8PtrTy(),
Sean Callanane8a59a82010-09-13 21:34:21 +0000478 "",
479 inst);
480
481 // Insert an instruction to call the helper with the result
482
Sean Callanan20b8a962011-10-27 00:02:05 +0000483 llvm::Value *arg_array[2];
Sean Callanan9b6898f2011-07-30 02:42:06 +0000484
485 arg_array[0] = bit_cast;
Sean Callanan20b8a962011-10-27 00:02:05 +0000486 arg_array[1] = selector;
Sean Callanan9b6898f2011-07-30 02:42:06 +0000487
Sean Callanan715f6b02011-10-31 22:11:40 +0000488 ArrayRef<llvm::Value*> args(arg_array, 2);
Sean Callanane8a59a82010-09-13 21:34:21 +0000489
490 CallInst::Create(m_objc_object_check_func,
Sean Callanan9b6898f2011-07-30 02:42:06 +0000491 args,
Sean Callanane8a59a82010-09-13 21:34:21 +0000492 "",
493 inst);
494
495 return true;
496 }
497
498 bool InspectInstruction(llvm::Instruction &i)
499 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000500 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanane8a59a82010-09-13 21:34:21 +0000501
502 CallInst *call_inst = dyn_cast<CallInst>(&i);
503
504 if (call_inst)
505 {
506 // This metadata is set by IRForTarget::MaybeHandleCall().
507
508 MDNode *metadata = call_inst->getMetadata("lldb.call.realName");
509
510 if (!metadata)
511 return true;
512
513 if (metadata->getNumOperands() != 1)
514 {
515 if (log)
516 log->Printf("Function call metadata has %d operands for [%p] %s", metadata->getNumOperands(), call_inst, PrintValue(call_inst).c_str());
517 return false;
518 }
519
Sean Callanan39a30342012-02-09 03:22:41 +0000520 ConstantDataArray *real_name = dyn_cast<ConstantDataArray>(metadata->getOperand(0));
Sean Callanane8a59a82010-09-13 21:34:21 +0000521
522 if (!real_name)
523 {
524 if (log)
525 log->Printf("Function call metadata is not a ConstantArray for [%p] %s", call_inst, PrintValue(call_inst).c_str());
526 return false;
527 }
528
529 if (!real_name->isString())
530 {
531 if (log)
532 log->Printf("Function call metadata is not a string for [%p] %s", call_inst, PrintValue(call_inst).c_str());
533 return false;
534 }
535
Sean Callanan7befa302011-11-16 00:20:50 +0000536 std::string name_str = real_name->getAsString();
537 const char* name_cstr = name_str.c_str();
538
Sean Callanan39a30342012-02-09 03:22:41 +0000539 if (log)
540 log->Printf("Found call to %s: %s\n", name_cstr, PrintValue(call_inst).c_str());
541
Sean Callanan7befa302011-11-16 00:20:50 +0000542 if (name_str.find("objc_msgSend") == std::string::npos)
543 return true;
544
545 if (!strcmp(name_cstr, "objc_msgSend"))
546 {
Sean Callanane8a59a82010-09-13 21:34:21 +0000547 RegisterInstruction(i);
Sean Callanan7befa302011-11-16 00:20:50 +0000548 msgSend_types[&i] = eMsgSend;
549 return true;
550 }
551
552 if (!strcmp(name_cstr, "objc_msgSend_stret"))
553 {
554 RegisterInstruction(i);
555 msgSend_types[&i] = eMsgSend_stret;
556 return true;
557 }
558
559 if (!strcmp(name_cstr, "objc_msgSend_fpret"))
560 {
561 RegisterInstruction(i);
562 msgSend_types[&i] = eMsgSend_fpret;
563 return true;
564 }
565
566 if (!strcmp(name_cstr, "objc_msgSendSuper"))
567 {
568 RegisterInstruction(i);
569 msgSend_types[&i] = eMsgSendSuper;
570 return true;
571 }
572
573 if (!strcmp(name_cstr, "objc_msgSendSuper_stret"))
574 {
575 RegisterInstruction(i);
576 msgSend_types[&i] = eMsgSendSuper_stret;
577 return true;
578 }
579
580 if (log)
581 log->Printf("Function name '%s' contains 'objc_msgSend' but is not handled", name_str.c_str());
582
583 return true;
Sean Callanane8a59a82010-09-13 21:34:21 +0000584 }
585
586 return true;
587 }
588
589 llvm::Value *m_objc_object_check_func;
Sean Callanan05262332010-09-02 00:37:32 +0000590};
591
592IRDynamicChecks::IRDynamicChecks(DynamicCheckerFunctions &checker_functions,
593 const char *func_name) :
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000594 ModulePass(ID),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +0000595 m_func_name(func_name),
596 m_checker_functions(checker_functions)
Sean Callanan05262332010-09-02 00:37:32 +0000597{
598}
599
Sean Callananf18d91c2010-09-01 00:58:00 +0000600IRDynamicChecks::~IRDynamicChecks()
601{
602}
603
604bool
605IRDynamicChecks::runOnModule(llvm::Module &M)
606{
Greg Claytone005f2c2010-11-06 01:53:30 +0000607 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananf18d91c2010-09-01 00:58:00 +0000608
609 llvm::Function* function = M.getFunction(StringRef(m_func_name.c_str()));
610
611 if (!function)
612 {
613 if (log)
614 log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
615
616 return false;
617 }
Sean Callanan05262332010-09-02 00:37:32 +0000618
Sean Callanan153bcca2012-05-29 23:46:46 +0000619 if (m_checker_functions.m_valid_pointer_check.get())
620 {
621 ValidPointerChecker vpc(M, m_checker_functions);
622
623 if (!vpc.Inspect(*function))
624 return false;
625
626 if (!vpc.Instrument())
627 return false;
628 }
Sean Callananf18d91c2010-09-01 00:58:00 +0000629
Sean Callanan153bcca2012-05-29 23:46:46 +0000630 if (m_checker_functions.m_objc_object_check.get())
631 {
632 ObjcObjectChecker ooc(M, m_checker_functions);
633
634 if (!ooc.Inspect(*function))
635 return false;
636
637 if (!ooc.Instrument())
638 return false;
639 }
640
Jim Inghambbce7122011-01-22 01:25:40 +0000641 if (log && log->GetVerbose())
Sean Callanan65af7342010-09-08 20:04:08 +0000642 {
643 std::string s;
644 raw_string_ostream oss(s);
645
646 M.print(oss, NULL);
647
648 oss.flush();
649
Jim Inghambbce7122011-01-22 01:25:40 +0000650 log->Printf ("Module after dynamic checks: \n%s", s.c_str());
Sean Callanan65af7342010-09-08 20:04:08 +0000651 }
652
Sean Callananf18d91c2010-09-01 00:58:00 +0000653 return true;
654}
655
656void
657IRDynamicChecks::assignPassManager(PMStack &PMS,
658 PassManagerType T)
659{
660}
661
662PassManagerType
663IRDynamicChecks::getPotentialPassManagerType() const
664{
665 return PMT_ModulePassManager;
666}