Skip to content

Allow using ldsflda on initonly fields outside of cctor - #112081

Closed
kant2002 wants to merge 2 commits into
dotnet:mainfrom
kant2002:kant/ldsflda-ilverify-readonly
Closed

kant2002 wants to merge 2 commits into
dotnet:mainfrom
kant2002:kant/ldsflda-ilverify-readonly

Conversation

@kant2002

@kant2002 kant2002 commented Feb 3, 2025

Copy link
Copy Markdown
Contributor
  • Fixed incorect assumption that ldsflda cannot be used inside .ctor
  • Add test cases which explicitly forbig storing static initonly field outside cctor

Fixes #94090

@ghost ghost added the area-Tools-ILVerification Issues related to ilverify tool and IL verification in general label Feb 3, 2025
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Feb 3, 2025
- Fixed incorect assumption that `ldsflda` cannot be used inside .ctor
- Add test case which explicitly forbig storing static initonly field outside cctor

Fixes dotnet#94090
@kant2002

kant2002 commented Feb 3, 2025

Copy link
Copy Markdown
Contributor Author

@MichalStrehovsky in case you miss that one 😄

@MichalStrehovsky

Copy link
Copy Markdown
Member

@MichalStrehovsky in case you miss that one 😄

ILVerification is owned by the JIT team and I honestly don't know the verification rules for this off the top of my head.


if (field.IsInitOnly)
Check(_method.IsStaticConstructor && field.OwningType == _method.OwningType, VerifierError.InitOnly);
isReadOnly = true;

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.

As discussed in #57444 (comment), isReadOnly means controlled mutability in the current implementation of IL verify.

If you would like to workaround this without implementing proper implementation of read-only references, should this rather be // TODO: verification of readonly references https://github.com/dotnet/runtime/issues/57444 like for the instance field cases to make it less confusing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah. that's probably great idea, even I plan to subsequently start working on #57444 . I still confuse what isReadonly mean in the code, is this "new" readonly-references, or this is "old" controlled mutability references. I assume that now this is controlled mutability references have confusing name isReadonly/have flag ReadOnly

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.

is this "new" readonly-references, or this is "old" controlled mutability references.

It should be the old controlled mutability. The new readonly references did not exist when this code was written.

@kant2002 kant2002 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Add comment about future work


if (field.IsInitOnly)
Check(_method.IsStaticConstructor && field.OwningType == _method.OwningType, VerifierError.InitOnly);
// TODO: verification of readonly references https://github.com/dotnet/runtime/issues/57444

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.

The current implementation allows the following in cctor:

ldsflda int32 FieldTestsType::StaticInitonlyField
ldc.i4 
stind.i4

If I am reading the code correctly, this sequence is going to be unverifiable with this fix. The fix is replacing one bug with a different bug.

For temporary workaround, I meant to disable this completely just like it is disabled for instance fields below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. Quite possible.

  1. Should I add this sample as test case ? I start thinking that I very far from understanding even this small place/interaction. If I may be able to at least document my mistakes in form of tests, that can be valuable for future me/other outsiders.
  2. My idea was to load controlled managed pointer and assumption that storing anything using it would be impossible. Since All other operations (including stobj, stind.*, initobj, and mkrefany) are invalid. for them by rules in the old code. Am I miss something?

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.

Hmm, I think you are saying that the sequence that I have pointed out should remain unverifiable. I agree with that after thinking about it some more.

I believe that the handling of initonly instance and initonly static fields should be the same. #57385 made it diverge. It would be nice if this PR gets the two in sync again. Do you agree?

Yes, it is fine to add new tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you agree?

Yes, sure. I think they should be the same, except that small not in the ECMA-335 which state that [Note: Using stfld to change the value of a static, init-only field outside the body of the class initializer can lead to unpredictable behavior. It cannot, however, compromise memory integrity or type safety so it is not tested by verification. end note]

I really confused why IL verification happens at 'ldflda/ldsfldainstruction, and not on thestfld/stsfld`. I understand that there should be some reasons to that, but cannot create test case in my head where I can break things. Only idea whcich I have is that I load address of field, and save it somewhere in the other field, and then use that address to do bad thing. but isn't whole pointer math unverifieable, and I will have error somewhere else?

For me I either should treat these ldlda/ldsflda on init-only fields as regular pointers within constructors, or even better create special type on the stack. Maybe it would be something like https://github.com/dotnet/designs/blob/main/proposed/verifiable-ref-readonly.md, not sure.

Anyway I will keep thinking about it. so just getting you know, that

  • a) I'm stuck
  • b) I have some ideas which I keep exploring.

@jkotas jkotas Feb 24, 2025 •

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.

[Note: Using stfld to change the value of a static, init-only field outside the body of the class initializer can lead to unpredictable behavior. It cannot, however, compromise memory integrity or type safety so it is not tested by verification. end note]

We made this invalid for static fields, Changing the value of a static, init-only field outside the body of the class initializer can compromise memory integrity and type safety. You can add a note about this to https://github.com/dotnet/runtime/blob/main/docs/design/specs/Ecma-335-Augments.md .

It is still ok for instance fields. Many serializers out there depend on it, and the init properties in C# took a dependency on it being fine too.

@jkotas jkotas Feb 24, 2025 •

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.

It is still ok for instance fields

Ideally, verification would check that the modification of init-only fields is done in instance constructors or init methods only. It is what the existing TODO in handling of instance fields is about.

@amanasifkhalid

Copy link
Copy Markdown
Contributor

@kant2002 are you still working on this? No worries if you're still thinking through your approach; I'm going to flag this PR just to keep it from going stale on our end.

@amanasifkhalid amanasifkhalid added the needs-author-action An issue or pull request that requires more info or actions from the author. label Mar 24, 2025
@kant2002

Copy link
Copy Markdown
Contributor Author

@amanasifkhalid I'm still try (in free time) to think about proper way to move forward, partialy I think I may add more tests, but whole thing seems to be a bit problematic for me. Previosuly validation works on loading field address, and that was prohibited in static contstructure.

I'm moving towards prohibiting on save which potentially may cause an issues, but still conforming to spec. At the same time, I think that disallowing ldsflda in PEVerify was for a reasons. So I'm trying to solve that puzzle.

@dotnet-policy-service dotnet-policy-service Bot removed the needs-author-action An issue or pull request that requires more info or actions from the author. label Mar 24, 2025
@JulieLeeMSFT JulieLeeMSFT added the needs-author-action An issue or pull request that requires more info or actions from the author. label Mar 31, 2025
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

This pull request will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the pull request, but please note that it will be locked if it remains inactive for another 30 days.

@github-actions github-actions Bot locked and limited conversation to collaborators May 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Tools-ILVerification Issues related to ilverify tool and IL verification in general community-contribution Indicates that the PR has been added by a community member needs-author-action An issue or pull request that requires more info or actions from the author. no-recent-activity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ILVerify incorrectly complains about use of initonly field outside of constructor+

5 participants