public unsafe void VerifyMultipleOptionalModifiers()
        {
            // Type 1: int32 modopt([mscorlib]System.Runtime.CompilerServices.IsLong) modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl)
            // Type 2: char*
            // Type 3: uint32
            // Type 4: char modopt([mscorlib]System.Runtime.CompilerServices.IsConst)*
            var testSignature = new byte[] { 0x20, 0x45, 0x20, 0x69, 0x08, 0x0F, 0x03, 0x09, 0x0F, 0x20, 0x55, 0x03 };
            var types         = new string[]
            {
                "int32 modopt(100001A) modopt(1000011)",
                "char*",
                "uint32",
                "char modopt(1000015)*"
            };

            fixed(byte *testSignaturePtr = &testSignature[0])
            {
                var signatureBlob = new BlobReader(testSignaturePtr, testSignature.Length);
                var provider      = new OpaqueTokenTypeProvider();
                var decoder       = new SignatureDecoder <string, DisassemblingGenericContext>(provider, metadataReader: null, genericContext: null);

                foreach (string typeString in types)
                {
                    // Verify that each type is decoded as expected
                    Assert.Equal(typeString, decoder.DecodeType(ref signatureBlob));
                }
                // And that nothing is left over to decode
                Assert.True(signatureBlob.RemainingBytes == 0);
                Assert.Throws <BadImageFormatException>(() => decoder.DecodeType(ref signatureBlob));
            }
        }
Example #2
0
        public unsafe int GetSignature(
            int bufferLength,
            out int count,
            [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0), Out] byte[] signature)
        {
            var localSignatureHandle = _symMethod.GetLocalSignatureHandle();
            var metadataImport       = _symMethod.SymReader.GetMetadataImport();
            var local = _symMethod.MetadataReader.GetLocalVariable(_handle);

            byte *signaturePtr;
            int   signatureLength;
            int   hr = metadataImport.GetSigFromToken(MetadataTokens.GetToken(localSignatureHandle), out signaturePtr, out signatureLength);

            if (hr != HResult.S_OK)
            {
                count = 0;
                return(hr);
            }

            var signatureReader = new BlobReader(signaturePtr, signatureLength);

            SignatureHeader header = signatureReader.ReadSignatureHeader();

            if (header.Kind != SignatureKind.LocalVariables)
            {
                count = 0;
                return(HResult.E_FAIL);
            }

            int slotCount = signatureReader.ReadCompressedInteger();
            int slotIndex = local.Index;

            if (slotIndex >= slotCount)
            {
                count = 0;
                return(HResult.E_FAIL);
            }

            var typeProvider = DummyTypeProvider.Instance;

            var decoder = new SignatureDecoder <object, object>(typeProvider, metadataReader: null, genericContext: null);

            for (int i = 0; i < slotIndex - 1; i++)
            {
                decoder.DecodeType(ref signatureReader, allowTypeSpecifications: false);
            }

            int localSlotStart = signatureReader.Offset;

            decoder.DecodeType(ref signatureReader, allowTypeSpecifications: false);
            int localSlotLength = signatureReader.Offset - localSlotStart;

            if (localSlotLength <= bufferLength)
            {
                Marshal.Copy((IntPtr)(signaturePtr + localSlotStart), signature, 0, localSlotLength);
            }

            count = localSlotLength;
            return(HResult.S_OK);
        }
        TType DecodeSignature <TType>(ISignatureTypeProvider <TType> provider, SignatureDecoderOptions options = SignatureDecoderOptions.None)
        {
            var decoder    = new SignatureDecoder <TType>(provider, _reader, options);
            var blobReader = _reader.GetBlobReader(Signature);

            return(decoder.DecodeType(ref blobReader));
        }
Example #4
0
        private static string GetMemberRef(MetadataReader metadataReader, int token, CilTypeProvider provider, string genericParameterSignature = "")
        {
            var    refHandle   = MetadataTokens.MemberReferenceHandle(token);
            var    reference   = metadataReader.GetMemberReference(refHandle);
            var    parentToken = MetadataTokens.GetToken(reference.Parent);
            string type;

            if (IsTypeSpecification(parentToken))
            {
                var typeSpecificationHandle = MetadataTokens.TypeSpecificationHandle(parentToken);
                type = SignatureDecoder.DecodeType(typeSpecificationHandle, provider, null).ToString();
            }
            else
            {
                var parentHandle = MetadataTokens.TypeReferenceHandle(parentToken);
                type = SignatureDecoder.DecodeType(parentHandle, provider, null).ToString(false);
            }
            string signatureValue;
            string parameters = string.Empty;

            if (reference.GetKind() == MemberReferenceKind.Method)
            {
                MethodSignature <CilType> signature = SignatureDecoder.DecodeMethodSignature(reference.Signature, provider);
                signatureValue = GetMethodReturnType(signature);
                parameters     = provider.GetParameterList(signature);
                return(String.Format("{0} {1}::{2}{3}{4}", signatureValue, type, GetString(metadataReader, reference.Name), genericParameterSignature, parameters));
            }
            signatureValue = SignatureDecoder.DecodeFieldSignature(reference.Signature, provider).ToString();
            return(String.Format("{0} {1}::{2}{3}", signatureValue, type, GetString(metadataReader, reference.Name), parameters));
        }
Example #5
0
        internal static string SolveMethodName(MetadataReader metadataReader, int token, CilTypeProvider provider)
        {
            string genericParameters = string.Empty;

            if (IsMethodSpecification(token))
            {
                var methodHandle = MetadataTokens.MethodSpecificationHandle(token);
                var methodSpec   = metadataReader.GetMethodSpecification(methodHandle);
                token             = MetadataTokens.GetToken(methodSpec.Method);
                genericParameters = GetGenericParametersSignature(methodSpec, provider);
            }
            if (IsMemberReference(token))
            {
                return(GetMemberRef(metadataReader, token, provider, genericParameters));
            }
            var handle     = MetadataTokens.MethodDefinitionHandle(token);
            var definition = metadataReader.GetMethodDefinition(handle);
            var parent     = definition.GetDeclaringType();
            MethodSignature <CilType> signature = SignatureDecoder.DecodeMethodSignature(definition.Signature, provider);
            var returnType = GetMethodReturnType(signature);
            var parameters = provider.GetParameterList(signature);
            var parentType = SignatureDecoder.DecodeType(parent, provider, null);

            return(string.Format("{0} {1}::{2}{3}{4}", returnType, parentType.ToString(false), GetString(metadataReader, definition.Name), genericParameters, parameters));
        }
Example #6
0
        public TType DecodeSignature <TType, TGenericContext>(ISignatureTypeProvider <TType, TGenericContext> provider, TGenericContext genericContext)
        {
            var decoder    = new SignatureDecoder <TType, TGenericContext>(provider, _reader, genericContext);
            var blobReader = _reader.GetBlobReader(Signature);

            return(decoder.DecodeType(ref blobReader));
        }
        public MemberMetadataInfo GetMemberParentInfo(MemberReference memberReference)
        {
            Handle parent = memberReference.Parent;

            switch (parent.Kind)
            {
            case HandleKind.TypeReference:
                return(GetFullName((TypeReferenceHandle)parent));

            case HandleKind.TypeDefinition:
                return(new MemberMetadataInfo(GetFullName((TypeDefinitionHandle)parent))
                {
                    IsTypeDef = true
                });

            case HandleKind.TypeSpecification:
                return(SignatureDecoder.DecodeType(parent, this));

            case HandleKind.MethodDefinition:
                var method = Reader.GetMethodDefinition((MethodDefinitionHandle)parent);
                return(new MemberMetadataInfo(GetFullName(method.GetDeclaringType())));

            default:
                return(null);
            }
        }
Example #8
0
        internal TType DecodeSignature <TType>(ISignatureTypeProvider <TType> provider)
        {
            var decoder    = new SignatureDecoder <TType>(provider, _reader);
            var blobReader = _reader.GetBlobReader(Signature);

            return(decoder.DecodeType(ref blobReader));
        }
        private bool TryCalculateQualifiedTypeSpecificationName(BlobReader sigReader, Dictionary <EntityHandle, AQName> handleToName, out AQName name)
        {
            var builder = new ImportTypeSpecNameBuilder(handleToName, _lazyAssemblyRefMap.Value.Item2);
            var decoder = new SignatureDecoder <AQName, object>(builder, metadataReader: null, genericContext: null);

            name = decoder.DecodeType(ref sigReader);
            return(builder.IsSupported);
        }
Example #10
0
        internal static string DecodeOverridenMethodName(MetadataReader metadataReader, int token, CilTypeProvider provider)
        {
            var handle     = MetadataTokens.MethodDefinitionHandle(token);
            var definition = metadataReader.GetMethodDefinition(handle);
            var parent     = definition.GetDeclaringType();
            var parentType = SignatureDecoder.DecodeType(parent, provider, null);

            return(string.Format("{0}::{1}", parentType.ToString(false), GetString(metadataReader, definition.Name)));
        }
 private RuntimeTypeInfo TryResolveSignature(TypeContext typeContext, ref Exception exception)
 {
     ReflectionTypeProvider typeProvider = new ReflectionTypeProvider(throwOnError: false);
     SignatureDecoder<RuntimeTypeInfo, TypeContext> signatureDecoder = new SignatureDecoder<RuntimeTypeInfo, TypeContext>(typeProvider, (MetadataReader)Reader, typeContext);
     BlobReader localCopyOfReader = _blobReader;
     RuntimeTypeInfo result = signatureDecoder.DecodeType(ref localCopyOfReader, false);
     exception = typeProvider.ExceptionResult;
     return result;
 }
Example #12
0
        private static string GetFieldInformation(MetadataReader metadataReader, int intOperand, CilTypeProvider provider)
        {
            if (IsMemberReference(intOperand))
            {
                return(GetMemberRef(metadataReader, intOperand, provider));
            }
            var handle        = MetadataTokens.FieldDefinitionHandle(intOperand);
            var definition    = metadataReader.GetFieldDefinition(handle);
            var typeHandle    = definition.GetDeclaringType();
            var typeSignature = SignatureDecoder.DecodeType(typeHandle, provider, null);
            var signature     = SignatureDecoder.DecodeFieldSignature(definition.Signature, provider);

            return(String.Format("{0} {1}::{2}", signature.ToString(), typeSignature.ToString(false), GetString(metadataReader, definition.Name)));
        }
        public unsafe void DecodeValidMethodSpecificationSignature(string[] expectedTypes, byte[] testSignature)
        {
            fixed(byte *testSignaturePtr = &testSignature[0])
            {
                var signatureBlob = new BlobReader(testSignaturePtr, testSignature.Length);
                var provider      = new OpaqueTokenTypeProvider();
                var decoder       = new SignatureDecoder <string, DisassemblingGenericContext>(provider, metadataReader: null, genericContext: null);

                IEnumerable <string> actualTypes = decoder.DecodeMethodSpecificationSignature(ref signatureBlob);

                Assert.Equal(expectedTypes, actualTypes);
                Assert.True(signatureBlob.RemainingBytes == 0);
                Assert.Throws <BadImageFormatException>(() => decoder.DecodeType(ref signatureBlob));
            }
        }
Example #14
0
        private static string GetTypeInformation(MetadataReader metadataReader, int intOperand, CilTypeProvider provider)
        {
            if (IsTypeReference(intOperand))
            {
                var refHandle = MetadataTokens.TypeReferenceHandle(intOperand);
                return(SignatureDecoder.DecodeType(refHandle, provider, null).ToString());
            }
            if (IsTypeSpecification(intOperand))
            {
                var typeHandle = MetadataTokens.TypeSpecificationHandle(intOperand);
                return(SignatureDecoder.DecodeType(typeHandle, provider, null).ToString());
            }
            var defHandle = MetadataTokens.TypeDefinitionHandle(intOperand);

            return(SignatureDecoder.DecodeType(defHandle, provider, null).ToString());
        }
Example #15
0
 internal static CilType DecodeType(EntityHandle type, CilTypeProvider provider)
 {
     return(SignatureDecoder.DecodeType(type, provider, null));
 }