gpt4 book ai didi

.net - 为什么 PEVerify 说 CLR 期待静态大小数组的单维数组?

转载 作者:行者123 更新时间:2023-12-01 00:51:35 25 4
gpt4 key购买 nike

此代码验证失败:

.assembly extern mscorlib {}
.assembly Program {}

.method private static void Main() cil managed
{
.entrypoint
.maxstack 3

.locals init ( int32[0 ... 10] a )

ldc.i4 10
newarr int32
stloc.0

ldloc.0
ldc.i4.0
ldc.i4.s 32
stelem.i4

call string [mscorlib]System.Console::ReadLine()
pop
ret
}

出现以下错误:

[IL]: Error: [C:[...]Program.exe : ::Main ][offset 0x0000000F] Expected single dimension array. 1 Error(s) Verifying Program.exe

然而这段代码验证正常:

.assembly extern mscorlib {}
.assembly Program {}

.method private static void Main() cil managed
{
.entrypoint
.maxstack 3

.locals init ( int32[] a )

ldc.i4 10
newarr int32
stloc.0

ldloc.0
ldc.i4.0
ldc.i4.s 32
stelem.i4

call string [mscorlib]System.Console::ReadLine()
pop
ret
}

这是未实现的功能、CLR 或 PEVerify 中的错误,还是仅仅是对用法的误解?这两个程序都执行得很好。

最佳答案

我的猜测 - 这只是一个猜测 - 这与向量和数组之间的区别有关。

ECMA CLI Specification (第二部分,第 14.1 和 14.2 节)是这样说的:

Vectors are single-dimension arrays with a zero lower bound. They have direct support in CIL instructions (newarr, ldelem, stelem, and ldelema).

[...]

While vectors have direct support through CIL instructions, all other arrays are supported by the VES by creating subtypes of the abstract class System.Array.

向量是使用 T[] 语法声明的,而数组可以使用 T[n]T[p,q]T[x...y] 等。因此,在您的第一个示例中,int32[0 ... 10] 语法是一个 数组声明。在您的第二个示例中,int32[] 语法是一个vector 声明。

我的猜测是验证器反对您的第一个示例,因为它认为您正试图在数组newarr 和stelem > 而不是向量。据推测,验证者只检查声明的类型,而不考虑数组的等级、边界等。这两个示例都正确执行的原因是因为下限为零的一维数组一个向量,无论是出于何种意图和目的。

至于为什么错误消息说“预期的一维数组”而不是“预期的向量”,我不知道!

关于.net - 为什么 PEVerify 说 CLR 期待静态大小数组的单维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8610224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com