Skip to content
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

Fix visibility checks when generating proxies based on internal interfaces #804

Merged
merged 2 commits into from
Jun 10, 2022
Merged
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
30 changes: 26 additions & 4 deletions src/StreamJsonRpc/SkipClrVisibilityChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,15 @@ internal static ImmutableHashSet<AssemblyName> GetSkipVisibilityChecksRequiremen
CheckForNonPublicTypes(typeInfo, assembliesDeclaringInternalTypes, visitedTypes);

// Enumerate members on the interface that we're going to need to implement.
foreach (MethodInfo methodInfo in typeInfo.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy))
foreach (TypeInfo iteratedTypeInfo in ThisAndBaseTypes(typeInfo))
{
CheckForNonPublicTypes(methodInfo.ReturnType.GetTypeInfo(), assembliesDeclaringInternalTypes, visitedTypes);
foreach (ParameterInfo parameter in methodInfo.GetParameters())
foreach (MethodInfo methodInfo in iteratedTypeInfo.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
CheckForNonPublicTypes(parameter.ParameterType.GetTypeInfo(), assembliesDeclaringInternalTypes, visitedTypes);
CheckForNonPublicTypes(methodInfo.ReturnType.GetTypeInfo(), assembliesDeclaringInternalTypes, visitedTypes);
foreach (ParameterInfo parameter in methodInfo.GetParameters())
{
CheckForNonPublicTypes(parameter.ParameterType.GetTypeInfo(), assembliesDeclaringInternalTypes, visitedTypes);
}
}
}

Expand Down Expand Up @@ -131,6 +134,25 @@ internal void SkipVisibilityChecksFor(AssemblyName assemblyName)
}
}

private static IEnumerable<TypeInfo> ThisAndBaseTypes(TypeInfo startingPoint)
{
if (startingPoint.IsInterface)
{
yield return startingPoint.GetTypeInfo();
foreach (Type iface in startingPoint.GetInterfaces())
{
yield return iface.GetTypeInfo();
}
}
else
{
for (TypeInfo? t = startingPoint.GetTypeInfo(); t is not null && t != typeof(object).GetTypeInfo(); t = t.BaseType?.GetTypeInfo())
{
yield return t;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to recurse into interfaces implemented by these base types or do we only care about the interfaces declared directly by the original starting point?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know, I copied this code from another place in the library without thinking. This code will never be hit because proxies are always based on interfaces, not classes. I'll delete.

}
}
}

private static void CheckForNonPublicTypes(TypeInfo typeInfo, ImmutableHashSet<AssemblyName>.Builder assembliesDeclaringInternalTypes, HashSet<TypeInfo> visitedTypes)
{
Requires.NotNull(typeInfo, nameof(typeInfo));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace StreamJsonRpc.Tests.ExternalAssembly
{
using System.Threading.Tasks;

internal interface IInternalGenericInterface<TOptions>
{
Task<TOptions> GetOptionsAsync(InternalStruct id, CancellationToken cancellationToken);
}
}
9 changes: 9 additions & 0 deletions test/StreamJsonRpc.Tests.ExternalAssembly/InternalStruct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace StreamJsonRpc.Tests.ExternalAssembly
{
internal struct InternalStruct
{
}
}
34 changes: 25 additions & 9 deletions test/StreamJsonRpc.Tests/JsonRpcProxyGenerationTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft;
using Microsoft.VisualStudio.Threading;
using Nerdbank;
using StreamJsonRpc;
using Xunit;
using Xunit.Abstractions;
using ExAssembly = StreamJsonRpc.Tests.ExternalAssembly;

public class JsonRpcProxyGenerationTests : TestBase
{
Expand Down Expand Up @@ -137,14 +134,27 @@ public interface IServerWithGenericMethod
Task AddAsync<T>(T a, T b);
}

internal interface IServerInternal : StreamJsonRpc.Tests.ExternalAssembly.ISomeInternalProxyInterface, IServerInternalWithInternalTypesFromOtherAssemblies
internal interface IServerInternal : ExAssembly.ISomeInternalProxyInterface, IServerInternalWithInternalTypesFromOtherAssemblies
{
Task<int> AddAsync(int a, int b);
}

internal interface IServerInternalWithInternalTypesFromOtherAssemblies
{
Task<StreamJsonRpc.Tests.ExternalAssembly.SomeOtherInternalType> SomeMethodAsync();
Task<ExAssembly.SomeOtherInternalType> SomeMethodAsync();
}

internal interface IRemoteService
{
internal interface ICallback : ExAssembly.IInternalGenericInterface<ExAssembly.SomeOtherInternalType?>
{
}
}

[Fact]
public void Tomas_Internal()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to update the test name to describe the scenario better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't my name self-explanatory? ;)

{
JsonRpc.Attach<IRemoteService.ICallback>(new MemoryStream());
}

[Fact]
Expand Down Expand Up @@ -428,7 +438,7 @@ public async Task InternalInterface()
var clientRpc = JsonRpc.Attach(streams.Item1);

// Try the first internal interface, which is external to this test assembly
var proxy1 = clientRpc.Attach<StreamJsonRpc.Tests.ExternalAssembly.ISomeInternalProxyInterface>();
var proxy1 = clientRpc.Attach<ExAssembly.ISomeInternalProxyInterface>();
Assert.Equal(-1, await proxy1.SubtractAsync(1, 2).WithCancellation(this.TimeoutToken));

// Now create a proxy for another interface that is internal within this assembly, but derives from the external assembly's internal interface.
Expand Down Expand Up @@ -815,11 +825,17 @@ internal class ServerOfInternalInterface : IServerInternal
{
public Task<int> AddAsync(int a, int b) => Task.FromResult(a + b);

public Task<StreamJsonRpc.Tests.ExternalAssembly.SomeOtherInternalType> SomeMethodAsync()
public Task<ExAssembly.SomeOtherInternalType> SomeMethodAsync()
{
return Task.FromResult(new StreamJsonRpc.Tests.ExternalAssembly.SomeOtherInternalType());
return Task.FromResult(new ExAssembly.SomeOtherInternalType());
}

public Task<int> SubtractAsync(int a, int b) => Task.FromResult(a - b);
}

internal class Callback : IRemoteService.ICallback
{
public Task<ExAssembly.SomeOtherInternalType?> GetOptionsAsync(ExAssembly.InternalStruct id, CancellationToken cancellationToken)
=> Task.FromResult<ExAssembly.SomeOtherInternalType?>(null);
}
}