Skip to content

Enable AOT and platform compatibility analyzers. #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion src/Microsoft.DiaSymReader/Microsoft.DiaSymReader.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetCurrent)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand All @@ -7,6 +7,7 @@
<Description>Microsoft DiaSymReader interop interfaces and utilities.</Description>
<PackageReleaseNotes>Microsoft DiaSymReader interop interfaces and utilities</PackageReleaseNotes>
<PackageTags>DiaSymReader ISymUnmanagedReader PDB COM interop debugging</PackageTags>
<IsAotCompatible Condition="'$(TargetFramework)' == '$(NetCurrent)'">true</IsAotCompatible>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="Microsoft.DiaSymReader.PortablePdb" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace Microsoft.DiaSymReader
{
#if NET
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public static class SymUnmanagedReaderFactory
{
/// <summary>
Expand Down
56 changes: 36 additions & 20 deletions src/Microsoft.DiaSymReader/SymUnmanagedFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand All @@ -21,6 +21,9 @@

namespace Microsoft.DiaSymReader
{
#if NET
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
internal static partial class SymUnmanagedFactory
{
private const string AlternativeLoadPathEnvironmentVariableName = "MICROSOFT_DIASYMREADER_NATIVE_ALT_LOAD_PATH";
Expand All @@ -40,7 +43,11 @@ internal static partial class SymUnmanagedFactory
// CorSymReader_SxS from corsym.idl
private const string SymReaderClsid = "0A3976C5-4529-4ef8-B0B0-42EED37082CD";

#if NET
private const string IUnknownIid = "00000000-0000-0000-C000-000000000046";
#else
private static Type s_lazySymReaderComType, s_lazySymWriterComType;
#endif

internal static string DiaSymReaderModuleName
=> RuntimeInformation.ProcessArchitecture switch
Expand Down Expand Up @@ -99,6 +106,11 @@ internal static string DiaSymReaderModuleName
private delegate void NativeFactory(ref Guid id, [MarshalAs(UnmanagedType.IUnknown)] out object instance);
#endif

#if NET
[LibraryImport("Ole32")]
private static unsafe partial int CoCreateInstance(in Guid rclsid, void* pUnkOuter, int dwClsContext, in Guid riid, out void* ppObj);
#endif

// internal for testing
internal static string GetEnvironmentVariable(string name)
{
Expand Down Expand Up @@ -140,7 +152,7 @@ private static unsafe object TryLoadFromAlternativePath(Guid clsid, bool createR
var creator = Marshal.GetDelegateForFunctionPointer<NativeFactory>(createAddress);
creator(ref clsid, out instance);
#else
var creator = (delegate*unmanaged<Guid*, IntPtr*, void>)createAddress;
var creator = (delegate* unmanaged<Guid*, IntPtr*, void>)createAddress;
IntPtr rawInstance = default;
creator(&clsid, &rawInstance);
instance = createReader
Expand All @@ -159,22 +171,27 @@ private static unsafe object TryLoadFromAlternativePath(Guid clsid, bool createR
return instance;
}

private static Type GetComTypeType(ref Type lazyType, Guid clsid)
#if NET
private static unsafe object ActivateClass(Guid clsid)
{
if (lazyType == null)
int hr = CoCreateInstance(in clsid, null, 1, new Guid(IUnknownIid), out void* rawInstance);
if (hr < 0)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
lazyType = Marshal.GetTypeFromCLSID(clsid);
}
else
{
throw new NotSupportedException("COM lookup is not supported");
}
Marshal.ThrowExceptionForHR(hr);
}

return lazyType;
return ComInterfaceMarshaller<object>.ConvertToManaged(rawInstance);
}
#else
private static object ActivateClass(ref Type lazyType, Guid clsid)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
throw new PlatformNotSupportedException("COM lookup is not supported");
}
lazyType ??= Marshal.GetTypeFromCLSID(clsid);
return Activator.CreateInstance(lazyType);
}
#endif

internal static unsafe object CreateObject(bool createReader, bool useAlternativeLoadPath, bool useComRegistry, out string moduleName, out Exception loadException)
{
Expand Down Expand Up @@ -265,11 +282,11 @@ internal static unsafe object CreateObject(bool createReader, bool useAlternativ
// Try to find a registered CLR implementation
try
{
var comType = createReader ?
GetComTypeType(ref s_lazySymReaderComType, clsid) :
GetComTypeType(ref s_lazySymWriterComType, clsid);

instance = Activator.CreateInstance(comType);
#if NET
instance = ActivateClass(clsid);
#else
instance = ActivateClass(ref createReader ? ref s_lazySymReaderComType : ref s_lazySymWriterComType, clsid);
#endif
moduleName = LegacyDiaSymReaderModuleName;
}
catch (Exception e)
Expand All @@ -281,6 +298,5 @@ internal static unsafe object CreateObject(bool createReader, bool useAlternativ

return instance;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace Microsoft.DiaSymReader
{
#if NET
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public static class SymUnmanagedWriterFactory
{
/// <summary>
Expand Down
Loading