Exemplo n.º 1
0
        public static void CreateMethodRestore(MethodDef target, MethodDef newMethod, ModuleDefMD module)
        {
            AssemblyRef dnlib          = module.GetAssemblyRef(new UTF8String("dnlib"));
            TypeRefUser Instruction    = new TypeRefUser(module, new UTF8String("dnlib"), new UTF8String("Instruction"), dnlib);
            TypeSig     instructionSig = Instruction.ToTypeSig();

            var assemblyRef = module.CorLibTypes.AssemblyRef;

            var listRef            = new TypeRefUser(module, @"System.Collections.Generic", "List`1", assemblyRef);
            var listGenericInstSig = new GenericInstSig(new ClassSig(listRef), instructionSig);

            var listTypeSpec = new TypeSpecUser(listGenericInstSig);

            var listCtor         = new MemberRefUser(module, ".ctor", MethodSig.CreateInstance(module.CorLibTypes.Void), listTypeSpec);
            var instruictionCtor = new MemberRefUser(module, ".ctor", MethodSig.CreateInstance(module.CorLibTypes.Void), instructionSig.TryGetTypeSpec());

            var listAdd = new MemberRefUser(module, "Add", MethodSig.CreateInstance(module.CorLibTypes.Void, new GenericVar(0)), listTypeSpec);

            // sdsd

            newMethod.Body.Instructions.Add(OpCodes.Newobj.ToInstruction(listCtor));
            newMethod.Body.Instructions.Add(OpCodes.Stloc_0.ToInstruction()); // Store list to local[0]

            /*
             * newMethod.Body.Instructions.Add(new Instruction(OpCodes.Dup));
             * newMethod.Body.Instructions.Add(new Instruction(OpCodes.Ldsfld, OpCodes.Add));
             * newMethod.Body.Instructions.Add(new Instruction(OpCodes.Ldc_I4_S, 0x37));
             * newMethod.Body.Instructions.Add(new Instruction(OpCodes.Box, module.CorLibTypes.Int32));
             * newMethod.Body.Instructions.Add(new Instruction(OpCodes.Newobj, instruictionCtor));
             * newMethod.Body.Instructions.Add(new Instruction(OpCodes.Callvirt, listAdd));
             */
        }
Exemplo n.º 2
0
 private static MemberRefUser FindReadMethod(ModuleDef m, GenericInstSig sig, IMethod method)
 {
     return(new MemberRefUser(m, method.Name, method.MethodSig)
     {
         Class = new TypeSpecUser(sig)
     });
 }
Exemplo n.º 3
0
        static int GetSystemValueTupleRank(GenericInstSig gis)
        {
            GenericInstSig?gis2 = gis;
            int            rank = 0;

            for (int i = 0; i < 1000; i++)
            {
                int currentRank = GetValueTupleSimpleRank(gis2);
                if (currentRank < 0)
                {
                    return(-1);
                }
                if (rank < 8)
                {
                    return(rank + currentRank);
                }
                rank += currentRank - 1;
                gis2  = gis2.GenericArguments[currentRank - 1] as GenericInstSig;
                if (gis2 is null)
                {
                    return(-1);
                }
            }
            return(-1);
        }
Exemplo n.º 4
0
        bool?CanAccess(MethodDef md, GenericInstSig git)
        {
            if (md == null)
            {
                return(null);
            }
            var access = GetTypeAccess(md.DeclaringType, git);

            if (access == null)
            {
                return(null);
            }
            var acc = access.Value;

            if ((acc & CheckTypeAccess.Normal) == 0)
            {
                return(false);
            }
            if ((acc & CheckTypeAccess.FullMemberAccess) != 0)
            {
                return(true);
            }

            return(IsVisible(md, git));
        }
Exemplo n.º 5
0
        bool?CanAccess(TypeDef td, GenericInstSig git, MemberRef mr)
        {
            if (mr == null || td == null)
            {
                return(null);
            }

            if (mr.MethodSig != null)
            {
                var md = td.FindMethodCheckBaseType(mr.Name, mr.MethodSig);
                if (md == null)
                {
                    // Assume that it's an array type if it's one of these methods
                    if (mr.Name == "Get" || mr.Name == "Set" || mr.Name == "Address" || mr.Name == ".ctor")
                    {
                        return(true);
                    }
                    return(null);
                }
                return(CanAccess(md, git));
            }

            if (mr.FieldSig != null)
            {
                return(CanAccess(td.FindFieldCheckBaseType(mr.Name, mr.FieldSig), git));
            }

            return(null);
        }
Exemplo n.º 6
0
        public TypeSig GetTypeSig()
        {
            if (IsArrayType)
            {
                if (ArrayInfo.IsSZArray)
                {
                    return(new SZArraySig(GenArgs[0]));
                }

                return(new ArraySig(GenArgs[0], ArrayInfo.Rank, ArrayInfo.Sizes, ArrayInfo.LowerBounds));
            }

            ClassOrValueTypeSig tySig;

            if (IsValueType)
            {
                tySig = new ValueTypeSig(Def);
            }
            else
            {
                tySig = new ClassSig(Def);
            }

            TypeSig thisSig = tySig;

            if (HasGenArgs)
            {
                thisSig = new GenericInstSig(tySig, GenArgs);
            }

            return(thisSig);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Checks whether <see cref="userType"/> has access to <paramref name="td"/>.
        /// <paramref name="td"/> is Family, FamANDAssem, or FamORAssem.
        /// </summary>
        /// <param name="td">Type</param>
        /// <param name="git">Generic instance of <paramref name="td"/> or <c>null</c> if none</param>
        bool CheckFamily(TypeDef td, GenericInstSig git)
        {
            if (td == null)
            {
                return(false);
            }
            InitializeBaseTypes();

            if (baseTypes.ContainsKey(git == null ? (IType)td : git))
            {
                return(true);
            }

            // td is Family, FamANDAssem, or FamORAssem. If we derive from its enclosing type,
            // we have access to it.
            var td2 = td.DeclaringType;

            if (td2 != null && baseTypes.ContainsKey(td2))
            {
                return(true);
            }

            // If one of our enclosing types derive from it, we also have access to it
            var userDeclType = userType.DeclaringType;

            if (userDeclType != null)
            {
                return(new AccessChecker(userDeclType).CheckFamily(td, git));
            }

            return(false);
        }
Exemplo n.º 8
0
        TypeSpec ApplyGenerics(ITypeDefOrRef type, IList <TypeSig> generics)
        {
            ClassOrValueTypeSig typeSig    = type.ToTypeSig().ToClassOrValueTypeSig();
            GenericInstSig      genericSig = new GenericInstSig(typeSig, generics);

            return(new TypeSpecUser(genericSig));
        }
Exemplo n.º 9
0
 // Token: 0x06000156 RID: 342 RVA: 0x0000BA60 File Offset: 0x00009C60
 private static void FindTypeRefsInternal(TypeSig typeSig, IList <ITypeDefOrRef> ret)
 {
     while (typeSig.Next != null)
     {
         if (typeSig is ModifierSig)
         {
             ret.Add(((ModifierSig)typeSig).Modifier);
         }
         typeSig = typeSig.Next;
     }
     if (typeSig is GenericInstSig)
     {
         GenericInstSig genInst = (GenericInstSig)typeSig;
         ret.Add(genInst.GenericType.TypeDefOrRef);
         using (IEnumerator <TypeSig> enumerator = genInst.GenericArguments.GetEnumerator())
         {
             while (enumerator.MoveNext())
             {
                 TypeSig genArg = enumerator.Current;
                 DnlibUtils.FindTypeRefsInternal(genArg, ret);
             }
             return;
         }
     }
     if (typeSig is TypeDefOrRefSig)
     {
         for (ITypeDefOrRef type = ((TypeDefOrRefSig)typeSig).TypeDefOrRef; type != null; type = type.DeclaringType)
         {
             ret.Add(type);
         }
     }
 }
Exemplo n.º 10
0
        internal TypeSig CreateTypeSig(IList <TSpec> tspecs, TypeSig currentSig)
        {
            foreach (var tspec in tspecs)
            {
                switch (tspec.etype)
                {
                case ElementType.SZArray:
                    currentSig = new SZArraySig(currentSig);
                    break;

                case ElementType.Array:
                    var arraySpec = (ArraySpec)tspec;
                    currentSig = new ArraySig(currentSig, arraySpec.rank, arraySpec.sizes, arraySpec.lowerBounds);
                    break;

                case ElementType.GenericInst:
                    var ginstSpec = (GenericInstSpec)tspec;
                    currentSig = new GenericInstSig(currentSig as ClassOrValueTypeSig, ginstSpec.args);
                    break;

                case ElementType.ByRef:
                    currentSig = new ByRefSig(currentSig);
                    break;

                case ElementType.Ptr:
                    currentSig = new PtrSig(currentSig);
                    break;

                default:
                    Verify(false, "Unknown TSpec");
                    break;
                }
            }
            return(currentSig);
        }
Exemplo n.º 11
0
        static bool ContainsAnonymousType(this TypeSig type, int depth)
        {
            if (depth >= 30)
            {
                return(false);
            }
            GenericInstSig git = type as GenericInstSig;

            if (git != null && git.GenericType != null)
            {
                if (IsAnonymousType(git.GenericType.TypeDefOrRef))
                {
                    return(true);
                }
                for (int i = 0; i < git.GenericArguments.Count; i++)
                {
                    if (git.GenericArguments[i].ContainsAnonymousType(depth + 1))
                    {
                        return(true);
                    }
                }
                return(false);
            }
            if (type != null && type.Next != null)
            {
                return(type.Next.ContainsAnonymousType(depth + 1));
            }
            return(false);
        }
Exemplo n.º 12
0
        public static bool IsSystemNullable(this GenericInstSig gis)
        {
            if (gis == null)
            {
                return(false);
            }
            if (gis.GenericArguments.Count != 1)
            {
                return(false);
            }
            var type = gis.GenericType as ValueTypeSig;

            if (type == null)
            {
                return(false);
            }

            var mdip = type.TypeDefOrRef as IMetaDataImportProvider;

            if (mdip == null)
            {
                return(false);
            }
            return(MetaDataUtils.IsSystemNullable(mdip.MetaDataImport, mdip.MDToken.Raw));
        }
Exemplo n.º 13
0
        public static bool ContainsAnonymousType(this TypeSig type)
        {
            GenericInstSig git = type as GenericInstSig;

            if (git != null)
            {
                if (IsAnonymousType(git.GenericType.TypeDefOrRef))
                {
                    return(true);
                }
                for (int i = 0; i < git.GenericArguments.Count; i++)
                {
                    if (git.GenericArguments[i].ContainsAnonymousType())
                    {
                        return(true);
                    }
                }
                return(false);
            }
            if (type.Next != null)
            {
                return(ContainsAnonymousType(type.Next));
            }
            return(false);
        }
        public static bool ContainsAnonymousType(this TypeSig typeSig)
        {
            GenericInstSig inst = typeSig as GenericInstSig;

            if (inst != null)
            {
                if (IsAnonymousType(inst.GenericType.TypeDefOrRef))
                {
                    return(true);
                }
                for (int i = 0; i < inst.GenericArguments.Count; i++)
                {
                    if (inst.GenericArguments[i].ContainsAnonymousType())
                    {
                        return(true);
                    }
                }
                return(false);
            }

            if (typeSig.Next == null)
            {
                return(false);
            }

            while (typeSig.Next != null)
            {
                typeSig = typeSig.Next;
            }
            return(typeSig.ContainsAnonymousType());
        }
Exemplo n.º 15
0
        // 检查是否存在要替换的泛型签名
        private static bool IsReplaceNeeded(TypeSig tySig)
        {
            while (tySig != null)
            {
                switch (tySig.ElementType)
                {
                case ElementType.Var:
                case ElementType.MVar:
                    return(true);

                case ElementType.GenericInst:
                {
                    GenericInstSig genInstSig = (GenericInstSig)tySig;
                    foreach (var arg in genInstSig.GenericArguments)
                    {
                        if (IsReplaceNeeded(arg))
                        {
                            return(true);
                        }
                    }
                }
                break;
                }

                tySig = tySig.Next;
            }
            return(false);
        }
Exemplo n.º 16
0
        public TypeSig GetThisTypeSig()
        {
            ClassOrValueTypeSig tySig;

            if (IsValueType)
            {
                tySig = new ValueTypeSig(Def);
            }
            else
            {
                tySig = new ClassSig(Def);
            }

            TypeSig thisSig = tySig;

            if (HasGenArgs)
            {
                thisSig = new GenericInstSig(tySig, GenArgs);
            }
            if (IsValueType)
            {
                thisSig = new ByRefSig(thisSig);
            }
            return(thisSig);
        }
Exemplo n.º 17
0
        void FindEncryptedResourceReader()
        {
            var type = GetTypeFromCode(encryptedResourceSet_GetDefaultReader);

            if (type == null)
            {
                return;
            }
            if (type.BaseType == null || !HasInterface(type, "System.Resources.IResourceReader"))
            {
                return;
            }
            if (!new FieldTypes(type).All(encryptedResourceReaderType_fields))
            {
                return;
            }
            var dictType = GetDlxResDict(type);

            if (dictType == null)
            {
                return;
            }
            if (FindXxteaMethod(type) == null)
            {
                return;
            }

            encryptedResourceReaderType     = type;
            encryptedResourceReaderTypeDict = dictType;
        }
Exemplo n.º 18
0
        public static TypeSig GetGenericArgument(GenericInstSig typeOwner, MethodSpec methodOwner, TypeSig type)
        {
            var typeArgs      = typeOwner == null ? null : typeOwner.GenericArguments;
            var genMethodArgs = methodOwner == null || methodOwner.GenericInstMethodSig == null ?
                                null : methodOwner.GenericInstMethodSig.GenericArguments;

            return(GenericArgsSubstitutor.Create(type, typeArgs, genMethodArgs));
        }
Exemplo n.º 19
0
 public static FieldSig Create(FieldSig sig, GenericInstSig git)
 {
     if (git == null)
     {
         return(sig);
     }
     return(Create(sig, git.GenericArguments));
 }
 public static IField create(IField field, GenericInstSig git)
 {
     if (git == null)
     {
         return(field);
     }
     return(create(field, git.GenericArguments));
 }
 public static ITypeDefOrRef create(ITypeDefOrRef type, GenericInstSig git)
 {
     if (git == null)
     {
         return(type);
     }
     return(create(type, git.GenericArguments));
 }
 public static IMethodDefOrRef create(IMethodDefOrRef method, GenericInstSig git)
 {
     if (git == null)
     {
         return(method);
     }
     return(create(method, git.GenericArguments));
 }
 public static GenericInstMethodSig create(GenericInstMethodSig sig, GenericInstSig git)
 {
     if (git == null)
     {
         return(sig);
     }
     return(create(sig, git.GenericArguments));
 }
Exemplo n.º 24
0
        public static bool IsSystemNullable(GenericInstSig gis)
        {
            var gt = gis.GenericType as ValueTypeSig;

            return(gt != null &&
                   gt.TypeDefOrRef != null &&
                   gt.TypeDefOrRef.DefinitionAssembly.IsCorLib() &&
                   gt.TypeDefOrRef.FullName == "System.Nullable`1");
        }
Exemplo n.º 25
0
 private static void SetupSignatureReferences(INameService service, ICollection <ModuleDefMD> modules,
                                              ModuleDef module, GenericInstSig typeSig)
 {
     SetupSignatureReferences(service, modules, module, typeSig.GenericType);
     foreach (var genericArgument in typeSig.GenericArguments)
     {
         SetupSignatureReferences(service, modules, module, genericArgument);
     }
 }
Exemplo n.º 26
0
 public void InitializeFrom(MethodInstances other, GenericInstSig git)
 {
     foreach (var list in other.methodInstances.Values)
     {
         foreach (var methodInst in list)
         {
             var newMethod = GenericArgsSubstitutor.Create(methodInst.methodRef, git);
             Add(new MethodInst(methodInst.origMethodDef, newMethod));
         }
     }
 }
Exemplo n.º 27
0
        GenericInstSig ReadGenericInstanceType(out List <int> genericArgs)
        {
            var git     = new GenericInstSig(ReadTypeSig() as ClassOrValueTypeSig);
            int numArgs = ReadVariableLengthInt32();

            genericArgs = new List <int>(numArgs);
            for (int i = 0; i < numArgs; i++)
            {
                genericArgs.Add(ReadVariableLengthInt32());
            }
            return(git);
        }
Exemplo n.º 28
0
 public static IList <TypeSig> ReplaceGenericParameters(GenericInstSig typeOwner, MethodSpec methodOwner, IList <TypeSig> types)
 {
     if (typeOwner == null && methodOwner == null)
     {
         return(types);
     }
     for (int i = 0; i < types.Count; i++)
     {
         types[i] = GetGenericArgument(typeOwner, methodOwner, types[i]);
     }
     return(types);
 }
Exemplo n.º 29
0
 static VTable ResolveGenericArgument(TypeDef openType, GenericInstSig genInst, VTable vTable)
 {
     Debug.Assert(new SigComparer().Equals(openType, vTable.Type));
     return(new VTable(genInst,
                       vTable.Slots.Select(slot => ResolveSlot(openType, (VTableSlot)slot, genInst.GenericArguments))
                       .ToList(),
                       vTable.InterfaceSlots.ToDictionary(
                           iface => GenericArgumentResolver.Resolve(iface.Key, genInst.GenericArguments),
                           iface => (IReadOnlyList <IVTableSlot>)iface.Value
                           .Select(slot => ResolveSlot(openType, (VTableSlot)slot, genInst.GenericArguments)).ToList())
                       ));
 }
        public static MethodSpec create(MethodSpec method, GenericInstSig git)
        {
            if (method == null || git == null)
            {
                return(method);
            }
            var  newMethod = create(method.Method, git);
            var  newInst   = create(method.GenericInstMethodSig, git);
            bool updated   = newMethod != method.Method || newInst != method.GenericInstMethodSig;

            return(updated ? new MethodSpecUser(newMethod, newInst) : method);
        }