On Oct 27, 2008, at 8:33 AM, Hueneke, Immo wrote:
Hi all,
When using JNI, there is a well known method of crashing the JVM by
changing the value of the FPU control word within the native code
and failing to restore it. Whenever the VM executes the so-called
“Safepoint Blob” to switch threads, it has to pop all the floating-
point registers into a thread context area and does not expect the
FPU to raise an exception when it underflows. Some native libraries
need to unmask floating-point exceptions, so to honour the JNI
contract they should restore the CW register afterwards (a
relatively slow operation, which is why the JNI implementation
doesn’t do this).
I can’t find any documentation on this issue where JNA is concerned.
Does JNA safeguard the FPUCW each time execution passes between Java
and native code, or does the native library have to conform to a
contract similar to JNI? Are there any documented instances of VMs
crashing sporadically with error messages along the following lines?
- Java HotSpot(TM) Client VM warning: Floating point
control word changed by native JNI code.
- An unexpected error has been detected by HotSpot Virtual
Machine: EXCEPTION_ACCESS_VIOLATION
- EXCEPTION_FLT_DIVIDE_BY_ZERO
- EXCEPTION_FLT_STACK_CHECK
Thanks in advance for any information.
More background: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6346124
and http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5105765
JNA doesn't do anything to preserve the control word state. No one's
reported errors, but that would be dependent on whether anyone is
using a library which changes the control word state (same as JNI).
Having an optional save/restore option on a per-call basis in JNA
might be preferable to the global JVM workarounds, but none exists at
the moment.
T.