[Lldb-commits] [lldb] r365853 - [Expression] Move IRDynamicChecks to ClangExpressionParser

Alex Langford via lldb-commits lldb-commits at lists.llvm.org
Thu Jul 11 17:58:03 PDT 2019


Author: xiaobai
Date: Thu Jul 11 17:58:02 2019
New Revision: 365853

URL: http://llvm.org/viewvc/llvm-project?rev=365853&view=rev
Log:
[Expression] Move IRDynamicChecks to ClangExpressionParser

Summary:
IRDynamicChecks in its current form is specific to Clang since it deals
with the C language family. It is possible that we may want to
instrument code generated for other languages, but we can factor in a
more general mechanism to do so at a later time.

This decouples ObCLanguageRuntime from Expression!

Reviewers: compnerd, clayborg, jingham, JDevlieghere

Subscribers: mgorny, lldb-commits

Differential Revision: https://reviews.llvm.org/D64591

Added:
    lldb/trunk/include/lldb/Expression/DynamicCheckerFunctions.h
    lldb/trunk/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp
      - copied, changed from r365843, lldb/trunk/source/Expression/IRDynamicChecks.cpp
    lldb/trunk/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h
      - copied, changed from r365843, lldb/trunk/include/lldb/Expression/IRDynamicChecks.h
Removed:
    lldb/trunk/include/lldb/Expression/IRDynamicChecks.h
    lldb/trunk/source/Expression/IRDynamicChecks.cpp
Modified:
    lldb/trunk/source/Expression/CMakeLists.txt
    lldb/trunk/source/Plugins/ExpressionParser/Clang/CMakeLists.txt
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
    lldb/trunk/source/Target/Process.cpp
    lldb/trunk/source/Target/ThreadPlanCallUserExpression.cpp

Added: lldb/trunk/include/lldb/Expression/DynamicCheckerFunctions.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/DynamicCheckerFunctions.h?rev=365853&view=auto
==============================================================================
--- lldb/trunk/include/lldb/Expression/DynamicCheckerFunctions.h (added)
+++ lldb/trunk/include/lldb/Expression/DynamicCheckerFunctions.h Thu Jul 11 17:58:02 2019
@@ -0,0 +1,62 @@
+//===-- DynamicCheckerFunctions.h -------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef liblldb_DynamicCheckerFunctions_h_
+#define liblldb_DynamicCheckerFunctions_h_
+
+#include "lldb/lldb-types.h"
+
+namespace lldb_private {
+
+class DiagnosticManager;
+class ExecutionContext;
+
+/// Encapsulates dynamic check functions used by expressions.
+///
+/// Each of the utility functions encapsulated in this class is responsible
+/// for validating some data that an expression is about to use.  Examples
+/// are:
+///
+/// a = *b;     // check that b is a valid pointer
+/// [b init];   // check that b is a valid object to send "init" to
+///
+/// The class installs each checker function into the target process and makes
+/// it available to IRDynamicChecks to use.
+class DynamicCheckerFunctions {
+public:
+  enum DynamicCheckerFunctionsKind {
+    DCF_Clang,
+  };
+
+  DynamicCheckerFunctions(DynamicCheckerFunctionsKind kind) : m_kind(kind) {}
+  virtual ~DynamicCheckerFunctions() = default;
+
+  /// Install the utility functions into a process.  This binds the instance
+  /// of DynamicCheckerFunctions to that process.
+  ///
+  /// \param[in] diagnostic_manager
+  ///     A diagnostic manager to report errors to.
+  ///
+  /// \param[in] exe_ctx
+  ///     The execution context to install the functions into.
+  ///
+  /// \return
+  ///     True on success; false on failure, or if the functions have
+  ///     already been installed.
+  virtual bool Install(DiagnosticManager &diagnostic_manager,
+                       ExecutionContext &exe_ctx) = 0;
+  virtual bool DoCheckersExplainStop(lldb::addr_t addr, Stream &message) = 0;
+
+  DynamicCheckerFunctionsKind GetKind() const { return m_kind; }
+
+private:
+  const DynamicCheckerFunctionsKind m_kind;
+};
+} // namespace lldb_private
+
+#endif // liblldb_DynamicCheckerFunctions_h_

Removed: lldb/trunk/include/lldb/Expression/IRDynamicChecks.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/IRDynamicChecks.h?rev=365852&view=auto
==============================================================================
--- lldb/trunk/include/lldb/Expression/IRDynamicChecks.h (original)
+++ lldb/trunk/include/lldb/Expression/IRDynamicChecks.h (removed)
@@ -1,145 +0,0 @@
-//===-- IRDynamicChecks.h ---------------------------------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef liblldb_IRDynamicChecks_h_
-#define liblldb_IRDynamicChecks_h_
-
-#include "lldb/lldb-types.h"
-#include "llvm/Pass.h"
-
-namespace llvm {
-class BasicBlock;
-class CallInst;
-class Constant;
-class Function;
-class Instruction;
-class Module;
-class DataLayout;
-class Value;
-}
-
-namespace lldb_private {
-
-class ClangExpressionDeclMap;
-class ExecutionContext;
-class Stream;
-
-/// \class DynamicCheckerFunctions IRDynamicChecks.h
-/// "lldb/Expression/IRDynamicChecks.h" Encapsulates dynamic check functions
-/// used by expressions.
-///
-/// Each of the utility functions encapsulated in this class is responsible
-/// for validating some data that an expression is about to use.  Examples
-/// are:
-///
-/// a = *b;     // check that b is a valid pointer [b init];   // check that b
-/// is a valid object to send "init" to
-///
-/// The class installs each checker function into the target process and makes
-/// it available to IRDynamicChecks to use.
-class DynamicCheckerFunctions {
-public:
-  /// Constructor
-  DynamicCheckerFunctions();
-
-  /// Destructor
-  ~DynamicCheckerFunctions();
-
-  /// Install the utility functions into a process.  This binds the instance
-  /// of DynamicCheckerFunctions to that process.
-  ///
-  /// \param[in] diagnostic_manager
-  ///     A diagnostic manager to report errors to.
-  ///
-  /// \param[in] exe_ctx
-  ///     The execution context to install the functions into.
-  ///
-  /// \return
-  ///     True on success; false on failure, or if the functions have
-  ///     already been installed.
-  bool Install(DiagnosticManager &diagnostic_manager,
-               ExecutionContext &exe_ctx);
-
-  bool DoCheckersExplainStop(lldb::addr_t addr, Stream &message);
-
-  std::shared_ptr<UtilityFunction> m_valid_pointer_check;
-  std::shared_ptr<UtilityFunction> m_objc_object_check;
-};
-
-/// \class IRDynamicChecks IRDynamicChecks.h
-/// "lldb/Expression/IRDynamicChecks.h" Adds dynamic checks to a user-entered
-/// expression to reduce its likelihood of crashing
-///
-/// When an IR function is executed in the target process, it may cause
-/// crashes or hangs by dereferencing NULL pointers, trying to call
-/// Objective-C methods on objects that do not respond to them, and so forth.
-///
-/// IRDynamicChecks adds calls to the functions in DynamicCheckerFunctions to
-/// appropriate locations in an expression's IR.
-class IRDynamicChecks : public llvm::ModulePass {
-public:
-  /// Constructor
-  ///
-  /// \param[in] checker_functions
-  ///     The checker functions for the target process.
-  ///
-  /// \param[in] func_name
-  ///     The name of the function to prepare for execution in the target.
-  ///
-  /// \param[in] decl_map
-  ///     The mapping used to look up entities in the target process. In
-  ///     this case, used to find objc_msgSend
-  IRDynamicChecks(DynamicCheckerFunctions &checker_functions,
-                  const char *func_name = "$__lldb_expr");
-
-  /// Destructor
-  ~IRDynamicChecks() override;
-
-  /// Run this IR transformer on a single module
-  ///
-  /// \param[in] M
-  ///     The module to run on.  This module is searched for the function
-  ///     $__lldb_expr, and that function is passed to the passes one by
-  ///     one.
-  ///
-  /// \return
-  ///     True on success; false otherwise
-  bool runOnModule(llvm::Module &M) override;
-
-  /// Interface stub
-  void assignPassManager(
-      llvm::PMStack &PMS,
-      llvm::PassManagerType T = llvm::PMT_ModulePassManager) override;
-
-  /// Returns PMT_ModulePassManager
-  llvm::PassManagerType getPotentialPassManagerType() const override;
-
-private:
-  /// A basic block-level pass to find all pointer dereferences and
-  /// validate them before use.
-
-  /// The top-level pass implementation
-  ///
-  /// \param[in] M
-  ///     The module currently being processed.
-  ///
-  /// \param[in] BB
-  ///     The basic block currently being processed.
-  ///
-  /// \return
-  ///     True on success; false otherwise
-  bool FindDataLoads(llvm::Module &M, llvm::BasicBlock &BB);
-
-  std::string m_func_name; ///< The name of the function to add checks to
-  DynamicCheckerFunctions
-      &m_checker_functions; ///< The checker functions for the process
-};
-
-} // namespace lldb_private
-
-#endif // liblldb_IRDynamicChecks_h_

Modified: lldb/trunk/source/Expression/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/CMakeLists.txt?rev=365853&r1=365852&r2=365853&view=diff
==============================================================================
--- lldb/trunk/source/Expression/CMakeLists.txt (original)
+++ lldb/trunk/source/Expression/CMakeLists.txt Thu Jul 11 17:58:02 2019
@@ -8,7 +8,6 @@ add_lldb_library(lldbExpression
   Expression.cpp
   ExpressionVariable.cpp
   FunctionCaller.cpp
-  IRDynamicChecks.cpp
   IRExecutionUnit.cpp
   IRInterpreter.cpp
   IRMemoryMap.cpp

Removed: lldb/trunk/source/Expression/IRDynamicChecks.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/IRDynamicChecks.cpp?rev=365852&view=auto
==============================================================================
--- lldb/trunk/source/Expression/IRDynamicChecks.cpp (original)
+++ lldb/trunk/source/Expression/IRDynamicChecks.cpp (removed)
@@ -1,593 +0,0 @@
-//===-- IRDynamicChecks.cpp -------------------------------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#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"
-
-#include "lldb/Expression/IRDynamicChecks.h"
-
-#include "lldb/Expression/UtilityFunction.h"
-#include "lldb/Target/ExecutionContext.h"
-#include "lldb/Target/ObjCLanguageRuntime.h"
-#include "lldb/Target/Process.h"
-#include "lldb/Target/StackFrame.h"
-#include "lldb/Target/Target.h"
-#include "lldb/Utility/ConstString.h"
-#include "lldb/Utility/Log.h"
-
-using namespace llvm;
-using namespace lldb_private;
-
-static char ID;
-
-#define VALID_POINTER_CHECK_NAME "_$__lldb_valid_pointer_check"
-#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"
-    "}";
-
-DynamicCheckerFunctions::DynamicCheckerFunctions() = default;
-
-DynamicCheckerFunctions::~DynamicCheckerFunctions() = default;
-
-bool DynamicCheckerFunctions::Install(DiagnosticManager &diagnostic_manager,
-                                      ExecutionContext &exe_ctx) {
-  Status 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;
-
-  Process *process = exe_ctx.GetProcessPtr();
-
-  if (process) {
-    ObjCLanguageRuntime *objc_language_runtime =
-        ObjCLanguageRuntime::Get(*process);
-
-    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;
-    }
-  }
-
-  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;
-}
-
-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;
-}
-
-/// \class Instrumenter IRDynamicChecks.cpp
-/// Finds and instruments individual LLVM IR instructions
-///
-/// When instrumenting LLVM IR, it is frequently desirable to first search for
-/// instructions, and then later modify them.  This way iterators remain
-/// intact, and multiple passes can look at the same code base without
-/// treading on each other's toes.
-///
-/// The Instrumenter class implements this functionality.  A client first
-/// calls Inspect on a function, which populates a list of instructions to be
-/// instrumented.  Then, later, when all passes' Inspect functions have been
-/// called, the client calls Instrument, which adds the desired
-/// instrumentation.
-///
-/// A subclass of Instrumenter must override InstrumentInstruction, which
-/// is responsible for adding whatever instrumentation is necessary.
-///
-/// A subclass of Instrumenter may override:
-///
-/// - InspectInstruction [default: does nothing]
-///
-/// - InspectBasicBlock [default: iterates through the instructions in a
-///   basic block calling InspectInstruction]
-///
-/// - InspectFunction [default: iterates through the basic blocks in a
-///   function calling InspectBasicBlock]
-class Instrumenter {
-public:
-  /// Constructor
-  ///
-  /// \param[in] module
-  ///     The module being instrumented.
-  Instrumenter(llvm::Module &module,
-               std::shared_ptr<UtilityFunction> checker_function)
-      : m_module(module), m_checker_function(checker_function),
-        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;
-    }
-
-    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);
-  }
-
-  /// 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;
-    }
-
-    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;
-    }
-
-    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::FunctionCallee 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 {fun_ty, 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::FunctionCallee 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 {fun_ty, 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());
-    }
-
-    return m_intptr_ty;
-  }
-
-  typedef std::vector<llvm::Instruction *> InstVector;
-  typedef InstVector::iterator InstIterator;
-
-  InstVector m_to_instrument; ///< List of instructions the inspector found
-  llvm::Module &m_module;     ///< The module which is being instrumented
-  std::shared_ptr<UtilityFunction>
-      m_checker_function; ///< The dynamic checker function for the process
-
-private:
-  PointerType *m_i8ptr_ty;
-  IntegerType *m_intptr_ty;
-};
-
-class ValidPointerChecker : public Instrumenter {
-public:
-  ValidPointerChecker(llvm::Module &module,
-                      std::shared_ptr<UtilityFunction> checker_function)
-      : Instrumenter(module, checker_function),
-        m_valid_pointer_check_func(nullptr) {}
-
-  ~ValidPointerChecker() override = default;
-
-protected:
-  bool InstrumentInstruction(llvm::Instruction *inst) override {
-    Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
-
-    if (log)
-      log->Printf("Instrumenting load/store instruction: %s\n",
-                  PrintValue(inst).c_str());
-
-    if (!m_valid_pointer_check_func)
-      m_valid_pointer_check_func =
-          BuildPointerValidatorFunc(m_checker_function->StartAddress());
-
-    llvm::Value *dereferenced_ptr = nullptr;
-
-    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;
-
-    // Insert an instruction to cast the loaded value to int8_t*
-
-    BitCastInst *bit_cast =
-        new BitCastInst(dereferenced_ptr, GetI8PtrTy(), "", inst);
-
-    // Insert an instruction to call the helper with the result
-
-    llvm::Value *arg_array[1];
-
-    arg_array[0] = bit_cast;
-
-    llvm::ArrayRef<llvm::Value *> args(arg_array, 1);
-
-    CallInst::Create(m_valid_pointer_check_func, args, "", inst);
-
-    return true;
-  }
-
-  bool InspectInstruction(llvm::Instruction &i) override {
-    if (dyn_cast<llvm::LoadInst>(&i) || dyn_cast<llvm::StoreInst>(&i))
-      RegisterInstruction(i);
-
-    return true;
-  }
-
-private:
-  llvm::FunctionCallee m_valid_pointer_check_func;
-};
-
-class ObjcObjectChecker : public Instrumenter {
-public:
-  ObjcObjectChecker(llvm::Module &module,
-                    std::shared_ptr<UtilityFunction> checker_function)
-      : Instrumenter(module, checker_function),
-        m_objc_object_check_func(nullptr) {}
-
-  ~ObjcObjectChecker() override = default;
-
-  enum msgSend_type {
-    eMsgSend = 0,
-    eMsgSendSuper,
-    eMsgSendSuper_stret,
-    eMsgSend_fpret,
-    eMsgSend_stret
-  };
-
-  std::map<llvm::Instruction *, msgSend_type> msgSend_types;
-
-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_function->StartAddress());
-
-    // id objc_msgSend(id theReceiver, SEL theSelector, ...)
-
-    llvm::Value *target_object;
-    llvm::Value *selector;
-
-    switch (msgSend_types[inst]) {
-    case eMsgSend:
-    case eMsgSend_fpret:
-      // On arm64, clang uses objc_msgSend for scalar and struct return
-      // calls.  The call instruction will record which was used.
-      if (call_inst->hasStructRetAttr()) {
-        target_object = call_inst->getArgOperand(1);
-        selector = call_inst->getArgOperand(2);
-      } else {
-        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;
-    }
-
-    // These objects should always be valid according to Sean Calannan
-    assert(target_object);
-    assert(selector);
-
-    // Insert an instruction to cast the receiver id to int8_t*
-
-    BitCastInst *bit_cast =
-        new BitCastInst(target_object, GetI8PtrTy(), "", inst);
-
-    // Insert an instruction to call the helper with the result
-
-    llvm::Value *arg_array[2];
-
-    arg_array[0] = bit_cast;
-    arg_array[1] = selector;
-
-    ArrayRef<llvm::Value *> args(arg_array, 2);
-
-    CallInst::Create(m_objc_object_check_func, args, "", inst);
-
-    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;
-    }
-
-    return true;
-  }
-
-private:
-  llvm::FunctionCallee 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) {}
-
-IRDynamicChecks::~IRDynamicChecks() = default;
-
-bool IRDynamicChecks::runOnModule(llvm::Module &M) {
-  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
-
-  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());
-
-    return false;
-  }
-
-  if (m_checker_functions.m_valid_pointer_check) {
-    ValidPointerChecker vpc(M, m_checker_functions.m_valid_pointer_check);
-
-    if (!vpc.Inspect(*function))
-      return false;
-
-    if (!vpc.Instrument())
-      return false;
-  }
-
-  if (m_checker_functions.m_objc_object_check) {
-    ObjcObjectChecker ooc(M, m_checker_functions.m_objc_object_check);
-
-    if (!ooc.Inspect(*function))
-      return false;
-
-    if (!ooc.Instrument())
-      return false;
-  }
-
-  if (log && log->GetVerbose()) {
-    std::string s;
-    raw_string_ostream oss(s);
-
-    M.print(oss, nullptr);
-
-    oss.flush();
-
-    log->Printf("Module after dynamic checks: \n%s", s.c_str());
-  }
-
-  return true;
-}
-
-void IRDynamicChecks::assignPassManager(PMStack &PMS, PassManagerType T) {}
-
-PassManagerType IRDynamicChecks::getPotentialPassManagerType() const {
-  return PMT_ModulePassManager;
-}

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/CMakeLists.txt?rev=365853&r1=365852&r2=365853&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/CMakeLists.txt (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/CMakeLists.txt Thu Jul 11 17:58:02 2019
@@ -19,6 +19,7 @@ add_lldb_library(lldbPluginExpressionPar
   ClangUserExpression.cpp
   ClangUtilityFunction.cpp
   IRForTarget.cpp
+  IRDynamicChecks.cpp
 
   DEPENDS
   ${tablegen_deps}

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp?rev=365853&r1=365852&r2=365853&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp Thu Jul 11 17:58:02 2019
@@ -62,6 +62,7 @@
 #include "ClangHost.h"
 #include "ClangModulesDeclVendor.h"
 #include "ClangPersistentVariables.h"
+#include "IRDynamicChecks.h"
 #include "IRForTarget.h"
 #include "ModuleDependencyCollector.h"
 
@@ -69,7 +70,6 @@
 #include "lldb/Core/Disassembler.h"
 #include "lldb/Core/Module.h"
 #include "lldb/Core/StreamFile.h"
-#include "lldb/Expression/IRDynamicChecks.h"
 #include "lldb/Expression/IRExecutionUnit.h"
 #include "lldb/Expression/IRInterpreter.h"
 #include "lldb/Host/File.h"
@@ -1281,8 +1281,8 @@ lldb_private::Status ClangExpressionPars
         (execution_policy != eExecutionPolicyTopLevel && !can_interpret)) {
       if (m_expr.NeedsValidation() && process) {
         if (!process->GetDynamicCheckers()) {
-          DynamicCheckerFunctions *dynamic_checkers =
-              new DynamicCheckerFunctions();
+          ClangDynamicCheckerFunctions *dynamic_checkers =
+              new ClangDynamicCheckerFunctions();
 
           DiagnosticManager install_diagnostics;
 
@@ -1302,23 +1302,26 @@ lldb_private::Status ClangExpressionPars
                         "Finished installing dynamic checkers ==");
         }
 
-        IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(),
-                                          function_name.AsCString());
-
-        llvm::Module *module = execution_unit_sp->GetModule();
-        if (!module || !ir_dynamic_checks.runOnModule(*module)) {
-          err.SetErrorToGenericError();
-          err.SetErrorString("Couldn't add dynamic checks to the expression");
-          return err;
-        }
+        if (auto *checker_funcs = llvm::dyn_cast<ClangDynamicCheckerFunctions>(
+                process->GetDynamicCheckers())) {
+          IRDynamicChecks ir_dynamic_checks(*checker_funcs,
+                                            function_name.AsCString());
+
+          llvm::Module *module = execution_unit_sp->GetModule();
+          if (!module || !ir_dynamic_checks.runOnModule(*module)) {
+            err.SetErrorToGenericError();
+            err.SetErrorString("Couldn't add dynamic checks to the expression");
+            return err;
+          }
 
-        if (custom_passes.LatePasses) {
-          if (log)
-            log->Printf("%s - Running Late IR Passes from LanguageRuntime on "
-                        "expression module '%s'",
-                        __FUNCTION__, m_expr.FunctionName());
+          if (custom_passes.LatePasses) {
+            if (log)
+              log->Printf("%s - Running Late IR Passes from LanguageRuntime on "
+                          "expression module '%s'",
+                          __FUNCTION__, m_expr.FunctionName());
 
-          custom_passes.LatePasses->run(*module);
+            custom_passes.LatePasses->run(*module);
+          }
         }
       }
     }

Copied: lldb/trunk/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp (from r365843, lldb/trunk/source/Expression/IRDynamicChecks.cpp)
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp?p2=lldb/trunk/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp&p1=lldb/trunk/source/Expression/IRDynamicChecks.cpp&r1=365843&r2=365853&rev=365853&view=diff
==============================================================================
--- lldb/trunk/source/Expression/IRDynamicChecks.cpp (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp Thu Jul 11 17:58:02 2019
@@ -14,7 +14,7 @@
 #include "llvm/IR/Value.h"
 #include "llvm/Support/raw_ostream.h"
 
-#include "lldb/Expression/IRDynamicChecks.h"
+#include "IRDynamicChecks.h"
 
 #include "lldb/Expression/UtilityFunction.h"
 #include "lldb/Target/ExecutionContext.h"
@@ -40,12 +40,13 @@ static const char g_valid_pointer_check_
     "    unsigned char $__lldb_local_val = *$__lldb_arg_ptr;\n"
     "}";
 
-DynamicCheckerFunctions::DynamicCheckerFunctions() = default;
+ClangDynamicCheckerFunctions::ClangDynamicCheckerFunctions()
+    : DynamicCheckerFunctions(DCF_Clang) {}
 
-DynamicCheckerFunctions::~DynamicCheckerFunctions() = default;
+ClangDynamicCheckerFunctions::~ClangDynamicCheckerFunctions() = default;
 
-bool DynamicCheckerFunctions::Install(DiagnosticManager &diagnostic_manager,
-                                      ExecutionContext &exe_ctx) {
+bool ClangDynamicCheckerFunctions::Install(
+    DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx) {
   Status error;
   m_valid_pointer_check.reset(
       exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(
@@ -75,8 +76,8 @@ bool DynamicCheckerFunctions::Install(Di
   return true;
 }
 
-bool DynamicCheckerFunctions::DoCheckersExplainStop(lldb::addr_t addr,
-                                                    Stream &message) {
+bool ClangDynamicCheckerFunctions::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.
@@ -533,8 +534,8 @@ private:
   llvm::FunctionCallee m_objc_object_check_func;
 };
 
-IRDynamicChecks::IRDynamicChecks(DynamicCheckerFunctions &checker_functions,
-                                 const char *func_name)
+IRDynamicChecks::IRDynamicChecks(
+    ClangDynamicCheckerFunctions &checker_functions, const char *func_name)
     : ModulePass(ID), m_func_name(func_name),
       m_checker_functions(checker_functions) {}
 

Copied: lldb/trunk/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h (from r365843, lldb/trunk/include/lldb/Expression/IRDynamicChecks.h)
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h?p2=lldb/trunk/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h&p1=lldb/trunk/include/lldb/Expression/IRDynamicChecks.h&r1=365843&r2=365853&rev=365853&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/IRDynamicChecks.h (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h Thu Jul 11 17:58:02 2019
@@ -9,46 +9,32 @@
 #ifndef liblldb_IRDynamicChecks_h_
 #define liblldb_IRDynamicChecks_h_
 
+#include "lldb/Expression/DynamicCheckerFunctions.h"
 #include "lldb/lldb-types.h"
 #include "llvm/Pass.h"
 
 namespace llvm {
 class BasicBlock;
-class CallInst;
-class Constant;
-class Function;
-class Instruction;
 class Module;
-class DataLayout;
-class Value;
 }
 
 namespace lldb_private {
 
-class ClangExpressionDeclMap;
 class ExecutionContext;
 class Stream;
 
-/// \class DynamicCheckerFunctions IRDynamicChecks.h
-/// "lldb/Expression/IRDynamicChecks.h" Encapsulates dynamic check functions
-/// used by expressions.
-///
-/// Each of the utility functions encapsulated in this class is responsible
-/// for validating some data that an expression is about to use.  Examples
-/// are:
-///
-/// a = *b;     // check that b is a valid pointer [b init];   // check that b
-/// is a valid object to send "init" to
-///
-/// The class installs each checker function into the target process and makes
-/// it available to IRDynamicChecks to use.
-class DynamicCheckerFunctions {
+class ClangDynamicCheckerFunctions
+    : public lldb_private::DynamicCheckerFunctions {
 public:
   /// Constructor
-  DynamicCheckerFunctions();
+  ClangDynamicCheckerFunctions();
 
   /// Destructor
-  ~DynamicCheckerFunctions();
+  virtual ~ClangDynamicCheckerFunctions();
+
+  static bool classof(const DynamicCheckerFunctions *checker_funcs) {
+    return checker_funcs->GetKind() == DCF_Clang;
+  }
 
   /// Install the utility functions into a process.  This binds the instance
   /// of DynamicCheckerFunctions to that process.
@@ -63,9 +49,9 @@ public:
   ///     True on success; false on failure, or if the functions have
   ///     already been installed.
   bool Install(DiagnosticManager &diagnostic_manager,
-               ExecutionContext &exe_ctx);
+               ExecutionContext &exe_ctx) override;
 
-  bool DoCheckersExplainStop(lldb::addr_t addr, Stream &message);
+  bool DoCheckersExplainStop(lldb::addr_t addr, Stream &message) override;
 
   std::shared_ptr<UtilityFunction> m_valid_pointer_check;
   std::shared_ptr<UtilityFunction> m_objc_object_check;
@@ -94,7 +80,7 @@ public:
   /// \param[in] decl_map
   ///     The mapping used to look up entities in the target process. In
   ///     this case, used to find objc_msgSend
-  IRDynamicChecks(DynamicCheckerFunctions &checker_functions,
+  IRDynamicChecks(ClangDynamicCheckerFunctions &checker_functions,
                   const char *func_name = "$__lldb_expr");
 
   /// Destructor
@@ -136,7 +122,7 @@ private:
   bool FindDataLoads(llvm::Module &M, llvm::BasicBlock &BB);
 
   std::string m_func_name; ///< The name of the function to add checks to
-  DynamicCheckerFunctions
+  ClangDynamicCheckerFunctions
       &m_checker_functions; ///< The checker functions for the process
 };
 

Modified: lldb/trunk/source/Target/Process.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Process.cpp?rev=365853&r1=365852&r2=365853&view=diff
==============================================================================
--- lldb/trunk/source/Target/Process.cpp (original)
+++ lldb/trunk/source/Target/Process.cpp Thu Jul 11 17:58:02 2019
@@ -22,7 +22,7 @@
 #include "lldb/Core/PluginManager.h"
 #include "lldb/Core/StreamFile.h"
 #include "lldb/Expression/DiagnosticManager.h"
-#include "lldb/Expression/IRDynamicChecks.h"
+#include "lldb/Expression/DynamicCheckerFunctions.h"
 #include "lldb/Expression/UserExpression.h"
 #include "lldb/Expression/UtilityFunction.h"
 #include "lldb/Host/ConnectionFileDescriptor.h"

Modified: lldb/trunk/source/Target/ThreadPlanCallUserExpression.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/ThreadPlanCallUserExpression.cpp?rev=365853&r1=365852&r2=365853&view=diff
==============================================================================
--- lldb/trunk/source/Target/ThreadPlanCallUserExpression.cpp (original)
+++ lldb/trunk/source/Target/ThreadPlanCallUserExpression.cpp Thu Jul 11 17:58:02 2019
@@ -13,7 +13,7 @@
 #include "lldb/Breakpoint/BreakpointLocation.h"
 #include "lldb/Core/Address.h"
 #include "lldb/Expression/DiagnosticManager.h"
-#include "lldb/Expression/IRDynamicChecks.h"
+#include "lldb/Expression/DynamicCheckerFunctions.h"
 #include "lldb/Expression/UserExpression.h"
 #include "lldb/Host/HostInfo.h"
 #include "lldb/Target/LanguageRuntime.h"




More information about the lldb-commits mailing list