简体   繁体   中英

Why won't my code segfault on Windows 7?

This is an unusual question to ask but here goes:

In my code, I accidentally dereference NULL somewhere. But instead of the application crashing with a segfault, it seems to stop execution of the current function and just return control back to the UI. This makes debugging difficult because I would normally like to be alerted to the crash so I can attach a debugger.

What could be causing this?

Specifically, my code is an ODBC Driver (ie. a DLL). My test application is ODBC Test (odbct32w.exe) which allows me to explicitly call the ODBC API functions in my DLL. When I call one of the functions which has a known segfault, instead of crashing the application, ODBC Test simply returns control to the UI without printing the result of the function call. I can then call any function in my driver again.

I do know that technically the application calls the ODBC driver manager which loads and calls the functions in my driver. But that is beside the point as my segfault (or whatever is happening) causes the driver manager function to not return either (as evidenced by the application not printing a result).

One of my co-workers with a similar machine experiences this same problem while another does not but we have not been able to determine any specific differences.

Windows has non-portable language extensions (known as "SEH") which allow you to catch page faults and segmentation violations as exceptions.

There are parts of the OS libraries (particularly inside the OS code that processes some window messages, if I remember correctly) which have a __try block and will make your code continue to run even in the face of such catastrophic errors. Likely you are being called inside one of these __try blocks. Sad but true.

Check out this blog post, for example: The case of the disappearing OnLoad exception – user-mode callback exceptions in x64

I find it kind of weird the kind of ideas that are being attributed to me in the comments. For the record:

  • I did claim that SEH itself is bad. 声称自己SEH是坏的。

    I said that it is "non-portable", which is true. I also claimed that using SEH to ignore STATUS_ACCESS_VIOLATION in user mode code is "sad". I stand by this. I should hope that I had the nerve to do this in new code and you were reviewing my code that you would yell at me, just as if I wrote catch (...) { /* Ignore this! */ } catch (...) { /* Ignore this! */ } . It's a bad idea. It's especially bad for access violation because getting an AV typically means your process is in a bad state, and you shouldn't continue execution.

  • I did argue that the existence of SEH means that you must swallow all errors. 认为SEH的存在意味着你必须吞下所有的错误。

    Of course SEH is a general mechanism and not to blame for every idiotic use of it. What I said was that some Windows binaries swallow STATUS_ACCESS_VIOLATION when calling into a function pointer, a true and observable fact, and that this is less than pretty. Note that they may have historical reasons or extenuating circumstances to justify this. Hence "sad but true."

  • I did inject any "Windows vs. Unix" rhetoric here. 注入任何“Windows与Unix的”豪言壮语。 A bad idea is a bad idea on any platform. Trying to recover from SIGSEGV on a Unix-type OS would be equally sketchy.

解除引用NULL指针是一种未定义的行为,它几乎可以产生任何东西 - 一个seg.fault,一个写入IRS的信,或一个发布到stackoverflow :)

在这里阅读不同类型的异常处理程序 - 它们没有捕获相同类型的异常。

Windows 7 also have its Fault Tollerant Heap (FTH) which sometimes does such things. In my case it was also a NULL-dereference. If you develop on Windows 7 you really want to turn it off!

What is Windows 7's Fault Tolerant Heap?

http://msdn.microsoft.com/en-us/library/dd744764%28v=vs.85%29.aspx

Attach your debugger to all the apps that might call your dll, turn on the feature to break when an excption is thrown not just unhandled in the [debug]|[exceptions] menu.

ODBC is most (if not all) COM as such unhandled exceptions will cause issues, which could appear as exiting the ODBC function strangely or as bad as it hang and never return.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM