Skip to content

Commit

Permalink
Merge branch 'CLAM-1486-script2exe' into 'main'
Browse files Browse the repository at this point in the history
Test fix for CLAM-1486

See merge request clamav/clamav-bytecode-compiler!17
  • Loading branch information
ragusaa committed Jun 4, 2021
2 parents 05f3206 + e06e4fd commit bc15034
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions libclambcc/ClamBCRemovePointerPHIs/ClamBCRemovePointerPHIs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ class ClambcRemovePointerPHIs : public FunctionPass
return pRet;
}

bool phiIsDependent(GetElementPtrInst *pgepi, PHINode *phiNode)
/*Returns true if phiNode is a dependent instruction of pVal.*/
bool phiIsDependent(Value *pVal, PHINode *phiNode)
{

std::set<llvm::Instruction *> insts;
std::set<llvm::GlobalVariable *> globs;

getDependentValues(pgepi, insts, globs);
getDependentValues(pVal, insts, globs);

for (auto i : insts) {
if (phiNode == i) {
Expand Down Expand Up @@ -195,10 +196,17 @@ class ClambcRemovePointerPHIs : public FunctionPass

for (size_t i = 0; i < pn->getNumIncomingValues(); i++) {
Value *incoming = getOrigValue(pn->getIncomingValue(i));

//If this value is dependent on the phi node, then it cannot
//be what the PHINode was initialized to the first time the
//block was entered, which is what we are looking for.
if (not (phiIsDependent(incoming, pn))){
continue;
}

if (GetElementPtrInst *p = llvm::dyn_cast<GetElementPtrInst>(pn->getIncomingValue(i))) {
//Need to fix the index to be whatever the index is for the GetElementPtrInst here
//Replace initValue with the index operand of the GetElementPtrInst here.
for (auto idx = p->idx_begin(), idxe = p->idx_end(); idx != idxe; idx++) {

initValue = llvm::cast<Value>(idx);
}
if (initValue->getType() != pType) {
Expand Down Expand Up @@ -238,15 +246,14 @@ class ClambcRemovePointerPHIs : public FunctionPass
BasicBlock *pred = findPredecessor(idxNode->getParent(), pgepi->getParent(), omitNodes);
assert(pred && "Could not find predecessor");
idxNode->addIncoming(add, pred);
//delLst.push_back(pgepi);

Instruction *gepiNew = GetElementPtrInst::Create(nullptr, pBasePtr, add, "ClamBCRemovePointerPHIs_gepi_", pgepi);
if (pgepi->getType() != gepiNew->getType()) {
gepiNew = CastInst::CreatePointerCast(gepiNew, pgepi->getType(), "ClamBCRemovePointerPHIs_cast_", pgepi);
Instruction *gepiNew2 = gepiNew;
if (pgepi->getType() != gepiNew2->getType()) {
gepiNew2 = CastInst::CreatePointerCast(gepiNew2, pgepi->getType(), "ClamBCRemovePointerPHIs_cast_", pgepi);
newInsts.push_back(gepiNew2);
}
newInsts.push_back(gepiNew);

pgepi->replaceAllUsesWith(gepiNew);
pgepi->replaceAllUsesWith(gepiNew2);
}
}
}
Expand All @@ -263,13 +270,10 @@ class ClambcRemovePointerPHIs : public FunctionPass

for (size_t j = 0; j < newInsts.size(); j++) {
Instruction *pJ = newInsts[j];
if (pUser->getParent() == pJ->getParent()) { //same basic block

for (size_t k = 0; k < pUser->getNumOperands(); k++) {
if (pUser->getOperand(k) == pn) {
pUser->setOperand(k, pJ);
break;
}
for (size_t k = 0; k < pUser->getNumOperands(); k++) {
if (pUser->getOperand(k) == pn) {
pUser->setOperand(k, pJ);
break;
}
}
}
Expand Down

0 comments on commit bc15034

Please sign in to comment.