1 /*
   2  * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 6880034
  27  * @summary SIGBUS during deoptimisation at a safepoint on 64bit-SPARC
  28  *
  29  * @run main/othervm -Xcomp -Xbatch -XX:CompileCommand=compileonly,Test6880034,deopt_compiledframe_at_safepoint -XX:+PrintCompilation Test6880034
  30  */
  31 
  32 
  33 
  34 // This test provokes a deoptimisation at a safepoint. 
  35 //
  36 // It achieves this by compiling the method 'deopt_compiledframe_at_safepoint'
  37 // before its first usage at a point in time when a call to the virtual method
  38 // A::doSomething() from within 'deopt_compiledframe_at_safepoint' can be
  39 // optimised to a static call because class A has no descendants. 
  40 // 
  41 // Later, when deopt_compiledframe_at_safepoint() is running, class B which
  42 // extends A and overrides the virtual method "doSomething()", is loaded
  43 // asynchronously in another thread.  This makes the compiled code of
  44 // 'deopt_compiledframe_at_safepoint' invalid and triggers a deoptimisation of
  45 // the frame where 'deopt_compiledframe_at_safepoint' is running in a
  46 // loop. 
  47 //
  48 // The deoptimisation leads to a SIGBUS on 64-bit server VMs on SPARC and to
  49 // an incorrect result on 32-bit server VMs on SPARC due to a regression
  50 // introduced by the change: "6420645: Create a vm that uses compressed oops
  51 // for up to 32gb heapsizes"
  52 // (http://hg.openjdk.java.net/jdk7/jdk7/hotspot/rev/ba764ed4b6f2).  Further
  53 // investigation showed that change 6420645 is not really the root cause of
  54 // this error but only reveals a problem with the float register encodings in
  55 // sparc.ad which was hidden until now.
  56 // 
  57 // Notice that for this test to fail in jtreg it is crucial that
  58 // deopt_compiledframe_at_safepoint() runs in the main thread. Otherwise a
  59 // crash in deopt_compiledframe_at_safepoint() will not be detected as a test
  60 // failure by jtreg.
  61 // 
  62 // Author: Volker H. Simonis
  63 
  64 class A {
  65   public int doSomething() {
  66     return 0;
  67   }
  68 }
  69 
  70 class B extends A {
  71   public B() {}
  72   // override 'A::doSomething()'
  73   public int doSomething() {
  74     return 1;
  75   }
  76 }
  77 
  78 class G {
  79   public static volatile A a = new A();
  80 
  81   // Change 'a' to point to a 'B' object
  82   public static void setAtoB() {
  83     try {
  84       a =  (A) ClassLoader.
  85         getSystemClassLoader().
  86         loadClass("B").
  87         getConstructor(new Class[] {}).
  88         newInstance(new Object[] {});
  89     }
  90     catch (Exception e) {
  91       System.out.println(e);
  92     }
  93   }
  94 }
  95 
  96 public class Test6880034 {
  97   
  98   public static volatile boolean is_in_loop = false;
  99   public static volatile boolean stop_while_loop = false;
 100 
 101   public static double deopt_compiledframe_at_safepoint() {
 102     // This will be an optimised static call to A::doSomething() until we load "B"
 103     int i = G.a.doSomething();
 104 
 105     // Need more than 16 'double' locals in this frame
 106     double local1 = 1;
 107     double local2 = 2;
 108     double local3 = 3;
 109     double local4 = 4;
 110     double local5 = 5;
 111     double local6 = 6;
 112     double local7 = 7;
 113     double local8 = 8;
 114 
 115     long k = 0;
 116     // Once we load "B", this method will be made 'not entrant' and deoptimised
 117     // at the safepoint which is at the end of this loop.
 118     while (!stop_while_loop) { 
 119       if (k ==  1) local1 += i;
 120       if (k ==  2) local2 += i;
 121       if (k ==  3) local3 += i;
 122       if (k ==  4) local4 += i;
 123       if (k ==  5) local5 += i;
 124       if (k ==  6) local6 += i;
 125       if (k ==  7) local7 += i;
 126       if (k ==  8) local8 += i;
 127 
 128       // Tell the world that we're now running wild in the loop
 129       if (k++ == 20000) is_in_loop = true;
 130     } 
 131 
 132     return 
 133       local1 + local2 + local3 + local4 + 
 134       local5 + local6 + local7 + local8 + i; 
 135   }
 136 
 137   public static void main(String[] args) {
 138 
 139     // Just to resolve G before we compile deopt_compiledframe_at_safepoint()
 140     G g = new G();
 141 
 142     // Asynchronous thread which will eventually invalidate the code for
 143     // deopt_compiledframe_at_safepoint() and therefore triggering a
 144     // deoptimisation of that method.
 145     new Thread() {
 146       public void run() {
 147         while (!is_in_loop) { 
 148           // Wait until the loop is running
 149         }
 150         // Load class 'B' asynchronously..
 151         G.setAtoB();        
 152         // ..and stop the loop
 153         stop_while_loop = true;
 154       }
 155     }.start();
 156 
 157     // Run the loop in deopt_compiledframe_at_safepoint()
 158     double retVal = deopt_compiledframe_at_safepoint();
 159 
 160     System.out.println(retVal == 36 ? "OK" : "ERROR : " + retVal);
 161     if (retVal != 36) throw new RuntimeException();
 162   }
 163 }