This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/
NativeCallableTest.cs
306 lines (256 loc) · 10.1 KB
/
NativeCallableTest.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Threading;
using TestLibrary;
using Console = Internal.Console;
public class Program
{
public static class NativeMethods
{
[DllImport("NativeCallableDll")]
public static extern int CallManagedProc(IntPtr callbackProc, int n);
}
private delegate int IntNativeMethodInvoker();
private delegate void NativeMethodInvoker();
public static int Main(string[] args)
{
try
{
TestNativeCallableValid();
NegativeTest_ViaDelegate();
NegativeTest_NonBlittable();
NegativeTest_GenericArguments();
NativeCallableViaUnmanagedCalli();
if (args.Length != 0 && args[0].Equals("calli"))
{
NegativeTest_ViaCalli();
}
}
catch (Exception e)
{
Console.WriteLine($"Test Failure: {e}");
return 101;
}
return 100;
}
[NativeCallable]
public static int ManagedDoubleCallback(int n)
{
return DoubleImpl(n);
}
private static int DoubleImpl(int n)
{
return 2 * n;
}
public static void TestNativeCallableValid()
{
Console.WriteLine($"{nameof(NativeCallableAttribute)} function");
/*
void TestNativeCallable()
{
.locals init ([0] native int ptr)
IL_0000: nop
IL_0001: ldftn int32 ManagedDoubleCallback(int32)
IL_0007: stloc.0
IL_0008: ldloc.0
IL_0009: ldc.i4 <n> local
IL_000e: call bool NativeMethods::CallManagedProc(native int, int)
IL_0013: ret
}
*/
DynamicMethod testNativeCallable = new DynamicMethod("TestNativeCallable", typeof(int), null, typeof(Program).Module);
ILGenerator il = testNativeCallable.GetILGenerator();
il.DeclareLocal(typeof(IntPtr));
il.Emit(OpCodes.Nop);
// Get native function pointer of the callback
il.Emit(OpCodes.Ldftn, typeof(Program).GetMethod(nameof(ManagedDoubleCallback)));
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloc_0);
int n = 12345;
il.Emit(OpCodes.Ldc_I4, n);
il.Emit(OpCodes.Call, typeof(NativeMethods).GetMethod("CallManagedProc"));
il.Emit(OpCodes.Ret);
var testNativeMethod = (IntNativeMethodInvoker)testNativeCallable.CreateDelegate(typeof(IntNativeMethodInvoker));
int expected = DoubleImpl(n);
Assert.AreEqual(expected, testNativeMethod());
}
public static void NegativeTest_ViaDelegate()
{
Console.WriteLine($"{nameof(NativeCallableAttribute)} function as delegate");
// Try invoking method directly
try
{
CallAsDelegate();
Assert.Fail($"Invalid to call {nameof(ManagedDoubleCallback)} as delegate");
}
catch (NotSupportedException)
{
}
// Local function to delay exception thrown during JIT
void CallAsDelegate()
{
Func<int, int> invoker = ManagedDoubleCallback;
invoker(0);
}
}
[NativeCallable]
public static int CallbackMethodNonBlittable(bool x1)
{
Assert.Fail($"Functions with attribute {nameof(NativeCallableAttribute)} cannot have non-blittable arguments");
return -1;
}
public static void NegativeTest_NonBlittable()
{
Console.WriteLine($"{nameof(NativeCallableAttribute)} function with non-blittable arguments");
/*
void TestNativeCallableNonBlittable()
{
.locals init ([0] native int ptr)
IL_0000: nop
IL_0001: ldftn int32 CallbackMethodNonBlittable(bool)
IL_0007: stloc.0
IL_0008: ret
}
*/
DynamicMethod testNativeCallable = new DynamicMethod("TestNativeCallableNonBlittable", null, null, typeof(Program).Module);
ILGenerator il = testNativeCallable.GetILGenerator();
il.DeclareLocal(typeof(IntPtr));
il.Emit(OpCodes.Nop);
// Get native function pointer of the callback
il.Emit(OpCodes.Ldftn, typeof(Program).GetMethod(nameof(CallbackMethodNonBlittable)));
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ret);
var testNativeMethod = (NativeMethodInvoker)testNativeCallable.CreateDelegate(typeof(NativeMethodInvoker));
// Try invoking method
try
{
testNativeMethod();
Assert.Fail($"Function {nameof(CallbackMethodNonBlittable)} has non-blittable types");
}
catch (NotSupportedException)
{
}
}
[NativeCallable]
public static int CallbackMethodGeneric<T>(T arg)
{
Assert.Fail($"Functions with attribute {nameof(NativeCallableAttribute)} cannot have generic arguments");
return -1;
}
public static void NegativeTest_GenericArguments()
{
/*
void TestNativeCallableGenericArguments()
{
.locals init ([0] native int ptr)
IL_0000: nop
IL_0001: ldftn int32 CallbackMethodGeneric(T)
IL_0007: stloc.0
IL_0008: ret
}
*/
DynamicMethod testNativeCallable = new DynamicMethod("TestNativeCallableGenericArguments", null, null, typeof(Program).Module);
ILGenerator il = testNativeCallable.GetILGenerator();
il.DeclareLocal(typeof(IntPtr));
il.Emit(OpCodes.Nop);
// Get native function pointer of the callback
il.Emit(OpCodes.Ldftn, typeof(Program).GetMethod(nameof(CallbackMethodGeneric)));
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ret);
var testNativeMethod = (NativeMethodInvoker)testNativeCallable.CreateDelegate(typeof(NativeMethodInvoker));
// Try invoking method
try
{
testNativeMethod();
Assert.Fail($"Function {nameof(CallbackMethodGeneric)} has generic types");
}
catch (InvalidProgramException)
{
}
}
[NativeCallable]
public static void CallbackViaCalli(int val)
{
Assert.Fail($"Functions with attribute {nameof(NativeCallableAttribute)} cannot be called via calli");
}
public static void NegativeTest_ViaCalli()
{
Console.WriteLine($"{nameof(NativeCallableAttribute)} function via calli instruction. The CLR _will_ crash.");
/*
void TestNativeCallableViaCalli()
{
.locals init (native int V_0)
IL_0000: nop
IL_0001: ldftn void CallbackViaCalli(int32)
IL_0007: stloc.0
IL_0008: ldc.i4 1234
IL_000d: ldloc.0
IL_000e: calli void(int32)
IL_0013: nop
IL_0014: ret
}
*/
DynamicMethod testNativeCallable = new DynamicMethod("TestNativeCallableViaCalli", null, null, typeof(Program).Module);
ILGenerator il = testNativeCallable.GetILGenerator();
il.DeclareLocal(typeof(IntPtr));
il.Emit(OpCodes.Nop);
// Get native function pointer of the callback
il.Emit(OpCodes.Ldftn, typeof(Program).GetMethod(nameof(CallbackViaCalli)));
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldc_I4, 1234);
il.Emit(OpCodes.Ldloc_0);
il.EmitCalli(OpCodes.Calli, CallingConventions.Standard, null, new Type[] { typeof(int) }, null);
il.Emit(OpCodes.Nop);
il.Emit(OpCodes.Ret);
NativeMethodInvoker testNativeMethod = (NativeMethodInvoker)testNativeCallable.CreateDelegate(typeof(NativeMethodInvoker));
// It is not possible to catch the resulting ExecutionEngineException exception.
// To observe the crashing behavior set a breakpoint in the ReversePInvokeBadTransition() function
// located in src/vm/dllimportcallback.cpp.
testNativeMethod();
}
[NativeCallable(CallingConvention = CallingConvention.StdCall)]
public static int CallbackViaUnmanagedCalli(int val)
{
return DoubleImpl(val);
}
public static void NativeCallableViaUnmanagedCalli()
{
Console.WriteLine($"{nameof(NativeCallableAttribute)} function via calli instruction with unmanaged calling convention.");
/*
void TestNativeCallableViaCalli()
{
.locals init (native int V_0)
IL_0000: nop
IL_0001: ldftn int CallbackViaUnmanagedCalli(int32)
IL_0007: stloc.0
IL_0008: ldc.i4 1234
IL_000d: ldloc.0
IL_000e: calli int32 stdcall(int32)
IL_0014: ret
}
*/
DynamicMethod testNativeCallable = new DynamicMethod("TestNativeCallableViaUnmanagedCalli", typeof(int), null, typeof(Program).Module);
ILGenerator il = testNativeCallable.GetILGenerator();
il.DeclareLocal(typeof(IntPtr));
il.Emit(OpCodes.Nop);
// Get native function pointer of the callback
il.Emit(OpCodes.Ldftn, typeof(Program).GetMethod(nameof(CallbackViaUnmanagedCalli)));
il.Emit(OpCodes.Stloc_0);
int n = 1234;
il.Emit(OpCodes.Ldc_I4, n);
il.Emit(OpCodes.Ldloc_0);
il.EmitCalli(OpCodes.Calli, CallingConvention.StdCall, typeof(int), new Type[] { typeof(int) });
il.Emit(OpCodes.Ret);
IntNativeMethodInvoker testNativeMethod = (IntNativeMethodInvoker)testNativeCallable.CreateDelegate(typeof(IntNativeMethodInvoker));
int expected = DoubleImpl(n);
Assert.AreEqual(expected, testNativeMethod());
}
}