From a3e16668b0eb39d6bd096cf713b716e6ed138a51 Mon Sep 17 00:00:00 2001 From: Andy Ragusa Date: Fri, 15 Dec 2023 12:23:28 -0800 Subject: [PATCH] blah --- libclambcc/CMakeLists.txt | 4 - libclambcc/ClamBCRegAlloc/ClamBCRegAlloc.cpp | 67 +---- libclambcc/ClamBCRegAlloc/ClamBCRegAlloc.h | 3 +- .../ClamBCRemoveFSHL/ClamBCRemoveFSHL.cpp | 6 +- libclambcc/ClamBCRemoveSMAX/CMakeLists.txt | 72 ------ .../ClamBCRemoveSMAX/ClamBCRemoveSMAX.cpp | 140 ----------- .../ClamBCRemoveUMIN.cpp.save | 234 ------------------ libclambcc/ClamBCRemoveUMAX/CMakeLists.txt | 72 ------ .../ClamBCRemoveUMAX/ClamBCRemoveUMAX.cpp | 144 ----------- libclambcc/ClamBCRemoveUMIN/CMakeLists.txt | 72 ------ .../ClamBCRemoveUMIN/ClamBCRemoveUMIN.cpp | 140 ----------- .../ClamBCRemoveUMIN.cpp.save | 234 ------------------ 12 files changed, 5 insertions(+), 1183 deletions(-) delete mode 100644 libclambcc/ClamBCRemoveSMAX/CMakeLists.txt delete mode 100644 libclambcc/ClamBCRemoveSMAX/ClamBCRemoveSMAX.cpp delete mode 100644 libclambcc/ClamBCRemoveSMAX/ClamBCRemoveUMIN.cpp.save delete mode 100644 libclambcc/ClamBCRemoveUMAX/CMakeLists.txt delete mode 100644 libclambcc/ClamBCRemoveUMAX/ClamBCRemoveUMAX.cpp delete mode 100644 libclambcc/ClamBCRemoveUMIN/CMakeLists.txt delete mode 100644 libclambcc/ClamBCRemoveUMIN/ClamBCRemoveUMIN.cpp delete mode 100644 libclambcc/ClamBCRemoveUMIN/ClamBCRemoveUMIN.cpp.save diff --git a/libclambcc/CMakeLists.txt b/libclambcc/CMakeLists.txt index 00f9fc9968..04f7cf2182 100644 --- a/libclambcc/CMakeLists.txt +++ b/libclambcc/CMakeLists.txt @@ -18,13 +18,9 @@ add_subdirectory(ClamBCChangeMallocArgSize) add_subdirectory(ClamBCExtendPHIsTo64Bit) add_subdirectory(ClamBCRebuild) add_subdirectory(ClamBCRegAlloc) -#add_subdirectory(ClamBCTypeAnalyzer) add_subdirectory(ClamBCConvertMemsetsTo32Bit) add_subdirectory(ClamBCPrepareGEPsForWriter) add_subdirectory(ClamBCRemoveICMPSLE) -#add_subdirectory(ClamBCRemoveUMIN) -#add_subdirectory(ClamBCRemoveUMAX) -#add_subdirectory(ClamBCRemoveSMAX) add_subdirectory(ClamBCRemoveUSUB) add_subdirectory(ClamBCRemoveUnsupportedICMPIntrinsics) add_subdirectory(ClamBCRemoveFSHL) diff --git a/libclambcc/ClamBCRegAlloc/ClamBCRegAlloc.cpp b/libclambcc/ClamBCRegAlloc/ClamBCRegAlloc.cpp index acfb89aae4..f7c651c31c 100644 --- a/libclambcc/ClamBCRegAlloc/ClamBCRegAlloc.cpp +++ b/libclambcc/ClamBCRegAlloc/ClamBCRegAlloc.cpp @@ -81,12 +81,7 @@ void ClamBCRegAllocAnalysis::handlePHI(PHINode *PN) ++It; } while (isa(It)); builder.SetInsertPoint(&*It); -#if 0 - LoadInst *LI = builder.CreateLoad(AI, ".phiload"); -#else - //llvm::errs() << "<" << __LINE__ << ">" << "There is a chance I'll have to do something like getPoinerOperand->getType or something like that, so leave this in here as a reminder to go look\n" << "\n"; LoadInst *LI = builder.CreateLoad(AI->getAllocatedType(), AI, ".phiload"); -#endif builder.SetInstDebugLocation(LI); PN->replaceAllUsesWith(LI); PN->eraseFromParent(); @@ -96,9 +91,6 @@ bool ClamBCRegAllocAnalysis::runOnFunction(Function &F) { ValueMap.clear(); RevValueMap.clear(); -#if 0 - DT = &getAnalysis().getDomTree(); -#endif bool Changed = false; for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { BasicBlock &BB = *I; @@ -119,8 +111,6 @@ bool ClamBCRegAllocAnalysis::runOnFunction(Function &F) ValueMap[A] = id; if (RevValueMap.size() == id) { RevValueMap.push_back(A); - } else { - errs() << id << " " << __FILE__ << ":" << __LINE__ << "\n"; } ++id; } @@ -142,8 +132,7 @@ bool ClamBCRegAllocAnalysis::runOnFunction(Function &F) { static int first = 1; if (first){ - DEBUGERR << "TODO: Move these checks to the verifier" << "\n"; - first = 0; + first = 0; } } if (CastInst *BC = dyn_cast(II)) { @@ -151,54 +140,18 @@ bool ClamBCRegAllocAnalysis::runOnFunction(Function &F) if (!BCI->isLosslessCast()) { ClamBCStop("Non lossless bitcast is not supported", BCI); } -#if 0 - const Type *SrcTy = BC->getOperand(0)->getType(); - const Type *DstTy = BC->getType(); - const PointerType *SPTy, *DPTy; - while ((SPTy = dyn_cast(SrcTy))) { - DPTy = dyn_cast(DstTy); - if (!DPTy) { - ClamBCStop("Cast from pointer to non-pointer element", - BCI); - } -#if 0 - SrcTy = SPTy->getElementType(); - DstTy = DPTy->getElementType(); -#else - - llvm::errs() << "\n\n\n"; - DEBUG_VALUE(BCI); - DEBUG_VALUE(BCI->getOperand(0)); - DEBUG_NONPOINTER(llvm::isa(SrcTy)); - DEBUG_NONPOINTER(llvm::isa(DstTy)); - - - DEBUGERR << "EXITING" << "\n"; - exit(1); - - - /*Don't expect any issues with this change.*/ - SrcTy = SPTy->getPointerElementType(); - DstTy = DPTy->getPointerElementType(); -#endif - } -#else if (BCI->getSrcTy()->isPointerTy() and (not BCI->getDestTy()->isPointerTy())){ ClamBCStop("Cast from pointer to non-pointer element", BCI); } -#endif - if (AllocaInst *AI = dyn_cast(BCI->getOperand(0))) { if (!AI->isArrayAllocation()) { // we need to use a GEP 0,0 for bitcast here ValueMap[II] = id; if (RevValueMap.size() == id) { RevValueMap.push_back(II); - } else { - errs() << id << " " << __FILE__ << ":" << __LINE__ << "\n"; } ++id; continue; @@ -230,14 +183,6 @@ bool ClamBCRegAllocAnalysis::runOnFunction(Function &F) } } // single-use of load from alloca -> use directly value id of alloca - //TODO: we must check for intervening stores here, better use memdep! - /* if (LoadInst *LI = dyn_cast(II)) { - if (AllocaInst *AI = dyn_cast(LI->getPointerOperand())) { - ValueMap[LI] = getValueID(AI); - SkipMap.insert(LI); - continue; - } - }*/ } ValueMap[II] = id; if (RevValueMap.size() == id) { @@ -313,15 +258,6 @@ void ClamBCRegAllocAnalysis::getAnalysisUsage(AnalysisUsage &AU) const -#if 0 -char ClamBCRegAllocAnalysis::ID = 0; -static RegisterPass X("clambc-ra", - "ClamAV bytecode register allocator"); - -const PassInfo *const ClamBCRegAllocID = &X; -#else - - // This part is the new way of registering your pass extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo() { @@ -339,4 +275,3 @@ llvmGetPassPluginInfo() { -#endif diff --git a/libclambcc/ClamBCRegAlloc/ClamBCRegAlloc.h b/libclambcc/ClamBCRegAlloc/ClamBCRegAlloc.h index 040f022a47..81c4a04445 100644 --- a/libclambcc/ClamBCRegAlloc/ClamBCRegAlloc.h +++ b/libclambcc/ClamBCRegAlloc/ClamBCRegAlloc.h @@ -43,8 +43,7 @@ class ClamBCRegAllocAnalysis { public: static char ID; - explicit ClamBCRegAllocAnalysis() - /* : FunctionPass(ID) */ {} + explicit ClamBCRegAllocAnalysis() {} unsigned buildReverseMap(std::vector &); bool skipInstruction(const llvm::Instruction *I) const diff --git a/libclambcc/ClamBCRemoveFSHL/ClamBCRemoveFSHL.cpp b/libclambcc/ClamBCRemoveFSHL/ClamBCRemoveFSHL.cpp index db5fe7fde0..80c20230ec 100644 --- a/libclambcc/ClamBCRemoveFSHL/ClamBCRemoveFSHL.cpp +++ b/libclambcc/ClamBCRemoveFSHL/ClamBCRemoveFSHL.cpp @@ -53,6 +53,7 @@ namespace } virtual llvm::Function * addFunction64(IntegerType * functionArgType, const char * const functionName){ + /*Will determine if this is necessary during the rc phase.*/ #if 0 This is an example function, needs to be converted to IR static uint8_t fshl8_noshifts(uint8_t left, uint8_t right, uint8_t shift){ @@ -137,6 +138,8 @@ static uint8_t fshl8_noshifts(uint8_t left, uint8_t right, uint8_t shift){ virtual ~ClamBCRemoveFSHL() {} + /*TODO: + * Add this to validator.*/ PreservedAnalyses run(Module & m, ModuleAnalysisManager & MAM) { pMod = &m; @@ -145,9 +148,6 @@ static uint8_t fshl8_noshifts(uint8_t left, uint8_t right, uint8_t shift){ bRet |= replaceCalls("llvm.fshl.i16", ".fshl.i16", Type::getInt16Ty(pMod->getContext())); bRet |= replaceCalls("llvm.fshl.i8", ".fshl.i8", Type::getInt16Ty(pMod->getContext())); - - DEBUGERR << "TODO: ADD fshl detection to the validator" << "\n"; - if (bRet){ return PreservedAnalyses::none(); } diff --git a/libclambcc/ClamBCRemoveSMAX/CMakeLists.txt b/libclambcc/ClamBCRemoveSMAX/CMakeLists.txt deleted file mode 100644 index c0d63dbb2a..0000000000 --- a/libclambcc/ClamBCRemoveSMAX/CMakeLists.txt +++ /dev/null @@ -1,72 +0,0 @@ -# Copyright (C) 2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved. - -# -# The clambcremovesmax object library -# -add_library(clambcremovesmax_obj OBJECT) -target_sources(clambcremovesmax_obj - PRIVATE - ClamBCRemoveSMAX.cpp -) - -target_include_directories(clambcremovesmax_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(clambcremovesmax_obj PROPERTIES COMPILE_FLAGS "${WARNCXXFLAGS}") - -# -# For testing -# -#target_compile_definitions(clambcremovesmax_obj -DLOG_BEFORE_AFTER=1) - -# -# The clambcremovesmax shared library. -# -add_library( clambcremovesmax SHARED ) -target_link_libraries( clambcremovesmax - PUBLIC - clambcremovesmax_obj ) -set_target_properties( clambcremovesmax PROPERTIES - VERSION ${LIBCLAMBC_VERSION} - SOVERSION ${LIBCLAMBC_SOVERSION} ) - -target_link_directories(clambcremovesmax PRIVATE ${LLVM_LIBRARY_DIRS}) -target_link_libraries(clambcremovesmax PUBLIC ${LLVM_LIBS}) - -if(WIN32) - install(TARGETS clambcremovesmax 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 clambcremovesmax DESTINATION ${CMAKE_INSTALL_LIBDIR}) -endif() - - - diff --git a/libclambcc/ClamBCRemoveSMAX/ClamBCRemoveSMAX.cpp b/libclambcc/ClamBCRemoveSMAX/ClamBCRemoveSMAX.cpp deleted file mode 100644 index eed3b1642e..0000000000 --- a/libclambcc/ClamBCRemoveSMAX/ClamBCRemoveSMAX.cpp +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Compile LLVM bytecode to ClamAV bytecode. - * - * Copyright (C) 2020-2023 Sourcefire, Inc. - * - * Authors: Andy Ragusa - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - -#include "Common/clambc.h" -#include "Common/ClamBCUtilities.h" - -#include -#include -#include - -#include -#include - -#include - -using namespace llvm; -using namespace std; - -namespace -{ - /* - * Remove smax intrinsic because it's not supported by our runtime. - */ - struct ClamBCRemoveSMAX : public PassInfoMixin - { - protected: - Module *pMod = nullptr; - const char * const SMAX_NAME = ".smax"; - - FunctionType * smaxType = nullptr; - - virtual llvm::IntegerType* addSMAXArgType(){ - return Type::getInt32Ty(pMod->getContext()); - } - - virtual llvm::FunctionType * getSMAXFunctionType(){ - IntegerType * it = addSMAXArgType(); - return FunctionType::get(it, {it, it}, false); - } - - virtual llvm::Function * addSMAX(){ - uint32_t addressSpace = pMod->getDataLayout().getProgramAddressSpace(); - - IntegerType * it = addSMAXArgType(); - FunctionType * ft = getSMAXFunctionType(); - - llvm::Function * smax = Function::Create(ft, GlobalValue::InternalLinkage, SMAX_NAME, *pMod); - Value * pLeft = smax->getArg(0); - Value * pRight = smax->getArg(1); - BasicBlock * pEntry = BasicBlock::Create(pMod->getContext(), "entry", smax); - BasicBlock * pLHS = BasicBlock::Create(pMod->getContext(), "left", smax); - BasicBlock * pRHS = BasicBlock::Create(pMod->getContext(), "right", smax); - BasicBlock * pRetBlock = BasicBlock::Create(pMod->getContext(), "ret", smax); - - //entry block - AllocaInst * retVar = new AllocaInst(it, addressSpace , "ret", pEntry); - ICmpInst * cmp = new ICmpInst(*pEntry, CmpInst::ICMP_SGT, pLeft, pRight, "icmp"); - BranchInst::Create(pLHS, pRHS, cmp, pEntry); - - //left < right - new StoreInst (pLeft, retVar, pLHS); - BranchInst::Create(pRetBlock, pLHS); - - //right >= left - new StoreInst (pRight, retVar, pRHS); - BranchInst::Create(pRetBlock, pRHS); - - LoadInst * pli = new LoadInst(it, retVar, "load", pRetBlock); - ReturnInst::Create(pMod->getContext(), pli, pRetBlock); - return smax; - } - - public: - - virtual ~ClamBCRemoveSMAX() {} - - PreservedAnalyses run(Module & m, ModuleAnalysisManager & MAM) - { - pMod = &m; - - DEBUGERR << "TODO: ADD smax detection to the validator" << "\n"; - const char * const INTRINSIC_NAME = "llvm.smax.i32"; - - std::vector calls; - gatherCallsToIntrinsic(pMod, INTRINSIC_NAME, calls); - if (calls.size()){ - Function * smax = addSMAX(); - replaceAllCalls(getSMAXFunctionType(), smax, calls, "ClamBCRemoveSMAX_"); - - return PreservedAnalyses::none(); - } - - return PreservedAnalyses::all(); - } - - }; // end of struct ClamBCRemoveSMAX - -} // end of anonymous namespace - -// This part is the new way of registering your pass -extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK -llvmGetPassPluginInfo() { - return { - LLVM_PLUGIN_API_VERSION, "ClamBCRemoveSMAX", "v0.1", - [](PassBuilder &PB) { - PB.registerPipelineParsingCallback( - [](StringRef Name, ModulePassManager &FPM, - ArrayRef) { - if(Name == "clambc-remove-smax"){ - FPM.addPass(ClamBCRemoveSMAX()); - return true; - } - return false; - } - ); - } - }; -} - - - diff --git a/libclambcc/ClamBCRemoveSMAX/ClamBCRemoveUMIN.cpp.save b/libclambcc/ClamBCRemoveSMAX/ClamBCRemoveUMIN.cpp.save deleted file mode 100644 index 8fda71c9e7..0000000000 --- a/libclambcc/ClamBCRemoveSMAX/ClamBCRemoveUMIN.cpp.save +++ /dev/null @@ -1,234 +0,0 @@ -/* - * Compile LLVM bytecode to ClamAV bytecode. - * - * Copyright (C) 2020-2023 Sourcefire, Inc. - * - * Authors: Andy Ragusa - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - -#include "Common/clambc.h" -#include "Common/ClamBCUtilities.h" - -#include -#include -#include - -#include -#include - -#include - -using namespace llvm; -using namespace std; - -namespace -{ - - struct ClamBCRemoveUnsupportedIntrinsic : public PassInfoMixin< ClamBCRemoveUnsupportedIntrinsic > - { - protected: - Module *pMod = nullptr; - bool bChanged = false; - - //TODO: change - llvm::Function * umin = nullptr; - const char * const UMIN_NAME = ".umin"; - - virtual const char * const getIntrinsicName() const = 0; - - virtual llvm::IntegerType* getArgumentType() const = 0; - - virtual llvm::FunctionType * getFunctionType() const = 0; - - virtual void gatherCalls(Function *pFunc, std::vector & calls) { - - const char * const intrinsicName = getIntrinsicName(); - - for (auto fi = pFunc->begin(), fe = pFunc->end(); fi != fe; fi++){ - BasicBlock * pBB = llvm::cast(fi); - for (auto bi = pBB->begin(), be = pBB->end(); bi != be; bi++){ - if (CallInst * pci = llvm::dyn_cast(bi)){ - Function * pCalled = pci->getCalledFunction(); - if (pCalled->isIntrinsic()){ - if (intrinsicName == pCalled->getName()) { - calls.push_back(pci); - } - } - } - } - } - } - - virtual llvm::Function * getFunction(){ - if (nullptr == umin){ - uint32_t addressSpace = pMod->getDataLayout().getProgramAddressSpace(); - - IntegerType * it = getArgumentType(); - FunctionType * ft = getFunctionType(); - - umin = Function::Create(ft, GlobalValue::InternalLinkage, UMIN_NAME, *pMod); - Value * pLeft = umin->getArg(0); - Value * pRight = umin->getArg(1); - BasicBlock * pEntry = BasicBlock::Create(pMod->getContext(), "entry", umin); - BasicBlock * pLHS = BasicBlock::Create(pMod->getContext(), "left", umin); - BasicBlock * pRHS = BasicBlock::Create(pMod->getContext(), "right", umin); - BasicBlock * pRetBlock = BasicBlock::Create(pMod->getContext(), "ret", umin); - - //entry block - AllocaInst * retVar = new AllocaInst(it, addressSpace , "ret", pEntry); - ICmpInst * cmp = new ICmpInst(*pEntry, CmpInst::ICMP_ULT, pLeft, pRight, "icmp"); - BranchInst::Create(pLHS, pRHS, cmp, pEntry); - - //left < right - new StoreInst (pLeft, retVar, pLHS); - BranchInst::Create(pRetBlock, pLHS); - - //right >= left - new StoreInst (pRight, retVar, pRHS); - BranchInst::Create(pRetBlock, pRHS); - - LoadInst * pli = new LoadInst(it, retVar, "load", pRetBlock); - ReturnInst::Create(pMod->getContext(), pli, pRetBlock); - } - - return umin; - } - - - virtual void processFunction(Function *pFunc) { - vector calls; - gatherCalls(pFunc, calls); - - for (size_t i = 0; i < calls.size(); i++){ - bChanged = true; - CallInst * pci = calls[i]; - - Function * umin = getFunction(); - - BasicBlock * pBlock = pci->getParent(); - DEBUG_VALUE(pBlock); - - std::vector args; - for (size_t i = 0; i < pci->arg_size(); i++){ - args.push_back(pci->getArgOperand(i)); - } - CallInst * pNew = CallInst::Create(getFunctionType(), umin, args, "ClamBCRemoveUMIN_", pci); - pci->replaceAllUsesWith(pNew); - pci->eraseFromParent(); - - DEBUG_VALUE(pBlock); - } - } - - public: - - PreservedAnalyses run(Module & m, ModuleAnalysisManager & MAM) - { - pMod = &m; - - DEBUGERR << "TODO: ADD umin detection to the validator" << "\n"; - - for (auto i = pMod->begin(), e = pMod->end(); i != e; i++) { - Function *pFunc = llvm::cast(i); - if (pFunc->isDeclaration()) { - continue; - } - - processFunction(pFunc); - } - - if (bChanged){ - return PreservedAnalyses::none(); - } else{ - return PreservedAnalyses::all(); - } - } - - virtual ~ClamBCRemoveUnsupportedIntrinsic () {} - }; - - /* - * Remove umin intrinsic because it's not supported by our runtime. - */ - struct ClamBCRemoveUMIN : public ClamBCRemoveUnsupportedIntrinsic - { - protected: - - FunctionType * uminType = nullptr; - IntegerType * uminArgType = nullptr; - - const char * const getIntrinsicName() const { - return "llvm.umin.i32" ; - } - - virtual llvm::IntegerType* getArgumentType() const { - - //if (nullptr == uminArgType){ - // uminArgType = Type::getInt32Ty(pMod->getContext()); - //} - - return uminArgType; - } - - virtual llvm::FunctionType * getFunctionType() const { - // if (nullptr == uminType){ - // IntegerType * it = getArgumentType(); - // uminType = FunctionType::get(it, {it, it}, false); - // } - return uminType; - } - - - public: - - ClamBCRemoveUMIN() { - DEBUGERR << "Constructor" << "\n"; - uminArgType = Type::getInt32Ty(pMod->getContext()); - DEBUGERR << "Constructor" << "\n"; - uminType = FunctionType::get(uminArgType , {uminArgType, uminArgType}, false); - DEBUGERR << "Constructor" << "\n"; - } - - virtual ~ClamBCRemoveUMIN() {} - - - }; // end of struct ClamBCRemoveUMIN - -} // end of anonymous namespace - -// This part is the new way of registering your pass -extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK -llvmGetPassPluginInfo() { - return { - LLVM_PLUGIN_API_VERSION, "ClamBCRemoveUMIN", "v0.1", - [](PassBuilder &PB) { - PB.registerPipelineParsingCallback( - [](StringRef Name, ModulePassManager &FPM, - ArrayRef) { - if(Name == "clambc-remove-umin"){ - FPM.addPass(ClamBCRemoveUMIN()); - return true; - } - return false; - } - ); - } - }; -} - - - diff --git a/libclambcc/ClamBCRemoveUMAX/CMakeLists.txt b/libclambcc/ClamBCRemoveUMAX/CMakeLists.txt deleted file mode 100644 index ce48740d6b..0000000000 --- a/libclambcc/ClamBCRemoveUMAX/CMakeLists.txt +++ /dev/null @@ -1,72 +0,0 @@ -# Copyright (C) 2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved. - -# -# The clambcremoveumax object library -# -add_library(clambcremoveumax_obj OBJECT) -target_sources(clambcremoveumax_obj - PRIVATE - ClamBCRemoveUMAX.cpp -) - -target_include_directories(clambcremoveumax_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(clambcremoveumax_obj PROPERTIES COMPILE_FLAGS "${WARNCXXFLAGS}") - -# -# For testing -# -#target_compile_definitions(clambcremoveumax_obj -DLOG_BEFORE_AFTER=1) - -# -# The clambcremoveumax shared library. -# -add_library( clambcremoveumax SHARED ) -target_link_libraries( clambcremoveumax - PUBLIC - clambcremoveumax_obj ) -set_target_properties( clambcremoveumax PROPERTIES - VERSION ${LIBCLAMBC_VERSION} - SOVERSION ${LIBCLAMBC_SOVERSION} ) - -target_link_directories(clambcremoveumax PRIVATE ${LLVM_LIBRARY_DIRS}) -target_link_libraries(clambcremoveumax PUBLIC ${LLVM_LIBS}) - -if(WIN32) - install(TARGETS clambcremoveumax 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 clambcremoveumax DESTINATION ${CMAKE_INSTALL_LIBDIR}) -endif() - - - diff --git a/libclambcc/ClamBCRemoveUMAX/ClamBCRemoveUMAX.cpp b/libclambcc/ClamBCRemoveUMAX/ClamBCRemoveUMAX.cpp deleted file mode 100644 index 879ce22d52..0000000000 --- a/libclambcc/ClamBCRemoveUMAX/ClamBCRemoveUMAX.cpp +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Compile LLVM bytecode to ClamAV bytecode. - * - * Copyright (C) 2020-2023 Sourcefire, Inc. - * - * Authors: Andy Ragusa - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - -#include "Common/clambc.h" -#include "Common/ClamBCUtilities.h" - -#include -#include -#include - -#include -#include - -#include - -using namespace llvm; -using namespace std; - -namespace -{ - /* - * Remove umax intrinsic because it's not supported by our runtime. - */ - struct ClamBCRemoveUMAX : public PassInfoMixin - { - protected: - Module *pMod = nullptr; - const char * const UMAX_NAME = ".umax"; - - FunctionType * umaxType = nullptr; - - virtual llvm::FunctionType * getUMAXFunctionType(Type * functionArgType){ - return FunctionType::get(functionArgType, {functionArgType, functionArgType}, false); - } - - virtual llvm::Function * addUMAX(Type * functionArgType){ - uint32_t addressSpace = pMod->getDataLayout().getProgramAddressSpace(); - - FunctionType * ft = getUMAXFunctionType(functionArgType); - - llvm::Function * umax = Function::Create(ft, GlobalValue::InternalLinkage, UMAX_NAME, *pMod); - Value * pLeft = umax->getArg(0); - Value * pRight = umax->getArg(1); - BasicBlock * pEntry = BasicBlock::Create(pMod->getContext(), "entry", umax); - BasicBlock * pLHS = BasicBlock::Create(pMod->getContext(), "left", umax); - BasicBlock * pRHS = BasicBlock::Create(pMod->getContext(), "right", umax); - BasicBlock * pRetBlock = BasicBlock::Create(pMod->getContext(), "ret", umax); - - //entry block - AllocaInst * retVar = new AllocaInst(functionArgType, addressSpace , "ret", pEntry); - ICmpInst * cmp = new ICmpInst(*pEntry, CmpInst::ICMP_UGT, pLeft, pRight, "icmp"); - BranchInst::Create(pLHS, pRHS, cmp, pEntry); - - //left > right - new StoreInst (pLeft, retVar, pLHS); - BranchInst::Create(pRetBlock, pLHS); - - //right >= left - new StoreInst (pRight, retVar, pRHS); - BranchInst::Create(pRetBlock, pRHS); - - LoadInst * pli = new LoadInst(functionArgType, retVar, "load", pRetBlock); - ReturnInst::Create(pMod->getContext(), pli, pRetBlock); - return umax; - } - - virtual bool replaceCalls(const char * const intrinsicName, Type * functionArgType){ - std::vector calls; - gatherCallsToIntrinsic(pMod, intrinsicName, calls); - if (calls.size()){ - Function * umax = addUMAX(functionArgType); - replaceAllCalls(getUMAXFunctionType(functionArgType), umax, calls, "ClamBCRemoveUMAX_"); - - return true; - } - return false; - } - - public: - - virtual ~ClamBCRemoveUMAX() {} - - PreservedAnalyses run(Module & m, ModuleAnalysisManager & MAM) - { - pMod = &m; - - bool bRet = replaceCalls("llvm.umax.i32", Type::getInt32Ty(pMod->getContext())); - bRet |= replaceCalls("llvm.umax.i16", Type::getInt16Ty(pMod->getContext())); - - - DEBUGERR << "TODO: ADD umax detection to the validator" << "\n"; - - if (bRet){ - return PreservedAnalyses::none(); - } - - return PreservedAnalyses::all(); - } - - }; // end of struct ClamBCRemoveUMAX - -} // end of anonymous namespace - -// This part is the new way of registering your pass -extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK -llvmGetPassPluginInfo() { - return { - LLVM_PLUGIN_API_VERSION, "ClamBCRemoveUMAX", "v0.1", - [](PassBuilder &PB) { - PB.registerPipelineParsingCallback( - [](StringRef Name, ModulePassManager &FPM, - ArrayRef) { - if(Name == "clambc-remove-umax"){ - FPM.addPass(ClamBCRemoveUMAX()); - return true; - } - return false; - } - ); - } - }; -} - - - diff --git a/libclambcc/ClamBCRemoveUMIN/CMakeLists.txt b/libclambcc/ClamBCRemoveUMIN/CMakeLists.txt deleted file mode 100644 index fb9fa2f456..0000000000 --- a/libclambcc/ClamBCRemoveUMIN/CMakeLists.txt +++ /dev/null @@ -1,72 +0,0 @@ -# Copyright (C) 2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved. - -# -# The clambcremoveumin object library -# -add_library(clambcremoveumin_obj OBJECT) -target_sources(clambcremoveumin_obj - PRIVATE - ClamBCRemoveUMIN.cpp -) - -target_include_directories(clambcremoveumin_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(clambcremoveumin_obj PROPERTIES COMPILE_FLAGS "${WARNCXXFLAGS}") - -# -# For testing -# -#target_compile_definitions(clambcremoveumin_obj -DLOG_BEFORE_AFTER=1) - -# -# The clambcremoveumin shared library. -# -add_library( clambcremoveumin SHARED ) -target_link_libraries( clambcremoveumin - PUBLIC - clambcremoveumin_obj ) -set_target_properties( clambcremoveumin PROPERTIES - VERSION ${LIBCLAMBC_VERSION} - SOVERSION ${LIBCLAMBC_SOVERSION} ) - -target_link_directories(clambcremoveumin PRIVATE ${LLVM_LIBRARY_DIRS}) -target_link_libraries(clambcremoveumin PUBLIC ${LLVM_LIBS}) - -if(WIN32) - install(TARGETS clambcremoveumin 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 clambcremoveumin DESTINATION ${CMAKE_INSTALL_LIBDIR}) -endif() - - - diff --git a/libclambcc/ClamBCRemoveUMIN/ClamBCRemoveUMIN.cpp b/libclambcc/ClamBCRemoveUMIN/ClamBCRemoveUMIN.cpp deleted file mode 100644 index 68b03cc60d..0000000000 --- a/libclambcc/ClamBCRemoveUMIN/ClamBCRemoveUMIN.cpp +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Compile LLVM bytecode to ClamAV bytecode. - * - * Copyright (C) 2020-2023 Sourcefire, Inc. - * - * Authors: Andy Ragusa - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - -#include "Common/clambc.h" -#include "Common/ClamBCUtilities.h" - -#include -#include -#include - -#include -#include - -#include - -using namespace llvm; -using namespace std; - -namespace -{ - /* - * Remove umin intrinsic because it's not supported by our runtime. - */ - struct ClamBCRemoveUMIN : public PassInfoMixin - { - protected: - Module *pMod = nullptr; - const char * const UMIN_NAME = ".umin"; - - FunctionType * uminType = nullptr; - - virtual llvm::IntegerType* addUMINArgType(){ - return Type::getInt32Ty(pMod->getContext()); - } - - virtual llvm::FunctionType * getUMINFunctionType(){ - IntegerType * it = addUMINArgType(); - return FunctionType::get(it, {it, it}, false); - } - - virtual llvm::Function * addUMIN(){ - uint32_t addressSpace = pMod->getDataLayout().getProgramAddressSpace(); - - IntegerType * it = addUMINArgType(); - FunctionType * ft = getUMINFunctionType(); - - llvm::Function * umin = Function::Create(ft, GlobalValue::InternalLinkage, UMIN_NAME, *pMod); - Value * pLeft = umin->getArg(0); - Value * pRight = umin->getArg(1); - BasicBlock * pEntry = BasicBlock::Create(pMod->getContext(), "entry", umin); - BasicBlock * pLHS = BasicBlock::Create(pMod->getContext(), "left", umin); - BasicBlock * pRHS = BasicBlock::Create(pMod->getContext(), "right", umin); - BasicBlock * pRetBlock = BasicBlock::Create(pMod->getContext(), "ret", umin); - - //entry block - AllocaInst * retVar = new AllocaInst(it, addressSpace , "ret", pEntry); - ICmpInst * cmp = new ICmpInst(*pEntry, CmpInst::ICMP_ULT, pLeft, pRight, "icmp"); - BranchInst::Create(pLHS, pRHS, cmp, pEntry); - - //left < right - new StoreInst (pLeft, retVar, pLHS); - BranchInst::Create(pRetBlock, pLHS); - - //right >= left - new StoreInst (pRight, retVar, pRHS); - BranchInst::Create(pRetBlock, pRHS); - - LoadInst * pli = new LoadInst(it, retVar, "load", pRetBlock); - ReturnInst::Create(pMod->getContext(), pli, pRetBlock); - return umin; - } - - public: - - virtual ~ClamBCRemoveUMIN() {} - - PreservedAnalyses run(Module & m, ModuleAnalysisManager & MAM) - { - pMod = &m; - - DEBUGERR << "TODO: ADD umin detection to the validator" << "\n"; - const char * const INTRINSIC_NAME = "llvm.umin.i32"; - - std::vector calls; - gatherCallsToIntrinsic(pMod, INTRINSIC_NAME, calls); - if (calls.size()){ - Function * umin = addUMIN(); - replaceAllCalls(getUMINFunctionType(), umin, calls, "ClamBCRemoveUMIN_"); - - return PreservedAnalyses::none(); - } - - return PreservedAnalyses::all(); - } - - }; // end of struct ClamBCRemoveUMIN - -} // end of anonymous namespace - -// This part is the new way of registering your pass -extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK -llvmGetPassPluginInfo() { - return { - LLVM_PLUGIN_API_VERSION, "ClamBCRemoveUMIN", "v0.1", - [](PassBuilder &PB) { - PB.registerPipelineParsingCallback( - [](StringRef Name, ModulePassManager &FPM, - ArrayRef) { - if(Name == "clambc-remove-umin"){ - FPM.addPass(ClamBCRemoveUMIN()); - return true; - } - return false; - } - ); - } - }; -} - - - diff --git a/libclambcc/ClamBCRemoveUMIN/ClamBCRemoveUMIN.cpp.save b/libclambcc/ClamBCRemoveUMIN/ClamBCRemoveUMIN.cpp.save deleted file mode 100644 index 8fda71c9e7..0000000000 --- a/libclambcc/ClamBCRemoveUMIN/ClamBCRemoveUMIN.cpp.save +++ /dev/null @@ -1,234 +0,0 @@ -/* - * Compile LLVM bytecode to ClamAV bytecode. - * - * Copyright (C) 2020-2023 Sourcefire, Inc. - * - * Authors: Andy Ragusa - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - -#include "Common/clambc.h" -#include "Common/ClamBCUtilities.h" - -#include -#include -#include - -#include -#include - -#include - -using namespace llvm; -using namespace std; - -namespace -{ - - struct ClamBCRemoveUnsupportedIntrinsic : public PassInfoMixin< ClamBCRemoveUnsupportedIntrinsic > - { - protected: - Module *pMod = nullptr; - bool bChanged = false; - - //TODO: change - llvm::Function * umin = nullptr; - const char * const UMIN_NAME = ".umin"; - - virtual const char * const getIntrinsicName() const = 0; - - virtual llvm::IntegerType* getArgumentType() const = 0; - - virtual llvm::FunctionType * getFunctionType() const = 0; - - virtual void gatherCalls(Function *pFunc, std::vector & calls) { - - const char * const intrinsicName = getIntrinsicName(); - - for (auto fi = pFunc->begin(), fe = pFunc->end(); fi != fe; fi++){ - BasicBlock * pBB = llvm::cast(fi); - for (auto bi = pBB->begin(), be = pBB->end(); bi != be; bi++){ - if (CallInst * pci = llvm::dyn_cast(bi)){ - Function * pCalled = pci->getCalledFunction(); - if (pCalled->isIntrinsic()){ - if (intrinsicName == pCalled->getName()) { - calls.push_back(pci); - } - } - } - } - } - } - - virtual llvm::Function * getFunction(){ - if (nullptr == umin){ - uint32_t addressSpace = pMod->getDataLayout().getProgramAddressSpace(); - - IntegerType * it = getArgumentType(); - FunctionType * ft = getFunctionType(); - - umin = Function::Create(ft, GlobalValue::InternalLinkage, UMIN_NAME, *pMod); - Value * pLeft = umin->getArg(0); - Value * pRight = umin->getArg(1); - BasicBlock * pEntry = BasicBlock::Create(pMod->getContext(), "entry", umin); - BasicBlock * pLHS = BasicBlock::Create(pMod->getContext(), "left", umin); - BasicBlock * pRHS = BasicBlock::Create(pMod->getContext(), "right", umin); - BasicBlock * pRetBlock = BasicBlock::Create(pMod->getContext(), "ret", umin); - - //entry block - AllocaInst * retVar = new AllocaInst(it, addressSpace , "ret", pEntry); - ICmpInst * cmp = new ICmpInst(*pEntry, CmpInst::ICMP_ULT, pLeft, pRight, "icmp"); - BranchInst::Create(pLHS, pRHS, cmp, pEntry); - - //left < right - new StoreInst (pLeft, retVar, pLHS); - BranchInst::Create(pRetBlock, pLHS); - - //right >= left - new StoreInst (pRight, retVar, pRHS); - BranchInst::Create(pRetBlock, pRHS); - - LoadInst * pli = new LoadInst(it, retVar, "load", pRetBlock); - ReturnInst::Create(pMod->getContext(), pli, pRetBlock); - } - - return umin; - } - - - virtual void processFunction(Function *pFunc) { - vector calls; - gatherCalls(pFunc, calls); - - for (size_t i = 0; i < calls.size(); i++){ - bChanged = true; - CallInst * pci = calls[i]; - - Function * umin = getFunction(); - - BasicBlock * pBlock = pci->getParent(); - DEBUG_VALUE(pBlock); - - std::vector args; - for (size_t i = 0; i < pci->arg_size(); i++){ - args.push_back(pci->getArgOperand(i)); - } - CallInst * pNew = CallInst::Create(getFunctionType(), umin, args, "ClamBCRemoveUMIN_", pci); - pci->replaceAllUsesWith(pNew); - pci->eraseFromParent(); - - DEBUG_VALUE(pBlock); - } - } - - public: - - PreservedAnalyses run(Module & m, ModuleAnalysisManager & MAM) - { - pMod = &m; - - DEBUGERR << "TODO: ADD umin detection to the validator" << "\n"; - - for (auto i = pMod->begin(), e = pMod->end(); i != e; i++) { - Function *pFunc = llvm::cast(i); - if (pFunc->isDeclaration()) { - continue; - } - - processFunction(pFunc); - } - - if (bChanged){ - return PreservedAnalyses::none(); - } else{ - return PreservedAnalyses::all(); - } - } - - virtual ~ClamBCRemoveUnsupportedIntrinsic () {} - }; - - /* - * Remove umin intrinsic because it's not supported by our runtime. - */ - struct ClamBCRemoveUMIN : public ClamBCRemoveUnsupportedIntrinsic - { - protected: - - FunctionType * uminType = nullptr; - IntegerType * uminArgType = nullptr; - - const char * const getIntrinsicName() const { - return "llvm.umin.i32" ; - } - - virtual llvm::IntegerType* getArgumentType() const { - - //if (nullptr == uminArgType){ - // uminArgType = Type::getInt32Ty(pMod->getContext()); - //} - - return uminArgType; - } - - virtual llvm::FunctionType * getFunctionType() const { - // if (nullptr == uminType){ - // IntegerType * it = getArgumentType(); - // uminType = FunctionType::get(it, {it, it}, false); - // } - return uminType; - } - - - public: - - ClamBCRemoveUMIN() { - DEBUGERR << "Constructor" << "\n"; - uminArgType = Type::getInt32Ty(pMod->getContext()); - DEBUGERR << "Constructor" << "\n"; - uminType = FunctionType::get(uminArgType , {uminArgType, uminArgType}, false); - DEBUGERR << "Constructor" << "\n"; - } - - virtual ~ClamBCRemoveUMIN() {} - - - }; // end of struct ClamBCRemoveUMIN - -} // end of anonymous namespace - -// This part is the new way of registering your pass -extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK -llvmGetPassPluginInfo() { - return { - LLVM_PLUGIN_API_VERSION, "ClamBCRemoveUMIN", "v0.1", - [](PassBuilder &PB) { - PB.registerPipelineParsingCallback( - [](StringRef Name, ModulePassManager &FPM, - ArrayRef) { - if(Name == "clambc-remove-umin"){ - FPM.addPass(ClamBCRemoveUMIN()); - return true; - } - return false; - } - ); - } - }; -} - - -