From c596b58d3ef3ab4daa7120a7b90b7326486d9e7d Mon Sep 17 00:00:00 2001 From: walon Date: Fri, 1 Aug 2025 10:57:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20MethodControlFlowCalculato?= =?UTF-8?q?r=E7=94=9F=E6=88=90=E7=9A=84=E6=8C=87=E4=BB=A4=E6=9C=89?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E8=BF=9D=E5=8F=8DIL=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E6=B5=81=E8=A7=84=E5=88=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MethodControlFlowCalculator.cs | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/Editor/ObfusPasses/ControlFlowObfus/MethodControlFlowCalculator.cs b/Editor/ObfusPasses/ControlFlowObfus/MethodControlFlowCalculator.cs index 06488c0..9a87dee 100644 --- a/Editor/ObfusPasses/ControlFlowObfus/MethodControlFlowCalculator.cs +++ b/Editor/ObfusPasses/ControlFlowObfus/MethodControlFlowCalculator.cs @@ -330,7 +330,8 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus group.basicBlocks.Insert(group.basicBlocks.IndexOf(to), saveLocalBasicBlock); group.switchMachineCases.Add(new SwitchMachineCase { index = -1, prepareBlock = saveLocalBasicBlock, targetBlock = to}); saveLocalBasicBlock.instructions.Add(Instruction.Create(OpCodes.Ldsfld, (FieldDef)null)); - saveLocalBasicBlock.instructions.Add(Instruction.Create(OpCodes.Br, group.switchMachineInst)); + saveLocalBasicBlock.instructions.Add(Instruction.Create(OpCodes.Stloc, GlobalSwitchIndexLocal)); + saveLocalBasicBlock.instructions.Add(Instruction.Create(OpCodes.Br, group.switchMachineEntryInst)); return saveLocalBasicBlock; @@ -426,6 +427,21 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus //} } + private Local _globalSwitchIndexLocal; + + Local GlobalSwitchIndexLocal + { + get + { + if (_globalSwitchIndexLocal == null) + { + _globalSwitchIndexLocal = new Local(_method.Module.CorLibTypes.Int32); + _method.Body.Variables.Add(_globalSwitchIndexLocal); + } + return _globalSwitchIndexLocal; + } + } + private void InsertSwitchMachineBasicBlockForGroup(BlockGroup group, Dictionary inst2bb) { if (group.subGroups != null && group.subGroups.Count > 0) @@ -449,6 +465,8 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus var instructions = new List() { Instruction.Create(OpCodes.Ldsfld, (FieldDef)null), + Instruction.Create(OpCodes.Stloc, GlobalSwitchIndexLocal), + group.switchMachineEntryInst, group.switchMachineInst, Instruction.Create(OpCodes.Br, firstCase.targetBlock.FirstInstruction), }; @@ -456,7 +474,7 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus { instructions.Insert(0, Instruction.Create(OpCodes.Br, firstBlock.FirstInstruction)); } - + var switchMachineBb = new BasicBlockInfo() { group = group, @@ -480,7 +498,7 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus switchMachineCase.index = i; List prepareBlockInstructions = switchMachineCase.prepareBlock.instructions; - Instruction setBranchIndexInst = prepareBlockInstructions[prepareBlockInstructions.Count - 2]; + Instruction setBranchIndexInst = prepareBlockInstructions[prepareBlockInstructions.Count - 3]; Assert.AreEqual(setBranchIndexInst.OpCode, OpCodes.Ldsfld, "first instruction of prepareBlock should be Ldsfld"); //setBranchIndexInst.Operand = i; var indexField = _constFieldAllocator.Allocate(i); @@ -489,9 +507,10 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus } // after shuffle - Assert.IsTrue(instructions.Count == 3 || instructions.Count == 4, "Switch machine basic block should contain 3 or 4 instructions"); - Assert.AreEqual(Code.Ldsfld, instructions[instructions.Count - 3].OpCode.Code, "First instruction should be Ldsfld"); - instructions[instructions.Count - 3].Operand = _constFieldAllocator.Allocate(firstCase.index); + //Assert.IsTrue(instructions.Count == 4 || instructions.Count == 5, "Switch machine basic block should contain 4 or 5 instructions"); + Instruction loadFirstIndex = instructions[instructions.Count - 5]; + Assert.AreEqual(Code.Ldsfld, loadFirstIndex.OpCode.Code, "First instruction should be Ldsfld"); + loadFirstIndex.Operand = _constFieldAllocator.Allocate(firstCase.index); } } @@ -553,6 +572,7 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus public List basicBlocks; + public Instruction switchMachineEntryInst; public Instruction switchMachineInst; public List switchMachineCases; @@ -663,7 +683,7 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus this.subGroups = finalGroupList; } - public void ComputeBasicBlocks(Dictionary inst2bb) + public void ComputeBasicBlocks(Dictionary inst2bb, Func switchIndexLocalGetter) { if (subGroups == null || subGroups.Count == 0) { @@ -684,13 +704,14 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus basicBlocks.Add(block); } } + switchMachineEntryInst = Instruction.Create(OpCodes.Ldloc, switchIndexLocalGetter()); switchMachineInst = Instruction.Create(OpCodes.Switch, new List()); switchMachineCases = new List(); return; } foreach (var subGroup in subGroups) { - subGroup.ComputeBasicBlocks(inst2bb); + subGroup.ComputeBasicBlocks(inst2bb, switchIndexLocalGetter); } } } @@ -799,7 +820,7 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus var rootGroup = new BlockGroup(new List(instructions), inst2blockGroup); rootGroup.SplitInstructionsNotInAnySubGroupsToIndividualGroups(inst2blockGroup); - rootGroup.ComputeBasicBlocks(BuildInstructionToBasicBlockInfoDic()); + rootGroup.ComputeBasicBlocks(BuildInstructionToBasicBlockInfoDic(), () => GlobalSwitchIndexLocal); return rootGroup; }