Skip to content

When invoking class constructor ensure class is initialized - #40293

Merged
davidwrighton merged 4 commits into
dotnet:masterfrom
davidwrighton:RunCctorInReflection
Aug 5, 2020
Merged

davidwrighton merged 4 commits into
dotnet:masterfrom
davidwrighton:RunCctorInReflection

Conversation

@davidwrighton

Copy link
Copy Markdown
Member

When invoking the class constructor method via reflection invoke, ensure that the class constructor is run via the standard run class constructor pathway before manually invoking the class constructor

This ensure that all of the various data structures associated with the class constructor are initialized such as valuetype statics.

Fixes #1748

@Dotnet-GitSync-Bot

Copy link
Copy Markdown
Collaborator

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

[Fact]
public void Invoke_StaticConstructorMultipleTimes()
{
ConstructorInfo[] constructors = GetConstructors(typeof(ClassWithStaticConstructorThatIsCalledMultipleTimesViaReflection));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

BTW this test library doesn't run against .NET Framework, so you'd have to paste this into a test program if you wanted to be sure NETFX behavior was the same.

@jkotas

jkotas commented Aug 3, 2020

Copy link
Copy Markdown
Member

Does this create easy path to mutate read-only statics (by accident)?

We have disallowed setting of read-only fields via reflection in .NET Core. This is a more subtle variant of the same. I think it may be better to fix this by disallowing static cctor invocation via reflection.

@jkotas

jkotas commented Aug 3, 2020

Copy link
Copy Markdown
Member

I think it may be better to fix this by disallowing static cctor invocation via reflection.

Or to allow it just once.

@davidwrighton

Copy link
Copy Markdown
Member Author

This creates all sorts of questionable paths. But they aren't new. @danmosemsft I've verified that this is the behavior of Netfx 4.8.

I'd be pleased to convert an attempt invoke the static ctor via this mechanism into a simple call to RunClassConstructor instead of actually, you know, repeatedly invoking the class constructor directly. The current checked in scheme is clearly not safe, as it doesn't behave reliably, and can skip critical aspects of cctor invocation.

@jkotas

jkotas commented Aug 3, 2020

Copy link
Copy Markdown
Member

I'd be pleased to convert an attempt invoke the static ctor via this mechanism into a simple call to RunClassConstructor

+1

{
// Run the class constructor through the class constructor mechanism instead of the Invoke path.
// This avoids allowing mutation of readonly static fields, and initializes the type correctly.
RuntimeHelpers.RunClassConstructor(DeclaringType!.TypeHandle);

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.

Can we get here for module constructors? I believe DeclaringType is going to be null for them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sigh, we can. I'll update the code to handle them by running the module constructor api.

@jkotas jkotas 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.

LGTM!

@jkotas jkotas changed the title When invoking class constructor ensure class is intialized When invoking class constructor ensure class is initialized Aug 4, 2020
@davidwrighton
davidwrighton merged commit dd05d47 into dotnet:master Aug 5, 2020
Jacksondr5 pushed a commit to Jacksondr5/runtime that referenced this pull request Aug 10, 2020
…0293)

When invoking the class constructor method via reflection invoke, ensure that the class constructor is run via the standard run class constructor pathway instead of running it explicitly. Do the same for module constructors.
@karelz karelz added this to the 5.0.0 milestone Aug 18, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
@davidwrighton
davidwrighton deleted the RunCctorInReflection branch April 20, 2021 17:43
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.

static constructors throw NRE if having struct fields and invoked via TypeInitializer

5 participants