Orvid King
Orvid King

Reputation: 1218

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?

Upvotes: 5

Views: 165

Answers (1)

Hans Passant
Hans Passant

Reputation: 941208

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

Upvotes: 10

Related Questions