Skip to content

Commit

Permalink
Use reflection or a compile-time only shim assembly to reference unex…
Browse files Browse the repository at this point in the history
…posed corelib types. (#61802)

Co-authored-by: Aaron Robinson <[email protected]>
  • Loading branch information
jkoritzinsky and AaronRobinsonMSFT authored Nov 20, 2021
1 parent 06a0c76 commit 7099f4b
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 122 deletions.
2 changes: 0 additions & 2 deletions docs/workflow/testing/coreclr/test-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ Therefore the managed portion of each test **must not contain**:
* Exclude test from JIT stress runs runs by adding the following to the csproj:
* `<JitOptimizationSensitive>true</JitOptimizationSensitive>`
* Add NuGet references by updating the following [test project](https://github.com/dotnet/runtime/blob/main/src/tests/Common/test_dependencies/test_dependencies.csproj).
* Get access to System.Private.CoreLib types and methods that are not exposed via public surface by adding the following to the csproj:
* `<ReferenceSystemPrivateCoreLib>true</ReferenceSystemPrivateCoreLib>`
* Any System.Private.CoreLib types and methods used by tests must be available for building on all platforms.
This means there must be enough implementation for the C# compiler to find the referenced types and methods. Unsupported target platforms
should simply `throw new PlatformNotSupportedException()` in its dummy method implementations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public unsafe struct ComActivationContextInternal
//

[StructLayout(LayoutKind.Sequential)]
public partial struct ComActivationContext
internal partial struct ComActivationContext
{
public Guid ClassId;
public Guid InterfaceId;
Expand All @@ -38,7 +38,7 @@ public partial struct ComActivationContext
[ComVisible(false)]
[Guid("00000001-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IClassFactory
internal interface IClassFactory
{
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
void CreateInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,24 @@ public static class ComActivator
/// Internal entry point for unmanaged COM activation API from native code
/// </summary>
/// <param name="pCxtInt">Pointer to a <see cref="ComActivationContextInternal"/> instance</param>
[CLSCompliant(false)]
[UnmanagedCallersOnly]
public static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInternal* pCxtInt)
private static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInternal* pCxtInt)
=> throw new PlatformNotSupportedException();

/// <summary>
/// Internal entry point for registering a managed COM server API from native code
/// </summary>
/// <param name="pCxtInt">Pointer to a <see cref="ComActivationContextInternal"/> instance</param>
[CLSCompliant(false)]
[UnmanagedCallersOnly]
public static unsafe int RegisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
private static unsafe int RegisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
=> throw new PlatformNotSupportedException();

/// <summary>
/// Internal entry point for unregistering a managed COM server API from native code
/// </summary>
/// <param name="pCxtInt">Pointer to a <see cref="ComActivationContextInternal"/> instance</param>
[CLSCompliant(false)]
[UnmanagedCallersOnly]
public static unsafe int UnregisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
=> throw new PlatformNotSupportedException();

// Exists here to allow tests to build on any platform.
public static object GetClassFactoryForType(ComActivationContext cxt)
=> throw new PlatformNotSupportedException();

// Exists here to allow tests to build on any platform.
public static void ClassRegistrationScenarioForType(ComActivationContext cxt, bool register)
private static unsafe int UnregisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
=> throw new PlatformNotSupportedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ void CreateInstanceLic(
out IntPtr ppvObject);
}

public partial struct ComActivationContext
internal partial struct ComActivationContext
{
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
[CLSCompliant(false)]
public static unsafe ComActivationContext Create(ref ComActivationContextInternal cxtInt)
{
if (!Marshal.IsBuiltInComSupported)
Expand Down Expand Up @@ -91,7 +90,7 @@ public static class ComActivator
/// </summary>
/// <param name="cxt">Reference to a <see cref="ComActivationContext"/> instance</param>
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
public static object GetClassFactoryForType(ComActivationContext cxt)
private static object GetClassFactoryForType(ComActivationContext cxt)
{
if (!Marshal.IsBuiltInComSupported)
{
Expand Down Expand Up @@ -125,7 +124,7 @@ public static object GetClassFactoryForType(ComActivationContext cxt)
/// <param name="cxt">Reference to a <see cref="ComActivationContext"/> instance</param>
/// <param name="register">true if called for register or false to indicate unregister</param>
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
public static void ClassRegistrationScenarioForType(ComActivationContext cxt, bool register)
private static void ClassRegistrationScenarioForType(ComActivationContext cxt, bool register)
{
if (!Marshal.IsBuiltInComSupported)
{
Expand Down Expand Up @@ -219,9 +218,8 @@ public static void ClassRegistrationScenarioForType(ComActivationContext cxt, bo
/// </summary>
/// <param name="pCxtInt">Pointer to a <see cref="ComActivationContextInternal"/> instance</param>
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
[CLSCompliant(false)]
[UnmanagedCallersOnly]
public static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInternal* pCxtInt)
private static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInternal* pCxtInt)
{
if (!Marshal.IsBuiltInComSupported)
{
Expand Down Expand Up @@ -262,9 +260,8 @@ public static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInte
/// </summary>
/// <param name="pCxtInt">Pointer to a <see cref="ComActivationContextInternal"/> instance</param>
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
[CLSCompliant(false)]
[UnmanagedCallersOnly]
public static unsafe int RegisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
private static unsafe int RegisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
{
if (!Marshal.IsBuiltInComSupported)
{
Expand Down Expand Up @@ -308,9 +305,8 @@ public static unsafe int RegisterClassForTypeInternal(ComActivationContextIntern
/// Internal entry point for unregistering a managed COM server API from native code
/// </summary>
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
[CLSCompliant(false)]
[UnmanagedCallersOnly]
public static unsafe int UnregisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
private static unsafe int UnregisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
{
if (!Marshal.IsBuiltInComSupported)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<CompilerVisibleProperty Include="TargetArchitecture" />
<CompilerVisibleProperty Include="Priority" />
<!-- Properties that influence test harness generation -->
<CompilerVisibleProperty Include="ReferenceSystemPrivateCoreLib" />
<CompilerVisibleProperty Include="IsMergedTestRunnerAssembly" />
<CompilerVisibleProperty Include="TestFilter" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="IsOutOfProcessTestAssembly" />
Expand Down
28 changes: 0 additions & 28 deletions src/tests/Common/override.targets

This file was deleted.

12 changes: 2 additions & 10 deletions src/tests/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
<_WillCLRTestProjectBuild Condition="'$(BuildAllProjects)' != 'true'">true</_WillCLRTestProjectBuild>
<_WillCLRTestProjectBuild Condition="'$(BuildAllProjects)' == 'true' And '$(CLRTestPriority)' &lt;= '$(CLRTestPriorityToBuild)'">true</_WillCLRTestProjectBuild>
<_WillCLRTestProjectBuild Condition="'$(CLRTestBuildAllTargets)' != 'allTargets' And '$(CLRTestTargetUnsupported)' == 'true'">false</_WillCLRTestProjectBuild>
<_WillCLRTestProjectBuild Condition="'$(ReferenceSystemPrivateCoreLib)' == 'true' and '$(RuntimeFlavor)' == 'mono'">false</_WillCLRTestProjectBuild>
<_WillCLRTestProjectBuild Condition="'$(DisableProjectBuild)' == 'true'">false</_WillCLRTestProjectBuild>
</PropertyGroup>
<PropertyGroup>
Expand All @@ -95,8 +94,6 @@
<!-- RunOnly projects have a special build for dependent projects -->
<Import Project="$(MSBuildThisFileDirectory)Common\runonly.targets" Condition="'$(CLRTestKind)' == 'RunOnly'" />

<Import Project="$(MSBuildThisFileDirectory)Common\override.targets" />

<!-- We enable auto-unification of assembly references after importing the common targets. Binding redirects are not needed
for coreclr since it auto-unifies, so the warnings we get without this setting are just noise -->
<PropertyGroup>
Expand Down Expand Up @@ -225,12 +222,11 @@
BeforeTargets="BeforeResolveReferences"
>
<MSBuild Projects="$(MSBuildProjectFullPath)"
Targets="GetLiveRefAssemblies"
Condition="'$(ReferenceSystemPrivateCoreLib)' != 'true'">
Targets="GetLiveRefAssemblies">
<Output TaskParameter="TargetOutputs" ItemName="Reference" />
</MSBuild>

<ItemGroup Condition="'$(ReferenceSystemPrivateCoreLib)' != 'true'">
<ItemGroup >
<Reference Include="$(TargetingPackPath)/*.dll" >
<Private>false</Private>
</Reference>
Expand All @@ -255,10 +251,6 @@
<ProjectAssetsFile>$(BaseOutputPath)\packages\Common\test_dependencies\test_dependencies\project.assets.json</ProjectAssetsFile>
</PropertyGroup>

<PropertyGroup Condition="'$(ReferenceSystemPrivateCoreLib)' == 'true' and '$(UsingMicrosoftNETSdk)' != 'true'">
<ProjectAssetsFile></ProjectAssetsFile>
</PropertyGroup>

<PropertyGroup>
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
</PropertyGroup>
Expand Down
5 changes: 3 additions & 2 deletions src/tests/Interop/COM/Activator/Activator.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<!-- Internal.Runtime.InteropServices is CoreCLR-only -->
<ReferenceSystemPrivateCoreLib>true</ReferenceSystemPrivateCoreLib>
<RequiresMockHostPolicy>true</RequiresMockHostPolicy>
<!-- The test fails casting from ClassFromA from the default ALC to type IGetTypeFromC from a custom ALC -->
<UnloadabilityIncompatible>true</UnloadabilityIncompatible>
<DisableProjectBuild Condition="'$(RuntimeFlavor)' == 'Mono'">true</DisableProjectBuild>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="ComActivationContext.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NETServer\NETServer.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<!-- Internal.Runtime.InteropServices is CoreCLR-only -->
<ReferenceSystemPrivateCoreLib>true</ReferenceSystemPrivateCoreLib>
<RequiresMockHostPolicy>true</RequiresMockHostPolicy>
<!-- The test fails casting from ClassFromA from the default ALC to type IGetTypeFromC from a custom ALC -->
<UnloadabilityIncompatible>true</UnloadabilityIncompatible>
<DisableProjectBuild Condition="'$(RuntimeFlavor)' == 'Mono'">true</DisableProjectBuild>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="ComActivationContext.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NETServer\NETServer.csproj" />
Expand Down
19 changes: 19 additions & 0 deletions src/tests/Interop/COM/Activator/ComActivationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Activator
{
[StructLayout(LayoutKind.Sequential)]
public partial struct ComActivationContext
{
public Guid ClassId;
public Guid InterfaceId;
public string AssemblyPath;
public string AssemblyName;
public string TypeName;
}
}
Loading

0 comments on commit 7099f4b

Please sign in to comment.