-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Access Violation in Marshal.StructureToPtr is not translated to the AccessViolationException #13805
Comments
@kirsan31 This behavior isn't something we are likely to change since we are officially done with the .NET Framework porting project: dotnet/announcements#130. This is non-consistent behavior with .NET Framework and we can consider this in the future, but ensuring this kind of behavior in a fully xplat manner isn't something we are likely to give high priority. Is there a specific scenario where this behavior is desired? Perhaps we can provide an alternate approach in addressing the issue. /cc @jkotas |
Brief description of our problem: do some action on any crash, before process exit. Or other ppl want to prevent some known crashes due to AV, for example from native call. Quote from original post:
And the answer was:
And now we see, that even this approach is not working at all cases :( |
Note that there is an internal contract violation in coreclr.dll, which is worth fixing anyway. Depending on how you fix it (e.g., by removing the |
@AntonLapounov It would require more than I don't have a strong opinion here about how the exception logic should or should not be updated. I will say that from an interop perspective catching any A/V should be discourage since the entire state of the runtime is in question. I would like to hear from @janvorli or @jkotas about what they want here. I have a fix locally so it isn't too much of an issue other than the tactical decision to permit these kind of scenarios. |
/cc @sergiy-k |
The EH works with the assumption that hardware exceptions should be allowed to translate to managed exceptions (like NullReferenceException, etc) only in managed code or in a small number of specific places in the native parts of the runtime - in asm helpers that run on behalf of the managed code (like GC write barriers). For these helpers, we pretend the exception happened in the managed caller at the call site. On Windows only, these exceptions are also allowed to leak from external dlls. There is also a holder AVInRuntimeImplOkayHolder which allows AVs in parts of code guarded by that holder to be handled in the native code and not shoot down the runtime. Besides the debugger code, there are only 26 occurences of that holder in the runtime at very specific places. And this holder has an effect only on Windows. Hardware exceptions happening anywhere else in the runtime / gc lead to FailFast, since they mean a corrupted state where attempting to handle them could possibly lead to even worse consequences. Hardware exceptions happening in external native dlls are supposed to be handled inside of those dlls, since the runtime has no idea whether such exceptions are allowed to happen there or not. But in case a hardware exception occurs in an external dll, it is not handled in there and leaks into our runtime, we try to handle it, but only on Windows. |
The only reliable way to do this is to start a second watchdog process when you app starts, and does the action when it sees that the app exited.
It did not work in all cases in .NET Framework either. |
Yea, we definitely will do this way.
Of course, but on practice for about 15 yeas we didn't see one. But I agree that this is not a substantive conversation already. One question that remain, is the way to diagnose - get crash details? You mentioned it here. Can we hope for such a feature? |
If you are on Windows and have the watchdog process, the easiest thing is to get the crash details from Event Log. |
Yea...
.net 4.7.2 from AppDomain.CurrentDomain.UnhandledException:
I think difference is obvious... |
The runtime tries to log the stacktrace when it seems safe to execute the additional code required to log the stacktrace. It is a best effort and the behavior will differ between runtimes and runtime versions. There are other cases where both .NET Framework and .NET Core log the error without stacktrace, for example:
Also, there are cases where .NET Core logs the error with stacktrace, but .NET Framework logs the error without the stacktrace. If you would like to extract as many details as possible from the crash, you may want to look at analyzing the crash dumps using https://github.com/microsoft/clrmd. |
When we set
COMPlus_legacyCorruptedStateExceptionsPolicy
environment variable to 1, we expect to catch AccessViolationException in .net core program. It's not working in case of StructureToPtr (some other cases?) - AccessViolationException not capturing.Test program:
Detailed explanation of the bug from @AntonLapounov.
Original question on stackoverflow.
Related? question on stackoverflow.
/cc @janvorli
The text was updated successfully, but these errors were encountered: