United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6636110 unaligned stackpointer leads to crash during deoptimization
JDK-6636110 : unaligned stackpointer leads to crash during deoptimization

Details
Type:
Bug
Submit Date:
2007-11-30
Status:
Closed
Updated Date:
2012-10-15
Project Name:
JDK
Resolved Date:
2012-01-23
Component:
hotspot
OS:
windows_xp
Sub-Component:
compiler
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
5.0u13
Fixed Versions:
hs23 (b06)

Related Reports
Backport:
Backport:
Backport:
Backport:
Relates:

Sub Tasks

Description
FULL PRODUCT VERSION :
This error is in the Hotspot 1.5 and 1.6, as far as I see in the sources.

FULL OS VERSION :
Microsoft Windows [Version 5.2.3790]
x64 (amd64)


A DESCRIPTION OF THE PROBLEM :
This bug occurs in SAP's Java VM 5, which is based on the 1.5 Hotspot sources.

We found a bug which caused under rare circumstances a crash on Windows AMD64. In theory, this bug might happen on all AMD64 platforms. It also seems to affect 1.6 and 1.7 as well, according to the sources.

----

The following explanation and the source line numbers refer to cpu/amd64/runtime_amd64.cpp as of update release 13.

The offending function is

void SharedRuntime::generate_deopt_blob().


That function generates a CodeBlob which handles deoptimization. Inside that function (lines 395ff) Interpreter frames are pushed in a loop onto the stack.

Then, after repushing the deopt frame itself, the function Deoptimization::unpack_frames() is called (line 432). Deoptimization::unpack_frames() is a "normal" C++ function, not generated at runtime.

The problem is that at the point of that call, the stackpointer is not guaranteed to be 16 byte aligned. The alignment is pretty random - depending on the number of interpreter frames pushed and of their size.
This violates the ABI for Windows x64, but normally does not cause an error, only when 128bit instructions are used.

This exactly happened at SAP. We did add some tracing code to Deoptimization::unpack_frames() and recompiled, and the C++ compiler decided as part of an optimization to
use SSE instructions. In particular it generated code like this:

movaps [rsp + ...0h], xmm0

which relies on rsp being 16 byte aligned. This was not the case and the VM crashed.

We think that if C++ code gets called from within generated code, it should follow the ABI exactly.

----

I fixed the problem in our own (SAPs) VM by nesting the call to Deoptimization::unpack_frames() into a little wrapper subroutine, where I align the stackpointer before the call into the runtime.
If you are interested in this fix, we can give it to you (I did not find any way to submit a bug fix to 1.5 on your website).


THE PROBLEM WAS REPRODUCIBLE WITH -Xint FLAG: No

THE PROBLEM WAS REPRODUCIBLE WITH -server FLAG: Did not try

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just debug the VM and check the stackpointer inside Deoptimization::unpack_frames. Sometimes it will be 16-byte-aligned, sometimes not. If not, the code under it might crash.

Also, you could simply emit an SSE instruction just before the call to Deoptimization::unpack_frames(). Something like this:

; make room for one xmm register while preserving alignment
sub  rsp, 0x10

; next instruction will crash if done with unaligned rsp:
movaps  [rsp], xmm0

; restore stack pointer
add rsp, 0x10

This will crash very soon, proving that alignment is random at this point.

REPRODUCIBILITY :
This bug can be reproduced rarely.

                                    

Comments
EVALUATION

http://hg.openjdk.java.net/hsx/hotspot-comp/hotspot/rev/1feb272af3a7
                                     
2011-11-05
EVALUATION

http://hg.openjdk.java.net/hsx/hotspot-rt/hotspot/rev/1feb272af3a7
                                     
2011-11-15
EVALUATION

http://hg.openjdk.java.net/hsx/hotspot-emb/hotspot/rev/1feb272af3a7
                                     
2011-11-29
EVALUATION

http://hg.openjdk.java.net/lambda/lambda/hotspot/rev/1feb272af3a7
                                     
2012-03-22



Hardware and Software, Engineered to Work Together