1 //===-- IRDynamicChecks.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/IR/Constants.h"
11 #include "llvm/IR/DataLayout.h"
12 #include "llvm/IR/Function.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/IR/Value.h"
16 #include "llvm/Support/raw_ostream.h"
18 #include "lldb/Expression/IRDynamicChecks.h"
20 #include "lldb/Expression/UtilityFunction.h"
21 #include "lldb/Target/ExecutionContext.h"
22 #include "lldb/Target/ObjCLanguageRuntime.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/StackFrame.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Utility/ConstString.h"
27 #include "lldb/Utility/Log.h"
30 using namespace lldb_private;
34 #define VALID_POINTER_CHECK_NAME "_$__lldb_valid_pointer_check"
35 #define VALID_OBJC_OBJECT_CHECK_NAME "$__lldb_objc_object_check"
37 static const char g_valid_pointer_check_text[] =
39 "_$__lldb_valid_pointer_check (unsigned char *$__lldb_arg_ptr)\n"
41 " unsigned char $__lldb_local_val = *$__lldb_arg_ptr;\n"
44 DynamicCheckerFunctions::DynamicCheckerFunctions() = default;
46 DynamicCheckerFunctions::~DynamicCheckerFunctions() = default;
48 bool DynamicCheckerFunctions::Install(DiagnosticManager &diagnostic_manager,
49 ExecutionContext &exe_ctx) {
51 m_valid_pointer_check.reset(
52 exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(
53 g_valid_pointer_check_text, lldb::eLanguageTypeC,
54 VALID_POINTER_CHECK_NAME, error));
58 if (!m_valid_pointer_check->Install(diagnostic_manager, exe_ctx))
61 Process *process = exe_ctx.GetProcessPtr();
64 ObjCLanguageRuntime *objc_language_runtime =
65 process->GetObjCLanguageRuntime();
67 if (objc_language_runtime) {
68 m_objc_object_check.reset(objc_language_runtime->CreateObjectChecker(
69 VALID_OBJC_OBJECT_CHECK_NAME));
71 if (!m_objc_object_check->Install(diagnostic_manager, exe_ctx))
79 bool DynamicCheckerFunctions::DoCheckersExplainStop(lldb::addr_t addr,
81 // FIXME: We have to get the checkers to know why they scotched the call in
83 // so we can print a better message here.
84 if (m_valid_pointer_check && m_valid_pointer_check->ContainsAddress(addr)) {
85 message.Printf("Attempted to dereference an invalid pointer.");
87 } else if (m_objc_object_check &&
88 m_objc_object_check->ContainsAddress(addr)) {
89 message.Printf("Attempted to dereference an invalid ObjC Object or send it "
90 "an unrecognized selector");
96 static std::string PrintValue(llvm::Value *V, bool truncate = false) {
98 raw_string_ostream rso(s);
102 s.resize(s.length() - 1);
106 //----------------------------------------------------------------------
107 /// @class Instrumenter IRDynamicChecks.cpp
108 /// Finds and instruments individual LLVM IR instructions
110 /// When instrumenting LLVM IR, it is frequently desirable to first search for
111 /// instructions, and then later modify them. This way iterators remain
112 /// intact, and multiple passes can look at the same code base without
113 /// treading on each other's toes.
115 /// The Instrumenter class implements this functionality. A client first
116 /// calls Inspect on a function, which populates a list of instructions to be
117 /// instrumented. Then, later, when all passes' Inspect functions have been
118 /// called, the client calls Instrument, which adds the desired
121 /// A subclass of Instrumenter must override InstrumentInstruction, which
122 /// is responsible for adding whatever instrumentation is necessary.
124 /// A subclass of Instrumenter may override:
126 /// - InspectInstruction [default: does nothing]
128 /// - InspectBasicBlock [default: iterates through the instructions in a
129 /// basic block calling InspectInstruction]
131 /// - InspectFunction [default: iterates through the basic blocks in a
132 /// function calling InspectBasicBlock]
133 //----------------------------------------------------------------------
136 //------------------------------------------------------------------
139 /// @param[in] module
140 /// The module being instrumented.
141 //------------------------------------------------------------------
142 Instrumenter(llvm::Module &module, DynamicCheckerFunctions &checker_functions)
143 : m_module(module), m_checker_functions(checker_functions),
144 m_i8ptr_ty(nullptr), m_intptr_ty(nullptr) {}
146 virtual ~Instrumenter() = default;
148 //------------------------------------------------------------------
149 /// Inspect a function to find instructions to instrument
151 /// @param[in] function
152 /// The function to inspect.
155 /// True on success; false on error.
156 //------------------------------------------------------------------
157 bool Inspect(llvm::Function &function) { return InspectFunction(function); }
159 //------------------------------------------------------------------
160 /// Instrument all the instructions found by Inspect()
163 /// True on success; false on error.
164 //------------------------------------------------------------------
166 for (InstIterator ii = m_to_instrument.begin(),
167 last_ii = m_to_instrument.end();
168 ii != last_ii; ++ii) {
169 if (!InstrumentInstruction(*ii))
177 //------------------------------------------------------------------
178 /// Add instrumentation to a single instruction
181 /// The instruction to be instrumented.
184 /// True on success; false otherwise.
185 //------------------------------------------------------------------
186 virtual bool InstrumentInstruction(llvm::Instruction *inst) = 0;
188 //------------------------------------------------------------------
189 /// Register a single instruction to be instrumented
192 /// The instruction to be instrumented.
193 //------------------------------------------------------------------
194 void RegisterInstruction(llvm::Instruction &i) {
195 m_to_instrument.push_back(&i);
198 //------------------------------------------------------------------
199 /// Determine whether a single instruction is interesting to instrument,
200 /// and, if so, call RegisterInstruction
203 /// The instruction to be inspected.
206 /// False if there was an error scanning; true otherwise.
207 //------------------------------------------------------------------
208 virtual bool InspectInstruction(llvm::Instruction &i) { return true; }
210 //------------------------------------------------------------------
211 /// Scan a basic block to see if any instructions are interesting
214 /// The basic block to be inspected.
217 /// False if there was an error scanning; true otherwise.
218 //------------------------------------------------------------------
219 virtual bool InspectBasicBlock(llvm::BasicBlock &bb) {
220 for (llvm::BasicBlock::iterator ii = bb.begin(), last_ii = bb.end();
221 ii != last_ii; ++ii) {
222 if (!InspectInstruction(*ii))
229 //------------------------------------------------------------------
230 /// Scan a function to see if any instructions are interesting
233 /// The function to be inspected.
236 /// False if there was an error scanning; true otherwise.
237 //------------------------------------------------------------------
238 virtual bool InspectFunction(llvm::Function &f) {
239 for (llvm::Function::iterator bbi = f.begin(), last_bbi = f.end();
240 bbi != last_bbi; ++bbi) {
241 if (!InspectBasicBlock(*bbi))
248 //------------------------------------------------------------------
249 /// Build a function pointer for a function with signature void
250 /// (*)(uint8_t*) with a given address
252 /// @param[in] start_address
253 /// The address of the function.
256 /// The function pointer, for use in a CallInst.
257 //------------------------------------------------------------------
258 llvm::Value *BuildPointerValidatorFunc(lldb::addr_t start_address) {
259 llvm::Type *param_array[1];
261 param_array[0] = const_cast<llvm::PointerType *>(GetI8PtrTy());
263 ArrayRef<llvm::Type *> params(param_array, 1);
265 FunctionType *fun_ty = FunctionType::get(
266 llvm::Type::getVoidTy(m_module.getContext()), params, true);
267 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
268 Constant *fun_addr_int =
269 ConstantInt::get(GetIntptrTy(), start_address, false);
270 return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
273 //------------------------------------------------------------------
274 /// Build a function pointer for a function with signature void
275 /// (*)(uint8_t*, uint8_t*) with a given address
277 /// @param[in] start_address
278 /// The address of the function.
281 /// The function pointer, for use in a CallInst.
282 //------------------------------------------------------------------
283 llvm::Value *BuildObjectCheckerFunc(lldb::addr_t start_address) {
284 llvm::Type *param_array[2];
286 param_array[0] = const_cast<llvm::PointerType *>(GetI8PtrTy());
287 param_array[1] = const_cast<llvm::PointerType *>(GetI8PtrTy());
289 ArrayRef<llvm::Type *> params(param_array, 2);
291 FunctionType *fun_ty = FunctionType::get(
292 llvm::Type::getVoidTy(m_module.getContext()), params, true);
293 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
294 Constant *fun_addr_int =
295 ConstantInt::get(GetIntptrTy(), start_address, false);
296 return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
299 PointerType *GetI8PtrTy() {
301 m_i8ptr_ty = llvm::Type::getInt8PtrTy(m_module.getContext());
306 IntegerType *GetIntptrTy() {
308 llvm::DataLayout data_layout(&m_module);
310 m_intptr_ty = llvm::Type::getIntNTy(m_module.getContext(),
311 data_layout.getPointerSizeInBits());
317 typedef std::vector<llvm::Instruction *> InstVector;
318 typedef InstVector::iterator InstIterator;
320 InstVector m_to_instrument; ///< List of instructions the inspector found
321 llvm::Module &m_module; ///< The module which is being instrumented
322 DynamicCheckerFunctions
323 &m_checker_functions; ///< The dynamic checker functions for the process
326 PointerType *m_i8ptr_ty;
327 IntegerType *m_intptr_ty;
330 class ValidPointerChecker : public Instrumenter {
332 ValidPointerChecker(llvm::Module &module,
333 DynamicCheckerFunctions &checker_functions)
334 : Instrumenter(module, checker_functions),
335 m_valid_pointer_check_func(nullptr) {}
337 ~ValidPointerChecker() override = default;
340 bool InstrumentInstruction(llvm::Instruction *inst) override {
341 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
344 log->Printf("Instrumenting load/store instruction: %s\n",
345 PrintValue(inst).c_str());
347 if (!m_valid_pointer_check_func)
348 m_valid_pointer_check_func = BuildPointerValidatorFunc(
349 m_checker_functions.m_valid_pointer_check->StartAddress());
351 llvm::Value *dereferenced_ptr = nullptr;
353 if (llvm::LoadInst *li = dyn_cast<llvm::LoadInst>(inst))
354 dereferenced_ptr = li->getPointerOperand();
355 else if (llvm::StoreInst *si = dyn_cast<llvm::StoreInst>(inst))
356 dereferenced_ptr = si->getPointerOperand();
360 // Insert an instruction to cast the loaded value to int8_t*
362 BitCastInst *bit_cast =
363 new BitCastInst(dereferenced_ptr, GetI8PtrTy(), "", inst);
365 // Insert an instruction to call the helper with the result
367 llvm::Value *arg_array[1];
369 arg_array[0] = bit_cast;
371 llvm::ArrayRef<llvm::Value *> args(arg_array, 1);
373 CallInst::Create(m_valid_pointer_check_func, args, "", inst);
378 bool InspectInstruction(llvm::Instruction &i) override {
379 if (dyn_cast<llvm::LoadInst>(&i) || dyn_cast<llvm::StoreInst>(&i))
380 RegisterInstruction(i);
386 llvm::Value *m_valid_pointer_check_func;
389 class ObjcObjectChecker : public Instrumenter {
391 ObjcObjectChecker(llvm::Module &module,
392 DynamicCheckerFunctions &checker_functions)
393 : Instrumenter(module, checker_functions),
394 m_objc_object_check_func(nullptr) {}
396 ~ObjcObjectChecker() override = default;
406 std::map<llvm::Instruction *, msgSend_type> msgSend_types;
409 bool InstrumentInstruction(llvm::Instruction *inst) override {
410 CallInst *call_inst = dyn_cast<CallInst>(inst);
413 return false; // call_inst really shouldn't be nullptr, because otherwise
414 // InspectInstruction wouldn't have registered it
416 if (!m_objc_object_check_func)
417 m_objc_object_check_func = BuildObjectCheckerFunc(
418 m_checker_functions.m_objc_object_check->StartAddress());
420 // id objc_msgSend(id theReceiver, SEL theSelector, ...)
422 llvm::Value *target_object;
423 llvm::Value *selector;
425 switch (msgSend_types[inst]) {
428 target_object = call_inst->getArgOperand(0);
429 selector = call_inst->getArgOperand(1);
432 target_object = call_inst->getArgOperand(1);
433 selector = call_inst->getArgOperand(2);
436 case eMsgSendSuper_stret:
440 // These objects should always be valid according to Sean Calannan
441 assert(target_object);
444 // Insert an instruction to cast the receiver id to int8_t*
446 BitCastInst *bit_cast =
447 new BitCastInst(target_object, GetI8PtrTy(), "", inst);
449 // Insert an instruction to call the helper with the result
451 llvm::Value *arg_array[2];
453 arg_array[0] = bit_cast;
454 arg_array[1] = selector;
456 ArrayRef<llvm::Value *> args(arg_array, 2);
458 CallInst::Create(m_objc_object_check_func, args, "", inst);
463 static llvm::Function *GetFunction(llvm::Value *value) {
464 if (llvm::Function *function = llvm::dyn_cast<llvm::Function>(value)) {
468 if (llvm::ConstantExpr *const_expr =
469 llvm::dyn_cast<llvm::ConstantExpr>(value)) {
470 switch (const_expr->getOpcode()) {
473 case llvm::Instruction::BitCast:
474 return GetFunction(const_expr->getOperand(0));
481 static llvm::Function *GetCalledFunction(llvm::CallInst *inst) {
482 return GetFunction(inst->getCalledValue());
485 bool InspectInstruction(llvm::Instruction &i) override {
486 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
488 CallInst *call_inst = dyn_cast<CallInst>(&i);
491 const llvm::Function *called_function = GetCalledFunction(call_inst);
493 if (!called_function)
496 std::string name_str = called_function->getName().str();
497 const char *name_cstr = name_str.c_str();
500 log->Printf("Found call to %s: %s\n", name_cstr,
501 PrintValue(call_inst).c_str());
503 if (name_str.find("objc_msgSend") == std::string::npos)
506 if (!strcmp(name_cstr, "objc_msgSend")) {
507 RegisterInstruction(i);
508 msgSend_types[&i] = eMsgSend;
512 if (!strcmp(name_cstr, "objc_msgSend_stret")) {
513 RegisterInstruction(i);
514 msgSend_types[&i] = eMsgSend_stret;
518 if (!strcmp(name_cstr, "objc_msgSend_fpret")) {
519 RegisterInstruction(i);
520 msgSend_types[&i] = eMsgSend_fpret;
524 if (!strcmp(name_cstr, "objc_msgSendSuper")) {
525 RegisterInstruction(i);
526 msgSend_types[&i] = eMsgSendSuper;
530 if (!strcmp(name_cstr, "objc_msgSendSuper_stret")) {
531 RegisterInstruction(i);
532 msgSend_types[&i] = eMsgSendSuper_stret;
538 "Function name '%s' contains 'objc_msgSend' but is not handled",
548 llvm::Value *m_objc_object_check_func;
551 IRDynamicChecks::IRDynamicChecks(DynamicCheckerFunctions &checker_functions,
552 const char *func_name)
553 : ModulePass(ID), m_func_name(func_name),
554 m_checker_functions(checker_functions) {}
556 IRDynamicChecks::~IRDynamicChecks() = default;
558 bool IRDynamicChecks::runOnModule(llvm::Module &M) {
559 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
561 llvm::Function *function = M.getFunction(StringRef(m_func_name));
565 log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
570 if (m_checker_functions.m_valid_pointer_check) {
571 ValidPointerChecker vpc(M, m_checker_functions);
573 if (!vpc.Inspect(*function))
576 if (!vpc.Instrument())
580 if (m_checker_functions.m_objc_object_check) {
581 ObjcObjectChecker ooc(M, m_checker_functions);
583 if (!ooc.Inspect(*function))
586 if (!ooc.Instrument())
590 if (log && log->GetVerbose()) {
592 raw_string_ostream oss(s);
594 M.print(oss, nullptr);
598 log->Printf("Module after dynamic checks: \n%s", s.c_str());
604 void IRDynamicChecks::assignPassManager(PMStack &PMS, PassManagerType T) {}
606 PassManagerType IRDynamicChecks::getPotentialPassManagerType() const {
607 return PMT_ModulePassManager;