Skip to content

Commit

Permalink
cached marshaled exception (#1263)
Browse files Browse the repository at this point in the history
* synchronous exception handler fix, cached marshaled exception

* Remove all error reporting from projection and to client code like microsoft.ui.xaml.dll
  • Loading branch information
Scottj1s authored Nov 1, 2022
1 parent 2a4876a commit 8435845
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/WinRT.Runtime/ExceptionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ static void Throw(int hr)
}
}

// Retrieve restricted error info from thread without removing it, for propagation and debugging (watch, locals, etc)
private static IObjectReference BorrowRestrictedErrorInfo()
{
if (getRestrictedErrorInfo == null)
return null;

Marshal.ThrowExceptionForHR(getRestrictedErrorInfo(out IntPtr restrictedErrorInfoPtr));
if (restrictedErrorInfoPtr == IntPtr.Zero)
return null;

if (setRestrictedErrorInfo != null)
{
setRestrictedErrorInfo(restrictedErrorInfoPtr);
return ObjectReference<ABI.WinRT.Interop.IRestrictedErrorInfo.Vftbl>.FromAbi(restrictedErrorInfoPtr);
}

return ObjectReference<ABI.WinRT.Interop.IRestrictedErrorInfo.Vftbl>.Attach(ref restrictedErrorInfoPtr);
}

public static Exception GetExceptionForHR(int hr) => hr >= 0 ? null : GetExceptionForHR(hr, true, out _);

private static Exception GetExceptionForHR(int hr, bool useGlobalErrorState, out bool restoredExceptionFromGlobalState)
Expand All @@ -99,15 +118,12 @@ private static Exception GetExceptionForHR(int hr, bool useGlobalErrorState, out
string restrictedCapabilitySid = null;
bool hasOtherLanguageException = false;

if (useGlobalErrorState && getRestrictedErrorInfo != null)
if (useGlobalErrorState)
{
Marshal.ThrowExceptionForHR(getRestrictedErrorInfo(out IntPtr restrictedErrorInfoPtr));

if (restrictedErrorInfoPtr != IntPtr.Zero)
using var restrictedErrorInfoRef = BorrowRestrictedErrorInfo();
if (restrictedErrorInfoRef != null)
{
IObjectReference restrictedErrorInfoRef = ObjectReference<ABI.WinRT.Interop.IRestrictedErrorInfo.Vftbl>.Attach(ref restrictedErrorInfoPtr);
restrictedErrorInfoToSave = restrictedErrorInfoRef.As<ABI.WinRT.Interop.IRestrictedErrorInfo.Vftbl>();

ABI.WinRT.Interop.IRestrictedErrorInfo restrictedErrorInfo = new ABI.WinRT.Interop.IRestrictedErrorInfo(restrictedErrorInfoRef);
restrictedErrorInfo.GetErrorDetails(out description, out int hrLocal, out restrictedError, out restrictedCapabilitySid);
restrictedErrorReference = restrictedErrorInfo.GetReference();
Expand Down Expand Up @@ -230,11 +246,13 @@ public static unsafe void SetErrorInfo(Exception ex)
public static void ReportUnhandledError(Exception ex)
{
SetErrorInfo(ex);
if (getRestrictedErrorInfo != null && roReportUnhandledError != null)
if (roReportUnhandledError != null)
{
Marshal.ThrowExceptionForHR(getRestrictedErrorInfo(out IntPtr ppRestrictedErrorInfo));
using var restrictedErrorRef = ObjectReference<IUnknownVftbl>.Attach(ref ppRestrictedErrorInfo);
roReportUnhandledError(restrictedErrorRef.ThisPtr);
var restrictedErrorInfoRef = BorrowRestrictedErrorInfo();
if (restrictedErrorInfoRef != null)
{
roReportUnhandledError(restrictedErrorInfoRef.ThisPtr);
}
}
}

Expand Down

0 comments on commit 8435845

Please sign in to comment.