/// <summary>
        /// Print out information about whether or not a devirtualization attempt was
        /// successful, and if not why not.
        /// </summary>
        /// <param name="options">MonoOptions set from passed command-line arguments</param>
        /// <param name="module">EazModule</param>
        /// <param name="attempt">Data about the devirtualization attempt</param>
        static void PrintAttemptSuccess(MonoOptions options, EazModule module,
                                        DevirtualizeAttempt attempt)
        {
            var reader = attempt.Reader;

            if (attempt.Successful)
            {
                Console.WriteLine("--> Devirtualizable");
            }
            else if (attempt.WasInstructionUnknown)
            {
                var matches = module.VirtualInstructions
                              .Where((instr) => { return(instr.VirtualCode == reader.LastVirtualOpCode); })
                              .ToArray();

                if (matches.Length > 0)
                {
                    VirtualOpCode v = matches[0];
                    Console.WriteLine("--> Not yet devirtualizable (contains unknown virtual instruction)");
                    Console.WriteLine("-----> Virtual OpCode = {0} @ [{1}] (0x{2:X8})",
                                      reader.LastVirtualOpCode, reader.CurrentInstructionOffset, reader.CurrentVirtualOffset);
                    Console.WriteLine("-----> Delegate method: {0} (MDToken = 0x{1:X8})",
                                      v.DelegateMethod.FullName, v.DelegateMethod.MDToken.Raw);
                }
                else
                {
                    Console.WriteLine("--> Not yet devirtualizable (contains unexpected virtual instruction @ [{0}] (0x{1:X8}))",
                                      reader.CurrentInstructionOffset, reader.CurrentVirtualOffset);
                }
            }
            else
            {
                Console.WriteLine("--> Not yet devirtualizable (threw exception)");
            }
        }
Example #2
0
        /// <summary>
        /// Find all virtual instructions and attempt to identify them.
        /// </summary>
        private void InitializeIdentifiedOpCodes()
        {
            this.IdentifiedOpCodes = new Dictionary <Int32, VirtualOpCode>();

            this.VirtualInstructions = VirtualOpCode.FindAllInstructions(this, this.VType.Type);
            var identified = this.VirtualInstructions.Where((instruction) => { return(instruction.IsIdentified); });

            Boolean warningOccurred = false;

            foreach (var instruction in identified)
            {
                Boolean containsVirtual = this.IdentifiedOpCodes.ContainsKey(instruction.VirtualCode);

                VirtualOpCode existing = this.IdentifiedOpCodes.Where((kvp, index) => {
                    return(kvp.Value.IdentityEquals(instruction));
                }).FirstOrDefault().Value;
                Boolean containsActual = (existing != null);

                if (containsVirtual)
                {
                    this.Logger.Warning(this, "WARNING: Multiple instruction types with the same virtual opcode detected ({0})",
                                        instruction.VirtualCode);
                }

                if (containsActual && !instruction.ExpectsMultiple)
                {
                    String opcodeName;

                    if (instruction.HasCILOpCode)
                    {
                        opcodeName = instruction.OpCode.ToString();
                    }
                    else
                    {
                        opcodeName = instruction.SpecialOpCode.ToString();
                    }

                    this.Logger.Warning(this, "WARNING: Multiple virtual opcodes map to the same actual opcode ({0}, {1} => {2})",
                                        existing.VirtualCode, instruction.VirtualCode, opcodeName);
                }

                if (!warningOccurred)
                {
                    warningOccurred = (containsVirtual || containsActual);
                }

                this.IdentifiedOpCodes.Add(instruction.VirtualCode, instruction);
            }

            if (warningOccurred)
            {
                Console.WriteLine();
            }
        }
Example #3
0
        /// <summary>
        /// Check whether or not this virtual opcode identifies the same as another
        /// virtual opcode.
        /// </summary>
        /// <param name="vopcode">Other virtual opcode to compare against</param>
        /// <returns>true if identifies the same, false if not</returns>
        public Boolean IdentityEquals(VirtualOpCode vopcode)
        {
            if (vopcode == null)
            {
                throw new ArgumentNullException();
            }

            return(this.IsIdentified && vopcode.IsIdentified &&
                   (this.HasCILOpCode == vopcode.HasCILOpCode && (
                        (this.HasCILOpCode && this.OpCode == vopcode.OpCode) ||
                        (!this.HasCILOpCode && this.SpecialOpCode == vopcode.SpecialOpCode)
                        )));
        }
		public OriginalOpcodeUnknownException(VirtualOpCode instruction)
		{
			this.VirtualInstruction = instruction;
		}
Example #5
0
        /// <summary>
        /// Check whether or not this virtual opcode identifies the same as another
        /// virtual opcode.
        /// </summary>
        /// <param name="vopcode">Other virtual opcode to compare against</param>
        /// <returns>true if identifies the same, false if not</returns>
        public Boolean IdentityEquals(VirtualOpCode vopcode)
        {
            if (vopcode == null)
                throw new ArgumentNullException();

            return this.IsIdentified && vopcode.IsIdentified &&
                (this.HasCILOpCode == vopcode.HasCILOpCode && (
                    (this.HasCILOpCode && this.OpCode == vopcode.OpCode) ||
                    (!this.HasCILOpCode && this.SpecialOpCode == vopcode.SpecialOpCode)
            ));
        }
Example #6
0
        /// <summary>
        /// Find all virtual instructions given the main virtualization type.
        /// </summary>
        /// <param name="module">Module</param>
        /// <param name="virtualizationType">Main virtualization type (class)</param>
        /// <returns>All found virtualization instructions</returns>
        public static IList<VirtualOpCode> FindAllInstructions(EazModule module, TypeDef virtualizationType)
        {
            if (module == null || virtualizationType == null)
                throw new ArgumentNullException();

            // Find dictionary method
            MethodDef dictMethod = null;
            var methods = virtualizationType.Methods;
            foreach(var method in methods)
            {
                if(method.IsPrivate && !method.IsStatic
                && method.Parameters.Count == 1
                && method.HasReturnType
                && method.ReturnType.FullName.StartsWith("System.Collections.Generic.Dictionary"))
                {
                    dictMethod = method;
                    break;
                }
            }

            if (dictMethod == null)
                throw new Exception("Unable to find dictionary method");

            // Each dictionary addition looks like this:
            //IL_000b: ldloc.0  // [0]
            //IL_000c: ldarg.0  // [0]
            //IL_000d: ldfld class Class33 Class805::class33_0 // 0x0400092c // TypeDef of this class (Class33) is important
            //IL_0012: ldfld class Class487 Class33::class487_162 // 0x040000da // FieldDef of this field (class487_162) is important
            //IL_0017: callvirt instance int32 Class487::method_1() // 0x06000ac3
            //IL_001c: ldarg.0  // [0]
            //IL_001d: ldfld class Class33 Class805::class33_0 // 0x0400092c
            //IL_0022: ldfld class Class487 Class33::class487_162 // 0x040000da
            //IL_0027: ldarg.0  // [0]
            //IL_0028: ldftn instance void Class805::method_281(class Class1) // 0x060015ca
            //IL_002e: newobj instance void Class805/Delegate9::.ctor(object, native int) // 0x060015dc
            //IL_0033: newobj instance void Class805/Class808::.ctor(class Class487, class Class805/Delegate9) // 0x060015e5
            //IL_0038: callvirt instance void class [mscorlib]System.Collections.Generic.Dictionary`2<int32, class Class805/Class808>::Add(!0, !1) // 0x0a000b63

            if (!dictMethod.HasBody || !dictMethod.Body.HasInstructions)
                throw new Exception("Dictionary method has no instructions");

            IList<Instruction[]> subsequences = Helpers.FindOpCodePatterns(dictMethod.Body.Instructions, DictionaryAddPattern);

            // Remove this check later..?
            if (subsequences.Count != 203)
                throw new Exception("Number of found subsequences (DictionaryAddPattern) != 203 (expected value)");

            List<VirtualOpCode> vInstructions = new List<VirtualOpCode>();

            TypeDef containerType = null;

            // Each series of instructions represents a virtualized instruction
            foreach(var instrs in subsequences)
            {
                VirtualOpCode vInstruction = new VirtualOpCode();

                containerType = ((FieldDef)instrs[2].Operand).FieldType.TryGetTypeDef(); // ldfld
                FieldDef instructionField = ((FieldDef)instrs[3].Operand); // ldfld
                MethodDef delegateMethod = ((MethodDef)instrs[9].Operand); // ldftn

                vInstruction.Parent = module;
                vInstruction.DictionaryMethod = dictMethod;
                vInstruction.ContainerType = containerType;
                vInstruction.InstructionField = instructionField;
                vInstruction.DelegateMethod = delegateMethod;

                vInstructions.Add(vInstruction);
            }

            if (containerType == null)
                throw new Exception("Container type cannot be null");

            // Get the container .ctor method
            MethodDef containerCtor = null;
            foreach(var m in containerType.FindMethods(".ctor"))
            {
                containerCtor = m;
                break;
            }

            if (containerCtor == null)
                throw new Exception("Container .ctor method cannot be found");

            // Each field construction looks like this:
            //IL_0000: ldarg.0  // [0]
            //IL_0001: ldc.i4 1550052828
            //IL_0006: ldc.i4.5
            //IL_0007: newobj instance void Class487::.ctor(int32, valuetype Enum2) // 0x06000ac1
            //IL_000c: stfld class Class487 Class33::class487_47 // 0x04000067

            if (!containerCtor.HasBody || !containerCtor.Body.HasInstructions)
                throw new Exception("Container .ctor method has no instructions");

            if (containerCtor.Body.Instructions.Count < (vInstructions.Count * 5))
                throw new Exception("Container .ctor not large enough for all virtual instructions");

            // 5 instructions per sequence, with 3 trailing instructions
            int subsequenceCount = (containerCtor.Body.Instructions.Count - 3) / 5;

            // This makes a bit of an assumption..
            for(int i = 0; i < subsequenceCount; i++)
            {
                // Grab the subsequence
                List<Instruction> subsequence = new List<Instruction>();
                for (int j = 0; j < 5; j++)
                    subsequence.Add(containerCtor.Body.Instructions[(i * 5) + j]);

                if (subsequence[0].OpCode.Code != Code.Ldarg_0)
                    throw new Exception("Unexpected opcode in container .ctor subsequence");

                Int32 virtualOpCode = Helpers.GetLdcOperand(subsequence[1]);
                Int32 operandType = Helpers.GetLdcOperand(subsequence[2]);
                FieldDef instructionField = (FieldDef)subsequence[4].Operand;

                // Find virtual instruction with matching instruction field MD token to set
                foreach(var vInstr in vInstructions)
                {
                    if(vInstr.InstructionField.MDToken == instructionField.MDToken)
                    {
                        vInstr.HasVirtualCode = true;
                        vInstr.VirtualCode = virtualOpCode;
                        vInstr.VirtualOperandType = operandType;
                        vInstr.TrySetIdentify(); // Try to identify and set original opcode
                        break;
                    }
                }
            }

            return vInstructions.ToArray();
        }
Example #7
0
        /// <summary>
        /// Find all virtual instructions given the main virtualization type.
        /// </summary>
        /// <param name="module">Module</param>
        /// <param name="virtualizationType">Main virtualization type (class)</param>
        /// <returns>All found virtualization instructions</returns>
        public static IList <VirtualOpCode> FindAllInstructions(EazModule module, TypeDef virtualizationType)
        {
            if (module == null || virtualizationType == null)
            {
                throw new ArgumentNullException();
            }

            // Find dictionary method
            MethodDef dictMethod = null;
            var       methods    = virtualizationType.Methods;

            foreach (var method in methods)
            {
                if (method.IsPrivate && !method.IsStatic &&
                    method.Parameters.Count == 1 &&
                    method.HasReturnType &&
                    method.ReturnType.FullName.StartsWith("System.Collections.Generic.Dictionary"))
                {
                    dictMethod = method;
                    break;
                }
            }

            if (dictMethod == null)
            {
                throw new Exception("Unable to find dictionary method");
            }

            // Each dictionary addition looks like this:
            //IL_000b: ldloc.0  // [0]
            //IL_000c: ldarg.0  // [0]
            //IL_000d: ldfld class Class33 Class805::class33_0 // 0x0400092c // TypeDef of this class (Class33) is important
            //IL_0012: ldfld class Class487 Class33::class487_162 // 0x040000da // FieldDef of this field (class487_162) is important
            //IL_0017: callvirt instance int32 Class487::method_1() // 0x06000ac3
            //IL_001c: ldarg.0  // [0]
            //IL_001d: ldfld class Class33 Class805::class33_0 // 0x0400092c
            //IL_0022: ldfld class Class487 Class33::class487_162 // 0x040000da
            //IL_0027: ldarg.0  // [0]
            //IL_0028: ldftn instance void Class805::method_281(class Class1) // 0x060015ca
            //IL_002e: newobj instance void Class805/Delegate9::.ctor(object, native int) // 0x060015dc
            //IL_0033: newobj instance void Class805/Class808::.ctor(class Class487, class Class805/Delegate9) // 0x060015e5
            //IL_0038: callvirt instance void class [mscorlib]System.Collections.Generic.Dictionary`2<int32, class Class805/Class808>::Add(!0, !1) // 0x0a000b63

            if (!dictMethod.HasBody || !dictMethod.Body.HasInstructions)
            {
                throw new Exception("Dictionary method has no instructions");
            }

            IList <Instruction[]> subsequences = Helpers.FindOpCodePatterns(dictMethod.Body.Instructions, DictionaryAddPattern);

            // Remove this check later..?
            if (subsequences.Count != 203)
            {
                throw new Exception(String.Format(
                                        "Number of found subsequences (DictionaryAddPattern) {0} != 203 (expected value)", subsequences.Count));
            }

            List <VirtualOpCode> vInstructions = new List <VirtualOpCode>();

            TypeDef containerType = null;

            // Each series of instructions represents a virtualized instruction
            foreach (var instrs in subsequences)
            {
                VirtualOpCode vInstruction = new VirtualOpCode();

                containerType = ((FieldDef)instrs[2].Operand).FieldType.TryGetTypeDef();   // ldfld
                FieldDef  instructionField = ((FieldDef)instrs[3].Operand);                // ldfld
                MethodDef delegateMethod   = ((MethodDef)instrs[9].Operand);               // ldftn

                vInstruction.Parent           = module;
                vInstruction.DictionaryMethod = dictMethod;
                vInstruction.ContainerType    = containerType;
                vInstruction.InstructionField = instructionField;
                vInstruction.DelegateMethod   = delegateMethod;

                vInstructions.Add(vInstruction);
            }

            if (containerType == null)
            {
                throw new Exception("Container type cannot be null");
            }

            // Get the container .ctor method
            MethodDef containerCtor = null;

            foreach (var m in containerType.FindMethods(".ctor"))
            {
                containerCtor = m;
                break;
            }

            if (containerCtor == null)
            {
                throw new Exception("Container .ctor method cannot be found");
            }

            // Each field construction looks like this:
            //IL_0000: ldarg.0  // [0]
            //IL_0001: ldc.i4 1550052828
            //IL_0006: ldc.i4.5
            //IL_0007: newobj instance void Class487::.ctor(int32, valuetype Enum2) // 0x06000ac1
            //IL_000c: stfld class Class487 Class33::class487_47 // 0x04000067

            if (!containerCtor.HasBody || !containerCtor.Body.HasInstructions)
            {
                throw new Exception("Container .ctor method has no instructions");
            }

            if (containerCtor.Body.Instructions.Count < (vInstructions.Count * 5))
            {
                throw new Exception("Container .ctor not large enough for all virtual instructions");
            }

            // 5 instructions per sequence, with 3 trailing instructions
            int subsequenceCount = (containerCtor.Body.Instructions.Count - 3) / 5;

            // This makes a bit of an assumption..
            for (int i = 0; i < subsequenceCount; i++)
            {
                // Grab the subsequence
                List <Instruction> subsequence = new List <Instruction>();
                for (int j = 0; j < 5; j++)
                {
                    subsequence.Add(containerCtor.Body.Instructions[(i * 5) + j]);
                }

                if (subsequence[0].OpCode.Code != Code.Ldarg_0)
                {
                    throw new Exception("Unexpected opcode in container .ctor subsequence");
                }

                Int32    virtualOpCode    = Helpers.GetLdcOperand(subsequence[1]);
                Int32    operandType      = Helpers.GetLdcOperand(subsequence[2]);
                FieldDef instructionField = (FieldDef)subsequence[4].Operand;

                // Find virtual instruction with matching instruction field MD token to set
                foreach (var vInstr in vInstructions)
                {
                    if (vInstr.InstructionField.MDToken == instructionField.MDToken)
                    {
                        vInstr.HasVirtualCode     = true;
                        vInstr.VirtualCode        = virtualOpCode;
                        vInstr.VirtualOperandType = operandType;
                        vInstr.TrySetIdentify();                         // Try to identify and set original opcode
                        break;
                    }
                }
            }

            return(vInstructions.ToArray());
        }
 public OriginalOpcodeUnknownException(VirtualOpCode instruction)
 {
     this.VirtualInstruction = instruction;
 }