MethodControlFlowCalculator对InputArgs为0的BasicBlock随机化打乱顺序

main
walon 2025-08-01 11:32:33 +08:00
parent c596b58d3e
commit 4db68f707b
1 changed files with 81 additions and 54 deletions

View File

@ -362,69 +362,96 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus
InsertSwitchMachineBasicBlockForGroup(rootGroup, inst2bb); InsertSwitchMachineBasicBlockForGroup(rootGroup, inst2bb);
} }
//private void ShuffleBasicBlocks0(List<BasicBlockInfo> bbs) private void ShuffleBasicBlocks0(List<BasicBlockInfo> bbs)
//{ {
// int n = bbs.Count; if (bbs.Count <= 2)
// if (n <= 1) {
// { return;
// return; }
// }
// var firstSection = new List<BasicBlockInfo>() { bbs[0] }; var subBlocksExcludeFirstLast = bbs.GetRange(1, bbs.Count - 2);
// var sectionsExcludeFirstLast = new List<List<BasicBlockInfo>>();
// List<BasicBlockInfo> currentSection = firstSection; var blocksInputArgsCountZero = new List<BasicBlockInfo>();
// for (int i = 1; i < n; i++) var blocksInputArgsCountNonZero = new List<BasicBlockInfo>();
// { foreach (var bb in subBlocksExcludeFirstLast)
// BasicBlockInfo cur = bbs[i]; {
// if (cur.inputArgs.locals.Count == 0) if (bb.inputArgs.locals.Count == 0)
// { {
// currentSection = new List<BasicBlockInfo>() { cur }; blocksInputArgsCountZero.Add(bb);
// sectionsExcludeFirstLast.Add(currentSection); }
// } else
// else {
// { blocksInputArgsCountNonZero.Add(bb);
// currentSection.Add(cur); }
// } }
// } RandomUtil.ShuffleList(blocksInputArgsCountZero, _random);
// if (sectionsExcludeFirstLast.Count <= 1)
// { int index = 1;
// return; foreach (var bb in blocksInputArgsCountZero)
// } {
// var lastSection = sectionsExcludeFirstLast.Last(); bbs[index++] = bb;
// sectionsExcludeFirstLast.RemoveAt(sectionsExcludeFirstLast.Count - 1); }
foreach (var bb in blocksInputArgsCountNonZero)
{
bbs[index++] = bb;
}
Assert.AreEqual(bbs.Count - 1, index, "Shuffled basic blocks count should be the same as original count minus first and last blocks");
//var firstSection = new List<BasicBlockInfo>() { bbs[0] };
//var sectionsExcludeFirstLast = new List<List<BasicBlockInfo>>();
//List<BasicBlockInfo> currentSection = firstSection;
//for (int i = 1; i < n; i++)
//{
// BasicBlockInfo cur = bbs[i];
// if (cur.inputArgs.locals.Count == 0)
// {
// currentSection = new List<BasicBlockInfo>() { cur };
// sectionsExcludeFirstLast.Add(currentSection);
// }
// else
// {
// currentSection.Add(cur);
// }
//}
//if (sectionsExcludeFirstLast.Count <= 1)
//{
// return;
//}
//var lastSection = sectionsExcludeFirstLast.Last();
//sectionsExcludeFirstLast.RemoveAt(sectionsExcludeFirstLast.Count - 1);
// RandomUtil.ShuffleList(sectionsExcludeFirstLast, _random); //RandomUtil.ShuffleList(sectionsExcludeFirstLast, _random);
// bbs.Clear(); //bbs.Clear();
// bbs.AddRange(firstSection); //bbs.AddRange(firstSection);
// bbs.AddRange(sectionsExcludeFirstLast.SelectMany(section => section)); //bbs.AddRange(sectionsExcludeFirstLast.SelectMany(section => section));
// bbs.AddRange(lastSection); //bbs.AddRange(lastSection);
// Assert.AreEqual(n, bbs.Count, "Shuffled basic blocks count should be the same as original count"); //Assert.AreEqual(n, bbs.Count, "Shuffled basic blocks count should be the same as original count");
//} }
private void ShuffleBasicBlocks(List<BasicBlockInfo> bbs) private void ShuffleBasicBlocks(List<BasicBlockInfo> bbs)
{ {
// TODO // TODO
//int n = bbs.Count; int n = bbs.Count;
//BasicBlockInfo groupPrev = bbs[0].prev; BasicBlockInfo groupPrev = bbs[0].prev;
//BasicBlockInfo groupNext = bbs[n - 1].next; BasicBlockInfo groupNext = bbs[n - 1].next;
////RandomUtil.ShuffleList(bbs, _random); //RandomUtil.ShuffleList(bbs, _random);
//ShuffleBasicBlocks0(bbs); ShuffleBasicBlocks0(bbs);
//BasicBlockInfo prev = groupPrev; BasicBlockInfo prev = groupPrev;
//for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
//{ {
// BasicBlockInfo cur = bbs[i]; BasicBlockInfo cur = bbs[i];
// cur.prev = prev; cur.prev = prev;
// prev.next = cur; prev.next = cur;
// prev = cur; prev = cur;
//} }
//prev.next = groupNext; prev.next = groupNext;
//if (groupNext != null) if (groupNext != null)
//{ {
// groupNext.prev = prev; groupNext.prev = prev;
//} }
} }
private Local _globalSwitchIndexLocal; private Local _globalSwitchIndexLocal;