开发者

What is a stack imbalance?

开发者 https://www.devze.com 2023-03-21 20:42 出处:网络
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 A Warning, Perhaps an Omen.

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 A Warning, Perhaps an Omen.

I googled and 开发者_运维技巧searched on SO, but could only find people struggling with stack imbalances and no generall explanations.

Bonus Question: Does it only affect f# or is it a general problem in C, C++, Python, Java, etc.?

p.s. please change the tags of the question if necessary


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号