Skip to content

InvalidProgramException is extremely unhelpful #63198

Open

Description

@masonwheeler

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

  1. Modify Roslyn's code generator to omit the leave opcode at the end of a try body.
  2. Compile an otherwise-valid test case containing a try block.
  3. 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

  1. ghost added
    untriaged
    on Dec 29, 2021
  2. added
    area-CodeGen-coreclr
    on Dec 29, 2021
  3. CyrusNajmabadi commented on Dec 29, 2021

    @CyrusNajmabadi
    Contributor

    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!

  4. SingleAccretion commented on Dec 29, 2021

    @SingleAccretion
    Contributor

    The diagnostic story around InvalidProgramException is 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 BADCODE macros that will turn into IPEs. You will find many more noway_asserts (that can turn into IPEs if the Jit fails to compile the code even with all optimizations turned off), and yet more regular asserts, some of them guarding conditions that aren't critical to the integrity of the compiler's internal data structures (those are usually BADCODEs), 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. In III.4.11 ldflda – load field address, we can find under Correctness: Correct CIL ensures that ... obj is compatible-with the Class of field. In other words, this forbids accessing fields of type A "through" fields of an unrelated type B. Yet, that happens on a daily basis because of Unsafe.As and 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 more asserts 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.

  5. EgorBo commented on Dec 29, 2021

    @EgorBo
    Member

    would this be easy to add for an external contributor?

    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* to StringObject, if there is one than it should be easy.
    UPD ah, no need for StringObject actually

  6. masonwheeler commented on Dec 29, 2021

    @masonwheeler
    ContributorAuthor

    @masonwheeler would you be interested in contributing a fix here if it was low-cost to do so? Thanks!

    C++ is never "low-cost" for me, unfortunately. I've been actively avoiding the language ever since it traumatized me in college. 😛

  7. CyrusNajmabadi commented on Dec 29, 2021

    @CyrusNajmabadi
    Contributor

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

  8. CyrusNajmabadi commented on Dec 29, 2021

    @CyrusNajmabadi
    Contributor

    @EgorBo Can you clarify this bit:

    UPD ah, no need for StringObject actually

  9. masonwheeler commented on Dec 29, 2021

    @masonwheeler
    ContributorAuthor

    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.

    Sure, but let's not let the perfect be the enemy of the good here.

    And we're going to delete the verifier anyways.

    You are? That feels like a security problem, for reasons I noted on the linked issue.

    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.

    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 InvalidProgramException errors in the current shipping product. All I'm asking is that, when it does so, it ought to explain itself.

  10. EgorBo commented on Dec 29, 2021

    @EgorBo
    Member

    @EgorBo Can you clarify this bit:

    UPD ah, no need for StringObject actually

    I meant we could materialize it in VM itself, jit will only bypass a second argument to RaiseException

  11. EgorBo commented on Dec 29, 2021

    @EgorBo
    Member

    As an one-line fix: we can define DOTNET_PrintBadCodeToStdout= config switch ;-)

  12. masonwheeler commented on Dec 29, 2021

    @masonwheeler
    ContributorAuthor
  13. CyrusNajmabadi commented on Dec 29, 2021

    @CyrusNajmabadi
    Contributor

    Printing to stdout would be unhelpful for testing scenarios where the test harness ignores or does custom things with stdout.

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

  14. 18 remaining items

  15. masonwheeler commented on Jan 5, 2022

    @masonwheeler
    ContributorAuthor

    Oh, that makes sense, thanks!

  16. JulieLeeMSFT commented on Jan 25, 2022

    @JulieLeeMSFT
    Member

    CC @TIHan .

  17. removed
    untriaged
    on Jan 25, 2022
  18. added this to the Future milestone on Jan 25, 2022
  19. added
    area-Tools-ILVerification
    on Jan 25, 2022
  20. removed
    area-CodeGen-coreclr
    on Jan 25, 2022
  21. TIHan commented on Jan 26, 2022

    @TIHan
    Contributor

    Will take a look.

  22. self-assigned this
    on Jan 26, 2022
  23. modified the milestones: Future, 8.0.0 on Jun 6, 2022
  24. modified the milestones: 8.0.0, Future on Jul 5, 2022
  25. added
    Priority:3
    on Apr 24, 2023
  26. removed their assignment
    on Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority:3area-Tools-ILVerification

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions