c# - Visual Studio 编译器如何将安全属性编译为 CIL?

标签 c# cil il ildasm ilasm

我的类中的方法具有以下属性 SecurityPermission(SecurityAction.Assert)。我编译它(调试构建)并通过查看原始堆和查看包含 PermissionSet blob 的 blob 堆来查看 ildasm.exe 中的输出。我期望的(根据 ECMA-335)是:

2e 01 80 84 53 79 73 74  65 6d 2e 53 65 63 75 72 >.   System.Secur<
69 74 79 2e 50 65 72 6d  69 73 73 69 6f 6e 73 2e >ity.Permissions.<
53 65 63 75 72 69 74 79  50 65 72 6d 69 73 73 69 >SecurityPermissi<
6f 6e 41 74 74 72 69 62  75 74 65 2c 20 6d 73 63 >onAttribute, msc<
6f 72 6c 69 62 2c 20 56  65 72 73 69 6f 6e 3d 32 >orlib, Version=2<
2e 30 2e 30 2e 30 2c 20  43 75 6c 74 75 72 65 3d >.0.0.0, Culture=<
6e 65 75 74 72 61 6c 2c  20 50 75 62 6c 69 63 4b >neutral, PublicK<
65 79 54 6f 6b 65 6e 3d  62 37 37 61 35 63 35 36 >eyToken=b77a5c56<
31 39 33 34 65 30 38 39  00 00

但是我看到的是这样的:

2e 01 80 84 53 79 73 74  65 6d 2e 53 65 63 75 72 >.   System.Secur<
69 74 79 2e 50 65 72 6d  69 73 73 69 6f 6e 73 2e >ity.Permissions.<
53 65 63 75 72 69 74 79  50 65 72 6d 69 73 73 69 >SecurityPermissi<
6f 6e 41 74 74 72 69 62  75 74 65 2c 20 6d 73 63 >onAttribute, msc<
6f 72 6c 69 62 2c 20 56  65 72 73 69 6f 6e 3d 32 >orlib, Version=2<
2e 30 2e 30 2e 30 2c 20  43 75 6c 74 75 72 65 3d >.0.0.0, Culture=<
6e 65 75 74 72 61 6c 2c  20 50 75 62 6c 69 63 4b >neutral, PublicK<
65 79 54 6f 6b 65 6e 3d  62 37 37 61 35 63 35 36 >eyToken=b77a5c56<
31 39 33 34 65 30 38 39  01 00

请特别注意末尾的 01 00,而我期望的是 00 00。规范说,在计数的字符串之后应该是命名参数的数量。由于我没有传递任何命名参数,因此我希望该数字为 16 位 0。

这是使用 Visual Studio 2013 针对 .NET 2.0 编译的。

更复杂的是,如果我添加一个命名参数,我会得到这个:

2e 01 80 84 53 79 73 74  65 6d 2e 53 65 63 75 72 >.   System.Secur<
69 74 79 2e 50 65 72 6d  69 73 73 69 6f 6e 73 2e >ity.Permissions.<
53 65 63 75 72 69 74 79  50 65 72 6d 69 73 73 69 >SecurityPermissi<
6f 6e 41 74 74 72 69 62  75 74 65 2c 20 6d 73 63 >onAttribute, msc<
6f 72 6c 69 62 2c 20 56  65 72 73 69 6f 6e 3d 32 >orlib, Version=2<
2e 30 2e 30 2e 30 2c 20  43 75 6c 74 75 72 65 3d >.0.0.0, Culture=<
6e 65 75 74 72 61 6c 2c  20 50 75 62 6c 69 63 4b >neutral, PublicK<
65 79 54 6f 6b 65 6e 3d  62 37 37 61 35 63 35 36 >eyToken=b77a5c56<
31 39 33 34 65 30 38 39  12 01 54 02 0d 55 6e 6d >1934e089  T  Unm<
61 6e 61 67 65 64 43 6f  64 65 01                >anagedCode      <

再一次查看属性的计数字符串的末尾,您可以看到 12 01 后跟命名参数列表(一项列表)。我预计这是 01 00,一个 16 位小尾数法 1,表示命名参数的数量。

基于此,我假设计数字符串后的第二个字节是命名参数计数,但我仍然不明白第一个字节是什么(第一个示例中的 0x01,下一个示例中的 0x12)。

如果我添加第二个命名属性,第一个字节变为 26,如果我添加第三个命名属性,它变为 33。我没有看到数字的明显模式,除了它们在增加这一事实.

我问这个问题是因为我正在尝试手动构建一个 PermissionSet blob(我正在编写一个 CLR 分析器)并且我需要知道在该字节中放入什么。

最佳答案

我认为你的沮丧是正确的,我记得我以前使用命名参数的经验 - NumNamed 是作为压缩 int 而不是规范中规定的 int16 实现的,并且与§VI.B.3 给出的示例。我不知道这在后续的 .NET 3.0+ 实现中是否有所改变。

对于您正在寻找的PermissionSet

... * A set of properties, encoded as the named arguments to a custom attribute would be (as in §II.23.3, beginning with NumNamed).

... §II.23.3 ...

Next is a description of the optional “named” fields and properties. This starts with NumNamed – an unsigned int16 giving the number of “named” properties or fields that follow. ... In the case where NumNamed is non-zero, it is followed by NumNamed repeats of NamedArgs.

12 01 是小端整数,“给出后面的属性数”。一个命名属性,总长度为 18(十进制,含)。

这个总长度是您正在寻找的模式,但要小心,因为我认为这个长度是可选的,有时编译器会设法将属性数打包到前面的 int16 中,而丢弃长度。您必须尝试使用​​不同数量的属性,以确保您正确解析了所有情况。

NamesArgs:

... SerString – a PackedLen count of bytes, followed by the UTF8 characters

PROPERTY is the single byte 0x54. The FieldOrPropName is the name of the field or property, stored as a SerString (defined above)

关于c# - Visual Studio 编译器如何将安全属性编译为 CIL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20753526/

相关文章:

c# - DotNetZip 不从 WinZip 中提取最佳压缩

c# - .NET 函数反汇编

c# - 如何找到带有 params 参数的方法的用法,以使参数不为空?

compilation - 如何将CIL转换为LLVM IR?

.net - callvirt 在底层是如何工作的?

c# - 为什么加入 beforefieldinit 会大幅提升泛型类的执行速度?

c# - 为什么图像的流媒体源不起作用?

c# - 二维集的 LINQ 新手问题

c# - 即使异常将 DbNull 作为类型抛出,也无法将具有空值的 DbGeometry 与 DbNull 进行比较

c# - 如何在DynamicMethod中调用DynamicMethod