是否有用于在运行时验证动态程序集的 MSIL 的 API?

发布于 2024-12-03 01:09:14 字数 185 浏览 5 评论 0 原文

当使用 Reflection.Emit 在运行时构建程序集时,我想在保存到光盘之前验证程序集 MSIL。类似于 PEVerify 但在运行时。有这样的API吗?

When using Reflection.Emit to build an assembly at runtime, I'd like to verify the assembly MSIL before saving to disc. Like PEVerify but at runtime. Is there such an API?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坦然微笑 2024-12-10 01:09:14

看来 peverify.exe 是 c:\Windows\Microsoft.NET\Framework\v4.0.30319\peverify.dll 的前端(或 c:\Windows\Microsoft.NET\Framework\v2.0.50727\peverify.dll对于 CLR 2.0),这是一个本机 DLL(实际上,peverify.exe 也是本机的)

我没有在任何地方看到这个记录,所以它可能不是公共 API。您也许可以使用 Dependency Walker 之类的东西找出该 DLL 的导出函数,但我认为它会更简单的方法是调用 peverify.exe。

编辑:轶事证据:

It seems that peverify.exe is a front-end to c:\Windows\Microsoft.NET\Framework\v4.0.30319\peverify.dll (or c:\Windows\Microsoft.NET\Framework\v2.0.50727\peverify.dll for CLR 2.0), which is a native DLL (actually, peverify.exe is also native)

I don't see this documented anywhere so it's probably not a public API. You may be able to figure out the exported functions from that DLL using something like Dependency Walker, but I think it would be simpler to just call peverify.exe.

EDIT: anecdotal evidence:

岁吢 2024-12-10 01:09:14

您可以使用 ILSpy 的反编译器作为进程内解决方案,而不是使用 PEVerify,如下所述: http://www.codeproject.com/Tips/659692/Automated-MSIL-PE-verification-using-ILSpy

文章摘要为:

  1. 收集从您的测试项目中引用相关的 DLL,或者
    本例中为运行时 IL 检查器
  2. 使用 Mono.Cecil 迭代方法以进行验证
  3. 对于每个方法,将其添加到 ICSharpCode.Decompiler 中定义的 AstBuilder 中以执行验证。例如。
var context = new DecompilerContext(method.Module) { CurrentType = method.DeclaringType };
var astBuilder = new AstBuilder(context);
astBuilder.AddMethod(method);

就性能而言,我还没有检查哪种方法更快。尽管此方法是进程内的,但它可能会较慢,因为抽象语法树是在验证 IL 时构建的(我必须设置性能测试来检查此理论)。

正如上一篇文章中指出的,我发现 ILSpy 反编译器比 PEVerify 更可靠,在一个实例中,PEVerify 声明一个程序集有效,而 ILSpy 正确地给出了一个漂亮的堆栈跟踪,指示我在生成过程中的错误。

Instead of using PEVerify you could use the ILSpy's decompiler for an in-process solution, as described here: http://www.codeproject.com/Tips/659692/Automated-MSIL-PE-verification-using-ILSpy

A summary of the article is:

  1. Collect the relevant DLLs to reference from your test project, or
    runtime IL checker in this case
  2. Iterate through the methods to verify using Mono.Cecil
  3. For each method, add it to the AstBuilder defined in ICSharpCode.Decompiler which performs the validation. Eg.
var context = new DecompilerContext(method.Module) { CurrentType = method.DeclaringType };
var astBuilder = new AstBuilder(context);
astBuilder.AddMethod(method);

Performance-wise I have not checked which method is faster. Although this method is in-proc it may be slower since the Abstract Syntax Tree is built as the IL is validated (I'll have to setup a performance test to check this theory).

I found the ILSpy decompiler to be more reliable than PEVerify as pointed out in the above article, in one instance PEVerify declared one assembly to be valid, while ILSpy correctly gave a beautiful stack trace indicating my error in generation.

孤独陪着我 2024-12-10 01:09:14

调试 LCG 允许您调试生成的代码在运行时使用 Windbg。

也许它可以帮助你。

Debugging LCG allows you to debug the generated code at runtime using Windbg.

Maybe it can help you.

拥有 2024-12-10 01:09:14

调用 peverify 确实可能是最好的方法,但根据 .NET 的运行版本,peverify 位于许多不同的目录中。您可以尝试枚举所有这些路径并检查最新的路径,但这在最后计数 IIRC 时至少有 6 个不同的路径,并且不是跨平台的,即。不包括单声道。

我最近发现我可以链接到 Microsoft.Build.Tasks 程序集,然后创建 Microsoft.Build.Tasks.GetFrameworkSdkPath 的实例并调用 Path 属性。我注意到的一个奇怪的行为是,第一次访问路径会引发异常,但如果您只是吞下该异常,则可以从那时起访问该路径。

Peverify.exe 则为 Path.Combine(new GetFrameworkSdkPath().Path, "bin\peverify")。

Calling peverify is indeed probably the best approach, but peverify is located in many different directories depending on the running version of .NET. You can try to enumerate all these paths and check for the latest one, but this was at least 6 different paths at last count IIRC, and isn't cross-platform, ie. doesn't include Mono.

I recently found that I could just link to the Microsoft.Build.Tasks assembly, and then create an instance of Microsoft.Build.Tasks.GetFrameworkSdkPath and call the Path property. One weird behaviour I noticed is that accessing the path the first time throws an exception, but if you just swallow that exception you can access the path from then on.

Peverify.exe is then Path.Combine(new GetFrameworkSdkPath().Path, "bin\peverify").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文