Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Fix invalid IL generated in Xml serializers - #39765

Merged
stephentoub merged 1 commit into
dotnet:masterfrom
filipnavara:xml-invalid-il
Oct 19, 2019
Merged

stephentoub merged 1 commit into
dotnet:masterfrom
filipnavara:xml-invalid-il

Conversation

@filipnavara

Copy link
Copy Markdown
Member

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

III.1.7.5 Backward branch constraints

It shall be possible, with a single forward-pass through the CIL instruction stream for any
method, to infer the exact state of the evaluation stack at every instruction (where by “state” we
mean the number and type of each item on the evaluation stack).

In particular, if that single-pass analysis arrives at an instruction, call it location X, that
immediately follows an unconditional branch, and where X is not the target of an earlier branch
instruction, then the state of the evaluation stack at X, clearly, cannot be derived from existing
information. In this case, the CLI demands that the evaluation stack at X be empty.

Following on from this rule, it would clearly be invalid CIL if a later branch instruction to X
were to have a non-empty evaluation stack

[Rationale: This constraint ensures that CIL code can be processed by a simple CIL-to-native code compiler. It ensures that the state of the evaluation stack at the beginning of each CIL can
be inferred from a single, forward-pass analysis of the instruction stream. end rationale]
[Note: the stack state at location X in the above can be inferred by various means: from a
previous forward branch to X; because X marks the start of an exception handler, etc. end note]

This particular condition was violated in code generated in ILGenForCreateInstance method [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 ILGenForCreateInstance always 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 modify ILGenForCreateInstance to work with non-empty stack. That could be done by modifying the while loop generation.

I opted for the smallest possible fix that makes the code valid by modifying the code while loop generation. Alternatively I can modify it to move the while condition block to the beginning of the while loop. 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]

internal void ILGenForCreateInstance(CodeGenerator ilg, Type type, Type cast, bool nonPublic)

[2]
ilg.Ldloc(arrayLoc);
ilg.Ldc(i);
RaCodeGen.ILGenForCreateInstance(ilg, mappings[i].TypeDesc.Type, false, false);

[3]
ilg.Ldarg(0);
RaCodeGen.ILGenForCreateInstance(ilg, sm.TypeDesc.Type, sm.TypeDesc.CannotNew, false);

{
WhileState whileState = new WhileState(this);
Br(whileState.CondLabel);
// ECMA-335 III.1.7.5 states that code blocks after unconditional

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

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.

If we ever get to updating the spec, we are likely to delete this restriction from it.

@filipnavara filipnavara Jul 25, 2019 •

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.

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.

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.

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

@filipnavara filipnavara Jul 25, 2019 •

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.

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

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

@filipnavara filipnavara Jul 26, 2019 •

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.

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.

@filipnavara filipnavara Jul 26, 2019 •

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.

@stephentoub stephentoub added the * NO MERGE * The PR is not ready for merge yet (see discussion for detailed reasons) label Jul 25, 2019
@karelz karelz added this to the 5.0 milestone Aug 3, 2019
@eiriktsarpalis

Copy link
Copy Markdown
Member

Bumping this PR since it hasn't been updated in a while. What is the final resolution on this?

@stephentoub stephentoub removed the * NO MERGE * The PR is not ready for merge yet (see discussion for detailed reasons) label Aug 8, 2019
@StephenMolloy

Copy link
Copy Markdown
Contributor

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?

@stephentoub

Copy link
Copy Markdown
Member

Let's go ahead and take this then. Thanks.

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.

[netcore] Make System.Data.Tests.SqlTypes.SqlInt32Test.ReadWriteXmlTest Pass

8 participants