예제 #1
0
        internal void VerifyWinRTGenericInterfaceGuids()
        {
#if !CORECLR && ENABLE_WINRT
            // Check dynamic guid generation generates same guid as mcg generated one
            if (m_interfaceData != null)
            {
                foreach (McgInterfaceData item in m_interfaceData)
                {
                    RuntimeTypeHandle typeHnd = item.ItfType;
                    if (!typeHnd.IsInvalid())
                    {
                        if (typeHnd.IsGenericType())
                        {
                            Guid expectedGuid = item.ItfGuid;
                            Guid actualGuid   = DynamicInteropGuidHelpers.GetGuid_NoThrow(typeHnd);
                            if (!expectedGuid.Equals(actualGuid))
                            {
                                Environment.FailFast("Guid mismatch:" + "  expected:" + expectedGuid + "  actual:" + actualGuid);
                            }
                        }
                    }
                }
            }
#endif
        }
예제 #2
0
        public static unsafe void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld)
        {
            if (structure == null)
            {
                throw new ArgumentNullException(nameof(structure));
            }

            if (ptr == IntPtr.Zero)
            {
                throw new ArgumentNullException(nameof(ptr));
            }

            if (fDeleteOld)
            {
                DestroyStructure(ptr, structure.GetType());
            }

            RuntimeTypeHandle structureTypeHandle = structure.GetType().TypeHandle;

            if (structureTypeHandle.IsGenericType() || structureTypeHandle.IsGenericTypeDefinition())
            {
                throw new ArgumentException(SR.Argument_NeedNonGenericObject, nameof(structure));
            }

            IntPtr marshalStub;

            if (structureTypeHandle.IsBlittable())
            {
                if (!RuntimeAugments.InteropCallbacks.TryGetStructMarshalStub(structureTypeHandle, out marshalStub))
                {
                    marshalStub = IntPtr.Zero;
                }
            }
            else
            {
                marshalStub = RuntimeAugments.InteropCallbacks.GetStructMarshalStub(structureTypeHandle);
            }

            if (marshalStub != IntPtr.Zero)
            {
                if (structureTypeHandle.IsValueType())
                {
                    ((delegate * < ref byte, ref byte, void >)marshalStub)(ref structure.GetRawData(), ref *(byte *)ptr);
                }
                else
                {
                    ((delegate * < object, ref byte, void >)marshalStub)(structure, ref *(byte *)ptr);
                }
            }
            else
            {
                nuint size = (nuint)RuntimeAugments.InteropCallbacks.GetStructUnsafeStructSize(structureTypeHandle);

                Buffer.Memmove(ref *(byte *)ptr, ref structure.GetRawData(), size);
            }
        }
예제 #3
0
        public bool TryGetFieldOffset(RuntimeTypeHandle declaringTypeHandle, uint fieldOrdinal, out int fieldOffset)
        {
            fieldOffset = int.MinValue;

            // No use going further for non-generic types... TypeLoader doesn't have offset answers for non-generic types!
            if (!declaringTypeHandle.IsGenericType())
                return false;

            using (LockHolder.Hold(_typeLoaderLock))
            {
                return TypeBuilder.TryGetFieldOffset(declaringTypeHandle, fieldOrdinal, out fieldOffset);
            }
        }
예제 #4
0
        public static unsafe void DestroyStructure(IntPtr ptr, Type structuretype)
        {
            if (ptr == IntPtr.Zero)
            {
                throw new ArgumentNullException(nameof(ptr));
            }

            if (structuretype == null)
            {
                throw new ArgumentNullException(nameof(structuretype));
            }

            RuntimeTypeHandle structureTypeHandle = structuretype.TypeHandle;

            if (structureTypeHandle.IsGenericType() || structureTypeHandle.IsGenericTypeDefinition())
            {
                throw new ArgumentException(SR.Argument_NeedNonGenericType, "t");
            }

            if (structureTypeHandle.IsEnum() ||
                structureTypeHandle.IsInterface() ||
                InteropExtensions.AreTypesAssignable(typeof(Delegate).TypeHandle, structureTypeHandle))
            {
                throw new ArgumentException(SR.Format(SR.Argument_MustHaveLayoutOrBeBlittable, structureTypeHandle.LastResortToString));
            }

            if (structureTypeHandle.IsBlittable())
            {
                // ok to call with blittable structure, but no work to do in this case.
                return;
            }

            IntPtr destroyStructureStub = RuntimeAugments.InteropCallbacks.GetDestroyStructureStub(structureTypeHandle, out bool hasInvalidLayout);

            if (hasInvalidLayout)
            {
                throw new ArgumentException(SR.Format(SR.Argument_MustHaveLayoutOrBeBlittable, structureTypeHandle.LastResortToString));
            }
            // DestroyStructureStub == IntPtr.Zero means its fields don't need to be destroied
            if (destroyStructureStub != IntPtr.Zero)
            {
                CalliIntrinsics.Call <int>(
                    destroyStructureStub,
                    (void *)ptr                                     // unsafe (no need to adjust as it is always struct)
                    );
            }
        }
예제 #5
0
        public int GetTokenFor(RuntimeMethodHandle method)
        {
            IRuntimeMethodInfo          methodInfo = method.GetMethodInfo();
            RuntimeMethodHandleInternal internal2  = methodInfo.Value;

            if ((methodInfo != null) && !RuntimeMethodHandle.IsDynamicMethod(internal2))
            {
                RuntimeType declaringType = RuntimeMethodHandle.GetDeclaringType(internal2);
                if ((declaringType != null) && RuntimeTypeHandle.IsGenericType(declaringType))
                {
                    MethodBase methodBase            = RuntimeType.GetMethodBase(methodInfo);
                    Type       genericTypeDefinition = methodBase.DeclaringType.GetGenericTypeDefinition();
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_MethodDeclaringTypeGenericLcg"), new object[] { methodBase, genericTypeDefinition }));
                }
            }
            this.m_tokens.Add(method);
            return((this.m_tokens.Count - 1) | 0x6000000);
        }
예제 #6
0
        public static unsafe void DestroyStructure(IntPtr ptr, Type structuretype)
        {
            if (ptr == IntPtr.Zero)
            {
                throw new ArgumentNullException(nameof(ptr));
            }

            if (structuretype == null)
            {
                throw new ArgumentNullException(nameof(structuretype));
            }

            RuntimeTypeHandle structureTypeHandle = structuretype.TypeHandle;

            if (structureTypeHandle.IsGenericType() || structureTypeHandle.IsGenericTypeDefinition())
            {
                throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(structuretype));
            }

            if (structureTypeHandle.IsEnum() ||
                structureTypeHandle.IsInterface() ||
                InteropExtensions.AreTypesAssignable(typeof(Delegate).TypeHandle, structureTypeHandle))
            {
                throw new ArgumentException(SR.Format(SR.Argument_MustHaveLayoutOrBeBlittable, structureTypeHandle.LastResortToString));
            }

            if (structureTypeHandle.IsBlittable())
            {
                // ok to call with blittable structure, but no work to do in this case.
                return;
            }

            IntPtr destroyStructureStub = RuntimeInteropData.GetDestroyStructureStub(structureTypeHandle, out bool hasInvalidLayout);

            if (hasInvalidLayout)
            {
                throw new ArgumentException(SR.Format(SR.Argument_MustHaveLayoutOrBeBlittable, structureTypeHandle.LastResortToString));
            }
            // DestroyStructureStub == IntPtr.Zero means its fields don't need to be destroyed
            if (destroyStructureStub != IntPtr.Zero)
            {
                ((delegate * < ref byte, void >)destroyStructureStub)(ref *(byte *)ptr);
            }
        }
예제 #7
0
        public static unsafe void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld)
        {
            if (structure == null)
            {
                throw new ArgumentNullException(nameof(structure));
            }

            if (ptr == IntPtr.Zero)
            {
                throw new ArgumentNullException(nameof(ptr));
            }

            if (fDeleteOld)
            {
                DestroyStructure(ptr, structure.GetType());
            }

            RuntimeTypeHandle structureTypeHandle = structure.GetType().TypeHandle;

            if (structureTypeHandle.IsGenericType() || structureTypeHandle.IsGenericTypeDefinition())
            {
                throw new ArgumentException(nameof(structure), SR.Argument_NeedNonGenericObject);
            }

            // Boxed struct start at offset 1 (EEType* at offset 0) while class start at offset 0
            int offset = structureTypeHandle.IsValueType() ? 1 : 0;

            IntPtr marshalStub;

            if (structureTypeHandle.IsBlittable())
            {
                if (!RuntimeAugments.InteropCallbacks.TryGetStructMarshalStub(structureTypeHandle, out marshalStub))
                {
                    marshalStub = IntPtr.Zero;
                }
            }
            else
            {
                marshalStub = RuntimeAugments.InteropCallbacks.GetStructMarshalStub(structureTypeHandle);
            }

            if (marshalStub != IntPtr.Zero)
            {
                InteropExtensions.PinObjectAndCall(structure,
                                                   unboxedStructPtr =>
                {
                    CalliIntrinsics.Call <int>(
                        marshalStub,
                        ((void *)((IntPtr *)unboxedStructPtr + offset)),    // safe (need to adjust offset as it could be class)
                        (void *)ptr                                         // unsafe (no need to adjust as it is always struct)
                        );
                });
            }
            else
            {
                int structSize = Marshal.SizeOf(structure);
                InteropExtensions.PinObjectAndCall(structure,
                                                   unboxedStructPtr =>
                {
                    InteropExtensions.Memcpy(
                        ptr,                                                // unsafe (no need to adjust as it is always struct)
                        (IntPtr)((IntPtr *)unboxedStructPtr + offset),      // safe (need to adjust offset as it could be class)
                        structSize
                        );
                });
            }
        }