public static void RestoreMethod(MethodDef methodTo, MethodDef methodFrom)
        {
            var body = new CilBody();

            body.KeepOldMaxStack = methodTo.Body.KeepOldMaxStack;
            body.InitLocals      = methodTo.Body.InitLocals;
            body.HeaderSize      = methodTo.Body.HeaderSize;
            body.MaxStack        = methodTo.Body.MaxStack;
            body.LocalVarSigTok  = methodTo.Body.LocalVarSigTok;
            body.Instructions.Clear();
            foreach (var d in methodFrom.Body.Instructions)
            {
                body.Instructions.Add(d);
            }
            body.ExceptionHandlers.Clear();
            foreach (var d in methodFrom.Body.ExceptionHandlers)
            {
                body.ExceptionHandlers.Add(d);
            }
            body.Variables.Clear();
            foreach (var d in methodFrom.Body.Variables)
            {
                body.Variables.Add(d);
            }
            body.Scope = null;
            body.UpdateInstructionOffsets();
            methodTo.MethodBody = body;
        }
        private static CilBody CreateThrowingCilBody()
        {
            var body = new CilBody(false,
                                   ThrowingInstructionList,
                                   Array.Empty <ExceptionHandler>(),
                                   Array.Empty <Local>()
                                   );

            body.UpdateInstructionOffsets();
            return(body);
        }
Exemple #3
0
 public CilBody CopyTo(CilBody body)
 {
     body.KeepOldMaxStack = KeepOldMaxStack;
     body.InitLocals      = InitLocals;
     body.MaxStack        = MaxStack;
     body.LocalVarSigTok  = LocalVarSigTok;
     body.Instructions.Clear();
     body.Instructions.AddRange(Instructions);
     body.ExceptionHandlers.Clear();
     body.ExceptionHandlers.AddRange(ExceptionHandlers);
     body.Variables.Clear();
     body.Variables.AddRange(this.Locals);
     body.Scope = this.Scope;
     body.UpdateInstructionOffsets();
     return(body);
 }
Exemple #4
0
		public CilBody CopyTo(CilBody body) {
			body.KeepOldMaxStack = KeepOldMaxStack;
			body.InitLocals = InitLocals;
			body.HeaderSize = HeaderSize;
			body.MaxStack = MaxStack;
			body.LocalVarSigTok = LocalVarSigTok;
			body.Instructions.Clear();
			body.Instructions.AddRange(Instructions);
			body.ExceptionHandlers.Clear();
			body.ExceptionHandlers.AddRange(ExceptionHandlers);
			body.Variables.Clear();
			body.Variables.AddRange(Locals);
			body.PdbMethod = PdbMethod;
			body.UpdateInstructionOffsets();
			return body;
		}
Exemple #5
0
            public Trace(CilBody body, bool hasReturnValue)
            {
                RefCount    = new Dictionary <uint, int>();
                BrRefs      = new Dictionary <uint, List <Instruction> >();
                BeforeStack = new Dictionary <uint, int>();
                AfterStack  = new Dictionary <uint, int>();

                body.UpdateInstructionOffsets();

                foreach (ExceptionHandler eh in body.ExceptionHandlers)
                {
                    BeforeStack[eh.TryStart.Offset]     = 0;
                    BeforeStack[eh.HandlerStart.Offset] = (eh.HandlerType != ExceptionHandlerType.Finally ? 1 : 0);
                    if (eh.FilterStart != null)
                    {
                        BeforeStack[eh.FilterStart.Offset] = 1;
                    }
                }

                int currentStack = 0;

                for (int i = 0; i < body.Instructions.Count; i++)
                {
                    var instr = body.Instructions[i];

                    if (BeforeStack.ContainsKey(instr.Offset))
                    {
                        currentStack = BeforeStack[instr.Offset];
                    }

                    BeforeStack[instr.Offset] = currentStack;
                    instr.UpdateStack(ref currentStack, hasReturnValue);
                    AfterStack[instr.Offset] = currentStack;

                    uint offset;
                    switch (instr.OpCode.FlowControl)
                    {
                    case FlowControl.Branch:
                        offset = ((Instruction)instr.Operand).Offset;
                        if (!BeforeStack.ContainsKey(offset))
                        {
                            BeforeStack[offset] = currentStack;
                        }

                        Increment(RefCount, offset);
                        BrRefs.AddListEntry(offset, instr);

                        currentStack = 0;
                        continue;

                    case FlowControl.Call:
                        if (instr.OpCode.Code == Code.Jmp)
                        {
                            currentStack = 0;
                        }
                        break;

                    case FlowControl.Cond_Branch:
                        if (instr.OpCode.Code == Code.Switch)
                        {
                            foreach (Instruction target in (Instruction[])instr.Operand)
                            {
                                if (!BeforeStack.ContainsKey(target.Offset))
                                {
                                    BeforeStack[target.Offset] = currentStack;
                                }

                                Increment(RefCount, target.Offset);
                                BrRefs.AddListEntry(target.Offset, instr);
                            }
                        }
                        else
                        {
                            offset = ((Instruction)instr.Operand).Offset;
                            if (!BeforeStack.ContainsKey(offset))
                            {
                                BeforeStack[offset] = currentStack;
                            }

                            Increment(RefCount, offset);
                            BrRefs.AddListEntry(offset, instr);
                        }
                        break;

                    case FlowControl.Meta:
                    case FlowControl.Next:
                    case FlowControl.Break:
                        break;

                    case FlowControl.Return:
                    case FlowControl.Throw:
                        continue;

                    default:
                        throw new UnreachableException();
                    }

                    if (i + 1 < body.Instructions.Count)
                    {
                        offset = body.Instructions[i + 1].Offset;
                        Increment(RefCount, offset);
                    }
                }
            }
Exemple #6
0
 public void Dispose()
 {
     body.UpdateInstructionOffsets();
 }
        private static void BuildStore(ModuleDef mod, ref TypeDef store)
        {
            //reused variables
            CilBody     body;
            Instruction instr;
            var         importer = new Importer(mod);

            var trType            = importer.Import(typeof(System.Type));
            var trResourceManager = importer.Import(typeof(System.Resources.ResourceManager));

            TypeSig tsType = trType.ToTypeSig();
            TypeSig tsRuntimeTypeHandle = new ValueTypeSig(importer.Import(typeof(System.RuntimeTypeHandle)));
            TypeSig tsAssembly          = importer.ImportAsTypeSig(typeof(System.Reflection.Assembly));
            TypeSig tsResourceManager   = trResourceManager.ToTypeSig();
            TypeSig tsCultureInfo       = importer.ImportAsTypeSig(typeof(System.Globalization.CultureInfo));

            //create fields

            /*
             *      .field private static class [mscorlib]System.Resources.ResourceManager resourceMan
             *      .field private static class [mscorlib]System.Globalization.CultureInfo resourceCulture
             */
            var fieldMan = new FieldDefUser("resourceMan", new FieldSig(tsResourceManager))
            {
                Attributes = FieldAttributes.Private | FieldAttributes.Static
            };

            store.Fields.Add(fieldMan);

            var fieldCulture = new FieldDefUser("resourceCulture", new FieldSig(tsCultureInfo))
            {
                Attributes = FieldAttributes.Private | FieldAttributes.Static
            };

            store.Fields.Add(fieldCulture);


            #region .ctor

            /*
             *  .method assembly hidebysig specialname rtspecialname
             *      instance void .ctor () cil managed
             *
             *  IL_0000: ldarg.0
             *      IL_0001: call      instance void [mscorlib]System.Object::.ctor()
             *      IL_0006: ret
             */

            MethodDef ctor = new MethodDefUser(".ctor", MethodSig.CreateInstance(mod.CorLibTypes.Void))
            {
                Attributes = MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
            };

            ctor.Body = body = new CilBody();
            body.Instructions.Add(OpCodes.Ldarg_0.ToInstruction());
            body.Instructions.Add(OpCodes.Call.ToInstruction(new MemberRefUser(mod, ".ctor", MethodSig.CreateInstance(mod.CorLibTypes.Void), mod.CorLibTypes.Object.ToTypeDefOrRef())));
            body.Instructions.Add(OpCodes.Ret.ToInstruction());

            store.Methods.Add(ctor);
            #endregion

            #region get_ResourceManager

            /*
             *  .method public hidebysig specialname static
             *          class [mscorlib]System.Resources.ResourceManager get_ResourceManager () cil managed
             *
             *  IL_0000: ldsfld    class [mscorlib]System.Resources.ResourceManager osu_ui.ResourcesStore::resourceMan
             *          IL_0005: brtrue.s  IL_0025
             *          IL_0007: ldstr     "osu_ui.ResourcesStore"
             *          IL_000C: ldtoken   osu_ui.ResourcesStore
             *          IL_0011: call      class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
             *          IL_0016: callvirt  instance class [mscorlib]System.Reflection.Assembly [mscorlib]System.Type::get_Assembly()
             *          IL_001B: newobj    instance void [mscorlib]System.Resources.ResourceManager::.ctor(string, class [mscorlib]System.Reflection.Assembly)
             *          IL_0020: stsfld    class [mscorlib]System.Resources.ResourceManager osu_ui.ResourcesStore::resourceMan
             *          IL_0025: ldsfld    class [mscorlib]System.Resources.ResourceManager osu_ui.ResourcesStore::resourceMan
             *          IL_002A: ret
             */

            //create method
            MethodDef getMan = new MethodDefUser("get_ResourceManager", MethodSig.CreateStatic(tsResourceManager))
            {
                Attributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.Static,
            };

            getMan.Body = body = new CilBody();
            body.Instructions.Add(OpCodes.Ldsfld.ToInstruction(fieldMan));
            body.Instructions.Add(OpCodes.Brtrue_S.ToInstruction((Instruction)null));    //add operand later
            body.Instructions.Add(OpCodes.Ldstr.ToInstruction(ResourceStoreFull));
            body.Instructions.Add(OpCodes.Ldtoken.ToInstruction((ITokenOperand)store));
            body.Instructions.Add(OpCodes.Call.ToInstruction(new MemberRefUser(mod, "GetTypeFromHandle", MethodSig.CreateStatic(tsType, tsRuntimeTypeHandle), trType)));
            body.Instructions.Add(OpCodes.Callvirt.ToInstruction(new MemberRefUser(mod, "get_Assembly", MethodSig.CreateInstance(tsAssembly), trType)));
            body.Instructions.Add(OpCodes.Newobj.ToInstruction(new MemberRefUser(mod, ".ctor", MethodSig.CreateInstance(mod.CorLibTypes.Void, mod.CorLibTypes.String, tsAssembly), trResourceManager)));
            body.Instructions.Add(OpCodes.Stsfld.ToInstruction(fieldMan));
            body.Instructions.Add(instr = OpCodes.Ldsfld.ToInstruction(fieldMan));
            body.Instructions.Add(OpCodes.Ret.ToInstruction());

            body.UpdateInstructionOffsets();
            body.Instructions[1].Operand = instr;

            store.Methods.Add(getMan);
            #endregion

            #region get_Culture

            /*
             *  .method public hidebysig specialname static
             *      class [mscorlib]System.Globalization.CultureInfo get_Culture () cil managed
             *
             *  IL_0000: ldsfld    class [mscorlib]System.Globalization.CultureInfo osu_ui.ResourcesStore::resourceCulture
             *      IL_0005: ret
             *
             */

            MethodDef getCult = new MethodDefUser("get_Culture", MethodSig.CreateStatic(tsCultureInfo))
            {
                Attributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.Static,
            };

            getCult.Body = body = new CilBody();
            body.Instructions.Add(OpCodes.Ldsfld.ToInstruction(fieldCulture));
            body.Instructions.Add(OpCodes.Ret.ToInstruction());

            store.Methods.Add(getCult);
            #endregion

            #region get_Culture

            /*
             *  .method public hidebysig specialname static
             *      void set_Culture (class [mscorlib]System.Globalization.CultureInfo 'value') cil managed
             *
             *  IL_0000: ldarg.0
             *      IL_0001: stsfld    class [mscorlib]System.Globalization.CultureInfo osu_ui.ResourcesStore::resourceCulture
             *      IL_0006: ret
             */

            MethodDef setCult = new MethodDefUser("set_Culture", MethodSig.CreateStatic(mod.CorLibTypes.Void, tsCultureInfo))
            {
                Attributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.Static,
            };
            setCult.Parameters[0].CreateParamDef();
            setCult.Parameters[0].ParamDef.Name = "value";

            setCult.Body = body = new CilBody();
            body.Instructions.Add(OpCodes.Ldarg_0.ToInstruction());
            body.Instructions.Add(OpCodes.Stsfld.ToInstruction(fieldCulture));
            body.Instructions.Add(OpCodes.Ret.ToInstruction());

            store.Methods.Add(setCult);
            #endregion
        }