Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging a .net Compiler

I'm currently working on a compiler for a language (an external byte-code), and am using System.Reflection.Emit.

Now, everything looks fine when I open the generated assembly in reflector, and it re-compiles as C# just fine as well (and runs), but when I try to run the main function which gets generated, I get an InvalidProgramException:

"Common Language Runtime detected an invalid program."

It is probably just a single op-code that is causing the issue, but, because that main function uses 100+ op-codes, I can't determine which op-code is causing the issue.

Each op-code is very involved, so manually testing every op-code is a no-go.

Is there a way to get .NET to tell me where in the function it's detecting an invalid program?

And if not, is there some other tool I can use to determine the source of the issue?

like image 426
Orvid King Avatar asked Mar 25 '12 20:03

Orvid King


1 Answers

I'd better write this up as an answer. You can use the PEVerify tool to perform assembly validation. It is part of the Windows SDK tools, best way to run it is from the Visual Studio Command Prompt, peverify.exe command. You'll want to run it with the /il command line option to check the IL you generated, /md to verify the assembly metadata.

You'll get a better diagnostic from this tool, the runtime exception that the jitter generates is a bit too uninformative to pinpoint the exact mistake. I cannot vouch that it performs the exact same checks as the jitter, these are distinct chunks of code and the jitter has a bit of an edge over static analysis. The tool was however specifically designed for your use case, people that write IL generators. I'll just quote the promises made in the MSDN article:

Peverify.exe performs comprehensive MSIL verification checks based on dataflow analysis plus a list of several hundred rules on valid metadata. For detailed information on the checks Peverify.exe performs, see the "Metadata Validation Specification" and the "MSIL Instruction Set Specification" in the Tools Developers Guide folder in the Windows Software Development Kit (SDK).

Turns out it worked well for you, that's cool :)

like image 74
Hans Passant Avatar answered Sep 24 '22 23:09

Hans Passant