Open sandboxFocusImprove this doc

PS0050: Stack Underflow in a Woven Method

Severity Warning
Reported by PostSharp
Message Stack underflow in method "<Method>" after instruction <InstructionIndex> of sequence <Sequence>: the stack size at this point is <StackSize>.

Cause

The IL of a method declares the maximal size of its evaluation stack. When PostSharp writes a method whose body it modified, for example to add the code of an aspect, it computes this size again by following the instructions of the method and counting the values that each one pushes and pops.

PostSharp reports PS0050 when this count becomes negative: an instruction pops a value that no previous instruction pushed. <InstructionIndex> is the number of instructions that PostSharp has followed in the method, <Sequence> identifies the block of instructions, and <StackSize> is the negative size. PostSharp then continues with an empty stack, but the method is invalid, and the runtime usually rejects it with an InvalidProgramException when it is called.

The C# and Visual Basic compilers produce valid IL, so the invalid IL comes from one of these sources:

  • A plug-in that uses the PostSharp SDK to modify method bodies, and emits wrong instructions.
  • Another tool that rewrote the assembly before PostSharp, such as an IL weaver or an obfuscator, and left an invalid method that an aspect of PostSharp then modifies.
  • A defect of PostSharp.

This message depends on invalid IL, which the compilers of the samples of this documentation cannot produce, so no example can show it.

Resolution

  • Disable, one at a time, the tools and the PostSharp plug-ins that rewrite the assembly before PostSharp, and find the one that causes the message. Run ILVerify on the assembly before PostSharp to check whether its IL is already invalid.
  • When you develop the plug-in, check the instructions that it emits in <Method>: each one must find on the stack the values that it pops.
  • When the message persists with only the aspects of PostSharp, report the defect to PostSharp support, with the method that the message names and the aspects applied to it.

See Also

Other Resources

Debugging Build-Time Logic