Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

[ILVerify] Implement backward branch verification - #5113

Merged
jkotas merged 4 commits into
dotnet:masterfrom
ArztSamuel:backwardBranch
Dec 14, 2017
Merged

jkotas merged 4 commits into
dotnet:masterfrom
ArztSamuel:backwardBranch

Conversation

@ArztSamuel

Copy link
Copy Markdown
Collaborator

This implements the verification of backward branches. According to ECMA backward branches require their target blocks to have an empty entry stack or a predecessor block with a lower IL-offset.

In the course of implementing this verification I also implemented the checks for CatchByRef and correct entry stack depth of exception regions.

I also noticed the PEVerify implementation of THIS_UNINIT_EXCEP, but do not fully understand it:
A comment above the relevant code says // cannot enter a try with an uninitialized 'this', however the code then actually only performs a check for an initialized 'this' if the region is not of type RGN_TRY or RGN_FAULT. I also couldn't find the definition of this rule in ECMA.
(see: https://github.com/lewischeng-ms/sscli/blob/master/clr/src/jit64/newverify.cpp#L1580)

// ECMA III 1.7.5 Backward Branch Constraints
// if stack is not empty at beginning of this block,
// there must exist a predecessor block with lower IL offset.
Check(basicBlock.HasPredecessorWithLowerOffset, VerifierError.BackwardBranch);

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 basic block can have more than one predecessor. We can be here because we have seen the one with higher offset, but not the one with lower offset yet, and thus report error incorrectly.

Would it be better to this in InitialPass instead? This InitialPass does a single forward scan of IL that this is trying to check for.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I actually thought that the way the current implementation imports fallthroughs would guarantee that all relevant blocks with lower offsets will be verified first.
But you are right, this is not always the case. I found another test case, which would actually produce such an incorrect error.

I will change this so the HasPredecessor flag is set inside the InitialPass and add the mentioned test case.

@jkotas

jkotas commented Dec 14, 2017

Copy link
Copy Markdown
Member

cc @VSadov

@ArztSamuel

Copy link
Copy Markdown
Collaborator Author

I just noticed that the FindJumptTargets method of the ILImporter is missing the no prefix in its switch. I am not very familiar with this instruction, so please correct me if I am wrong, but shouldn't this instruction also skip 1 byte here?

@jkotas

jkotas commented Dec 14, 2017

Copy link
Copy Markdown
Member

but shouldn't this instruction also skip 1 byte here?

Yes.

This instruction is not support by the .NET runtimes, but it does not hurt to have it for completeness.

@ArztSamuel

Copy link
Copy Markdown
Collaborator Author

This instruction is not support by the .NET runtimes

I see, that explains why I haven't heard of it yet, haha. Thanks!

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

Thanks

@jkotas
jkotas merged commit 0818c06 into dotnet:master Dec 14, 2017
@ArztSamuel
ArztSamuel deleted the backwardBranch branch December 14, 2017 17:51

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

@VSadov

VSadov commented Dec 21, 2017

Copy link
Copy Markdown
Member

The cannot enter a try with an uninitialized 'this' deal is likely an indirect result from the guarantee that base/other constructor is called before the instance can be used for anything other than reading/storing fields. (III.1.8.1.4 Class and object initialization rules)

(Interestingly - there is a known hole involving throwing from ctors and then resurrecting uninited instances. Arguably, resurrection is in the same "messy" league as reflection usage though, so not a huge concern ...)

Anyways, I think the initialization rules themselves make sense but validation likely just follows from general flow analysis and exception regions are not special. The code that special cases them for the this initialization tracking is probably dead code, unless there is some subtlety, that I can't think of, which requires that special casing.

@ArztSamuel

Copy link
Copy Markdown
Collaborator Author

@VSadov thanks for your insight!
That resurrection scenario sounds very interesting.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants