Skip to content

[release/5.0] Fix covariant returns with generic return types - #45873

Merged
Anipik merged 3 commits into
release/5.0from
backport/pr-45275-to-release/5.0
Dec 11, 2020
Merged

[release/5.0] Fix covariant returns with generic return types#45873
Anipik merged 3 commits into
release/5.0from
backport/pr-45275-to-release/5.0

Conversation

@github-actions

@github-actions github-actions Bot commented Dec 9, 2020

Copy link
Copy Markdown
Contributor

Backport of #45275 to release/5.0

/cc @janvorli

Customer Impact

Valid application that uses generic types in covariant returns crashes at runtime with System.TypeLoadException when attempting to use the underlying type or enumerating types in the assembly using Assembly.GetTypes().

Testing

CoreCLR pri1 tests, including two new tests added that specifically target the problematic cases (copies of the repros provided by our users that have found the issue)

Risk

Low

Regression

No

When a covariant return type was an uninstantiated generic type, the
ClassLoader::IsCompatibleWith was not working properly. In debug builds,
it was asserting because there was no MethodTable for that type and in
release builds, it resulted in ExecutionEngineException or an internal
CLR error.

This change fixes it by using TypeHandle::CanCastTo instead of
MethodTable::CanCastTo and adds a regression test for two cases where
the problem was observed (Assembly.GetTypes() and creating an instance
of a type with a covariant return with a problematic kind of type).
There were two issues. First, the
ClassLoader::ValidateMethodsWithCovariantReturnTypes was called before
typeHnd.DoFullyLoad and that resulted in an assert down the call chain
of TypeDesc::CanCastTo due to a wrong load level.
Second, the SigTypeContext generation for the current MD in the
ClassLoader::ValidateMethodsWithCovariantReturnTypes requires the same
change for class instantiation as the one that we had for the parent MD.
The call to ClassLoader::ValidateMethodsWithCovariantReturnTypes is now
in MethodTable::DoFullyLoad.
I have also added a test case that verifies a case that David Wrighton
has suggested offline, where there are 3 types... A, B and C.
C derives from B which derives from A. B has a bad override which
should produce an error.  Then, cause C to be fully loaded without
otherwise triggering a load of B.
@janvorli
janvorli requested a review from jkotas December 9, 2020 23:29
@janvorli janvorli added the Servicing-consider Issue for next servicing release review label Dec 9, 2020
@janvorli janvorli added this to the 5.0.x milestone Dec 9, 2020

@jeffschwMSFT jeffschwMSFT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approved. We will consider this for 5.0.2

@leecow leecow removed the Servicing-consider Issue for next servicing release review label Dec 10, 2020
@leecow leecow modified the milestones: 5.0.x, 5.0.2 Dec 10, 2020
@jeffschwMSFT jeffschwMSFT added the Servicing-approved Approved for servicing release label Dec 10, 2020
@jeffschwMSFT

Copy link
Copy Markdown
Member

Approved during tactics.

@Anipik
Anipik merged commit fb2e992 into release/5.0 Dec 11, 2020
@jkotas
jkotas deleted the backport/pr-45275-to-release/5.0 branch December 13, 2020 23:59
@ghost ghost locked as resolved and limited conversation to collaborators Jan 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-TypeSystem-coreclr Servicing-approved Approved for servicing release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants