1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package compiler.c2.cr6589834;
  25 
  26 public class InlinedArrayCloneTestCase implements Runnable {
  27     private Test_ia32 executionController;
  28 
  29     public InlinedArrayCloneTestCase(Test_ia32 executionController) {
  30         this.executionController = executionController;
  31     }
  32 
  33     /*
  34      * Please leave following two methods (invokeArrayClone and verifyArguments)
  35      * static.
  36      *
  37      * It does not really matter if these methods are static or instance,
  38      * original issue could be reproduced in both cases, but if these methods
  39      * are static then it is much easier to understand that reproduced issue
  40      * is actually interpreter's stack corruption.
  41      *
  42      * If these methods are non-static, then interpreter's stack will contain
  43      * invalid 'this' pointer required for instance's method call and
  44      * verifyArguments' call may throw NullPointerException. There was another
  45      * issue w/ NPE after deoptimization addressed by JDK-6833129, so NPE looks
  46      * a little bit confusing.
  47      *
  48      * If these methods are static then after deptimization we'll get incorrect
  49      * arguments values in verifyArguments.
  50      * Something like "2, -1289936896, 3, 4" instead of "1, 2, 3, 4".
  51      * This information tells much more about actual issue comparing to NPE,
  52      * so it's preferable to leave these methods static.
  53      */
  54     private static int verifyArguments(int i1, int i2, LoadedClass[] arr,
  55             int i3, int i4) {
  56         if (!(i1==1 && i2==2 && i3==3 && i4==4)) {
  57             throw new RuntimeException(String.format(
  58                     "Arguments have unexpected values: %d, %d, %d, %d",
  59                     i1, i2, i3, i4));
  60         }
  61         return arr.length;
  62     }
  63 
  64     private static int invokeArrayClone(LoadedClass[] a) {
  65         return InlinedArrayCloneTestCase.verifyArguments(1, 2, a.clone(), 3, 4);
  66     }
  67 
  68     @Override
  69     public void run() {
  70         LoadedClass[] array = executionController.getArray();
  71         int length;
  72 
  73         while (executionController.continueExecution()) {
  74             try {
  75                 length = InlinedArrayCloneTestCase.invokeArrayClone(array);
  76             } catch (Throwable e) {
  77                 e.printStackTrace();
  78                 executionController.setTestFailed();
  79                 return;
  80             }
  81             if (length != array.length) {
  82                 System.out.println(String.format("f(array) returned %d "
  83                         + "instead of %d.", length, array.length));
  84                 executionController.setTestFailed();
  85             }
  86         }
  87     }
  88 }