Skip to content
This repository was archived by the owner on Nov 1, 2020. It is now read-only.

[ILVerify] Handling of assignment of interface to object - with CastingHelper#3666

Merged
jkotas merged 11 commits intodotnet:masterfrom
Dynatrace:VerifyInterfaces
May 21, 2017
Merged

[ILVerify] Handling of assignment of interface to object - with CastingHelper#3666
jkotas merged 11 commits intodotnet:masterfrom
Dynatrace:VerifyInterfaces

Conversation

@gregkalapos
Copy link
Collaborator

@gregkalapos gregkalapos commented May 20, 2017

We had some branching issues on our fork, so I opened a new PR.
Original PR: #3649

One problem I have is this:
On CoreCLR we have lots of type forwardings. E.g. Application code references System.Runtime.dll, which defines System.Object. The application code always references System.Object from System.Runtime, but System.Runtime.dll forwards it to Sysmte.PricateCorLib.dll. When I do curType.Context.GetWellKnownType(WellKnownType.Object) it creates a [System.PriveCorLib]System.Object, but the otherType is [System.Runtime]System.Object (since it comes from the code referencing System.Runtime.dll). How should I handle this?

Additionally I also have a question:
Is it possible to start the unit tests (in this case these: https://github.com/dotnet/corert/blob/master/src/ILCompiler.TypeSystem/tests/CastingTests.cs ) directly in Visual Studio? The VS Test runner does not discover these tests, the error I get is this:

Dependency '"test-runtime": {
  "target": "project",
  "exclude": "compile"
}' has invalid version specification.

As a workaround I do it from the command line like this:
CoreRun.exe xunit.console.netcore.exe TypeSystem.Tests.dll -xml testResults.xml -notrait Benchmark=true -notrait category=nonnetcoreapp1.1tests -notrait category=OuterLoop -notrait category=failing -notrait category=nonwindowstests
I found this in a log from runtest.cmd, but it would be much better to do it in VS, so I can also debug the tests.

Thanks.

@jkotas
Copy link
Member

jkotas commented May 21, 2017

curType.Context.GetWellKnownType(WellKnownType.Object) it creates a [System.PriveCorLib]System.Object, but the otherType is [System.Runtime]System.Object (since it comes from the code referencing System.Runtime.dll)

You need to run with a consistent set of assemblies:

  • Either reference assemblies (i.e. what gets passed as /r to C# compiler): There is no System.Private.CoreLib, System.Runtime defines System.Object.
  • Or implementation assemblies (i.e. what is used at runtime): System.Private.CoreLib defines System.Object, System.Runtime forwards System.Object to System.Private.CoreLib.

Mixing and matching of reference assemblies and implementation does not work. The problem that you have described occurs when you mix System.Runtime reference assembly with System.Private.CoreLib implementation assembly.

curType = curType.BaseType;
if (curType.IsInterface)
{
curType = curType.Context.GetWellKnownType(WellKnownType.Object);
Copy link
Member

Choose a reason for hiding this comment

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

The other implementation of casting logic implement this case as one-off check before this loop, for example: https://github.com/dotnet/corert/blob/master/src/Runtime.Base/src/System/Runtime/TypeCast.cs#L633

It is a bit more efficient because of you need this check only first time through the loop. It would be nice to do it same way here.

return true;

return IsAssignable(src.Type, dst.Type);
return CastingHelper.CanCastTo(src.Type, dst.Type);
Copy link
Member

@jkotas jkotas May 21, 2017

Choose a reason for hiding this comment

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

Change IsAssignable(TypeDesc src, TypeDesc dst) to call CastingHelper.CanCastTo as well while you are on it? At this line https://github.com/dotnet/corert/pull/3666/files#diff-ab14902486f8fc54c4fd3c1845fa8babR187

@jkotas
Copy link
Member

jkotas commented May 21, 2017

it would be much better to do it in VS, so I can also debug the tests.

Agree. The CoreRT is building with old project.json-based .NET CLI that does not integrate well with current Visual Studio. For example, you might have noticed that the VS project that we use for debugging the CoreRT compiler is actually full .NET Framework project, not .NET Core project.

I have been working on upgrading the build system to recent .NET CLI that is a big step towards fixing this sort of problems. It will take me 1-2 weeks to get it done. The full .NET Framework helper projects should be gone once I am done.

BTW: If you run into this with adding ILVerify tests, feel free to create a vanilla .NET Core projects using dotnet new console, dotnet new xunit, that are not plugged in into the repo build for now. I will take care of plugging them into the repo build.

@MichalStrehovsky
Copy link
Member

Is it possible to start the unit tests directly in Visual Studio

Yup, this works. Open the src\ILCompiler.TypeSystem\TypeSystem.sln solution and set the TypeSystem.Tests project as the startup project. Hit Debug -> Start debugging in the menu (run it as a normal command line app).

@gregkalapos
Copy link
Collaborator Author

You need to run with a consistent set of assemblies:

Now I understand. I will add this to the readme. I just naively passed every reference (direct or indirect) to ILVerify, maybe I won't be the only one doing this.

@jkotas your second comment answers a ton of questions I had. Thanks for that!

@MichalStrehovsky Great! Some tests fail when I start them that way (they are fine from the command line) I will dig deeper if I need to debug, but thanks for the info!

return thisType.CanCastTo(otherType.Instantiation[0]);
}

if(curType.IsInterface)
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Add space between if and (

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Thanks!

@jkotas jkotas merged commit a80c986 into dotnet:master May 21, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants