| | | 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.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace System.Reflection.Metadata.Ecma335 |
| | | 7 | | { |
| | | 8 | | public readonly struct ExceptionRegionEncoder |
| | | 9 | | { |
| | | 10 | | private const int TableHeaderSize = 4; |
| | | 11 | | |
| | | 12 | | private const int SmallRegionSize = |
| | | 13 | | sizeof(short) + // Flags |
| | | 14 | | sizeof(short) + // TryOffset |
| | | 15 | | sizeof(byte) + // TryLength |
| | | 16 | | sizeof(short) + // HandlerOffset |
| | | 17 | | sizeof(byte) + // HandleLength |
| | | 18 | | sizeof(int); // ClassToken | FilterOffset |
| | | 19 | | |
| | | 20 | | private const int FatRegionSize = |
| | | 21 | | sizeof(int) + // Flags |
| | | 22 | | sizeof(int) + // TryOffset |
| | | 23 | | sizeof(int) + // TryLength |
| | | 24 | | sizeof(int) + // HandlerOffset |
| | | 25 | | sizeof(int) + // HandleLength |
| | | 26 | | sizeof(int); // ClassToken | FilterOffset |
| | | 27 | | |
| | | 28 | | private const int ThreeBytesMaxValue = 0xffffff; |
| | | 29 | | internal const int MaxSmallExceptionRegions = (byte.MaxValue - TableHeaderSize) / SmallRegionSize; |
| | | 30 | | internal const int MaxExceptionRegions = (ThreeBytesMaxValue - TableHeaderSize) / FatRegionSize; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// The underlying builder. |
| | | 34 | | /// </summary> |
| | 0 | 35 | | public BlobBuilder Builder { get; } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// True if the encoder uses small format. |
| | | 39 | | /// </summary> |
| | 0 | 40 | | public bool HasSmallFormat { get; } |
| | | 41 | | |
| | | 42 | | internal ExceptionRegionEncoder(BlobBuilder builder, bool hasSmallFormat) |
| | 0 | 43 | | { |
| | 0 | 44 | | Builder = builder; |
| | 0 | 45 | | HasSmallFormat = hasSmallFormat; |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Returns true if the number of exception regions first small format. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="exceptionRegionCount">Number of exception regions.</param> |
| | | 52 | | public static bool IsSmallRegionCount(int exceptionRegionCount) => |
| | 0 | 53 | | unchecked((uint)exceptionRegionCount) <= MaxSmallExceptionRegions; |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Returns true if the region fits small format. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="startOffset">Start offset of the region.</param> |
| | | 59 | | /// <param name="length">Length of the region.</param> |
| | | 60 | | public static bool IsSmallExceptionRegion(int startOffset, int length) => |
| | 0 | 61 | | unchecked((uint)startOffset) <= ushort.MaxValue && unchecked((uint)length) <= byte.MaxValue; |
| | | 62 | | |
| | | 63 | | internal static bool IsSmallExceptionRegionFromBounds(int startOffset, int endOffset) => |
| | 0 | 64 | | IsSmallExceptionRegion(startOffset, endOffset - startOffset); |
| | | 65 | | |
| | | 66 | | internal static int GetExceptionTableSize(int exceptionRegionCount, bool isSmallFormat) => |
| | 0 | 67 | | TableHeaderSize + exceptionRegionCount * (isSmallFormat ? SmallRegionSize : FatRegionSize); |
| | | 68 | | |
| | | 69 | | internal static bool IsExceptionRegionCountInBounds(int exceptionRegionCount) => |
| | 0 | 70 | | unchecked((uint)exceptionRegionCount) <= MaxExceptionRegions; |
| | | 71 | | |
| | | 72 | | internal static bool IsValidCatchTypeHandle(EntityHandle catchType) |
| | 0 | 73 | | { |
| | 0 | 74 | | return !catchType.IsNil && |
| | 0 | 75 | | (catchType.Kind == HandleKind.TypeDefinition || |
| | 0 | 76 | | catchType.Kind == HandleKind.TypeSpecification || |
| | 0 | 77 | | catchType.Kind == HandleKind.TypeReference); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | internal static ExceptionRegionEncoder SerializeTableHeader(BlobBuilder builder, int exceptionRegionCount, bool |
| | 0 | 81 | | { |
| | 0 | 82 | | Debug.Assert(exceptionRegionCount > 0); |
| | | 83 | | |
| | | 84 | | const byte EHTableFlag = 0x01; |
| | | 85 | | const byte FatFormatFlag = 0x40; |
| | | 86 | | |
| | 0 | 87 | | bool hasSmallFormat = hasSmallRegions && IsSmallRegionCount(exceptionRegionCount); |
| | 0 | 88 | | int dataSize = GetExceptionTableSize(exceptionRegionCount, hasSmallFormat); |
| | | 89 | | |
| | 0 | 90 | | builder.Align(4); |
| | 0 | 91 | | if (hasSmallFormat) |
| | 0 | 92 | | { |
| | 0 | 93 | | builder.WriteByte(EHTableFlag); |
| | 0 | 94 | | builder.WriteByte(unchecked((byte)dataSize)); |
| | 0 | 95 | | builder.WriteInt16(0); |
| | 0 | 96 | | } |
| | | 97 | | else |
| | 0 | 98 | | { |
| | 0 | 99 | | Debug.Assert(dataSize <= 0x00ffffff); |
| | 0 | 100 | | builder.WriteByte(EHTableFlag | FatFormatFlag); |
| | 0 | 101 | | builder.WriteByte(unchecked((byte)dataSize)); |
| | 0 | 102 | | builder.WriteUInt16(unchecked((ushort)(dataSize >> 8))); |
| | 0 | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | return new ExceptionRegionEncoder(builder, hasSmallFormat); |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Adds a finally clause. |
| | | 110 | | /// </summary> |
| | | 111 | | /// <param name="tryOffset">Try block start offset.</param> |
| | | 112 | | /// <param name="tryLength">Try block length.</param> |
| | | 113 | | /// <param name="handlerOffset">Handler start offset.</param> |
| | | 114 | | /// <param name="handlerLength">Handler length.</param> |
| | | 115 | | /// <returns>Encoder for the next clause.</returns> |
| | | 116 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 117 | | /// <paramref name="tryOffset"/>, <paramref name="tryLength"/>, <paramref name="handlerOffset"/> or <paramref na |
| | | 118 | | /// </exception> |
| | | 119 | | /// <exception cref="InvalidOperationException">Method body was not declared to have exception regions.</excepti |
| | | 120 | | public ExceptionRegionEncoder AddFinally(int tryOffset, int tryLength, int handlerOffset, int handlerLength) |
| | 0 | 121 | | { |
| | 0 | 122 | | return Add(ExceptionRegionKind.Finally, tryOffset, tryLength, handlerOffset, handlerLength, default(EntityHa |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Adds a fault clause. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <param name="tryOffset">Try block start offset.</param> |
| | | 129 | | /// <param name="tryLength">Try block length.</param> |
| | | 130 | | /// <param name="handlerOffset">Handler start offset.</param> |
| | | 131 | | /// <param name="handlerLength">Handler length.</param> |
| | | 132 | | /// <returns>Encoder for the next clause.</returns> |
| | | 133 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 134 | | /// <paramref name="tryOffset"/>, <paramref name="tryLength"/>, <paramref name="handlerOffset"/> or <paramref na |
| | | 135 | | /// </exception> |
| | | 136 | | /// <exception cref="InvalidOperationException">Method body was not declared to have exception regions.</excepti |
| | | 137 | | public ExceptionRegionEncoder AddFault(int tryOffset, int tryLength, int handlerOffset, int handlerLength) |
| | 0 | 138 | | { |
| | 0 | 139 | | return Add(ExceptionRegionKind.Fault, tryOffset, tryLength, handlerOffset, handlerLength, default(EntityHand |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Adds a fault clause. |
| | | 144 | | /// </summary> |
| | | 145 | | /// <param name="tryOffset">Try block start offset.</param> |
| | | 146 | | /// <param name="tryLength">Try block length.</param> |
| | | 147 | | /// <param name="handlerOffset">Handler start offset.</param> |
| | | 148 | | /// <param name="handlerLength">Handler length.</param> |
| | | 149 | | /// <param name="catchType"> |
| | | 150 | | /// <see cref="TypeDefinitionHandle"/>, <see cref="TypeReferenceHandle"/> or <see cref="TypeSpecificationHandle" |
| | | 151 | | /// </param> |
| | | 152 | | /// <returns>Encoder for the next clause.</returns> |
| | | 153 | | /// <exception cref="ArgumentException"><paramref name="catchType"/> is invalid.</exception> |
| | | 154 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 155 | | /// <paramref name="tryOffset"/>, <paramref name="tryLength"/>, <paramref name="handlerOffset"/> or <paramref na |
| | | 156 | | /// </exception> |
| | | 157 | | /// <exception cref="InvalidOperationException">Method body was not declared to have exception regions.</excepti |
| | | 158 | | public ExceptionRegionEncoder AddCatch(int tryOffset, int tryLength, int handlerOffset, int handlerLength, Entit |
| | 0 | 159 | | { |
| | 0 | 160 | | return Add(ExceptionRegionKind.Catch, tryOffset, tryLength, handlerOffset, handlerLength, catchType, 0); |
| | 0 | 161 | | } |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Adds a fault clause. |
| | | 165 | | /// </summary> |
| | | 166 | | /// <param name="tryOffset">Try block start offset.</param> |
| | | 167 | | /// <param name="tryLength">Try block length.</param> |
| | | 168 | | /// <param name="handlerOffset">Handler start offset.</param> |
| | | 169 | | /// <param name="handlerLength">Handler length.</param> |
| | | 170 | | /// <param name="filterOffset">Offset of the filter block.</param> |
| | | 171 | | /// <returns>Encoder for the next clause.</returns> |
| | | 172 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 173 | | /// <paramref name="tryOffset"/>, <paramref name="tryLength"/>, <paramref name="handlerOffset"/> or <paramref na |
| | | 174 | | /// </exception> |
| | | 175 | | /// <exception cref="InvalidOperationException">Method body was not declared to have exception regions.</excepti |
| | | 176 | | public ExceptionRegionEncoder AddFilter(int tryOffset, int tryLength, int handlerOffset, int handlerLength, int |
| | 0 | 177 | | { |
| | 0 | 178 | | return Add(ExceptionRegionKind.Filter, tryOffset, tryLength, handlerOffset, handlerLength, default(EntityHan |
| | 0 | 179 | | } |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Adds an exception clause. |
| | | 183 | | /// </summary> |
| | | 184 | | /// <param name="kind">Clause kind.</param> |
| | | 185 | | /// <param name="tryOffset">Try block start offset.</param> |
| | | 186 | | /// <param name="tryLength">Try block length.</param> |
| | | 187 | | /// <param name="handlerOffset">Handler start offset.</param> |
| | | 188 | | /// <param name="handlerLength">Handler length.</param> |
| | | 189 | | /// <param name="catchType"> |
| | | 190 | | /// <see cref="TypeDefinitionHandle"/>, <see cref="TypeReferenceHandle"/> or <see cref="TypeSpecificationHandle" |
| | | 191 | | /// or nil if <paramref name="kind"/> is not <see cref="ExceptionRegionKind.Catch"/> |
| | | 192 | | /// </param> |
| | | 193 | | /// <param name="filterOffset"> |
| | | 194 | | /// Offset of the filter block, or 0 if the <paramref name="kind"/> is not <see cref="ExceptionRegionKind.Filter |
| | | 195 | | /// </param> |
| | | 196 | | /// <returns>Encoder for the next clause.</returns> |
| | | 197 | | /// <exception cref="ArgumentException"><paramref name="catchType"/> is invalid.</exception> |
| | | 198 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="kind"/> has invalid value.</exception> |
| | | 199 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 200 | | /// <paramref name="tryOffset"/>, <paramref name="tryLength"/>, <paramref name="handlerOffset"/> or <paramref na |
| | | 201 | | /// </exception> |
| | | 202 | | /// <exception cref="InvalidOperationException">Method body was not declared to have exception regions.</excepti |
| | | 203 | | public ExceptionRegionEncoder Add( |
| | | 204 | | ExceptionRegionKind kind, |
| | | 205 | | int tryOffset, |
| | | 206 | | int tryLength, |
| | | 207 | | int handlerOffset, |
| | | 208 | | int handlerLength, |
| | | 209 | | EntityHandle catchType = default(EntityHandle), |
| | | 210 | | int filterOffset = 0) |
| | 0 | 211 | | { |
| | 0 | 212 | | if (Builder == null) |
| | 0 | 213 | | { |
| | 0 | 214 | | Throw.InvalidOperation(SR.MethodHasNoExceptionRegions); |
| | | 215 | | } |
| | | 216 | | |
| | 0 | 217 | | if (HasSmallFormat) |
| | 0 | 218 | | { |
| | 0 | 219 | | if (unchecked((ushort)tryOffset) != tryOffset) Throw.ArgumentOutOfRange(nameof(tryOffset)); |
| | 0 | 220 | | if (unchecked((byte)tryLength) != tryLength) Throw.ArgumentOutOfRange(nameof(tryLength)); |
| | 0 | 221 | | if (unchecked((ushort)handlerOffset) != handlerOffset) Throw.ArgumentOutOfRange(nameof(handlerOffset)); |
| | 0 | 222 | | if (unchecked((byte)handlerLength) != handlerLength) Throw.ArgumentOutOfRange(nameof(handlerLength)); |
| | 0 | 223 | | } |
| | | 224 | | else |
| | 0 | 225 | | { |
| | 0 | 226 | | if (tryOffset < 0) Throw.ArgumentOutOfRange(nameof(tryOffset)); |
| | 0 | 227 | | if (tryLength < 0) Throw.ArgumentOutOfRange(nameof(tryLength)); |
| | 0 | 228 | | if (handlerOffset < 0) Throw.ArgumentOutOfRange(nameof(handlerOffset)); |
| | 0 | 229 | | if (handlerLength < 0) Throw.ArgumentOutOfRange(nameof(handlerLength)); |
| | 0 | 230 | | } |
| | | 231 | | |
| | | 232 | | int catchTokenOrOffset; |
| | 0 | 233 | | switch (kind) |
| | | 234 | | { |
| | | 235 | | case ExceptionRegionKind.Catch: |
| | 0 | 236 | | if (!IsValidCatchTypeHandle(catchType)) |
| | 0 | 237 | | { |
| | 0 | 238 | | Throw.InvalidArgument_Handle(nameof(catchType)); |
| | | 239 | | } |
| | | 240 | | |
| | 0 | 241 | | catchTokenOrOffset = MetadataTokens.GetToken(catchType); |
| | 0 | 242 | | break; |
| | | 243 | | |
| | | 244 | | case ExceptionRegionKind.Filter: |
| | 0 | 245 | | if (filterOffset < 0) |
| | 0 | 246 | | { |
| | 0 | 247 | | Throw.ArgumentOutOfRange(nameof(filterOffset)); |
| | | 248 | | } |
| | | 249 | | |
| | 0 | 250 | | catchTokenOrOffset = filterOffset; |
| | 0 | 251 | | break; |
| | | 252 | | |
| | | 253 | | case ExceptionRegionKind.Finally: |
| | | 254 | | case ExceptionRegionKind.Fault: |
| | 0 | 255 | | catchTokenOrOffset = 0; |
| | 0 | 256 | | break; |
| | | 257 | | |
| | | 258 | | default: |
| | 0 | 259 | | throw new ArgumentOutOfRangeException(nameof(kind)); |
| | | 260 | | } |
| | | 261 | | |
| | 0 | 262 | | AddUnchecked(kind, tryOffset, tryLength, handlerOffset, handlerLength, catchTokenOrOffset); |
| | 0 | 263 | | return this; |
| | 0 | 264 | | } |
| | | 265 | | |
| | | 266 | | internal void AddUnchecked( |
| | | 267 | | ExceptionRegionKind kind, |
| | | 268 | | int tryOffset, |
| | | 269 | | int tryLength, |
| | | 270 | | int handlerOffset, |
| | | 271 | | int handlerLength, |
| | | 272 | | int catchTokenOrOffset) |
| | 0 | 273 | | { |
| | 0 | 274 | | if (HasSmallFormat) |
| | 0 | 275 | | { |
| | 0 | 276 | | Builder.WriteUInt16((ushort)kind); |
| | 0 | 277 | | Builder.WriteUInt16((ushort)tryOffset); |
| | 0 | 278 | | Builder.WriteByte((byte)tryLength); |
| | 0 | 279 | | Builder.WriteUInt16((ushort)handlerOffset); |
| | 0 | 280 | | Builder.WriteByte((byte)handlerLength); |
| | 0 | 281 | | } |
| | | 282 | | else |
| | 0 | 283 | | { |
| | 0 | 284 | | Builder.WriteInt32((int)kind); |
| | 0 | 285 | | Builder.WriteInt32(tryOffset); |
| | 0 | 286 | | Builder.WriteInt32(tryLength); |
| | 0 | 287 | | Builder.WriteInt32(handlerOffset); |
| | 0 | 288 | | Builder.WriteInt32(handlerLength); |
| | 0 | 289 | | } |
| | | 290 | | |
| | 0 | 291 | | Builder.WriteInt32(catchTokenOrOffset); |
| | 0 | 292 | | } |
| | | 293 | | } |
| | | 294 | | } |
| | | 295 | | |