Repository navigation
Conversation
- 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
56e9f76 to
4771f78
Compare
|
@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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yes. Quite possible.
- 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.
- 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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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.
|
@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 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. |
|
This pull request has been automatically marked |
|
This pull request will now be closed since it had been marked |
ldsfldacannot be used inside .ctorFixes #94090