Repository navigation
Fix invalid IL generated in Xml serializers - #39765
Conversation
| { | ||
| WhileState whileState = new WhileState(this); | ||
| Br(whileState.CondLabel); | ||
| // ECMA-335 III.1.7.5 states that code blocks after unconditional |
There was a problem hiding this comment.
Note that .NET Framework and .NET Core JITs never took advantage of this restriction and always handled backward branches to non-empty stacks just fine. There is a lot of code out there with backward branches to non-empty stacks.
There was a problem hiding this comment.
If we ever get to updating the spec, we are likely to delete this restriction from it.
There was a problem hiding this comment.
Mono can handle it in certain modes (eg. interpreter) but not in others (the default JIT). The restriction concerns only backward branches to non-empty stack where no forward branch to the same code exists. The C# compiler never generates this AFAIK.
If you consider this to be something that should be supported then we should raise an issue with Mono to allow it.
There was a problem hiding this comment.
@jkotas do you think this is something Mono will have to implement for .net 5? This does not look like something used often as I don't remember seeing this before.
There was a problem hiding this comment.
@marek-safar I don't think it would be particularly hard to implement if you decide that's the right way to go. I can even give it a try.
I do expect XmlSerializer to be quite important to get right in .NET 5 but whether it is accomplished by changing Mono or CoreFX I am impartial on. As far as the backward branches go this is the first time I saw it with unbalanced stack (although my own code analysis tools handle this gracefully).
There was a problem hiding this comment.
is this is something Mono will have to implement for .net 5?
Depends on whether something else runs into it, how hard the fix would be and how much time is spent diagnosing the problem (the xml serializer took like 4 people to diagnose).
The proposed fix in the serializer does not look too distruptive, so I would not have a problem with taking it if you prefer that.
IIRC, .NET Native took advantage of this restriction originally, but then was fixed to match .NET Framework because of somebody reported it as compat problem.
There was a problem hiding this comment.
I did search through Mono GitHub issues to look for reports of issues with the same root cause. Aside from the ones triggering this investigation there was one (mono/mono#8077) reported to be a code generated by obfuscator. All other "invalid IL" issues were caused by different bugs, mostly thread-safety issue in dynamic code generation that was recently fixed.
We currently run tests for LINQ expressions and compiled regular expressions (the other two dynamic code heavy libraries) on mono/netcore CI and as far as I can tell they didn't hit this particular issue.
There was a problem hiding this comment.
Further search shows that it was hit back in 2004 by IKVM: http://weblog.ikvm.net/2004/03/29/BackwardBranchConstraints.aspx, gluck/ikvm@3930374
|
Bumping this PR since it hasn't been updated in a while. What is the final resolution on this? |
|
The PR itself looks fine to me. But it sounds like the spec for this might get updated? In which case, mono will have to be updated to handle this case anyway? If that's all just conjecture, then by all means lets fix this here. dotnet:master is for 5.0 now, right? Is this where we want the fix? |
|
Let's go ahead and take this then. Thanks. |
Fixes mono/mono#15174, mono/mono#15175, mono/mono#15176, mono/mono#15177, mono/mono#15178, mono/mono#15179.
The detailed analysis of the problem is described in mono/mono#15179.
ECMA 335, section 1.7.5 states
This particular condition was violated in code generated in
ILGenForCreateInstancemethod [1] in the XML serialization. The method itself is correctly stack balanced as long as the initial stack has depth 0 at the time it starts the code generation. However, that assumption is violated on several call sites (eg. [2], [3]).Two possible fixes are possible. First, we can ensure that the call sites of
ILGenForCreateInstancealways have stack depth of zero. Due to the number of call sites that are affected by this it would be hard to verify that all of them are correct. Second, we can modifyILGenForCreateInstanceto work with non-empty stack. That could be done by modifying thewhileloop generation.I opted for the smallest possible fix that makes the code valid by modifying the code
whileloop generation. Alternatively I can modify it to move thewhilecondition block to the beginning of thewhileloop. It will require a few more code changes and it will differ more significantly from what C# compiler produces. Let me know if that is preferable./cc @marek-safar @jkotas
[1]
corefx/src/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationWriterILGen.cs
Line 2386 in 8c2be78
[2]
corefx/src/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationReaderILGen.cs
Lines 626 to 628 in 8c2be78
[3]
corefx/src/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationReaderILGen.cs
Lines 3251 to 3252 in 8c2be78