Is it possible to handle a SEGFAULT orginating in native code?
Asked Answered
M

2

9

I have a Java 1.6 application that accesses a third party native module, through a JNI class provided as the interface. Recently we noticed that a SEGFAULT is occurring in the native module, and is causing our application to crash. Is it possible to catch and handle this event, at least to log it properly before dieing?


I tried both Java techniques in the article from kjp's answer. Neither worked. Attempting to install a signal handler on 'SEGV' results in the exception

Signal already used by VM: SEGV

The shutdown handler I installed simply failed to fire, presumably because of what the IBM article states:

Shutdown hooks will not be run if

Runtime.halt() method is called to terminate the JVM. Runtime.halt() is provided to allow a quick shutdown of the JVM.
The -Xrs JVM option is specified.
The JVM exits abnormally, such as an exception condition or forced abort generated by the JVM software.

Madel answered 4/7, 2012 at 19:46 Comment(6)
Are public debugging symbols available for the native code? If you can find more about why it's happening, you might be able to submit a comprehensive enough bug report to get them to fix their library.Vintner
@Vintner Working on that in parallel. They'll be getting more info than they want. Was hoping to have a backup plan.Madel
For example, on Windows you can use structured exception handling (SEH) to catch a seg fault. If you could write a native wrapper which calls their code that might be an option. It really depends on the reason for the SEGV though, a bad pointer dereference can be caught this way, but heap/stack corruption can also a SEGV and catching this is likely to lead to more issues.Vintner
@Vintner AIX 64bit, using the IBM 64bit JVM.Madel
@Vintner I don't really want to do anything on catching a failure besides log it and notify.Madel
If you'ved created the JVM yourself using JNI, it appears you could log something on abnormal termination: ibm.com/developerworks/java/library/i-signalhandlingVintner
F
2

If all you want to do is log and notify you can write a script which runs your application. When the application dies, the script can detect whether the application terminated normally and from the hs_errXXXX file which has all the crash/SEGV information and mail it to someone (and restart the application if you want)


What you need to do is to run the faulty JNI code in another JVM and communicate with that JVM using RMI or JMS or Sockets. This way when the library dies, it won't bring down your main application and you can restart it.

Fabri answered 4/7, 2012 at 19:48 Comment(1)
Some significant performance implications with doing this... It'd have to be the only option left to tempt me to go this way.Vintner
M
1

Based on several weeks of research at the time, as well as conversations with JVM engineers at a conference this is not possible. The system will not let you install a SignalHandler on the SEGV signal.

Madel answered 8/11, 2016 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.