1007N/A/*
1007N/A * Copyright 2009 SAP AG. All Rights Reserved.
1007N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1007N/A *
1007N/A * This code is free software; you can redistribute it and/or modify it
1007N/A * under the terms of the GNU General Public License version 2 only, as
1007N/A * published by the Free Software Foundation.
1007N/A *
1007N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1007N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1007N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1007N/A * version 2 for more details (a copy is included in the LICENSE file that
1007N/A * accompanied this code).
1007N/A *
1007N/A * You should have received a copy of the GNU General Public License version
1007N/A * 2 along with this work; if not, write to the Free Software Foundation,
1007N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1007N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
1007N/A */
1007N/A
1007N/A/**
1007N/A * @test
1007N/A * @bug 6880034
1007N/A * @summary SIGBUS during deoptimisation at a safepoint on 64bit-SPARC
1007N/A *
1007N/A * @run main/othervm -Xcomp -Xbatch -XX:CompileCommand=compileonly,Test6880034,deopt_compiledframe_at_safepoint -XX:+PrintCompilation Test6880034
1007N/A */
1007N/A
1007N/A
1007N/A
1007N/A// This test provokes a deoptimisation at a safepoint.
1007N/A//
1007N/A// It achieves this by compiling the method 'deopt_compiledframe_at_safepoint'
1007N/A// before its first usage at a point in time when a call to the virtual method
1007N/A// A::doSomething() from within 'deopt_compiledframe_at_safepoint' can be
1007N/A// optimised to a static call because class A has no descendants.
1007N/A//
1007N/A// Later, when deopt_compiledframe_at_safepoint() is running, class B which
1007N/A// extends A and overrides the virtual method "doSomething()", is loaded
1007N/A// asynchronously in another thread. This makes the compiled code of
1007N/A// 'deopt_compiledframe_at_safepoint' invalid and triggers a deoptimisation of
1007N/A// the frame where 'deopt_compiledframe_at_safepoint' is running in a
1007N/A// loop.
1007N/A//
1007N/A// The deoptimisation leads to a SIGBUS on 64-bit server VMs on SPARC and to
1007N/A// an incorrect result on 32-bit server VMs on SPARC due to a regression
1007N/A// introduced by the change: "6420645: Create a vm that uses compressed oops
1007N/A// for up to 32gb heapsizes"
1007N/A// (http://hg.openjdk.java.net/jdk7/jdk7/hotspot/rev/ba764ed4b6f2). Further
1007N/A// investigation showed that change 6420645 is not really the root cause of
1007N/A// this error but only reveals a problem with the float register encodings in
1007N/A// sparc.ad which was hidden until now.
1007N/A//
1007N/A// Notice that for this test to fail in jtreg it is crucial that
1007N/A// deopt_compiledframe_at_safepoint() runs in the main thread. Otherwise a
1007N/A// crash in deopt_compiledframe_at_safepoint() will not be detected as a test
1007N/A// failure by jtreg.
1007N/A//
1007N/A// Author: Volker H. Simonis
1007N/A
1007N/Aclass A {
1007N/A public int doSomething() {
1007N/A return 0;
1007N/A }
1007N/A}
1007N/A
1007N/Aclass B extends A {
1007N/A public B() {}
1007N/A // override 'A::doSomething()'
1007N/A public int doSomething() {
1007N/A return 1;
1007N/A }
1007N/A}
1007N/A
1007N/Aclass G {
1007N/A public static volatile A a = new A();
1007N/A
1007N/A // Change 'a' to point to a 'B' object
1007N/A public static void setAtoB() {
1007N/A try {
1007N/A a = (A) ClassLoader.
1007N/A getSystemClassLoader().
1007N/A loadClass("B").
1007N/A getConstructor(new Class[] {}).
1007N/A newInstance(new Object[] {});
1007N/A }
1007N/A catch (Exception e) {
1007N/A System.out.println(e);
1007N/A }
1007N/A }
1007N/A}
1007N/A
1007N/Apublic class Test6880034 {
1007N/A
1007N/A public static volatile boolean is_in_loop = false;
1007N/A public static volatile boolean stop_while_loop = false;
1007N/A
1007N/A public static double deopt_compiledframe_at_safepoint() {
1007N/A // This will be an optimised static call to A::doSomething() until we load "B"
1007N/A int i = G.a.doSomething();
1007N/A
1007N/A // Need more than 16 'double' locals in this frame
1007N/A double local1 = 1;
1007N/A double local2 = 2;
1007N/A double local3 = 3;
1007N/A double local4 = 4;
1007N/A double local5 = 5;
1007N/A double local6 = 6;
1007N/A double local7 = 7;
1007N/A double local8 = 8;
1007N/A
1007N/A long k = 0;
1007N/A // Once we load "B", this method will be made 'not entrant' and deoptimised
1007N/A // at the safepoint which is at the end of this loop.
1007N/A while (!stop_while_loop) {
1007N/A if (k == 1) local1 += i;
1007N/A if (k == 2) local2 += i;
1007N/A if (k == 3) local3 += i;
1007N/A if (k == 4) local4 += i;
1007N/A if (k == 5) local5 += i;
1007N/A if (k == 6) local6 += i;
1007N/A if (k == 7) local7 += i;
1007N/A if (k == 8) local8 += i;
1007N/A
1007N/A // Tell the world that we're now running wild in the loop
1007N/A if (k++ == 20000) is_in_loop = true;
1007N/A }
1007N/A
1007N/A return
1007N/A local1 + local2 + local3 + local4 +
1007N/A local5 + local6 + local7 + local8 + i;
1007N/A }
1007N/A
1007N/A public static void main(String[] args) {
1007N/A
1007N/A // Just to resolve G before we compile deopt_compiledframe_at_safepoint()
1007N/A G g = new G();
1007N/A
1007N/A // Asynchronous thread which will eventually invalidate the code for
1007N/A // deopt_compiledframe_at_safepoint() and therefore triggering a
1007N/A // deoptimisation of that method.
1007N/A new Thread() {
1007N/A public void run() {
1007N/A while (!is_in_loop) {
1007N/A // Wait until the loop is running
1007N/A }
1007N/A // Load class 'B' asynchronously..
1007N/A G.setAtoB();
1007N/A // ..and stop the loop
1007N/A stop_while_loop = true;
1007N/A }
1007N/A }.start();
1007N/A
1007N/A // Run the loop in deopt_compiledframe_at_safepoint()
1007N/A double retVal = deopt_compiledframe_at_safepoint();
1007N/A
1007N/A System.out.println(retVal == 36 ? "OK" : "ERROR : " + retVal);
1007N/A if (retVal != 36) throw new RuntimeException();
1007N/A }
1007N/A}