1 /*
   2  * Copyright (c) 2011, 2018, 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 /*
  25  * @test
  26  * @key stress gc
  27  *
  28  * @summary converted from VM Testbase gc/gctests/GcPointerCheckTest.
  29  * VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit]
  30  * VM Testbase readme:
  31  * DESCRIPTION
  32  * Test that no pointers are broken.
  33  *
  34  * COMMENTS
  35  * This test was ported from JRockit test suite.
  36  *
  37  * @library /vmTestbase
  38  *          /test/lib
  39  * @run driver jdk.test.lib.FileInstaller . .
  40  * @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.GcPointerCheckTest.GcPointerCheckTest
  41  */
  42 
  43 package gc.gctests.GcPointerCheckTest;
  44 
  45 import nsk.share.TestFailure;
  46 import nsk.share.gc.GC;
  47 import nsk.share.gc.ThreadedGCTest;
  48 import nsk.share.test.ExecutionController;
  49 
  50 /**
  51  * Test that no pointers are broken.
  52  */
  53 public class GcPointerCheckTest extends ThreadedGCTest {
  54 
  55     @Override
  56     protected Runnable createRunnable(int i) {
  57         return new Test();
  58     }
  59 
  60     class Test implements Runnable {
  61 
  62         /**
  63          * Helper class for linking objects together.
  64          */
  65         private class PointerHelper {
  66 
  67             public PointerHelper next;
  68         }
  69         private PointerHelper first;
  70         ExecutionController stresser;
  71 
  72         @Override
  73         public void run() {
  74             if (stresser == null) {
  75                 stresser = getExecutionController();
  76             }
  77             while (stresser.continueExecution()) {
  78                 testGcPointers();
  79             }
  80         }
  81 
  82         /**
  83          * Create a lot of objects and link them together, then verify
  84          * that the pointers are pointing to the correct type of objects.
  85          *
  86          * @return success if all references points to the correct type
  87          * of object.
  88          */
  89         public void testGcPointers() {
  90 
  91             int innerIters = 1;
  92             int outerIters = 200;
  93 
  94             PointerHelper tmp1;
  95             PointerHelper tmp2;
  96 
  97             while (outerIters > 0) {
  98                 int i = 0;
  99                 tmp1 = new PointerHelper();
 100                 this.first = tmp1;
 101 
 102                 while (i != innerIters) {
 103                     i++;
 104                     tmp2 = new PointerHelper();
 105                     tmp1.next = tmp2;
 106                     tmp1 = tmp2;
 107                     tmp2 = new PointerHelper();
 108                 }
 109 
 110                 outerIters--;
 111 
 112                 if (!checkRefs()) {
 113                     throw new TestFailure("Some references were bad");
 114                 }
 115             }
 116         }
 117 
 118         private boolean checkRefs() {
 119             PointerHelper iter = this.first;
 120 
 121             for (int i = 0; iter != null; iter = iter.next) {
 122                 i++;
 123 
 124                 if (iter.getClass() != PointerHelper.class) {
 125                     //("GC causer bad ref on " + i);
 126                     return false;
 127                 }
 128             }
 129 
 130             return true;
 131         }
 132     }
 133 
 134     public static void main(String[] args) {
 135         GC.runTest(new GcPointerCheckTest(), args);
 136     }
 137 }