Why does a program with a PEVerified Stack Overflow Scenario (maxstack) Not Crash the CLR?
Asked Answered
R

1

11

I can write, compile and successfully run the following IL program with a .maxstack size set to 1 which is too low because the program has two values on the stack at one point in time (i.e. 2+2==4). This program does not crash in the CLR and finishes executing with all the expected output of "Hello World" followed by the number 4.

However this program will (rightfully) not pass PEVerify which points out a stack overflow exception with the following message:

Microsoft (R) .NET Framework PE Verifier. Version 4.0.30319.18020 Copyright (c) Microsoft Corporation. All rights reserved.

[IL]: Error: [C:\tmp\hello.exe : HelloWorld1.Program::Main][offset 0x00000011] Stack overflow. 1 Error(s) Verifying hello.exe

Why will it not crash in the 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
Redemptioner answered 29/5, 2014 at 1:16 Comment(7)
.maxstack declares the maximum depth of the CLR stack used by a method. This is not the same as the maximum size of the CPU stack. They are both called stacks but they are not the same stack. In particular, the CLR stack is imaginary. It is a theoretical construct of the virtual machine.Irreducible
@RaymondChen Why couldn't you have posted that before I finished writing up my answer that assumed the latter? :-)Damascene
@JonathonReinhart, if your deleted answer contains valuable technical information to supplement learning can you undelete it. I'm unsure if you deleted it because you think it contains wrong info or if it's because you thought you overlapped with +RaymondChen's comment.Redemptioner
@JohnK It's incorrect. Raymond has indicated that .maxstack has nothing to do with the actual CPU stack, to which my answer was referring.Damascene
@RaymondChen It leaves me wondering of the usefulness then. Almost seems akin to HTML Content-Length whereby browsers might interpret it correctly despite absence or error. Do you think that maxstack is relegated to being a simple CPU stack check then, or maybe is is just practically useless but being kept for theoretical correctness. For example, if the CLR was created as a hardware machine it might be used.Redemptioner
"This value is mainly used by IL code verification and certain JIT compilers that want to know up front how deep the IL stack will be."Irreducible
@RaymondChen thanks for the link. That thread also mentions "Reading the CIL specification I see that maxstack is "related to the analysis of the program, not the size of the stack at runtime" Good info.Redemptioner
R
9

Answer derived from the question comments via @RaymondChen

Common Language Infrastructure (CLI)
Partition III
CIL Instruction Set
Final Draft, Apr 2005

1.7.4 Must provide maxstack

[... snip ... ]
[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 shall be tracked by an analysis tool. end note]

Redemptioner answered 29/5, 2014 at 3:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.