Ejemplo n.º 1
0
        internal static void ReadLocalVarSig(ModuleReader module, ByteReader br, IGenericContext context, List <LocalVariableInfo> list)
        {
            if (br.Length < 2 || br.ReadByte() != LOCAL_SIG)
            {
                throw new BadImageFormatException("Invalid local variable signature");
            }
            int count = br.ReadCompressedInt();

            for (int i = 0; i < count; i++)
            {
                if (br.PeekByte() == ELEMENT_TYPE_TYPEDBYREF)
                {
                    br.ReadByte();
                    list.Add(new LocalVariableInfo(i, module.universe.System_TypedReference, false));
                }
                else
                {
                    SkipCustomModifiers(br);
                    bool pinned = false;
                    if (br.PeekByte() == ELEMENT_TYPE_PINNED)
                    {
                        br.ReadByte();
                        pinned = true;
                    }
                    SkipCustomModifiers(br);
                    Type type = ReadTypeOrByRef(module, br, context);
                    list.Add(new LocalVariableInfo(i, type, pinned));
                }
            }
        }
Ejemplo n.º 2
0
        protected static void ReadCustomModifiers(ModuleReader module, ByteReader br, IGenericContext context, out Type[] requiredCustomModifiers, out Type[] optionalCustomModifiers)
        {
            byte b = br.PeekByte();

            if (IsCustomModifier(b))
            {
                List <Type> required = new List <Type>();
                List <Type> optional = new List <Type>();
                while (IsCustomModifier(b))
                {
                    br.ReadByte();
                    Type type = ReadTypeDefOrRefEncoded(module, br, context);
                    if (b == ELEMENT_TYPE_CMOD_REQD)
                    {
                        required.Add(type);
                    }
                    else
                    {
                        optional.Add(type);
                    }
                    b = br.PeekByte();
                }
                requiredCustomModifiers = required.ToArray();
                optionalCustomModifiers = optional.ToArray();
            }
            else
            {
                requiredCustomModifiers = null;
                optionalCustomModifiers = null;
            }
        }
Ejemplo n.º 3
0
        internal static void Skip(ByteReader br)
        {
            byte b = br.PeekByte();

            while (IsCustomModifier(b))
            {
                br.ReadByte();
                br.ReadCompressedInt();
                b = br.PeekByte();
            }
        }
Ejemplo n.º 4
0
        protected static void SkipCustomModifiers(ByteReader br)
        {
            byte b = br.PeekByte();

            while (IsCustomModifier(b))
            {
                br.ReadByte();
                br.ReadCompressedInt();
                b = br.PeekByte();
            }
        }
Ejemplo n.º 5
0
        // this reads just the optional parameter types, from a MethodRefSig
        internal static Type[] ReadOptionalParameterTypes(ModuleReader module, ByteReader br)
        {
            br.ReadByte();
            int paramCount = br.ReadCompressedInt();

            SkipCustomModifiers(br);
            ReadRetType(module, br, null);
            for (int i = 0; i < paramCount; i++)
            {
                if (br.PeekByte() == SENTINEL)
                {
                    br.ReadByte();
                    Type[] types = new Type[paramCount - i];
                    for (int j = 0; j < types.Length; j++)
                    {
                        SkipCustomModifiers(br);
                        types[j] = ReadType(module, br, null);
                    }
                    return(types);
                }
                SkipCustomModifiers(br);
                ReadType(module, br, null);
            }
            return(Type.EmptyTypes);
        }
Ejemplo n.º 6
0
        internal static void ReadDeclarativeSecurity(Assembly asm, List <CustomAttributeData> list, int action, ByteReader br)
        {
            Universe u = asm.universe;

            if (br.PeekByte() == '.')
            {
                br.ReadByte();
                int count = br.ReadCompressedInt();
                for (int j = 0; j < count; j++)
                {
                    Type            type        = ReadType(asm, br);
                    ConstructorInfo constructor = type.GetPseudoCustomAttributeConstructor(u.System_Security_Permissions_SecurityAction);
                    // LAMESPEC there is an additional length here (probably of the named argument list)
                    byte[] blob = br.ReadBytes(br.ReadCompressedInt());
                    list.Add(new CustomAttributeData(asm, constructor, action, blob));
                }
            }
            else
            {
                // .NET 1.x format (xml)
                char[] buf = new char[br.Length / 2];
                for (int i = 0; i < buf.Length; i++)
                {
                    buf[i] = br.ReadChar();
                }
                string          xml         = new String(buf);
                ConstructorInfo constructor = u.System_Security_Permissions_PermissionSetAttribute.GetPseudoCustomAttributeConstructor(u.System_Security_Permissions_SecurityAction);
                List <CustomAttributeNamedArgument> args = new List <CustomAttributeNamedArgument>();
                args.Add(new CustomAttributeNamedArgument(GetProperty(u.System_Security_Permissions_PermissionSetAttribute, "XML", u.System_String),
                                                          new CustomAttributeTypedArgument(u.System_String, xml)));
                list.Add(new CustomAttributeData(asm.ManifestModule, constructor, new object[] { action }, args));
            }
        }
Ejemplo n.º 7
0
        // this reads just the optional parameter types, from a MethodRefSig
        internal static Type[] ReadOptionalParameterTypes(ModuleReader module, ByteReader br, IGenericContext context, out CustomModifiers[] customModifiers)
        {
            br.ReadByte();
            int paramCount = br.ReadCompressedInt();

            CustomModifiers.Skip(br);
            ReadRetType(module, br, context);
            for (int i = 0; i < paramCount; i++)
            {
                if (br.PeekByte() == SENTINEL)
                {
                    br.ReadByte();
                    Type[] types = new Type[paramCount - i];
                    customModifiers = new CustomModifiers[types.Length];
                    for (int j = 0; j < types.Length; j++)
                    {
                        customModifiers[j] = CustomModifiers.Read(module, br, context);
                        types[j]           = ReadType(module, br, context);
                    }
                    return(types);
                }
                CustomModifiers.Skip(br);
                ReadType(module, br, context);
            }
            customModifiers = Empty <CustomModifiers> .Array;
            return(Type.EmptyTypes);
        }
        internal static MethodSignature ReadSig(ModuleReader module, ByteReader br, IGenericContext context)
        {
            CallingConventions callingConvention;
            int  genericParamCount;
            Type returnType;

            Type[] parameterTypes;
            byte   flags = br.ReadByte();

            switch (flags & 7)
            {
            case DEFAULT:
                callingConvention = CallingConventions.Standard;
                break;

            case VARARG:
                callingConvention = CallingConventions.VarArgs;
                break;

            default:
                throw new BadImageFormatException();
            }
            if ((flags & HASTHIS) != 0)
            {
                callingConvention |= CallingConventions.HasThis;
            }
            if ((flags & EXPLICITTHIS) != 0)
            {
                callingConvention |= CallingConventions.ExplicitThis;
            }
            genericParamCount = 0;
            if ((flags & GENERIC) != 0)
            {
                genericParamCount = br.ReadCompressedUInt();
                context           = new UnboundGenericMethodContext(context);
            }
            int paramCount = br.ReadCompressedUInt();

            CustomModifiers[] modifiers = null;
            PackedCustomModifiers.Pack(ref modifiers, 0, CustomModifiers.Read(module, br, context), paramCount + 1);
            returnType     = ReadRetType(module, br, context);
            parameterTypes = new Type[paramCount];
            for (int i = 0; i < parameterTypes.Length; i++)
            {
                if ((callingConvention & CallingConventions.VarArgs) != 0 && br.PeekByte() == SENTINEL)
                {
                    Array.Resize(ref parameterTypes, i);
                    if (modifiers != null)
                    {
                        Array.Resize(ref modifiers, i + 1);
                    }
                    break;
                }
                PackedCustomModifiers.Pack(ref modifiers, i + 1, CustomModifiers.Read(module, br, context), paramCount + 1);
                parameterTypes[i] = ReadParam(module, br, context);
            }
            return(new MethodSignature(returnType, parameterTypes, PackedCustomModifiers.Wrap(modifiers), callingConvention, genericParamCount));
        }
Ejemplo n.º 9
0
        private static CustomModifiers ReadCustomModifiers(ModuleReader module, ByteReader br, IGenericContext context)
        {
            CustomModifiers mods = new CustomModifiers();
            byte            b    = br.PeekByte();

            if (IsCustomModifier(b))
            {
                List <Type> required = new List <Type>();
                List <Type> optional = new List <Type>();
                while (IsCustomModifier(b))
                {
                    bool req  = br.ReadByte() == ELEMENT_TYPE_CMOD_REQD;
                    Type type = ReadTypeDefOrRefEncoded(module, br, context);
                    (req ? required : optional).Add(type);
                    b = br.PeekByte();
                }
                mods.required = required.ToArray();
                mods.optional = optional.ToArray();
            }
            return(mods);
        }
Ejemplo n.º 10
0
        protected static Type ReadParam(ModuleReader module, ByteReader br, IGenericContext context)
        {
            switch (br.PeekByte())
            {
            case ELEMENT_TYPE_TYPEDBYREF:
                br.ReadByte();
                return(module.universe.System_TypedReference);

            default:
                return(ReadTypeOrByRef(module, br, context));
            }
        }
Ejemplo n.º 11
0
 private static Type ReadTypeOrVoid(ModuleReader module, ByteReader br, IGenericContext context)
 {
     if (br.PeekByte() == ELEMENT_TYPE_VOID)
     {
         br.ReadByte();
         return(module.universe.System_Void);
     }
     else
     {
         return(ReadType(module, br, context));
     }
 }
Ejemplo n.º 12
0
        internal static CustomModifiers Read(ModuleReader module, ByteReader br, IGenericContext context)
        {
            byte b = br.PeekByte();

            if (!IsCustomModifier(b))
            {
                return(new CustomModifiers());
            }
            List <Type> list = new List <Type>();
            Type        mode = Initial;

            do
            {
                Type cmod = br.ReadByte() == Signature.ELEMENT_TYPE_CMOD_REQD ? MarkerType.ModReq : MarkerType.ModOpt;
                if (mode != cmod)
                {
                    mode = cmod;
                    list.Add(mode);
                }
                list.Add(Signature.ReadTypeDefOrRefEncoded(module, br, context));
                b = br.PeekByte();
            }while (IsCustomModifier(b));
            return(new CustomModifiers(list.ToArray()));
        }
Ejemplo n.º 13
0
        internal static PropertySignature ReadSig(ModuleReader module, ByteReader br, IGenericContext context)
        {
            byte flags = br.ReadByte();

            if ((flags & PROPERTY) == 0)
            {
                throw new BadImageFormatException();
            }
            CallingConventions callingConvention = CallingConventions.Standard;

            if ((flags & HASTHIS) != 0)
            {
                callingConvention |= CallingConventions.HasThis;
            }
            if ((flags & EXPLICITTHIS) != 0)
            {
                callingConvention |= CallingConventions.ExplicitThis;
            }
            Type returnType;

            Type[]   returnTypeRequiredCustomModifiers;
            Type[]   returnTypeOptionalCustomModifiers;
            Type[]   parameterTypes;
            Type[][] parameterRequiredCustomModifiers;
            Type[][] parameterOptionalCustomModifiers;
            int      paramCount = br.ReadCompressedInt();

            ReadCustomModifiers(module, br, context, out returnTypeRequiredCustomModifiers, out returnTypeOptionalCustomModifiers);
            returnType     = ReadRetType(module, br, context);
            parameterTypes = new Type[paramCount];
            parameterRequiredCustomModifiers = null;
            parameterOptionalCustomModifiers = null;
            for (int i = 0; i < parameterTypes.Length; i++)
            {
                if (IsCustomModifier(br.PeekByte()))
                {
                    if (parameterOptionalCustomModifiers == null)
                    {
                        parameterOptionalCustomModifiers = new Type[parameterTypes.Length][];
                        parameterRequiredCustomModifiers = new Type[parameterTypes.Length][];
                    }
                    ReadCustomModifiers(module, br, context, out parameterRequiredCustomModifiers[i], out parameterOptionalCustomModifiers[i]);
                }
                parameterTypes[i] = ReadParam(module, br, context);
            }
            return(new PropertySignature(callingConvention, returnType, returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers,
                                         parameterTypes, parameterOptionalCustomModifiers, parameterRequiredCustomModifiers));
        }
Ejemplo n.º 14
0
 private static Type ReadTypeOrByRef(ModuleReader module, ByteReader br, IGenericContext context)
 {
     if (br.PeekByte() == ELEMENT_TYPE_BYREF)
     {
         br.ReadByte();
         // LAMESPEC it is allowed (by C++/CLI, ilasm and peverify) to have custom modifiers after the BYREF
         // (which makes sense, as it is analogous to pointers)
         CustomModifiers mods = ReadCustomModifiers(module, br, context);
         // C++/CLI generates void& local variables, so we need to use ReadTypeOrVoid here
         return(ReadTypeOrVoid(module, br, context).__MakeByRefType(mods.required, mods.optional));
     }
     else
     {
         return(ReadType(module, br, context));
     }
 }
Ejemplo n.º 15
0
        internal static void ReadDeclarativeSecurity(Assembly asm, List <CustomAttributeData> list, int action, ByteReader br)
        {
            Universe u = asm.universe;

            if (br.PeekByte() == '.')
            {
                br.ReadByte();
                int count = br.ReadCompressedInt();
                for (int j = 0; j < count; j++)
                {
                    Type            type = ReadType(asm, br);
                    ConstructorInfo constructor;
                    if (type == u.System_Security_Permissions_HostProtectionAttribute && action == (int)System.Security.Permissions.SecurityAction.LinkDemand)
                    {
                        constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
                    }
                    else
                    {
                        constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { u.System_Security_Permissions_SecurityAction }, null);
                    }
                    // LAMESPEC there is an additional length here (probably of the named argument list)
                    ByteReader slice = br.Slice(br.ReadCompressedInt());
                    // LAMESPEC the count of named arguments is a compressed integer (instead of UInt16 as NumNamed in custom attributes)
                    list.Add(new CustomAttributeData(constructor, action, ReadNamedArguments(asm, slice, slice.ReadCompressedInt(), type)));
                }
            }
            else
            {
                // .NET 1.x format (xml)
                char[] buf = new char[br.Length / 2];
                for (int i = 0; i < buf.Length; i++)
                {
                    buf[i] = br.ReadChar();
                }
                string          xml         = new String(buf);
                ConstructorInfo constructor = u.System_Security_Permissions_PermissionSetAttribute.GetConstructor(new Type[] { u.System_Security_Permissions_SecurityAction });
                List <CustomAttributeNamedArgument> args = new List <CustomAttributeNamedArgument>();
                args.Add(new CustomAttributeNamedArgument(u.System_Security_Permissions_PermissionSetAttribute.GetProperty("XML"),
                                                          new CustomAttributeTypedArgument(u.System_String, xml)));
                list.Add(new CustomAttributeData(constructor, action, args));
            }
        }
Ejemplo n.º 16
0
        internal static __StandAloneMethodSig ReadStandAloneMethodSig(ModuleReader module, ByteReader br, IGenericContext context)
        {
            CallingConventions callingConvention = 0;

            System.Runtime.InteropServices.CallingConvention unmanagedCallingConvention = 0;
            bool unmanaged;
            byte flags = br.ReadByte();

            switch (flags & 7)
            {
            case DEFAULT:
                callingConvention = CallingConventions.Standard;
                unmanaged         = false;
                break;

            case 0x01:                          // C
                unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl;
                unmanaged = true;
                break;

            case 0x02:                          // STDCALL
                unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall;
                unmanaged = true;
                break;

            case 0x03:                          // THISCALL
                unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.ThisCall;
                unmanaged = true;
                break;

            case 0x04:                          // FASTCALL
                unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.FastCall;
                unmanaged = true;
                break;

            case VARARG:
                callingConvention = CallingConventions.VarArgs;
                unmanaged         = false;
                break;

            default:
                throw new BadImageFormatException();
            }
            if ((flags & HASTHIS) != 0)
            {
                callingConvention |= CallingConventions.HasThis;
            }
            if ((flags & EXPLICITTHIS) != 0)
            {
                callingConvention |= CallingConventions.ExplicitThis;
            }
            if ((flags & GENERIC) != 0)
            {
                throw new BadImageFormatException();
            }
            int paramCount = br.ReadCompressedInt();

            SkipCustomModifiers(br);
            Type        returnType             = ReadRetType(module, br, context);
            List <Type> parameterTypes         = new List <Type>();
            List <Type> optionalParameterTypes = new List <Type>();
            List <Type> curr = parameterTypes;

            for (int i = 0; i < paramCount; i++)
            {
                if (br.PeekByte() == SENTINEL)
                {
                    br.ReadByte();
                    curr = optionalParameterTypes;
                }
                SkipCustomModifiers(br);
                curr.Add(ReadParam(module, br, context));
            }
            return(new __StandAloneMethodSig(unmanaged, unmanagedCallingConvention, callingConvention, returnType, parameterTypes.ToArray(), optionalParameterTypes.ToArray()));
        }