feat: 集成 LLVM IR 混淆体系、Route 2 全量合规与系统调用调度加固
- defender: 扩展 IGDispatch/IGArchiveGuard/IGAntiHook/IGObjCBaseline/IGResolve,支持 Route 2 静态库桥接 (IGThreadID.c) 与防重签名检测 - llvm_passes: 新增 IGObfuscator LLVM IR 插件源码(控制流平坦化、字符串加密、间接跳转/调用、不透明常量/字段、MBA、函数外提、基本块拆分、反 Hook 序言) - tools & config: 新增 Xcode 归档编译/链接包装器、LLVM 自愈工具链 (ig_toolchain_resolver)、IPA 审计门禁、崩溃符号还原及最新 ig_llvm_policy.json(文件级匹配 + 3.0 全业务 Handler 平坦化) - tests & docs: 补齐 M0~M3、Pass 单测及系统调用调度/Route 2/LLVM 方案文档;.gitignore 忽略 *.bc 与 .obfuscate_extraction_cache.json
This commit is contained in:
@@ -0,0 +1,232 @@
|
||||
#include "IGPassUtils.h"
|
||||
#include "IGPolicyConfig.h"
|
||||
#include "llvm/ADT/SetVector.h"
|
||||
#include "llvm/ADT/SmallVector.h"
|
||||
#include "llvm/IR/Attributes.h"
|
||||
#include "llvm/IR/DebugInfo.h"
|
||||
#include "llvm/IR/Dominators.h"
|
||||
#include "llvm/IR/Function.h"
|
||||
#include "llvm/IR/Instructions.h"
|
||||
#include "llvm/IR/IntrinsicInst.h"
|
||||
#include "llvm/IR/Module.h"
|
||||
#include "llvm/IR/Verifier.h"
|
||||
#include "llvm/Support/ErrorHandling.h"
|
||||
#include "llvm/Transforms/Utils/CodeExtractor.h"
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
namespace igobf {
|
||||
namespace {
|
||||
|
||||
bool isSwiftABIHazardValue(const Value *V) {
|
||||
if (!V)
|
||||
return false;
|
||||
if (V->getType()->isTokenTy() || V->getType()->isLabelTy() ||
|
||||
V->getType()->isMetadataTy()) {
|
||||
return true;
|
||||
}
|
||||
if (const auto *AI = dyn_cast<AllocaInst>(V)) {
|
||||
if (AI->isSwiftError())
|
||||
return true;
|
||||
}
|
||||
if (const auto *Arg = dyn_cast<Argument>(V)) {
|
||||
if (Arg->hasAttribute(Attribute::SwiftError) ||
|
||||
Arg->hasAttribute(Attribute::SwiftAsync) ||
|
||||
Arg->hasAttribute(Attribute::SwiftSelf)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isBlockEligibleForOutline(BasicBlock &BB, const Function &F,
|
||||
unsigned MinInsts) {
|
||||
if (&BB == &F.getEntryBlock() || BB.isEHPad() || BB.hasAddressTaken() ||
|
||||
pred_empty(&BB)) {
|
||||
return false;
|
||||
}
|
||||
if (isa<PHINode>(BB.front()))
|
||||
return false;
|
||||
if (!isa<BranchInst>(BB.getTerminator()))
|
||||
return false;
|
||||
|
||||
unsigned WorkInsts = 0;
|
||||
for (const Instruction &I : BB) {
|
||||
if (isa<DbgInfoIntrinsic>(I))
|
||||
continue;
|
||||
if (isa<AllocaInst>(I) || isa<InvokeInst>(I) || isa<CallBrInst>(I) ||
|
||||
isa<LandingPadInst>(I) || isa<CatchSwitchInst>(I) ||
|
||||
isa<CatchReturnInst>(I) || isa<CleanupReturnInst>(I) ||
|
||||
isa<IndirectBrInst>(I) || isa<VAArgInst>(I) || I.isAtomic()) {
|
||||
return false;
|
||||
}
|
||||
if (I.getType()->isTokenTy())
|
||||
return false;
|
||||
for (const Use &U : I.operands()) {
|
||||
if (isa<BlockAddress>(U.get()))
|
||||
return false;
|
||||
if (isa<BasicBlock>(U.get()))
|
||||
continue;
|
||||
if (isSwiftABIHazardValue(U.get()))
|
||||
return false;
|
||||
}
|
||||
if (const auto *CB = dyn_cast<CallBase>(&I)) {
|
||||
if (CB->isInlineAsm() || CB->isMustTailCall() ||
|
||||
CB->getCallingConv() == CallingConv::SwiftTail) {
|
||||
return false;
|
||||
}
|
||||
for (unsigned i = 0, e = CB->arg_size(); i != e; ++i) {
|
||||
if (CB->paramHasAttr(i, Attribute::SwiftError) ||
|
||||
CB->paramHasAttr(i, Attribute::SwiftAsync) ||
|
||||
CB->paramHasAttr(i, Attribute::SwiftSelf) ||
|
||||
CB->paramHasAttr(i, Attribute::InAlloca) ||
|
||||
CB->paramHasAttr(i, Attribute::Preallocated) ||
|
||||
CB->paramHasAttr(i, Attribute::ImmArg)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (const Function *Callee = CB->getCalledFunction()) {
|
||||
StringRef CName = Callee->getName();
|
||||
if (CName.starts_with("llvm.coro.") ||
|
||||
CName.starts_with("llvm.objc.clang.arc.use") ||
|
||||
CName.starts_with("llvm.va_")) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isa<DbgInfoIntrinsic>(I) && !I.isTerminator())
|
||||
++WorkInsts;
|
||||
}
|
||||
return WorkInsts >= MinInsts;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool runFunctionOutline(Module &M, const IGPolicyConfig &Policy,
|
||||
IGAuditRecord &Audit) {
|
||||
if (!Policy.isFunctionOutlineEnabled() || Policy.isModuleExempt(M))
|
||||
return false;
|
||||
if (!isMachOAArch64NonArm64e(M))
|
||||
return false;
|
||||
|
||||
const unsigned MaxPerFn = Policy.functionOutlineMaxPerFn();
|
||||
const unsigned MinInsts = Policy.functionOutlineMinInsts();
|
||||
if (MaxPerFn == 0)
|
||||
return false;
|
||||
|
||||
SmallVector<Function *, 32> OrigFuncs;
|
||||
for (Function &F : M)
|
||||
OrigFuncs.push_back(&F);
|
||||
|
||||
std::string Tag = computeShortModuleTag(Policy.seed(), M.getModuleIdentifier(),
|
||||
Policy.sourcePath());
|
||||
unsigned GlobalOutlineIdx = 0;
|
||||
bool Changed = false;
|
||||
|
||||
for (Function *F : OrigFuncs) {
|
||||
if (!F || !Policy.shouldOutlineFunction(*F))
|
||||
continue;
|
||||
if (!isFunctionSafeForTransform(*F, Policy,
|
||||
&Policy.functionOutlineExemptFunctions())) {
|
||||
continue;
|
||||
}
|
||||
if (F->isVarArg() || F->hasPersonalityFn() || F->size() < 2)
|
||||
continue;
|
||||
|
||||
SmallVector<BasicBlock *, 16> OrigBlocks;
|
||||
for (BasicBlock &BB : *F)
|
||||
OrigBlocks.push_back(&BB);
|
||||
|
||||
IGDeterministicRNG RNG(Policy.seed(), M.getModuleIdentifier(),
|
||||
(F->getName() + "|outline").str());
|
||||
RNG.shuffle(OrigBlocks);
|
||||
|
||||
unsigned FnOutlined = 0;
|
||||
for (BasicBlock *BB : OrigBlocks) {
|
||||
if (FnOutlined >= MaxPerFn)
|
||||
break;
|
||||
if (!BB || BB->getParent() != F)
|
||||
continue;
|
||||
if (!isBlockEligibleForOutline(*BB, *F, MinInsts))
|
||||
continue;
|
||||
|
||||
DominatorTree DT(*F);
|
||||
CodeExtractorAnalysisCache CEAC(*F);
|
||||
SmallVector<BasicBlock *, 1> Region{BB};
|
||||
CodeExtractor CE(Region, &DT, /*AggregateArgs=*/false);
|
||||
if (!CE.isEligible())
|
||||
continue;
|
||||
|
||||
SetVector<Value *> Inputs, Outputs, Allocas;
|
||||
CE.findInputsOutputs(Inputs, Outputs, Allocas);
|
||||
if (Inputs.size() > 5 || Outputs.size() > 2)
|
||||
continue;
|
||||
|
||||
bool Hazard = false;
|
||||
for (const Value *V : Inputs) {
|
||||
if (isSwiftABIHazardValue(V)) {
|
||||
Hazard = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!Hazard) {
|
||||
for (const Value *V : Outputs) {
|
||||
if (isSwiftABIHazardValue(V)) {
|
||||
Hazard = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Hazard)
|
||||
continue;
|
||||
|
||||
Function *OutF = CE.extractCodeRegion(CEAC);
|
||||
if (!OutF)
|
||||
continue;
|
||||
|
||||
OutF->setName("ig_sub_" + Tag + "_" + Twine(GlobalOutlineIdx++));
|
||||
OutF->setLinkage(GlobalValue::InternalLinkage);
|
||||
OutF->setVisibility(GlobalValue::DefaultVisibility);
|
||||
OutF->addFnAttr(Attribute::NoInline);
|
||||
OutF->removeFnAttr(Attribute::AlwaysInline);
|
||||
OutF->removeFnAttr(Attribute::InlineHint);
|
||||
|
||||
llvm::stripDebugInfo(*OutF);
|
||||
for (BasicBlock &OBB : *OutF) {
|
||||
for (Instruction &OI : make_early_inc_range(OBB)) {
|
||||
if (isa<DbgInfoIntrinsic>(OI))
|
||||
OI.eraseFromParent();
|
||||
}
|
||||
}
|
||||
|
||||
if (F->getSubprogram()) {
|
||||
for (BasicBlock &FBB : *F) {
|
||||
for (Instruction &FI : FBB) {
|
||||
if (!FI.getDebugLoc())
|
||||
FI.setDebugLoc(resolveInstDebugLoc(&FI, *F));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (verifyFunction(*OutF, &errs())) {
|
||||
report_fatal_error(Twine("[IGFunctionOutline] IR verification failed on ") +
|
||||
OutF->getName());
|
||||
}
|
||||
if (verifyFunction(*F, &errs())) {
|
||||
report_fatal_error(Twine("[IGFunctionOutline] IR verification failed on ") +
|
||||
F->getName());
|
||||
}
|
||||
|
||||
++FnOutlined;
|
||||
++Audit.FunctionsOutlinedCount;
|
||||
Changed = true;
|
||||
}
|
||||
|
||||
if (FnOutlined > 0)
|
||||
Audit.OutlinedFunctions.push_back(F->getName().str());
|
||||
}
|
||||
|
||||
return Changed;
|
||||
}
|
||||
|
||||
} // namespace igobf
|
||||
Reference in New Issue
Block a user