Пример #1
0
        void AnalyzeMethod(ConfuserContext context, INameService service, MethodDef method)
        {
            var binding          = new List <Tuple <bool, Instruction> >();
            var dataPropertyName = new List <Instruction>();

            foreach (var instr in method.Body.Instructions)
            {
                var target = instr.Operand as IMethod;
                switch (instr.OpCode.Code)
                {
                case Code.Call:
                case Code.Callvirt:
                    Debug.Assert(target != null);

                    if ((target.DeclaringType.FullName == "System.Windows.Forms.ControlBindingsCollection" ||
                         target.DeclaringType.FullName == "System.Windows.Forms.BindingsCollection") &&
                        target.Name == "Add" && target.MethodSig.Params.Count != 1)
                    {
                        binding.Add(Tuple.Create(true, instr));
                    }
                    else if (target.DeclaringType.FullName == "System.Windows.Forms.DataGridViewColumn" &&
                             target.Name == "set_DataPropertyName" &&
                             target.MethodSig.Params.Count == 1)
                    {
                        dataPropertyName.Add(instr);
                    }
                    break;

                case Code.Newobj:
                    Debug.Assert(target != null);
                    if (target.DeclaringType.FullName == "System.Windows.Forms.Binding" &&
                        target.Name.String == ".ctor")
                    {
                        binding.Add(Tuple.Create(false, instr));
                    }
                    break;
                }
            }

            if (binding.Count == 0 && dataPropertyName.Count == 0)
            {
                return;
            }

            var         traceSrv = context.Registry.GetService <ITraceService>();
            MethodTrace trace    = traceSrv.Trace(method);

            bool erred = false;

            foreach (var instrInfo in binding)
            {
                int[] args = trace.TraceArguments(instrInfo.Item2);
                if (args == null)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract binding property name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }

                var argumentIndex = (instrInfo.Item1 ? 1 : 0);
                var propertyName  = ResolveNameInstruction(method, args, ref argumentIndex);
                if (propertyName.OpCode.Code != Code.Ldstr)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract binding property name in '{0}'.", method.FullName);
                    }
                    erred = true;
                }
                else
                {
                    List <PropertyDef> props;
                    if (!properties.TryGetValue((string)propertyName.Operand, out props))
                    {
                        if (!erred)
                        {
                            context.Logger.WarnFormat("Failed to extract target property in '{0}'.", method.FullName);
                        }
                        erred = true;
                    }
                    else
                    {
                        foreach (var property in props)
                        {
                            service.SetCanRename(property, false);
                        }
                    }
                }

                argumentIndex += 2;
                var dataMember = ResolveNameInstruction(method, args, ref argumentIndex);
                if (dataMember.OpCode.Code != Code.Ldstr)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract binding property name in '{0}'.", method.FullName);
                    }
                    erred = true;
                }
                else
                {
                    List <PropertyDef> props;
                    if (!properties.TryGetValue((string)dataMember.Operand, out props))
                    {
                        if (!erred)
                        {
                            context.Logger.WarnFormat("Failed to extract target property in '{0}'.", method.FullName);
                        }
                        erred = true;
                    }
                    else
                    {
                        foreach (var property in props)
                        {
                            service.SetCanRename(property, false);
                        }
                    }
                }
            }

            foreach (var instrInfo in dataPropertyName)
            {
                int[] args = trace.TraceArguments(instrInfo);
                if (args == null)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract binding property name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }

                var argumentIndex = 1;
                var propertyName  = ResolveNameInstruction(method, args, ref argumentIndex);
                if (propertyName.OpCode.Code != Code.Ldstr)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract binding property name in '{0}'.", method.FullName);
                    }
                    erred = true;
                }
                else
                {
                    if (!properties.TryGetValue((string)propertyName.Operand, out var props))
                    {
                        if (!erred)
                        {
                            context.Logger.WarnFormat("Failed to extract target property in '{0}'.", method.FullName);
                        }
                        erred = true;
                    }
                    else
                    {
                        foreach (var property in props)
                        {
                            service.SetCanRename(property, false);
                        }
                    }
                }
            }
        }
Пример #2
0
        void AnalyzeMethod(ConfuserContext context, INameService service, MethodDef method)
        {
            var dpRegInstrs        = new List <Tuple <bool, Instruction> >();
            var routedEvtRegInstrs = new List <Instruction>();

            for (int i = 0; i < method.Body.Instructions.Count; i++)
            {
                Instruction instr = method.Body.Instructions[i];
                if ((instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt))
                {
                    var regMethod = (IMethod)instr.Operand;

                    if (regMethod.DeclaringType.FullName == "System.Windows.DependencyProperty" &&
                        regMethod.Name.String.StartsWith("Register"))
                    {
                        dpRegInstrs.Add(Tuple.Create(regMethod.Name.String.StartsWith("RegisterAttached"), instr));
                    }
                    else if (regMethod.DeclaringType.FullName == "System.Windows.EventManager" &&
                             regMethod.Name.String == "RegisterRoutedEvent")
                    {
                        routedEvtRegInstrs.Add(instr);
                    }
                }
                else if (instr.OpCode.Code == Code.Newobj)
                {
                    var methodRef = (IMethod)instr.Operand;

                    if (methodRef.DeclaringType.FullName == "System.Windows.Data.PropertyGroupDescription" &&
                        methodRef.Name == ".ctor" && i - 1 >= 0 && method.Body.Instructions[i - 1].OpCode.Code == Code.Ldstr)
                    {
                        foreach (PropertyDef property in analyzer.LookupProperty((string)method.Body.Instructions[i - 1].Operand))
                        {
                            service.SetCanRename(property, false);
                        }
                    }
                }
                else if (instr.OpCode == OpCodes.Ldstr)
                {
                    string operand = ((string)instr.Operand).ToUpperInvariant();
                    if (operand.EndsWith(".BAML") || operand.EndsWith(".XAML"))
                    {
                        Match match = UriPattern.Match(operand);
                        if (match.Success)
                        {
                            operand = match.Groups[1].Value;
                        }
                        else if (operand.Contains("/"))
                        {
                            context.Logger.LogFormat("WARN: Fail to extract XAML name from '{0}'.", instr.Operand);
                        }

                        var reference = new BAMLStringReference(instr);
                        operand = operand.TrimStart('/');
                        string baml = operand.Substring(0, operand.Length - 5) + ".BAML";
                        string xaml = operand.Substring(0, operand.Length - 5) + ".XAML";
                        bamlRefs.AddListEntry(baml, reference);
                        bamlRefs.AddListEntry(xaml, reference);
                    }
                }
            }

            if (dpRegInstrs.Count == 0)
            {
                return;
            }

            ITraceService traceSrv = context.Registry.GetService <ITraceService>();
            MethodTrace   trace    = traceSrv.Trace(method);

            bool erred = false;

            foreach (Tuple <bool, Instruction> instrInfo in dpRegInstrs)
            {
                int[] args = trace.TraceArguments(instrInfo.Item2);
                if (args == null)
                {
                    if (!erred)
                    {
                        context.Logger.LogFormat("WARN: Failed to extract dependency property name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }
                Instruction ldstr = method.Body.Instructions[args[0]];
                if (ldstr.OpCode.Code != Code.Ldstr)
                {
                    if (!erred)
                    {
                        context.Logger.LogFormat("WARN: Failed to extract dependency property name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }

                string  name     = (string)ldstr.Operand;
                TypeDef declType = method.DeclaringType;
                bool    found    = false;
                if (instrInfo.Item1)                 // Attached DP
                {
                    MethodDef accessor;
                    if ((accessor = declType.FindMethod("Get" + name)) != null && accessor.IsStatic)
                    {
                        service.SetCanRename(accessor, false);
                        found = true;
                    }
                    if ((accessor = declType.FindMethod("Set" + name)) != null && accessor.IsStatic)
                    {
                        service.SetCanRename(accessor, false);
                        found = true;
                    }
                }

                // Normal DP
                // Find CLR property for attached DP as well, because it seems attached DP can be use as normal DP as well.
                PropertyDef property = null;
                if ((property = declType.FindProperty(name)) != null)
                {
                    service.SetCanRename(property, false);

                    found = true;
                    if (property.GetMethod != null)
                    {
                        service.SetCanRename(property.GetMethod, false);
                    }

                    if (property.SetMethod != null)
                    {
                        service.SetCanRename(property.SetMethod, false);
                    }

                    if (property.HasOtherMethods)
                    {
                        foreach (MethodDef accessor in property.OtherMethods)
                        {
                            service.SetCanRename(accessor, false);
                        }
                    }
                }
                if (!found)
                {
                    if (instrInfo.Item1)
                    {
                        context.Logger.LogFormat("WARN: Failed to find the accessors of attached dependency property '{0}' in type '{1}'.",
                                                 name, declType.FullName);
                    }
                    else
                    {
                        context.Logger.LogFormat("WARN: Failed to find the CLR property of normal dependency property '{0}' in type '{1}'.",
                                                 name, declType.FullName);
                    }
                }
            }

            erred = false;
            foreach (Instruction instr in routedEvtRegInstrs)
            {
                int[] args = trace.TraceArguments(instr);
                if (args == null)
                {
                    if (!erred)
                    {
                        context.Logger.LogFormat("WARN: Failed to extract routed event name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }
                Instruction ldstr = method.Body.Instructions[args[0]];
                if (ldstr.OpCode.Code != Code.Ldstr)
                {
                    if (!erred)
                    {
                        context.Logger.LogFormat("WARN: Failed to extract routed event name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }

                string  name     = (string)ldstr.Operand;
                TypeDef declType = method.DeclaringType;

                EventDef eventDef = null;
                if ((eventDef = declType.FindEvent(name)) == null)
                {
                    context.Logger.LogFormat("WARN: Failed to find the CLR event of routed event '{0}' in type '{1}'.",
                                             name, declType.FullName);
                    continue;
                }
                service.SetCanRename(eventDef, false);

                if (eventDef.AddMethod != null)
                {
                    service.SetCanRename(eventDef.AddMethod, false);
                }

                if (eventDef.RemoveMethod != null)
                {
                    service.SetCanRename(eventDef.RemoveMethod, false);
                }

                if (eventDef.InvokeMethod != null)
                {
                    service.SetCanRename(eventDef.InvokeMethod, false);
                }

                if (eventDef.HasOtherMethods)
                {
                    foreach (MethodDef accessor in eventDef.OtherMethods)
                    {
                        service.SetCanRename(accessor, false);
                    }
                }
            }
        }
Пример #3
0
        void AnalyzeMethod(DotProtectContext context, INameService service, MethodDef method)
        {
            var binding = new List <Tuple <bool, Instruction> >();

            foreach (Instruction instr in method.Body.Instructions)
            {
                if ((instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt))
                {
                    var target = (IMethod)instr.Operand;

                    if ((target.DeclaringType.FullName == "System.Windows.Forms.ControlBindingsCollection" ||
                         target.DeclaringType.FullName == "System.Windows.Forms.BindingsCollection") &&
                        target.Name == "Add" && target.MethodSig.Params.Count != 1)
                    {
                        binding.Add(Tuple.Create(true, instr));
                    }
                    else if (target.DeclaringType.FullName == "System.Windows.Forms.Binding" &&
                             target.Name.String == ".ctor")
                    {
                        binding.Add(Tuple.Create(false, instr));
                    }
                }
            }

            if (binding.Count == 0)
            {
                return;
            }

            var         traceSrv = context.Registry.GetService <ITraceService>();
            MethodTrace trace    = traceSrv.Trace(method);

            bool erred = false;

            foreach (var instrInfo in binding)
            {
                int[] args = trace.TraceArguments(instrInfo.Item2);
                if (args == null)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract binding property name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }

                Instruction propertyName = method.Body.Instructions[args[0 + (instrInfo.Item1 ? 1 : 0)]];
                if (propertyName.OpCode.Code != Code.Ldstr)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract binding property name in '{0}'.", method.FullName);
                    }
                    erred = true;
                }
                else
                {
                    List <PropertyDef> props;
                    if (!properties.TryGetValue((string)propertyName.Operand, out props))
                    {
                        if (!erred)
                        {
                            context.Logger.WarnFormat("Failed to extract target property in '{0}'.", method.FullName);
                        }
                        erred = true;
                    }
                    else
                    {
                        foreach (var property in props)
                        {
                            service.SetCanRename(property, false);
                        }
                    }
                }

                Instruction dataMember = method.Body.Instructions[args[2 + (instrInfo.Item1 ? 1 : 0)]];
                if (dataMember.OpCode.Code != Code.Ldstr)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract binding property name in '{0}'.", method.FullName);
                    }
                    erred = true;
                }
                else
                {
                    List <PropertyDef> props;
                    if (!properties.TryGetValue((string)dataMember.Operand, out props))
                    {
                        if (!erred)
                        {
                            context.Logger.WarnFormat("Failed to extract target property in '{0}'.", method.FullName);
                        }
                        erred = true;
                    }
                    else
                    {
                        foreach (var property in props)
                        {
                            service.SetCanRename(property, false);
                        }
                    }
                }
            }
        }
        // Token: 0x06000044 RID: 68 RVA: 0x00004ED4 File Offset: 0x000030D4
        private void AnalyzeMethod(ConfuserContext context, INameService service, MethodDef method)
        {
            List <Tuple <bool, Instruction> > dpRegInstrs = new List <Tuple <bool, Instruction> >();
            List <Instruction> routedEvtRegInstrs         = new List <Instruction>();

            foreach (Instruction instr in method.Body.Instructions)
            {
                if (instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt)
                {
                    IMethod regMethod = (IMethod)instr.Operand;
                    if (regMethod.DeclaringType.FullName == "System.Windows.DependencyProperty" && regMethod.Name.String.StartsWith("Register"))
                    {
                        dpRegInstrs.Add(Tuple.Create <bool, Instruction>(regMethod.Name.String.StartsWith("RegisterAttached"), instr));
                    }
                    else if (regMethod.DeclaringType.FullName == "System.Windows.EventManager" && regMethod.Name.String == "RegisterRoutedEvent")
                    {
                        routedEvtRegInstrs.Add(instr);
                    }
                }
                else if (instr.OpCode == OpCodes.Ldstr)
                {
                    string operand = ((string)instr.Operand).ToUpperInvariant();
                    if (operand.EndsWith(".BAML") || operand.EndsWith(".XAML"))
                    {
                        Match match = WPFAnalyzer.UriPattern.Match(operand);
                        if (match.Success)
                        {
                            operand = match.Groups[1].Value;
                        }
                        BAMLStringReference reference = new BAMLStringReference(instr);
                        operand = operand.TrimStart(new char[]
                        {
                            '/'
                        });
                        string baml = operand.Substring(0, operand.Length - 5) + ".BAML";
                        string xaml = operand.Substring(0, operand.Length - 5) + ".XAML";
                        this.bamlRefs.AddListEntry(baml, reference);
                        this.bamlRefs.AddListEntry(xaml, reference);
                    }
                }
            }
            if (dpRegInstrs.Count == 0)
            {
                return;
            }
            ITraceService traceSrv = context.Registry.GetService <ITraceService>();
            MethodTrace   trace    = traceSrv.Trace(method);
            bool          erred    = false;

            foreach (Tuple <bool, Instruction> instrInfo in dpRegInstrs)
            {
                int[] args = trace.TraceArguments(instrInfo.Item2);
                if (args == null)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract dependency property name in '{0}'.", new object[]
                        {
                            method.FullName
                        });
                    }
                    erred = true;
                }
                else
                {
                    Instruction ldstr = method.Body.Instructions[args[0]];
                    if (ldstr.OpCode.Code != Code.Ldstr)
                    {
                        if (!erred)
                        {
                            context.Logger.WarnFormat("Failed to extract dependency property name in '{0}'.", new object[]
                            {
                                method.FullName
                            });
                        }
                        erred = true;
                    }
                    else
                    {
                        string  name     = (string)ldstr.Operand;
                        TypeDef declType = method.DeclaringType;
                        bool    found    = false;
                        if (instrInfo.Item1)
                        {
                            MethodDef accessor;
                            if ((accessor = declType.FindMethod("Get" + name)) != null && accessor.IsStatic)
                            {
                                service.SetCanRename(accessor, false);
                                found = true;
                            }
                            if ((accessor = declType.FindMethod("Set" + name)) != null && accessor.IsStatic)
                            {
                                service.SetCanRename(accessor, false);
                                found = true;
                            }
                        }
                        PropertyDef property;
                        if ((property = declType.FindProperty(name)) != null)
                        {
                            service.SetCanRename(property, false);
                            found = true;
                            if (property.GetMethod != null)
                            {
                                service.SetCanRename(property.GetMethod, false);
                            }
                            if (property.SetMethod != null)
                            {
                                service.SetCanRename(property.SetMethod, false);
                            }
                            if (property.HasOtherMethods)
                            {
                                foreach (MethodDef accessor2 in property.OtherMethods)
                                {
                                    service.SetCanRename(accessor2, false);
                                }
                            }
                        }
                        if (!found)
                        {
                            if (instrInfo.Item1)
                            {
                                context.Logger.WarnFormat("Failed to find the accessors of attached dependency property '{0}' in type '{1}'.", new object[]
                                {
                                    name,
                                    declType.FullName
                                });
                            }
                            else
                            {
                                context.Logger.WarnFormat("Failed to find the CLR property of normal dependency property '{0}' in type '{1}'.", new object[]
                                {
                                    name,
                                    declType.FullName
                                });
                            }
                        }
                    }
                }
            }
            erred = false;
            foreach (Instruction instr2 in routedEvtRegInstrs)
            {
                int[] args2 = trace.TraceArguments(instr2);
                if (args2 == null)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract routed event name in '{0}'.", new object[]
                        {
                            method.FullName
                        });
                    }
                    erred = true;
                }
                else
                {
                    Instruction ldstr2 = method.Body.Instructions[args2[0]];
                    if (ldstr2.OpCode.Code != Code.Ldstr)
                    {
                        if (!erred)
                        {
                            context.Logger.WarnFormat("Failed to extract routed event name in '{0}'.", new object[]
                            {
                                method.FullName
                            });
                        }
                        erred = true;
                    }
                    else
                    {
                        string   name2     = (string)ldstr2.Operand;
                        TypeDef  declType2 = method.DeclaringType;
                        EventDef eventDef;
                        if ((eventDef = declType2.FindEvent(name2)) == null)
                        {
                            context.Logger.WarnFormat("Failed to find the CLR event of routed event '{0}' in type '{1}'.", new object[]
                            {
                                name2,
                                declType2.FullName
                            });
                        }
                        else
                        {
                            service.SetCanRename(eventDef, false);
                            if (eventDef.AddMethod != null)
                            {
                                service.SetCanRename(eventDef.AddMethod, false);
                            }
                            if (eventDef.RemoveMethod != null)
                            {
                                service.SetCanRename(eventDef.RemoveMethod, false);
                            }
                            if (eventDef.InvokeMethod != null)
                            {
                                service.SetCanRename(eventDef.InvokeMethod, false);
                            }
                            if (eventDef.HasOtherMethods)
                            {
                                foreach (MethodDef accessor3 in eventDef.OtherMethods)
                                {
                                    service.SetCanRename(accessor3, false);
                                }
                            }
                        }
                    }
                }
            }
        }