Skip to content

Commit

Permalink
Minor improvements to error info (#1331)
Browse files Browse the repository at this point in the history
* Fix issue when lanaguage exception is proxied, error info is not set and minor improvements

* Handle scenario where Net core fills in same fields as us.

* Move to using static function for hashing
  • Loading branch information
manodasanW authored Jun 8, 2023
1 parent 0afca0f commit 0e91479
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
31 changes: 16 additions & 15 deletions src/WinRT.Runtime/ExceptionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static unsafe class ExceptionHelpers
[DllImport("oleaut32.dll")]
private static extern int SetErrorInfo(uint dwReserved, IntPtr perrinfo);

private static delegate* unmanaged[Stdcall]<out IntPtr, int> getRestrictedErrorInfo;
private static delegate* unmanaged[Stdcall]<IntPtr*, int> getRestrictedErrorInfo;
private static delegate* unmanaged[Stdcall]<IntPtr, int> setRestrictedErrorInfo;
private static delegate* unmanaged[Stdcall]<int, IntPtr, IntPtr, int> roOriginateLanguageException;
private static delegate* unmanaged[Stdcall]<IntPtr, int> roReportUnhandledError;
Expand All @@ -56,7 +56,7 @@ private static bool Initialize()

if (winRTErrorModule != IntPtr.Zero)
{
getRestrictedErrorInfo = (delegate* unmanaged[Stdcall]<out IntPtr, int>)Platform.GetProcAddress(winRTErrorModule, "GetRestrictedErrorInfo");
getRestrictedErrorInfo = (delegate* unmanaged[Stdcall]<IntPtr*, int>)Platform.GetProcAddress(winRTErrorModule, "GetRestrictedErrorInfo");
setRestrictedErrorInfo = (delegate* unmanaged[Stdcall]<IntPtr, int>)Platform.GetProcAddress(winRTErrorModule, "SetRestrictedErrorInfo");
}

Expand Down Expand Up @@ -90,7 +90,8 @@ private static IObjectReference BorrowRestrictedErrorInfo()
if (getRestrictedErrorInfo == null)
return null;

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

Expand Down Expand Up @@ -144,16 +145,15 @@ private static Exception GetExceptionForHR(int hr, bool useGlobalErrorState, out
}
else
{
// This could also be a proxy to a managed exception.
hasOtherLanguageException = true;
}
}
}
else

if (hr == hrLocal)
{
if (hr == hrLocal)
{
restrictedErrorInfoRef.TryAs<ABI.WinRT.Interop.IErrorInfo.Vftbl>(out iErrorInfo);
}
restrictedErrorInfoRef.TryAs<ABI.WinRT.Interop.IErrorInfo.Vftbl>(out iErrorInfo);
}
}
}
Expand Down Expand Up @@ -309,15 +309,15 @@ internal static void AddExceptionDataForRestrictedErrorInfo(
IDictionary dict = ex.Data;
if (dict != null)
{
dict.Add("Description", description);
dict.Add("RestrictedDescription", restrictedError);
dict.Add("RestrictedErrorReference", restrictedErrorReference);
dict.Add("RestrictedCapabilitySid", restrictedCapabilitySid);
dict["Description"] = description;
dict["RestrictedDescription"] = restrictedError;
dict["RestrictedErrorReference"] = restrictedErrorReference;
dict["RestrictedCapabilitySid"] = restrictedCapabilitySid;

// Keep the error object alive so that user could retrieve error information
// using Data["RestrictedErrorReference"]
dict.Add("__RestrictedErrorObjectReference", restrictedErrorObject == null ? null : new __RestrictedErrorObject(restrictedErrorObject));
dict.Add("__HasRestrictedLanguageErrorObject", hasRestrictedLanguageErrorObject);
dict["__RestrictedErrorObjectReference"] = restrictedErrorObject == null ? null : new __RestrictedErrorObject(restrictedErrorObject);
dict["__HasRestrictedLanguageErrorObject"] = hasRestrictedLanguageErrorObject;
}
}

Expand Down Expand Up @@ -353,7 +353,8 @@ public static Exception AttachRestrictedErrorInfo(Exception e)
// HRESULT ABI return values. However, in many cases async APIs will set the thread's restricted
// error info as a convention in order to provide extended debugging information for the ErrorCode
// property.
Marshal.ThrowExceptionForHR(getRestrictedErrorInfo(out IntPtr restrictedErrorInfoPtr));
IntPtr restrictedErrorInfoPtr = IntPtr.Zero;
Marshal.ThrowExceptionForHR(getRestrictedErrorInfo(&restrictedErrorInfoPtr));

if (restrictedErrorInfoPtr != IntPtr.Zero)
{
Expand Down
13 changes: 8 additions & 5 deletions src/WinRT.Runtime/GuidGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,22 @@ public static Guid CreateIID(Type type)
}
#if !NET
var data = wrt_pinterface_namespace.ToByteArray().Concat(UTF8Encoding.UTF8.GetBytes(sig)).ToArray();

using (SHA1 sha = new SHA1CryptoServiceProvider())
{
return encode_guid(sha.ComputeHash(data));
}
#else
var maxBytes = UTF8Encoding.UTF8.GetMaxByteCount(sig.Length);

var data = new byte[16 /* Number of bytes in a GUID */ + maxBytes];
Span<byte> dataSpan = data;
wrt_pinterface_namespace.TryWriteBytes(dataSpan);
var numBytes = UTF8Encoding.UTF8.GetBytes(sig, dataSpan[16..]);
data = data[..(16 + numBytes)];
data = data[..(16 + numBytes)];

return encode_guid(SHA1.HashData(data));
#endif
using (SHA1 sha = new SHA1CryptoServiceProvider())
{
return encode_guid(sha.ComputeHash(data));
}
}
}
}
3 changes: 0 additions & 3 deletions src/WinRT.Runtime/Interop/ExceptionErrorInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,6 @@ internal unsafe class IRestrictedErrorInfo : global::WinRT.Interop.IRestrictedEr
[Guid("82BA7092-4C88-427D-A7BC-16DD93FEB67E")]
public struct Vftbl
{
public delegate int _GetErrorDetails(IntPtr thisPtr, out IntPtr description, out int error, out IntPtr restrictedDescription, out IntPtr capabilitySid);
public delegate int _GetReference(IntPtr thisPtr, out IntPtr reference);

internal global::WinRT.Interop.IUnknownVftbl unknownVftbl;
private void* _GetErrorDetails_0;
public delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int*, IntPtr*, IntPtr*, int> GetErrorDetails_0 => (delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int*, IntPtr*, IntPtr*, int>)_GetErrorDetails_0;
Expand Down

0 comments on commit 0e91479

Please sign in to comment.