Skip to content

Commit

Permalink
Use Neg instead of Not
Browse files Browse the repository at this point in the history
  • Loading branch information
KomiMoe committed Sep 1, 2024
1 parent c6a3c83 commit 4e655a8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
15 changes: 7 additions & 8 deletions llvm/lib/Transforms/Obfuscation/IndirectBranch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ struct IndirectBranch : public FunctionPass {
Constant *CE = ConstantExpr::getBitCast(BlockAddress::get(BB), PointerType::getUnqual(F.getContext()));
CE = ConstantExpr::getGetElementPtr(Type::getInt8Ty(F.getContext()), CE, ConstantExpr::getXor(AddKey, ConstantExpr::getNSWMul(XorKey, ConstantInt::get(intType, BBNumbering[BB], false))));

XorKey = ConstantExpr::getNot(XorKey);
XorKey = ConstantExpr::getNSWNeg(XorKey);
XorKey = ConstantExpr::getXor(XorKey, AddKey);
XorKey = ConstantExpr::getNot(XorKey);
XorKey = ConstantExpr::getNSWNeg(XorKey);
XorKeys.push_back(XorKey);
Elements.push_back(CE);
}
Expand Down Expand Up @@ -213,7 +213,6 @@ struct IndirectBranch : public FunctionPass {
ConstantInt *EncKey = ConstantInt::get(intType, V, false);
ConstantInt *EncKey1 = ConstantInt::get(intType, -V, false);
ConstantInt *Zero = ConstantInt::get(intType, 0);
ConstantInt *One = ConstantInt::get(intType, 1);

GlobalVariable *GXorKey = nullptr;
GlobalVariable *DestBBs = nullptr;
Expand Down Expand Up @@ -266,10 +265,10 @@ struct IndirectBranch : public FunctionPass {

if (opt.level() == 1) {
DecKey = IRB.CreateXor(EncKey1, XorKey);
DecKey = IRB.CreateAdd(IRB.CreateNot(DecKey), One);
DecKey = IRB.CreateNSWNeg(DecKey);
} else if (opt.level() == 2) {
DecKey = IRB.CreateXor(EncKey1, IRB.CreateNSWMul(XorKey, Idx));
DecKey = IRB.CreateAdd(IRB.CreateNot(DecKey), One);
DecKey = IRB.CreateNSWNeg(DecKey);
}
}

Expand All @@ -279,12 +278,12 @@ struct IndirectBranch : public FunctionPass {
Value *XorKey = IRB.CreateLoad(intType, XorKeysGEP);
dyn_cast<LoadInst>(XorKey)->setVolatile(true);

XorKey = IRB.CreateNot(XorKey);
XorKey = IRB.CreateNSWNeg(XorKey);
XorKey = IRB.CreateXor(XorKey, EncKey1);
XorKey = IRB.CreateNot(XorKey);
XorKey = IRB.CreateNSWNeg(XorKey);

DecKey = IRB.CreateXor(EncKey1, IRB.CreateNSWMul(XorKey, Idx));
DecKey = IRB.CreateAdd(IRB.CreateNot(DecKey), One);
DecKey = IRB.CreateNSWNeg(DecKey);
}

Value *DestAddr = IRB.CreateGEP(
Expand Down
15 changes: 7 additions & 8 deletions llvm/lib/Transforms/Obfuscation/IndirectCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ struct IndirectCall : public FunctionPass {
Callee, PointerType::getUnqual(F.getContext()));
CE = ConstantExpr::getGetElementPtr(Type::getInt8Ty(F.getContext()), CE, ConstantExpr::getXor(AddKey, ConstantExpr::getNSWMul(XorKey, ConstantInt::get(intType, CalleeNumbering[Callee], false))));

XorKey = ConstantExpr::getNot(XorKey);
XorKey = ConstantExpr::getNSWNeg(XorKey);
XorKey = ConstantExpr::getXor(XorKey, AddKey);
XorKey = ConstantExpr::getNot(XorKey);
XorKey = ConstantExpr::getNSWNeg(XorKey);
XorKeys.push_back(XorKey);
Elements.push_back(CE);
}
Expand Down Expand Up @@ -217,7 +217,6 @@ struct IndirectCall : public FunctionPass {
ConstantInt *EncKey = ConstantInt::get(intType, V, false);
ConstantInt *EncKey1 = ConstantInt::get(intType, -V, false);
ConstantInt *Zero = ConstantInt::get(intType, 0);
ConstantInt *One = ConstantInt::get(intType, 1);

GlobalVariable *GXorKey = nullptr;
GlobalVariable *Targets = nullptr;
Expand Down Expand Up @@ -266,10 +265,10 @@ struct IndirectCall : public FunctionPass {

if (opt.level() == 1) {
DecKey = IRB.CreateXor(EncKey1, XorKey);
DecKey = IRB.CreateAdd(IRB.CreateNot(DecKey), One);
DecKey = IRB.CreateNSWNeg(DecKey);
} else if (opt.level() == 2) {
DecKey = IRB.CreateXor(EncKey1, IRB.CreateNSWMul(XorKey, Idx));
DecKey = IRB.CreateAdd(IRB.CreateNot(DecKey), One);
DecKey = IRB.CreateNSWNeg(DecKey);
}
}

Expand All @@ -279,12 +278,12 @@ struct IndirectCall : public FunctionPass {
Value *XorKey = IRB.CreateLoad(intType, XorKeysGEP);
dyn_cast<LoadInst>(XorKey)->setVolatile(true);

XorKey = IRB.CreateNot(XorKey);
XorKey = IRB.CreateNSWNeg(XorKey);
XorKey = IRB.CreateXor(XorKey, EncKey1);
XorKey = IRB.CreateNot(XorKey);
XorKey = IRB.CreateNSWNeg(XorKey);

DecKey = IRB.CreateXor(EncKey1, IRB.CreateNSWMul(XorKey, Idx));
DecKey = IRB.CreateAdd(IRB.CreateNot(DecKey), One);
DecKey = IRB.CreateNSWNeg(DecKey);
}

Value *DestAddr = IRB.CreateGEP(Type::getInt8Ty(Ctx),
Expand Down

0 comments on commit 4e655a8

Please sign in to comment.