Hi Marek,
Thanks for your feedback.
I have tried to a configuration file and environment variable to enable MDA
in CLR for both debug build and release build. When the p/invoke call stack
is not balance, I can get the MDA PInvokeStackImbalance, which causes a JIT
debugger to be selected. Do you get the same behavior as me?
Also, I have got confirmation from our product team that although we can
enable MDA at runtime, it is not shown out as exception, try...catch will
have no effect on this. It is something like Assert, not exception. So if
you just want a JIT to popup, yes, you can enable MDA at runtime. However,
there is no way to catch it as an exception.
Hope this helps
Best regards,
Jeffrey Tan
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Marek - 27 Jan 2006 08:51 GMT
Hi Jeffrey
Thanks very much for looking into this for me. I can't really let my end
users be confronted with with a JIT window. Is there anything that could be
done in code to test the state of the stack before and after the invoke call
which would provide me with any information that I could use to determine
whether a stack imbalance had occurred?
Thanks again,
Marek
> Hi Marek,
>
[quoted text clipped - 18 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 27 Jan 2006 09:17 GMT
Hi Marek,
Thanks for your feedback.
Yes, I have already consulted our product team for this issue. However,
they confirmed that MDA can not help in this scenario. If you are curious,
below is the reply email content:
"The StackImbalance MDA also isn’t a catch-all. If you’ve got an
application model where the user can cause the CLR to invoke arbitrary
native functions calls, then it’s ALWAYS going to be unsafe and have the
potential to cause corruption. Invoking native methods is inherently
unsafe. The application could use various heuristics to try and weed out
some obvious errors, but the CLR doesn’t have any direct support to help
them with that."
Thanks
Best regards,
Jeffrey Tan
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Marek - 27 Jan 2006 09:30 GMT
Hi Jeffrey
Thank you again. Please could you point me in the right direction of these
"various heuristics".
Best regards,
Marek
> Hi Marek,
>
[quoted text clipped - 18 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 31 Jan 2006 01:45 GMT
Hi Marek,
If you are curious, below is the feedback from our product team:
"In theory an app could do something like PInvoke to their own native
wrapper function which would do the work of calling the user-specified
native method and trying to detect some forms of corruption that can result
from an incorrect call (specifically looking for stack imbalance). Doing
this however would be non-trivial and would require a good understanding of
the native calling convention etc. Regardless, there are many other ways
an incorrect native call could corrupt memory which would be difficult or
impossible to detect, and so anything the application were to do here would
only provide partial protection (and perhaps a false sense of security).
For that reason, I don't think we should be encouraging the customer taking
this approach"
Hope this helps
Best regards,
Jeffrey Tan
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.