Skip to content

Commit e87544a

Browse files
Enable AOT and platform compatibility analyzers. (#340)
* Mark unmanaged reader/writer APIs as supported only on Windows. * Enable AOT compatibility analyzer. This required to add a trim-friendly version of the COM registry activation path. * Use `ComInterfaceMarshaller` as marshaller. * Add `dotnet11` NuGet feed. * Suppress platform compatibility warnings for test code that already runs only on Windows. * Update src/Microsoft.DiaSymReader/SymUnmanagedFactory.cs * Revert "Update src/Microsoft.DiaSymReader/SymUnmanagedFactory.cs" This reverts commit 8ecedb2. * Do not check for negative `HRESULT`.
1 parent 029a1d1 commit e87544a

8 files changed

Lines changed: 51 additions & 21 deletions

File tree

NuGet.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<add key="dotnet-public" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json" />
66
<add key="dotnet-eng" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json" />
77
<add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
8+
<add key="dotnet11" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet11/nuget/v3/index.json" />
89
</packageSources>
910
<disabledPackageSources>
1011
<clear />

src/Microsoft.DiaSymReader.Native.Tests/SymUnmanagedFactoryNativeTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the License.txt file in the project root for more information.
44

5+
// Tests are skipped on non-Windows already.
6+
#pragma warning disable CA1416 // Validate platform compatibility
7+
58
using System;
69
using System.IO;
710
using Microsoft.DiaSymReader.Tools;

src/Microsoft.DiaSymReader.Tests/SymUnmanagedFactoryTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the License.txt file in the project root for more information.
44

5+
// Tests are skipped on non-Windows already.
6+
#pragma warning disable CA1416 // Validate platform compatibility
7+
58
using System;
69
using System.IO;
710
using System.Reflection;

src/Microsoft.DiaSymReader.Tests/SymUnmanagedWriterTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the License.txt file in the project root for more information.
44

5+
// Tests are skipped on non-Windows already.
6+
#pragma warning disable CA1416 // Validate platform compatibility
7+
58
using System;
69
using System.Collections.Generic;
710
using System.Diagnostics;

src/Microsoft.DiaSymReader/Microsoft.DiaSymReader.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<Description>Microsoft DiaSymReader interop interfaces and utilities.</Description>
99
<PackageReleaseNotes>Microsoft DiaSymReader interop interfaces and utilities</PackageReleaseNotes>
1010
<PackageTags>DiaSymReader ISymUnmanagedReader PDB COM interop debugging</PackageTags>
11+
<IsAotCompatible Condition="'$(TargetFramework)' == '$(NetCurrent)'">true</IsAotCompatible>
1112
</PropertyGroup>
1213

1314
<ItemGroup>

src/Microsoft.DiaSymReader/Reader/SymUnmanagedReaderFactory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
namespace Microsoft.DiaSymReader
1010
{
11+
#if NET
12+
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
13+
#endif
1114
public static class SymUnmanagedReaderFactory
1215
{
1316
/// <summary>

src/Microsoft.DiaSymReader/SymUnmanagedFactory.cs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -21,6 +21,9 @@
2121

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

46+
#if NET
47+
private const string IUnknownIid = "00000000-0000-0000-C000-000000000046";
48+
#else
4349
private static Type s_lazySymReaderComType, s_lazySymWriterComType;
50+
#endif
4451

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

109+
#if NET
110+
[LibraryImport("Ole32")]
111+
private static unsafe partial int CoCreateInstance(in Guid rclsid, void* pUnkOuter, int dwClsContext, in Guid riid, [MarshalUsing(typeof(ComInterfaceMarshaller<object>))] out object ppObj);
112+
#endif
113+
102114
// internal for testing
103115
internal static string GetEnvironmentVariable(string name)
104116
{
@@ -140,7 +152,7 @@ private static unsafe object TryLoadFromAlternativePath(Guid clsid, bool createR
140152
var creator = Marshal.GetDelegateForFunctionPointer<NativeFactory>(createAddress);
141153
creator(ref clsid, out instance);
142154
#else
143-
var creator = (delegate*unmanaged<Guid*, IntPtr*, void>)createAddress;
155+
var creator = (delegate* unmanaged<Guid*, IntPtr*, void>)createAddress;
144156
IntPtr rawInstance = default;
145157
creator(&clsid, &rawInstance);
146158
instance = createReader
@@ -159,22 +171,24 @@ private static unsafe object TryLoadFromAlternativePath(Guid clsid, bool createR
159171
return instance;
160172
}
161173

162-
private static Type GetComTypeType(ref Type lazyType, Guid clsid)
174+
#if NET
175+
private static unsafe object ActivateClass(Guid clsid)
163176
{
164-
if (lazyType == null)
177+
int hr = CoCreateInstance(in clsid, null, 1, new Guid(IUnknownIid), out object instance);
178+
Marshal.ThrowExceptionForHR(hr);
179+
return instance;
180+
}
181+
#else
182+
private static object ActivateClass(ref Type lazyType, Guid clsid)
183+
{
184+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
165185
{
166-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
167-
{
168-
lazyType = Marshal.GetTypeFromCLSID(clsid);
169-
}
170-
else
171-
{
172-
throw new NotSupportedException("COM lookup is not supported");
173-
}
186+
throw new PlatformNotSupportedException("COM lookup is not supported");
174187
}
175-
176-
return lazyType;
188+
lazyType ??= Marshal.GetTypeFromCLSID(clsid);
189+
return Activator.CreateInstance(lazyType);
177190
}
191+
#endif
178192

179193
internal static unsafe object CreateObject(bool createReader, bool useAlternativeLoadPath, bool useComRegistry, out string moduleName, out Exception loadException)
180194
{
@@ -265,11 +279,11 @@ internal static unsafe object CreateObject(bool createReader, bool useAlternativ
265279
// Try to find a registered CLR implementation
266280
try
267281
{
268-
var comType = createReader ?
269-
GetComTypeType(ref s_lazySymReaderComType, clsid) :
270-
GetComTypeType(ref s_lazySymWriterComType, clsid);
271-
272-
instance = Activator.CreateInstance(comType);
282+
#if NET
283+
instance = ActivateClass(clsid);
284+
#else
285+
instance = ActivateClass(ref createReader ? ref s_lazySymReaderComType : ref s_lazySymWriterComType, clsid);
286+
#endif
273287
moduleName = LegacyDiaSymReaderModuleName;
274288
}
275289
catch (Exception e)
@@ -281,6 +295,5 @@ internal static unsafe object CreateObject(bool createReader, bool useAlternativ
281295

282296
return instance;
283297
}
284-
285298
}
286299
}

src/Microsoft.DiaSymReader/Writer/SymUnmanagedWriterFactory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace Microsoft.DiaSymReader
99
{
10+
#if NET
11+
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
12+
#endif
1013
public static class SymUnmanagedWriterFactory
1114
{
1215
/// <summary>

0 commit comments

Comments
 (0)