Exemplo n.º 1
0
        public static void TestUseCase(Assert assert)
        {
            assert.Expect(7);

            var t1 = new Type();

            assert.Ok(t1 != null, "#565 t1");

            var t2 = new ValueType();

            assert.Ok(t2 != null, "#565 t2");

            var t3 = new IntPtr();

            assert.Ok(t3.GetType() == typeof(IntPtr), "#565 t3");

            var t4 = new UIntPtr();

            assert.Ok(t4.GetType() == typeof(UIntPtr), "#565 t4");

            var t5 = new ParamArrayAttribute();

            assert.Ok(t5 != null, "#565 t5");

            var t6 = new RuntimeTypeHandle();

            assert.Ok(t6.GetType() == typeof(RuntimeTypeHandle), "#565 t6");

            var t7 = new RuntimeFieldHandle();

            assert.Ok(t7.GetType() == typeof(RuntimeFieldHandle), "#565 t7");
        }
Exemplo n.º 2
0
        void RestoreMethodsModuleVariant(string pathFile)
        {
            int    delegateNumber    = 0;
            string declaringTypeName = string.Empty;
            string fieldName         = string.Empty;

            foreach (var method in moduleDef.Types[0].Methods)
            {
                if (method.HasBody)
                {
                    if (method.Body.HasInstructions)
                    {
                        if (method.Body.Instructions[0].OpCode == OpCodes.Ldsfld &&
                            method.Body.Instructions[1].OpCode.ToString().Contains("ldc"))
                        {
                            delegateNumber = method.Body.Instructions[1].GetLdcI4Value();
                            dynamic ldsfldOperand = method.Body.Instructions[0]?.Operand;
                            if (ldsfldOperand is FieldDef)
                            {
                                declaringTypeName = ldsfldOperand.DeclaringType.Name;
                                fieldName         = ldsfldOperand.Name;
                            }
                            var delegatesArray = (object[])assembly.ManifestModule.GetType(declaringTypeName)
                                                 .GetField(fieldName).GetValue(null);
                            var currentDelegate = (Delegate)delegatesArray[delegateNumber];

                            var m_owner = currentDelegate.Method
                                          .GetType()
                                          .GetField("m_owner", BindingFlags.NonPublic | BindingFlags.Instance)
                                          ?.GetValue(currentDelegate.Method);
                            if (m_owner != null)
                            {
                                var m_resolver = m_owner
                                                 .GetType()
                                                 .GetField("m_resolver", BindingFlags.NonPublic | BindingFlags.Instance)
                                                 ?.GetValue(m_owner);
                                if (m_resolver != null)
                                {
                                    var m_scope = m_resolver.GetType()
                                                  .GetField("m_scope", BindingFlags.NonPublic | BindingFlags.Instance)
                                                  ?.GetValue(m_resolver);
                                    List <object> m_tokens = (List <object>)m_scope.GetType()
                                                             .GetField("m_tokens", BindingFlags.NonPublic | BindingFlags.Instance)
                                                             .GetValue(m_scope);
                                    if (m_tokens[m_tokens.Count - 1] is RuntimeMethodHandle)
                                    {
                                        RuntimeMethodHandle calledMethod      = (RuntimeMethodHandle)m_tokens[m_tokens.Count - 1];
                                        dynamic             calledMethodMInfo = calledMethod.GetType()
                                                                                .GetField("m_value", BindingFlags.NonPublic | BindingFlags.Instance)
                                                                                ?.GetValue(calledMethod);
                                        if (calledMethodMInfo != null)
                                        {
                                            try
                                            {
                                                string fullName = calledMethodMInfo.GetType()
                                                                  .GetProperty("FullName", BindingFlags.Instance | BindingFlags.NonPublic)
                                                                  .GetValue(calledMethodMInfo).ToString();

                                                if (fullName.Contains(".ctor") && !fullName.Contains("System.Windows.Forms.Form..ctor"))
                                                {
                                                    method.Body.Instructions[method.Body.Instructions.Count - 2] =
                                                        Instruction.Create(OpCodes.Newobj, moduleDef.Import(calledMethodMInfo));
                                                    method.Body.Instructions[0] = Instruction.Create(OpCodes.Nop);
                                                    method.Body.Instructions[1] = Instruction.Create(OpCodes.Nop);
                                                    method.Body.Instructions[2] = Instruction.Create(OpCodes.Nop);
                                                    method.Body.UpdateInstructionOffsets();
                                                }
                                                else
                                                {
                                                    method.Body.Instructions[method.Body.Instructions.Count - 2] =
                                                        Instruction.Create(OpCodes.Call, moduleDef.Import(calledMethodMInfo));
                                                    method.Body.Instructions[0] = Instruction.Create(OpCodes.Nop);
                                                    method.Body.Instructions[1] = Instruction.Create(OpCodes.Nop);
                                                    method.Body.Instructions[2] = Instruction.Create(OpCodes.Nop);
                                                    method.Body.UpdateInstructionOffsets();
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                            }
                                        }

                                        // * - this runtime method
                                        Console.WriteLine(delegateNumber + "*: " + calledMethodMInfo.GetType()
                                                          .GetProperty("FullName", BindingFlags.Instance | BindingFlags.NonPublic)
                                                          .GetValue(calledMethodMInfo));
                                    }
                                    else if (m_tokens[m_tokens.Count - 1] is RuntimeFieldHandle)
                                    {
                                        RuntimeFieldHandle calledField      = (RuntimeFieldHandle)m_tokens[m_tokens.Count - 1];
                                        dynamic            calledFieldFInfo = calledField.GetType()
                                                                              .GetField("m_ptr", BindingFlags.NonPublic | BindingFlags.Instance)
                                                                              ?.GetValue(calledField);
                                        if (calledFieldFInfo != null)
                                        {
                                            method.Body.Instructions[method.Body.Instructions.Count - 2] =
                                                Instruction.Create(OpCodes.Ldsfld,
                                                                   moduleDef.Import(calledFieldFInfo));
                                            method.Body.Instructions[0] = Instruction.Create(OpCodes.Nop);
                                            method.Body.Instructions[1] = Instruction.Create(OpCodes.Nop);
                                            method.Body.Instructions[2] = Instruction.Create(OpCodes.Nop);
                                            method.Body.UpdateInstructionOffsets();
                                        }
                                        // * - this runtime field
                                        Console.WriteLine(delegateNumber + "*: " + calledFieldFInfo.GetType()
                                                          .GetProperty("FullName", BindingFlags.Instance | BindingFlags.NonPublic)
                                                          .GetValue(calledFieldFInfo));
                                    }
                                    else
                                    {
                                        Console.ForegroundColor = ConsoleColor.Cyan;
                                        Console.WriteLine("UNKNOWN");
                                        Console.ForegroundColor = ConsoleColor.Blue;
                                    }
                                }
                            }
                            else
                            {
                                method.Body.Instructions[0] = Instruction.Create(OpCodes.Nop);
                                method.Body.Instructions[1] = Instruction.Create(OpCodes.Nop);
                                method.Body.Instructions[2] = Instruction.Create(OpCodes.Nop);
                                method.Body.Instructions[method.Body.Instructions.Count - 2] =
                                    Instruction.Create(OpCodes.Call, moduleDef.Import(currentDelegate.Method));
                                method.Body.UpdateInstructionOffsets();
                                Console.WriteLine(delegateNumber + ": " + currentDelegate.Method);
                            }
                        }
                    }
                }
            }
        }