Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pinvokestackimbalance on Windows memset call #50

Open
RafaelThome opened this issue Oct 26, 2023 · 1 comment
Open

pinvokestackimbalance on Windows memset call #50

RafaelThome opened this issue Oct 26, 2023 · 1 comment

Comments

@RafaelThome
Copy link

RafaelThome commented Oct 26, 2023

We use Blake2b on a WPF application.

We updated the version from 1.1.12 to 2.0.0 and the library started to throw a Managed Debugging Assistant 'PInvokeStackImbalance' on the Isopoh.Cryptography.Blake2B.Create(Blake2BConfig? config, SecureArrayCall secureArrayCall) method.

image

Though the throw only occurs in debug, it also can cause troubles in release.

I could fix the issue by changing the memset PInvoke Calling Convention to CDecl

image

That is because ntdll exposes NAPI (Native API), lower level functions than other Win32API DLLs.

Some of the NAPI are also part of the CRT (C Runtime). All of those use the C calling convention (CDECL) instead of STDCALL (the default calling convention for higher level Win32 APIs)

In CDECL, the caller is responsible for freeing stack memory allocated by the call. In STDCALL that responsibility is from the calee.

That leaves the call with an unexpected stack size, which is the reason why the Managed Debugging Assistant throws the 'PInvokeStackImbalance' when debugging.

In a release build the PInvokeStackImbalance is not thrown, but some relate it could cause memory leak or stackoverflow in extreme cases.

Anyway, it is very bad for debugging and it is simply not the correct behavior.

That was tested in a x64 Windows 11, but there is no change in that for other Windows versions.

@apg1034
Copy link

apg1034 commented Feb 12, 2024

I have the same issue if I use Argon2:

Managed Debugging Assistant 'PInvokeStackImbalance'
A call to PInvoke function 'Isopoh.Cryptography.SecureArray!Isopoh.Cryptography.SecureArray.WindowsNative.UnsafeNativeMethods::WindowsMemset' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants