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

InteropExtensions.cs « InteropExtensions « src « System.Private.Interop « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1b6d888d875b2b7a310b36c92eb71f23341280b5 (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
// 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.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Runtime;
using System.Reflection;

namespace System.Runtime.InteropServices
{
    /// <summary>
    ///     Hooks for System.Private.Interop.dll code to access internal functionality in System.Private.CoreLib.dll.
    ///     Methods added to InteropExtensions should also be added to the System.Private.CoreLib.InteropServices contract
    ///     in order to be accessible from System.Private.Interop.dll.
    /// </summary>
    [CLSCompliant(false)]
    public static class InteropExtensions
    {
        // Converts a managed DateTime to native OLE datetime
        // Used by MCG marshalling code
        public static double ToNativeOleDate(DateTime dateTime)
        {
            return dateTime.ToOADate();
        }

        // Converts native OLE datetime to managed DateTime
        // Used by MCG marshalling code
        public static DateTime FromNativeOleDate(double nativeOleDate)
        {
            return new DateTime(nativeOleDate.DoubleDateToTicks());
        }

        // Used by MCG's SafeHandle marshalling code to initialize a handle
        public static void InitializeHandle(SafeHandle safeHandle, IntPtr win32Handle)
        {
            // We need private reflection here to access the handle field.
            FieldInfo fieldInfo = safeHandle.GetType().GetField("handle", BindingFlags.NonPublic | BindingFlags.Instance);
            fieldInfo.SetValue(safeHandle, win32Handle);
        }

        // Used for methods in System.Private.Interop.dll that need to work from offsets on boxed structs
        public unsafe static void PinObjectAndCall(Object obj, Action<IntPtr> del)
        {
            throw new NotSupportedException("PinObjectAndCall");
        }

        public static void CopyToManaged(IntPtr source, Array destination, int startIndex, int length)
        {
            throw new NotSupportedException("CopyToManaged");
        }

        public static void CopyToNative(Array array, int startIndex, IntPtr destination, int length)
        {
            throw new NotSupportedException("CopyToNative");
        }

        public static int GetElementSize(this Array array)
        {
            throw new NotSupportedException("GetElementSize");
        }

        public static bool IsBlittable(this RuntimeTypeHandle handle)
        {
            throw new NotSupportedException("IsBlittable");
        }

        public static bool IsElementTypeBlittable(this Array array)
        {
            throw new NotSupportedException("IsElementTypeBlittable");
        }

        public static bool IsGenericType(this RuntimeTypeHandle handle)
        {
            throw new NotSupportedException("IsGenericType");
        }


        public static bool IsClass(RuntimeTypeHandle handle)
        {
            return Type.GetTypeFromHandle(handle).GetTypeInfo().IsClass;
        }

        public static IntPtr GetNativeFunctionPointer(this Delegate del)
        {
            throw new PlatformNotSupportedException("GetNativeFunctionPointer");
        }
        public static IntPtr GetFunctionPointer(this Delegate del, out RuntimeTypeHandle typeOfFirstParameterIfInstanceDelegate)
        {
            // Note this work only for non-static methods , for static methods 
            // _methodPtr points to a stub that remove the this pointer and 
            // _methodPtrAux points actual pointer.
            typeOfFirstParameterIfInstanceDelegate = default(RuntimeTypeHandle);
            BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
            FieldInfo field = del.GetType().GetField("_methodPtr", bindFlags);
            return (IntPtr)field.GetValue(del);
        }

        public static IntPtr GetRawFunctionPointerForOpenStaticDelegate(this Delegate del)
        {
            throw new NotSupportedException("GetRawFunctionPointerForOpenStaticDelegate");
        }

        public static IntPtr GetRawValue(this RuntimeTypeHandle handle)
        {
            throw new NotSupportedException("GetRawValue");
        }
      
        public static bool IsOfType(this Object obj, RuntimeTypeHandle handle)
        {
            return obj.GetType() == Type.GetTypeFromHandle(handle);
        }

        public static bool IsNull(this RuntimeTypeHandle handle)
        {
            return handle.Equals(default(RuntimeTypeHandle));
        }

        public static Type GetTypeFromHandle(IntPtr typeHandle)
        {
            throw new NotSupportedException("GetTypeFromHandle(IntPtr)");
        }

        public static Type GetTypeFromHandle(RuntimeTypeHandle typeHandle)
        {
            return Type.GetTypeFromHandle(typeHandle);
        }

        public static int GetValueTypeSize(this RuntimeTypeHandle handle)
        {
            throw new NotSupportedException("GetValueTypeSize");
        }

        public static bool IsValueType(this RuntimeTypeHandle handle)
        {
            return Type.GetTypeFromHandle(handle).GetTypeInfo().IsValueType;
        }

        public static bool IsEnum(this RuntimeTypeHandle handle)
        {
            return Type.GetTypeFromHandle(handle).GetTypeInfo().IsEnum;
        }

        public static bool AreTypesAssignable(RuntimeTypeHandle sourceHandle, RuntimeTypeHandle targetHandle)
        {
            Type srcType = Type.GetTypeFromHandle(sourceHandle);
            Type targetType = Type.GetTypeFromHandle(targetHandle);
            return targetType.IsAssignableFrom(srcType);
        }

        public static unsafe void Memcpy(IntPtr destination, IntPtr source, int bytesToCopy)
        {
            throw new NotSupportedException("Memcpy");
        }

        public static bool RuntimeRegisterGcCalloutForGCStart(IntPtr pCalloutMethod)
        {
            //Nop
            return true;
        }

        public static bool RuntimeRegisterGcCalloutForGCEnd(IntPtr pCalloutMethod)
        {
            //Nop
            return true;
        }

        public static bool RuntimeRegisterGcCalloutForAfterMarkPhase(IntPtr pCalloutMethod)
        {
            //Nop
            return true;
        }

        public static bool RuntimeRegisterRefCountedHandleCallback(IntPtr pCalloutMethod, RuntimeTypeHandle pTypeFilter)
        {
            // Nop 
            return true;
        }

        public static void RuntimeUnregisterRefCountedHandleCallback(IntPtr pCalloutMethod, RuntimeTypeHandle pTypeFilter)
        {
            //Nop 
        }
        public static IntPtr RuntimeHandleAllocRefCounted(Object value)
        {
            return GCHandle.ToIntPtr( GCHandle.Alloc(value, GCHandleType.Normal));
        }

        public static void RuntimeHandleSet(IntPtr handle, Object value)
        {
            GCHandle gcHandle = GCHandle.FromIntPtr(handle);
            gcHandle.Target = value;
        }

        public static void RuntimeHandleFree(IntPtr handlePtr)
        {
            GCHandle handle = GCHandle.FromIntPtr(handlePtr);
            handle.Free();
        }

        public static IntPtr RuntimeHandleAllocDependent(object primary, object secondary)
        {
            throw new NotSupportedException("RuntimeHandleAllocDependent");
        }

        public static bool RuntimeIsPromoted(object obj)
        {
            throw new NotSupportedException("RuntimeIsPromoted");
        }

        public static void RuntimeHandleSetDependentSecondary(IntPtr handle, Object secondary)
        {
            throw new NotSupportedException("RuntimeHandleSetDependentSecondary");
        }

        public static T UncheckedCast<T>(object obj) where T : class
        {
            return obj as T;
        }

        public static bool IsArray(RuntimeTypeHandle type)
        {
            throw new NotSupportedException("IsArray");
        }

        public static RuntimeTypeHandle GetArrayElementType(RuntimeTypeHandle arrayType)
        {
            throw new NotSupportedException("GetArrayElementType");
        }

        public static RuntimeTypeHandle GetTypeHandle(this object target)
        {
            Type type = target.GetType();
            return type.TypeHandle;
        }

        public static bool IsInstanceOf(object obj, RuntimeTypeHandle typeHandle)
        {
            Type type =  Type.GetTypeFromHandle(typeHandle);
            return type.IsInstanceOfType(obj);
        }

        public static bool IsInstanceOfClass(object obj, RuntimeTypeHandle classTypeHandle)
        {
            return obj.GetType() == Type.GetTypeFromHandle(classTypeHandle);
        }

        public static bool IsInstanceOfInterface(object obj, RuntimeTypeHandle interfaceTypeHandle)
        {
            Type interfaceType = Type.GetTypeFromHandle(interfaceTypeHandle);
            return TypeExtensions.IsInstanceOfType(interfaceType, obj);
        }

        public static bool GuidEquals(ref Guid left, ref Guid right)
        {
            return left.Equals(right);
        }

        public static bool ComparerEquals<T>(T left, T right)
        {
            throw new NotSupportedException("ComparerEquals");
        }

        public static object RuntimeNewObject(RuntimeTypeHandle typeHnd)
        {
            Func<Type, object> getUninitializedObjectDelegate = 
                                                    (Func<Type, object>)
                                                     typeof(string)
                                                      .GetTypeInfo()
                                                     .Assembly
                                                     .GetType("System.Runtime.Serialization.FormatterServices")
                                                     ?.GetMethod("GetUninitializedObject", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)
                                                     ?.CreateDelegate(typeof(Func<Type, object>));

            return getUninitializedObjectDelegate.Invoke(Type.GetTypeFromHandle(typeHnd));
        }

        internal static unsafe int wcslen(char* ptr)
        {
            char* end = ptr;

            // The following code is (somewhat surprisingly!) significantly faster than a naive loop,
            // at least on x86 and the current jit.

            // First make sure our pointer is aligned on a dword boundary
            while (((uint)end & 3) != 0 && *end != 0)
                end++;
            if (*end != 0)
            {
                // The loop condition below works because if "end[0] & end[1]" is non-zero, that means
                // neither operand can have been zero. If is zero, we have to look at the operands individually,
                // but we hope this going to fairly rare.

                // In general, it would be incorrect to access end[1] if we haven't made sure
                // end[0] is non-zero. However, we know the ptr has been aligned by the loop above
                // so end[0] and end[1] must be in the same page, so they're either both accessible, or both not.

                while ((end[0] & end[1]) != 0 || (end[0] != 0 && end[1] != 0))
                {
                    end += 2;
                }
            }
            // finish up with the naive loop
            for (; *end != 0; end++)
                ;

            int count = (int)(end - ptr);

            return count;
        }

        /// <summary>
        /// Copy StringBuilder's contents to the char*, and appending a '\0'
        /// The destination buffer must be big enough, and include space for '\0'
        /// NOTE: There is no guarantee the destination pointer has enough size, but we have no choice
        /// because the pointer might come from native code.
        /// </summary>
        /// <param name="stringBuilder"></param>
        /// <param name="destination"></param>
        public static unsafe void UnsafeCopyTo(this System.Text.StringBuilder stringBuilder, char* destination)
        {
            for (int i = 0; i < stringBuilder.Length; i++)
            {
                destination[i] = stringBuilder[i];
            }
            destination[stringBuilder.Length] = '\0';
        }

        public static unsafe void ReplaceBuffer(this System.Text.StringBuilder stringBuilder, char* newBuffer)
        {
            // Mimic N stringbuilder replacebuffer behaviour.wcslen assume newBuffer to be '\0' terminated.
            int len = wcslen(newBuffer);
            stringBuilder.Clear();
            // the '+1' is for back-compat with desktop CLR in terms of length calculation because desktop
            // CLR had '\0'
            stringBuilder.EnsureCapacity(len + 1);
            stringBuilder.Append(newBuffer, len);
        }

        public static void ReplaceBuffer(this System.Text.StringBuilder stringBuilder, char[] newBuffer)
        {
            // mimic N stringbuilder replacebuffer behaviour. this's safe to do since we know the 
            // length of newBuffer.
            stringBuilder.Clear();
            // the '+1' is for back-compat with desktop CLR in terms of length calculation because desktop
            // CLR had '\0'
            stringBuilder.EnsureCapacity(newBuffer.Length + 1);
            stringBuilder.Append(newBuffer);
        }

        public static char[] GetBuffer(this System.Text.StringBuilder stringBuilder, out int len)
        {
            return stringBuilder.GetBuffer(out len);
        }

        public static IntPtr RuntimeHandleAllocVariable(Object value, uint type)
        {
            throw new NotSupportedException("RuntimeHandleAllocVariable");
        }

        public static uint RuntimeHandleGetVariableType(IntPtr handle)
        {
            throw new NotSupportedException("RuntimeHandleGetVariableType");
        }

        public static void RuntimeHandleSetVariableType(IntPtr handle, uint type)
        {
            throw new NotSupportedException("RuntimeHandleSetVariableType");
        }

        public static uint RuntimeHandleCompareExchangeVariableType(IntPtr handle, uint oldType, uint newType)
        {
            throw new NotSupportedException("RuntimeHandleCompareExchangeVariableType");
        }
        
        public static void SetExceptionErrorCode(Exception exception, int hr)
        {
            throw new NotSupportedException("SetExceptionErrorCode");
        }

        public static Exception CreateDataMisalignedException(string message)
        {
            return new DataMisalignedException(message);
        }

        public static Delegate CreateDelegate(RuntimeTypeHandle typeHandleForDelegate, IntPtr ldftnResult, Object thisObject, bool isStatic, bool isVirtual, bool isOpen)
        {
            throw new NotSupportedException("CreateDelegate");
        }

        public enum VariableHandleType
        {
            WeakShort = 0x00000100,
            WeakLong = 0x00000200,
            Strong = 0x00000400,
            Pinned = 0x00000800,
        }

        public static void AddExceptionDataForRestrictedErrorInfo(Exception ex, string restrictedError, string restrictedErrorReference, string restrictedCapabilitySid, object restrictedErrorObject)
        {
            throw new NotSupportedException("AddExceptionDataForRestrictedErrorInfo");
        }

        public static bool TryGetRestrictedErrorObject(Exception ex, out object restrictedErrorObject)
        {
            throw new NotSupportedException("TryGetRestrictedErrorObject");
        }

        public static bool TryGetRestrictedErrorDetails(Exception ex, out string restrictedError, out string restrictedErrorReference, out string restrictedCapabilitySid)
        {
            throw new NotSupportedException("TryGetRestrictedErrorDetails");
        }

        public static TypeInitializationException CreateTypeInitializationException(string message)
        {
            return new TypeInitializationException(message,null);
        }

        public unsafe static IntPtr GetObjectID(object obj)
        {
            throw new NotSupportedException("GetObjectID");
        }

        public static bool RhpETWShouldWalkCom()
        {
            throw new NotSupportedException("RhpETWShouldWalkCom");
        }

        public static void RhpETWLogLiveCom(int eventType, IntPtr CCWHandle, IntPtr objectID, IntPtr typeRawValue, IntPtr IUnknown, IntPtr VTable, Int32 comRefCount, Int32 jupiterRefCount, Int32 flags)
        {
            throw new NotSupportedException("RhpETWLogLiveCom");
        }

        public static bool SupportsReflection(this Type type)
        {
            return true;
        }

        public static void SuppressReentrantWaits()
        {
            // Nop 
        }

        public static void RestoreReentrantWaits()
        {
            //Nop
        }

        public static IntPtr GetCriticalHandle(CriticalHandle criticalHandle)
        { 
            throw new NotSupportedException("GetCriticalHandle"); 
        }
 
        public static void SetCriticalHandle(CriticalHandle criticalHandle, IntPtr handle)
        { 
            throw new NotSupportedException("SetCriticalHandle"); 
        }
    }

    public class GCHelpers
    {
        public static void RhpSetThreadDoNotTriggerGC() { }
        public static void RhpClearThreadDoNotTriggerGC() { }
    }
}