我可以编写,编译并成功运行以下IL程序,其.maxstack大小设置为1,这太低了,因为程序在一个时间点在堆栈上有两个值(即2 + 2 == 4)。这个计划 才不是 在CLR中崩溃,并以“Hello World”的所有预期输出结束执行,后跟数字4。
但是,该程序(正确地)不会传递PEVerify,它指出了堆栈溢出异常,并带有以下消息:
Microsoft(R).NET Framework PE Verifier。版本4.0.30319.18020 版权所有(c)Microsoft Corporation。版权所有。
[IL]:错误:[C:\ tmp \ hello.exe:HelloWorld1.Program :: Main] [偏移量 0x00000011]堆栈溢出。 1错误验证hello.exe
为什么它不会在CLR中崩溃?
.assembly extern mscorlib {}
.assembly SampleIL {
.ver 1:0:1:0
}
.class private auto ansi beforefieldinit HelloWorld1.Program
extends [mscorlib]System.Object
{
// Methods
.method private hidebysig static
void Main (
string[] args
) cil managed
{
// Method begins at RVA 0x2050
// Code size 13 (0xd)
.maxstack 1 // **** NOTE THIS LINE *****
.entrypoint
IL_0000: nop
IL_0001: ldstr "hello world"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
ldc.i4 2
ldc.i4 2
add
call void [mscorlib]System.Console::WriteLine(int32)
IL_000c: ret
} // end of method Program::Main
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x205e
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Program::.ctor
} // end of class HelloWorld1.Program