Skip to content

Commit

Permalink
Bug fixes to remove interpreter warning message
Browse files Browse the repository at this point in the history
Properly handle converting array GEP instructions, as well as adding
globals to the list twice to avoid a global ID of 0 or 1, since the
interpreter can't handle that.
  • Loading branch information
ragusaa committed Jun 22, 2022
1 parent 0381fdd commit 68dfdcd
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 2,248 deletions.
2 changes: 1 addition & 1 deletion clambcc/clambc-compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ def optimize(clangLLVM: ClangLLVM, inFile: str, outFile: str, sigFile: str, inpu
f' -clambc-preserve-abis' #remove fake function calls because O3 has already run
f' -clambc-remove-pointer-phis'
f' -dce'
f' -disable-loop-unrolling'
f' -disable-loop-vectorization'
f' -disable-slp-vectorization'
f' -globaldce'
Expand Down Expand Up @@ -547,7 +548,6 @@ def optimize(clangLLVM: ClangLLVM, inFile: str, outFile: str, sigFile: str, inpu
f' -clambc-verifier'
f' -verify'
f' -strip-debug-declare'
f' -clambc-gepsplitter-placeholder'
f' -clambc-lowering-final'
f' -clambc-trace'
f' -dce'
Expand Down
2 changes: 0 additions & 2 deletions libclambcc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ target_sources(clambcc_obj
PRIVATE
ClamBCLowering/ClamBCLowering.cpp
ClamBCVerifier/ClamBCVerifier.cpp
ClamBCRTChecks/ClamBCRTChecks.cpp
ClamBCLogicalCompiler/ClamBCLogicalCompiler.cpp
ClamBCRebuild/ClamBCRebuild.cpp
ClamBCGEPSplitter/ClamBCGEPSplitter.cpp
ClamBCTrace/ClamBCTrace.cpp
ClamBCModule/ClamBCModule.cpp
ClamBCWriter/ClamBCWriter.cpp
Expand Down
108 changes: 57 additions & 51 deletions libclambcc/ClamBCAnalyzer/ClamBCAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,61 +125,67 @@ bool ClamBCAnalyzer::runOnModule(Module &M)
std::set<ConstantExpr *> ces;
getDependentValues(gv, insts, globs, ces);

for (auto J : ces) {
ConstantExpr *CE = llvm::cast<ConstantExpr>(J);
// ClamAV bytecode doesn't support arbitrary constant expressions for
// globals, so introduce helper globals for nested constant expressions.
if (CE->getOpcode() != Instruction::GetElementPtr) {
if (CE->getOpcode() == Instruction::BitCast) {
GlobalVariable *GV = new GlobalVariable(M, CE->getType(), true,
GlobalValue::InternalLinkage,
CE, I->getName() + "_bc");
CEMap[CE] = GV;
continue;
/*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
* be incorrect in the interpreter. Look at line 2011 of bytecode.c
*/
for (size_t loop = 0; loop < 2; loop++){
for (auto J : ces) {
ConstantExpr *CE = llvm::cast<ConstantExpr>(J);
// ClamAV bytecode doesn't support arbitrary constant expressions for
// globals, so introduce helper globals for nested constant expressions.
if (CE->getOpcode() != Instruction::GetElementPtr) {
if (CE->getOpcode() == Instruction::BitCast) {
GlobalVariable *GV = new GlobalVariable(M, CE->getType(), true,
GlobalValue::InternalLinkage,
CE, I->getName() + "_bc");
CEMap[CE] = GV;
continue;
}
errs() << "UNSUPPORTED: " << *CE << "\n";
ClamBCStop("Unsupported constant expression", &M);
}
ConstantInt *C0 = dyn_cast<ConstantInt>(CE->getOperand(1));
ConstantInt *C1 = dyn_cast<ConstantInt>(CE->getOperand(2));
uint64_t v = C1->getValue().getZExtValue();
if (!C0->isZero()) {
errs() << "UNSUPPORTED: " << *CE << "\n";
ClamBCStop("Unsupported constant expression, nonzero first"
" index",
&M);
}
errs() << "UNSUPPORTED: " << *CE << "\n";
ClamBCStop("Unsupported constant expression", &M);
}
ConstantInt *C0 = dyn_cast<ConstantInt>(CE->getOperand(1));
ConstantInt *C1 = dyn_cast<ConstantInt>(CE->getOperand(2));
uint64_t v = C1->getValue().getZExtValue();
if (!C0->isZero()) {
errs() << "UNSUPPORTED: " << *CE << "\n";
ClamBCStop("Unsupported constant expression, nonzero first"
" index",
&M);
}

const DataLayout &dataLayout = pMod->getDataLayout();
std::vector<Value *> indices;
for (unsigned i = 1; i < CE->getNumOperands(); i++) {
indices.push_back(CE->getOperand(i));
}
Type *IP8Ty = PointerType::getUnqual(Type::getInt8Ty(CE->getContext()));
const DataLayout &dataLayout = pMod->getDataLayout();
std::vector<Value *> indices;
for (unsigned i = 1; i < CE->getNumOperands(); i++) {
indices.push_back(CE->getOperand(i));
}
Type *IP8Ty = PointerType::getUnqual(Type::getInt8Ty(CE->getContext()));

Type *type = CE->getOperand(0)->getType();
if (llvm::isa<PointerType>(type)) {
type = llvm::cast<PointerType>(type)->getElementType();
}
uint64_t idx = dataLayout.getIndexedOffsetInType(type, indices);

Value *Idxs[1];
Idxs[0] = ConstantInt::get(Type::getInt64Ty(CE->getContext()), idx);
Constant *C = ConstantExpr::getPointerCast(CE->getOperand(0), IP8Ty);
ConstantExpr *NewCE =
cast<ConstantExpr>(ConstantExpr::getGetElementPtr(nullptr, C,
Idxs));
NewCE = cast<ConstantExpr>(ConstantExpr::getPointerCast(NewCE,
CE->getType()));
if (CE != NewCE) {
CE->replaceAllUsesWith(NewCE);
Type *type = CE->getOperand(0)->getType();
if (llvm::isa<PointerType>(type)) {
type = llvm::cast<PointerType>(type)->getElementType();
}
uint64_t idx = dataLayout.getIndexedOffsetInType(type, indices);

Value *Idxs[1];
Idxs[0] = ConstantInt::get(Type::getInt64Ty(CE->getContext()), idx);
Constant *C = ConstantExpr::getPointerCast(CE->getOperand(0), IP8Ty);
ConstantExpr *NewCE =
cast<ConstantExpr>(ConstantExpr::getGetElementPtr(nullptr, C,
Idxs));
NewCE = cast<ConstantExpr>(ConstantExpr::getPointerCast(NewCE,
CE->getType()));
if (CE != NewCE) {
CE->replaceAllUsesWith(NewCE);
}
CE = NewCE;
GlobalVariable *GV = new GlobalVariable(M, CE->getType(), true,
GlobalValue::InternalLinkage,
CE,
I->getName() + "_" + Twine(v));
CEMap[CE] = GV;
}
CE = NewCE;
GlobalVariable *GV = new GlobalVariable(M, CE->getType(), true,
GlobalValue::InternalLinkage,
CE,
I->getName() + "_" + Twine(v));
CEMap[CE] = GV;
}

// Collect types of all globals.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ class ChangeMallocArgSize : public ModulePass
}
}

/*Yes, I know there is a "getTerminator" function, but I have come across blocks
* that have more than one branch instruction (I think it is a bug in the runtime), but
* until that is resolved, I want to use this function.*/
/* Yes, I know there is a "getTerminator" function, but I have come across blocks
* that have more than one branch instruction (I think it is a bug in the runtime), but
* until that is resolved, I want to use this function.
*/
Instruction* findTerminator(BasicBlock* pb)
{
Instruction* inst = nullptr;
Expand Down Expand Up @@ -129,7 +130,7 @@ class ChangeMallocArgSize : public ModulePass
}
}

pn->eraseFromParent(); //THIS IS NEW (SHOULDN'T BE A PROBLEM, BUT JUST IN CASE)
pn->eraseFromParent();
}
}
}
Expand Down
30 changes: 0 additions & 30 deletions libclambcc/ClamBCGEPSplitter/ClamBCGEPSplitter.cpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class ClamBCPrepareGEPsForWriter : public ModulePass

virtual int64_t getTypeSize(Type *pt)
{

int64_t size = pt->getScalarSizeInBits();
if (size) {
return size;
Expand Down Expand Up @@ -203,12 +204,22 @@ class ClamBCPrepareGEPsForWriter : public ModulePass
currType = llvm::cast<StructType>(tmp);
} else if (llvm::isa<ArrayType>(tmp)) {
currType = tmp;
}
}
} else if (ArrayType *pat = llvm::dyn_cast<ArrayType>(currType)) {

uint64_t size = getTypeSizeInBytes(pat->getArrayElementType());
Constant *pci = ConstantInt::get(vIdx->getType(), size);
ciAddend = BinaryOperator::Create(Instruction::Mul, pci, vIdx, "processGEPI_", pgepi);

Type *tmp = findTypeAtIndex(currType, ciIdx);
assert(tmp && "Should always be defined");

if (llvm::isa<StructType>(tmp)) {
currType = llvm::cast<StructType>(tmp);
} else if (llvm::isa<ArrayType>(tmp)) {
currType = tmp;
}

} else {
assert(0 && "Figure out what to do here");
}
Expand Down Expand Up @@ -314,6 +325,7 @@ class ClamBCPrepareGEPsForWriter : public ModulePass
return pInst;
}


virtual void processGEPI(GetElementPtrInst *pgepi)
{

Expand Down Expand Up @@ -363,7 +375,6 @@ class ClamBCPrepareGEPsForWriter : public ModulePass

virtual bool runOnModule(Module &m)
{

pMod = &m;
for (auto i = pMod->begin(), e = pMod->end(); i != e; i++) {
Function *pFunc = llvm::cast<Function>(i);
Expand Down
Loading

0 comments on commit 68dfdcd

Please sign in to comment.