1 /*
   2  * Copyright (c) 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 import sun.hotspot.WhiteBox;
  26 
  27 //
  28 // Help test archived box cache consistency.
  29 //
  30 // Takes two arguments:
  31 // 0: the expected maximum value expected to be archived
  32 // 1: if the values are expected to be retrieved from the archive or not
  33 //    (only applies to IntegerCache; other caches should always be mapped
  34 //    from archive)
  35 //
  36 public class CheckIntegerCacheApp {
  37     static WhiteBox wb;
  38 
  39     public static void main(String[] args) throws Exception {
  40         wb = WhiteBox.getWhiteBox();
  41 
  42         if (!wb.areOpenArchiveHeapObjectsMapped()) {
  43             System.out.println("This may happen during normal operation. Test Skipped.");
  44             return;
  45         }
  46 
  47         if (args.length != 2) {
  48             throw new RuntimeException(
  49                     "FAILED. Incorrect argument length: " + args.length);
  50         }
  51 
  52         boolean archivedExpected = Boolean.parseBoolean(args[1]);
  53 
  54         // Base JLS compliance check
  55         for (int i = -128; i <= 127; i++) {
  56             if (Integer.valueOf(i) != Integer.valueOf(i)) {
  57                 throw new RuntimeException(
  58                         "FAILED. All values in range [-128, 127] should be interned in cache: " + i);
  59             }
  60             if (Byte.valueOf((byte)i) != Byte.valueOf((byte)i)) {
  61                 throw new RuntimeException(
  62                         "FAILED. All Byte values in range [-128, 127] should be interned in cache: " + (byte)i);
  63             }
  64             if (Short.valueOf((short)i) != Short.valueOf((short)i)) {
  65                 throw new RuntimeException(
  66                         "FAILED. All Short values in range [-128, 127] should be interned in cache: " + (byte)i);
  67             }
  68             if (Long.valueOf(i) != Long.valueOf(i)) {
  69                 throw new RuntimeException(
  70                         "FAILED. All Long values in range [-128, 127] should be interned in cache: " + i);
  71             }
  72             checkArchivedAsExpected(archivedExpected, Integer.valueOf(i));
  73             checkArchivedAsExpected(true, Byte.valueOf((byte)i));
  74             checkArchivedAsExpected(true, Short.valueOf((short)i));
  75             checkArchivedAsExpected(true, Long.valueOf(i));
  76 
  77             // Character cache only values 0 through 127
  78             if (i >= 0) {
  79                 if (Character.valueOf((char)i) != Character.valueOf((char)i)) {
  80                     throw new RuntimeException(
  81                             "FAILED. All Character values in range [0, 127] should be interned in cache: " + i);
  82                 }
  83                 checkArchivedAsExpected(true, Character.valueOf((char)i));
  84             }
  85         }
  86 
  87         int high = Integer.parseInt(args[0]);
  88         if (Integer.valueOf(high) != Integer.valueOf(high)) {
  89             throw new RuntimeException(
  90                     "FAILED. Value expected to be retrieved from cache: " + high);
  91         }
  92         checkArchivedAsExpected(archivedExpected, Integer.valueOf(high));
  93 
  94         if (Integer.valueOf(high + 1) == Integer.valueOf(high + 1)) {
  95             throw new RuntimeException(
  96                     "FAILED. Value not expected to be retrieved from cache: " + high);
  97         }
  98         checkArchivedAsExpected(false, Integer.valueOf(high + 1));
  99         checkArchivedAsExpected(false, Short.valueOf((short)128));
 100         checkArchivedAsExpected(false, Long.valueOf(128));
 101         checkArchivedAsExpected(false, Character.valueOf((char)128));
 102     }
 103 
 104     private static void checkArchivedAsExpected(boolean archivedExpected, Object value) {
 105         if (archivedExpected) {
 106             if (!wb.isShared(value)) {
 107                 throw new RuntimeException(
 108                         "FAILED. Value expected to be archived: " + value +
 109                         " of type " + value.getClass().getName());
 110             }
 111         } else {
 112             if (wb.isShared(value)) {
 113                 throw new RuntimeException(
 114                         "FAILED. Value not expected to be archived: " + value +
 115                         " of type " + value.getClass().getName());
 116             }
 117         }
 118     }
 119 }