What is a stack imbalance?

后端 未结 2 844
孤城傲影
孤城傲影 2021-01-06 23:33

Having read this article F# Versus Mathematics: Part One - Getting Started with BLAS and LAPACK I stumbled across the term stack imbalance in the paragraph

相关标签:
2条回答
  • 2021-01-07 00:17

    We just saw this the other day (marshalling with c# and c++)

    I refer you to the text from an MSDN page:

    The pInvokeStackImbalance managed debugging assistant (MDA) is activated when the CLR detects that the stack depth after a platform invoke call does not match the expected stack depth, given the calling convention specified in the DllImportAttribute attribute as well as the declaration of the parameters in the managed signature.

    I realize this is for a specific compiler warning but the page gives some info on what a stack imbalance is, what causes it, what the symptoms are, and how to resolve it. As Daniel said it's typically due to the signatures of the managed and unmanaged not matching.

    Hope this helps.

    0 讨论(0)
  • 2021-01-07 00:19

    A stack imbalance occurs when the data structure used to keep track of called functions, arguments, and return values becomes corrupted or misaligned.

    Most times, the stack is a memory pointer that stores the address where control will resume when the current function call exits back to the caller. There are different variants on this, sometimes the arguments to a function are also appended to the stack, as well as the return value. What is most important here is that the caller and callee should agree upon how to restore it back to the prior state when the callee exits. This agreement is frequently known as the Calling Convention.

    In .NET, stack imbalances are a rare to nonexistent problem in pure managed code. However, this can be a frequent problem when calling unmanaged code, as you will need to tell the compiler how the method should be called, which then implies how the stack should be cleaned up per the calling convention.

    On windows, there are a few standard calling conventions that cover the bulk of invocation cases.

    stdcall - Callee will fix the stack upon exit.
    fastcall - Potentially no need to fix the stack aside from return address, instead, CPU registers are used to pass arguments.
    cdecl - The caller will fix the stack after the called function returns.

    A formal reference is available here: Argument Passing and Naming Conventions @ MSDN

    This is also of interest: X86 calling convention list @ Wikipedia

    Within a given development domain, this tends not to be a problem. Each language generally has a convention that is implicit for all method calls. C/C++ uses the same convention for invocation of C/C++ calls, Python for other Python calls, etc. When crossing domains, it can become a problem if one domain doesn't use the same as another. Perhaps most common in windows, a function exported with "C" style declarations (cdecl) may cause an unbalanced stack (or worse) when called as though it had a stdcall convention, which is the method recognized by WINAPI (windows system) calls.

    0 讨论(0)
提交回复
热议问题