| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Immutable; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Reflection.Internal; |
| | | 7 | | using System.Reflection.Metadata.Ecma335; |
| | | 8 | | using System.Runtime.InteropServices; |
| | | 9 | | |
| | | 10 | | namespace System.Reflection.Metadata |
| | | 11 | | { |
| | | 12 | | public sealed class MethodBodyBlock |
| | | 13 | | { |
| | | 14 | | private readonly MemoryBlock _il; |
| | | 15 | | private readonly int _size; |
| | | 16 | | private readonly ushort _maxStack; |
| | | 17 | | private readonly bool _localVariablesInitialized; |
| | | 18 | | private readonly StandaloneSignatureHandle _localSignature; |
| | | 19 | | private readonly ImmutableArray<ExceptionRegion> _exceptionRegions; |
| | | 20 | | |
| | 0 | 21 | | private MethodBodyBlock( |
| | 0 | 22 | | bool localVariablesInitialized, |
| | 0 | 23 | | ushort maxStack, |
| | 0 | 24 | | StandaloneSignatureHandle localSignatureHandle, |
| | 0 | 25 | | MemoryBlock il, |
| | 0 | 26 | | ImmutableArray<ExceptionRegion> exceptionRegions, |
| | 0 | 27 | | int size) |
| | 0 | 28 | | { |
| | 0 | 29 | | Debug.Assert(!exceptionRegions.IsDefault); |
| | | 30 | | |
| | 0 | 31 | | _localVariablesInitialized = localVariablesInitialized; |
| | 0 | 32 | | _maxStack = maxStack; |
| | 0 | 33 | | _localSignature = localSignatureHandle; |
| | 0 | 34 | | _il = il; |
| | 0 | 35 | | _exceptionRegions = exceptionRegions; |
| | 0 | 36 | | _size = size; |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Size of the method body - includes the header, IL and exception regions. |
| | | 41 | | /// </summary> |
| | | 42 | | public int Size |
| | | 43 | | { |
| | 0 | 44 | | get { return _size; } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public int MaxStack |
| | | 48 | | { |
| | 0 | 49 | | get { return _maxStack; } |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | public bool LocalVariablesInitialized |
| | | 53 | | { |
| | 0 | 54 | | get { return _localVariablesInitialized; } |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public StandaloneSignatureHandle LocalSignature |
| | | 58 | | { |
| | 0 | 59 | | get { return _localSignature; } |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public ImmutableArray<ExceptionRegion> ExceptionRegions |
| | | 63 | | { |
| | 0 | 64 | | get { return _exceptionRegions; } |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | public byte[]? GetILBytes() |
| | 0 | 68 | | { |
| | 0 | 69 | | return _il.ToArray(); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | public ImmutableArray<byte> GetILContent() |
| | 0 | 73 | | { |
| | 0 | 74 | | byte[]? bytes = GetILBytes(); |
| | 0 | 75 | | return ImmutableCollectionsMarshal.AsImmutableArray(bytes); |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | public BlobReader GetILReader() |
| | 0 | 79 | | { |
| | 0 | 80 | | return new BlobReader(_il); |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | private const byte ILTinyFormat = 0x02; |
| | | 84 | | private const byte ILFatFormat = 0x03; |
| | | 85 | | private const byte ILFormatMask = 0x03; |
| | | 86 | | private const int ILTinyFormatSizeShift = 2; |
| | | 87 | | private const byte ILMoreSects = 0x08; |
| | | 88 | | private const byte ILInitLocals = 0x10; |
| | | 89 | | private const byte ILFatFormatHeaderSize = 0x03; |
| | | 90 | | private const int ILFatFormatHeaderSizeShift = 4; |
| | | 91 | | private const byte SectEHTable = 0x01; |
| | | 92 | | private const byte SectFatFormat = 0x40; |
| | | 93 | | |
| | | 94 | | public static MethodBodyBlock Create(BlobReader reader) |
| | 0 | 95 | | { |
| | 0 | 96 | | int startOffset = reader.Offset; |
| | | 97 | | int ilSize; |
| | | 98 | | |
| | | 99 | | // Error need to check if the Memory Block is empty. This is false for all the calls... |
| | 0 | 100 | | byte headByte = reader.ReadByte(); |
| | 0 | 101 | | if ((headByte & ILFormatMask) == ILTinyFormat) |
| | 0 | 102 | | { |
| | | 103 | | // tiny IL can't have locals so technically this shouldn't matter, |
| | | 104 | | // but false is consistent with other metadata readers and helps |
| | | 105 | | // for use cases involving comparing our output with theirs. |
| | | 106 | | const bool initLocalsForTinyIL = false; |
| | | 107 | | |
| | 0 | 108 | | ilSize = headByte >> ILTinyFormatSizeShift; |
| | 0 | 109 | | return new MethodBodyBlock( |
| | 0 | 110 | | initLocalsForTinyIL, |
| | 0 | 111 | | 8, |
| | 0 | 112 | | default(StandaloneSignatureHandle), |
| | 0 | 113 | | reader.GetMemoryBlockAt(0, ilSize), |
| | 0 | 114 | | ImmutableArray<ExceptionRegion>.Empty, |
| | 0 | 115 | | 1 + ilSize // header + IL |
| | 0 | 116 | | ); |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | if ((headByte & ILFormatMask) != ILFatFormat) |
| | 0 | 120 | | { |
| | 0 | 121 | | throw new BadImageFormatException(SR.Format(SR.InvalidMethodHeader1, headByte)); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | // FatILFormat |
| | 0 | 125 | | byte headByte2 = reader.ReadByte(); |
| | 0 | 126 | | if ((headByte2 >> ILFatFormatHeaderSizeShift) != ILFatFormatHeaderSize) |
| | 0 | 127 | | { |
| | 0 | 128 | | throw new BadImageFormatException(SR.Format(SR.InvalidMethodHeader2, headByte, headByte2)); |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | bool localsInitialized = (headByte & ILInitLocals) == ILInitLocals; |
| | 0 | 132 | | bool hasExceptionHandlers = (headByte & ILMoreSects) == ILMoreSects; |
| | | 133 | | |
| | 0 | 134 | | ushort maxStack = reader.ReadUInt16(); |
| | 0 | 135 | | ilSize = reader.ReadInt32(); |
| | | 136 | | |
| | 0 | 137 | | int localSignatureToken = reader.ReadInt32(); |
| | | 138 | | StandaloneSignatureHandle localSignatureHandle; |
| | 0 | 139 | | if (localSignatureToken == 0) |
| | 0 | 140 | | { |
| | 0 | 141 | | localSignatureHandle = default(StandaloneSignatureHandle); |
| | 0 | 142 | | } |
| | 0 | 143 | | else if ((localSignatureToken & TokenTypeIds.TypeMask) == TokenTypeIds.Signature) |
| | 0 | 144 | | { |
| | 0 | 145 | | localSignatureHandle = StandaloneSignatureHandle.FromRowId((int)((uint)localSignatureToken & TokenTypeId |
| | 0 | 146 | | } |
| | | 147 | | else |
| | 0 | 148 | | { |
| | 0 | 149 | | throw new BadImageFormatException(SR.Format(SR.InvalidLocalSignatureToken, unchecked((uint)localSignatur |
| | | 150 | | } |
| | | 151 | | |
| | 0 | 152 | | var ilBlock = reader.GetMemoryBlockAt(0, ilSize); |
| | 0 | 153 | | reader.Offset += ilSize; |
| | | 154 | | |
| | | 155 | | ImmutableArray<ExceptionRegion> exceptionHandlers; |
| | 0 | 156 | | if (hasExceptionHandlers) |
| | 0 | 157 | | { |
| | 0 | 158 | | reader.Align(4); |
| | 0 | 159 | | byte sehHeader = reader.ReadByte(); |
| | 0 | 160 | | if ((sehHeader & SectEHTable) != SectEHTable) |
| | 0 | 161 | | { |
| | 0 | 162 | | throw new BadImageFormatException(SR.Format(SR.InvalidSehHeader, sehHeader)); |
| | | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | bool sehFatFormat = (sehHeader & SectFatFormat) == SectFatFormat; |
| | 0 | 166 | | int dataSize = reader.ReadByte(); |
| | 0 | 167 | | if (sehFatFormat) |
| | 0 | 168 | | { |
| | 0 | 169 | | dataSize += reader.ReadUInt16() << 8; |
| | 0 | 170 | | exceptionHandlers = ReadFatExceptionHandlers(ref reader, dataSize / 24); |
| | 0 | 171 | | } |
| | | 172 | | else |
| | 0 | 173 | | { |
| | 0 | 174 | | reader.Offset += 2; // skip over reserved field |
| | 0 | 175 | | exceptionHandlers = ReadSmallExceptionHandlers(ref reader, dataSize / 12); |
| | 0 | 176 | | } |
| | 0 | 177 | | } |
| | | 178 | | else |
| | 0 | 179 | | { |
| | 0 | 180 | | exceptionHandlers = ImmutableArray<ExceptionRegion>.Empty; |
| | 0 | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | return new MethodBodyBlock( |
| | 0 | 184 | | localsInitialized, |
| | 0 | 185 | | maxStack, |
| | 0 | 186 | | localSignatureHandle, |
| | 0 | 187 | | ilBlock, |
| | 0 | 188 | | exceptionHandlers, |
| | 0 | 189 | | reader.Offset - startOffset); |
| | 0 | 190 | | } |
| | | 191 | | |
| | | 192 | | private static ImmutableArray<ExceptionRegion> ReadSmallExceptionHandlers(ref BlobReader memReader, int count) |
| | 0 | 193 | | { |
| | 0 | 194 | | var result = ImmutableArray.CreateBuilder<ExceptionRegion>(count); |
| | 0 | 195 | | for (int i = 0; i < count; i++) |
| | 0 | 196 | | { |
| | 0 | 197 | | var kind = (ExceptionRegionKind)memReader.ReadUInt16(); |
| | 0 | 198 | | var tryOffset = memReader.ReadUInt16(); |
| | 0 | 199 | | var tryLength = memReader.ReadByte(); |
| | 0 | 200 | | var handlerOffset = memReader.ReadUInt16(); |
| | 0 | 201 | | var handlerLength = memReader.ReadByte(); |
| | 0 | 202 | | var classTokenOrFilterOffset = memReader.ReadInt32(); |
| | 0 | 203 | | result.Add(new ExceptionRegion(kind, tryOffset, tryLength, handlerOffset, handlerLength, classTokenOrFil |
| | 0 | 204 | | } |
| | | 205 | | |
| | 0 | 206 | | return result.MoveToImmutable(); |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | private static ImmutableArray<ExceptionRegion> ReadFatExceptionHandlers(ref BlobReader memReader, int count) |
| | 0 | 210 | | { |
| | 0 | 211 | | var result = ImmutableArray.CreateBuilder<ExceptionRegion>(count); |
| | 0 | 212 | | for (int i = 0; i < count; i++) |
| | 0 | 213 | | { |
| | 0 | 214 | | var kind = (ExceptionRegionKind)memReader.ReadUInt32(); |
| | 0 | 215 | | int tryOffset = memReader.ReadInt32(); |
| | 0 | 216 | | int tryLength = memReader.ReadInt32(); |
| | 0 | 217 | | int handlerOffset = memReader.ReadInt32(); |
| | 0 | 218 | | int handlerLength = memReader.ReadInt32(); |
| | 0 | 219 | | int classTokenOrFilterOffset = memReader.ReadInt32(); |
| | 0 | 220 | | result.Add(new ExceptionRegion(kind, tryOffset, tryLength, handlerOffset, handlerLength, classTokenOrFil |
| | 0 | 221 | | } |
| | | 222 | | |
| | 0 | 223 | | return result.MoveToImmutable(); |
| | 0 | 224 | | } |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | |