SoFunction
Updated on 2025-03-02

A brief discussion on the memory overflow problem of C# interoperability

C# calls C++ DLL code and finds a hidden problem. It is very harmful and not easy to detect.

Probably when declaring a function in C++, there is a pointer of type long. In C# my statement became like this:

public extern void Method(ref uint para);

At first, I didn't find any problem here. I knew that this hidden problem was exposed and changed a variable stated earlier, and then I suddenly realized it.

Copy the codeThe code is as follows:

uint test = 0;
int *p = new IntPtr();
Method(ref test);

When calling Method, the breakpoint is placed, the value of p is an allocated memory address. F10 skips Method, and the p pointer points to 0x00000000!!!;

Initial analysis, only 4 bytes of storing values ​​were allocated to the test on the stack. As a result, 8 bytes were returned through interoperability, and the 4 bytes of the next p pointer address were occupied. It just so happened that these four bytes were high bits again, and the high bits of the returned data were all 0. I have learned about the memory overflow of C++ before, but I didn’t expect that I encountered it in C#. The problem seems to be small. How to get the four bytes that happen to be the return address, maybe it’s a big harm! ! It seems that it is better to be careful when interoperating C#.