Skip to content

Commit

Permalink
修复cse
Browse files Browse the repository at this point in the history
  • Loading branch information
KomiMoe committed May 26, 2022
1 parent 31c84f4 commit 9910301
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
13 changes: 5 additions & 8 deletions llvm/lib/Transforms/Obfuscation/ObfuscationPassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,9 @@ struct ObfuscationPassManager : public ModulePass {
}

bool runOnModule(Module &M) override {

if (EnableIRStringEncryption) {
errs() << "\"-irobf-cse\" WAS OBSOLETED ,PLEASE REMOVE IT IN COMMAND.\n";
}

if (EnableIndirectBr || EnableIndirectCall || EnableIndirectGV ||
EnableIRFlattening) {
EnableIRFlattening || EnableIRStringEncryption) {
EnableIRObfusaction = true;
}

Expand All @@ -124,10 +121,10 @@ struct ObfuscationPassManager : public ModulePass {

std::unique_ptr<ObfuscationOptions> Options(getOptions());

if (Options->EnableCSE) {
errs() << "\"CSE\" WAS OBSOLETED ,PLEASE REMOVE IT IN OPTIONS.\n";
if (EnableIRStringEncryption || Options->EnableCSE) {
add(llvm::createStringEncryptionPass(true, Options.get()));
}

add(llvm::createFlatteningPass(EnableIRFlattening || Options->EnableCFF, Options.get()));
add(llvm::createIndirectBranchPass(
EnableIndirectBr || Options->EnableIndirectBr, Options.get()));
Expand Down
23 changes: 14 additions & 9 deletions llvm/lib/Transforms/Obfuscation/StringEncryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ bool StringEncryption::runOnModule(Module &M) {
LLVMContext &Ctx = M.getContext();
ConstantInt *Zero = ConstantInt::get(Type::getInt32Ty(Ctx), 0);
for (GlobalVariable &GV : M.globals()) {
if (!GV.isConstant() || !GV.hasInitializer()) {
if (!GV.isConstant() || !GV.hasInitializer() ||
GV.hasDLLExportStorageClass() || GV.isDLLImportDependent()) {
continue;
}
Constant *Init = GV.getInitializer();
Expand Down Expand Up @@ -258,7 +259,7 @@ Function *StringEncryption::buildDecryptFunction(Module *M, const StringEncrypti
IRB.SetInsertPoint(Enter);
ConstantInt *KeySize = ConstantInt::get(Type::getInt32Ty(Ctx), Entry->EncKey.size());
Value *EncPtr = IRB.CreateInBoundsGEP(
Data->getType()->getScalarType()->getPointerElementType(), Data, KeySize);
Data->getType()->getPointerElementType(), Data, KeySize);
Value *DecStatus = IRB.CreateLoad(
Entry->DecStatus->getType()->getPointerElementType(), Entry->DecStatus);
Value *IsDecrypted = IRB.CreateICmpEQ(DecStatus, IRB.getInt32(1));
Expand All @@ -269,20 +270,20 @@ Function *StringEncryption::buildDecryptFunction(Module *M, const StringEncrypti
LoopCounter->addIncoming(IRB.getInt32(0), Enter);

Value *EncCharPtr = IRB.CreateInBoundsGEP(
EncPtr->getType()->getScalarType()->getPointerElementType(), EncPtr,
EncPtr->getType()->getPointerElementType(), EncPtr,
LoopCounter);
Value *EncChar = IRB.CreateLoad(
EncCharPtr->getType()->getPointerElementType(), EncCharPtr);
Value *KeyIdx = IRB.CreateURem(LoopCounter, KeySize);

Value *KeyCharPtr = IRB.CreateInBoundsGEP(
Data->getType()->getScalarType()->getPointerElementType(), Data, KeyIdx);
Data->getType()->getPointerElementType(), Data, KeyIdx);
Value *KeyChar = IRB.CreateLoad(
KeyCharPtr->getType()->getPointerElementType(), KeyCharPtr);

Value *DecChar = IRB.CreateXor(EncChar, KeyChar);
Value *DecCharPtr = IRB.CreateInBoundsGEP(
PlainString->getType()->getScalarType()->getPointerElementType(),
PlainString->getType()->getPointerElementType(),
PlainString, LoopCounter);
IRB.CreateStore(DecChar, DecCharPtr);

Expand Down Expand Up @@ -356,7 +357,7 @@ void StringEncryption::lowerGlobalConstantArray(ConstantArray *CA, IRBuilder<> &
for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
Constant *CV = CA->getOperand(i);
Value *GEP =
IRB.CreateGEP(Ptr->getType()->getScalarType()->getPointerElementType(),
IRB.CreateGEP(Ptr->getType()->getPointerElementType(),
Ptr, {IRB.getInt32(0), IRB.getInt32(i)});
lowerGlobalConstant(CV, IRB, GEP);
}
Expand All @@ -365,7 +366,7 @@ void StringEncryption::lowerGlobalConstantArray(ConstantArray *CA, IRBuilder<> &
void StringEncryption::lowerGlobalConstantStruct(ConstantStruct *CS, IRBuilder<> &IRB, Value *Ptr) {
for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
Value *GEP =
IRB.CreateGEP(Ptr->getType()->getScalarType()->getPointerElementType(),
IRB.CreateGEP(Ptr->getType()->getPointerElementType(),
Ptr, {IRB.getInt32(0), IRB.getInt32(i)});
lowerGlobalConstant(CS->getOperand(i), IRB, GEP);
}
Expand All @@ -383,7 +384,13 @@ bool StringEncryption::processConstantStringUse(Function *F) {
bool Changed = false;
for (BasicBlock &BB : *F) {
DecryptedGV.clear();
if (BB.isEHPad()) {
continue;
}
for (Instruction &Inst: BB) {
if (Inst.isEHPad()) {
continue;
}
if (PHINode *PHI = dyn_cast<PHINode>(&Inst)) {
for (unsigned int i = 0; i < PHI->getNumIncomingValues(); ++i) {
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(PHI->getIncomingValue(i))) {
Expand Down Expand Up @@ -413,7 +420,6 @@ bool StringEncryption::processConstantStringUse(Function *F) {
Value *OutBuf = IRB.CreateBitCast(Entry->DecGV, IRB.getInt8PtrTy());
Value *Data = IRB.CreateInBoundsGEP(
EncryptedStringTable->getType()
->getScalarType()
->getPointerElementType(),
EncryptedStringTable,
{IRB.getInt32(0), IRB.getInt32(Entry->Offset)});
Expand Down Expand Up @@ -454,7 +460,6 @@ bool StringEncryption::processConstantStringUse(Function *F) {
Value *OutBuf = IRB.CreateBitCast(Entry->DecGV, IRB.getInt8PtrTy());
Value *Data = IRB.CreateInBoundsGEP(
EncryptedStringTable->getType()
->getScalarType()
->getPointerElementType(),
EncryptedStringTable,
{IRB.getInt32(0), IRB.getInt32(Entry->Offset)});
Expand Down

0 comments on commit 9910301

Please sign in to comment.