From 5698a79b14daac4c180a7677f06c23ee01da2d98 Mon Sep 17 00:00:00 2001 From: Andy Ragusa Date: Wed, 10 Jan 2024 11:51:19 -0800 Subject: [PATCH] formatting cleanup --- clambcc/clambc-compiler.py | 2 +- libclambcc/CMakeLists.txt | 2 +- .../ClamBCLogicalCompiler.cpp | 95 +------------------ .../ClamBCRemoveFSHL/ClamBCRemoveFSHL.cpp | 44 ++++----- libclambcc/ClamBCRemoveFSHL/test.c.txt | 88 ----------------- .../ClamBCRemoveUndefs/ClamBCRemoveUndefs.cpp | 37 +------- libclambcc/ClamBCWriter/ClamBCWriter.cpp | 23 ----- 7 files changed, 27 insertions(+), 264 deletions(-) delete mode 100644 libclambcc/ClamBCRemoveFSHL/test.c.txt diff --git a/clambcc/clambc-compiler.py b/clambcc/clambc-compiler.py index 4e7ee6b78d..662590f429 100755 --- a/clambcc/clambc-compiler.py +++ b/clambcc/clambc-compiler.py @@ -671,7 +671,7 @@ def createInputSourceFile(clangLLVM: ClangLLVM, name: str, args: list, options: ] OPTIMIZE_LOADS=[ f"--load {SHARED_OBJ_DIR}/libclambccommon.so" - , f"--load-pass-plugin {SHARED_OBJ_DIR}/libclambcremoveundefs.so" +# , f"--load-pass-plugin {SHARED_OBJ_DIR}/libclambcremoveundefs.so" , f"--load-pass-plugin {SHARED_OBJ_DIR}/libclambcpreserveabis.so" , f"--load-pass-plugin {SHARED_OBJ_DIR}/libclambcremoveunsupportedicmpintrinsics.so" , f"--load-pass-plugin {SHARED_OBJ_DIR}/libclambcremoveusub.so" diff --git a/libclambcc/CMakeLists.txt b/libclambcc/CMakeLists.txt index 8070347b2d..88ea7ce2c8 100644 --- a/libclambcc/CMakeLists.txt +++ b/libclambcc/CMakeLists.txt @@ -3,7 +3,7 @@ add_subdirectory(ClamBCLogicalCompiler) add_subdirectory(ClamBCLogicalCompilerHelper) -add_subdirectory(ClamBCRemoveUndefs) +#add_subdirectory(ClamBCRemoveUndefs) add_subdirectory(ClamBCPreserveABIs) add_subdirectory(ClamBCAnalyzer) add_subdirectory(Common) diff --git a/libclambcc/ClamBCLogicalCompiler/ClamBCLogicalCompiler.cpp b/libclambcc/ClamBCLogicalCompiler/ClamBCLogicalCompiler.cpp index 76ad3d38ab..3317565c79 100644 --- a/libclambcc/ClamBCLogicalCompiler/ClamBCLogicalCompiler.cpp +++ b/libclambcc/ClamBCLogicalCompiler/ClamBCLogicalCompiler.cpp @@ -937,24 +937,6 @@ class LogicalCompiler } Instruction *pInst = llvm::cast(I); -#if 0 - /*Look through all operands of the instruction and add the - * constants to the logical map, so that we won't fail to create - * the siganture if O3 changes a logical expression to a constant - * at compile time. - */ - for (size_t i = 0; i < pInst->getNumOperands(); i++){ - if (ConstantInt * pci = llvm::dyn_cast(pInst->getOperand(i))){ - if (pci->isOne()){ - LogicalNode * ln = LogicalNode::getTrue(allNodes) ; - Map[pci] = ln; - } else if (pci->isZero()){ - LogicalNode * ln = LogicalNode::getTrue(allNodes) ; - Map[pci] = ln; - } - } - } -#endif switch (I->getOpcode()) { case Instruction::Load: @@ -992,30 +974,6 @@ class LogicalCompiler LogicalMap::iterator FalseNode = Map.find(SI->getFalseValue()); -#if 0 - if (Map.end() == TrueNode){ - Value * pv = SI->getTrueValue(); - if (ConstantInt * pci = llvm::dyn_cast(pv)){ - if (pci->isOne()){ - LogicalNode * ln = LogicalNode::getTrue(allNodes) ; - Map[SI->getTrueValue()] = ln; - TrueNode = Map.find(SI->getTrueValue()); - } - } - } - -#endif - - -#if 0 - if (CondNode == Map.end() || TrueNode == Map.end() || FalseNode == Map.end()) { - assert (0 && "FTT"); - printDiagnostic("Logical signature: select operands must be logical" - " expressions", - SI); - return false; - } -#else /*O3 creates blocks that look like the following, which are legitimate blocks. * This is essentially an AND of all the %cmp.i instructions. * Since the cmp instructions all have false at the end, comparisons will be skipped @@ -1058,7 +1016,7 @@ class LogicalCompiler SI); return false; } -#endif + // select cond, trueval, falseval -> cond && trueval || !cond && falseval LogicalNode *N = nullptr; LogicalNode *NotCond = nullptr; @@ -1770,21 +1728,6 @@ bool ClamBCLogicalCompiler::compileVirusNames(Module &M, unsigned kind) bool Valid = true; for (auto I : F->users()) { -#if 0 - Value *pv = nullptr; - pv = llvm::cast(I); - CallSite CS(pv); - if (!CS.getInstruction()) { - continue; - } - if (CS.getCalledFunction() != F) { - printDiagnostic("setvirusname can only be directly called", - CS.getInstruction()); - Valid = false; - continue; - } - assert(CS.arg_size() == 2 && "setvirusname has 2 args"); -#else CallInst * pCallInst = llvm::cast(I); if (nullptr == pCallInst){ assert (0 && "NOT sure how this is possible"); @@ -1801,7 +1744,6 @@ bool ClamBCLogicalCompiler::compileVirusNames(Module &M, unsigned kind) Valid = false; continue; } -#endif if (2 != pCallInst->arg_size()){ printDiagnostic("setvirusname has 2 args", pCallInst); @@ -1811,10 +1753,6 @@ bool ClamBCLogicalCompiler::compileVirusNames(Module &M, unsigned kind) std::string param; llvm::StringRef sr; -#if 0 - Value *V = CS.getArgument(0); -#else -#endif Value * V = llvm::cast(pCallInst->arg_begin()); if (nullptr == V){ printDiagnostic("Invalid argument passed to setvirusname", pCallInst); @@ -1846,30 +1784,19 @@ bool ClamBCLogicalCompiler::compileVirusNames(Module &M, unsigned kind) Value *C = builder.CreateGlobalStringPtr(fullname.c_str()); IntegerType *I32Ty = Type::getInt32Ty(M.getContext()); -#if 0 - CS.setArgument(0, C); - CS.setArgument(1, ConstantInt::get(I32Ty, fullname.size())); -#else pCallInst->setArgOperand(0, C); pCallInst->setArgOperand(1, ConstantInt::get(I32Ty, fullname.size())); -#endif } return Valid; } -#if 0 -bool ClamBCLogicalCompiler::runOnModule(Module &M) -#else PreservedAnalyses ClamBCLogicalCompiler::run(Module & M, ModuleAnalysisManager & MAM) -#endif { bool Valid = true; LogicalSignature = ""; virusnames = ""; pMod = &M; - //dumpPHIGraphs(); - // Handle virusname unsigned kind = 0; GlobalVariable *GVKind = M.getGlobalVariable("__clambc_kind"); @@ -1896,12 +1823,8 @@ bool ClamBCLogicalCompiler::runOnModule(Module &M) } if (F) { -#if 0 - LoopInfo &li = getAnalysis(*F).getLoopInfo(); -#else FunctionAnalysisManager &fam = MAM.getResult(M).getManager(); LoopInfo * li = &fam.getResult(*F); -#endif if (functionHasLoop(F, *li)) { printDiagnostic("Logical signature: loop/recursion not supported", F); Valid = false; @@ -2035,20 +1958,6 @@ bool ClamBCLogicalCompiler::runOnModule(Module &M) return PreservedAnalyses::none(); } -#if 0 -const PassInfo *const ClamBCLogicalCompilerID = &X; llvm::ModulePass *createClamBCLogicalCompiler() -{ - return new ClamBCLogicalCompiler(); -} -#endif - - -#if 0 -char ClamBCLogicalCompiler::ID = 0; -RegisterPass X("clambc-lcompiler", - "ClamAV Logical Compiler"); -#else - // This part is the new way of registering your pass extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo() { @@ -2068,8 +1977,6 @@ llvmGetPassPluginInfo() { } }; } -#endif - } // namespace diff --git a/libclambcc/ClamBCRemoveFSHL/ClamBCRemoveFSHL.cpp b/libclambcc/ClamBCRemoveFSHL/ClamBCRemoveFSHL.cpp index 80c20230ec..dad1fc1466 100644 --- a/libclambcc/ClamBCRemoveFSHL/ClamBCRemoveFSHL.cpp +++ b/libclambcc/ClamBCRemoveFSHL/ClamBCRemoveFSHL.cpp @@ -54,29 +54,28 @@ 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){ - uint8_t ret = 0; - uint8_t bitwidth = 8; - uint8_t bitIdx = (2 * bitwidth) - (shift % bitwidth) - 1; - uint8_t bit; - - for (size_t i = 0; i < bitwidth; i++){ - if (bitIdx >= bitwidth) { - bit = (left & (1 << (bitIdx - bitwidth))) ? 1 : 0; - ret |= (bit << ((bitwidth - 1) - i)); - } else { - bit = right & (1 << bitIdx); - ret |= (bit << ((bitwidth - 1) - i)); - } - bitIdx-- ; - } - - return ret; -} + static uint8_t fshl8_noshifts(uint8_t left, uint8_t right, uint8_t shift){ + uint8_t ret = 0; + uint8_t bitwidth = 8; + uint8_t bitIdx = (2 * bitwidth) - (shift % bitwidth) - 1; + uint8_t bit; + + for (size_t i = 0; i < bitwidth; i++){ + if (bitIdx >= bitwidth) { + bit = (left & (1 << (bitIdx - bitwidth))) ? 1 : 0; + ret |= (bit << ((bitwidth - 1) - i)); + } else { + bit = right & (1 << bitIdx); + ret |= (bit << ((bitwidth - 1) - i)); + } + bitIdx-- ; + } -#endif + return ret; + } + */ assert (0 && "Unimplemented"); } @@ -138,8 +137,7 @@ static uint8_t fshl8_noshifts(uint8_t left, uint8_t right, uint8_t shift){ virtual ~ClamBCRemoveFSHL() {} - /*TODO: - * Add this to validator.*/ + /*TODO: Add this to validator.*/ PreservedAnalyses run(Module & m, ModuleAnalysisManager & MAM) { pMod = &m; diff --git a/libclambcc/ClamBCRemoveFSHL/test.c.txt b/libclambcc/ClamBCRemoveFSHL/test.c.txt deleted file mode 100644 index e1477c35a4..0000000000 --- a/libclambcc/ClamBCRemoveFSHL/test.c.txt +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include -#include -#include - -#define LIDX 1 -#define RIDX 2 -#define SIDX 3 -#define BWIDX 4 - -static uint8_t fshl8_noshifts(uint8_t left, uint8_t right, uint8_t shift){ - uint8_t ret = 0; - uint8_t bitwidth = 8; - uint8_t bitIdx = (2 * bitwidth) - (shift % bitwidth) - 1; - uint8_t bit; - - for (size_t i = 0; i < bitwidth; i++){ - if (bitIdx >= bitwidth) { - bit = (left & (1 << (bitIdx - bitwidth))) ? 1 : 0; - ret |= (bit << ((bitwidth - 1) - i)); - } else { - bit = right & (1 << bitIdx); - ret |= (bit << ((bitwidth - 1) - i)); - } - bitIdx-- ; - } - - return ret; -} - -static uint8_t fshl8_shifts(uint8_t left, uint8_t right, uint8_t shift){ - - uint16_t tmp = (left << 8) | right; - tmp <<= (shift % 8); -#if 0 - tmp = (tmp & 0xff00) >> 8; -#else - tmp = tmp >> 8; -#endif - - return (uint8_t) (tmp & 0xff); - -} - -static void fshl8ascii(char ** argv){ - uint8_t left = atoi(argv[LIDX]); - uint8_t right = atoi(argv[RIDX]); - uint8_t shift = atoi(argv[SIDX]); - - uint8_t shiftsRet, noshiftsRet; - - shiftsRet = fshl8_shifts(left, right, shift); - - noshiftsRet = fshl8_noshifts(left, right, shift); - - if (shiftsRet != noshiftsRet){ - fprintf(stderr, "Incorrect value = '0x%x'\n", noshiftsRet); - fprintf(stderr, "Correct value = '0x%x'\n", shiftsRet); - exit(11); - } - - printf ("left = 0x%x, right = 0x%x, shift = 0x%x, result = 0x%x\n", left, right, shift, shiftsRet); - -} - -int main(int argc, char ** argv){ - int ret = -1; - uint8_t bitwidth = 0; - if (5 > argc){ - fprintf(stderr, "usage: %s \n", argv[0]); - goto done; - } - - bitwidth = atoi(argv[BWIDX]); - printf("bitwidth = '%d'\n", bitwidth); - switch (bitwidth){ - case 8: - fshl8ascii(argv); - break; - default: - fprintf(stderr, "Unsupported bitwidth of '%d'\n", bitwidth); - goto done; - } - - ret = 0; -done: - return ret; -} diff --git a/libclambcc/ClamBCRemoveUndefs/ClamBCRemoveUndefs.cpp b/libclambcc/ClamBCRemoveUndefs/ClamBCRemoveUndefs.cpp index 4d6a717268..36c1b36264 100644 --- a/libclambcc/ClamBCRemoveUndefs/ClamBCRemoveUndefs.cpp +++ b/libclambcc/ClamBCRemoveUndefs/ClamBCRemoveUndefs.cpp @@ -18,15 +18,9 @@ using namespace llvm; -#if 0 +/* THIS APPEARS TO NO LONGER BE NEEDED. LEAVING IN PLACE DURING THE RC PHASE, JUST IN CASE. */ -THIS APPEARS TO NO LONGER BE NEEDED. LEAVING IN PLACE DURING THE RC PHASE, JUST IN CASE. - - - -#endif - @@ -75,17 +69,9 @@ struct ClamBCRemoveUndefs : public PassInfoMixin FunctionType *rterrTy = FunctionType::get( Type::getInt32Ty(BB->getContext()), {Type::getInt32Ty(BB->getContext())}, false); -#if 0 - Constant *func_abort = - BB->getParent()->getParent()->getOrInsertFunction("abort", abrtTy); - Constant *func_rterr = - BB->getParent()->getParent()->getOrInsertFunction("bytecode_rt_error", rterrTy); -#else - //DEBUGERR << "DON'T KNOW IF THIS WILL WORK, REMOVE IFDEF LATER" << "\n"; FunctionCallee func_abort = BB->getParent()->getParent()->getOrInsertFunction("abort", abrtTy); FunctionCallee func_rterr = BB->getParent()->getParent()->getOrInsertFunction("bytecode_rt_error", rterrTy); -#endif BasicBlock *abort = BasicBlock::Create(BB->getContext(), "rterr.trig", BB->getParent()); Constant *PN = ConstantInt::get(Type::getInt32Ty(BB->getContext()), 99); if (MDDbgKind) { @@ -243,17 +229,11 @@ struct ClamBCRemoveUndefs : public PassInfoMixin } public: - //static char ID; - ClamBCRemoveUndefs() - /* : ModulePass(ID) */ {} + ClamBCRemoveUndefs() {} virtual ~ClamBCRemoveUndefs() {} -#if 0 - bool runOnModule(Module &m) override -#else PreservedAnalyses run(Module & m, ModuleAnalysisManager & MAM) -#endif { /*This no longer appears to be needed. Will keep it during the -rc phase and then remove.*/ return PreservedAnalyses::all(); @@ -273,26 +253,15 @@ struct ClamBCRemoveUndefs : public PassInfoMixin delLst[i]->eraseFromParent(); } -#if 0 - return bChanged; -#else if (bChanged){ return PreservedAnalyses::none(); } return PreservedAnalyses::all(); -#endif } }; // end of struct ClamBCRemoveUndefs } // end of anonymous namespace -#if 0 -char ClamBCRemoveUndefs::ID = 0; -static RegisterPass X("clambc-remove-undefs", "Remove Undefs", - false /* Only looks at CFG */, - false /* Analysis Pass */); -#else - // This part is the new way of registering your pass extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo() { @@ -313,4 +282,4 @@ llvmGetPassPluginInfo() { }; } -#endif + diff --git a/libclambcc/ClamBCWriter/ClamBCWriter.cpp b/libclambcc/ClamBCWriter/ClamBCWriter.cpp index e4d4867061..0d86f4b50c 100644 --- a/libclambcc/ClamBCWriter/ClamBCWriter.cpp +++ b/libclambcc/ClamBCWriter/ClamBCWriter.cpp @@ -790,18 +790,6 @@ class ClamBCWriter : public PassInfoMixin, public InstVisitor\n"; - return; - - uint32_t tid = pAnalyzer->getHighestTID(); - DEBUG_NONPOINTER(tid); - - - } -#endif - void fixGEPs(Function *pFunc) { std::vector geps; @@ -817,7 +805,6 @@ class ClamBCWriter : public PassInfoMixin, public InstVisitorgetContext()); CastInst *ci = CastInst::CreatePointerCast(operand, pDestType, "ClamBCWriter_fixGEPs", pGep); - //updateAnalyzer(ci); Value *index = pGep->getOperand(1); @@ -834,10 +821,8 @@ class ClamBCWriter : public PassInfoMixin, public InstVisitorgetType(), multiplier); - //updateAnalyzer(cMultiplier); Value *newIndex = BinaryOperator::Create(Instruction::Mul, cMultiplier, index, "ClamBCWriter_fixGEPs", pGep); - //updateAnalyzer(newIndex); GetElementPtrInst *pNew = nullptr; @@ -847,7 +832,6 @@ class ClamBCWriter : public PassInfoMixin, public InstVisitorgetPointerElementType(); } pNew = GetElementPtrInst::Create(pt, ci, newIndex, "ClamBCWriter_fixGEPs", pGep); - //updateAnalyzer(pNew); } else { assert(0 && "DON'T THINK THIS CAN HAPPEN"); } @@ -855,7 +839,6 @@ class ClamBCWriter : public PassInfoMixin, public InstVisitorgetType(), "ClamBCWriter_fixGEPs", pGep); - //updateAnalyzer(ci); pGep->replaceAllUsesWith(ci); pGep->eraseFromParent(); @@ -1413,12 +1396,6 @@ class ClamBCWriter : public PassInfoMixin, public InstVisitor X("clambc-writer", "ClamBCWriter Pass", - false /* Only looks at CFG */, - false /* Analysis Pass */); -#endif bool ClamBCWriter::doInitialization(Module &M) {