Skip to content

Commit

Permalink
Move DllImportGenerator to use DisableRuntimeMarshalling for its blit…
Browse files Browse the repository at this point in the history
…table classification (dotnet#64279)
  • Loading branch information
jkoritzinsky authored Feb 10, 2022
1 parent b0e16a1 commit ccd67b0
Show file tree
Hide file tree
Showing 214 changed files with 6,593 additions and 2,951 deletions.
4 changes: 4 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<InformationalVersion Condition="'$(InformationalVersion)' == '' and '$(VersionSuffix)' != ''">$(ProductVersion)-$(VersionSuffix)</InformationalVersion>
</PropertyGroup>

<ItemGroup>
<SupportedNETCoreAppTargetFramework Include=".NETCoreApp,Version=v$(NETCoreAppMaximumVersion)" DisplayName=".NET $(NETCoreAppMaximumVersion)" Alias="net$(NETCoreAppMaximumVersion)" />
</ItemGroup>

<!-- The Default behavior in VS is to show files for the first target framework in TargetFrameworks property.
This is required to show all the files corresponding to all target frameworks in VS. -->
<ItemGroup Condition="'$(DefaultLanguageSourceExtension)' != '' and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ private static unsafe partial ulong Enable(
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "EventPipeInternal_DeleteProvider")]
internal static partial void DeleteProvider(IntPtr provHandle);

#pragma warning disable DLLIMPORTGENANALYZER015 // Use 'GeneratedDllImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
// TODO: [DllImportGenerator] Switch to use GeneratedDllImport once we annotate blittable types used in interop in CoreLib (like Guid)
[DllImport(RuntimeHelpers.QCall, EntryPoint = "EventPipeInternal_EventActivityIdControl")]
internal static extern int EventActivityIdControl(uint controlCode, ref Guid activityId);
#pragma warning restore DLLIMPORTGENANALYZER015
[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "EventPipeInternal_EventActivityIdControl")]
internal static partial int EventActivityIdControl(uint controlCode, ref Guid activityId);

[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "EventPipeInternal_WriteEventData")]
internal static unsafe partial void WriteEventData(IntPtr eventHandle, EventProvider.EventData* pEventData, uint dataCount, Guid* activityId, Guid* relatedActivityId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,9 @@ public static string GetTypeInfoName(ITypeInfo typeInfo!!)
GetTypeFromCLSID(clsid, server, ObjectHandleOnStack.Create(ref type));
return type;
}
#pragma warning disable DLLIMPORTGENANALYZER015 // Use 'GeneratedDllImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
// TODO: [DllImportGenerator] Switch to use GeneratedDllImport once we annotate blittable types used in interop in CoreLib (like Guid)
[DllImport(RuntimeHelpers.QCall, EntryPoint = "MarshalNative_GetTypeFromCLSID", CharSet = CharSet.Unicode)]
private static extern void GetTypeFromCLSID(in Guid clsid, string? server, ObjectHandleOnStack retType);
#pragma warning restore DLLIMPORTGENANALYZER015

[GeneratedDllImport(RuntimeHelpers.QCall, EntryPoint = "MarshalNative_GetTypeFromCLSID", CharSet = CharSet.Unicode)]
private static partial void GetTypeFromCLSID(in Guid clsid, string? server, ObjectHandleOnStack retType);

/// <summary>
/// Return the IUnknown* for an Object if the current context is the one
Expand Down Expand Up @@ -698,32 +696,48 @@ public static object BindToMoniker(string monikerName)
throw new NotSupportedException(SR.NotSupported_COM);
}

CreateBindCtx(0, out IBindCtx bindctx);

MkParseDisplayName(bindctx, monikerName, out _, out IMoniker pmoniker);
BindMoniker(pmoniker, 0, ref IID_IUnknown, out object obj);
ThrowExceptionForHR(CreateBindCtx(0, out IntPtr bindctx));

return obj;
try
{
ThrowExceptionForHR(MkParseDisplayName(bindctx, monikerName, out _, out IntPtr pmoniker));
try
{
ThrowExceptionForHR(BindMoniker(pmoniker, 0, ref IID_IUnknown, out IntPtr ptr));
try
{
return GetObjectForIUnknown(ptr);
}
finally
{
Release(ptr);
}
}
finally
{
Release(pmoniker);
}
}
finally
{
Release(bindctx);
}
}
#pragma warning disable DLLIMPORTGENANALYZER015 // Use 'GeneratedDllImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
// These methods use built-in COM interop, which is not supported by the source generator.

// Revist after https://github.com/mono/linker/issues/1989 is fixed
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2050:UnrecognizedReflectionPattern",
Justification = "The calling method is annotated with RequiresUnreferencedCode")]
[DllImport(Interop.Libraries.Ole32, PreserveSig = false)]
private static extern void CreateBindCtx(uint reserved, out IBindCtx ppbc);
[GeneratedDllImport(Interop.Libraries.Ole32)]
private static partial int CreateBindCtx(uint reserved, out IntPtr ppbc);

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2050:UnrecognizedReflectionPattern",
Justification = "The calling method is annotated with RequiresUnreferencedCode")]
[DllImport(Interop.Libraries.Ole32, PreserveSig = false)]
private static extern void MkParseDisplayName(IBindCtx pbc, [MarshalAs(UnmanagedType.LPWStr)] string szUserName, out uint pchEaten, out IMoniker ppmk);
[GeneratedDllImport(Interop.Libraries.Ole32, PreserveSig = false)]
private static partial int MkParseDisplayName(IntPtr pbc, [MarshalAs(UnmanagedType.LPWStr)] string szUserName, out uint pchEaten, out IntPtr ppmk);

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2050:UnrecognizedReflectionPattern",
Justification = "The calling method is annotated with RequiresUnreferencedCode")]
[DllImport(Interop.Libraries.Ole32, PreserveSig = false)]
private static extern void BindMoniker(IMoniker pmk, uint grfOpt, ref Guid iidResult, [MarshalAs(UnmanagedType.Interface)] out object ppvResult);
#pragma warning restore DLLIMPORTGENANALYZER015
[GeneratedDllImport(Interop.Libraries.Ole32, PreserveSig = false)]
private static partial int BindMoniker(IntPtr pmk, uint grfOpt, ref Guid iidResult, out IntPtr ppvResult);

[SupportedOSPlatform("windows")]
[MethodImpl(MethodImplOptions.InternalCall)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal enum ClassLibFunctionId
IDynamicCastableGetInterfaceImplementation = 9,
}

internal static class InternalCalls
internal static partial class InternalCalls
{
//
// internalcalls for System.GC.
Expand All @@ -59,17 +59,19 @@ internal static void RhCollect(int generation, InternalGCCollectionMode mode)
RhpCollect(generation, mode);
}

[DllImport(Redhawk.BaseName, CallingConvention = CallingConvention.Cdecl)]
private static extern void RhpCollect(int generation, InternalGCCollectionMode mode);
[GeneratedDllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
private static partial void RhpCollect(int generation, InternalGCCollectionMode mode);

[RuntimeExport("RhGetGcTotalMemory")]
internal static long RhGetGcTotalMemory()
{
return RhpGetGcTotalMemory();
}

[DllImport(Redhawk.BaseName, CallingConvention = CallingConvention.Cdecl)]
private static extern long RhpGetGcTotalMemory();
[GeneratedDllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
private static partial long RhpGetGcTotalMemory();

[RuntimeExport("RhStartNoGCRegion")]
internal static int RhStartNoGCRegion(long totalSize, bool hasLohSize, long lohSize, bool disallowFullBlockingGC)
Expand Down Expand Up @@ -284,39 +286,49 @@ internal static extern unsafe bool RhpCallFilterFunclet(

// Block the current thread until at least one object needs to be finalized (returns true) or
// memory is low (returns false and the finalizer thread should initiate a garbage collection).
[DllImport(Redhawk.BaseName, CallingConvention = CallingConvention.Cdecl)]
internal static extern uint RhpWaitForFinalizerRequest();
[GeneratedDllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static partial uint RhpWaitForFinalizerRequest();

// Indicate that the current round of finalizations is complete.
[DllImport(Redhawk.BaseName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void RhpSignalFinalizationComplete();
[GeneratedDllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static partial void RhpSignalFinalizationComplete();

[DllImport(Redhawk.BaseName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void RhpAcquireCastCacheLock();
[GeneratedDllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static partial void RhpAcquireCastCacheLock();

[DllImport(Redhawk.BaseName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void RhpReleaseCastCacheLock();
[GeneratedDllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static partial void RhpReleaseCastCacheLock();

[DllImport(Redhawk.BaseName, CallingConvention = CallingConvention.Cdecl)]
internal static extern ulong RhpGetTickCount64();
[GeneratedDllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static partial ulong RhpGetTickCount64();

[DllImport(Redhawk.BaseName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void RhpAcquireThunkPoolLock();
[GeneratedDllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static partial void RhpAcquireThunkPoolLock();

[DllImport(Redhawk.BaseName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void RhpReleaseThunkPoolLock();
[GeneratedDllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static partial void RhpReleaseThunkPoolLock();

[DllImport(Redhawk.BaseName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr RhAllocateThunksMapping();
[GeneratedDllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static partial IntPtr RhAllocateThunksMapping();

// Enters a no GC region, possibly doing a blocking GC if there is not enough
// memory available to satisfy the caller's request.
[DllImport(Redhawk.BaseName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int RhpStartNoGCRegion(long totalSize, bool hasLohSize, long lohSize, bool disallowFullBlockingGC);
[GeneratedDllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static partial int RhpStartNoGCRegion(long totalSize, bool hasLohSize, long lohSize, bool disallowFullBlockingGC);

// Exits a no GC region, possibly doing a GC to clean up the garbage that
// the caller allocated.
[DllImport(Redhawk.BaseName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int RhpEndNoGCRegion();
[GeneratedDllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
internal static partial int RhpEndNoGCRegion();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Runtime.InteropServices
{
/// <summary>
/// Provides an equivalent to <see cref="UnmanagedCallersOnlyAttribute"/> for native
/// functions declared in .NET.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class UnmanagedCallConvAttribute : Attribute
{
public UnmanagedCallConvAttribute()
{
}

/// <summary>
/// Types indicating calling conventions for the unmanaged target.
/// </summary>
/// <remarks>
/// If <c>null</c>, the semantics are identical to <c>CallingConvention.Winapi</c>.
/// </remarks>
public Type[]? CallConvs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace System.Runtime
{
internal static class RuntimeExports
internal static partial class RuntimeExports
{
//
// internal calls for allocation
Expand Down Expand Up @@ -301,8 +301,9 @@ public static unsafe int RhGetCurrentThreadStackTrace(IntPtr[] outputBuffer)
return RhpGetCurrentThreadStackTrace(pOutputBuffer, (uint)((outputBuffer != null) ? outputBuffer.Length : 0), new UIntPtr(&pOutputBuffer));
}

[DllImport(Redhawk.BaseName, CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe int RhpGetCurrentThreadStackTrace(IntPtr* pOutputBuffer, uint outputBufferLength, UIntPtr addressInCurrentFrame);
[GeneratedDllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
private static unsafe partial int RhpGetCurrentThreadStackTrace(IntPtr* pOutputBuffer, uint outputBufferLength, UIntPtr addressInCurrentFrame);

// Worker for RhGetCurrentThreadStackTrace. RhGetCurrentThreadStackTrace just allocates a transition
// frame that will be used to seed the stack trace and this method does all the real work.
Expand Down
Loading

0 comments on commit ccd67b0

Please sign in to comment.