0

I get an InvalidProgramException when trying to execute a particular procedure in an application I am working on when it has been compiled with optimizations (Visual Studio 2015). I used PEVerify to see what it says about the problem, and it tells me "Method[offset 0x00000351] Stack underflow".

Obviously I can fix the problem by turning optimizations off, but that is a less than optimal solution, as is waiting for MS to fix whatever bug causes it in the next version.

What can I do to fix a stack underflow error? If I had to guess I'd say that it's probably related to the fact that this class is somewhere around 18k lines, but there's not a lot I can do about that...

Edit: Just to be clear, I don't expect an answer along the lines of "delete lines 6276 and 6277", what I am looking for is general strategies for troubleshooting this type of problem in .net. Something like the answers to this ActionScript question: How to debug a runtime stack underflow error?, except specific to .net. I am posting this so that the next person that has this type of problem will have a starting point for what they might try to work around this issue.

Community
  • 1
  • 1
jmoreno
  • 12,752
  • 4
  • 60
  • 91
  • 1
    You've given essentially no useful information to solve the issue. You need to work out what piece of code causes the bad IL to be generated, and ideally reduce it to the smallest snippet that can be compiled to reproduce the problem. Once you have that, you might be able to change the problem code yourself so it no longer exhibits the issue, or someone else may be able to advise further. You can then also potentially submit the bug to MS so that they can fix it, rather than relying on their telepathic skills. – Iridium Mar 10 '16 at 18:47
  • Someone else had aproblem with a Stack Underflow here - http://stackoverflow.com/questions/6270837/how-to-debug-a-runtime-stack-underflow-error - I don't know if it will help, but it's an idea. – David Wilson Mar 10 '16 at 21:37
  • @DavidWilson: I saw that, and that was the kind of answers I was hoping to get, but for .net, not actionscript. – jmoreno Mar 10 '16 at 21:41
  • Sorry. It wasn't more help. I suspect this is a rare occurrence and not. Many people have experience of it. – David Wilson Mar 10 '16 at 21:43
  • @DavidWilson: I think you're right, which is why I posted the question -- in the hope that it would give anyone else that had this problem a starting point. While turning off optimizations isn't ideal, it works. But it'd be nice to have other options (such as turning off a specific optimization as in the case of that ActionScript question). And with the problem being so rare, there's not a lot out there on what to do. It took me quite a while to even find mention of PEVerify. – jmoreno Mar 10 '16 at 21:56
  • Given such bugs are generally pretty rare, there aren't really any "general strategies" for avoiding them (and such a question would likely be off-topic for SO anyway) so you need to determine the piece of code triggering the issue. You can use ildasm to dump the IL for the method, and you have the IL offset of the underflow from PEVerify, so you ought to be able to determine approximately the line(s) of code that are being incorrectly optimized. You can then start cutting out code around it until you have a minimal example that shows the bug, and can update your question with the details. – Iridium Mar 10 '16 at 22:20

1 Answers1

2

Ok, first off an Invalid Program Exception indicates something is wrong with the compiler, not your application. This means that you can test to see if the problem still exists (or has morphed into a different problem) without running your application. Once you see this error you are out of the normal debugging scenario -- you haven't done something wrong, and thus can't find or fix the problem by examing variables and seeing where you are doing the wrong thing.

The first suspect is probably optimizations--build without optimizations and see if your exe is still messed up. If so, you may consider turning optimizations off a sufficient workaround. You might even be able to use the System.Runtime.CompilerServices.Methodimpl(methodimploptions.nooptimization) to turn it off for a specific method.

If not, or that doesn't change anything, you want to find out what is triggering the bug, and a good staring point for that is Peverify. Run it against the compiled exe and it will list all of the places where bad code exists, even if it is an uncalled method.

Since this is a compiler bug, you can't step through a method to see exactly which line is off. That has it's upside -- you don't have to have a functioning (or as I said above, reachable) method. As long as the code compiles, it will either have the problem or not, so you can find the offending line(s) by commenting out or deleting lines in the method that is causing problems.

jmoreno
  • 12,752
  • 4
  • 60
  • 91