1 /*
   2  * Copyright (c) 2019 SAP SE. 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  * @bug 8221083
  27  * @requires vm.bits == 64 & vm.opt.final.UseCompressedOops == true
  28  * @summary On ppc64, C1 erroneously emits a 32-bit compare instruction for oop compares.
  29  * @modules java.base/jdk.internal.misc:+open
  30  * @library /test/lib /
  31  * @build sun.hotspot.WhiteBox
  32  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  33  *
  34  * @run main/othervm -Xbatch -XX:-UseTLAB -Xmx4m -XX:+UseSerialGC -XX:HeapBaseMinAddress=0x700000000
  35  *      -XX:CompileCommand=compileonly,compiler.codegen.TestOopCmp::nullTest
  36  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  37  *      --add-exports java.base/jdk.internal.misc=ALL-UNNAMED
  38  *      compiler.codegen.TestOopCmp
  39  * @author volker.simonis@gmail.com
  40  */
  41 
  42 package compiler.codegen;
  43 
  44 import jdk.internal.misc.Unsafe;
  45 import sun.hotspot.WhiteBox;
  46 
  47 public class TestOopCmp {
  48 
  49     private static Object nullObj = null;
  50 
  51     public static boolean nullTest(Object o) {
  52         if (o == nullObj) {
  53             return true;
  54         } else {
  55             return false;
  56         }
  57     }
  58 
  59     public static void main(String args[]) {
  60 
  61         Unsafe UNSAFE = Unsafe.getUnsafe();
  62         WhiteBox WB = WhiteBox.getWhiteBox();
  63         String s = null;
  64 
  65         /*
  66           The test is started with -XX:HeapBaseMinAddress=0x700000000 and a
  67           small heap of only 4mb. This works pretty reliable and at least on
  68           Linux/Windows/Solaris we will get a heap starting at 0x700000000.
  69           The test also runs with -XX:+UseSerialGC which means that we'll get
  70           eden starting at 0x700000000.
  71           The following loop executes 'System.gc()' followed by a nested loop
  72           which repeatedly allocates a string.
  73           If eden starts at 0x700000000 we quickly get an object allcoated at
  74           address 0x700000000 and exit both loops.
  75           In the worst case when our heap doesn't start at 0x700000000 or if
  76           we don't get an object at 0x700000000 we will execute 10 system GCs
  77           and 10*1024 allocations which shouldn't do any harm.
  78          */
  79         for (int i = 0; i < 10; i++) {
  80             System.gc();
  81             for (int j = 0; j < 1024; j++) {
  82                 s = new String("I'm not null!!!");
  83                 if (WB.getObjectAddress(s) == 0x700000000L) break;
  84             }
  85             if (WB.getObjectAddress(s) == 0x700000000L) {
  86                 System.out.println("Got object at address 0x700000000");
  87                 break;
  88             }
  89         }
  90 
  91         /*
  92           We call 'nullTest()' with the newly allocated String object. If it was
  93           allocated at 0x700000000, its 32 least-significant bits will be 0 and a
  94           32-bit comparison with 'nullObj' (which is 'null') will yield true and
  95           result in a test failure.
  96           If the code generated for 'nullTest()' correctly performs a 64-bit
  97           comparison or if we didn't manage to allcoate 's' at 0x700000000 the
  98           test will always succeed.
  99          */
 100         for (int i = 0; i < 30_000; i++) {
 101             if (nullTest(s)) throw new NullPointerException(s);
 102         }
 103     }
 104 }