Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ private static unsafe string[] InitializeCommandLineArgs(char* exePath, int argc
return mainMethodArgs;
}

[UnmanagedCallersOnly]
private static unsafe void InitializeCommandLineArgs(char* exePath, int argc, char** argv, string[]* pResult, Exception* pException)
{
try
{
*pResult = InitializeCommandLineArgs(exePath, argc, argv);
}
catch (Exception ex)
{
*pException = ex;
}
}

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "Environment_GetProcessorCount")]
internal static partial int GetProcessorCount();

Expand Down
14 changes: 14 additions & 0 deletions src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,20 @@ private static unsafe uint RunFinalizers()
return count;
}

[UnmanagedCallersOnly]
private static unsafe uint RunFinalizers(Exception* pException)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this affect diagnosability of unhandled exceptions thrown by finalizers (e.g. from crash dumps)?

Also, the two RunFinalizers methods can be merged.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkoritzinsky See the "Exception Propagation Helper" section in #123864. I've not written the QCall yet, but I can share the details if you'd like.

{
try
{
return RunFinalizers();
}
catch (Exception ex)
{
*pException = ex;
return 0;
}
}

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "GCInterface_WaitForPendingFinalizers")]
private static partial void _WaitForPendingFinalizers();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,32 @@ private static unsafe void ParseAsAssemblySpec(char* pAssemblyName, void* pAssem
}
}

[UnmanagedCallersOnly]
private static unsafe void ParseAsAssemblySpec(char* pAssemblyName, void* pAssemblySpec, Exception* pException)
{
try
{
ParseAsAssemblySpec(pAssemblyName, pAssemblySpec);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actual work can be inlined here.

}
catch (Exception ex)
{
*pException = ex;
}
}

[UnmanagedCallersOnly]
private static unsafe void CreateAssemblyName(AssemblyName* pResult, NativeAssemblyNameParts* pParts, Exception* pException)
{
try
{
*pResult = new AssemblyName(pParts);
}
catch (Exception ex)
{
*pException = ex;
}
}

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "AssemblyName_InitializeAssemblySpec")]
private static unsafe partial void InitializeAssemblySpec(NativeAssemblyNameParts* pAssemblyNameParts, void* pAssemblySpec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ internal static RuntimeType GetTypeReferencedByCustomAttribute(string typeName,
return GetTypeHelper(typeName, requestingAssembly, throwOnError, requireAssemblyQualifiedName, unsafeAccessorMethod);
}

[UnmanagedCallersOnly]
private static unsafe void GetTypeHelper(char* pTypeName, RuntimeAssembly* pRequestingAssembly, bool throwOnError, bool requireAssemblyQualifiedName, IntPtr unsafeAccessorMethod, RuntimeType* pResult, Exception* pException)
{
try
{
*pResult = GetTypeHelper(pTypeName, *pRequestingAssembly, throwOnError, requireAssemblyQualifiedName, unsafeAccessorMethod);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actual work can be inlined here.

}
catch (Exception ex)
{
*pException = ex;
}
}

internal static RuntimeType? GetTypeHelper(ReadOnlySpan<char> typeName, RuntimeAssembly? requestingAssembly,
bool throwOnError, bool requireAssemblyQualifiedName, IntPtr unsafeAccessorMethod = 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ internal static int CallICustomQueryInterface(ManagedObjectWrapperHolder holder,
return -1; // See TryInvokeICustomQueryInterfaceResult
}

[UnmanagedCallersOnly]
private static unsafe void CallICustomQueryInterface(ManagedObjectWrapperHolder* pHolder, Guid* pIid, IntPtr* ppObject, int* pResult, Exception* pException)
{
try
{
*pResult = CallICustomQueryInterface(*pHolder, ref *pIid, out *ppObject);
}
catch (Exception ex)
{
*pException = ex;
}
}

internal static IntPtr GetOrCreateComInterfaceForObjectWithGlobalMarshallingInstance(object obj)
{
if (s_globalInstanceForMarshalling == null)
Expand All @@ -84,6 +97,19 @@ internal static IntPtr GetOrCreateComInterfaceForObjectWithGlobalMarshallingInst
}
}

[UnmanagedCallersOnly]
private static unsafe void GetOrCreateComInterfaceForObjectWithGlobalMarshallingInstance(object* pObj, IntPtr* pResult, Exception* pException)
{
try
{
*pResult = GetOrCreateComInterfaceForObjectWithGlobalMarshallingInstance(*pObj);
}
catch (Exception ex)
{
*pException = ex;
}
}

internal static object? GetOrCreateObjectForComInstanceWithGlobalMarshallingInstance(IntPtr comObject, CreateObjectFlags flags)
{
if (s_globalInstanceForMarshalling == null)
Expand All @@ -103,6 +129,19 @@ internal static IntPtr GetOrCreateComInterfaceForObjectWithGlobalMarshallingInst
}
}

[UnmanagedCallersOnly]
private static unsafe void GetOrCreateObjectForComInstanceWithGlobalMarshallingInstance(IntPtr comObject, int flags, object* pResult, Exception* pException)
{
try
{
*pResult = GetOrCreateObjectForComInstanceWithGlobalMarshallingInstance(comObject, (CreateObjectFlags)flags);
}
catch (Exception ex)
{
*pException = ex;
}
}

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ComWrappers_GetIReferenceTrackerTargetVftbl")]
[SuppressGCTransition]
private static partial IntPtr GetDefaultIReferenceTrackerTargetVftbl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ public IntPtr MarshalManagedToNative(object ManagedObj)
internal static object InternalMarshalNativeToManaged(IntPtr pNativeData)
=> GetInstance(null).MarshalNativeToManaged(pNativeData);

[System.Runtime.InteropServices.UnmanagedCallersOnly]
private static unsafe void InternalMarshalNativeToManaged(IntPtr pNativeData, object* pResult, Exception* pException)
{
try
{
*pResult = InternalMarshalNativeToManaged(pNativeData);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actual work can be inlined here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This applies to all cases where the managed method exists for the sole purpose of being called by the VM, and it is not very complicated.)

}
catch (Exception ex)
{
*pException = ex;
}
}

public object MarshalNativeToManaged(IntPtr pNativeData)
{
ArgumentNullException.ThrowIfNull(pNativeData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ internal static bool IsInterfaceImplemented(IDynamicInterfaceCastable castable,
return isImplemented;
}

[UnmanagedCallersOnly]
private static unsafe void IsInterfaceImplemented(IDynamicInterfaceCastable* pCastable, RuntimeType* pInterfaceType, bool throwIfNotImplemented, bool* pResult, Exception* pException)
{
try
{
*pResult = IsInterfaceImplemented(*pCastable, *pInterfaceType, throwIfNotImplemented);
}
catch (Exception ex)
{
*pException = ex;
}
}

[StackTraceHidden]
internal static RuntimeType? GetInterfaceImplementation(IDynamicInterfaceCastable castable, RuntimeType interfaceType)
{
Expand All @@ -41,5 +54,18 @@ internal static bool IsInterfaceImplemented(IDynamicInterfaceCastable castable,

return implType;
}

[UnmanagedCallersOnly]
private static unsafe void GetInterfaceImplementation(IDynamicInterfaceCastable* pCastable, RuntimeType* pInterfaceType, RuntimeType* pResult, Exception* pException)
{
try
{
*pResult = GetInterfaceImplementation(*pCastable, *pInterfaceType);
}
catch (Exception ex)
{
*pException = ex;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,18 @@ internal static IntPtr LoadLibraryByName(string libraryName, Assembly assembly,
internal static partial IntPtr LoadByName(string libraryName, QCallAssembly callingAssembly,
[MarshalAs(UnmanagedType.Bool)] bool hasDllImportSearchPathFlag, uint dllImportSearchPathFlag,
[MarshalAs(UnmanagedType.Bool)] bool throwOnError);

[UnmanagedCallersOnly]
private static unsafe void LoadLibraryCallbackStub(string* pLibraryName, Assembly* pAssembly, bool hasDllImportSearchPathFlags, uint dllImportSearchPathFlags, IntPtr* pResult, Exception* pException)
{
try
{
*pResult = LoadLibraryCallbackStub(*pLibraryName, *pAssembly, hasDllImportSearchPathFlags, dllImportSearchPathFlags);
}
catch (Exception ex)
{
*pException = ex;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ private static IntPtr ResolveUnmanagedDll(string unmanagedDllName, IntPtr gchAss
return context.LoadUnmanagedDll(unmanagedDllName);
}

[UnmanagedCallersOnly]
private static unsafe void ResolveUnmanagedDll(string* pUnmanagedDllName, IntPtr gchAssemblyLoadContext, IntPtr* pResult, Exception* pException)
{
try
{
*pResult = ResolveUnmanagedDll(*pUnmanagedDllName, gchAssemblyLoadContext);
}
catch (Exception ex)
{
*pException = ex;
}
}

// This method is invoked by the VM to resolve a native library using the ResolvingUnmanagedDll event
// after trying all other means of resolution.
private static IntPtr ResolveUnmanagedDllUsingEvent(string unmanagedDllName, Assembly assembly, IntPtr gchAssemblyLoadContext)
Expand All @@ -124,6 +137,19 @@ private static IntPtr ResolveUnmanagedDllUsingEvent(string unmanagedDllName, Ass
return context.GetResolvedUnmanagedDll(assembly, unmanagedDllName);
}

[UnmanagedCallersOnly]
private static unsafe void ResolveUnmanagedDllUsingEvent(string* pUnmanagedDllName, Assembly* pAssembly, IntPtr gchAssemblyLoadContext, IntPtr* pResult, Exception* pException)
{
try
{
*pResult = ResolveUnmanagedDllUsingEvent(*pUnmanagedDllName, *pAssembly, gchAssemblyLoadContext);
}
catch (Exception ex)
{
*pException = ex;
}
}

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "AssemblyNative_GetLoadContextForAssembly")]
private static partial IntPtr GetLoadContextForAssembly(QCallAssembly assembly);

Expand Down Expand Up @@ -191,6 +217,19 @@ private static void StartAssemblyLoad(ref Guid activityId, ref Guid relatedActiv
ActivityTracker.Instance.OnStart(NativeRuntimeEventSource.Log.Name, AssemblyLoadName, 0, ref activityId, ref relatedActivityId, EventActivityOptions.Recursive, useTplSource: false);
}

[UnmanagedCallersOnly]
private static unsafe void StartAssemblyLoad(Guid* activityId, Guid* relatedActivityId, Exception* pException)
{
try
{
StartAssemblyLoad(ref *activityId, ref *relatedActivityId);
}
catch (Exception ex)
{
*pException = ex;
}
}

/// <summary>
/// Called by the runtime to stop an assembly load activity for tracing
/// </summary>
Expand All @@ -200,12 +239,38 @@ private static void StopAssemblyLoad(ref Guid activityId)
ActivityTracker.Instance.OnStop(NativeRuntimeEventSource.Log.Name, AssemblyLoadName, 0, ref activityId, useTplSource: false);
}

[UnmanagedCallersOnly]
private static unsafe void StopAssemblyLoad(Guid* activityId, Exception* pException)
{
try
{
StopAssemblyLoad(ref *activityId);
}
catch (Exception ex)
{
*pException = ex;
}
}

/// <summary>
/// Called by the runtime to make sure the default ALC is initialized
/// </summary>
private static void InitializeDefaultContext()
{
_ = Default;
}

[UnmanagedCallersOnly]
private static unsafe void InitializeDefaultContext(Exception* pException)
{
try
{
InitializeDefaultContext();
}
catch (Exception ex)
{
*pException = ex;
}
}
}
}
26 changes: 26 additions & 0 deletions src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ internal static unsafe IntPtr ConvertToNative(string strManaged, IntPtr pNativeB
}
}

[UnmanagedCallersOnly]
private static unsafe void ConvertToNative(string* pStr, IntPtr pNativeBuffer, IntPtr* pResult, Exception* pException)
{
try
{
*pResult = ConvertToNative(*pStr, pNativeBuffer);
}
catch (Exception ex)
{
*pException = ex;
}
}

internal static unsafe string? ConvertToManaged(IntPtr bstr)
{
if (IntPtr.Zero == bstr)
Expand Down Expand Up @@ -362,6 +375,19 @@ internal static unsafe IntPtr ConvertToNative(string strManaged, IntPtr pNativeB
}
}

[UnmanagedCallersOnly]
private static unsafe void ConvertToManaged(IntPtr bstr, string* pResult, Exception* pException)
{
try
{
*pResult = ConvertToManaged(bstr);
}
catch (Exception ex)
{
*pException = ex;
}
}

internal static void ClearNative(IntPtr pNative)
{
Marshal.FreeBSTR(pNative);
Expand Down
Loading
Loading