Repository navigation
Description
Description
The documentation for InvalidProgramException states right up front that:
Generally this [exception being thrown] indicates a bug in the compiler that generated the program.
Therefore the people most likely to see it are those working on compilers or similar code generation tools. (It's certainly possible that other people could see it if untested compiler bugs are released into the wild, but hopefully the bulk of them will stay inside of the realm of compiler development!)
Exceptions, by design, are full of information meant to make debugging easier. The class describes the general type of problem, the message gives specifics, the stack trace shows you exactly where the error occurred, etc. So you'd expect an exception caused by a bad compiler, whose primary expected audience is compiler developers, to give them a decent clue as to what their compiler did wrong. Unfortunately, InvalidProgramException fails pretty hard at this.
Reproduction Steps
- Modify Roslyn's code generator to omit the
leaveopcode at the end of atrybody. - Compile an otherwise-valid test case containing a
tryblock. - Run the test case.
Expected behavior
The exception that comes up should tell you that there's a try body with no leave instruction. The JIT knows which IL rule was violated, so it ought to be able to convey this information to the user.
Actual behavior
You get a generic error message that says nothing whatsoever about the nature of the problem. All you know is "something went wrong," during the JITting of this method.
Other information
This is one specific example that I recently discovered can cause this error. A glance at the JIT source suggests there are plenty of others. Ideally, each rule violated that leads to this exception being thrown should return an informative error message explaining what went wrong.
Activity
CyrusNajmabadi commentedon Dec 29, 2021
This seems reasonable.
@jkotas would this be easy to add for an external contributor? Is there a reasonable place to point someone (like @masonwheeler) at where they could easily add this information (or pass it through) so the final exception contained more useful info?
@masonwheeler would you be interested in contributing a fix here if it was low-cost to do so? Thanks!
SingleAccretion commentedon Dec 29, 2021
The diagnostic story around
InvalidProgramExceptionis indeed that there is none. In some ways, this is by design.For example, the Jit is not at all obliged [1] to throw anything when it encounters invalid IL. It could hard crash instead, or, as has happened, just accept it and compile "something". It is this latter kind of failure mode that's perhaps more insidious, though (much, I'd imagine?) less common.
If you search around the Jit sources, you will indeed find some
BADCODEmacros that will turn intoIPEs. You will find many morenoway_asserts (that can turn intoIPEs if the Jit fails to compile the code even with all optimizations turned off), and yet more regularasserts, some of them guarding conditions that aren't critical to the integrity of the compiler's internal data structures (those are usuallyBADCODEs), but not expected to be produced by the compiler itself.There is some infrastructure in the compiler for verification that would not allow for "invalid IL", but of course, not all valid IL is verifiable. And we're going to delete the verifier anyways.
There are some issues with defining the concept of "valid IL", too. One example is
ldfld/a. InIII.4.11 ldflda – load field address, we can find underCorrectness:Correct CIL ensures that ... obj is compatible-with the Class of field. In other words, this forbids accessing fields of typeA"through" fields of an unrelated typeB. Yet, that happens on a daily basis because ofUnsafe.Asand even works fine, most of the time.All that overcome, we would still be left with the fact that building a "correctness verifier" into the Jit would slow it down, perhaps just a little, but in the Jit world, even, say, a 0.2% regression in throughput is a significant one.
All in all, I don't forsee us fixing this issue in a systematic way (though having the error messages for
BADCODEs would be nice regardless, and presumably those could be tunnelled through without too much difficulty). I would recommend to use tools like ILVerify, and if those are not suitable (say, because you're using Ref.Emit), acquiring a Checked version of the Jit (say with a local runtime build), that has both moreasserts built in and actually prints useful messages from them :).[1]: ECMA 335,
III.1.8 Verifiability and correctness:The operation of CIL sequences that do not meet the CIL correctness requirements is unspecified.EgorBo commentedon Dec 29, 2021
In theory it's only about extending badCode macro and pass the string we use here to this.
But I am not sure we have an API in JIT-EE to materialize a
const char*toStringObject, if there is one than it should be easy.UPD ah, no need for StringObject actually
masonwheeler commentedon Dec 29, 2021
C++ is never "low-cost" for me, unfortunately. I've been actively avoiding the language ever since it traumatized me in college. 😛
CyrusNajmabadi commentedon Dec 29, 2021
@masonwheeler I meant 'low cost' in the sense that it does not add overhead, and slots in cleanly to the existing code without a ton of churn.
CyrusNajmabadi commentedon Dec 29, 2021
@EgorBo Can you clarify this bit:
masonwheeler commentedon Dec 29, 2021
Sure, but let's not let the perfect be the enemy of the good here.
You are? That feels like a security problem, for reasons I noted on the linked issue.
I'm not asking to build a correctness verifier into the JIT. There is already one there, imperfect and incomplete though it may be. It exists, and it's producing
InvalidProgramExceptionerrors in the current shipping product. All I'm asking is that, when it does so, it ought to explain itself.EgorBo commentedon Dec 29, 2021
I meant we could materialize it in VM itself, jit will only bypass a second argument to RaiseException
EgorBo commentedon Dec 29, 2021
As an one-line fix: we can define
DOTNET_PrintBadCodeToStdout=config switch ;-)masonwheeler commentedon Dec 29, 2021
CyrusNajmabadi commentedon Dec 29, 2021
It feels like it would at least be a better starting point than no info at all. You could also just have the test harness print this out. Or just do a manual case and get this info.
I do agree that having the exception have more info would be valuable. Just thinking about what would be easiest to get in.
As per the post above, it could just be a one line fix (and easy for you to create and get reviewed).
18 remaining items
masonwheeler commentedon Jan 5, 2022
Oh, that makes sense, thanks!
JulieLeeMSFT commentedon Jan 25, 2022
CC @TIHan .
TIHan commentedon Jan 26, 2022
Will take a look.