Skip to content

Commit

Permalink
CLEANUP COMMIT, WORKED BEFORE
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusaa committed Dec 14, 2023
1 parent 0399be4 commit a47af53
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 109 deletions.
1 change: 0 additions & 1 deletion clambcc/clambc-compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,6 @@ def createInputSourceFile(clangLLVM: ClangLLVM, name: str, args: list, options:
OPTIMIZE_OPTIONS.append("-opaque-pointers=0")

OPTIMIZE_PASSES = ["function(mem2reg)"
, 'clambc-remove-undefs'
, 'verify'
, 'clambc-preserve-abis'
, 'verify'
Expand Down
89 changes: 2 additions & 87 deletions libclambcc/ClamBCAnalyzer/ClamBCAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ using namespace llvm;

AnalysisKey ClamBCAnalyzer::Key;

//extern cl::opt<bool> WriteDI;

static unsigned getSpecialIndex(StringRef Name)
{
// Ensure main/entrypoint is sorted before any other function
Expand Down Expand Up @@ -69,16 +67,10 @@ static bool compare_lt_functions(Function *A, Function *B)
return NA.compare(NB) < 0;
}

#if 0
bool ClamBCAnalysis::runOnModule(Module &M)
#else
void ClamBCAnalysis::run(Module & m)
#endif
{
pMod = &m;

DEBUGERR << "ClamBCAnalysis::run::" << __LINE__ << "><END>\n";

// Determine bytecode kind, default is 0 (generic).
kind = 0;
GlobalVariable *GVKind = pMod->getGlobalVariable("__clambc_kind");
Expand All @@ -99,9 +91,7 @@ void ClamBCAnalysis::run(Module & m)
if (!getConstantStringInfo(C, c)) {
ClamBCStop("Failed to extract copyright string\n", pMod);
}
// copyright = strdup(c.c_str());
copyright = c.str();
// G->setLinkage(GlobalValue::InternalLinkage);
// Do not set the linkage type to internal because the optimizer will remove it.
}

Expand All @@ -124,18 +114,13 @@ void ClamBCAnalysis::run(Module & m)
}

std::vector<const Type *> types;
// cid=1;
fid = 1;
for (Module::global_iterator I = pMod->global_begin(); I != pMod->global_end(); ++I) {
GlobalVariable *gv = llvm::cast<GlobalVariable>(I);
//DEBUG_VALUE(gv);
std::set<Instruction *> insts;
std::set<GlobalVariable *> globs;
std::set<ConstantExpr *> ces;
getDependentValues(gv, insts, globs, ces);
//DEBUG_NONPOINTER(insts.size());
//DEBUG_NONPOINTER(globs.size());
//DEBUG_NONPOINTER(ces.size());

/*It is necessary to add these twice, because there is a condition we
* can't use global idx 0 or 1 in the interpreter, since the size will
Expand All @@ -157,12 +142,8 @@ void ClamBCAnalysis::run(Module & m)
errs() << "UNSUPPORTED: " << *CE << "\n";
ClamBCStop("Unsupported constant expression", pMod);
}
//DEBUG_VALUE(CE);
//DEBUG_NONPOINTER(CE->getNumOperands());
ConstantInt *C0 = dyn_cast<ConstantInt>(CE->getOperand(1));
ConstantInt *C1 = dyn_cast<ConstantInt>(CE->getOperand(2));
//DEBUG_NONPOINTER(C0);
//DEBUG_NONPOINTER(C1);
uint64_t v = C1->getValue().getZExtValue();
if (!C0->isZero()) {
errs() << "UNSUPPORTED: " << *CE << "\n";
Expand All @@ -177,29 +158,7 @@ void ClamBCAnalysis::run(Module & m)
indices.push_back(CE->getOperand(i));
}
Type *IP8Ty = PointerType::getUnqual(Type::getInt8Ty(CE->getContext()));

#if 0
Type *type = CE->getOperand(0)->getType();
if (llvm::isa<PointerType>(type)) {
#if 0
type = llvm::cast<PointerType>(type)->getElementType();
#else
if (llvm::isa<GEPOperator>(CE)){
GEPOperator * pgep = llvm::cast<GEPOperator>(CE);
type = pgep->getPointerOperandType();
} else if (llvm::isa<BitCastOperator>(CE)){
BitCastOperator
} else {
llvm::errs() << "<" << __LINE__ << ">" << "https://llvm.org/docs/OpaquePointers.html" << "<END>\n";
llvm::errs() << "<" << __LINE__ << ">" << *CE << "<END>\n";
assert (0 && "FIGURE OUT WHAT TO DO HERE");
}
#endif

}
#else
Type * type = getResultType(CE);
#endif

uint64_t idx = dataLayout.getIndexedOffsetInType(type, indices);

Expand All @@ -226,10 +185,6 @@ void ClamBCAnalysis::run(Module & m)
// Collect types of all globals.
const Type *Ty = I->getType();
Ty = I->getValueType();
//DEBUGERR << "FIGURE OUT IF THIS IS RIGHT" << "<END>\n";

//DEBUG_VALUE(Ty);
//DEBUG_VALUE(I->getValueType());
if (!typeIDs.count(Ty)) {
extraTypes.push_back(Ty);
typeIDs[Ty] = tid++;
Expand All @@ -243,7 +198,6 @@ void ClamBCAnalysis::run(Module & m)
Function *F = &*I;
++I;
functions.push_back(F);
// F->removeFromParent();
}

for (size_t i = 0; i < functions.size(); i++){
Expand Down Expand Up @@ -297,9 +251,6 @@ void ClamBCAnalysis::run(Module & m)
}
continue;
}
//DEBUGERR << "THIS IS THE PLACE TO LOOK" << "<END>\n";
//DEBUG_NONPOINTER(F.getName());
//DEBUG_VALUE(&F);
functionIDs[&F] = fid++;
for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
const Type *Ty = I->getType();
Expand All @@ -310,9 +261,7 @@ void ClamBCAnalysis::run(Module & m)
typeIDs[Ty] = tid++;
}

//DEBUG_VALUE(&F);
for (inst_iterator II = inst_begin(F), IE = inst_end(F); II != IE; ++II) {
//DEBUG_VALUE(II);
const Type *Ty;
// Skip debug intrinsics, so we don't add llvm.dbg.* types
if (isa<DbgInfoIntrinsic>(&*II)) {
Expand All @@ -330,8 +279,6 @@ void ClamBCAnalysis::run(Module & m)
// Collect types of all instructions.
if (const AllocaInst *AI = dyn_cast<AllocaInst>(&*II)) {
Ty = AI->getAllocatedType();
//DEBUG_VALUE(AI);
//DEBUG_VALUE(Ty);
} else {
Ty = II->getType();
}
Expand All @@ -345,31 +292,25 @@ void ClamBCAnalysis::run(Module & m)
}
}

//DEBUG_VALUE(Ty);
for (size_t i = 0; i < II->getNumOperands(); i++){
Value * operand = II->getOperand(i);
if (llvm::isa<BasicBlock>(operand)){
continue;
}
Type * pt = operand->getType();
//DEBUG_VALUE(pt);
if (0 == typeIDs.count(pt)){
//DEBUG_VALUE(pt);
types.push_back(pt);
extraTypes.push_back(pt);
typeIDs[pt] = tid++;
}
}


if (typeIDs.count(Ty)) {
continue;
}
types.push_back(Ty);
extraTypes.push_back(Ty);
typeIDs[Ty] = tid++;


}
}

Expand Down Expand Up @@ -406,17 +347,13 @@ void ClamBCAnalysis::run(Module & m)
}

printGlobals(startTID);

// DEBUGERR << "ClamBCAnalysis::run::" << __LINE__ << "><END>\n";
//return false;
}

void ClamBCAnalysis::printGlobals(uint16_t stid)
{
llvm::Module &M = *pMod;
// Describe types
maxApi = 0;
// std::vector<const Function *> apis;
for (Module::iterator I = pMod->begin(), E = pMod->end(); I != E; ++I) {
llvm::Function *pFunc = llvm::cast<llvm::Function>(I);
// Skip dead declarations
Expand Down Expand Up @@ -478,13 +415,9 @@ void ClamBCAnalysis::printGlobals(uint16_t stid)
specialGlobals.insert(GV);
}

//DEBUGERR << "PRINTING ALL GLOBALS IN ANALYZER" << "<END>\n";

// std::vector<Constant *> globalInits;
globalInits.push_back(0); // ConstantPointerNul placeholder
for (Module::global_iterator I = pMod->global_begin(), E = pMod->global_end(); I != E; ++I) {
GlobalVariable *pgv = llvm::cast<GlobalVariable>(I);
//DEBUG_VALUE(pgv);
if (specialGlobals.count(pgv)) {
continue;
}
Expand All @@ -511,15 +444,15 @@ void ClamBCAnalysis::printGlobals(uint16_t stid)
&M);
}
Constant *C = pgv->getInitializer();
if (C->use_empty())
if (C->use_empty()) {
continue;
}
globalInits.push_back(C);
globals[pgv] = i++;
if (i >= 32768) {
ClamBCStop("Attempted to use more than 32k global variables!", &M);
}
}
DEBUGERR << "DONE PRINTING ALL GLOBALS IN ANALYZER" << "<END>\n";

if (anyDbgIds) {
mds.resize(dbgMap.size());
Expand Down Expand Up @@ -664,22 +597,6 @@ void ClamBCAnalysis::populateAPIMap()
apiMap["bzip2_done"] = id++;
}

#if 0
void ClamBCAnalysis::getAnalysisUsage(AnalysisUsage &AU) const
{
// Preserve the CFG, we only eliminate PHIs, and introduce some
// loads/stores.
AU.setPreservesAll();
}
#endif
#if 0
char ClamBCAnalysis::ID = 0;
static RegisterPass<ClamBCAnalysis> X("clambc-analyzer",
"ClamAV bytecode register allocator");

const PassInfo *const ClamBCAnalysisID = &X;
#else

// This part is the new way of registering your pass
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
llvmGetPassPluginInfo() {
Expand All @@ -696,5 +613,3 @@ llvmGetPassPluginInfo() {
}


#endif

21 changes: 1 addition & 20 deletions libclambcc/ClamBCAnalyzer/ClamBCAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,34 +135,15 @@ class ClamBCAnalysis
}

~ClamBCAnalysis() {}
//virtual bool runOnModule(llvm::Module &m) override;
virtual void run(llvm::Module & m);

#if 0
virtual void getAnalysisUsage(llvm::AnalysisUsage &au) const override;
#endif

virtual uint32_t getTypeID(const llvm::Type *const t)
{
TypeMapTy::iterator I = typeIDs.find(t);
if (I == typeIDs.end()){
DEBUG_NONPOINTER("BAD VALUE");
DEBUG_VALUE(t);
}
#if 0
static int first = 1;
if (first) {
for (auto i = typeIDs.begin(), e = typeIDs.end(); i != e; i++)
{
DEBUGERR << *(i->first) << "::" << i->second << "<END>\n";
}
first = 0;
}
#endif





assert((I != typeIDs.end()) && "Type ID requested for unknown type");
return I->second;
Expand Down Expand Up @@ -284,7 +265,7 @@ class ClamBCAnalysis
}
};

class ClamBCAnalyzer : public llvm::AnalysisInfoMixin<ClamBCAnalyzer> //llvm::ModulePass
class ClamBCAnalyzer : public llvm::AnalysisInfoMixin<ClamBCAnalyzer>
{
protected:
ClamBCAnalysis clamBCAnalysis;
Expand Down
3 changes: 2 additions & 1 deletion libclambcc/ClamBCRemoveUndefs/ClamBCRemoveUndefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ struct ClamBCRemoveUndefs : public PassInfoMixin<ClamBCRemoveUndefs >
PreservedAnalyses run(Module & m, ModuleAnalysisManager & MAM)
#endif
{
DEBUGERR << "This was causing issues in testing, so removed. Still need to verify that we no longer need it" << "<END>\n"; return PreservedAnalyses::all();
/*This no longer appears to be needed. Will keep it during the -rc phase and then remove.*/
return PreservedAnalyses::all();

pMod = &m;

Expand Down

0 comments on commit a47af53

Please sign in to comment.