blob: 2af5e5ebedd1b348e70e5c4321aba02c9cb62d4e [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"
Sean Callanan2a8c3382011-04-14 02:01:31 +000021#include "llvm/Constants.h"
Sean Callananf18d91c2010-09-01 00:58:00 +000022#include "llvm/Function.h"
Sean Callanan05262332010-09-02 00:37:32 +000023#include "llvm/Instructions.h"
Sean Callananf18d91c2010-09-01 00:58:00 +000024#include "llvm/Module.h"
25#include "llvm/Value.h"
26
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
Sean Callananf18d91c2010-09-01 00:58:00 +000077static std::string
78PrintValue(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 Callanan05262332010-09-02 00:37:32 +000089//----------------------------------------------------------------------
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//----------------------------------------------------------------------
117class Instrumenter {
118public:
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 Callanane8a59a82010-09-13 21:34:21 +0000128 m_checker_functions(checker_functions),
129 m_i8ptr_ty(NULL)
Sean Callanan05262332010-09-02 00:37:32 +0000130 {
131 }
132
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000133 virtual~Instrumenter ()
134 {
135 }
136
Sean Callanan05262332010-09-02 00:37:32 +0000137 //------------------------------------------------------------------
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 }
169protected:
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 Callanane8a59a82010-09-13 21:34:21 +0000251 //------------------------------------------------------------------
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 Callanan9b6898f2011-07-30 02:42:06 +0000263 IntegerType *intptr_ty = llvm::Type::getIntNTy(m_module.getContext(),
Sean Callanane8a59a82010-09-13 21:34:21 +0000264 (m_module.getPointerSize() == llvm::Module::Pointer64) ? 64 : 32);
265
Sean Callanan9b6898f2011-07-30 02:42:06 +0000266 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 Callanane8a59a82010-09-13 21:34:21 +0000271
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 Callanan9b6898f2011-07-30 02:42:06 +0000278 PointerType *GetI8PtrTy()
Sean Callanan14a97ff2010-11-04 01:51:38 +0000279 {
280 if (!m_i8ptr_ty)
281 m_i8ptr_ty = llvm::Type::getInt8PtrTy(m_module.getContext());
282
283 return m_i8ptr_ty;
284 }
285
Sean Callanan05262332010-09-02 00:37:32 +0000286 typedef std::vector <llvm::Instruction *> InstVector;
287 typedef InstVector::iterator InstIterator;
288
289 InstVector m_to_instrument; ///< List of instructions the inspector found
290 llvm::Module &m_module; ///< The module which is being instrumented
291 DynamicCheckerFunctions &m_checker_functions; ///< The dynamic checker functions for the process
Sean Callanan14a97ff2010-11-04 01:51:38 +0000292private:
Sean Callanan9b6898f2011-07-30 02:42:06 +0000293 PointerType *m_i8ptr_ty;
Sean Callanan05262332010-09-02 00:37:32 +0000294};
295
296class ValidPointerChecker : public Instrumenter
297{
298public:
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000299 ValidPointerChecker (llvm::Module &module,
300 DynamicCheckerFunctions &checker_functions) :
Sean Callanan05262332010-09-02 00:37:32 +0000301 Instrumenter(module, checker_functions),
302 m_valid_pointer_check_func(NULL)
303 {
304 }
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000305
306 virtual ~ValidPointerChecker ()
307 {
308 }
Sean Callanan05262332010-09-02 00:37:32 +0000309private:
310 bool InstrumentInstruction(llvm::Instruction *inst)
311 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000312 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan05262332010-09-02 00:37:32 +0000313
Enrico Granata4c3fb4b2011-07-19 18:03:25 +0000314 if (log)
Sean Callanan05262332010-09-02 00:37:32 +0000315 log->Printf("Instrumenting load/store instruction: %s\n",
316 PrintValue(inst).c_str());
317
318 if (!m_valid_pointer_check_func)
Sean Callanane8a59a82010-09-13 21:34:21 +0000319 m_valid_pointer_check_func = BuildPointerValidatorFunc(m_checker_functions.m_valid_pointer_check->StartAddress());
Sean Callanan05262332010-09-02 00:37:32 +0000320
Sean Callanan58baaad2011-07-08 00:39:14 +0000321 llvm::Value *dereferenced_ptr = NULL;
Sean Callanan05262332010-09-02 00:37:32 +0000322
323 if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst> (inst))
324 dereferenced_ptr = li->getPointerOperand();
325 else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst> (inst))
326 dereferenced_ptr = si->getPointerOperand();
327 else
328 return false;
329
330 // Insert an instruction to cast the loaded value to int8_t*
331
332 BitCastInst *bit_cast = new BitCastInst(dereferenced_ptr,
Sean Callanan14a97ff2010-11-04 01:51:38 +0000333 GetI8PtrTy(),
Sean Callanan05262332010-09-02 00:37:32 +0000334 "",
335 inst);
336
337 // Insert an instruction to call the helper with the result
338
Sean Callanan9b6898f2011-07-30 02:42:06 +0000339 llvm::Value *arg_array[1];
340
341 arg_array[0] = bit_cast;
342
343 llvm::ArrayRef<llvm::Value *> args(arg_array, 1);
Sean Callanan05262332010-09-02 00:37:32 +0000344
345 CallInst::Create(m_valid_pointer_check_func,
Sean Callanan9b6898f2011-07-30 02:42:06 +0000346 args,
Sean Callanan05262332010-09-02 00:37:32 +0000347 "",
348 inst);
349
350 return true;
351 }
352
353 bool InspectInstruction(llvm::Instruction &i)
354 {
355 if (dyn_cast<llvm::LoadInst> (&i) ||
356 dyn_cast<llvm::StoreInst> (&i))
357 RegisterInstruction(i);
358
359 return true;
360 }
361
362 llvm::Value *m_valid_pointer_check_func;
Sean Callanane8a59a82010-09-13 21:34:21 +0000363};
364
365class ObjcObjectChecker : public Instrumenter
366{
367public:
368 ObjcObjectChecker(llvm::Module &module,
369 DynamicCheckerFunctions &checker_functions) :
370 Instrumenter(module, checker_functions),
371 m_objc_object_check_func(NULL)
372 {
373 }
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000374
375 virtual
376 ~ObjcObjectChecker ()
377 {
378 }
379
Sean Callanane8a59a82010-09-13 21:34:21 +0000380private:
381 bool InstrumentInstruction(llvm::Instruction *inst)
382 {
383 CallInst *call_inst = dyn_cast<CallInst>(inst);
384
385 if (!call_inst)
Sean Callanan14a97ff2010-11-04 01:51:38 +0000386 return false; // call_inst really shouldn't be NULL, because otherwise InspectInstruction wouldn't have registered it
Sean Callanane8a59a82010-09-13 21:34:21 +0000387
388 if (!m_objc_object_check_func)
389 m_objc_object_check_func = BuildPointerValidatorFunc(m_checker_functions.m_objc_object_check->StartAddress());
390
Sean Callanan58baaad2011-07-08 00:39:14 +0000391 llvm::Value *target_object = NULL;
Sean Callanane8a59a82010-09-13 21:34:21 +0000392
393 // id objc_msgSend(id theReceiver, SEL theSelector, ...)
394
395 target_object = call_inst->getArgOperand(0);
396
397 // Insert an instruction to cast the receiver id to int8_t*
398
399 BitCastInst *bit_cast = new BitCastInst(target_object,
Sean Callanan14a97ff2010-11-04 01:51:38 +0000400 GetI8PtrTy(),
Sean Callanane8a59a82010-09-13 21:34:21 +0000401 "",
402 inst);
403
404 // Insert an instruction to call the helper with the result
405
Sean Callanan9b6898f2011-07-30 02:42:06 +0000406 llvm::Value *arg_array[1];
407
408 arg_array[0] = bit_cast;
409
410 ArrayRef<llvm::Value*> args(arg_array, 1);
Sean Callanane8a59a82010-09-13 21:34:21 +0000411
412 CallInst::Create(m_objc_object_check_func,
Sean Callanan9b6898f2011-07-30 02:42:06 +0000413 args,
Sean Callanane8a59a82010-09-13 21:34:21 +0000414 "",
415 inst);
416
417 return true;
418 }
419
420 bool InspectInstruction(llvm::Instruction &i)
421 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000422 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanane8a59a82010-09-13 21:34:21 +0000423
424 CallInst *call_inst = dyn_cast<CallInst>(&i);
425
426 if (call_inst)
427 {
428 // This metadata is set by IRForTarget::MaybeHandleCall().
429
430 MDNode *metadata = call_inst->getMetadata("lldb.call.realName");
431
432 if (!metadata)
433 return true;
434
435 if (metadata->getNumOperands() != 1)
436 {
437 if (log)
438 log->Printf("Function call metadata has %d operands for [%p] %s", metadata->getNumOperands(), call_inst, PrintValue(call_inst).c_str());
439 return false;
440 }
441
442 ConstantArray *real_name = dyn_cast<ConstantArray>(metadata->getOperand(0));
443
444 if (!real_name)
445 {
446 if (log)
447 log->Printf("Function call metadata is not a ConstantArray for [%p] %s", call_inst, PrintValue(call_inst).c_str());
448 return false;
449 }
450
451 if (!real_name->isString())
452 {
453 if (log)
454 log->Printf("Function call metadata is not a string for [%p] %s", call_inst, PrintValue(call_inst).c_str());
455 return false;
456 }
457
458 if (log)
459 log->Printf("Found call to %s: %s\n", real_name->getAsString().c_str(), PrintValue(call_inst).c_str());
460
461 if (real_name->getAsString().find("objc_msgSend") != std::string::npos)
462 RegisterInstruction(i);
463 }
464
465 return true;
466 }
467
468 llvm::Value *m_objc_object_check_func;
Sean Callanan05262332010-09-02 00:37:32 +0000469};
470
471IRDynamicChecks::IRDynamicChecks(DynamicCheckerFunctions &checker_functions,
472 const char *func_name) :
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000473 ModulePass(ID),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +0000474 m_func_name(func_name),
475 m_checker_functions(checker_functions)
Sean Callanan05262332010-09-02 00:37:32 +0000476{
477}
478
Sean Callananf18d91c2010-09-01 00:58:00 +0000479IRDynamicChecks::~IRDynamicChecks()
480{
481}
482
483bool
484IRDynamicChecks::runOnModule(llvm::Module &M)
485{
Greg Claytone005f2c2010-11-06 01:53:30 +0000486 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananf18d91c2010-09-01 00:58:00 +0000487
488 llvm::Function* function = M.getFunction(StringRef(m_func_name.c_str()));
489
490 if (!function)
491 {
492 if (log)
493 log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
494
495 return false;
496 }
Sean Callanan05262332010-09-02 00:37:32 +0000497
498 ValidPointerChecker vpc(M, m_checker_functions);
Sean Callananf18d91c2010-09-01 00:58:00 +0000499
Sean Callanan05262332010-09-02 00:37:32 +0000500 if (!vpc.Inspect(*function))
501 return false;
Sean Callananf18d91c2010-09-01 00:58:00 +0000502
Sean Callanan05262332010-09-02 00:37:32 +0000503 if (!vpc.Instrument())
504 return false;
Sean Callananf18d91c2010-09-01 00:58:00 +0000505
Sean Callanane8a59a82010-09-13 21:34:21 +0000506 ObjcObjectChecker ooc(M, m_checker_functions);
507
508 if (!ooc.Inspect(*function))
509 return false;
510
511 if (!ooc.Instrument())
512 return false;
Jim Inghambbce7122011-01-22 01:25:40 +0000513
514 if (log && log->GetVerbose())
Sean Callanan65af7342010-09-08 20:04:08 +0000515 {
516 std::string s;
517 raw_string_ostream oss(s);
518
519 M.print(oss, NULL);
520
521 oss.flush();
522
Jim Inghambbce7122011-01-22 01:25:40 +0000523 log->Printf ("Module after dynamic checks: \n%s", s.c_str());
Sean Callanan65af7342010-09-08 20:04:08 +0000524 }
525
Sean Callananf18d91c2010-09-01 00:58:00 +0000526 return true;
527}
528
529void
530IRDynamicChecks::assignPassManager(PMStack &PMS,
531 PassManagerType T)
532{
533}
534
535PassManagerType
536IRDynamicChecks::getPotentialPassManagerType() const
537{
538 return PMT_ModulePassManager;
539}