From 0a78fec3fb901158e3c074098a3bc5c7cf1f31ea Mon Sep 17 00:00:00 2001 From: Andy Ragusa Date: Mon, 9 Oct 2023 10:50:37 -0700 Subject: [PATCH] Initial work on ClamBCTrace --- libclambcc/CMakeLists.txt | 1 + libclambcc/ClamBCTrace/CMakeLists.txt | 72 +++++++++++++++++++++ libclambcc/ClamBCTrace/ClamBCTrace.cpp | 88 ++++++++++++++++++-------- 3 files changed, 134 insertions(+), 27 deletions(-) create mode 100644 libclambcc/ClamBCTrace/CMakeLists.txt diff --git a/libclambcc/CMakeLists.txt b/libclambcc/CMakeLists.txt index 319997d104..d1fdd91f1a 100644 --- a/libclambcc/CMakeLists.txt +++ b/libclambcc/CMakeLists.txt @@ -11,4 +11,5 @@ add_subdirectory(ClamBCVerifier) add_subdirectory(ClamBCRemovePointerPHIs) add_subdirectory(ClamBCLowering) add_subdirectory(ClamBCRemoveFreezeInsts) +add_subdirectory(ClamBCTrace) diff --git a/libclambcc/ClamBCTrace/CMakeLists.txt b/libclambcc/ClamBCTrace/CMakeLists.txt new file mode 100644 index 0000000000..a2c73e645a --- /dev/null +++ b/libclambcc/ClamBCTrace/CMakeLists.txt @@ -0,0 +1,72 @@ +# Copyright (C) 2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved. + +# +# The clambctrace object library +# +add_library(clambctrace_obj OBJECT) +target_sources(clambctrace_obj + PRIVATE + ClamBCTrace.cpp +) + +target_include_directories(clambctrace_obj + PRIVATE + ${CMAKE_BINARY_DIR} # For clambc-version.h (generated file) + . # For Common/clambc.h + .. # For clambc.h #TODO: change all passes to use "Common" and then delete this line. + ${LLVM_INCLUDE_DIRS} +) + +set_target_properties(clambctrace_obj PROPERTIES COMPILE_FLAGS "${WARNCXXFLAGS}") + +# +# For testing +# +#target_compile_definitions(clambctrace_obj -DLOG_BEFORE_AFTER=1) + +# +# The clambctrace shared library. +# +add_library( clambctrace SHARED ) +target_link_libraries( clambctrace + PUBLIC + clambctrace_obj ) +set_target_properties( clambctrace PROPERTIES + VERSION ${LIBCLAMBC_VERSION} + SOVERSION ${LIBCLAMBC_SOVERSION} ) + +target_link_directories(clambctrace PRIVATE ${LLVM_LIBRARY_DIRS}) +target_link_libraries(clambctrace PUBLIC ${LLVM_LIBS}) + +if(WIN32) + install(TARGETS clambctrace DESTINATION .) + + # Also install shared library (DLL) dependencies + install(CODE [[ + file(GET_RUNTIME_DEPENDENCIES + LIBRARIES + $ + RESOLVED_DEPENDENCIES_VAR _r_deps + UNRESOLVED_DEPENDENCIES_VAR _u_deps + DIRECTORIES + ${LLVM_LIBRARY_DIRS} + ) + foreach(_file ${_r_deps}) + string(TOLOWER ${_file} _file_lower) + if(NOT ${_file_lower} MATCHES "c:[\\/]windows[\\/]system32.*") + file(INSTALL + DESTINATION "${CMAKE_INSTALL_PREFIX}" + TYPE SHARED_LIBRARY + FOLLOW_SYMLINK_CHAIN + FILES "${_file}" + ) + endif() + endforeach() + #message("UNRESOLVED_DEPENDENCIES_VAR: ${_u_deps}") + ]]) +else() + install(TARGETS clambctrace DESTINATION ${CMAKE_INSTALL_LIBDIR}) +endif() + + + diff --git a/libclambcc/ClamBCTrace/ClamBCTrace.cpp b/libclambcc/ClamBCTrace/ClamBCTrace.cpp index 2943147ad2..fee6702f35 100644 --- a/libclambcc/ClamBCTrace/ClamBCTrace.cpp +++ b/libclambcc/ClamBCTrace/ClamBCTrace.cpp @@ -19,10 +19,10 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ -#include "clambc.h" -#include "ClamBCModule.h" -#include "ClamBCCommon.h" -#include "ClamBCUtilities.h" +#include "Common/clambc.h" +#include "Common/ClamBCModule.h" +#include "Common/ClamBCCommon.h" +#include "Common/ClamBCUtilities.h" #include #include @@ -34,6 +34,8 @@ #include #include #include +#include +#include #include #include #include @@ -55,22 +57,22 @@ static cl::opt InsertTracing("clambc-trace", cl::Hidden, cl::init(false), cl::desc("Enable tracing of bytecode execution")); -namespace -{ -class ClamBCTrace : public ModulePass +namespace ClamBCTrace { + +class ClamBCTrace : public PassInfoMixin { public: - static char ID; + //static char ID; ClamBCTrace() - : ModulePass(ID) {} + /* : ModulePass(ID) */ {} virtual llvm::StringRef getPassName() const { return "ClamAV Bytecode Execution Tracing"; } - virtual bool runOnModule(Module &M); +// virtual bool runOnModule(Module &M); + PreservedAnalyses run(Module & m, ModuleAnalysisManager & MAM); }; -char ClamBCTrace::ID; -} // namespace +//char ClamBCTrace::ID; /* declare i32 @trace_directory(i8*, i32) @@ -87,10 +89,16 @@ declare i32 @trace_ptr(i8*, i32) */ +#if 0 bool ClamBCTrace::runOnModule(Module &M) +#else +PreservedAnalyses ClamBCTrace::run(Module & M, ModuleAnalysisManager & MAM) +#endif { - if (!InsertTracing) - return false; + if (!InsertTracing) { + return PreservedAnalyses::all(); + //return false; + } unsigned MDDbgKind = M.getContext().getMDKindID("dbg"); DenseMap scopeIDs; unsigned scopeid = 0; @@ -102,16 +110,16 @@ bool ClamBCTrace::runOnModule(Module &M) args.push_back(I32Ty); FunctionType *FTy = FunctionType::get(I32Ty, args, false); /* llvm 10 replaces this with FunctionCallee. */ - Constant *trace_directory = M.getOrInsertFunction("trace_directory", FTy); - Constant *trace_scope = M.getOrInsertFunction("trace_scope", FTy); - Constant *trace_source = M.getOrInsertFunction("trace_source", FTy); - Constant *trace_op = M.getOrInsertFunction("trace_op", FTy); - Constant *trace_value = M.getOrInsertFunction("trace_value", FTy); - Constant *trace_ptr = M.getOrInsertFunction("trace_ptr", FTy); + FunctionCallee trace_directory = M.getOrInsertFunction("trace_directory", FTy); + FunctionCallee trace_scope = M.getOrInsertFunction("trace_scope", FTy); + FunctionCallee trace_source = M.getOrInsertFunction("trace_source", FTy); + FunctionCallee trace_op = M.getOrInsertFunction("trace_op", FTy); + FunctionCallee trace_value = M.getOrInsertFunction("trace_value", FTy); + FunctionCallee trace_ptr = M.getOrInsertFunction("trace_ptr", FTy); assert(trace_scope && trace_source && trace_op && trace_value && trace_directory && trace_ptr); - if (!trace_directory->use_empty() || !trace_scope->use_empty() || !trace_source->use_empty() || !trace_op->use_empty() || - !trace_value->use_empty() || !trace_ptr->use_empty()) { + if (!trace_directory.getCallee()->use_empty() || !trace_scope.getCallee()->use_empty() || !trace_source.getCallee()->use_empty() || !trace_op.getCallee()->use_empty() || + !trace_value.getCallee()->use_empty() || !trace_ptr.getCallee()->use_empty()) { ClamBCStop("Tracing API can only be used by compiler!\n", &M); } @@ -156,7 +164,6 @@ bool ClamBCTrace::runOnModule(Module &M) while (llvm::isa(scope)) { DILexicalBlock *lex = llvm::cast(scope); //scope = lex->getContext(); - /*aragusa: I have no idea if this is the right thing to do here.*/ scope = lex->getScope(); } @@ -237,10 +244,37 @@ bool ClamBCTrace::runOnModule(Module &M) } } } - return true; + return PreservedAnalyses::none(); } -llvm::ModulePass *createClamBCTrace() -{ - return new ClamBCTrace(); +//llvm::ModulePass *createClamBCTrace() +//{ +// return new ClamBCTrace(); +//} + + +} // namespace + + +// This part is the new way of registering your pass +extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK +llvmGetPassPluginInfo() { + return { + LLVM_PLUGIN_API_VERSION, "ClamBCTrace", "v0.1", + [](PassBuilder &PB) { + PB.registerPipelineParsingCallback( + [](StringRef Name, ModulePassManager &FPM, + ArrayRef) { + if(Name == "clambc-trace"){ + FPM.addPass(ClamBCTrace::ClamBCTrace()); + return true; + } + return false; + } + ); + } + }; } + + +