为什么.MaxStack指令可选在MSIL code? [英] Why Is the .MaxStack Directive Optional in MSIL Code?

查看:180
本文介绍了为什么.MaxStack指令可选在MSIL code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习汇编语言在我的业余时间。任何人都可以解释为什么.maxstack似乎是可选此计划。我试图找到答案在网上和我有没有这样的运气书即程序编译并运行.Maxstack注释掉:

I am learning assembly language in my spare time. Can anyone explain why .maxstack appears to be optional in this program. I have tried to find the answer online and in my book with no such luck i.e. the program will compile and run with .Maxstack commented out:

//Add.il
//Add Two Numbers

.assembly extern mscorlib {}

.assembly Add
{
    .ver 1:0:1:0
}
.module add.exe

.method static void main() cil managed
{
    //.maxstack 2
    .entrypoint

    ldstr "The sum of 50 and 30 is = "
    call void [mscorlib]System.Console::Write (string)

    ldc.i4.s 50
    ldc.i4 30    
    add
    call void [mscorlib]System.Console::Write (int32)
    ret
}

我在命令行中使用ILASM工具编译程序,然后运行所产生的executeable。

I am compiling the program at the command line using the ILASM tool and then running the generated executeable.

推荐答案

我觉得从什么 .maxstack 实际上做了误解你的困惑茎。这是一个容易犯的错误,因为它看起来像它会在执行时可能会导致错误。出人意料的是,特定的指令实际上有什么做的与堆栈大小在运行时,相反,它是专门在code验证中使用。

I think your confusion stems from a misunderstanding of what .maxstack actually does. It's an easy mistake to make, because it seems like it would cause an error when executing. Surprisingly, that particular directive actually has nothing do with the with stack size at runtime, instead, it is specifically used during code verification.

从分区三 - 第1.7.4节

From Partition III - Section 1.7.4

注意:MAXSTACK是关系到节目的分析,而不是到堆栈在运行时的大小。它没有指定在一个堆栈帧的字节的最大尺寸,而是必须由分析工具被跟踪的项数。

Note: Maxstack is related to analysis of the program, not to the size of the stack at runtime. It does not specify the maximum size in bytes of a stack frame, but rather the number of items that must be tracked by an analysis tool.

在code变无法证实。同一部分,指出,任何符合标准的实现的无需的支持与无效的最大堆栈值的方法。但是,它并没有说,它的切不可的,并且很清楚,在运行时执行code。所以,如果它似乎没有任何效果,为什么要花费心思呢?

The code becomes unverifiable. That same section, notes that any conforming implementation need not support a method with an invalid max stack value. However, it doesn't say that it must not, and quite clearly, the runtime is executing the code. So if it seems to have no effect, why even bother having it?

不管你信不信,默认的是,.NET框架运行的无法证实的code。它实际上我很难弄清楚如何使验证在.NET 4.0中,但如果你打开​​CAS,你的计划( .maxstack 1 )将停止运行。与

Believe it or not, by default, the .NET framework runs unverifiable code. It was actually difficult for me to figure out how to enable verification in .NET 4.0, but if you do turn on CAS, your program (with .maxstack 1) will stop running with

未处理的异常:System.InvalidProgramException:公共语言运行库检测到无效的程序。     在主()

Unhandled Exception: System.InvalidProgramException: Common Language Runtime detected an invalid program. at main()

记住这一点,无法核实的code不能在不具有完全信任(从互联网上一般组件)的任何环境中运行。如果这不是重要的是你,你可以让它成为一个无效的值,它真的不会有所作为。如果的 code 的本身仍然是正确的,它将会运行得很好;当然,如果有一个实际的问题,与IL栈,它会抛出一个 InvalidProgramException

Keeping this in mind, unverifiable the code cannot run in any environment that doesn't have full trust (generally assemblies from the internet). If that's not important to you, you can let it be an invalid value, and it really won't make a difference. If the code itself is still correct, it will run fine; of course if there is an actually an issue with the IL stack, it will throw an InvalidProgramException.

这篇关于为什么.MaxStack指令可选在MSIL code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆