.NET 程序集是否可以独立于它可能引用的任何其他程序集进行验证?

发布于 2024-12-15 12:02:11 字数 65 浏览 3 评论 0原文

换句话说:对于要验证的 .NET 程序集,是否也需要读取和分析任何引用的程序集? PEVerify 工具有什么作用?

In other words: For a .NET assembly to be verified, do any of the referenced assembly need to be read and analyzed too? What does PEVerify tool do?

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

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

发布评论

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

评论(1

亽野灬性zι浪 2024-12-22 12:02:11

简短回答:是的,PEVerify 需要加载引用的程序集。

汇编格式非常独立。但 PEVerify 确实需要在引用的程序集中检查一些内容。

泛型实例化

在程序集中,当您使用泛型类型或泛型方法时,原始泛型数量不会保留,并且您必须加载类型或方法的定义才能正确验证实例化,两者都是为了数量(实例化具有适当数量的泛型参数?)以及约束(泛型参数是否满足泛型参数上指定的泛型约束?)。如果 PEVerify 找不到引用的程序集,验证将失败。

引用的成员可访问性

如果调用方法或操作另一个程序集中定义的字段,PEVerify 将尝试加载定义该成员的程序集以检查该成员是否具有适当的可见性。

如果找不到程序集,它将无法验证,否则会出现“无法解析令牌”错误。


还有其他几种情况,要正确读取一段元数据,您必须解析对类型的引用,从而加载其包含的程序集,但我不认为 PEVerify 检查它们:

包含枚举的自定义属性实例化:

例如:

[AttributeUsage (AttributeTargets.Field)]

您必须加载 AttributeTargets 的定义才能知道它由序列化自定义属性表单中跨越 4 个字节的 int32 支持。

其值以二进制形式序列化的字段:

某些编译器可能决定在程序集中以二进制形式存储常量值。在常量的类型不是已知原语的情况下,您必须解析对此类型的引用才能知道其大小。


但话又说回来,我不认为 PEVerify 会检查这些情况。我实际上很确定它不会检查自定义属性的编码,对于第二项,我不太确定。

Short answer: yes, PEVerify needs to load referenced assemblies.

The assembly format is pretty self contained. But there's indeed a few things that PEVerify needs to check in referenced assemblies.

Generic instantiations

In the assembly, when you use a generic type or a generic method, the original generic arity is not preserved, and you have to load the definition of the type or of the method to properly verify the instantiation, both for arity (does the instantiation have the proper number of generic arguments?) and for constraints (does the generic argument satisfy the generic constraint specified on the generic parameter?). The verification will fail if PEVerify can not find the referenced assemblies.

Referenced members accessibility

If you call a method or manipulate a field defined in another assembly, PEVerify will try to load the assembly defining the member to check that the member has an appropriate visibility.

It will fail to verify if it can not find the assembly, with an “Unable to resolve token” error otherwise.


There's a couple of other cases where to properly read a piece of metadata you have to resolve a reference to a type, and thus, load its containing assembly, but I don't think PEVerify checks them:

Custom Attribute instantiation containing enums:

For instance:

[AttributeUsage (AttributeTargets.Field)]

You have to load the definition of AttributeTargets to know that it's backed by an int32 spanning over 4 bytes in the serialized custom attribute form.

Fields whose values are serialized in a binary form:

Some compiler may decide to store a constant value in a binary form in the assembly. In the case where the type of the constant is not a known primitive, you have to resolve the reference to this type to know its size.


But then again, I don't think PEVerify checks those cases. I'm actually pretty sure it doesn't check the encoding of custom attributes, for the second item, I'm not so sure.

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