Index: include/lldb/Expression/DynamicCheckerFunctions.h =================================================================== --- /dev/null +++ include/lldb/Expression/DynamicCheckerFunctions.h @@ -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_ Index: source/Expression/CMakeLists.txt =================================================================== --- source/Expression/CMakeLists.txt +++ source/Expression/CMakeLists.txt @@ -8,7 +8,6 @@ Expression.cpp ExpressionVariable.cpp FunctionCaller.cpp - IRDynamicChecks.cpp IRExecutionUnit.cpp IRInterpreter.cpp IRMemoryMap.cpp Index: source/Plugins/ExpressionParser/Clang/CMakeLists.txt =================================================================== --- source/Plugins/ExpressionParser/Clang/CMakeLists.txt +++ source/Plugins/ExpressionParser/Clang/CMakeLists.txt @@ -19,6 +19,7 @@ ClangUserExpression.cpp ClangUtilityFunction.cpp IRForTarget.cpp + IRDynamicChecks.cpp DEPENDS ${tablegen_deps} Index: source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp =================================================================== --- source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp +++ source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp @@ -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 @@ (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 @@ "Finished installing dynamic checkers =="); } - IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), - function_name.AsCString()); + if (auto *checker_funcs = llvm::dyn_cast( + 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; - } + 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); + } } } } Index: source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h =================================================================== --- source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h +++ source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h @@ -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 @@ /// 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 m_valid_pointer_check; std::shared_ptr m_objc_object_check; @@ -94,7 +80,7 @@ /// \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 @@ 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 }; Index: source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp =================================================================== --- source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp +++ source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp @@ -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 @@ " 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 @@ 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 @@ 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) {} Index: source/Target/Process.cpp =================================================================== --- source/Target/Process.cpp +++ source/Target/Process.cpp @@ -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" Index: source/Target/ThreadPlanCallUserExpression.cpp =================================================================== --- source/Target/ThreadPlanCallUserExpression.cpp +++ source/Target/ThreadPlanCallUserExpression.cpp @@ -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"