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