Welcome to mirror list, hosted at ThFree Co, Russian Federation.

InteropHelpers.cs « CompilerHelpers « Runtime « Internal « src « System.Private.CoreLib « nativeaot « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f5367d848d4eda7e65d375f3e0edf86a5b14eac1 (plain)
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ObjectiveC;
using System.Runtime.Loader;
using System.Text;
using System.Threading;

using Internal.Runtime;
using Internal.Runtime.Augments;

namespace Internal.Runtime.CompilerHelpers
{
    /// <summary>
    /// These methods are used to throw exceptions from generated code.
    /// </summary>
    internal static class InteropHelpers
    {
        internal static unsafe byte* StringToAnsiString(string str, bool bestFit, bool throwOnUnmappableChar)
        {
            return PInvokeMarshal.StringToAnsiString(str, bestFit, throwOnUnmappableChar);
        }

        public static unsafe string AnsiStringToString(byte* buffer)
        {
            return PInvokeMarshal.AnsiStringToString(buffer);
        }

        internal static unsafe void StringToByValAnsiString(string str, byte* pNative, int charCount, bool bestFit, bool throwOnUnmappableChar)
        {
            if (str != null)
            {
                // Truncate the string if it is larger than specified by SizeConst
                int lenUnicode = str.Length;
                if (lenUnicode >= charCount)
                    lenUnicode = charCount - 1;

                fixed (char* pManaged = str)
                {
                    PInvokeMarshal.StringToAnsiString(pManaged, lenUnicode, pNative, /*terminateWithNull=*/true, bestFit, throwOnUnmappableChar);
                }
            }
            else
            {
                (*pNative) = (byte)'\0';
            }
        }

        public static unsafe string ByValAnsiStringToString(byte* buffer, int length)
        {
            int end = new ReadOnlySpan<byte>(buffer, length).IndexOf((byte)0);
            if (end >= 0)
            {
                length = end;
            }

            return new string((sbyte*)buffer, 0, length);
        }

        internal static unsafe void StringToUnicodeFixedArray(string str, ushort* buffer, int length)
        {
            ReadOnlySpan<char> managed = str;
            Span<char> native = new Span<char>((char*)buffer, length);

            int numChars = Math.Min(managed.Length, length - 1);

            managed.Slice(0, numChars).CopyTo(native);
            native[numChars] = '\0';
        }

        internal static unsafe string UnicodeToStringFixedArray(ushort* buffer, int length)
        {
            int end = new ReadOnlySpan<char>(buffer, length).IndexOf('\0');
            if (end >= 0)
            {
                length = end;
            }

            return new string((char*)buffer, 0, length);
        }

        internal static unsafe char* StringToUnicodeBuffer(string str)
        {
            return (char*)Marshal.StringToCoTaskMemUni(str);
        }

        public static unsafe byte* AllocMemoryForAnsiStringBuilder(StringBuilder sb)
        {
            if (sb == null)
            {
                return null;
            }
            return (byte *)CoTaskMemAllocAndZeroMemory(checked((sb.Capacity + 2) * Marshal.SystemMaxDBCSCharSize));
        }

        public static unsafe char* AllocMemoryForUnicodeStringBuilder(StringBuilder sb)
        {
            if (sb == null)
            {
                return null;
            }
            return (char *)CoTaskMemAllocAndZeroMemory(checked((sb.Capacity + 2) * 2));
        }

        public static unsafe byte* AllocMemoryForAnsiCharArray(char[] chArray)
        {
            if (chArray == null)
            {
                return null;
            }
            return (byte*)CoTaskMemAllocAndZeroMemory(checked((chArray.Length + 2) * Marshal.SystemMaxDBCSCharSize));
        }

        public static unsafe void AnsiStringToStringBuilder(byte* newBuffer, System.Text.StringBuilder stringBuilder)
        {
            if (stringBuilder == null)
                return;

            PInvokeMarshal.AnsiStringToStringBuilder(newBuffer, stringBuilder);
        }

        public static unsafe void UnicodeStringToStringBuilder(ushort* newBuffer, System.Text.StringBuilder stringBuilder)
        {
            if (stringBuilder == null)
                return;

            PInvokeMarshal.UnicodeStringToStringBuilder(newBuffer, stringBuilder);
        }

        public static unsafe void StringBuilderToAnsiString(System.Text.StringBuilder stringBuilder, byte* pNative,
            bool bestFit, bool throwOnUnmappableChar)
        {
            if (pNative == null)
                return;

            PInvokeMarshal.StringBuilderToAnsiString(stringBuilder, pNative, bestFit, throwOnUnmappableChar);
        }

        public static unsafe void StringBuilderToUnicodeString(System.Text.StringBuilder stringBuilder, ushort* destination)
        {
            if (destination == null)
                return;

            PInvokeMarshal.StringBuilderToUnicodeString(stringBuilder, destination);
        }

        public static unsafe void WideCharArrayToAnsiCharArray(char[] managedArray, byte* pNative, bool bestFit, bool throwOnUnmappableChar)
        {
            PInvokeMarshal.WideCharArrayToAnsiCharArray(managedArray, pNative, bestFit, throwOnUnmappableChar);
        }

        /// <summary>
        /// Convert ANSI ByVal byte array to UNICODE wide char array, best fit
        /// </summary>
        /// <remarks>
        /// * This version works with array instead to string, it means that the len must be provided and there will be NO NULL to
        /// terminate the array.
        /// * The buffer to the UNICODE wide char array must be allocated by the caller.
        /// </remarks>
        /// <param name="pNative">Pointer to the ANSI byte array. Could NOT be null.</param>
        /// <param name="managedArray">Wide char array that has already been allocated.</param>
        public static unsafe void AnsiCharArrayToWideCharArray(byte* pNative, char[] managedArray)
        {
            PInvokeMarshal.AnsiCharArrayToWideCharArray(pNative, managedArray);
        }

        /// <summary>
        /// Convert a single UNICODE wide char to a single ANSI byte.
        /// </summary>
        /// <param name="managedValue">single UNICODE wide char value</param>
        /// <param name="bestFit">Enable best-fit mapping behavior</param>
        /// <param name="throwOnUnmappableChar">Throw an exception on an unmappable Unicode character</param>
        public static unsafe byte WideCharToAnsiChar(char managedValue, bool bestFit, bool throwOnUnmappableChar)
        {
            return PInvokeMarshal.WideCharToAnsiChar(managedValue, bestFit, throwOnUnmappableChar);
        }

        /// <summary>
        /// Convert a single ANSI byte value to a single UNICODE wide char value, best fit.
        /// </summary>
        /// <param name="nativeValue">Single ANSI byte value.</param>
        public static unsafe char AnsiCharToWideChar(byte nativeValue)
        {
            return PInvokeMarshal.AnsiCharToWideChar(nativeValue);
        }

        internal static double DateTimeToOleDateTime(DateTime value)
        {
            return value.ToOADate();
        }

        internal static DateTime OleDateTimeToDateTime(double value)
        {
            return DateTime.FromOADate(value);
        }

        internal static long DecimalToOleCurrency(decimal value)
        {
            return decimal.ToOACurrency(value);
        }

        internal static decimal OleCurrencyToDecimal(long value)
        {
            return decimal.FromOACurrency(value);
        }

        internal static unsafe string BstrBufferToString(char* buffer)
        {
            if (buffer == null)
                return null;

            return Marshal.PtrToStringBSTR((IntPtr)buffer);
        }

        internal static unsafe byte* StringToAnsiBstrBuffer(string s)
        {
            if (s is null)
            {
                return (byte*)IntPtr.Zero;
            }

            int stringLength = s.Length;
            fixed (char* pStr = s)
            {
                int nativeLength = PInvokeMarshal.GetByteCount(pStr, stringLength);
                byte* bstr = (byte*)Marshal.AllocBSTRByteLen((uint)nativeLength);
                PInvokeMarshal.ConvertWideCharToMultiByte(pStr, stringLength, bstr, nativeLength, bestFit: false, throwOnUnmappableChar: false);
                return bstr;
            }
        }

        internal static unsafe string AnsiBstrBufferToString(byte* buffer)
        {
            if (buffer == null)
                return null;

            return Marshal.PtrToStringAnsi((IntPtr)buffer, (int)Marshal.SysStringByteLen((IntPtr)buffer));
        }

        internal static unsafe IntPtr ResolvePInvoke(MethodFixupCell* pCell)
        {
            if (pCell->Target != IntPtr.Zero)
                return pCell->Target;

            return ResolvePInvokeSlow(pCell);
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        internal static unsafe IntPtr ResolvePInvokeSlow(MethodFixupCell* pCell)
        {
            int lastSystemError = Marshal.GetLastSystemError();

            ModuleFixupCell* pModuleCell = pCell->Module;
            IntPtr hModule = pModuleCell->Handle;
            if (hModule == IntPtr.Zero)
            {
                FixupModuleCell(pModuleCell);
                hModule = pModuleCell->Handle;
            }

            FixupMethodCell(hModule, pCell);

            Marshal.SetLastSystemError(lastSystemError);

            return pCell->Target;
        }

        internal static unsafe void FreeLibrary(IntPtr hModule)
        {
#if !TARGET_UNIX
            Interop.Kernel32.FreeLibrary(hModule);
#else
            Interop.Sys.FreeLibrary(hModule);
#endif
        }

        private static unsafe string GetModuleName(ModuleFixupCell* pCell)
        {
            byte* pModuleName = (byte*)pCell->ModuleName;
            return Encoding.UTF8.GetString(pModuleName, string.strlen(pModuleName));
        }

        internal static unsafe void FixupModuleCell(ModuleFixupCell* pCell)
        {
            string moduleName = GetModuleName(pCell);

            uint dllImportSearchPath = 0;
            bool hasDllImportSearchPath = (pCell->DllImportSearchPathAndCookie & InteropDataConstants.HasDllImportSearchPath) != 0;
            if (hasDllImportSearchPath)
            {
                dllImportSearchPath = pCell->DllImportSearchPathAndCookie & ~InteropDataConstants.HasDllImportSearchPath;
            }

            Assembly callingAssembly = RuntimeAugments.Callbacks.GetAssemblyForHandle(new RuntimeTypeHandle(pCell->CallingAssemblyType));

            // First check if there's a NativeLibrary callback and call it to attempt the resolution
            IntPtr hModule = NativeLibrary.LoadLibraryCallbackStub(moduleName, callingAssembly, hasDllImportSearchPath, dllImportSearchPath);
            if (hModule == IntPtr.Zero)
            {
                // NativeLibrary callback didn't resolve the library. Use built-in rules.
                NativeLibrary.LoadLibErrorTracker loadLibErrorTracker = default;

                hModule = NativeLibrary.LoadBySearch(
                    callingAssembly,
                    searchAssemblyDirectory: false,
                    dllImportSearchPathFlags: 0,
                    ref loadLibErrorTracker,
                    moduleName);

                if (hModule == IntPtr.Zero)
                {
                    // Built-in rules didn't resolve the library. Use AssemblyLoadContext as a last chance attempt.
                    AssemblyLoadContext loadContext = AssemblyLoadContext.GetLoadContext(callingAssembly)!;
                    hModule = loadContext.GetResolvedUnmanagedDll(callingAssembly, moduleName);
                }

                if (hModule == IntPtr.Zero)
                {
                    // If the module is still unresolved, this is an error.
                    loadLibErrorTracker.Throw(moduleName);
                }
            }

            Debug.Assert(hModule != IntPtr.Zero);
            var oldValue = Interlocked.CompareExchange(ref pCell->Handle, hModule, IntPtr.Zero);
            if (oldValue != IntPtr.Zero)
            {
                // Some other thread won the race to fix it up.
                FreeLibrary(hModule);
            }
        }

        internal static unsafe void FixupMethodCell(IntPtr hModule, MethodFixupCell* pCell)
        {
            byte* methodName = (byte*)pCell->MethodName;
            IntPtr pTarget;

#if FEATURE_OBJCMARSHAL
#pragma warning disable CA1416
            if (pCell->IsObjectiveCMessageSend && ObjectiveCMarshal.TryGetGlobalMessageSendCallback(pCell->ObjectiveCMessageSendFunction, out pTarget))
            {
                Debug.Assert(pTarget != IntPtr.Zero);
                pCell->Target = pTarget;
                return;
            }
#pragma warning restore CA1416
#endif

#if TARGET_WINDOWS
            CharSet charSetMangling = pCell->CharSetMangling;
            if (charSetMangling == 0)
            {
                // Look for the user-provided entry point name only
                pTarget = Interop.Kernel32.GetProcAddress(hModule, methodName);
            }
            else
            if (charSetMangling == CharSet.Ansi)
            {
                // For ANSI, look for the user-provided entry point name first.
                // If that does not exist, try the charset suffix.
                pTarget = Interop.Kernel32.GetProcAddress(hModule, methodName);
                if (pTarget == IntPtr.Zero)
                    pTarget = GetProcAddressWithSuffix(hModule, methodName, (byte)'A');
            }
            else
            {
                // For Unicode, look for the entry point name with the charset suffix first.
                // The 'W' API takes precedence over the undecorated one.
                pTarget = GetProcAddressWithSuffix(hModule, methodName, (byte)'W');
                if (pTarget == IntPtr.Zero)
                    pTarget = Interop.Kernel32.GetProcAddress(hModule, methodName);
            }
#else
            pTarget = Interop.Sys.GetProcAddress(hModule, methodName);
#endif
            if (pTarget == IntPtr.Zero)
            {
                string entryPointName = Encoding.UTF8.GetString(methodName, string.strlen(methodName));
                throw new EntryPointNotFoundException(SR.Format(SR.Arg_EntryPointNotFoundExceptionParameterized, entryPointName, GetModuleName(pCell->Module)));
            }

            pCell->Target = pTarget;
        }

#if TARGET_WINDOWS
        private static unsafe IntPtr GetProcAddressWithSuffix(IntPtr hModule, byte* methodName, byte suffix)
        {
            int nameLength = string.strlen(methodName);

            // We need to add an extra byte for the suffix, and an extra byte for the null terminator
            byte* probedMethodName = stackalloc byte[nameLength + 2];

            for (int i = 0; i < nameLength; i++)
            {
                probedMethodName[i] = methodName[i];
            }

            probedMethodName[nameLength + 1] = 0;

            probedMethodName[nameLength] = suffix;

            return Interop.Kernel32.GetProcAddress(hModule, probedMethodName);
        }
#endif

        internal static unsafe void* CoTaskMemAllocAndZeroMemory(int size)
        {
            byte* ptr = (byte*)Marshal.AllocCoTaskMem(size);

            // Marshal.AllocCoTaskMem will throw OOMException if out of memory
            Debug.Assert(ptr != null);

            NativeMemory.Clear(ptr, (uint)size);
            return ptr;
        }

        /// <summary>
        /// Retrieves the function pointer for the current open static delegate that is being called
        /// </summary>
        public static IntPtr GetCurrentCalleeOpenStaticDelegateFunctionPointer()
        {
            return PInvokeMarshal.GetCurrentCalleeOpenStaticDelegateFunctionPointer();
        }

        /// <summary>
        /// Retrieves the current delegate that is being called
        /// </summary>
        public static T GetCurrentCalleeDelegate<T>() where T : class // constraint can't be System.Delegate
        {
            return PInvokeMarshal.GetCurrentCalleeDelegate<T>();
        }

        public static IntPtr ConvertManagedComInterfaceToNative(object pUnk, Guid interfaceGuid)
        {
            if (pUnk == null)
            {
                return IntPtr.Zero;
            }

#if TARGET_WINDOWS
#pragma warning disable CA1416
            return ComWrappers.ComInterfaceForObject(pUnk, interfaceGuid);
#pragma warning restore CA1416
#else
            throw new PlatformNotSupportedException(SR.PlatformNotSupported_ComInterop);
#endif
        }

        public static IntPtr ConvertManagedComInterfaceToIUnknown(object pUnk)
        {
            if (pUnk == null)
            {
                return IntPtr.Zero;
            }

#if TARGET_WINDOWS
#pragma warning disable CA1416
            return ComWrappers.ComInterfaceForObject(pUnk);
#pragma warning restore CA1416
#else
            throw new PlatformNotSupportedException(SR.PlatformNotSupported_ComInterop);
#endif
        }

        public static object ConvertNativeComInterfaceToManaged(IntPtr pUnk)
        {
            if (pUnk == IntPtr.Zero)
            {
                return null;
            }

#if TARGET_WINDOWS
#pragma warning disable CA1416
            return ComWrappers.ComObjectForInterface(pUnk);
#pragma warning restore CA1416
#else
            throw new PlatformNotSupportedException(SR.PlatformNotSupported_ComInterop);
#endif
        }

        [UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
            Justification = "This API will be called from compiler generated code only.")]
        internal static int AsAnyGetNativeSize(object o)
        {
            // Array, string and StringBuilder are not implemented.
            if (o.GetEETypePtr().IsArray ||
                o is string ||
                o is StringBuilder)
            {
                throw new PlatformNotSupportedException();
            }

            // Assume that this is a type with layout.
            return Marshal.SizeOf(o.GetType());
        }

        [UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
            Justification = "This API will be called from compiler generated code only.")]
        internal static void AsAnyMarshalManagedToNative(object o, IntPtr address)
        {
            // Array, string and StringBuilder are not implemented.
            if (o.GetEETypePtr().IsArray ||
                o is string ||
                o is StringBuilder)
            {
                throw new PlatformNotSupportedException();
            }

            Marshal.StructureToPtr(o, address, fDeleteOld: false);
        }

        internal static void AsAnyMarshalNativeToManaged(IntPtr address, object o)
        {
            // Array, string and StringBuilder are not implemented.
            if (o.GetEETypePtr().IsArray ||
                o is string ||
                o is StringBuilder)
            {
                throw new PlatformNotSupportedException();
            }

            Marshal.PtrToStructureImpl(address, o);
        }

        [UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
            Justification = "This API will be called from compiler generated code only.")]
        internal static void AsAnyCleanupNative(IntPtr address, object o)
        {
            // Array, string and StringBuilder are not implemented.
            if (o.GetEETypePtr().IsArray ||
                o is string ||
                o is StringBuilder)
            {
                throw new PlatformNotSupportedException();
            }

            Marshal.DestroyStructure(address, o.GetType());
        }

        internal static unsafe object? VariantToObject(IntPtr pSrcNativeVariant)
        {
            if (pSrcNativeVariant == IntPtr.Zero)
            {
                return null;
            }

#if TARGET_WINDOWS
#pragma warning disable CA1416
            return Marshal.GetObjectForNativeVariant(pSrcNativeVariant);
#pragma warning restore CA1416
#else
            throw new PlatformNotSupportedException(SR.PlatformNotSupported_ComInterop);
#endif
        }

        internal static unsafe void ConvertObjectToVariant(object? obj, IntPtr pDstNativeVariant)
        {
#if TARGET_WINDOWS
#pragma warning disable CA1416
            Marshal.GetNativeVariantForObject(obj, pDstNativeVariant);
#pragma warning restore CA1416
#else
            throw new PlatformNotSupportedException(SR.PlatformNotSupported_ComInterop);
#endif
        }

        internal static unsafe void CleanupVariant(IntPtr pDstNativeVariant)
        {
#if TARGET_WINDOWS
#pragma warning disable CA1416
            Variant* data = (Variant*)pDstNativeVariant;
            data->Clear();
#pragma warning restore CA1416
#else
            throw new PlatformNotSupportedException(SR.PlatformNotSupported_ComInterop);
#endif
        }

        public static unsafe object InitializeCustomMarshaller(RuntimeTypeHandle pParameterType, RuntimeTypeHandle pMarshallerType, string cookie, delegate*<string, object> getInstanceMethod)
        {
            if (getInstanceMethod == null)
            {
                throw new ApplicationException();
            }

            if (!RuntimeImports.AreTypesAssignable(pMarshallerType.ToEETypePtr(), EETypePtr.EETypePtrOf<ICustomMarshaler>()))
            {
                throw new ApplicationException();
            }

            var marshaller = CustomMarshallerTable.s_customMarshallersTable.GetOrAdd(new CustomMarshallerKey(pParameterType, pMarshallerType, cookie, getInstanceMethod));
            if (marshaller == null)
            {
                throw new ApplicationException();
            }

            if (!RuntimeImports.AreTypesAssignable(marshaller.GetEETypePtr(), EETypePtr.EETypePtrOf<ICustomMarshaler>()))
            {
                throw new ApplicationException();
            }

            return marshaller;
        }

        [StructLayout(LayoutKind.Sequential)]
        internal unsafe struct ModuleFixupCell
        {
            public IntPtr Handle;
            public IntPtr ModuleName;
            public EETypePtr CallingAssemblyType;
            public uint DllImportSearchPathAndCookie;
        }

        [StructLayout(LayoutKind.Sequential)]
        internal unsafe struct MethodFixupCell
        {
            public IntPtr Target;
            public IntPtr MethodName;
            public ModuleFixupCell* Module;
            private int Flags;

            public CharSet CharSetMangling => (CharSet)(Flags & MethodFixupCellFlagsConstants.CharSetMask);
            public bool IsObjectiveCMessageSend => (Flags & MethodFixupCellFlagsConstants.IsObjectiveCMessageSendMask) != 0;
            public int ObjectiveCMessageSendFunction => (Flags & MethodFixupCellFlagsConstants.ObjectiveCMessageSendFunctionMask) >> MethodFixupCellFlagsConstants.ObjectiveCMessageSendFunctionShift;
        }

        internal unsafe struct CustomMarshallerKey : IEquatable<CustomMarshallerKey>
        {
            public CustomMarshallerKey(RuntimeTypeHandle pParameterType, RuntimeTypeHandle pMarshallerType, string cookie, delegate*<string, object> getInstanceMethod)
            {
                ParameterType = pParameterType;
                MarshallerType = pMarshallerType;
                Cookie = cookie;
                GetInstanceMethod = getInstanceMethod;
            }

            public RuntimeTypeHandle ParameterType { get; }
            public RuntimeTypeHandle MarshallerType { get; }
            public string Cookie { get; }
            public delegate*<string, object> GetInstanceMethod { get; }

            public override bool Equals(object obj)
            {
                if (!(obj is CustomMarshallerKey other))
                    return false;
                return Equals(other);
            }

            public bool Equals(CustomMarshallerKey other)
            {
                return ParameterType.Equals(other.ParameterType)
                    && MarshallerType.Equals(other.MarshallerType)
                    && Cookie.Equals(other.Cookie);
            }

            public override int GetHashCode()
            {
                return ParameterType.GetHashCode()
                    ^ MarshallerType.GetHashCode()
                    ^ Cookie.GetHashCode();
            }
        }

        internal sealed class CustomMarshallerTable : ConcurrentUnifier<CustomMarshallerKey, object>
        {
            internal static CustomMarshallerTable s_customMarshallersTable = new CustomMarshallerTable();

            protected override unsafe object Factory(CustomMarshallerKey key)
            {
                return key.GetInstanceMethod(key.Cookie);
            }
        }
    }
}