void ProcessMethod(CilBody body, CFContext ctx) { uint maxStack; if (!MaxStackCalculator.GetMaxStack(body.Instructions, body.ExceptionHandlers, out maxStack)) { ctx.Context.Logger.Error("Failed to calcuate maxstack."); throw new ConfuserException(null); } body.MaxStack = (ushort)maxStack; ScopeBlock root = BlockParser.ParseBody(body); GetMangler(ctx.Type).Mangle(body, root, ctx); body.Instructions.Clear(); root.ToBody(body); foreach (ExceptionHandler eh in body.ExceptionHandlers) { var index = body.Instructions.IndexOf(eh.TryEnd) + 1; eh.TryEnd = index < body.Instructions.Count ? body.Instructions[index] : null; index = body.Instructions.IndexOf(eh.HandlerEnd) + 1; eh.HandlerEnd = index < body.Instructions.Count ? body.Instructions[index] : null; } body.KeepOldMaxStack = true; }
internal void Run() { var method = Mangler.Context.Plugins <ICFMangleMethod>().RandomElementOrDefault(Mangler.Rng); if (method == null) { return; } MethodBody.SimplifyBranches(); foreach (var block in RootBlock.EnumRegular()) { method.Mangle(this, block); } RootBlock.Write(MethodBody); MethodBody.KeepOldMaxStack = false; // dnlib will do this when writing the method, we can catch stack errors here if (Mangler.Context.DebugMode) { MaxStackCalculator.GetMaxStack(MethodBody.Instructions, MethodBody.ExceptionHandlers); } MethodBody.OptimizeBranches(); }