/
MethodInvoker.cs
490 lines (427 loc) · 19.6 KB
/
MethodInvoker.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// 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;
using System.Reflection.Emit;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using static System.Reflection.InvokerEmitUtil;
using static System.Reflection.MethodBase;
using static System.Reflection.MethodInvokerCommon;
namespace System.Reflection
{
/// <summary>
/// Invokes the method reflected by the provided <see cref="MethodBase"/>.
/// </summary>
/// <remarks>
/// Used for better performance than <seealso cref="MethodBase.Invoke"/> when compatibility with that method
/// is not necessary and when the caller can cache the MethodInvoker instance for additional invoke calls.<br/>
/// Unlike <see cref="MethodBase.Invoke"/>, the invoke methods do not look up default values for arguments when
/// <see cref="Type.Missing"/> is specified. In addition, the target method may be inlined for performance and not
/// appear in stack traces.
/// </remarks>
/// <seealso cref="ConstructorInvoker"/>
public sealed partial class MethodInvoker
{
private InvokeFunc_ObjSpanArgs? _invokeFunc_ObjSpanArgs;
private InvokeFunc_Obj4Args? _invokeFunc_Obj4Args;
private InvokeFunc_RefArgs? _invokeFunc_RefArgs;
private InvokerStrategy _strategy;
private readonly int _argCount;
private readonly RuntimeType[] _argTypes;
private readonly InvocationFlags _invocationFlags;
private readonly InvokerArgFlags[] _invokerArgFlags;
private readonly MethodBase _method;
private readonly bool _needsByRefStrategy;
private readonly bool _isStatic;
/// <summary>
/// Creates a new instance of MethodInvoker.
/// </summary>
/// <remarks>
/// For performance, the resulting instance should be cached for additional calls.
/// </remarks>
/// <param name="method">The method that will be invoked.</param>
/// <returns>An instance of a MethodInvoker.</returns>
/// <exception cref="ArgumentException">
/// The <paramref name="method"/> is not a runtime-based method.
/// </exception>
public static MethodInvoker Create(MethodBase method)
{
ArgumentNullException.ThrowIfNull(method, nameof(method));
if (method is RuntimeMethodInfo rmi)
{
return new MethodInvoker(rmi);
}
if (method is DynamicMethod dm)
{
return new MethodInvoker(dm);
}
if (method is RuntimeConstructorInfo rci)
{
// This is useful for calling a constructor on an already-initialized object
// such as created from RuntimeHelpers.GetUninitializedObject(Type).
MethodInvoker invoker = new MethodInvoker(rci);
// Use the interpreted version to avoid having to generate a new method that doesn't allocate.
invoker._strategy = GetStrategyForUsingInterpreted();
return invoker;
}
throw new ArgumentException(SR.Argument_MustBeRuntimeMethod, nameof(method));
}
private MethodInvoker(MethodBase method, RuntimeType[] argumentTypes)
{
_method = method;
_argTypes = argumentTypes;
_argCount = _argTypes.Length;
_isStatic = _method.IsStatic;
Initialize(argumentTypes, out _strategy, out _invokerArgFlags, out _needsByRefStrategy);
}
/// <summary>
/// Invokes the method using the specified parameters.
/// </summary>
/// <param name="obj">
/// The object on which to invoke the method. If the method is static, this argument is ignored.
/// </param>
/// <returns>
/// An object containing the return value of the invoked method,
/// or <c>null</c> if the invoked method does not have a return value.
/// </returns>
/// <exception cref="TargetException">
/// The <para>obj</para> parameter is <c>null</c> and the method is not static.
///
/// -or-
///
/// The method is not declared or inherited by the class of <para>obj</para>.
/// </exception>
/// <exception cref="InvalidOperationException">
/// The type that declares the method is an open generic type.
/// </exception>
/// <exception cref="TargetParameterCountException">
/// The correct number of arguments were not provided.
/// </exception>
/// <exception cref="NotSupportedException">
/// The calling convention or signature is not supported.
/// </exception>
public object? Invoke(object? obj)
{
if (_argCount != 0)
{
MethodBaseInvoker.ThrowTargetParameterCountException();
}
return InvokeImpl(obj, null, null, null, null);
}
/// <inheritdoc cref="Invoke(object?)"/>
/// <param name="obj"> The object on which to invoke the method. If the method is static, this argument is ignored. </param>
/// <param name="arg1">The first argument for the invoked method.</param>
/// <exception cref="ArgumentException">
/// The arguments do not match the signature of the invoked method.
/// </exception>
public object? Invoke(object? obj, object? arg1)
{
if (_argCount != 1)
{
MethodBaseInvoker.ThrowTargetParameterCountException();
}
return InvokeImpl(obj, arg1, null, null, null);
}
/// <inheritdoc cref="Invoke(object?)"/>
/// <param name="obj"> The object on which to invoke the method. If the method is static, this argument is ignored. </param>
/// <param name="arg1">The first argument for the invoked method.</param>
/// <param name="arg2">The second argument for the invoked method.</param>
public object? Invoke(object? obj, object? arg1, object? arg2)
{
if (_argCount != 2)
{
MethodBaseInvoker.ThrowTargetParameterCountException();
}
return InvokeImpl(obj, arg1, arg2, null, null);
}
/// <inheritdoc cref="Invoke(object?)"/>
/// <param name="obj"> The object on which to invoke the method. If the method is static, this argument is ignored. </param>
/// <param name="arg1">The first argument for the invoked method.</param>
/// <param name="arg2">The second argument for the invoked method.</param>
/// <param name="arg3">The third argument for the invoked method.</param>
public object? Invoke(object? obj, object? arg1, object? arg2, object? arg3)
{
if (_argCount != 3)
{
MethodBaseInvoker.ThrowTargetParameterCountException();
}
return InvokeImpl(obj, arg1, arg2, arg3, null);
}
/// <inheritdoc cref="Invoke(object?)"/>
/// <param name="obj"> The object on which to invoke the method. If the method is static, this argument is ignored. </param>
/// <param name="arg1">The first argument for the invoked method.</param>
/// <param name="arg2">The second argument for the invoked method.</param>
/// <param name="arg3">The third argument for the invoked method.</param>
/// <param name="arg4">The fourth argument for the invoked method.</param>
public object? Invoke(object? obj, object? arg1, object? arg2, object? arg3, object? arg4)
{
if (_argCount != 4)
{
MethodBaseInvoker.ThrowTargetParameterCountException();
}
return InvokeImpl(obj, arg1, arg2, arg3, arg4);
}
private object? InvokeImpl(object? obj, object? arg1, object? arg2, object? arg3, object? arg4)
{
if ((_invocationFlags & (InvocationFlags.NoInvoke | InvocationFlags.ContainsStackPointers | InvocationFlags.NoConstructorInvoke)) != 0)
{
ThrowForBadInvocationFlags();
}
if (!_isStatic)
{
ValidateInvokeTarget(obj, _method);
}
switch (_argCount)
{
case 4:
CheckArgument(ref arg4, 3);
goto case 3;
case 3:
CheckArgument(ref arg3, 2);
goto case 2;
case 2:
CheckArgument(ref arg2, 1);
goto case 1;
case 1:
CheckArgument(ref arg1, 0);
break;
}
// Check fast path first.
if (_invokeFunc_Obj4Args is not null)
{
return _invokeFunc_Obj4Args(obj, arg1, arg2, arg3, arg4);
}
if ((_strategy & InvokerStrategy.StrategyDetermined_Obj4Args) == 0)
{
DetermineStrategy_Obj4Args(ref _strategy, ref _invokeFunc_Obj4Args, _method, _needsByRefStrategy, backwardsCompat: false);
if (_invokeFunc_Obj4Args is not null)
{
return _invokeFunc_Obj4Args(obj, arg1, arg2, arg3, arg4);
}
}
return InvokeDirectByRef(obj, arg1, arg2, arg3, arg4);
}
/// <inheritdoc cref="Invoke(object?)"/>
/// <param name="obj"> The object on which to invoke the method. If the method is static, this argument is ignored. </param>
/// <param name="arguments">The arguments for the invoked method.</param>
/// <exception cref="ArgumentException">
/// The arguments do not match the signature of the invoked method.
/// </exception>
public object? Invoke(object? obj, Span<object?> arguments)
{
int argLen = arguments.Length;
if (argLen != _argCount)
{
MethodBaseInvoker.ThrowTargetParameterCountException();
}
if (!_needsByRefStrategy)
{
// Switch to fast path if possible.
switch (_argCount)
{
case 0:
return InvokeImpl(obj, null, null, null, null);
case 1:
return InvokeImpl(obj, arguments[0], null, null, null);
case 2:
return InvokeImpl(obj, arguments[0], arguments[1], null, null);
case 3:
return InvokeImpl(obj, arguments[0], arguments[1], arguments[2], null);
case 4:
return InvokeImpl(obj, arguments[0], arguments[1], arguments[2], arguments[3]);
default:
break;
}
}
if ((_invocationFlags & (InvocationFlags.NoInvoke | InvocationFlags.ContainsStackPointers)) != 0)
{
ThrowForBadInvocationFlags();
}
if (!_isStatic)
{
ValidateInvokeTarget(obj, _method);
}
if (argLen > MaxStackAllocArgCount)
{
return InvokeWithManyArgs(obj, arguments);
}
return InvokeWithFewArgs(obj, arguments);
}
private void ThrowForBadInvocationFlags()
{
if (_method is RuntimeMethodInfo rmi)
{
rmi.ThrowNoInvokeException();
}
Debug.Assert(_method is RuntimeConstructorInfo);
((RuntimeConstructorInfo)_method).ThrowNoInvokeException();
}
internal object? InvokeWithFewArgs(object? obj, Span<object?> arguments)
{
Debug.Assert(_argCount <= MaxStackAllocArgCount);
StackAllocatedArgumentsWithCopyBack stackArgStorage = default;
Span<object?> copyOfArgs = stackArgStorage._args.AsSpan(_argCount);
scoped Span<bool> shouldCopyBack = stackArgStorage._shouldCopyBack.AsSpan(_argCount);
for (int i = 0; i < _argCount; i++)
{
object? arg = arguments[i];
shouldCopyBack[i] = CheckArgument(ref arg, i);
copyOfArgs[i] = arg;
}
// Check fast path first.
if (_invokeFunc_ObjSpanArgs is not null)
{
return _invokeFunc_ObjSpanArgs(obj, copyOfArgs);
// No need to call CopyBack here since there are no ref values.
}
if ((_strategy & InvokerStrategy.StrategyDetermined_ObjSpanArgs) == 0)
{
DetermineStrategy_ObjSpanArgs(ref _strategy, ref _invokeFunc_ObjSpanArgs, _method, _needsByRefStrategy, backwardsCompat: false);
if (_invokeFunc_ObjSpanArgs is not null)
{
return _invokeFunc_ObjSpanArgs(obj, copyOfArgs);
}
}
object? ret = InvokeDirectByRefWithFewArgs(obj, copyOfArgs);
CopyBack(arguments, copyOfArgs, shouldCopyBack);
return ret;
}
internal object? InvokeDirectByRef(object? obj, object? arg1 = null, object? arg2 = null, object? arg3 = null, object? arg4 = null)
{
StackAllocatedArguments stackStorage = new(arg1, arg2, arg3, arg4);
return InvokeDirectByRefWithFewArgs(obj, stackStorage._args.AsSpan(_argCount));
}
internal unsafe object? InvokeDirectByRefWithFewArgs(object? obj, Span<object?> copyOfArgs)
{
if ((_strategy & InvokerStrategy.StrategyDetermined_RefArgs) == 0)
{
DetermineStrategy_RefArgs(ref _strategy, ref _invokeFunc_RefArgs, _method, backwardsCompat: false);
}
StackAllocatedByRefs byrefs = default;
#pragma warning disable CS8500
IntPtr* pByRefFixedStorage = (IntPtr*)&byrefs;
#pragma warning restore CS8500
for (int i = 0; i < _argCount; i++)
{
#pragma warning disable CS8500
*(ByReference*)(pByRefFixedStorage + i) = (_invokerArgFlags[i] & InvokerArgFlags.IsValueType) != 0 ?
#pragma warning restore CS8500
ByReference.Create(ref copyOfArgs[i]!.GetRawData()) :
ByReference.Create(ref copyOfArgs[i]);
}
return _invokeFunc_RefArgs!(obj, pByRefFixedStorage);
}
internal unsafe object? InvokeWithManyArgs(object? obj, Span<object?> arguments)
{
Span<object?> copyOfArgs;
GCFrameRegistration regArgStorage;
object? ret;
if ((_strategy & InvokerStrategy.StrategyDetermined_ObjSpanArgs) == 0)
{
DetermineStrategy_ObjSpanArgs(ref _strategy, ref _invokeFunc_ObjSpanArgs, _method, _needsByRefStrategy, backwardsCompat: false);
}
if (_invokeFunc_ObjSpanArgs is not null)
{
IntPtr* pArgStorage = stackalloc IntPtr[_argCount];
NativeMemory.Clear(pArgStorage, (nuint)_argCount * (nuint)sizeof(IntPtr));
copyOfArgs = new(ref Unsafe.AsRef<object?>(pArgStorage), _argCount);
regArgStorage = new((void**)pArgStorage, (uint)_argCount, areByRefs: false);
try
{
GCFrameRegistration.RegisterForGCReporting(®ArgStorage);
for (int i = 0; i < _argCount; i++)
{
object? arg = arguments[i];
CheckArgument(ref arg, i);
copyOfArgs[i] = arg;
}
ret = _invokeFunc_ObjSpanArgs(obj, copyOfArgs);
// No need to call CopyBack here since there are no ref values.
}
finally
{
GCFrameRegistration.UnregisterForGCReporting(®ArgStorage);
}
}
else
{
if ((_strategy & InvokerStrategy.StrategyDetermined_RefArgs) == 0)
{
DetermineStrategy_RefArgs(ref _strategy, ref _invokeFunc_RefArgs, _method, backwardsCompat: false);
}
IntPtr* pStorage = stackalloc IntPtr[2 * _argCount];
NativeMemory.Clear(pStorage, (nuint)(2 * _argCount) * (nuint)sizeof(IntPtr));
copyOfArgs = new(ref Unsafe.AsRef<object?>(pStorage), _argCount);
IntPtr* pByRefStorage = pStorage + _argCount;
scoped Span<bool> shouldCopyBack = stackalloc bool[_argCount];
regArgStorage = new((void**)pStorage, (uint)_argCount, areByRefs: false);
GCFrameRegistration regByRefStorage = new((void**)pByRefStorage, (uint)_argCount, areByRefs: true);
try
{
GCFrameRegistration.RegisterForGCReporting(®ArgStorage);
GCFrameRegistration.RegisterForGCReporting(®ByRefStorage);
for (int i = 0; i < _argCount; i++)
{
object? arg = arguments[i];
shouldCopyBack[i] = CheckArgument(ref arg, i);
copyOfArgs[i] = arg;
#pragma warning disable CS8500
*(ByReference*)(pByRefStorage + i) = (_invokerArgFlags[i] & InvokerArgFlags.IsValueType) != 0 ?
#pragma warning restore CS8500
ByReference.Create(ref Unsafe.AsRef<object>(pStorage + i).GetRawData()) :
ByReference.Create(ref Unsafe.AsRef<object>(pStorage + i));
}
ret = _invokeFunc_RefArgs!(obj, pByRefStorage);
CopyBack(arguments, copyOfArgs, shouldCopyBack);
}
finally
{
GCFrameRegistration.UnregisterForGCReporting(®ByRefStorage);
GCFrameRegistration.UnregisterForGCReporting(®ArgStorage);
}
}
return ret;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
// Copy modified values out. This is only done with ByRef parameters.
internal void CopyBack(Span<object?> dest, Span<object?> copyOfParameters, Span<bool> shouldCopyBack)
{
for (int i = 0; i < dest.Length; i++)
{
if (shouldCopyBack[i])
{
if ((_invokerArgFlags[i] & InvokerArgFlags.IsNullableOfT) != 0)
{
Debug.Assert(copyOfParameters[i] != null);
Debug.Assert(((RuntimeType)copyOfParameters[i]!.GetType()).IsNullableOfT);
dest![i] = RuntimeMethodHandle.ReboxFromNullable(copyOfParameters[i]);
}
else
{
dest![i] = copyOfParameters[i];
}
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool CheckArgument(ref object? arg, int i)
{
RuntimeType sigType = _argTypes[i];
// Convert the type if necessary.
// Note that Type.Missing is not supported.
if (arg is null)
{
if ((_invokerArgFlags[i] & InvokerArgFlags.IsValueType_ByRef_Or_Pointer) != 0)
{
return sigType.CheckValue(ref arg);
}
}
else if (!ReferenceEquals(arg.GetType(), sigType))
{
return sigType.CheckValue(ref arg);
}
return false;
}
}
}